Skip to content

misleading warning in img_as_uint #2215

Open

Description

@anntzer

Python 3.5.2, skimage 0.12.3

In [4]: skimage.img_as_uint(np.array([-3, -2]))
/usr/lib/python3.5/site-packages/skimage/util/dtype.py:106: UserWarning: 
Possible sign loss when converting negative image of type int64 to 
positive image of type uint16.
  "%s to positive image of type %s." % (dtypeobj_in, dtypeobj))
/usr/lib/python3.5/site-packages/skimage/util/dtype.py:136: UserWarning: 
Downcasting int64 to uint16 without scaling because max value -2 fits 
in uint16
  warn(msg)
Out[4]: array([0, 0], dtype=uint16)

Note how the second warning doesn't seem to make sense...
Now, it is true that the first warning should make the reader realize something is amiss, but thanks to the magic of python's warning cache, the first warning will only ever be printed once but the second one once per different max-value, so in practice the two warnings may appear rather far apart from each other.

Edit: In fact, I'd say it's a misfeature that img_as_uint clips negative data. I honestly can't see a case where that would be the expected behavior; as such I'd rather have it throw an exception when there's negative data. If someone really relies on that(!), they can always do the cast themselves.

Activity

  1. sciunto commented on Aug 1, 2016

    @sciunto
    Member

    I think the docstring provides a good reason for clipping negative values

        Numbers are not shifted to the negative side when converting from                                                                   
        unsigned to signed integer types. Negative values will be clipped when                                                              
        converting to unsigned integers
    
  2. anntzer commented on Aug 1, 2016

    @anntzer
    ContributorAuthor

    That's not really a "reason"? It's just the statement of a fact.

  3. sciunto commented on Aug 1, 2016

    @sciunto
    Member

    The reason, I guess, is to be consistent with the conversion unsigned to signed. It's a matter of choices and I think there is pro's and con's for each solution we can imagine. So far, i'm not convinced it's worth breaking the API here.

  4. added this to the 0.13 milestone on Aug 6, 2016
  5. sciunto commented on Sep 4, 2016

    @sciunto
    Member

    @scikit-image/core We need other point of views ;)

  6. soupault commented on Sep 4, 2016

    @soupault
    Member

    I think that we might do better here.
    The most clear solution, in my opinion, could be to add expand_n_clip parameter to img_as_... with the neat defaults (expand if True, clip otherwise).

  7. soupault commented on Sep 5, 2016

    @soupault
    Member

    xref #1229, #1234

  8. self-assigned this
    on Sep 7, 2016
  9. jni commented on Sep 9, 2016

    @jni
    Member

    @soupault I don't know a priori what your expand_n_clip parameter is doing, but a parameter would be welcome.

    Since you referenced #1234, I'll reiterate in this context: I think we should add the rescale_to functions and prefer those, including adding parameters that define the rescaling behaviour.

  10. soupault commented on Jan 2, 2017

    @soupault
    Member

    I'd say that it is not a good idea to commit to the solution of this issue before solving #1234.
    After looking into the code, the warnings in the issue message make complete sense. The thing is that once we use sign_loss()(i.e. notify the user on sign loss) it automatically means that we clip the negative values (however, we actually clip them later 🤕).
    I will fix this issue with the PR for #1234.

  11. modified the milestones: 0.14, 0.13 on Jan 2, 2017
  12. stefanv commented on Feb 15, 2017

    @stefanv
    Member

    img_as_u* should probably fail on negative values.

    Either way, we should fix the warnings to be sensible.

  13. modified the milestones: 0.14, 0.14.1 on May 29, 2018
  14. modified the milestones: 0.14.1, 0.15 on Aug 20, 2018
  15. modified the milestones: 0.15, 0.16 on Apr 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions