From 9e4d6b9fe5c228d5ee84d7affa9c718ff66b2aa7 Mon Sep 17 00:00:00 2001 From: Luca Moretti Date: Mon, 21 Dec 2020 16:00:36 +0100 Subject: [PATCH] Updated parallel implementation --- .../structure_constraint_based_estimator.py | 11 ++++++----- .../classes/optimizers/constraint_based_optimizer.py | 10 +++++++--- .../test_structure_constraint_based_estimator.py | 4 ++-- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/main_package/classes/estimators/structure_constraint_based_estimator.py b/main_package/classes/estimators/structure_constraint_based_estimator.py index 34eaa10..0935aac 100644 --- a/main_package/classes/estimators/structure_constraint_based_estimator.py +++ b/main_package/classes/estimators/structure_constraint_based_estimator.py @@ -215,7 +215,7 @@ class StructureConstraintBasedEstimator(se.StructureEstimator): return optimizer_obj.optimize_structure() @timing - def ctpc_algorithm(self): + def ctpc_algorithm(self,disable_multiprocessing:bool= False ): """ Compute the CTPC algorithm. Parameters: @@ -233,8 +233,9 @@ class StructureConstraintBasedEstimator(se.StructureEstimator): 'get the number of CPU' cpu_count = multiprocessing.cpu_count() - #if disable_multiprocessing: - #cpu_count = 1 + if disable_multiprocessing: + print("DISABILITATO") + cpu_count = 1 'Remove all the edges from the structure' self.sample_path.structure.clean_structure_edges() @@ -250,8 +251,8 @@ class StructureConstraintBasedEstimator(se.StructureEstimator): @timing - def estimate_structure(self): - return self.ctpc_algorithm() + def estimate_structure(self,disable_multiprocessing:bool=False): + return self.ctpc_algorithm(disable_multiprocessing=disable_multiprocessing) diff --git a/main_package/classes/optimizers/constraint_based_optimizer.py b/main_package/classes/optimizers/constraint_based_optimizer.py index dc6b6cc..98b4039 100644 --- a/main_package/classes/optimizers/constraint_based_optimizer.py +++ b/main_package/classes/optimizers/constraint_based_optimizer.py @@ -85,9 +85,13 @@ class ConstraintBasedOptimizer(Optimizer): #print("Removing EDGE:", test_parent, self.node_id) graph.remove_edges([(test_parent, self.node_id)]) other_nodes.remove(test_parent) - print(f"TEST PARENT: {test_parent}") - if u.__contains__(test_parent): - u.remove(test_parent) + print(f"TEST PARENT: {test_parent}") + try: + u.remove(test_parent) + except: + print(f"u: {u}") + print(f"utest_parent: {test_parent}") + removed = True break #else: diff --git a/main_package/tests/estimators/test_structure_constraint_based_estimator.py b/main_package/tests/estimators/test_structure_constraint_based_estimator.py index 7f8621a..1bf1bdc 100644 --- a/main_package/tests/estimators/test_structure_constraint_based_estimator.py +++ b/main_package/tests/estimators/test_structure_constraint_based_estimator.py @@ -22,7 +22,7 @@ class TestStructureConstraintBasedEstimator(unittest.TestCase): @classmethod def setUpClass(cls): #cls.read_files = glob.glob(os.path.join('../../data', "*.json")) - cls.importer = ji.JsonImporter("../../data/networks_and_trajectories_binary_data_01_15.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name') + cls.importer = ji.JsonImporter("../../data/networks_and_trajectories_ternary_data_01_6.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name') cls.s1 = sp.SamplePath(cls.importer) cls.s1.build_trajectories() cls.s1.build_structure() @@ -32,7 +32,7 @@ class TestStructureConstraintBasedEstimator(unittest.TestCase): true_edges = set(map(tuple, true_edges)) se1 = se.StructureConstraintBasedEstimator(self.s1,0.1,0.1) - edges = se1.ctpc_algorithm() + edges = se1.estimate_structure(disable_multiprocessing=True) self.assertEqual(edges, true_edges)