Skip to content

Emit error in structural_similarity when data_range is not specified in case of floating point data.#6612

Merged
grlee77 merged 4 commits intoscikit-image:mainfrom
mcourteaux:main
Nov 18, 2022
Merged

Emit error in structural_similarity when data_range is not specified in case of floating point data.#6612
grlee77 merged 4 commits intoscikit-image:mainfrom
mcourteaux:main

Conversation

@mcourteaux
Copy link
Copy Markdown
Contributor

Additionally, emit warnings when integers other than uint8 are used.

Description

Closes #6602.

Checklist

  • Docstrings for all functions
  • Gallery example in ./doc/examples (new features only)
  • Benchmark in ./benchmarks, if your changes aren't covered by an
    existing benchmark
  • Unit tests
  • Clean style in the spirit of PEP8
  • Descriptive commit messages (see below)

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.
  • There is a bot to help automate backporting a PR to an older branch. For
    example, to backport to v0.19.x after merging, add the following in a PR
    comment: @meeseeksdev backport to v0.19.x
  • To run benchmarks on a PR, add the run-benchmark label. To rerun, the label
    can be removed and then added again. The benchmark output can be checked in
    the "Actions" tab.

…. Emit warnings when integers other than uint8.
@mcourteaux
Copy link
Copy Markdown
Contributor Author

I'm facing a broken test, due to the SSIM being used to validate the equality of total variation denoising, but due to not specifying data_range, the result was too generous. The test checks for SSIM > 0.99, but actually is between 0.98 and 0.99. I have no expert knowledge on this algorithm, nor test:

def test_denoise_tv_chambolle_weighting():
# make sure a specified weight gives consistent results regardless of
# the number of input image dimensions
rstate = np.random.default_rng(1234)
img2d = astro_gray.copy()
img2d += 0.15 * rstate.standard_normal(img2d.shape)
img2d = np.clip(img2d, 0, 1)
# generate 4D image by tiling
img4d = np.tile(img2d[..., None, None], (1, 1, 2, 2))
w = 0.2
denoised_2d = restoration.denoise_tv_chambolle(img2d, weight=w)
denoised_4d = restoration.denoise_tv_chambolle(img4d, weight=w)
assert structural_similarity(denoised_2d,
denoised_4d[:, :, 0, 0]) > 0.99

Now I don't know if the test should pass, and the threshold should be lowered to 0.98, or this test was broken and now we discover a bug in this test/code. @ahojnnes @mkcor @grlee77: you are all authors of the lines of this test, according to git blame. Can you have a look?

… Fix a broken test that was using structural_similarity incorrectly;
@mcourteaux
Copy link
Copy Markdown
Contributor Author

For now I lowered the threshold to 0.98 to just be able to run all tests with GH actions.

@stefanv
Copy link
Copy Markdown
Member

stefanv commented Nov 5, 2022

Seems reasonable to me. I hope one of those authors can take a look soon, just to confirm, then we can get this in.

Thank you very much for spotting and fixing this issue, @mcourteaux!

@mkcor
Copy link
Copy Markdown
Member

mkcor commented Nov 5, 2022

Hi @mcourteaux,

thank you so much for this follow-up PR!

Test test_denoise_tv_chambolle_weighting with the 0.99 threshold comes from PR #1839 (more specifically, commit 682f089), a major contribution by @grlee77 which dates back to ~7 years ago. My understanding is that the 0.99 threshold doesn't bear any specific meaning other than 'close enough to 1.0' in this test, which is checking for consistency (not accuracy). So I'm not shocked by the fact that we would need to relax this threshold to 0.98 now that you're enforcing the proper data range.

The bug was in the structural_similarity function and you're fixing it with this PR. I wouldn't say that test_denoise_tv_chambolle_weighting had a bug, it's just a weaker kind of test.

Reviewing now!

@mkcor mkcor mentioned this pull request Nov 5, 2022
9 tasks
Copy link
Copy Markdown
Member

@mkcor mkcor left a comment

Choose a reason for hiding this comment

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

Thank you, @mcourteaux! Your contribution is bringing such a great improvement. I've left suggestions which are essentially stylistic. Happy to approve after you've addressed them!

dmin, dmax = dtype_range[im1.dtype.type]
data_range = dmax - dmin
if np.issubdtype(im1.dtype, np.integer) and (im1.dtype != np.uint8):
warn("Setting data_range based on im1.dtype. " +
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Unrelated to this PR, but I'm just frustrated we still have im1, im2 around... cf. https://github.com/scikit-image/scikit-image/pull/4187/files#r329088276

@mcourteaux
Copy link
Copy Markdown
Contributor Author

Applied your suggestions, @mkcor. I guess the only thing left to do is the checklist for reviewers, regarding change notes etc.

@mcourteaux mcourteaux requested a review from mkcor November 8, 2022 13:40
Copy link
Copy Markdown
Member

@mkcor mkcor left a comment

Choose a reason for hiding this comment

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

Thank you, @mcourteaux!

@lagru this is coming up for #6556 😉

@mcourteaux
Copy link
Copy Markdown
Contributor Author

What's the plan? Is it normal it's not merged yet?

@mkcor
Copy link
Copy Markdown
Member

mkcor commented Nov 18, 2022

Hi @mcourteaux,

I think that's because we are in between funding programs (and, hence, contracts), so we are currently short on manpower! cf. https://blog.scientific-python.org/scientific-python/2022-czi-grant/

@stefanv if you could have a look 🙏

Copy link
Copy Markdown
Contributor

@grlee77 grlee77 left a comment

Choose a reason for hiding this comment

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

Thanks @mcourteaux, this looks great. I will commit the suggested small whitespace fixes then should be able to merge.

@grlee77 grlee77 merged commit db55f38 into scikit-image:main Nov 18, 2022
@jarrodmillman jarrodmillman added this to the 0.20 milestone Nov 18, 2022
@mcourteaux
Copy link
Copy Markdown
Contributor Author

Cool! Thanks everyone! Happy to have contributed something of this type to the scientific community. 😄

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.

SSIM should fail unless data_range specified (especially for float)

6 participants