Repository navigation
Native graph engine: Louvain modularity, a faithful port of networkx's seeded Louvain (#3039) - #3049
Merged
Merged
Conversation
Contributor
squid-protocol
force-pushed
the
fix/3039-native-louvain
branch
from
September 15, 2026 00:05
90a5bd8 to
d0654e7
Compare
…s seeded Louvain (#3039) - graph_engine: louvain_communities / louvain_modularity, mirroring louvain_partitions, _one_level, _gen_graph and modularity order for order: - to_undirected's neighbour order, with the later-added reverse weight winning - the rebuilt G.edges() order - random.Random(42) shuffles shared across levels - the defaultdict own-community insert-last that orders gain ties - The result is the same communities as networkx, not just the same modularity: 300/300 seeded graphs, plus the crucible's 1,989 communities with a bit-identical 0.8150048644056641. 69.6 ms vs 158 ms. - NetworkRiskSensor._topology_metrics adds modularity in BOTH modes. It drops the to_undirected copy, nx Louvain and its greedy fallback, and nx.community.modularity. The V>5000 cutoff becomes PATH_METRICS_WORK_BUDGET. None when no file imports another, as before. - networkx now computes nothing; #3040 and #3041 remain. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…w native) Uncapped golden_diff scope: full precision 0 differences (crucible_check --update: no drift -- the Louvain port reproduces networkx's modularity on the real corpus); zero dependency exactly 1 -- network_macro/modularity None -> 0.8131, the value full precision reports. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
squid-protocol
force-pushed
the
fix/3039-native-louvain
branch
from
September 15, 2026 00:19
d0654e7 to
6e6437c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #3039. Part of #3033 (the native graph engine). Stacked: #3047 (#3036) → #3048 (#3038) → this PR. The branch carries their commits until they merge. Only the last commit, plus its re-bless, is this PR's own.
What changed
A faithful port of networkx's seeded Louvain is in
gitgalaxy/core/graph_engine.py:louvain_communities,louvain_modularity, and private helpers mirroringlouvain_partitions/_one_level/_gen_graph/modularity. Parity was the acceptance bar, and it goes beyond equal modularity to identical communities, in the same order. To get that, the port reproduces every order that decides a Louvain move:G.to_undirected(): edges are added source-major in out-run order. A mutually importing pair becomes one undirected edge that keeps the later-added direction's weight, while each neighbour keeps the position of its first edge (a dict key keeps its position when reassigned).louvain_partitionsrebuild: edges are re-added inG.edges()order, meaning node order with each edge once, per the seen-set rule. That rebuilt adjacency order drives_one_level.random.Random(42), which is what networkx'spy_random_statemakes of an int seed. One RNG is shared across levels, as in networkx.defaultdictinserts it last when no neighbour shares it, exactly as networkx does. That fixes the order in which equal gains are compared._gen_graph/ weighted degree: community edge weights are summed in edge order, and a self-loop counts twice in the degree.NetworkRiskSensor._topology_metrics()now also returns modularity, in both modes. This removes:G.to_undirected()copycommunity.louvain_communitiesand itsgreedy_modularity_communitiesfallbackcommunity.modularityWorkBudget(PATH_METRICS_WORK_BUDGET; each Louvain sweep charges the neighbour entries it reads)Modularity is
Nonewhen no file imports another. networkx divides by zero there, which the sensor already recorded asNone.After this PR, networkx computes nothing. The DiGraph builder still exists only for its degree counts and as the full-precision code path; #3040 (reach counts in
security_auditor) and #3041 (removal) remain.Parity and speed
==on the ordered list of sets); modularity equal at 4 dp 300/300graph_parity.py, warm)to_undirected+ Louvain + modularity; #3033 recorded 203 ms)One caveat: networkx's own
modularitysums each community's edges in set order. At level 0 the members are string paths, so the float summation order varies withPYTHONHASHSEED. Parity at the stored 4 dp is therefore the meaningful bar for modularity. The communities themselves never depend on hash order.Golden masters
I diffed fresh scans of each mode against the committed fixtures with an uncapped
golden_diff.deep_compare. The scans usedGITGALAXY_DISABLE_GIT_HISTORY=1and the same flags astest_golden_crucible.py.crucible_check.py --updatealso reported "No drift detected". On the real corpus the port reproduces networkx's modularity (0.8131).network_macro/modularitywent fromNoneto 0.8131, the value full precision reports.The zero-dependency fixture is re-blessed in the second commit.
Verification
pytestpasses on the 10 affected suites (509 passed), run as plainpytestfrom outside the repo.Nonelouvain_communities,modularityorto_undirected, and both modes agreetest_modularity_falls_back_to_greedy_when_louvain_unavailable: the fallback is gonetest_macro_math_outer_failure_returns_all_none: the outer macro block is gone. The newtest_macro_metrics_never_call_networkxcovers the same ground.audit_check.py: all clear. The seeded RNG carriesnoqa: S311, since it reproduces networkx's shuffle and has nothing to do with security.zero_dependency_mode.md(the networkx section now says it provides nothing; modularity row; older snapshots), the 02-16 wiki, README (networkx is no longer listed as needed for anything), CLAUDE.md, the console banner and the zero-dependency warning.🤖 Generated with Claude Code