【问题标题】:Why does a memmap need a filename when I try to reshape a numpy array?当我尝试重塑 numpy 数组时,为什么 memmap 需要文件名?
【发布时间】:2013-04-13 09:06:11
【问题描述】:

我得到一个我觉得难以理解的堆栈跟踪:

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。那里的一张海报将其归咎于由于ndarrays的酸洗而丢失的属性。我确实在酸洗数组,当像这样重新激活加载的数组时:

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

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

【问题讨论】:

  • 如果您包含 SSCCE (sscce.org),我们会为您提供帮助。
  • 我知道 - 我希望我能。我正在尝试的所有 SSCCE 的问题是它们实际上可以工作并且不会产生这个问题。可以看到atleast_4d是一个非常良性的小功能。我正在提交一个 numpy 形状的数组(192、384、6)。当我给它一个新创建的相同大小的数组时,它不会发生。不知何故,我的代码以某种方式准备数组,使其在 atleast_4d 内部爆炸。
  • 听起来像这样的可能在当前的 numpy 中已修复。 (虽然那可能是 1.7.x)

标签: python arrays numpy pickle


【解决方案1】:

这听起来与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 ticket 有关,与memmap 对象的酸洗有关。无论如何,该线程值得一读。

【讨论】:

  • 您可能正在做某事。创建这个ndarray 涉及一些酸洗。谢谢 - 将调查并回来。
  • @DrSAR:当然。阅读我刚刚链接到的讨论主题。
  • 是的,我有这让我相信你是对的。此后,我编辑了显示数据的问题以确认您的发现。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-05
  • 1970-01-01
  • 1970-01-01
  • 2020-05-05
  • 1970-01-01
相关资源
最近更新 更多