1
0
Fork 0

Added decorators class and cpu count

master
Luca Moretti 4 years ago
parent 46ab383b07
commit 7a4a6682fe
  1. 13
      main_package/classes/decorators.py
  2. 12
      main_package/classes/structure_score_based_estimator.py
  3. 2
      main_package/tests/test_structure_score_based_estimator.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

@ -18,6 +18,7 @@ import sample_path as sp
import structure as st import structure as st
import fam_score_calculator as fam_score import fam_score_calculator as fam_score
from decorators import timing
import multiprocessing import multiprocessing
from multiprocessing import Pool from multiprocessing import Pool
@ -68,7 +69,7 @@ class StructureScoreBasedEstimator:
return complete_graph return complete_graph
@timing
def estimate_structure(self, max_parents:int = None, iterations_number:int= 40, patience:int = None ): 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 Compute the score-based algorithm to find the optimal structure
@ -97,10 +98,13 @@ class StructureScoreBasedEstimator:
l_patience = [patience] * n_nodes l_patience = [patience] * n_nodes
'get the number of CPU'
cpu_count = multiprocessing.cpu_count()
'Estimate the best parents for each node' '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 = 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) #list_edges_partial = p.map(estimate_parents, self.nodes)
'Concatenate all the edges list' 'Concatenate all the edges list'
@ -119,6 +123,7 @@ class StructureScoreBasedEstimator:
for true_edge in true_edges: for true_edge in true_edges:
if not true_edge in list_edges: if not true_edge in list_edges:
n_missing_edges += 1 n_missing_edges += 1
print(true_edge)
print(f"n archi reali non trovati: {n_missing_edges}") print(f"n archi reali non trovati: {n_missing_edges}")
@ -190,7 +195,6 @@ class StructureScoreBasedEstimator:
return graph.edges return graph.edges
def get_score_from_structure(self,graph: ng.NetworkGraph,node_id:str): 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 Use the FamScore of a node in order to find the best parent nodes

@ -32,7 +32,7 @@ class TestStructureScoreBasedEstimator(unittest.TestCase):
def test_esecuzione(self): def test_esecuzione(self):
se1 = se.StructureScoreBasedEstimator(self.s1) se1 = se.StructureScoreBasedEstimator(self.s1)
se1.estimate_structure( se1.estimate_structure(
max_parents = 5, max_parents = 6,
iterations_number = 80, iterations_number = 80,
patience = None patience = None
) )