From 3165928ebdff1cf143c7d1f517a562fed3779b79 Mon Sep 17 00:00:00 2001 From: Luca Moretti Date: Mon, 12 Oct 2020 15:58:59 +0200 Subject: [PATCH] Refactoring classes Structure_estimator and organized files in folders --- main_package/basic_main.py | 10 +- .../{ => estimators}/fam_score_calculator.py | 9 +- .../{ => estimators}/parameters_estimator.py | 11 ++- .../structure_constraint_based_estimator.py} | 84 +++------------- .../classes/estimators/structure_estimator.py | 96 +++++++++++++++++++ .../structure_score_based_estimator.py | 74 +++----------- .../abstract_sample_path.py | 5 +- .../conditional_intensity_matrix.py | 2 + .../{ => structure_graph}/network_graph.py | 2 + .../{ => structure_graph}/sample_path.py | 12 ++- .../{ => structure_graph}/set_of_cims.py | 5 +- .../sets_of_cims_container.py | 5 +- .../{ => structure_graph}/structure.py | 3 + .../{ => structure_graph}/trajectory.py | 2 + .../{ => utility}/abstract_importer.py | 0 main_package/classes/{ => utility}/cache.py | 5 +- .../classes/{ => utility}/decorators.py | 0 .../classes/{ => utility}/json_importer.py | 4 +- .../test_parameters_estimator.py | 14 +-- .../tests/{ => estimators}/test_structure.py | 4 +- ...t_structure_constraint_based_estimator.py} | 24 ++--- .../test_structure_score_based_estimator.py | 12 +-- .../tests/{ => structure_graph}/test_cim.py | 4 +- .../test_networkgraph.py | 10 +- .../{ => structure_graph}/test_sample_path.py | 12 +-- .../{ => structure_graph}/test_setofcims.py | 4 +- .../test_sets_of_cims_container.py | 6 +- .../{ => structure_graph}/test_trajectory.py | 4 +- .../tests/{ => utility}/test_cache.py | 6 +- .../tests/{ => utility}/test_json_importer.py | 6 +- 30 files changed, 228 insertions(+), 207 deletions(-) rename main_package/classes/{ => estimators}/fam_score_calculator.py (97%) rename main_package/classes/{ => estimators}/parameters_estimator.py (96%) rename main_package/classes/{structure_estimator.py => estimators/structure_constraint_based_estimator.py} (78%) create mode 100644 main_package/classes/estimators/structure_estimator.py rename main_package/classes/{ => estimators}/structure_score_based_estimator.py (77%) rename main_package/classes/{ => structure_graph}/abstract_sample_path.py (92%) rename main_package/classes/{ => structure_graph}/conditional_intensity_matrix.py (97%) rename main_package/classes/{ => structure_graph}/network_graph.py (99%) rename main_package/classes/{ => structure_graph}/sample_path.py (92%) rename main_package/classes/{ => structure_graph}/set_of_cims.py (97%) rename main_package/classes/{ => structure_graph}/sets_of_cims_container.py (93%) rename main_package/classes/{ => structure_graph}/structure.py (98%) rename main_package/classes/{ => structure_graph}/trajectory.py (98%) rename main_package/classes/{ => utility}/abstract_importer.py (100%) rename main_package/classes/{ => utility}/cache.py (94%) rename main_package/classes/{ => utility}/decorators.py (100%) rename main_package/classes/{ => utility}/json_importer.py (99%) rename main_package/tests/{ => estimators}/test_parameters_estimator.py (90%) rename main_package/tests/{ => estimators}/test_structure.py (97%) rename main_package/tests/{test_structure_estimator.py => estimators/test_structure_constraint_based_estimator.py} (82%) rename main_package/tests/{ => estimators}/test_structure_score_based_estimator.py (87%) rename main_package/tests/{ => structure_graph}/test_cim.py (95%) rename main_package/tests/{ => structure_graph}/test_networkgraph.py (98%) rename main_package/tests/{ => structure_graph}/test_sample_path.py (76%) rename main_package/tests/{ => structure_graph}/test_setofcims.py (98%) rename main_package/tests/{ => structure_graph}/test_sets_of_cims_container.py (83%) rename main_package/tests/{ => structure_graph}/test_trajectory.py (96%) rename main_package/tests/{ => utility}/test_cache.py (94%) rename main_package/tests/{ => utility}/test_json_importer.py (98%) diff --git a/main_package/basic_main.py b/main_package/basic_main.py index b46eb9a..f57ccb1 100644 --- a/main_package/basic_main.py +++ b/main_package/basic_main.py @@ -4,11 +4,11 @@ import os import sys sys.path.append("./classes/") -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 def main(): diff --git a/main_package/classes/fam_score_calculator.py b/main_package/classes/estimators/fam_score_calculator.py similarity index 97% rename from main_package/classes/fam_score_calculator.py rename to main_package/classes/estimators/fam_score_calculator.py index 79638f2..5159da0 100644 --- a/main_package/classes/fam_score_calculator.py +++ b/main_package/classes/estimators/fam_score_calculator.py @@ -1,3 +1,6 @@ +import sys +sys.path.append('../') + import itertools import json @@ -12,9 +15,9 @@ from math import log from scipy.special import loggamma from random import choice -import set_of_cims as soCims -import network_graph as net_graph -import conditional_intensity_matrix as cim_class +import structure_graph.set_of_cims as soCims +import structure_graph.network_graph as net_graph +import structure_graph.conditional_intensity_matrix as cim_class ''' diff --git a/main_package/classes/parameters_estimator.py b/main_package/classes/estimators/parameters_estimator.py similarity index 96% rename from main_package/classes/parameters_estimator.py rename to main_package/classes/estimators/parameters_estimator.py index 4f3f91a..66484ea 100644 --- a/main_package/classes/parameters_estimator.py +++ b/main_package/classes/estimators/parameters_estimator.py @@ -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: diff --git a/main_package/classes/structure_estimator.py b/main_package/classes/estimators/structure_constraint_based_estimator.py similarity index 78% rename from main_package/classes/structure_estimator.py rename to main_package/classes/estimators/structure_constraint_based_estimator.py index 719cd0e..5cbb3d8 100644 --- a/main_package/classes/structure_estimator.py +++ b/main_package/classes/estimators/structure_constraint_based_estimator.py @@ -1,4 +1,5 @@ - +import sys +sys.path.append('../') import itertools import json import typing @@ -9,52 +10,28 @@ from networkx.readwrite import json_graph from scipy.stats import chi2 as chi2_dist from scipy.stats import f as f_dist -import cache as ch -import conditional_intensity_matrix as condim -import network_graph as ng -import parameters_estimator as pe -import sample_path as sp -import structure as st +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 estimators.structure_estimator as se +import structure_graph.sample_path as sp +import structure_graph.structure as st -class StructureEstimator: +class StructureConstraintBasedEstimator(se.StructureEstimator): """ 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 :exp_test_sign: the significance level for the exponential Hp test :chi_test_alfa: the significance level for the chi Hp test - - :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, exp_test_alfa: float, chi_test_alfa: float): - 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) + super().__init__(sample_path) self.exp_test_sign = exp_test_alfa self.chi_test_alfa = chi_test_alfa - 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 complete_test(self, test_parent: str, test_child: str, parent_set: typing.List, child_states_numb: int, tot_vars_count: int): @@ -260,23 +237,6 @@ class StructureEstimator: b += 1 self.cache.clear() - 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 ctpc_algorithm(self): """ Compute the CTPC algorithm. @@ -289,26 +249,4 @@ class StructureEstimator: total_vars_numb = self.sample_path.total_variables_count [ctpc_algo(n, total_vars_numb) for n in self.nodes] - 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) diff --git a/main_package/classes/estimators/structure_estimator.py b/main_package/classes/estimators/structure_estimator.py new file mode 100644 index 0000000..621f9ec --- /dev/null +++ b/main_package/classes/estimators/structure_estimator.py @@ -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) + diff --git a/main_package/classes/structure_score_based_estimator.py b/main_package/classes/estimators/structure_score_based_estimator.py similarity index 77% rename from main_package/classes/structure_score_based_estimator.py rename to main_package/classes/estimators/structure_score_based_estimator.py index 74a839f..1816f0b 100644 --- a/main_package/classes/structure_score_based_estimator.py +++ b/main_package/classes/estimators/structure_score_based_estimator.py @@ -1,4 +1,5 @@ - +import sys +sys.path.append('../') import itertools import json import typing @@ -10,15 +11,18 @@ from networkx.readwrite import json_graph from random import choice import copy -import cache as ch -import conditional_intensity_matrix as condim -import network_graph as ng -import parameters_estimator as pe -import sample_path as sp -import structure as st -import fam_score_calculator as fam_score +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 estimators.structure_estimator as se +import structure_graph.sample_path as sp +import structure_graph.structure as st +import estimators.fam_score_calculator as fam_score + +from utility.decorators import timing + -from decorators import timing #from numba import njit @@ -32,41 +36,15 @@ from multiprocessing import Pool #TODO: Evaluate if it could be better to change list_edges to set for improve the performance ''' -class StructureScoreBasedEstimator: +class StructureScoreBasedEstimator(se.StructureEstimator): """ Has the task of estimating the network structure given the trajectories in samplepath by using a score based approach. - :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 + super().__init__(sample_path) @timing @@ -241,26 +219,4 @@ class StructureScoreBasedEstimator: 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) diff --git a/main_package/classes/abstract_sample_path.py b/main_package/classes/structure_graph/abstract_sample_path.py similarity index 92% rename from main_package/classes/abstract_sample_path.py rename to main_package/classes/structure_graph/abstract_sample_path.py index f2f058f..36965b0 100644 --- a/main_package/classes/abstract_sample_path.py +++ b/main_package/classes/structure_graph/abstract_sample_path.py @@ -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): diff --git a/main_package/classes/conditional_intensity_matrix.py b/main_package/classes/structure_graph/conditional_intensity_matrix.py similarity index 97% rename from main_package/classes/conditional_intensity_matrix.py rename to main_package/classes/structure_graph/conditional_intensity_matrix.py index 927c257..c4646d7 100644 --- a/main_package/classes/conditional_intensity_matrix.py +++ b/main_package/classes/structure_graph/conditional_intensity_matrix.py @@ -1,5 +1,7 @@ import numpy as np +import sys +sys.path.append('../') class ConditionalIntensityMatrix: """ diff --git a/main_package/classes/network_graph.py b/main_package/classes/structure_graph/network_graph.py similarity index 99% rename from main_package/classes/network_graph.py rename to main_package/classes/structure_graph/network_graph.py index 7ed2063..d0d0992 100644 --- a/main_package/classes/network_graph.py +++ b/main_package/classes/structure_graph/network_graph.py @@ -4,6 +4,8 @@ import typing import networkx as nx import numpy as np +import sys +sys.path.append('../') class NetworkGraph: """ diff --git a/main_package/classes/sample_path.py b/main_package/classes/structure_graph/sample_path.py similarity index 92% rename from main_package/classes/sample_path.py rename to main_package/classes/structure_graph/sample_path.py index e365e85..018a6d0 100644 --- a/main_package/classes/sample_path.py +++ b/main_package/classes/structure_graph/sample_path.py @@ -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): diff --git a/main_package/classes/set_of_cims.py b/main_package/classes/structure_graph/set_of_cims.py similarity index 97% rename from main_package/classes/set_of_cims.py rename to main_package/classes/structure_graph/set_of_cims.py index d45b1f5..22ac8eb 100644 --- a/main_package/classes/set_of_cims.py +++ b/main_package/classes/structure_graph/set_of_cims.py @@ -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: diff --git a/main_package/classes/sets_of_cims_container.py b/main_package/classes/structure_graph/sets_of_cims_container.py similarity index 93% rename from main_package/classes/sets_of_cims_container.py rename to main_package/classes/structure_graph/sets_of_cims_container.py index cf1cc82..6fa6834 100644 --- a/main_package/classes/sets_of_cims_container.py +++ b/main_package/classes/structure_graph/sets_of_cims_container.py @@ -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: diff --git a/main_package/classes/structure.py b/main_package/classes/structure_graph/structure.py similarity index 98% rename from main_package/classes/structure.py rename to main_package/classes/structure_graph/structure.py index 010c639..d739d7f 100644 --- a/main_package/classes/structure.py +++ b/main_package/classes/structure_graph/structure.py @@ -1,3 +1,6 @@ +import sys +sys.path.append('../') + import typing as ty import numpy as np diff --git a/main_package/classes/trajectory.py b/main_package/classes/structure_graph/trajectory.py similarity index 98% rename from main_package/classes/trajectory.py rename to main_package/classes/structure_graph/trajectory.py index 1c5d9ea..518ae25 100644 --- a/main_package/classes/trajectory.py +++ b/main_package/classes/structure_graph/trajectory.py @@ -1,3 +1,5 @@ +import sys +sys.path.append('../') import numpy as np diff --git a/main_package/classes/abstract_importer.py b/main_package/classes/utility/abstract_importer.py similarity index 100% rename from main_package/classes/abstract_importer.py rename to main_package/classes/utility/abstract_importer.py diff --git a/main_package/classes/cache.py b/main_package/classes/utility/cache.py similarity index 94% rename from main_package/classes/cache.py rename to main_package/classes/utility/cache.py index 31c5466..4e715da 100644 --- a/main_package/classes/cache.py +++ b/main_package/classes/utility/cache.py @@ -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: diff --git a/main_package/classes/decorators.py b/main_package/classes/utility/decorators.py similarity index 100% rename from main_package/classes/decorators.py rename to main_package/classes/utility/decorators.py diff --git a/main_package/classes/json_importer.py b/main_package/classes/utility/json_importer.py similarity index 99% rename from main_package/classes/json_importer.py rename to main_package/classes/utility/json_importer.py index 708c6ad..6e3288f 100644 --- a/main_package/classes/json_importer.py +++ b/main_package/classes/utility/json_importer.py @@ -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): diff --git a/main_package/tests/test_parameters_estimator.py b/main_package/tests/estimators/test_parameters_estimator.py similarity index 90% rename from main_package/tests/test_parameters_estimator.py rename to main_package/tests/estimators/test_parameters_estimator.py index be75668..b4a0761 100644 --- a/main_package/tests/test_parameters_estimator.py +++ b/main_package/tests/estimators/test_parameters_estimator.py @@ -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() diff --git a/main_package/tests/test_structure.py b/main_package/tests/estimators/test_structure.py similarity index 97% rename from main_package/tests/test_structure.py rename to main_package/tests/estimators/test_structure.py index ee67643..61380c6 100644 --- a/main_package/tests/test_structure.py +++ b/main_package/tests/estimators/test_structure.py @@ -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): diff --git a/main_package/tests/test_structure_estimator.py b/main_package/tests/estimators/test_structure_constraint_based_estimator.py similarity index 82% rename from main_package/tests/test_structure_estimator.py rename to main_package/tests/estimators/test_structure_constraint_based_estimator.py index 9c3aae1..a4d6c62 100644 --- a/main_package/tests/test_structure_estimator.py +++ b/main_package/tests/estimators/test_structure_constraint_based_estimator.py @@ -1,5 +1,5 @@ import sys -sys.path.append("../classes/") +sys.path.append("../../classes/") import glob import math import os @@ -10,17 +10,17 @@ import numpy as np import psutil from line_profiler import LineProfiler -import cache as ch -import sample_path as sp -import structure_estimator as se -import json_importer as ji +import utility.cache as ch +import structure_graph.sample_path as sp +import estimators.structure_constraint_based_estimator as se +import utility.json_importer as ji -class TestStructureEstimator(unittest.TestCase): +class TestStructureConstraintBasedEstimator(unittest.TestCase): @classmethod def setUpClass(cls): - 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() @@ -29,7 +29,7 @@ class TestStructureEstimator(unittest.TestCase): def test_init(self): exp_alfa = 0.1 chi_alfa = 0.1 - se1 = se.StructureEstimator(self.s1, exp_alfa, chi_alfa) + se1 = se.StructureConstraintBasedEstimator(self.s1, exp_alfa, chi_alfa) self.assertEqual(self.s1, se1.sample_path) self.assertTrue(np.array_equal(se1.nodes, np.array(self.s1.structure.nodes_labels))) self.assertTrue(np.array_equal(se1.nodes_indxs, self.s1.structure.nodes_indexes)) @@ -43,7 +43,7 @@ class TestStructureEstimator(unittest.TestCase): exp_alfa = 0.1 chi_alfa = 0.1 nodes_numb = len(self.s1.structure.nodes_labels) - se1 = se.StructureEstimator(self.s1, exp_alfa, chi_alfa) + se1 = se.StructureConstraintBasedEstimator(self.s1, exp_alfa, chi_alfa) cg = se1.build_complete_graph(self.s1.structure.nodes_labels) self.assertEqual(len(cg.edges), nodes_numb*(nodes_numb - 1)) for node in self.s1.structure.nodes_labels: @@ -56,7 +56,7 @@ class TestStructureEstimator(unittest.TestCase): exp_alfa = 0.1 chi_alfa = 0.1 nodes_numb = len(self.s1.structure.nodes_labels) - se1 = se.StructureEstimator(self.s1, exp_alfa, chi_alfa) + se1 = se.StructureConstraintBasedEstimator(self.s1, exp_alfa, chi_alfa) for node in self.s1.structure.nodes_labels: for b in range(nodes_numb): @@ -68,7 +68,7 @@ class TestStructureEstimator(unittest.TestCase): self.assertFalse(node in sset) def test_time(self): - se1 = se.StructureEstimator(self.s1, 0.1, 0.1) + se1 = se.StructureConstraintBasedEstimator(self.s1, 0.1, 0.1) lp = LineProfiler() lp.add_function(se1.complete_test) lp.add_function(se1.one_iteration_of_CTPC_algorithm) @@ -89,7 +89,7 @@ class TestStructureEstimator(unittest.TestCase): se1.save_results() def test_memory(self): - se1 = se.StructureEstimator(self.s1, 0.1, 0.1) + se1 = se.StructureConstraintBasedEstimator(self.s1, 0.1, 0.1) se1.ctpc_algorithm() current_process = psutil.Process(os.getpid()) mem = current_process.memory_info().rss diff --git a/main_package/tests/test_structure_score_based_estimator.py b/main_package/tests/estimators/test_structure_score_based_estimator.py similarity index 87% rename from main_package/tests/test_structure_score_based_estimator.py rename to main_package/tests/estimators/test_structure_score_based_estimator.py index eca845f..b4acb1b 100644 --- a/main_package/tests/test_structure_score_based_estimator.py +++ b/main_package/tests/estimators/test_structure_score_based_estimator.py @@ -1,5 +1,5 @@ import sys -sys.path.append("../classes/") +sys.path.append("../../classes/") import glob import math import os @@ -10,10 +10,10 @@ import numpy as np import psutil from line_profiler import LineProfiler -import cache as ch -import sample_path as sp -import structure_score_based_estimator as se -import json_importer as ji +import utility.cache as ch +import structure_graph.sample_path as sp +import estimators.structure_score_based_estimator as se +import utility.json_importer as ji @@ -21,7 +21,7 @@ class TestStructureScoreBasedEstimator(unittest.TestCase): @classmethod def setUpClass(cls): - 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() diff --git a/main_package/tests/test_cim.py b/main_package/tests/structure_graph/test_cim.py similarity index 95% rename from main_package/tests/test_cim.py rename to main_package/tests/structure_graph/test_cim.py index 13f41a6..44d63fe 100644 --- a/main_package/tests/test_cim.py +++ b/main_package/tests/structure_graph/test_cim.py @@ -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): diff --git a/main_package/tests/test_networkgraph.py b/main_package/tests/structure_graph/test_networkgraph.py similarity index 98% rename from main_package/tests/test_networkgraph.py rename to main_package/tests/structure_graph/test_networkgraph.py index c03b4cb..c16d4ff 100644 --- a/main_package/tests/test_networkgraph.py +++ b/main_package/tests/structure_graph/test_networkgraph.py @@ -1,5 +1,5 @@ import sys -sys.path.append("../classes/") +sys.path.append("../../classes/") import unittest import glob import os @@ -7,16 +7,16 @@ import networkx as nx import numpy as np import itertools -import sample_path as sp -import network_graph as ng -import json_importer as ji +import structure_graph.sample_path as sp +import structure_graph.network_graph as ng +import utility.json_importer as ji class TestNetworkGraph(unittest.TestCase): @classmethod def setUpClass(cls): - 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() diff --git a/main_package/tests/test_sample_path.py b/main_package/tests/structure_graph/test_sample_path.py similarity index 76% rename from main_package/tests/test_sample_path.py rename to main_package/tests/structure_graph/test_sample_path.py index 49e3641..f55e795 100644 --- a/main_package/tests/test_sample_path.py +++ b/main_package/tests/structure_graph/test_sample_path.py @@ -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): diff --git a/main_package/tests/test_setofcims.py b/main_package/tests/structure_graph/test_setofcims.py similarity index 98% rename from main_package/tests/test_setofcims.py rename to main_package/tests/structure_graph/test_setofcims.py index f158472..b2a13e1 100644 --- a/main_package/tests/test_setofcims.py +++ b/main_package/tests/structure_graph/test_setofcims.py @@ -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): diff --git a/main_package/tests/test_sets_of_cims_container.py b/main_package/tests/structure_graph/test_sets_of_cims_container.py similarity index 83% rename from main_package/tests/test_sets_of_cims_container.py rename to main_package/tests/structure_graph/test_sets_of_cims_container.py index dddf987..43c1096 100644 --- a/main_package/tests/test_sets_of_cims_container.py +++ b/main_package/tests/structure_graph/test_sets_of_cims_container.py @@ -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): diff --git a/main_package/tests/test_trajectory.py b/main_package/tests/structure_graph/test_trajectory.py similarity index 96% rename from main_package/tests/test_trajectory.py rename to main_package/tests/structure_graph/test_trajectory.py index 62d3da8..b05edd4 100644 --- a/main_package/tests/test_trajectory.py +++ b/main_package/tests/structure_graph/test_trajectory.py @@ -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): diff --git a/main_package/tests/test_cache.py b/main_package/tests/utility/test_cache.py similarity index 94% rename from main_package/tests/test_cache.py rename to main_package/tests/utility/test_cache.py index bbb89e9..f53bf45 100644 --- a/main_package/tests/test_cache.py +++ b/main_package/tests/utility/test_cache.py @@ -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): diff --git a/main_package/tests/test_json_importer.py b/main_package/tests/utility/test_json_importer.py similarity index 98% rename from main_package/tests/test_json_importer.py rename to main_package/tests/utility/test_json_importer.py index f940ae6..95695a6 100644 --- a/main_package/tests/test_json_importer.py +++ b/main_package/tests/utility/test_json_importer.py @@ -1,11 +1,11 @@ import sys -sys.path.append("../classes/") +sys.path.append("../../classes/") import unittest import os import glob import numpy as np import pandas as pd -import json_importer as ji +import utility.json_importer as ji import json @@ -15,7 +15,7 @@ class TestJsonImporter(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")) def test_init(self): j1 = ji.JsonImporter(self.read_files[0], 'samples', 'dyn.str', 'variables', 'Time', 'Name')