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

fix wrong error for metric.structural_similarity when image is too small #5395

Merged
merged 14 commits into from
Jul 17, 2021
Merged

Conversation

lilisako
Copy link
Contributor

Description

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.

@pep8speaks
Copy link

pep8speaks commented May 14, 2021

Hello @lilisako! 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-07-15 14:52:02 UTC

@@ -239,3 +240,6 @@ def test_invalid_input():
structural_similarity(X, X, K2=-0.1)
with testing.raises(ValueError):
structural_similarity(X, X, sigma=-1.0)
# image is too small
Copy link
Member

Choose a reason for hiding this comment

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

Can you also add a test for when win_size exceeds image extent? Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mkcor Thanks for the review!
I think it was already added here!

# win_size exceeds image extent
with testing.raises(ValueError):
structural_similarity(X, X, win_size=X.shape[0] + 1)

If I am misunderstanding something, please let me know!

Copy link
Member

Choose a reason for hiding this comment

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

Oh, right! My intention would be to distinguish between the two cases... maybe:

    # win_size exceeds image extent                                            
    with testing.raises(ValueError) as cm:                                      
        structural_similarity(X, X, win_size=X.shape[0] + 1)
        assert 'win_size exceeds image extent' in cm.exception.msg

and

    # image is too small                                                       
    with testing.raises(ValueError) as cm:                                     
        structural_similarity(Z, Z)                                            
        assert 'Image is too small' in cm.exception.msg

?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see! Thanks for kindly letting me know. I just pushed new commits regarding these changes.

@mkcor
Copy link
Member

mkcor commented Jun 2, 2021

Hi @lilisako,

I've thought about the update and I wouldn't actually make this if/else distinction; it's quite artificial: You could have 6x6 or 5x5 images and compute their structural similarity, it's just that you would need to specify win_size=5 or win_size=3. I think I would leave it as one error message, describing several possibilities, mentioning the multichannel case last.

Indeed,

from skimage.metrics import structural_similarity
import numpy as np

im1 = np.ones((5, 5))
im2 = np.zeros((5, 5))

structural_similarity(im1, im2, win_size=3)
structural_similarity(im1, im2, win_size=5)
structural_similarity(im1, im2)  # ValueError (it is True that "win_size exceeds image extent.")

Therefore, I would suggest:
"win_size exceeds image extent. Either ensure that your images are at least 7x7; or pass win_size explicitly in the function call, with an odd value less or equal to the smaller side of your images. If your images are multichannel (with color channels), set channel_axis to the axis number corresponding to the channels."

Best,
Marianne

@lilisako
Copy link
Contributor Author

lilisako commented Jul 5, 2021

Hi, @mkcor !
Sorry for being late to get back to you. I changed the error message and I explicitly added the test cases for small images. Could you re-review the code when you have time?

Thanks in advance.
Risako

skimage/metrics/_structural_similarity.py Outdated Show resolved Hide resolved
skimage/metrics/_structural_similarity.py Outdated Show resolved Hide resolved
skimage/metrics/tests/test_structural_similarity.py Outdated Show resolved Hide resolved
skimage/metrics/tests/test_structural_similarity.py Outdated Show resolved Hide resolved
skimage/metrics/tests/test_structural_similarity.py Outdated Show resolved Hide resolved
@mkcor
Copy link
Member

mkcor commented Jul 13, 2021

Hi @lilisako,

Thank you for your patience! I left some suggestions when reviewing your latest version. In case you're not familiar with it, let me draw your attention to the possibility of applying suggested changes in batches: https://docs.github.com/en/github/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/incorporating-feedback-in-your-pull-request

Co-authored-by: Marianne Corvellec <marianne.corvellec@ens-lyon.org>
@lilisako
Copy link
Contributor Author

@mkcor
Thanks for the review! I've just pushed the suggestions into one commit. I appreciate for taking time to correct these changes🙏

skimage/metrics/_structural_similarity.py Outdated Show resolved Hide resolved
Co-authored-by: Marianne Corvellec <marianne.corvellec@ens-lyon.org>
@alexdesiqueira alexdesiqueira merged commit 2f6abf3 into scikit-image:main Jul 17, 2021
@alexdesiqueira
Copy link
Member

Thank you @lilisako!

Comment on lines +169 to +175
"win_size exceeds image extent. ",
"Either ensure that your images are ",
"at least 7x7; or pass win_size explicitly ",
"in the function call, with an odd value ",
"less than or equal to the smaller side of your ",
"images. If your images are multichannel ",
"(with color channels), set channel_axis to ",
Copy link

@Pomax Pomax Jul 17, 2021

Choose a reason for hiding this comment

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

small note. but: python does not want commas here. When splitting a long string over multiple lines, there should be no commas, as adding them means you're now specifying multiple arguments to the ValueError constructor, instead of a single argument.

Copy link
Member

Choose a reason for hiding this comment

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

Very true, good catch! Waiting for your PR to fix this 😄

Copy link

@Pomax Pomax Jul 17, 2021

Choose a reason for hiding this comment

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

See the issue this closes as for why I won't be submitting that. But if you think it's just a matter of seconds to make one, you're more than welcome to do that instead =)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @Pomax for pointing out the issue. I fixed this in newly created PR here #5480 .

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.

metric.structural_similarity throws the wrong error for small sizes
5 participants