Skip to content

Seed every random draw through an rng argument following SPEC 7 - #1237

Merged
Zethson merged 5 commits into
mainfrom
spec7-rng
Dec 4, 2026
Merged

Zethson merged 5 commits into
mainfrom
spec7-rng

Conversation

@Zethson

@Zethson Zethson commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

LLM usage

  • LLM or coding agent used: Claude Opus 5.5 via Claude Code
  • If one was used, I, a human, have reviewed, understood and tested every changed line

Every pertpy function that draws random numbers takes an rng argument following SPEC 7, replacing random_state, seed and rng_key and adding it where draws were unseeded, including the JAX-based tools.
As in scanpy 1.13, an explicit rng goes through np.random.default_rng, while an omitted rng and the former names keep their previous results.

Every function that seeds NumPy, scikit-learn, pynndescent or the standard library takes `rng: SeedLike | RNGLike | None` instead of `random_state`, `seed` or `rng_key`.
`SeedLike` and `RNGLike` are SPEC 7's public aliases and replace `RandomStateLike`.
The defaults stay the same, so tools seeded by default remain seeded.

The old names stay accepted as keyword aliases through `_transition_to_rng`, silently as in SPEC 7's first phase.
Unlike the SPEC's reference decorator, it only renames and does not pass an explicit `rng` through `np.random.default_rng`.
Integer seeds therefore keep their random streams: `f()`, `f(rng=<default>)` and `f(<old name>=<default>)` give the results of main, and the public methods that call each other by keyword (Augur, Distance, scCODA) keep their legacy semantics.
Generators and other seeds go through `np.random.default_rng`, and `_int_seed` turns them into the integer that scikit-learn, pynndescent and JAX need.
`None` at those sites now draws fresh entropy instead of following `np.random.seed`.

Milo's `make_nhoods` and Augur's subsampling no longer call `random.seed`, which reseeded the global standard library generator; they sample from a local `random.Random` with the same seed and draw the same cells and features.
Augur keeps `None` as "seed every subsample by its index", which matches R Augur, and a generator passed to `predict` now gives each subsample its own seed rather than one draw per joblib worker copy.
Dialogue records `rng` instead of `random_state` in its params.

Seeds that only reach JAX (`assign_mixture_model`, scGen's latent sampling) keep `seed`, since SPEC 7 covers NumPy random number generation.
Functions with an unseeded generator and no seed argument (DistanceTest, Scgen.predict, the classifier distances, Mixscape's plot jitter) and constructor kwargs forwarded to scikit-learn or pynndescent are unchanged.
`_transition_to_rng` now behaves like SPEC 7's reference decorator and scanpy 1.13.
An `rng` passed by keyword is normalized with `np.random.default_rng` and drawn from, so an integer gives a new random stream where it used to seed scikit-learn, pynndescent, JAX or the standard library directly.
An omitted `rng`, the former keyword and a positional value keep their former meaning as a `_LegacySeed`, with the former default (0, 42, 1234, 2024, or None) passed to the decorator, so existing code and default calls reproduce main.
Signatures show `rng=None`, and the docstrings name the seed an omitted `rng` falls back to.
Legacy seeds pass through the public methods that call each other by keyword (Augur, Distance, scCODA) unchanged, so a chain stays on one path.
At scikit-learn and pynndescent sites a legacy seed, `None` included, is passed as before, so `np.random.seed` keeps working until the former names are removed.

Augur's omitted `rng` keeps seeding every subsample by its index like R Augur, while an explicit `rng` draws the subsamples, features, label permutations and folds from the generator.
Milo's explicit `rng` samples neighbourhood vertices with `Generator.choice`.

JAX seeds move to `rng` as well: `assign_mixture_model`, `Scgen.get_latent_representation` and `Scgen.get_decoded_expression` (former name `seed`), with keys drawn from the generator on the new path.
Functions that drew from an unseeded generator take `rng=None`: `DistanceTest` and its `test_xy` and `test_precomputed`, `Scgen.predict`, the `classifier_proba` distance and `Mixscape.plot_perturbscore`.
`KMeansSpace.compute` and `Mixscape.perturbation_signature` take `rng` too, with the `random_state` they forwarded to KMeans and NNDescent as the former name.
@Zethson Zethson changed the title Seed with an rng argument following SPEC 7 Seed every random draw through an rng argument following SPEC 7 Dec 4, 2026
Zethson added a commit to scverse/pertpy-tutorials that referenced this pull request Dec 4, 2026
Needs scverse/pertpy#1237 merged first, since the scCODA/tascCODA case study now seeds `run_nuts` with `rng` instead of `rng_key`.
An explicit `rng=1234` draws from a NumPy generator, so re-executing the case study gives different MCMC samples than the stored outputs, while the Augur tutorial only drops `random_state=None`, which `predict` uses when `rng` is omitted.
Points at scverse/pertpy-tutorials#87, which seeds the scCODA/tascCODA case study with rng and drops the redundant random_state=None from the Augur tutorial.
The guide assignment mixture (2024), MLPClassifierSpace (42) and Dialogue (1234) now seed with 0 when rng is omitted, like every other seeded tool, so their default results change; the former keywords with an explicit value keep theirs.
The docstrings drop the note on the omitted seed.
They exist only for the SPEC 7 deprecation period and go once the former names are removed, while _types keeps the public SeedLike and RNGLike aliases.
@Zethson
Zethson merged commit c342cf3 into main Dec 4, 2026
7 of 23 checks passed
@Zethson
Zethson deleted the spec7-rng branch December 4, 2026 12:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant