0

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)
Nils
  • 910
  • 8
  • 30
  • 1
    There is something wrong with your copy paste. The `print` statement should print `` and not just `numpy.uint32`. Otherwise your error is that your are indexing your dict with a string rather than with a type. – JohanL Jun 22 '17 at 14:14
  • Yes you are right, I had not actually printed it in the source code, but executed it in the Ipython console of spyder, that is why my output looked different. I changed it to the correct form. – Nils Jun 22 '17 at 14:46

1 Answers1

1

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?

Tonechas
  • 13,398
  • 16
  • 46
  • 80
  • I added a part of `image` because the whole thing is quite big, but I checked and the error arises still, also with this small part of `image` – Nils Jun 28 '17 at 14:17
  • I replaced `dtype=uint32` by `dtype=np.uint32` in your code and got no error – Tonechas Jun 28 '17 at 14:23
  • Where did you replace it? – Nils Jun 29 '17 at 09:16
  • I've just edited my answer. Can you confirm whether you get an error when running the modified code? – Tonechas Jun 29 '17 at 10:44
  • Well I get my variable `image` from another function so I can not just change the dtype from `uint` to `np.uint` in the script.. Or can I? – Nils Jul 06 '17 at 10:35
  • You have different options here: 1) `image.dtype = np.uint32`, 2) `image = np.uint32(image)` – Tonechas Jul 06 '17 at 10:40
  • It worked thank you very much for your help :) Even though I still do not understand why Python has an issue with the namespace – Nils Jul 07 '17 at 13:10