np.uint32 和 numpy.uint32 的 KeyError
KeyError with np.uint32 and numpy.uint32
首先我得到的错误是:
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'>
疯狂的是,当我这样做时
print image.dtype.type
>>><type 'numpy.uint32'>
现在的主要问题是,这发生在 skimage.io 的标准库中,确切地说是在 dtype.py 中,我无法更改该源代码。所以我想知道在传递我的论点 image
以使其起作用时我可以如何改变或改变什么?我的猜测是命名空间有问题,因为它是 np
和 numpy
?但是我如何影响我的数据以将其保存为 np.uint32
而不是 numpy.uint32
?
我正在使用 Python 2.7 顺便说一句。
不胜感激。
编辑:
我通过
将图像投射到 uint8
image = image.astype(uint8)
现在这并没有给我一个错误,即使
print image.astype(uint8).dtype.type
>>><type 'numpy.uint8'>
这当然是您所期望的。
这解决了我的问题。但是,如果有人知道这里发生了什么,我很乐意回答只是为了了解问题。无论如何谢谢:)
编辑:
好吧,现在这是从 IPython 控制台的输出中复制的:
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)
我无法使用 Python 2.7.12 (Spyder 3.1.4) 和 NumPy 1.11.1 重现您的错误。当我 运行 这个片段时:
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])
我得到了这个输出:
<type 'numpy.uint32'>
(0, 4294967295L)
我的猜测是当您在代码中的某处将 image
转换为 uint32
时出现问题。您可以分享 image
并向我们展示完整代码吗?
首先我得到的错误是:
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'>
疯狂的是,当我这样做时
print image.dtype.type
>>><type 'numpy.uint32'>
现在的主要问题是,这发生在 skimage.io 的标准库中,确切地说是在 dtype.py 中,我无法更改该源代码。所以我想知道在传递我的论点 image
以使其起作用时我可以如何改变或改变什么?我的猜测是命名空间有问题,因为它是 np
和 numpy
?但是我如何影响我的数据以将其保存为 np.uint32
而不是 numpy.uint32
?
我正在使用 Python 2.7 顺便说一句。
不胜感激。
编辑:
我通过
将图像投射到uint8
image = image.astype(uint8)
现在这并没有给我一个错误,即使
print image.astype(uint8).dtype.type
>>><type 'numpy.uint8'>
这当然是您所期望的。
这解决了我的问题。但是,如果有人知道这里发生了什么,我很乐意回答只是为了了解问题。无论如何谢谢:)
编辑:
好吧,现在这是从 IPython 控制台的输出中复制的:
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)
我无法使用 Python 2.7.12 (Spyder 3.1.4) 和 NumPy 1.11.1 重现您的错误。当我 运行 这个片段时:
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])
我得到了这个输出:
<type 'numpy.uint32'>
(0, 4294967295L)
我的猜测是当您在代码中的某处将 image
转换为 uint32
时出现问题。您可以分享 image
并向我们展示完整代码吗?