Search code examples
pythonnumpyscikit-imageuint32

KeyError with np.uint32 and numpy.uint32


First off the Error I get:

import numpy as np    
dtype_range = {np.bool_: (False, True),
           np.bool8: (False, True),
           np.uint8: (0, 255),
           np.uint16: (0, 65535),
           np.int8: (-128, 127),
           np.int16: (-32768, 32767),
           np.int64: (-2**63, 2**63 - 1),
           np.uint64: (0, 2**64 - 1),
           np.int32: (-2**31, 2**31 - 1),
           np.uint32: (0, 2**32 - 1),
           np.float32: (-1, 1),
           np.float64: (-1, 1)}

dtype_range[image.dtype.type]

>>>KeyError: <type 'numpy.uint32'>

Crazy thing is, when I do

print image.dtype.type
>>><type 'numpy.uint32'>

Now the main problem is, that this happens in a standard lib of skimage.io, in dtype.py to be exact, and I can not change that source code. So I am wondering how or what can I change in passing my argument image to make it work? My guess would be that there is a problem with the namespace, since it is np and numpy? But how could I influence my data to save it as np.uint32 instead of numpy.uint32??

I am using Python 2.7 btw.

Help would be much appreciated.

EDIT:

I casted image to uint8 by

image = image.astype(uint8)

now this did not throw me an Error, even though

print image.astype(uint8).dtype.type
>>><type 'numpy.uint8'>

which you would expect, of course.

This resolved my issue. But if anyone knows what happened here, I would be glad for an answer just to understand the problem. Thanks anyways :)

EDIT:

Well now this is copied from the output of the IPython console:

 image = np.array([35, 37, 39, 36, 34, 31, 33, 32, 32, 33, 31, 33, 30, 34, 36, 37, 36,
   32, 33, 30, 28, 30, 28, 28, 29, 30, 29, 31, 30, 31, 36, 33, 34, 31,
   34, 35, 34, 32, 29, 26, 25, 27, 25, 26, 25, 27, 30, 30, 28, 26, 28,
   30, 32, 34, 36, 36, 36, 32, 36, 37, 34, 34, 35, 33, 33, 30, 33, 36,
   36, 36, 33, 33, 39, 38, 34, 32, 32, 29, 28, 29, 30, 32, 32, 28, 30,
   32, 34, 30, 28, 32, 34, 34, 35, 33, 35, 33, 33, 35, 37, 39], dtype=uint32)

Solution

  • I couldn't reproduce your error using Python 2.7.12 (Spyder 3.1.4) and NumPy 1.11.1. When I run this snippet:

    import numpy as np
    
    image = np.array([[35, 37, 39, 36, 34, 31], 
                      [33, 32, 32, 33, 31, 33], 
                      [30, 34, 36, 37, 36, 32],
                      [33, 30, 28, 30, 28, 28]], dtype=np.uint32)
    
    dtype_range = {np.bool_: (False, True),
                   np.bool8: (False, True),
                   np.uint8: (0, 255),
                   np.uint16: (0, 65535),
                   np.int8: (-128, 127),
                   np.int16: (-32768, 32767),
                   np.int64: (-2**63, 2**63 - 1),
                   np.uint64: (0, 2**64 - 1),
                   np.int32: (-2**31, 2**31 - 1),
                   np.uint32: (0, 2**32 - 1),
                   np.float32: (-1, 1),
                   np.float64: (-1, 1)}
    
    print(image.dtype.type)
    print(dtype_range[image.dtype.type])
    

    I got this output:

    <type 'numpy.uint32'>
    (0, 4294967295L)
    

    My guess is that the issue arises when you cast image to uint32 somewhere in the code. Could you share image and show us the full code?