Skip to content
Open
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 src/pygenstability/constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ def prepare(self, **kwargs):
adj_neg = -self.graph.copy()
adj_neg[self.graph > 0] = 0.0

deg_plus = adj_pos.sum(1).flatten()
deg_neg = adj_neg.sum(1).flatten()
deg_plus = np.asarray(adj_pos.sum(1)).flatten()
deg_neg = np.asarray(adj_neg.sum(1)).flatten()

deg_norm = deg_plus.sum() + deg_neg.sum()
# silence divide-by-zero when the graph has only positive or only negative edges
Expand Down
9 changes: 7 additions & 2 deletions src/pygenstability/pygenstability.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,12 +470,15 @@ def _optimise(

partitions = []
n_null = int(len(null_model) / 2)
# The CPM null-model contribution is quadratic in node_sizes, so scaling by
# sqrt(n_null) makes each layer contribute n_null * pair_k; after Leiden's
# 1/n_null averaging below this yields sum_k pair_k, matching the Louvain backend.
for null in null_model[::2]:
partitions.append(
leidenalg.CPMVertexPartition(
G,
weights=quality_values,
node_sizes=null.tolist(),
node_sizes=(np.sqrt(n_null) * null).tolist(),
correct_self_loops=True,
)
)
Expand Down Expand Up @@ -517,13 +520,15 @@ def _evaluate_quality(
# evaluate using Leiden method
if method == "leiden":
n_null = int(len(null_model) / 2)
# Scale node_sizes by sqrt(n_null) so the quadratic CPM null term matches the
# Louvain backend once the 1/n_null averaging is applied (see _optimise).
quality = np.sum(
[
leidenalg.CPMVertexPartition(
ig.Graph(edges=zip(*quality_indices), directed=True),
initial_membership=partition_id,
weights=quality_values,
node_sizes=null.tolist(),
node_sizes=(np.sqrt(n_null) * null).tolist(),
correct_self_loops=True,
).quality()
for null in null_model[::2]
Expand Down
176 changes: 88 additions & 88 deletions tests/data/test_constructor_signed_modularity.yaml
Original file line number Diff line number Diff line change
@@ -1,92 +1,92 @@
null_model:
- - - 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.053763440860215055
- 0.010752688172043012
- 0.010752688172043012
- 0.053763440860215055
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- - - 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.053763440860215055
- 0.010752688172043012
- 0.010752688172043012
- 0.053763440860215055
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- - - .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- - - 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- - 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.053763440860215055
- 0.010752688172043012
- 0.010752688172043012
- 0.053763440860215055
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- - 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.053763440860215055
- 0.010752688172043012
- 0.010752688172043012
- 0.053763440860215055
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- 0.04838709677419355
- - .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- .nan
- - 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
quality:
- - 0.0
- 0.005376344086021506
Expand Down
Loading
Loading