python - 快速内插网格数据

标签 python numpy scipy interpolation

我有一个大的3d np.ndarray数据,它表示以规则网格方式在某个卷上采样的物理变量(如array [0,0,0]中的值表示物理坐标(0,0,0 ))。

我想通过在粗糙网格中插值数据来获得更好的网格间距。目前,我正在使用scipy griddata线性插值法,但速度相当慢(20x20x20数组约为90秒)。就我的目的而言,它有些过分设计,可以对体积数据进行随机采样。有没有什么可以利用我定期排列的数据以及我想插值的特定点集有限这一事实呢?

最佳答案

当然!有两个选项可以做不同的事情,但都可以利用原始数据的规则网格性质。

第一个是 scipy.ndimage.zoom 。如果您只想基于对原始数据进行插值来生成更密集的规则网格,则可以采用这种方法。

第二个是 scipy.ndimage.map_coordinates 。如果您想在数据中插值几个(或多个)任意点,但仍然利用原始数据的规则网格性质(例如,不需要四叉树),那么这就是方法。

“缩放”数组( scipy.ndimage.zoom )

作为一个简单的示例(这将使用三次插值。对于双线性,使用order=1,对于最接近的使用order=0,等等):

import numpy as np
import scipy.ndimage as ndimage

data = np.arange(9).reshape(3,3)

print 'Original:\n', data
print 'Zoomed by 2x:\n', ndimage.zoom(data, 2)

这样产生:
Original:
[[0 1 2]
 [3 4 5]
 [6 7 8]]
Zoomed by 2x:
[[0 0 1 1 2 2]
 [1 1 1 2 2 3]
 [2 2 3 3 4 4]
 [4 4 5 5 6 6]
 [5 6 6 7 7 7]
 [6 6 7 7 8 8]]

这也适用于3D(和nD)阵列。但是请注意,例如,如果放大2倍,则会沿所有轴缩放。
data = np.arange(27).reshape(3,3,3)
print 'Original:\n', data
print 'Zoomed by 2x gives an array of shape:', ndimage.zoom(data, 2).shape

这样产生:
Original:
[[[ 0  1  2]
  [ 3  4  5]
  [ 6  7  8]]

 [[ 9 10 11]
  [12 13 14]
  [15 16 17]]

 [[18 19 20]
  [21 22 23]
  [24 25 26]]]
Zoomed by 2x gives an array of shape: (6, 6, 6)

如果您要缩放3波段RGB图像,可以通过指定一个元组序列作为缩放因子来实现:
print 'Zoomed by 2x along the last two axes:'
print ndimage.zoom(data, (1, 2, 2))

这样产生:
Zoomed by 2x along the last two axes:
[[[ 0  0  1  1  2  2]
  [ 1  1  1  2  2  3]
  [ 2  2  3  3  4  4]
  [ 4  4  5  5  6  6]
  [ 5  6  6  7  7  7]
  [ 6  6  7  7  8  8]]

 [[ 9  9 10 10 11 11]
  [10 10 10 11 11 12]
  [11 11 12 12 13 13]
  [13 13 14 14 15 15]
  [14 15 15 16 16 16]
  [15 15 16 16 17 17]]

 [[18 18 19 19 20 20]
  [19 19 19 20 20 21]
  [20 20 21 21 22 22]
  [22 22 23 23 24 24]
  [23 24 24 25 25 25]
  [24 24 25 25 26 26]]]

使用 map_coordinates 对规则网格数据进行任意插值

了解map_coordinates的第一件事是它在像素坐标下运行(例如,就像您对数组进行索引一样,但是值可以是浮点数)。根据您的描述,这正是您想要的,但是如果经常使人感到困惑。例如,如果您具有x,y,z“真实世界”坐标,则需要将它们转换为基于索引的“像素”坐标。

不管怎么说,我们想在原始数组的位置1.2、0.3、1.4处插值。

