Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preloop for constructors #51

Merged
merged 8 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# for some of the options that are available

[MESSAGES CONTROL]
disable=C0103,R0904,R0903,W0511,W0232,R0922,R0801,R0921,W0141,R0401,I0013,W0142,W0622,C0325,R0205,C0330,W1201,W0621,R0913,R0914,C0415
disable=C0103,R0904,R0903,W0511,R0801,R0401,I0013,W0622,C0325,R0205,W1201,W0621,R0913,R0914,C0415

[FORMAT]
# Maximum number of characters on a single line.
Expand All @@ -19,7 +19,7 @@ max-locals=15
# Maximum number of return / yield for function / method body
max-returns=6
# Maximum number of branch for function / method body
max-branchs=12
max-branches=12
# Maximum number of statements in function / method body
max-statements=50
# Maximum number of parents for a class (see R0901).
Expand Down
3 changes: 2 additions & 1 deletion examples/create_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def create_sbm():
plt.savefig("ground_truth.png", bbox_inches="tight")

# save adjacency with pickle
adjacency = nx.adjacency_matrix(graph, weight="weight")
adjacency = nx.to_numpy_matrix(graph, weight="weight")
with open("sbm_graph.pkl", "wb") as pickle_file:
pickle.dump(adjacency, pickle_file)

Expand All @@ -48,5 +48,6 @@ def create_sbm():

return adjacency


if __name__ == "__main__":
create_sbm()
17 changes: 12 additions & 5 deletions src/pygenstability/constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ def get_data(self, time):
"""Return quality and null model at given time."""
if self.with_spectral_gap:
time /= self.spectral_gap
return time * self.partial_quality_matrix, self.partial_null_model, 1 - time
return {
"quality": time * self.partial_quality_matrix,
"null_model": self.partial_null_model,
"shift": 1 - time,
}


class constructor_continuous_combinatorial(Constructor):
Expand All @@ -130,7 +134,7 @@ def get_data(self, time):
time /= self.spectral_gap
exp = apply_expm(-time * self.partial_quality_matrix)
quality_matrix = sp.diags(self.partial_null_model[0]).dot(exp)
return quality_matrix, self.partial_null_model, None
return {"quality": quality_matrix, "null_model": self.partial_null_model}


class constructor_continuous_normalized(Constructor):
Expand All @@ -155,7 +159,7 @@ def get_data(self, time):
time /= self.spectral_gap
exp = apply_expm(-time * self.partial_quality_matrix)
quality_matrix = sp.diags(self.partial_null_model[0]).dot(exp)
return quality_matrix, self.partial_null_model, None
return {"quality": quality_matrix, "null_model": self.partial_null_model}


class constructor_signed_modularity(Constructor):
Expand Down Expand Up @@ -188,7 +192,10 @@ def prepare(self, **kwargs):

def get_data(self, time):
"""Return quality and null model at given time."""
return time * self.partial_quality_matrix, self.partial_null_model, None
return {
"quality": time * self.partial_quality_matrix,
"null_model": self.partial_null_model,
}


class constructor_directed(Constructor):
Expand Down Expand Up @@ -217,4 +224,4 @@ def get_data(self, time):
"""Return quality and null model at given time."""
exp = apply_expm(time * self.partial_quality_matrix)
quality_matrix = sp.diags(self.partial_null_model[0]).dot(exp)
return quality_matrix, self.partial_null_model, None
return {"quality": quality_matrix, "null_model": self.partial_null_model}
46 changes: 26 additions & 20 deletions src/pygenstability/pygenstability.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,25 @@ def run(
times=times,
)
constructor = load_constructor(constructor, graph, with_spectral_gap=with_spectral_gap)

with multiprocessing.Pool(n_workers) as pool:

L.info("Loop over times...")
L.info("Precompute constructors...")
constructors = list(
tqdm(
pool.imap(constructor.get_data, times),
total=n_time,
disable=tqdm_disable,
)
)

L.info("Optimise stability...")
all_results = defaultdict(list)
all_results["run_params"] = run_params
for time in tqdm(times, disable=tqdm_disable):
quality_matrix, null_model, global_shift = constructor.get_data(time)
louvain_results = _run_several_louvains(
quality_matrix, null_model, global_shift, n_louvain, pool
)

for i, time in tqdm(enumerate(times), total=n_time, disable=tqdm_disable):
# stability optimisation
louvain_results = _run_several_louvains(constructors[i], n_louvain, pool)
communities = _process_louvain_run(time, louvain_results, all_results)

if with_VI:
Expand All @@ -137,7 +146,7 @@ def run(

if with_postprocessing:
L.info("Apply postprocessing...")
apply_postprocessing(all_results, pool, constructor=constructor)
apply_postprocessing(all_results, pool, constructors, tqdm_disable)

if with_ttprime or with_optimal_scales:
L.info("Compute ttprimes...")
Expand Down Expand Up @@ -236,15 +245,15 @@ def _evaluate_quality(partition_id, qualities_index, null_model, global_shift):
return quality


def _run_several_louvains(quality_matrix, null_model, global_shift, n_runs, pool):
def _run_several_louvains(constructor, n_runs, pool):
"""Run several louvain on the current quality matrix."""
quality_indices, quality_values = _to_indices(quality_matrix)
quality_indices, quality_values = _to_indices(constructor["quality"])
worker = partial(
_evaluate_louvain,
quality_indices=quality_indices,
quality_values=quality_values,
null_model=null_model,
global_shift=global_shift,
null_model=constructor["null_model"],
global_shift=constructor.get("shift"),
)

chunksize = _get_chunksize(n_runs, pool)
Expand All @@ -264,21 +273,18 @@ def compute_ttprime(all_results, pool):
all_results["ttprime"] += all_results["ttprime"].T


def apply_postprocessing(all_results, pool, constructor, tqdm_disable=False):
def apply_postprocessing(all_results, pool, constructors, tqdm_disable=False):
"""Apply postprocessing."""
all_results_raw = all_results.copy()

for i, time in tqdm(
enumerate(all_results["times"]),
total=len(all_results["times"]),
disable=tqdm_disable,
for i, constructor in tqdm(
enumerate(constructors), total=len(constructors), disable=tqdm_disable
):
quality_matrix, null_model, global_shift = constructor.get_data(time)
worker = partial(
_evaluate_quality,
qualities_index=_to_indices(quality_matrix),
null_model=null_model,
global_shift=global_shift,
qualities_index=_to_indices(constructor["quality"]),
null_model=constructor["null_model"],
global_shift=constructor.get("shift"),
)
best_quality_id = np.argmax(
list(
Expand Down
Loading