Skip to content

ENH: Add private RNG normalizer - #14199

Merged
larsoner merged 35 commits into
mne-tools:mainfrom
bruAristimunha:rng-foundation
Aug 25, 2026
Merged

ENH: Add private RNG normalizer#14199
larsoner merged 35 commits into
mne-tools:mainfrom
bruAristimunha:rng-foundation

Conversation

@bruAristimunha

@bruAristimunha bruAristimunha commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Closes #9233

@larsoner

Copy link
Copy Markdown
Member

I think we need to support legacy RandomState because scikit-learn still supports it (and actually does not support Generator IIRC), can you double check?

@bruAristimunha
bruAristimunha force-pushed the rng-foundation branch 2 times, most recently from 636e95e to cd46252 Compare August 24, 2026 11:55
scikit-learn accepts RandomState but not Generator instances
(check_random_state, FastICA, KFold all reject generators), so the new
rng parameter must pass legacy RandomState instances through unchanged.
Simplifies the ICA infomax branch accordingly, which also removes the
last np.random.RandomState spelling flagged by test_no_global_rng.
The _legacy_rng decorator now normalizes and injects the rng keyword
itself, making the redundant body-level _check_rng_compat calls (and
the helper) unnecessary at all 21 transition sites. ICA.__init__ keeps
explicit handling so integer rng seeds stay intact for third-party
random_state parameters during fitting.
The normalized-generator indirections duplicated every parameter list
just to forward rng: drop _permutation_cluster_test_normalized (publics
call the private implementation directly again, as before), un-split
infomax back into a single decorated function, and derive
seed_deprecated docdict from random_state_deprecated.
Drop per-domain assertions of decorator semantics that are covered once
centrally in test_check (both-supplied TypeError, FutureWarning,
plain int-vs-RandomState parity), and parametrize the spatio-temporal
sibling functions. Domain-specific behavior keeps explicit pins: the
per-event stream restart quirk, the ICA sklearn boundary, and nested
wrapper delegation.
Comment thread doc/changes/dev/14199.apichange.rst Outdated
Comment thread mne/tests/test_docstring_parameters.py Outdated
Comment thread mne/inverse_sparse/mxne_inverse.py Outdated
@@ -366,6 +367,8 @@ def mixed_norm(
sure_alpha_grid="auto",
random_state=None,

@larsoner larsoner Aug 25, 2026

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.

Okay hopefully one last thing 🤞 For signatures like this, let's move the * to before the random-state. Some people might need to update their scripts to pass random_state= but probably not that many since this is so far down the list. And we've been trying to add * to callables anyway in reasonable places. Once that's done, random_state can be grouped with rng (and ideally placed immediately after it).

@larsoner

Copy link
Copy Markdown
Member

@bruAristimunha one last-last idea... we've talked about how our random_state int values being 0 and 42 are not great. We should ideally randomly set these, at least in our examples and tutorials. Could you pick random values for those? The tricky part is that some of them might no longer work as expected, but in theory looking at the CircleCI output before and after to make a) things still execute without warnings and b) still look okay should make sure things are okay. You could also run examples locally but this might take a while...

@bruAristimunha

Copy link
Copy Markdown
Contributor Author

doing @larsoner :)

@larsoner

Copy link
Copy Markdown
Member

Minimal CI is unhappy, you need some exception for sklearn because it's not installed there

=================================== FAILURES ===================================
______________________________ test_no_global_rng ______________________________
[gw2] linux -- Python 3.12.14 /opt/hostedtoolcache/Python/3.12.14/x64/bin/python
mne/tests/test_docstring_parameters.py:379: in test_no_global_rng
    callables = _sklearn_callables(tree)
                ^^^^^^^^^^^^^^^^^^^^^^^^
mne/tests/test_docstring_parameters.py:340: in _sklearn_callables
    module = importlib.import_module(node.module)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/opt/hostedtoolcache/Python/3.12.14/x64/lib/python3.12/importlib/__init__.py:90: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
<frozen importlib._bootstrap>:1387: in _gcd_import
    ???
<frozen importlib._bootstrap>:1360: in _find_and_load
    ???
<frozen importlib._bootstrap>:1310: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:488: in _call_with_frames_removed
    ???
<frozen importlib._bootstrap>:1387: in _gcd_import
    ???
<frozen importlib._bootstrap>:1360: in _find_and_load
    ???
<frozen importlib._bootstrap>:1324: in _find_and_load_unlocked
    ???
E   ModuleNotFoundError: No module named 'sklearn'
- generated xml file: /home/runner/work/mne-python/mne-python/junit-results.xml -

@larsoner

Copy link
Copy Markdown
Member

(I'll cancel the others to save cycles)

@bruAristimunha

Copy link
Copy Markdown
Contributor Author

Local seed-grid check for the simulated time-frequency example (0, 42, 181, and 271). The intended 50 Hz burst and smoothing trade-off remain visible with the new seed (181).

Multitaper result across fixed seeds

@larsoner
larsoner marked this pull request as ready for review August 25, 2026 15:02

@larsoner larsoner 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.

Just had to fix a tiny bug with reST rendering, marking for merge-when-green, thanks in advance @bruAristimunha !

@larsoner
larsoner enabled auto-merge (squash) August 25, 2026 15:06
@larsoner
larsoner merged commit 2602acf into mne-tools:main Aug 25, 2026
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

API: RandomState deprecation, default_rng, and SPEC7

2 participants