- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在处理从大图像文件创建的相当大的数组。我遇到了使用过多内存的问题,因此决定尝试使用 numpy.memmap
数组而不是标准的 numpy.array
。我能够创建一个 memmap
并将数据从我的图像文件中分 block 加载到其中,但我不确定如何将操作结果加载到 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)
最佳答案
让我试着回答你问题的第一部分。将结果加载到 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
关于python - 来自 numpy 操作的 numpy.memmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19098241/
使用 numpy 的 有意义吗? memmap 跨多核(MPI)? 我在磁盘上有一个文件。 我可以创建一个单独的 memmap 每个核心上的对象,并使用它从文件中读取不同的切片? 给它写信怎么样? 最
我有一个包含电流和电压数据的非常大(10-100 GB)文件的内存映射。从给定的起始索引,我想找到电压满足给定条件的下一个点的索引。 如果列表相对较小,我可以使用迭代器来完成此操作,如下所示: fil
我试图在两个巨大的矩阵 (10*25,000,000) 之间生成一个普通的矩阵乘法。当我这样做时,我的内存力耗尽了。我如何使用 numpy 的 memmap 来处理这个问题?这是个好主意吗?我不太担心
我需要在内存中保存一个非常大的向量,大小大约为 10**8,并且我需要对其进行快速随机访问。我尝试使用 numpy.memmap,但遇到以下错误: RuntimeWarning: overflow e
我无法删除 numpy.memmap 函数创建的文件 class MyClass def __init__(self): self.fp = np.memmap(filename, dty
我有一个大型 (75000 x 5 x 6000) 3D 数组,存储为 NumPy 内存映射。如果我只是像这样迭代第一个维度: import numpy as np import time a = n
是否可以在不知道形状的情况下加载 numpy.memmap 并仍然恢复数据的形状? data = np.arange(12, dtype='float32') data.resize((3,4)) f
是否可以使用 numpy.memmap将基于磁盘的大型字符串数组映射到内存中? 我知道它可以用于 float 等,但这个问题专门针对字符串。 我对固定长度和可变长度字符串的解决方案都很感兴趣。 解决方
我有一个庞大的数据集,我希望对其进行 PCA。我受限于 RAM 和 PCA 的计算效率。因此,我转而使用迭代 PCA。 数据集大小-(140000,3504) documentation声明 此算法具
我正在尝试创建随机矩阵并使用 numpy.save 将其保存在二进制文件中 然后我尝试使用 numpy.memmap 映射这个文件,但它似乎映射错误。 如何解决? 它似乎读取了 .npy header
我感到困惑的操作看起来像这样。我一直在常规 Numpy 数组上这样做,但在 memmap 上我想了解它是如何工作的。 arr2 = np.argsort(np.argsort(arr1,axis=0)
我对 numpy 的 memmap 感到困惑使用写时复制 (mmap_mode=c) 时处理对数据的更改。由于没有将任何内容写入磁盘上的原始数组,因此我希望它必须将所有更改存储在内存中,因此如果您修改
我正在尝试使用一个非常大的 numpy 数组,使用 numpy memmap,将每个元素作为 ctypes 结构访问。 class My_Structure(Structure): _fiel
如何将大图像读取为 numpy.memmap 对象,以便可以操纵它们的像素并将它们保存回来?提前致谢。 最佳答案 在不知道更多细节的情况下,我会使用 scipy.misc.imread . Scipy
我想创建一个 memmap在 MATLAB 中。在 python 中,我可以通过以下方式做到这一点: ut = np.memmap('my_array.mmap', dtype=np.float64,
我有一个时间戳数组,矩阵 X 的第 2 列中的每一行都递增。我计算了时间戳的平均值,它大于最大值。我正在使用 numpy memmap 进行存储。为什么会这样? >>> self.X[:,1] mem
使用 numpy 的 memmap 打开设备文件(而不是常规文件)是否有原因不起作用? self.surface = np.memmap('/dev/fb1', dtype=np.uint16, mo
我正在处理一堆大型 numpy 数组,由于最近这些数组开始占用太多内存,我想用 numpy.memmap 实例替换它们。问题是,有时我必须调整数组的大小,我最好就地这样做。这对普通数组非常有效,但在
我在 x32 win xp 上使用 python x32 有时程序上线失败 fp = np.memmap('C:/memmap_test', dtype='float32', mode='w+', s
我使用 memmap=8G$4G Linux 内核启动参数保留内存块。 是否需要ioremap此内存? ioremap 手册页说: ioremap performs a platform specif
我是一名优秀的程序员,十分优秀!