Repository navigation
MNT: Replace RandomState with default Generator - #10644
Conversation
mhvk
left a comment
There was a problem hiding this comment.
@pllim - I think this is the way to go. I've only followed the discussion about the random-number generator from a distance, but it is clear that the old design had annoying limitations and that the very large break was considered necessary. The new method names certainly seem a lot clearer!
On your question about older numpy: I'd vote for moving to numpy 1.17 in 4.2 - it also means the __array_function__ override is now default, not optional. And with numpy 1.20 getting close, it would seem we're at about the right schedule anyway.
|
p.s. The failures are of course rather annoying! For tests, we really shouldn't be using random data (unless consciously to flush out bugs, like with |
Perhaps not, but fixing that is out of scope, as I have no idea what those devs had in mind when they designed the tests. I'll see about updating the results based on local run after #10664 goes in. |
66b09b3 to
4229c81
Compare
|
I tried to update results for the failed tests, but I got stuck with the failures in the following:
I am also not sure if some of my changes make sense. The relevant subpackage maintainers should look at them carefully. |
| x[rng.integers(0, len(t) // 2, N)] = 1 | ||
| edges = bayesian_blocks(t, x, fitness='regular_events', dt=dt) | ||
| assert_allclose(edges, [0, 5.105, 9.99]) | ||
| assert_allclose(edges, [0, 4.365, 4.995, 9.99]) |
There was a problem hiding this comment.
Extra element! Is this supposed to happen?
There was a problem hiding this comment.
Based on other comments by @larrybradley , this is probably okay too.
|
|
||
| dx, bins = knuth_bin_width(X, return_bins=True) | ||
| assert_allclose(len(bins), 59) | ||
| assert_allclose(len(bins), 58) |
There was a problem hiding this comment.
Off by one! Is this supposed to happen?
There was a problem hiding this comment.
Yes, I think this is fine. Different bins for different data.
| counts, bins = histogram(X, bins='blocks') | ||
| assert_allclose(counts, [10, 61, 29]) | ||
| assert_allclose(bins, [-2.55298982, -1.24381059, 0.46422235, 2.26975462]) | ||
| assert_allclose(counts, [3, 97]) |
There was a problem hiding this comment.
One less element! Is this supposed to happen?
|
I need input from subpackage maintainers about the failures and unexpected result changes before I can proceed with this. |
|
|
||
| dx, bins = knuth_bin_width(X, return_bins=True) | ||
| assert_allclose(len(bins), 59) | ||
| assert_allclose(len(bins), 58) |
There was a problem hiding this comment.
Yes, I think this is fine. Different bins for different data.
| counts, bins = histogram(X, bins='blocks') | ||
| assert_allclose(counts, [10, 61, 29]) | ||
| assert_allclose(bins, [-2.55298982, -1.24381059, 0.46422235, 2.26975462]) | ||
| assert_allclose(counts, [3, 97]) |
539bda2 to
903b050
Compare
| def data(): | ||
| rand = np.random.RandomState(123) | ||
| t = rand.uniform(0, 10, 500) | ||
| t = np.array([ |
There was a problem hiding this comment.
If this is too hacky, I welcome better ideas.
There was a problem hiding this comment.
Probably should open a follow-up issue about this too... cc @adrn
501eb97 to
91dc9a1
Compare
|
RTD failure is unrelated ( |
astrofrog
left a comment
There was a problem hiding this comment.
Looks good to me but I think in a follow up we should really try and get rid of the hard coded arrays. I have some ideas for finding a seed so if someone opens the issue it can be assigned to me.
|
@astrofrog , if you can find the seed soon, maybe this PR can wait. If not, feel free to merge. Thanks. |
|
This seems ready but needs a rebase. |
There was a problem hiding this comment.
@pllim I am a bit late sorry. We did this in SciPy and the only difference is that we removed all seeding from examples. This is to enforce best practices. Using small seeds is bad for the entropy of the generator and also common values like 0, 42, 12345 end up in every code which leads to bias in results. Seeing should only be used for testing.
Here I am proposing to remove the seed and just allow variation in the results to pass the doctests (in SciPy I can add # random or # may vary, I am not sure if this is standard or not. Maybe this is your # doctest: +FLOAT_CMP?).
I can do a PR on this branch if you want some help. Also, I would suggest to add somewhere in the doc a paragraph about seeding. See what we did for example https://scipy.github.io/devdocs/tutorial/stats.html#random-number-generation.
| >>> from astropy.stats import biweight_location | ||
| >>> rand = np.random.RandomState(12345) | ||
| >>> biloc = biweight_location(rand.randn(1000)) | ||
| >>> rand = np.random.default_rng(12345) |
There was a problem hiding this comment.
| >>> rand = np.random.default_rng(12345) | |
| >>> rand = np.random.default_rng() |
| >>> biloc = biweight_location(rand.standard_normal(1000)) | ||
| >>> print(biloc) # doctest: +FLOAT_CMP | ||
| -0.0175741540445 | ||
| 0.01535330525461019 |
There was a problem hiding this comment.
| 0.01535330525461019 | |
| 0.01535330525461019 # random |
|
@tupui - that sounds like a very good idea, but we do not have the |
Then do you suggest to use |
|
Re: No seed for doctest -- |
|
@tupui - I'm suggesting that for this PR we leave the explicit seed in place and use |
adrn
left a comment
There was a problem hiding this comment.
Took a quick look at this - seems fine to make these changes after rebasing. But in a new issue we should discuss modifying examples to remove seeds.
One other thing that's not clear to me is whether the default bit generator could ever change in numpy? If it does, all the tests could start failing. So if that is a possibility, I wonder if we want to have a test helper that can produce a random number generator given a seed, but with the PCG64 bit generator hard coded? May not be an issue, but others might know...
| # NOTE: PR 10644 replaced deprecated usage of RandomState but could not | ||
| # find a new seed that did not cause test failure, resorted to hardcoding. |
There was a problem hiding this comment.
Hm - seems like this should maybe become a TODO, with a new issue for @nden?
There was a problem hiding this comment.
Yeah, haven't gotten any replies here, so follow-up issue is the way to go.
Sure makes sense. I can propose a following PR if that's ok then.
We had a similar discussion on SciPy. While the underlying bit generator might change, it's a bit unlikely as people are now using it. Still, from NumPy's documentation they explicitly warn about not wanting to ensure reproducible results over the versions. If you must support a lot of versions |
Attempt to fix most of the failures.
TST: Undo some of the earlier changes, not needed after hardcoding.
91dc9a1 to
ce6589d
Compare
|
I rebased. The one failure is unrelated. Given there are multiple approvals, I added some follow-up action items and will merge when CI finishes. Thanks for the reviews and discussions, everyone! p.s. FWIW, I don't think people should be using random numbers in tests but addressing that is out of scope of this PR. |
|
Yay! And, yes, I agree that one should not use random numbers in tests in the first place. |
|
Plus, in the end if the seed is fixed, it's not really a "random" number generator anymore 😉 |
|
I can't read minds, but I think the "random" numbers got into the tests in one of two ways:
I don't think randomness was the intention but rather means to an end. |
|
I think RTD is stuck due to concurrency limits. #yolo |
Description
This pull request is to address the fact that RandomState is now considered legacy.
However, during local testing, it appears that replacing it with default
Generatorisn't a simple search-and-replace. That is, the results change enough to cause test failures.Should we proceed with this or trying something else like our own NumpyRNGContext?
@mhvk , do you have any opinion on this?
Fix #11519
Choices
default_rngis not available in Numpy 1.16 or older.Do we copy it intoNot a problem now as we have dropped support for Numpy 1.16.compator something, or maybe wait to do this till we can drop support for Numpy 1.16?Use something else.Keep usingRandomState.TODO
If its milestone changes, the milestone here needs to be updated to match.After merge
astropy/timeseries/periodograms/bls/tests/test_bls.pyandastropy/modeling/tests/test_models.pyand assign to @astrofrog , see MNT: Replace RandomState with default Generator #10644 (review) (see TST: Revert hardcoding in test_bls.py and test_models.py #11567)