【问题标题】:numpy.memmap from numpy operations来自 numpy 操作的 numpy.memmap
【发布时间】:2013-10-06 13:17:17
【问题描述】:

我正在处理从大图像文件创建的相当大的数组。我遇到了使用过多内存的问题,因此决定尝试使用numpy.memmap 数组而不是标准的numpy.array。我能够创建一个memmap 并将数据从我的图像文件中分块加载到其中,但我不确定如何将操作结果加载到memmap 中。

例如,我的图像文件作为二进制整数数组读入numpy。我编写了一个函数,可以将True 单元格的任何区域缓冲(扩展)指定数量的单元格。此函数使用array.astype(bool) 将输入数组转换为Boolean。如何将array.astype(bool) 创建的新Boolean 数组设为numpy.memmap 数组?

此外,如果有一个True 单元格比指定的缓冲区距离更靠近输入数组的边缘,则该函数将向数组的边缘添加行和/或列,以允许在现有True 单元格。这会改变数组的形状。是否可以更改numpy.memmap 的形状?

这是我的代码:

def getArray(dataset):
    '''Dataset is an instance of the GDALDataset class from the
    GDAL library for working with geospatial datasets

    '''
    chunks = readRaster.GetArrayParams(dataset, chunkSize=5000)
    datPath = re.sub(r'\.\w+$', '_temp.dat', dataset.GetDescription())
    pathExists = path.exists(datPath)
    arr = np.memmap(datPath, dtype=int, mode='r+',
                    shape=(dataset.RasterYSize, dataset.RasterXSize))
    if not pathExists:
        for chunk in chunks:
            xOff, yOff, xWidth, yWidth = chunk
            chunkArr = readRaster.GetArray(dataset, *chunk)
            arr[yOff:yOff + yWidth, xOff:xOff + xWidth] = chunkArr
    return arr

def Buffer(arr, dist, ring=False, full=True):
    '''Applies a buffer to any non-zero raster cells'''
    arr = arr.astype(bool)
    nzY, nzX = np.nonzero(arr)
    minY = np.amin(nzY)
    maxY = np.amax(nzY)
    minX = np.amin(nzX)
    maxX = np.amax(nzX)
    if minY - dist < 0:
        arr = np.vstack((np.zeros((abs(minY - dist), arr.shape[1]), bool),
                         arr))
    if maxY + dist >= arr.shape[0]:
        arr = np.vstack((arr,
                         np.zeros(((maxY + dist - arr.shape[0] + 1), arr.shape[1]), bool)))
    if minX - dist < 0:
        arr = np.hstack((np.zeros((arr.shape[0], abs(minX - dist)), bool),
                         arr))
    if maxX + dist >= arr.shape[1]:
        arr = np.hstack((arr,
                         np.zeros((arr.shape[0], (maxX + dist - arr.shape[1] + 1)), bool)))
    if dist >= 0: buffOp = binary_dilation
    else: buffOp = binary_erosion
    bufDist = abs(dist) * 2 + 1
    k = np.ones((bufDist, bufDist))
    bufArr = buffOp(arr, k)
    return bufArr.astype(int)

【问题讨论】:

  • in this answer解释了如何处理memmaps中的不连续数据,也许对你有帮助...
  • @SaulloCastro - 感谢您的链接。我不确定这在这里如何应用。我不认为我的情况与不连续的数据有关,但我是 numpy 的新手,所以我可能错了。
  • 我发布了该问题的链接,因为它提供了一些有关如何将偏移量处理到 memmap 中的信息,以便访问给定的数据块,这在您的情况下是需要的。
  • 那么您是否建议我将每个操作的输出保存到同一个 memmap 但具有不同的偏移量?
  • 是的,您甚至可以使用并行运行的不同进程同时访问同一个 memmap

标签: python arrays memory numpy


【解决方案1】:

让我试着回答你问题的第一部分。将结果加载到 memmap 数据存储中。

注意我假设磁盘上已经有一个 memmap 文件——它将是输入文件。调用MemmapInput,创建如下:

fpInput = np.memmap('MemmapInput', dtype='bool', mode='w+', shape=(3,4))
del fpInput
fpOutput = np.memmap('MemmapOutput', dtype='bool', mode='w+', shape=(3,4))
del fpOutput

在您的情况下,输出文件可能不存在,但根据文档: 'r+' 打开现有文件进行读写。

‘w+’创建或覆盖现有文件进行读写。

所以第一次创建 memmap 文件时必须使用 'w+',之后修改/覆盖文件,使用 'r+',可以使用 'r' 获得只读副本。请参阅http://docs.scipy.org/doc/numpy/reference/generated/numpy.memmap.html 了解更多信息。

现在我们将读入这个文件并对其执行一些操作。重点是将结果加载到 memamp 文件中,首先必须创建 memmap 文件并附加到文件中。

fpInput = np.memmap('MemmapInput', dtype='bool', mode='r', shape=(3,4))
fpOutput = np.memmap('MemmapOutput', dtype='bool', mode='r+', shape=(3,4))

对 fpOutput memmap 文件做任何你想做的事情,例如:

i,j = numpy.nonzero(fpInput==True)
for indexI in i:
  for indexJ in j:
    fpOutput[indexI-1,indexJ] = True
    fpOutput[indexI, indexJ-1] = True
    fpOutput[indexI+1, indexJ] = True
    fpOutput[indexI, indexJ+1] = True

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 2021-09-19
    • 2012-06-24
    • 2016-07-30
    • 2012-03-03
    • 1970-01-01
    相关资源
    最近更新 更多