New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DOC: update random and asserts in test guidelines #18777
Conversation
Using |
Oh sorry yes, I got confused with the other PR on SciPy which is about the examples and not the tests. I will remove this. Is the rest fine? |
Apparently there is an issue with using |
I am not sure about the global state though. Shouldn't it advertise at least for |
In the confines of a unit test, the global doesn't really present a problem. It even makes it a smidge easier to do the seeding in a |
assert_almost_equal | ||
assert_approx_equal | ||
assert_array_almost_equal |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If these are not recommended, can you add a ..note
in the docstring for each method that explains why, and what the alternative is?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is already a note in these functions pointing out to assert_allclose
and alike. That's why I am listing these specifically.
Are you talking about something else?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also I am not sure how to hide these. Because if I am not mistaking they are only defined here so I would need to do the autosummary
. But then I don't know how to hide.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I was not aware that we already had those notes.
Perhaps instead of hiding these you should just put them under a different heading, along with a remark saying that they are not recommended?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK sure. And also I am still bugged with how to do it otherwise 😅. So this? Asserts (not recommanded)
a86919e
to
2cbaaba
Compare
Doesn't this result in non-deterministic tests if pytest decides to run tests in multiple threads, such as with pytest-parallel? |
My 2 cents. For testing most people just need to draw number from a random uniform distribution. At most from a normal. Then instead of fighting with seeds and complicated algo, I would recommend to use simple deterministic sequences such as Halton. These are fixed and if someone want a different "seed", they can just advance the sequence. This way you are always guaranty to have the same exact sequence. |
2cbaaba
to
f47f64e
Compare
@rkern @eric-wieser I updated the PR with your suggestions. As for the Instead of setting the seed globally, it is recommanded to use NumPy's API::
rng = np.random.RandomState(some_number)
random_numbers = rng.random(...) |
@rkern I am curious about this. Why would you be hesitant to recommend using an
|
I agree. If we must stick with
Can't agree more as currently we have 2 cases to handle and it leads to confusions. |
I'm not. I said to recommend it as the primary method. I definitely would prefer that people do that in new code; their tests will be a little bit better for it. I'm saying not to disrecommend By all means, document |
Not really. People do know enough to set the seed in the |
My reading of the following is that I should add the text I proposed. This is ok? Instead of setting the seed globally, it is recommanded to use NumPy's API::
rng = np.random.RandomState(some_number)
random_numbers = rng.random(...) |
If they already are getting a |
@rkern would it be acceptable to recommend
|
Sorry didn't notice this one. The changes in |
Ahh sorry didn't see you already had a PR open for some related changes @tupui . I think my PR had a bit more explanatory text and content related to |
Co-authored-by: Matthias Bussonnier <bussonniermatthias@gmail.com>
Anything else? I believe this is ready. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The assert-related changed LGTM.
+1 to all @rkern said about unit testing. The current version of this PR doesn't include and random
changes, which is right.
Thanks @tupui , I was waiting to see if someone else wanted to comment. |
Thanks, no problem 😃 My first commits in NumPy, hopefully not the last ones! |
I propose to update the testing guide.
Changenp.random.seed
to using `np.random. default_rng.assert_
to plainassert
as the statement about python stripping them out is not relevant in this context.assert_almost_equal
and alike as better alternative are recommended.There was a discussion over at SciPy which suggested that this guide needed updates.
xref scipy/scipy#8583