如果我正确理解了您的问题,您输入的“观察”数据会定期网格化吗?
如果是这样,scipy.ndimage.map_coordinates 正是您想要的。
在第一次通过时有点难以理解,但本质上,您只需为其提供一系列坐标,您希望在像素/体素/n 维索引坐标中插入网格值。
作为 2D 示例:
import numpy as np
from scipy import ndimage
import matplotlib.pyplot as plt
# Note that the output interpolated coords will be the same dtype as your input
# data. If we have an array of ints, and we want floating point precision in
# the output interpolated points, we need to cast the array as floats
data = np.arange(40).reshape((8,5)).astype(np.float)
# I'm writing these as row, column pairs for clarity...
coords = np.array([[1.2, 3.5], [6.7, 2.5], [7.9, 3.5], [3.5, 3.5]])
# However, map_coordinates expects the transpose of this
coords = coords.T
# The "mode" kwarg here just controls how the boundaries are treated
# mode='nearest' is _not_ nearest neighbor interpolation, it just uses the
# value of the nearest cell if the point lies outside the grid. The default is
# to treat the values outside the grid as zero, which can cause some edge
# effects if you're interpolating points near the edge
# The "order" kwarg controls the order of the splines used. The default is
# cubic splines, order=3
zi = ndimage.map_coordinates(data, coords, order=3, mode='nearest')
row, column = coords
nrows, ncols = data.shape
im = plt.imshow(data, interpolation='nearest', extent=[0, ncols, nrows, 0])
plt.colorbar(im)
plt.scatter(column, row, c=zi, vmin=data.min(), vmax=data.max())
for r, c, z in zip(row, column, zi):
plt.annotate('%0.3f' % z, (c,r), xytext=(-10,10), textcoords='offset points',
arrowprops=dict(arrowstyle='->'), ha='right')
plt.show()
要在 n 维中做到这一点,我们只需要传入适当大小的数组:
import numpy as np
from scipy import ndimage
data = np.arange(3*5*9).reshape((3,5,9)).astype(np.float)
coords = np.array([[1.2, 3.5, 7.8], [0.5, 0.5, 6.8]])
zi = ndimage.map_coordinates(data, coords.T)
就缩放和内存使用而言,map_coordinates 将在您使用 > 1 的阶数(即非线性插值)时创建数组的过滤副本。如果您只想在极少数点进行插值,这是一个相当大的开销。但是,它不会随着您要插值的点数而增加。只要有足够的 RAM 用于输入数据数组的单个临时副本,就可以了。
如果您无法在内存中存储数据副本,您可以 a) 指定 prefilter=False 和 order=1 并使用线性插值,或者 b) 使用 ndimage.spline_filter 将原始数据替换为过滤版本,然后使用prefilter=False 调用map_coordinates。
即使您有足够的内存,如果您需要多次调用 map_coordinates(例如交互式使用等),保留过滤后的数据集可能会大大加快速度。