parent
5a57501164
commit
3165928ebd
@ -1,10 +1,11 @@ |
||||
|
||||
import sys |
||||
sys.path.append('../') |
||||
import numpy as np |
||||
|
||||
import network_graph as ng |
||||
import sample_path as sp |
||||
import set_of_cims as sofc |
||||
import sets_of_cims_container as acims |
||||
import structure_graph.network_graph as ng |
||||
import structure_graph.sample_path as sp |
||||
import structure_graph.set_of_cims as sofc |
||||
import structure_graph.sets_of_cims_container as acims |
||||
|
||||
|
||||
class ParametersEstimator: |
@ -0,0 +1,96 @@ |
||||
import sys |
||||
sys.path.append('../') |
||||
import itertools |
||||
import json |
||||
import typing |
||||
|
||||
import networkx as nx |
||||
import numpy as np |
||||
from networkx.readwrite import json_graph |
||||
|
||||
from abc import ABC |
||||
|
||||
import utility.cache as ch |
||||
import structure_graph.conditional_intensity_matrix as condim |
||||
import structure_graph.network_graph as ng |
||||
import estimators.parameters_estimator as pe |
||||
import structure_graph.sample_path as sp |
||||
import structure_graph.structure as st |
||||
|
||||
|
||||
class StructureEstimator(ABC): |
||||
""" |
||||
Has the task of estimating the network structure given the trajectories in samplepath. |
||||
|
||||
:sample_path: the sample_path object containing the trajectories and the real structure |
||||
|
||||
:nodes: the nodes labels |
||||
:nodes_vals: the nodes cardinalities |
||||
:nodes_indxs: the nodes indexes |
||||
:complete_graph: the complete directed graph built using the nodes labels in nodes |
||||
:cache: the cache object |
||||
""" |
||||
|
||||
def __init__(self, sample_path: sp.SamplePath): |
||||
self.sample_path = sample_path |
||||
self.nodes = np.array(self.sample_path.structure.nodes_labels) |
||||
self.nodes_vals = self.sample_path.structure.nodes_values |
||||
self.nodes_indxs = self.sample_path.structure.nodes_indexes |
||||
self.complete_graph = self.build_complete_graph(self.sample_path.structure.nodes_labels) |
||||
self.cache = ch.Cache() |
||||
|
||||
def build_complete_graph(self, node_ids: typing.List): |
||||
""" |
||||
Builds a complete directed graph (no self loops) given the nodes labels in the list node_ids: |
||||
|
||||
Parameters: |
||||
node_ids: the list of nodes labels |
||||
Returns: |
||||
a complete Digraph Object |
||||
""" |
||||
complete_graph = nx.DiGraph() |
||||
complete_graph.add_nodes_from(node_ids) |
||||
complete_graph.add_edges_from(itertools.permutations(node_ids, 2)) |
||||
return complete_graph |
||||
|
||||
|
||||
def generate_possible_sub_sets_of_size(self, u: typing.List, size: int, parent_label: str): |
||||
""" |
||||
Creates a list containing all possible subsets of the list u of size size, |
||||
that do not contains a the node identified by parent_label. |
||||
|
||||
Parameters: |
||||
u: the list of nodes |
||||
size: the size of the subsets |
||||
parent_label: the nodes to exclude in the subsets generation |
||||
Returns: |
||||
a Map Object containing a list of lists |
||||
|
||||
""" |
||||
list_without_test_parent = u[:] |
||||
list_without_test_parent.remove(parent_label) |
||||
return map(list, itertools.combinations(list_without_test_parent, size)) |
||||
|
||||
def save_results(self): |
||||
""" |
||||
Save the estimated Structure to a .json file |
||||
|
||||
Parameters: |
||||
void |
||||
Returns: |
||||
void |
||||
""" |
||||
res = json_graph.node_link_data(self.complete_graph) |
||||
name = self.sample_path.importer.file_path.rsplit('/',1)[-1] |
||||
#print(name) |
||||
name = '../results_' + name |
||||
with open(name, 'w+') as f: |
||||
json.dump(res, f) |
||||
|
||||
|
||||
def remove_diagonal_elements(self, matrix): |
||||
m = matrix.shape[0] |
||||
strided = np.lib.stride_tricks.as_strided |
||||
s0, s1 = matrix.strides |
||||
return strided(matrix.ravel()[1:], shape=(m - 1, m), strides=(s0 + s1, s1)).reshape(m, -1) |
||||
|
@ -1,6 +1,9 @@ |
||||
from abc import ABC, abstractmethod |
||||
|
||||
import abstract_importer as ai |
||||
import sys |
||||
sys.path.append('../') |
||||
|
||||
import utility.abstract_importer as ai |
||||
|
||||
|
||||
class AbstractSamplePath(ABC): |
@ -1,5 +1,7 @@ |
||||
import numpy as np |
||||
|
||||
import sys |
||||
sys.path.append('../') |
||||
|
||||
class ConditionalIntensityMatrix: |
||||
""" |
@ -1,7 +1,11 @@ |
||||
import abstract_sample_path as asam |
||||
import json_importer as imp |
||||
import structure as st |
||||
import trajectory as tr |
||||
import sys |
||||
sys.path.append('../') |
||||
|
||||
import structure_graph.abstract_sample_path as asam |
||||
import utility.json_importer as imp |
||||
import structure_graph.structure as st |
||||
import structure_graph.trajectory as tr |
||||
|
||||
|
||||
|
||||
class SamplePath(asam.AbstractSamplePath): |
@ -1,8 +1,11 @@ |
||||
import sys |
||||
sys.path.append('../') |
||||
|
||||
import typing |
||||
|
||||
import numpy as np |
||||
|
||||
import conditional_intensity_matrix as cim |
||||
import structure_graph.conditional_intensity_matrix as cim |
||||
|
||||
|
||||
class SetOfCims: |
@ -1,4 +1,7 @@ |
||||
import set_of_cims as socim |
||||
import sys |
||||
sys.path.append('../') |
||||
|
||||
import structure_graph.set_of_cims as socim |
||||
|
||||
|
||||
class SetsOfCimsContainer: |
@ -1,3 +1,6 @@ |
||||
import sys |
||||
sys.path.append('../') |
||||
|
||||
import typing as ty |
||||
|
||||
import numpy as np |
@ -1,3 +1,5 @@ |
||||
import sys |
||||
sys.path.append('../') |
||||
|
||||
import numpy as np |
||||
|
@ -1,6 +1,9 @@ |
||||
import sys |
||||
sys.path.append('../') |
||||
|
||||
import typing |
||||
|
||||
import set_of_cims as sofc |
||||
import structure_graph.set_of_cims as sofc |
||||
|
||||
|
||||
class Cache: |
@ -1,10 +1,12 @@ |
||||
import sys |
||||
sys.path.append('../') |
||||
|
||||
import json |
||||
import typing |
||||
|
||||
import pandas as pd |
||||
|
||||
import abstract_importer as ai |
||||
import utility.abstract_importer as ai |
||||
|
||||
|
||||
class JsonImporter(ai.AbstractImporter): |
@ -1,22 +1,22 @@ |
||||
import sys |
||||
sys.path.append("../classes/") |
||||
sys.path.append("../../classes/") |
||||
import unittest |
||||
import numpy as np |
||||
import glob |
||||
import os |
||||
|
||||
import network_graph as ng |
||||
import sample_path as sp |
||||
import set_of_cims as sofc |
||||
import parameters_estimator as pe |
||||
import json_importer as ji |
||||
import structure_graph.network_graph as ng |
||||
import structure_graph.sample_path as sp |
||||
import structure_graph.set_of_cims as sofc |
||||
import estimators.parameters_estimator as pe |
||||
import utility.json_importer as ji |
||||
|
||||
|
||||
class TestParametersEstimatior(unittest.TestCase): |
||||
|
||||
@classmethod |
||||
def setUpClass(cls) -> None: |
||||
cls.read_files = glob.glob(os.path.join('../data', "*.json")) |
||||
cls.read_files = glob.glob(os.path.join('../../data', "*.json")) |
||||
cls.importer = ji.JsonImporter(cls.read_files[0], 'samples', 'dyn.str', 'variables', 'Time', 'Name') |
||||
cls.s1 = sp.SamplePath(cls.importer) |
||||
cls.s1.build_trajectories() |
@ -1,8 +1,8 @@ |
||||
import sys |
||||
sys.path.append("../classes/") |
||||
sys.path.append("../../classes/") |
||||
import unittest |
||||
import numpy as np |
||||
import structure as st |
||||
import structure_graph.structure as st |
||||
|
||||
|
||||
class TestStructure(unittest.TestCase): |
@ -1,9 +1,9 @@ |
||||
import sys |
||||
sys.path.append("../classes/") |
||||
sys.path.append("../../classes/") |
||||
import unittest |
||||
import numpy as np |
||||
|
||||
import conditional_intensity_matrix as cim |
||||
import structure_graph.conditional_intensity_matrix as cim |
||||
|
||||
|
||||
class TestConditionalIntensityMatrix(unittest.TestCase): |
@ -1,19 +1,19 @@ |
||||
import sys |
||||
sys.path.append("../classes/") |
||||
sys.path.append("../../classes/") |
||||
import unittest |
||||
import glob |
||||
import os |
||||
import json_importer as ji |
||||
import sample_path as sp |
||||
import trajectory as tr |
||||
import structure as st |
||||
import utility.json_importer as ji |
||||
import structure_graph.sample_path as sp |
||||
import structure_graph.trajectory as tr |
||||
import structure_graph.structure as st |
||||
|
||||
|
||||
class TestSamplePath(unittest.TestCase): |
||||
|
||||
@classmethod |
||||
def setUpClass(cls) -> None: |
||||
cls.read_files = glob.glob(os.path.join('../data', "*.json")) |
||||
cls.read_files = glob.glob(os.path.join('../../data', "*.json")) |
||||
cls.importer = ji.JsonImporter(cls.read_files[0], 'samples', 'dyn.str', 'variables', 'Time', 'Name') |
||||
|
||||
def test_init(self): |
@ -1,10 +1,10 @@ |
||||
import sys |
||||
sys.path.append("../classes/") |
||||
sys.path.append("../../classes/") |
||||
import unittest |
||||
import numpy as np |
||||
import itertools |
||||
|
||||
import set_of_cims as soci |
||||
import structure_graph.set_of_cims as soci |
||||
|
||||
|
||||
class TestSetOfCims(unittest.TestCase): |
@ -1,8 +1,8 @@ |
||||
import sys |
||||
sys.path.append("../classes/") |
||||
sys.path.append("../../classes/") |
||||
import unittest |
||||
import set_of_cims as sc |
||||
import sets_of_cims_container as scc |
||||
import structure_graph.set_of_cims as sc |
||||
import structure_graph.sets_of_cims_container as scc |
||||
|
||||
|
||||
class TestSetsOfCimsContainer(unittest.TestCase): |
@ -1,9 +1,9 @@ |
||||
import sys |
||||
sys.path.append("../classes/") |
||||
sys.path.append("../../classes/") |
||||
import unittest |
||||
import numpy as np |
||||
|
||||
import trajectory as tr |
||||
import structure_graph.trajectory as tr |
||||
|
||||
|
||||
class TestTrajectory(unittest.TestCase): |
@ -1,10 +1,10 @@ |
||||
import sys |
||||
sys.path.append("../classes/") |
||||
sys.path.append("../../classes/") |
||||
import unittest |
||||
import numpy as np |
||||
|
||||
import cache as ch |
||||
import set_of_cims as soci |
||||
import utility.cache as ch |
||||
import structure_graph.set_of_cims as soci |
||||
|
||||
|
||||
class TestCache(unittest.TestCase): |
Reference in new issue