Bri*_*ian 9 python memory arrays numpy
我正在使用从大图像文件创建的相当大的数组.我在使用太多内存时遇到了问题,并决定尝试使用numpy.memmap数组而不是标准numpy.array.我能够创建一个memmap并从我的图像文件中以块的形式加载数据,但我不确定如何将操作的结果加载到memmap.
例如,我的图像文件被读numpy作二进制整数数组.我编写了一个函数,用于缓冲(扩展)True指定数量单元格的任何单元格区域.此函数将输入数组转换为Boolean使用array.astype(bool).我将如何使新Boolean的阵列创建array.astype(bool)一个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)
Run Code Online (Sandbox Code Playgroud)
让我尝试回答你问题的第一部分。将结果加载到 memmap 数据存储中。
\n\n注意我假设磁盘上已经有一个 memmap 文件 - 它将作为输入文件。名为MemmapInput,创建如下:
\n\nfpInput = np.memmap(\'MemmapInput\', dtype=\'bool\', mode=\'w+\', shape=(3,4))\ndel fpInput\nfpOutput = np.memmap(\'MemmapOutput\', dtype=\'bool\', mode=\'w+\', shape=(3,4))\ndel fpOutput\nRun Code Online (Sandbox Code Playgroud)\n\n在您的情况下,输出文件可能不存在,但根据文档:\n\'r+\' 打开现有文件进行读写。
\n\n\xe2\x80\x98w+\xe2\x80\x99 创建或覆盖现有文件以进行读写。
\n\n所以第一次创建memmap文件时必须使用\'w+\',之后要修改/覆盖该文件,请使用\'r+\',使用\'r\'可以获得只读副本。有关更多信息,请参阅http://docs.scipy.org/doc/numpy/reference/ generated/numpy.memmap.html。
\n\n现在我们将读入该文件并对其执行一些操作。要点是将结果加载到 memamp 文件中,必须首先创建 memmap 文件并将其附加到文件中。
\n\nfpInput = np.memmap(\'MemmapInput\', dtype=\'bool\', mode=\'r\', shape=(3,4))\nfpOutput = np.memmap(\'MemmapOutput\', dtype=\'bool\', mode=\'r+\', shape=(3,4))\nRun Code Online (Sandbox Code Playgroud)\n\n使用 fpOutput memmap 文件做任何你想做的事情,例如:
\n\ni,j = numpy.nonzero(fpInput==True)\nfor indexI in i:\n for indexJ in j:\n fpOutput[indexI-1,indexJ] = True\n fpOutput[indexI, indexJ-1] = True\n fpOutput[indexI+1, indexJ] = True\n fpOutput[indexI, indexJ+1] = True\nRun Code Online (Sandbox Code Playgroud)\n