Migrate all seeded functions from global random state to local RNG instances#689
Open
Migrate all seeded functions from global random state to local RNG instances#689
Conversation
Add seed parameter to h_eigenvector_centrality, degree_assortativity, simulate_kuramoto, and random_edge_shuffle for reproducibility. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Pandas now uses dtype='str' instead of dtype='object' for string Index columns. Compare as list to avoid version-dependent repr. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #689 +/- ##
==========================================
+ Coverage 93.59% 93.67% +0.08%
==========================================
Files 66 66
Lines 5123 5110 -13
==========================================
- Hits 4795 4787 -8
+ Misses 328 323 -5 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Seed numpy before eigsh in spectral_clustering so ARPACK produces consistent results. Seed random data in kmeans tests. Update pandas dtype doctest to compare as list. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
eigvalsh can return eigenvalues like -1.3e-17 for a positive semi-definite matrix due to floating point arithmetic. Use -1e-12 tolerance instead of strict >= 0. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Pass explicit v0 to eigsh when seed is provided, making ARPACK initialization deterministic. Relax test assertion to check core community membership rather than exact partition, since boundary nodes can be assigned differently across LAPACK implementations. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Replace all np.random.seed()/random.seed() + global function calls with np.random.default_rng(seed) and local Generator instances. This eliminates global state pollution, is thread-safe, and follows Scientific Python best practices. All functions now accept int | np.random.Generator | None for the seed parameter. Removed all stdlib random usage from xgi source. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The new RNG stream produces a different random hypergraph, changing the size of the largest connected component from 6 to 8. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The fast_random_hypergraph with seed=2 now produces a different number of dyads due to the RNG migration, causing a color array length mismatch in the multilayer drawing tutorial. Changed to seed=8 which produces 10 dyads matching the hardcoded color lists. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
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.
Summary
seedparameter to previously unseeded stochastic functions:h_eigenvector_centrality,uniform_h_eigenvector_centrality,degree_assortativity,simulate_kuramoto, andrandom_edge_shufflenp.random.seed()/random.seed()) to local Generator instances (np.random.default_rng(seed))randomusage from xgi source — everything now uses numpy's RNG exclusivelygeometric()helper to accept a numpyrngparameter instead of relying on global stateMotivation
The previous approach of calling
np.random.seed(seed)/random.seed(seed)pollutes global state, isn't thread-safe, and means seeding one function can affect another called later. The Scientific Python best practices recommendnp.random.default_rng(seed)with local Generator instances.Files changed
Source (13 files)
xgi/utils/utilities.py—geometric()now takes a numpyrngparameterxgi/algorithms/centrality.py—h_eigenvector_centrality,uniform_h_eigenvector_centralityxgi/algorithms/assortativity.py—degree_assortativity,_choose_degreesxgi/algorithms/connected.py— doctest updatexgi/dynamics/synchronization.py—simulate_kuramotoxgi/core/hypergraph.py—random_edge_shufflexgi/drawing/layout.py—random_layout+ removed unnecessaryrandom.seed()from spring layoutsxgi/generators/random.py— all 5 generator functionsxgi/generators/uniform.py— all 3 generator functionsxgi/generators/simplicial_complexes.py— 4 functionsxgi/generators/randomizing.py—shuffle_hyperedgesTests (2 files)
tests/dynamics/test_synchronization.py— updated expected valuestests/generators/test_uniform.py— updated seedTutorials (1 file)
tutorials/in_depth/In Depth 4 - Drawing multilayer-style.ipynb— updated seedTest plan
import randomand zero legacynp.random.seed()calls remain in xgi source🤖 Generated with Claude Code