为了帮助 heltonbiker 的响应,快速完成它很重要。就像重新组装你的“瓷砖”一样。如果您无法将大数组加载到内存中,则需要将其作为 memmapped 文件加载...但要将其作为 memmapped 文件,您需要先创建一个。
这是一个粗略的伪代码。
#you need to know how big your data matrix is
#todo: assign nrows and ncols based on your data.
fp = np.memmap('your_new_memmap_filename.dat', dtype='float32', mode='w+', shape=(nrows, ncols))#create file with appropriate dimensions
data = open('yourdatafile.txt', 'r')
i = 0
for line in data:
arr = map(float, line.strip().split(',')) #comma delimited? header row?
fp[i, :] = arr
i += 1
del fp #see documentation...del will flush the output and close the file.
现在处理..可以继续或新脚本
convolve_matrix = somenumpyarray
fp_result = np.memmap('your_results_memmap_filename.dat', dtype='float32', mode='w+', shape=(nrows, ncols))
#open big file read only
fp = np.memmap('your_new_memmap_filename.dat', dtype='float32', mode='r', shape=(nrows, ncols))
chunksize = 10000 #?
for i in range(int(nrows/chunksize) - 1): #don't forget the remainder at the end
chunk = fp[i * chunksize: (i + 1) * chunksize, :]
res = fftconvolve(chunk, convolve_matrix)
fp_result[i * chunksize: (i + 1) * chunksize, :] = res
#deal with remainder
del fp_result
del fp
请注意,此伪代码不重叠,您需要填补一些空白。此外,一旦您开始使用拼贴,请确保您使用 Joblib 并并行处理拼贴。 https://pythonhosted.org/joblib/parallel.html
抱歉,我不能提供更多代码,我有一个为 gis 制作的 2-d tiler/reassembler,但它不在这台计算机上。它甚至可能没有太大帮助,因为您的 tiler 不会返回实际的切片,而是切片列表,可能是几个获取切片的位置(在大数组上)的列表,将其放在结果中的位置(大结果数组)以及切片的位置切片的结果(从大数组中抓取的切片的卷积结果)......遍历切片列表,处理将很容易。但是制作切片函数会很棘手。
for source_slice, result_slice, mini_slice in zip(source_slice, result_slice, mini_slice):
matrix2convolve = big_fp[source_slice[0]:source_slice[1], :]
convolve_result = fftconvolve(matrix2convolve, convolve_matrix)
big_result_fp[result_slice[0]:result_slice[1], :] = convolve_result[mini_slice[0]:mini_slice[1], :]