API: RandomState deprecation
See original GitHub issuenp.random.RandomState is not a good choice for random number generation going forward:
RandomState is effectively frozen and will only receive updates that are required by changes in the the internals of Numpy. More substantial changes, including algorithmic improvements, are reserved for Generator.
I don’t understand all the details, but the TL;DR I’ve gathered from monitoring some SciPy discussions is that it’s not as good as the new default_rng
/ Generator
-based way of making random numbers and it should be replaced in people’s code.
In the long term I think we want to switch random_state=0
to use default_rng(random_state)
instead of RandomState(random_state)
internally. I propose:
- Have a
mne.set_rng_backend(backend)
that can be'RandomState'
or'Generator'
, maybe with ause_rng_backend
context manager. The context manager is maybe overkill, but I do think it might help people transition code from old to new, and it’s only a few lines for us. - If this has not been called by the user yet, if they do
random_state=<int>
they will see a DeprecationWarning stating that the default is'RandomState'
in 0.23 but will change to'Generator'
in 0.24 (or maybe this is a good case for making it 0.25 to give people more time to change over), and we will callset_rng_backend('RandomState')
. - In 0.24 (or 0.25) we no longer emit this warning and just use
'Generator'
.
I think this is a workable solution for modernizing our random_state
params.
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (6 by maintainers)
Top GitHub Comments
Hi everyone! I’m the cause of everyone’s problems here. As you work through these issues, I do recommend reading NEP 19 which goes into more detail about why we had the old backwards-compatibility policy and why we moved away from it. The concerns you have brought up here were definitely in our thoughts. I’m happy to answer any questions that the NEP doesn’t answer.
I can’t speak definitively as to when/how/if scipy really deprecates
RandomState
per se, but I have my guess as to where we’re going on the aforementioned PR. Some time after we drop support for numpy 1.16, I expect that we will transition the code to useGenerator
instead ofRandomState
. I expect that we’ll still acceptRandomState
instances, but we will wrap its core uniform PRNG inGenerator
and call its methods instead. So if you callscipy.stats.norm.rvs(random_state=RandomState(1234))
, you will get the results fromGenerator.standard_normal()
's improved algorithm rather than the older, slowerRandomState.standard_normal()
. The core Mersenne Twister PRNG underneath the passedRandomState
will still be used, however. Again, we haven’t made any decisions, yet, but I think that’s where we are going.Sounds like a good plan.