Skip to content
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

prefer use of new-style NumPy random API in tests #5450

Merged
merged 7 commits into from
Sep 8, 2021

Conversation

grlee77
Copy link
Contributor

@grlee77 grlee77 commented Jul 2, 2021

Description

This PR follows up on #5357, mainly removing residual use of np.random.RandomState from test classes. This changes here are mostly trivial, but I have a couple specific points that may be worth considering:

1.) Regarding segmentation.slice and segmentation.quickshift. For those two, there are a lot of tests which check specific label values as below:

assert_equal(seg[:10, :10], 0)
assert_equal(seg[10:, :10], 2)
assert_equal(seg[:10, 10:], 1)
assert_equal(seg[10:, 10:], 3)

Is it really important to check that the specific values occur in this order? If not, we could replace with something like just verifying there is only a single, unique label within each region (The correct overall number of labels is already checked separately)

assert np.unique(seg[:10, :10]).size == 1

etc.

2.) There is one residual use of RandomState used when performing slic with a mask.

rnd = random.RandomState(123)

There are a couple of test results that seem to assume the existing initial seed points based on the current RandomState seed, so if we really want to remove that we have to update those test cases with new expected results. I just added a comment to that effect rather than switching it here. Similarly for a specific seed used to compare to precomputed results in one of the unsupervised_wiener test cases.

Checklist

For reviewers

  • Check that the PR title is short, concise, and will make sense 1 year
    later.
  • Check that new functions are imported in corresponding __init__.py.
  • Check that new features, API changes, and deprecations are mentioned in
    doc/release/release_dev.rst.

@grlee77 grlee77 added the 🔧 type: Maintenance Refactoring and maintenance of internals label Jul 2, 2021
@pep8speaks
Copy link

pep8speaks commented Jul 2, 2021

Hello @grlee77! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2021-08-31 02:47:48 UTC

@grlee77
Copy link
Contributor Author

grlee77 commented Jul 2, 2021

This PR currently is only for modernizing the random call styles in the tests and does not make any API or behavior changes. I think we can address unifying random_state vs. random_seed etc. in a separate PR.

@grlee77 grlee77 changed the title prefer use of default_rng Generator over older RandomState in the test suite prefer use of new-style NumPy random API in tests Jul 2, 2021
@mkcor
Copy link
Member

mkcor commented Jul 15, 2021

  1. Is it really important to check that the specific values occur in this order?

No, indeed, since the order of labels as 'integers' (which they are not) doesn't bear any specific meaning. By the way, I'm in favour of closing this issue: #5049

If not, we could replace with something like just verifying there is only a single, unique label within each region.

Absolutely!

(The correct overall number of labels is already checked separately)

All good then.

@mkcor
Copy link
Member

mkcor commented Jul 15, 2021

@grlee77 the merge conflict here was expected from merging #5220.

@mkcor
Copy link
Member

mkcor commented Jul 15, 2021

  1. There are a couple of test results that seem to assume the existing initial seed points based on the current RandomState seed, so if we really want to remove that we have to update those test cases with new expected results. I just added a comment to that effect rather than switching it here.

Oh, iirc #5357 did that (updating expected results) for a few tests. Ok, let's leave these remaining pieces to a subsequent PR.

@grlee77
Copy link
Contributor Author

grlee77 commented Jul 15, 2021

Oh, iirc #5357 did that (updating expected results) for a few tests. Ok, let's leave these remaining pieces to a subsequent PR.

Perhaps a bigger concern in the case of slic is whether downstream projects or users were using a specific seed when calling slic. If we change to the new generator within that function, then they would suddenly get a different initialization. I don't think the old interface is going away, so I wasn't sure it was worth updating in this case.

Copy link
Member

@alexdesiqueira alexdesiqueira left a comment

Choose a reason for hiding this comment

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

LGTM; thanks @grlee77!

@@ -56,18 +53,16 @@ def test_structural_similarity_image():
@pytest.mark.parametrize('seed', [1, 2, 3, 5, 8, 13])
@pytest.mark.parametrize('dtype', [np.float16, np.float32, np.float64])
def test_structural_similarity_grad(seed, dtype):
N = 30
N = 60
Copy link
Member

Choose a reason for hiding this comment

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

Why this change, just out of curiosity? 🙂

@mkcor mkcor merged commit b0f20b8 into scikit-image:main Sep 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔧 type: Maintenance Refactoring and maintenance of internals
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants