Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python numpy操作中的numpy.memmap_Python_Arrays_Memory_Numpy - Fatal编程技术网

Python numpy操作中的numpy.memmap

Python numpy操作中的numpy.memmap,python,arrays,memory,numpy,Python,Arrays,Memory,Numpy,我正在使用由大型图像文件创建的相当大的数组。我遇到了使用太多内存的问题,决定尝试使用numpy.memmap数组,而不是标准的numpy.array。我能够创建一个memmap,并将图像文件中的数据分块加载到其中,但我不确定如何将操作结果加载到memmap 例如,我的图像文件作为二进制整数数组读入numpy。我已经编写了一个函数,它将True单元格的任何区域缓冲(扩展)指定数量的单元格。此函数使用array.astype(bool)将输入数组转换为Boolean。如何创建由array.astyp

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

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

此外,如果有一个
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)
def getArray(数据集): ''Dataset是来自 用于处理地理空间数据集的GDAL库 ''' chunks=readRaster.GetArrayParams(数据集,chunkSize=5000) datPath=re.sub(r'\.\w+$','\u temp.dat',dataset.GetDescription()) pathExists=path.exists(datPath) arr=np.memmap(datPath,dtype=int,mode='r+', 形状=(dataset.RasterYSize,dataset.RasterXSize)) 如果不存在: 对于块中的块: xOff,yOff,xWidth,yWidth=chunk chunkArr=readRaster.GetArray(数据集,*chunk) arr[yOff:yOff+yWidth,xOff:xOff+xWidth]=chunkArr 返回arr def缓冲区(arr、dist、ring=False、full=True): ''将缓冲区应用于任何非零光栅单元'' arr=arr.astype(bool) nzY,nzX=np.非零(arr) minY=np.amin(nzY) maxY=np.amax(nzY) minX=np.amin(nzX) maxX=np.amax(nzX) 如果最小距离<0: arr=np.vstack((np.zero((abs(minY-dist),arr.shape[1]),bool), arr) 如果maxY+dist>=arr.shape[0]: arr=np.vstack((arr, np.zero(((maxY+dist-arr.shape[0]+1),arr.shape[1]),bool))) 如果最小距离小于0: arr=np.hstack((np.zero((arr.shape[0],abs(minX-dist)),bool), arr) 如果maxX+dist>=arr.shape[1]: arr=np.hstack((arr, np.zero((arr.shape[0],(maxX+dist-arr.shape[1]+1)),bool))) 如果dist>=0:buffOp=binary\u膨胀 else:buffOp=二元腐蚀 bufDist=abs(距离)*2+1 k=np.ones((bufDist,bufDist)) bufArr=buffOp(arr,k) 返回bufArr.astype(int)
让我试着回答你问题的第一部分。将结果加载到memmap数据存储中

注意,我假设磁盘上已经有一个memmap文件——它将是输入文件。名为MemmaInput,创建方式如下:

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”可以获得只读副本。有关更多信息,请参阅

现在我们将读入该文件并对其执行一些操作。要点是要将结果加载到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

它解释了如何处理memmaps中的不连续数据,也许它会帮助你解决…@SaulloCastro-谢谢你的链接。我不确定这在这里是怎么回事。我不认为我的情况与不连续的数据有关,但是我对NoMPY是新的,所以我可能是错的。我把链接发布到那个问题上,因为它提供了一些关于如何将偏移量处理到<代码> MeMMAP>代码>中的信息,以便访问给定的数据块,在您的情况下,这是必需的。那么您是否建议我将每个操作的输出保存到同一个memmap,但使用不同的偏移量?是的,您甚至可以使用并行运行的不同进程同时访问同一个memmap