Skip to content

Commit

Permalink
- fixed major bug addressing a numpy array object in greedyoptimization
Browse files Browse the repository at this point in the history
- minor code cleaning
  • Loading branch information
wariobrega committed Sep 24, 2018
1 parent d68c0e4 commit 8ab3367
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
13 changes: 12 additions & 1 deletion algorithms/greedy_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 = {}
Expand Down Expand Up @@ -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)
5 changes: 4 additions & 1 deletion algorithms/keyplayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
1 change: 1 addition & 0 deletions cmds/cmds_utils/kpsearch_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 9 additions & 1 deletion cmds/keyplayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion tools/modules_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8ab3367

Please sign in to comment.