Skip to content

Fix RNG in ssim testing to enable threaded testing - #8196

Open
stefanv wants to merge 1 commit into
scikit-image:mainfrom
stefanv:temp-mark-ssim-tests-thread-unsafe
Open

stefanv wants to merge 1 commit into
scikit-image:mainfrom
stefanv:temp-mark-ssim-tests-thread-unsafe

Conversation

@stefanv

@stefanv stefanv commented May 26, 2026

Copy link
Copy Markdown
Member

No description provided.

@greptile-apps

greptile-apps Bot commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

Comments Outside Diff

These findings sit on lines the diff does not cover, so they could not be posted inline. Each one leaves this list once its file changes.

  • P2 The root cause of the thread-unsafety is likely the use of np.random.rand which draws from a shared global random state . The sibling test test structural similarity grad already documents this exact problem and works around it by using np.random.default rng seed with a small set of parametrized seeds. Applying the same pattern here would let the test run in threaded mode again without needing the thread unsafe marker. tests/skimage2/metrics/test_structural_similarity.py:90 ▶

    The root cause of the thread-unsafety is likely the use of np.random.rand (which draws from a shared global random state). The sibling test test_structural_similarity_grad already documents this exact problem and works around it by using np.random.default_rng(seed) with a small set of parametrized seeds. Applying the same pattern here would let the test run in threaded mode again without needing the thread_unsafe marker.

    @pytest.mark.parametrize('seed', [1, 2, 3])
    @pytest.mark.parametrize(
        'dtype', [np.uint8, np.int32, np.float16, np.float32, np.float64]
    )
    def test_structural_similarity_dtype(dtype, seed):
        N = 30
        rng = np.random.default_rng(seed)
        X = rng.random((N, N))
        Y = rng.random((N, N))
    

@ngoldbaum

Copy link
Copy Markdown
Contributor

I'll try to take a closer look at this

@stefanv

stefanv commented May 26, 2026

Copy link
Copy Markdown
Member Author

I think greptile found the problem, so pushed a fix here.

@stefanv stefanv changed the title Temporarily disable threaded ssim testing Fix RNG in ssim testing to enable threaded testing May 26, 2026
@greptile-apps

greptile-apps Bot commented May 26, 2026

Copy link
Copy Markdown
Contributor

Reviews (2): Last reviewed commit: "Fix rng for SSIM test to be run threaded" | Re-trigger Greptile

@greptile-apps

greptile-apps Bot commented May 26, 2026

Copy link
Copy Markdown
Contributor

Reviews (3): Last reviewed commit: "Seed each testing function" | Re-trigger Greptile

@ngoldbaum ngoldbaum left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice! Glad it was the same pattern as all the other rng changes.

@stefanv

stefanv commented Jun 2, 2026

Copy link
Copy Markdown
Member Author

@michaelbratsch This older PR was supposed to rework the RNGs for structural similarity. Do you mind taking a look, now that yours has been merged, whether we should apply the rest?

@michaelbratsch

Copy link
Copy Markdown
Contributor

test_structural_similarity_multichannel still uses np.random.rand:

X = (np.random.rand(N, N) * 255).astype(np.uint8)

@michaelbratsch

Copy link
Copy Markdown
Contributor

@michaelbratsch This older PR was supposed to rework the RNGs for structural similarity. Do you mind taking a look, now that yours has been merged, whether we should apply the rest?

I think it is reasonable to fix the seed for the unit tests to prevent flaky unit tests. It surely limits the test coverage but currently there is no way of knowing the seed so one can't debug anyway.

As a totally different approach, I tried in the past hypothesis testing (https://hypothesis.readthedocs.io/en/latest/) but in my experience it's really hard to follow through when it comes to numerical stability.

@matthew-brett
matthew-brett changed the base branch from main to pre-megamove-integration June 19, 2026 19:59
@matthew-brett

Copy link
Copy Markdown
Contributor

We have changed the base branch for this PR from the main branch to the pre-megamove-integration branch.

Summary - what this means for your PR

You can continue to work on your PR, now based on pre-megamove-integration. You should update your branch to the current state of pre-megamove-integration (this is what the Github "Update branch" button does). Please do not merge the scikit-image main branch into your PR branch.

Once we've merged your PR into the pre-megamove-integration branch, we'll update your changes for Megamove, and merge into the main branch.

In addition - our automated script detected that your branch had merge conflicts with the pre-Megamove branch. Please make sure to merge in the pre-megamove-integration branch, and resolve the merge conflicts in the way that you want.

Read on for the Megamove rationale, and more detailed instructions.

Details

Rationale

We've changed the base branch to make it easier and smoother to continue to work on your PR, while we work through a rather large change in the directory structure of the repository - a change that we have called Megamove.

See the Megamove explanation issue for more detail.

Changing the base branch means you do not see merge conflicts related to the Megamove change, and can continue to work with the repository in its original state, without worrying about porting those changes across to the new post-Megamove structure.

As you'll see in the linked issue, you can continue to work on your PR as before, and you can continue to use the Github GUI to "Update branch" - this updates from the new pre-megamove-integration branch. However, there is one caveat: please, do not try and merge the scikit-image main branch into this one, but instead, keep track of the pre-megamove-integration branch. The issue explains more.

To emphasize again, do not try and merge the scikit-image main branch. You will get Megamove related merge conflicts that are rather tricky to resolve correctly.

When we've approved your PR, we'll merge into the pre-megamove-integration branch, and then process it so we can merge it into the main branch. We'll let you know when we've merged your PR changes into main.

Any questions, please reply here.

blink1073 added a commit to stefanv/scikit-image that referenced this pull request Sep 27, 2026
Replace the module-level NumPy global RNG state in the skimage2
structural similarity tests with per-test local RNGs so the tests can
run under pytest-run-parallel. Use np.random.RandomState (per the
pattern established in scikit-image#8084) to keep the reference values that
cam_noisy depends on stable.

Resolves the merge conflict in test_structural_similarity_dtype and
addresses the review on scikit-image#8196.
@greptile-apps

greptile-apps Bot commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

This AI review is meant to assist maintainers with quick spot checks. You don't need to act on it until you hear from a maintainer.

Retrigger

[Low risk] Test file fixes random seeding for thread safety.

The PR appears safe to merge.

Reviews (5) · Last reviewed commit: "Seed each SSIM test with a local RNG"

Replace the module-level NumPy global RNG state in the skimage2
structural similarity tests with per-test local RNGs so the tests can
run under pytest-run-parallel. Use np.random.RandomState (per the
pattern established in scikit-image#8084) to keep the reference values that
cam_noisy depends on stable.

Resolves the merge conflict in test_structural_similarity_dtype and
addresses the review on scikit-image#8196.
@blink1073
blink1073 changed the base branch from pre-megamove-integration to main September 27, 2026 22:42
@blink1073
blink1073 force-pushed the temp-mark-ssim-tests-thread-unsafe branch from 5630301 to 93bd15e Compare September 27, 2026 22:45

@blink1073 blink1073 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, but I made some of the changes. 😄 . Feel free to merge when you're ready @stefanv

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants