Skip to content

Move toward determinism - #14090

Merged
larsoner merged 6 commits into
mne-tools:mainfrom
larsoner:rand
Jul 23, 2026
Merged

larsoner merged 6 commits into
mne-tools:mainfrom
larsoner:rand

Conversation

@larsoner

@larsoner larsoner commented Jul 22, 2026 •

Copy link
Copy Markdown
Member

Noticing we had an un-seeded random state in our tests last week made me wonder how many other places we potentially had something like this. Turns out the answer is "a lot"! And I think it's better not to have it this way (creates unnecessary uncertainty / heisenbugs in tests, and makes unnecessary churn in our doc HTMLs). This PR

  1. Adds a little AST parser that looks for anything non-seeded-like (should only find RandomState, default_rng, stuff like that)
  2. Fixes some instances in tests by using rng = default_rng(0)
  3. Fixes some instances in tutorials by using random_state=97 and seed=0 (seems to be our convention for those, but we could potentially change/unify now)

Notably I didn't fix all such occurrences, but I could. The diff is already at the "kind of annoying to look at" level and I didn't want to push it. But if we agree on the approach and utility I could get through the others and shrink the global_rng_ignores toward zero!

Claude Opus 4.8 helped me draft these changes and the initial AST implementation.

@drammock drammock left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at the AST test, and it looks fine. didn't scrutinize the actual changes in code beyond a quick glance at a couple files. I'm definitely +1 on whittling global_rng_ignores down to zero; +1 to merge this and do a follow-up, or do it here if you prefer.

This is probably the point at which we should think ahead a bit:

  1. would we ever want a package-global random state/generator like mne.RNG that gets passed in wherever it's needed? My gut feeling is "don't do that for tests" (it just re-introduces non-determinism based on test execution order / parallelization) but IDK if it is better/neutral/worse for cases where we need RNG in package code.
  2. are there any lingering outdated patterns we should clean up? (I saw you fixed some randn -> rng.normal or standard_normal, are there other patterns to migrate away from?)
  3. Should we make the test also check for the outdated patterns like randn?

@larsoner

Copy link
Copy Markdown
Member Author

For randn it'll already find np.random.randn global state stuff. But rng.randn it won't... I can see if there are places we could update that, too, though.

We could right now think about migrating all of our code to prefer np.random.default_rng(state)... Ideally passing random_state as an int would do this, too, but it's backward incompatible / would produce different results. So I'd say here I can push consistency to use default_rng where possible (tests and examples) but not touch random_state=<int>... we'll have to think harder about how to handle that properly.

@larsoner

larsoner commented Jul 22, 2026 •

Copy link
Copy Markdown
Member Author

Okay I think I made it more consistent according to the plan above! I'll double check all changes in the diff then mark for merge-when-green 😅

@larsoner
larsoner enabled auto-merge (squash) July 23, 2026 00:19
@larsoner
larsoner merged commit 346d5a1 into mne-tools:main Jul 23, 2026
34 of 35 checks passed
@larsoner
larsoner deleted the rand branch July 23, 2026 00:33
@sappelhoff

Copy link
Copy Markdown
Member

@larsoner in how far does this PR address parts of that issue?

PragnyaKhandelwal pushed a commit to PragnyaKhandelwal/mne-python that referenced this pull request Jul 23, 2026
@larsoner

Copy link
Copy Markdown
Member Author

It doesn't address random_state=int at all. That still routes through RandomState. It just prefers and enforces default_rng instead of legacy usages in our code. So we still need to figure out how to handle int. And it's complicated by the fact that sklearn doesn't support the newer generator IIRC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants