Memory management 使用scipy.ndimage的高效记忆高斯模糊

Memory management 使用scipy.ndimage的高效记忆高斯模糊,memory-management,scipy,gis,gaussian,Memory Management,Scipy,Gis,Gaussian,我正在尝试平滑大型GIS数据集(10000 x 10000阵列)。我目前的方法是将整个数组加载到内存中,平滑它,然后写回。看起来是这样的: big_array = band_on_disk.ReadAsArray() scipy.ndimage.gaussian_filter(big_array, sigma, output=smoothed_array) output_band.WriteArray(smoothed_array) 对于大型光栅,我会得到一个MemoryError,因此我想加

我正在尝试平滑大型GIS数据集(10000 x 10000阵列)。我目前的方法是将整个数组加载到内存中,平滑它,然后写回。看起来是这样的:

big_array = band_on_disk.ReadAsArray()
scipy.ndimage.gaussian_filter(big_array, sigma, output=smoothed_array)
output_band.WriteArray(smoothed_array)
对于大型光栅,我会得到一个
MemoryError
,因此我想加载该阵列的子块,但我不确定如何处理影响相邻子块的区域的高斯平滑

关于如何修复上述算法,使其在较小内存占用的情况下仍能正确平滑整个阵列,有何建议?

尝试使用文件

内存使用适中,速度快得可以承受 如果您能够负担得起内存中的一个阵列,那么这是可以承受的:

import numpy as np
from scipy.ndimage import gaussian_filter

# create some fake data, save it to disk, and free up its memory
shape = (10000,10000)
orig = np.random.random_sample(shape)
orig.tofile('orig.dat')
print 'saved original'
del orig

# allocate memory for the smoothed data
smoothed = np.zeros((10000,10000))

# memory-map the original data, so it isn't read into memory all at once
orig = np.memmap('orig.dat', np.float64, 'r', shape=shape)
print 'memmapped'

sigma = 10 # I have no idea what a reasonable value is here
gaussian_filter(orig, sigma, output = smoothed)
# save the smoothed data to disk
smoothed.tofile('smoothed.dat')
内存使用率低,速度非常慢 如果不能同时在内存中使用任意一个数组,则可以同时对原始数组和平滑数组进行内存映射。它的内存使用率非常低,但速度非常慢,至少在我的机器上是如此

您必须忽略此代码的第一部分,因为它一次欺骗并创建原始数组,然后将其保存到磁盘。您可以将其替换为代码,以加载以增量方式在磁盘上构建的数据

import numpy as np
from scipy.ndimage import gaussian_filter

# create some fake data, save it to disk, and free up its memory
shape = (10000,10000)
orig = np.random.random_sample(shape)
orig.tofile('orig.dat')
print 'saved original'
del orig

# memory-map the original data, so it isn't read into memory all at once
orig = np.memmap('orig.dat', np.float64, 'r', shape=shape)
# create a memory mapped array for the smoothed data
smoothed = np.memmap('smoothed.dat', np.float64, 'w+', shape = shape)
print 'memmapped'

sigma = 10 # I have no idea what a reasonable value is here
gaussian_filter(orig, sigma, output = smoothed)

能否将数组传递给
scipy.ndimage.gaussian_filter
?内存错误发生在哪里?假设您已经为
平滑_阵列
分配了内存,并且当您从磁盘加载
大_阵列
时会发生错误,我是否正确?谢谢@JohnVinyard,是的,当我为大_阵列分配内存时会发生错误。内存映射数组似乎适合我!