Asymmetric behavior in dtype conversion

Tony Yu tsyu80 at gmail.com
Wed Dec 21 04:30:46 EST 2011


I noticed that---when converting unsigned integers to (signed)
integers---the values of the uint are simply scaled to fit into the
positive range of the int array. On the other hand, when converting from
int to uint, the *full range* (i.e. negative and positive) values of the
int array get scaled to fit into the uint. The result is asymmetric, and it
can lead to drift when converting between types; e.g.:

#~~~
In [55]: skimage.img_as_ubyte(skimage.img_as_int(np.uint8([0])))
WARNING:dtype_converter:Possible sign loss when converting negative image
of type int16 to positive image of type uint8.
WARNING:dtype_converter:Possible precision loss when converting from int16
to uint8
Out[55]: array([128], dtype=uint8)
#~~~

I think one of two things should happen:

1)
    [uint -> int]: expand uint to *full* range of int.
    [int -> uint]: unchanged (squeeze into range of uint)

2)
    [uint -> int]: unchanged (fill *positive* range of int)
    [int -> uint]: clip negative values from int

Thoughts?
-Tony

P.S. This change would either have to wait for PR
99<https://github.com/scikits-image/scikits-image/pull/99>,
or it could be integrated into that pull request. (I haven't yet looked at
what it would take to make this change.)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scikit-image/attachments/20111221/7e26ff64/attachment.html>


More information about the scikit-image mailing list