From 7a4a6682fe469d0b81e6404a44d7ba268aef3611 Mon Sep 17 00:00:00 2001 From: Luca Moretti Date: Sat, 10 Oct 2020 12:10:54 +0200 Subject: [PATCH] Added decorators class and cpu count --- main_package/classes/decorators.py | 13 +++++++++++++ .../classes/structure_score_based_estimator.py | 12 ++++++++---- .../tests/test_structure_score_based_estimator.py | 2 +- 3 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 main_package/classes/decorators.py diff --git a/main_package/classes/decorators.py b/main_package/classes/decorators.py new file mode 100644 index 0000000..cc029f9 --- /dev/null +++ b/main_package/classes/decorators.py @@ -0,0 +1,13 @@ +from functools import wraps +from time import time + +def timing(f): + @wraps(f) + def wrap(*args, **kw): + ts = time() + result = f(*args, **kw) + te = time() + print (f"{f.__name__} args:[{args},{kw}] took: {te-ts} sec") + + return result + return wrap \ No newline at end of file diff --git a/main_package/classes/structure_score_based_estimator.py b/main_package/classes/structure_score_based_estimator.py index 008a30c..51b7e39 100644 --- a/main_package/classes/structure_score_based_estimator.py +++ b/main_package/classes/structure_score_based_estimator.py @@ -18,6 +18,7 @@ import sample_path as sp import structure as st import fam_score_calculator as fam_score +from decorators import timing import multiprocessing from multiprocessing import Pool @@ -68,7 +69,7 @@ class StructureScoreBasedEstimator: return complete_graph - + @timing def estimate_structure(self, max_parents:int = None, iterations_number:int= 40, patience:int = None ): """ Compute the score-based algorithm to find the optimal structure @@ -97,10 +98,13 @@ class StructureScoreBasedEstimator: l_patience = [patience] * n_nodes + 'get the number of CPU' + cpu_count = multiprocessing.cpu_count() + 'Estimate the best parents for each node' - with multiprocessing.Pool(processes=4) as pool: + with multiprocessing.Pool(processes=cpu_count) as pool: list_edges_partial = pool.starmap(estimate_parents, zip(self.nodes,l_max_parents,l_iterations_number,l_patience)) - #list_edges_partial = [estimate_parents(n) for n in self.nodes] + #list_edges_partial = [estimate_parents(n,max_parents,iterations_number,patience) for n in self.nodes] #list_edges_partial = p.map(estimate_parents, self.nodes) 'Concatenate all the edges list' @@ -119,6 +123,7 @@ class StructureScoreBasedEstimator: for true_edge in true_edges: if not true_edge in list_edges: n_missing_edges += 1 + print(true_edge) print(f"n archi reali non trovati: {n_missing_edges}") @@ -190,7 +195,6 @@ class StructureScoreBasedEstimator: return graph.edges - def get_score_from_structure(self,graph: ng.NetworkGraph,node_id:str): """ Use the FamScore of a node in order to find the best parent nodes diff --git a/main_package/tests/test_structure_score_based_estimator.py b/main_package/tests/test_structure_score_based_estimator.py index a4b602e..eca845f 100644 --- a/main_package/tests/test_structure_score_based_estimator.py +++ b/main_package/tests/test_structure_score_based_estimator.py @@ -32,7 +32,7 @@ class TestStructureScoreBasedEstimator(unittest.TestCase): def test_esecuzione(self): se1 = se.StructureScoreBasedEstimator(self.s1) se1.estimate_structure( - max_parents = 5, + max_parents = 6, iterations_number = 80, patience = None )