From 8ab3367c6037d1c70bb0c81621bece496918b4c2 Mon Sep 17 00:00:00 2001 From: wariobrega Date: Mon, 24 Sep 2018 16:14:32 +0200 Subject: [PATCH] - fixed major bug addressing a numpy array object in greedyoptimization - minor code cleaning --- algorithms/greedy_optimization.py | 13 ++++++++++++- algorithms/keyplayer.py | 5 ++++- cmds/cmds_utils/kpsearch_wrapper.py | 1 + cmds/keyplayer.py | 10 +++++++++- tools/modules_utils.py | 2 +- 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/algorithms/greedy_optimization.py b/algorithms/greedy_optimization.py index b18e3d7..c3c56d3 100644 --- a/algorithms/greedy_optimization.py +++ b/algorithms/greedy_optimization.py @@ -189,19 +189,26 @@ def reachability(graph, kp_size, kp_type: KpposEnum, seed=None, max_distance=Non notS = set(node_indices).difference(set(S)) utils = gu(graph=graph) + if kp_type == KpposEnum.mreach and m is None: raise WrongArgumentError("'m' is required for the mreach algorithm") + elif kp_type == KpposEnum.mreach: + + if not isinstance(m, int) or m <= 0: raise TypeError({"'m' must be a positive integer value"}) else: if implementation != CmodeEnum.igraph: + sps = sp.get_shortestpaths(graph=graph, cmode=implementation, nodes=None) + type_func = partial(kp.mreach, graph=graph, nodes=S_names, m=m, max_distance=max_distance, implementation=implementation, sp_matrix=sps) else: type_func = partial(kp.mreach, graph=graph, nodes=S_names, m=m, max_distance=max_distance, implementation=implementation) + elif kp_type == KpposEnum.dR: if implementation != CmodeEnum.igraph: sps = sp.get_shortestpaths(graph=graph, cmode=implementation, nodes=None) @@ -213,9 +220,12 @@ def reachability(graph, kp_size, kp_type: KpposEnum, seed=None, max_distance=Non else: raise KeyError("'kp_type' not valid. It must be one of the following: {}".format(list(KpposEnum))) - reachability_score = type_func() + + reachability_score = type_func(graph=graph) + kppset_score_pairs_history = {tuple(S): reachability_score} + optimal_set_found = False while not optimal_set_found: kppset_score_pairs = {} @@ -251,6 +261,7 @@ def reachability(graph, kp_size, kp_type: KpposEnum, seed=None, max_distance=Non else: optimal_set_found = True final = graph.vs(S)["name"] + sys.stdout.write("An optimal kpp-set of size {} is ({}) with score {}\n".format(kp_size, ', '.join(final), reachability_score)) return final, round(reachability_score, 5) diff --git a/algorithms/keyplayer.py b/algorithms/keyplayer.py index 10c20fa..0e0c6c5 100644 --- a/algorithms/keyplayer.py +++ b/algorithms/keyplayer.py @@ -223,10 +223,13 @@ def mreach(graph, nodes: list, m: int, max_distance: int=None, implementation=Cm if implementation == CmodeEnum.igraph: shortest_path_lengths = sp.shortest_path_igraph(graph, nodes=nodes) + else: - if not sp_matrix: + if sp_matrix is None: shortest_path_lengths = sp.get_shortestpaths(graph=graph, cmode=implementation, nodes=nodes) + else: + if not isinstance(sp_matrix, np.ndarray): raise ValueError("'sp_matrix' must be a numpy.ndarray instance") elif sp_matrix.shape[0] != graph.vcount(): diff --git a/cmds/cmds_utils/kpsearch_wrapper.py b/cmds/cmds_utils/kpsearch_wrapper.py index 3044ddb..b08fe88 100644 --- a/cmds/cmds_utils/kpsearch_wrapper.py +++ b/cmds/cmds_utils/kpsearch_wrapper.py @@ -204,6 +204,7 @@ def run_reachability(self, kp_size:int, kp_type:KpposEnum, m=None, max_distance= raise ValueError("\"m\" must be a positive integer for mreach ") go_results = self.go.reachability(graph=self.graph, kp_size=kp_size, kp_type=kp_type, max_distance=max_distance, seed=seed, m=m, implementation=implementation) + self.results[kp_type.name] = [go_results[0], go_results[1]] def get_results(self) -> dict: diff --git a/cmds/keyplayer.py b/cmds/keyplayer.py index e5371ee..6e21c30 100644 --- a/cmds/keyplayer.py +++ b/cmds/keyplayer.py @@ -242,10 +242,18 @@ def run(self): sys.stdout.write( "Finding best set of kp-nodes of size {0} using an MREACH measure of {1} (kp pos measure)\n".format( self.args.k_size, self.args.m_reach)) + + print("ENTRO IN MREACH") + input() + kp_runner.run_reachability(self.args.k_size, KpposEnum.mreach, m=self.args.m_reach, max_distance=self.args.max_distances, implementation=implementation, threads=self.args.threads) + print("ESCO DA MREACH") + + + else: sys.stdout.write("Wrong implementation. Please contact pyntacle Developers and sent this error message, along with a command line and a log.\nQuitting.\n") sys.exit(1) @@ -262,7 +270,7 @@ def run(self): else: plurals = ['', 'is'] - if self.args.implementation=='brute-force': + if self.args.implementation == 'brute-force': list_of_results = ['('+ ', '.join(x) + ')' for x in results[kp][0]] else: list_of_results = results[kp][0] diff --git a/tools/modules_utils.py b/tools/modules_utils.py index ee102a6..4ea7155 100644 --- a/tools/modules_utils.py +++ b/tools/modules_utils.py @@ -41,7 +41,7 @@ class ModuleUtils(): def __init__(self, modules: list, graph: Graph, algorithm: str): """ Implements all the necessary step to check a graph object and add the reserved attribute "__module_number" to - each submodule in order to retrace it back. If a graph attribute with that name aĆ²ready exist, it will be + each submodule in order to retrace it back. If a graph attribute with that name already exists, it will be overwritten :param modules:a list of graphs already divided by the CommunityFinder class :param graph: the input graph used to find modules