如果考虑较早的RGB图像情况,则第一个坐标对应于“带”,第二个坐标对应于“行”,最后一个坐标对应于“列”。什么顺序对应什么完全取决于您决定如何构造数据的方式,但是我将使用这些顺序作为“z,y,x”坐标,因为它使与打印数组的比较更容易可视化。
import numpy as np
import scipy.ndimage as ndimage

data = np.arange(27).reshape(3,3,3)

print 'Original:\n', data
print 'Sampled at 1.2, 0.3, 1.4:'
print ndimage.map_coordinates(data, [[1.2], [0.3], [1.4]])

这样产生:
Original:
[[[ 0  1  2]
  [ 3  4  5]
  [ 6  7  8]]

 [[ 9 10 11]
  [12 13 14]
  [15 16 17]]

 [[18 19 20]
  [21 22 23]
  [24 25 26]]]
Sampled at 1.2, 0.3, 1.4:
[14]

再一次,这是默认的三次插值。使用order kwarg控制插值的类型。

在这里值得注意的是,scipy.ndimage的所有操作都保留了原始数组的dtype。如果要浮点结果,则需要将原始数组强制转换为浮点数:
In [74]: ndimage.map_coordinates(data.astype(float), [[1.2], [0.3], [1.4]])
Out[74]: array([ 13.5965])

您可能会注意到的另一件事是,插值坐标格式对于单点而言非常麻烦(例如,它期望使用3xN数组而不是Nx3数组)。但是,当您具有坐标序列时,可以说是更好的选择。例如,考虑沿穿过数据“多维数据集”的线进行采样的情况:
xi = np.linspace(0, 2, 10)
yi = 0.8 * xi
zi = 1.2 * xi
print ndimage.map_coordinates(data, [zi, yi, xi])

这样产生:
[ 0  1  4  8 12 17 21 24  0  0]

这也是提及如何处理边界条件的好地方。默认情况下,数组外部的任何内容都设置为0。因此,序列中的最后两个值是0。 (即,最后两个元素的zi> 2)。

如果我们希望数组外的点为-999(我们不能使用nan,因为这是一个整数数组。如果要nan,则需要转换为浮点数。):
In [75]: ndimage.map_coordinates(data, [zi, yi, xi], cval=-999)
Out[75]: array([   0,    1,    4,    8,   12,   17,   21,   24, -999, -999])

如果我们希望它为数组外的点返回最接近的值,则可以执行以下操作:
In [76]: ndimage.map_coordinates(data, [zi, yi, xi], mode='nearest')
Out[76]: array([ 0,  1,  4,  8, 12, 17, 21, 24, 25, 25])

除了"reflect"和默认的"wrap"之外,您还可以将"nearest""constant"用作边界模式。这些都是不言自明的,但是如果您感到困惑,请尝试一下。

例如,让我们沿着数组中第一 strip 的第一行插入一条直线,该直线延伸为数组距离的两倍:
xi = np.linspace(0, 5, 10)
yi, zi = np.zeros_like(xi), np.zeros_like(xi)

默认给出:
In [77]: ndimage.map_coordinates(data, [zi, yi, xi])
Out[77]: array([0, 0, 1, 2, 0, 0, 0, 0, 0, 0])

比较一下:
In [78]: ndimage.map_coordinates(data, [zi, yi, xi], mode='reflect')
Out[78]: array([0, 0, 1, 2, 2, 1, 2, 1, 0, 0])

In [78]: ndimage.map_coordinates(data, [zi, yi, xi], mode='wrap')
Out[78]: array([0, 0, 1, 2, 0, 1, 1, 2, 0, 1])

希望这可以澄清一些事情!

关于python - 快速内插网格数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16983843/

相关文章:

python - pandas concat() 不加入同一列

python - 创建分块三对角矩阵

python - 计算非空值 Pandas

python - 如何有效地对 Pytables 中的数据进行插值

python - 如何一次旋转时间序列列的 N 个观察值

Python - 获取windows文件夹ACL权限

python - python中env变量的继承

Python 创建一个空的稀疏矩阵

python - 在 matplotlib 中绘制 10 负幂的 xscale

python scipy 暴力优化