I have a look-up table (LUT) that stores 65536 uint8
values:
lut = np.random.randint(256, size=(65536,)).astype('uint8')
I want to use this LUT to convert the values in an array of uint16
s:
arr = np.random.randint(65536, size=(1000, 1000)).astype('uint16')
and I want to do the conversion in place, because this last array can get pretty big. When I try it, the following happens:
>>> np.take(lut, arr, out=arr)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\numpy\core\fromnumeric.py", line 103, in take
return take(indices, axis, out, mode)
TypeError: array cannot be safely cast to required type
And I don't understand what is going on. I know that, without an out
argument, the return is of the same dtype as lut
, so uint8
. But why can't a uint8
be cast to a uint16
? If you ask numpy:
>>> np.can_cast('uint8', 'uint16')
True
Obviously the following works:
>>> lut = lut.astype('uint16')
>>> np.take(lut, arr, out=arr)
array([[173, 251, 218, ..., 110, 98, 235],
[200, 231, 91, ..., 158, 100, 88],
[ 13, 227, 223, ..., 94, 56, 36],
...,
[ 28, 198, 80, ..., 60, 87, 118],
[156, 46, 118, ..., 212, 198, 218],
[203, 97, 245, ..., 3, 191, 173]], dtype=uint16)
But this also works:
>>> lut = lut.astype('int32')
>>> np.take(lut, arr, out=arr)
array([[ 78, 249, 148, ..., 77, 12, 167],
[138, 5, 206, ..., 31, 43, 244],
[ 29, 134, 131, ..., 100, 107, 1],
...,
[109, 166, 14, ..., 64, 95, 102],
[152, 169, 102, ..., 240, 166, 148],
[ 47, 14, 129, ..., 237, 11, 78]], dtype=uint16)
This really makes no sense, since now int32
s are being cast to uint16
s, which is definitely not a safe thing to do:
>>> np.can_cast('int32', 'uint16')
False
My code works if I set the lut
's dtype to anything in uint16
, uint32
, uint64
, int32
or int64
, but fails for uint8
, int8
and int16
.
Am I missing something, or is this simply broken in numpy?
Workarounds are also welcome... Since the LUT is not that big, I guess it is not that bad to have its type match the array's, even if that takes twice the space, but it just doesn't feel right to do that...
Is there a way to tell numpy to not worry about casting safety?