Skip to content

Commit 050fc6d

Browse files
Max17190claude
authored andcommitted
fix(analyze): keep networkx's own betweenness on networkx 3.4
networkx 3.4, which pyproject.toml still allows, rescales sampled betweenness scores differently from 3.5 and later. The fast path follows 3.5, so on 3.4 a report could print a different bridge score: 0.108 instead of 0.107 on axum. On networkx before 3.5, _betweenness_centrality now returns networkx's own result, so the report matches v8 on every supported version, and the tests no longer skip the sampled cases there. Co-Authored-By: Claude Opus 5.5 <[email protected]> (cherry picked from commit 5eac2f3)
1 parent 21e4358 commit 050fc6d

2 files changed

Lines changed: 12 additions & 11 deletions

File tree

‎graphify/analyze.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from __future__ import annotations
33
import math
44
import random
5+
import re
56
from pathlib import Path
67
import networkx as nx
78

@@ -482,6 +483,10 @@ def _cross_community_surprises(
482483
return deduped[:top_n]
483484

484485

486+
# networkx 3.5 changed how sampled betweenness scores are rescaled.
487+
_NETWORKX_RESCALES_SAMPLES_BY_SOURCE = tuple(int(p) for p in re.findall(r"\d+", nx.__version__)[:2]) >= (3, 5)
488+
489+
485490
def _betweenness_centrality(G: nx.Graph, k: int | None, seed: int) -> dict[str, float]:
486491
"""The scores of ``nx.betweenness_centrality(G, k=k, seed=seed)``, bit for bit.
487492
@@ -490,8 +495,11 @@ def _betweenness_centrality(G: nx.Graph, k: int | None, seed: int) -> dict[str,
490495
predecessor lists over the whole graph for each source. This numbers the nodes
491496
once, keeps the same state in lists, and resets only the entries a source reached.
492497
The search order, the accumulation order and the rescale for sampled sources
493-
follow networkx 3.5 and later, so the scores do not depend on the installed networkx.
498+
follow networkx 3.5 and later. Older networkx rescaled sampled scores differently,
499+
so there this returns networkx's own result.
494500
"""
501+
if not _NETWORKX_RESCALES_SAMPLES_BY_SOURCE:
502+
return nx.betweenness_centrality(G, k=k, seed=seed)
495503
nodes = list(G)
496504
n = len(nodes)
497505
if k == n:

‎tests/test_analyze.py‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Tests for analyze.py."""
22
import json
3-
import re
43
import networkx as nx
54
import pytest
65
from pathlib import Path
@@ -908,9 +907,6 @@ def _betweenness_case(n: int, m: int, *, directed: bool = False, extras: bool =
908907
return G
909908

910909

911-
_NETWORKX_RESCALES_SAMPLES_BY_SOURCE = tuple(map(int, re.match(r"(\d+)\.(\d+)", nx.__version__).groups())) >= (3, 5)
912-
913-
914910
@pytest.mark.parametrize(
915911
("G", "k"),
916912
[
@@ -923,13 +919,10 @@ def _betweenness_case(n: int, m: int, *, directed: bool = False, extras: bool =
923919
(nx.complete_graph(5), None),
924920
(_betweenness_case(60, 70, extras=True), None),
925921
(_betweenness_case(60, 140, directed=True, extras=True), None),
926-
pytest.param(_betweenness_case(300, 420, extras=True), 40, marks=pytest.mark.skipif(
927-
not _NETWORKX_RESCALES_SAMPLES_BY_SOURCE, reason="networkx < 3.5 rescales sampled scores differently")),
928-
pytest.param(_betweenness_case(200, 500, directed=True), 30, marks=pytest.mark.skipif(
929-
not _NETWORKX_RESCALES_SAMPLES_BY_SOURCE, reason="networkx < 3.5 rescales sampled scores differently")),
922+
(_betweenness_case(300, 420, extras=True), 40),
923+
(_betweenness_case(200, 500, directed=True), 30),
930924
# suggest_questions samples 100 sources once a graph has more than 1000 nodes.
931-
pytest.param(_betweenness_case(1500, 1900, extras=True), 100, marks=pytest.mark.skipif(
932-
not _NETWORKX_RESCALES_SAMPLES_BY_SOURCE, reason="networkx < 3.5 rescales sampled scores differently")),
925+
(_betweenness_case(1500, 1900, extras=True), 100),
933926
],
934927
)
935928
def test_betweenness_matches_networkx_bit_for_bit(G, k):

0 commit comments

Comments
 (0)