Repository navigation
perf: compute ase2networkx bonds from sparse neighbor pairs - #132
Merged
Merged
Conversation
Contributor
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 44 minutes. View limit detailsLimit details: You’ve used the included review currently available. Review configuration: ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (2)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
The distance-based path of ase2networkx turns the vesin/ASE neighbor list into a sorted (M, 2) array of bonded pairs and adds those edges to a graph with nodes 0..N-1. Peak memory scales with atoms plus bonds: 7.5 MB for 8,100 atoms, down from 1.64 GB. The neighbor-list cutoff spans bonding atoms only, and a pair is bonded when any periodic image lies within its cutoff, so tiny periodic cells give the same bonds with vesin and ASE. Tests compare edges against an all-pairs minimum-image reference across wrapped, unwrapped, mixed-pbc, triclinic, ionic and molecular systems on both backends. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Pass one ulp above the bonding cutoff to the neighbor list so the inclusive pair filter decides pairs at exactly the cutoff. - Store the non-bonding ions as a tuple and read it directly in _add_node_properties. - Test ase2networkx with scale 1.0, 1.2 and 1.5 against the reference. - Test that the neighbor-list cutoff comes from bonding atoms only. - Measure peak memory on both neighbor-list backends at two box sizes and assert near-linear growth. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PythonFZ
force-pushed
the
perf/ase2networkx-memory
branch
from
October 2, 2026 12:40
95d1c24 to
2237a5a
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.
What changed
The distance-based path of
ase2networkxbuilds bonds from the neighbor list as a sorted(M, 2)pair array:_compute_bonded_pairskeepsi < jpairs between bonding atoms withd <= scale * (r_i + r_j), keeps zero-shift pairs only forpbc=False, and collapses periodic images withnp.unique.0..N-1, thenadd_edges_from(pairs.tolist(), bond_order=None). Node, edge and graph attributes and the edge order match the previous output.2 * max(scale * r)over bonding atoms (Li/Na/K/Rb/Cs/Fr excluded), so pairs at exactly their cutoff are bonded on both backends. For a Cs⁺/PF6⁻/EC box this is 2.57 Å, down from 5.86 Å.ase.data.covalent_radii[numbers] * scale, the same valuesnatural_cutoffsreturns.Peak memory goes from about 25 B·N² (dense
N x Nfloat and int arrays plusnx.from_numpy_array) to O(N + M). The signature and theatoms.info["connectivity"]path are unchanged.Behaviour change
In cells small enough that one pair has several periodic images inside the neighbor-list cutoff, the dense matrix kept whichever image the backend emitted last, so the bond depended on the backend. A pair is now bonded when any image lies within its cutoff. Example:
Atoms("CH", positions=[[0, 5, 5], [0.7, 5, 5]], cell=[2, 10, 10], pbc=True)(images at 0.7 Å and 1.3 Å, cutoff 1.284 Å) was bonded with vesin and unbonded with ASE on main; both backends bond it now.Tests
test_ase2networkx_matches_reference: edges equal an all-pairs minimum-image reference (ase.geometry.get_distances) for 7 systems (packed, unwrapped, wrapped, mixed pbc[T, F, T], triclinic, Li⁺/PF6⁻ electrolyte, phenol) ×pbc{True, False} ×scale{1.0, 1.2, 1.5} × backend {vesin, ASE}.test_ase2networkx_memory_is_linearon both backends: peak memory ratio between 8,232 and 648 atoms must stay below 2× the atom ratio (limit 25.4). This PR: 12.7 (vesin, 0.6 → 7.6 MB) and 11.5 (ASE, 7.9 → 90.4 MB); main: 146–160.test_ase2networkx_bonds_at_cutoff_boundary: a C–C pair at exactly2 * 1.2 * r_Cis bonded (fails on main).test_ase2networkx_neighbor_cutoff_ignores_ions: the neighbor-list cutoff for PF6⁻ + Cs⁺ is2 * 1.2 * r_P(main:2 * 1.2 * r_Cs).test_ase2networkx_bonds_through_any_periodic_image(fails on main with ASE),test_ase2networkx_skips_periodic_self_images,test_ase2networkx_only_non_bonding_ions,test_ase2networkx_graph_contract.pbc=Falseshift filter,np.unique,.tolist(), the ion filter orscale, switching<=to<, or sizing the cutoff over all atoms, each fails at least one new test.uv run pytest: 560 passed.uvx prek --all-files: clean.Benchmark
Periodic water box (100 H₂O, 14.4 Å) tiled with
Atoms.repeat, vesin 0.4.2 backend, macOS arm64, Python 3.11, one fresh process per cell. Peak is thetracemallocpeak during thease2networkxcall.At 1M atoms most of the 948 MB is the returned
nx.Graph(one attribute dict per node and edge).Closes #115
🤖 Generated with Claude Code