当我尝试重塑numpy数组时,为什么memmap需要文件名?

时间:2013-04-13 09:06:11

标签: python arrays numpy pickle

我得到的堆栈跟踪我觉得很难理解:

screwed_up_code.py in atleast_4d(arr)
     28 def atleast_4d(arr):
     29     stshape = arr.shape
     30     while len(stshape)<4: stshape+=(1,)
     31     print arr.shape, stshape
---> 32     return arr.reshape(stshape)

/usr/lib/python2.7/dist-packages/numpy/core/memmap.pyc in __array_finalize__(self, obj)
    255         if hasattr(obj, '_mmap'):
    256             self._mmap = obj._mmap
--> 257             self.filename = obj.filename
    258             self.offset = obj.offset
    259             self.mode = obj.mode

AttributeError: 'memmap' object has no attribute 'filename'

如果您想知道arr.shape = (192, 384, 6)stshape = (192, 384, 6, 1)


更新

根据NPE的建议,我调查了the bug report of a similar sounding AttributeError。一张海报将其归咎于由于腌制ndarray而迷失的属性。我确实在腌制数组,并在重新激活已加载的数组时如下:

newarr = numpy.ndarray(pickled_array)
pickled_array = newarr                  # use the recreated instead of the pickled arr

我收到警告而非异常,我的代码运行:

Exception AttributeError: AttributeError("'NoneType' object has no attribute 'tell'",) in  ignored
Exception AttributeError: AttributeError("'NoneType' object has no attribute 'tell'",) in  ignored
Exception AttributeError: AttributeError("'NoneType' object has no attribute 'tell'",) in <bound method memmap.__del__ of memmap([  85389.2734375,  125935.75     ,  173624.09375  ,  272958.78125  ,
        305687.65625  ,  433026.3125   ], dtype=float32)> ignored

我很高兴我的代码可以运行,并暂时保留它。

1 个答案:

答案 0 :(得分:3)

这听起来与this bug非常相似。

stack trace有一点不同,但代码最终在完全相同的情况下以完全相同的异常失败:

 File "C:\Python26\Lib\site-packages\numpy\core\memmap.py", line 257, in __array_finalize__
   self.filename = obj.filename
AttributeError: 'memmap' object has no attribute 'filename'

首次报告的numpy-discussion thread表明该问题可能与this other ticketmemmap对象的腌制有关。无论如何,该线程值得一读。