You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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
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.
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).
@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.
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.
Activity
sciunto commentedon Aug 1, 2016
I think the docstring provides a good reason for clipping negative values
anntzer commentedon Aug 1, 2016
That's not really a "reason"? It's just the statement of a fact.
sciunto commentedon Aug 1, 2016
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.
sciunto commentedon Sep 4, 2016
@scikit-image/core We need other point of views ;)
soupault commentedon Sep 4, 2016
I think that we might do better here.
The most clear solution, in my opinion, could be to add
expand_n_clipparameter toimg_as_...with the neat defaults (expand if True, clip otherwise).soupault commentedon Sep 5, 2016
xref #1229, #1234
jni commentedon Sep 9, 2016
@soupault I don't know a priori what your
expand_n_clipparameter 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_tofunctions and prefer those, including adding parameters that define the rescaling behaviour.soupault commentedon Jan 2, 2017
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.
stefanv commentedon Feb 15, 2017
img_as_u*should probably fail on negative values.Either way, we should fix the warnings to be sensible.