使用scipy.interpolate.griddata时如何设置插值点之间的最大距离?
How to set a maximum distance between points for interpolation when using scipy.interpolate.griddata?
我有一组具有 Z 值的空间数据,我想使用一些 matplotlib 或 scipy 模块进行插值。我的 XY 点有一个凹形,我不想在空白区域中插入值。有没有一种方法可以很容易地让用户设置点之间的最大距离以避免在空白区域进行插值?
我遇到了同样的问题,并通过重新使用 scipy 本身用于最近邻插值的 kd 树实现找到了解决方法,用 kd 的结果屏蔽了插值结果数组-树查询结果。
考虑下面的示例代码:
import numpy as np
import scipy.interpolate
import matplotlib.pyplot as plt
# Generate some random data
xy = np.random.random((2**15, 2))
z = np.sin(10*xy[:,0]) * np.cos(10*xy[:,1])
grid = np.meshgrid(
np.linspace(0, 1, 512),
np.linspace(0, 1, 512)
)
# Interpolate
result1 = scipy.interpolate.griddata(xy, z, tuple(grid), 'linear')
# Show
plt.figimage(result1)
plt.show()
# Remove rectangular window
mask = np.logical_and.reduce((xy[:,0] > 0.2, xy[:,0] < 0.8, xy[:,1] > 0.2, xy[:,1] < 0.8))
xy, z = xy[~mask], z[~mask]
# Interpolate
result2 = scipy.interpolate.griddata(xy, z, tuple(grid), 'linear')
# Show
plt.figimage(result2)
plt.show()
这将生成以下两个图像。由于数据中心缺少矩形 window,请注意强烈的插值伪影。
现在如果我们运行下面的代码在相同的示例数据上,得到下面的图像。
THRESHOLD = 0.01
from scipy.interpolate.interpnd import _ndim_coords_from_arrays
from scipy.spatial import cKDTree
# Construct kd-tree, functionality copied from scipy.interpolate
tree = cKDTree(xy)
xi = _ndim_coords_from_arrays(tuple(grid), ndim=xy.shape[1])
dists, indexes = tree.query(xi)
# Copy original result but mask missing values with NaNs
result3 = result2[:]
result3[dists > THRESHOLD] = np.nan
# Show
plt.figimage(result3)
plt.show()
我知道这可能不是您想要的视觉效果。特别是如果你的数据集不是很密集,你需要有一个高距离阈值,以便合法插值的数据不被掩盖。如果您的数据足够密集,您可能可以使用相对较小的半径,或者可以想出一个更智能的截止函数。希望对您有所帮助。
我有一组具有 Z 值的空间数据,我想使用一些 matplotlib 或 scipy 模块进行插值。我的 XY 点有一个凹形,我不想在空白区域中插入值。有没有一种方法可以很容易地让用户设置点之间的最大距离以避免在空白区域进行插值?
我遇到了同样的问题,并通过重新使用 scipy 本身用于最近邻插值的 kd 树实现找到了解决方法,用 kd 的结果屏蔽了插值结果数组-树查询结果。
考虑下面的示例代码:
import numpy as np
import scipy.interpolate
import matplotlib.pyplot as plt
# Generate some random data
xy = np.random.random((2**15, 2))
z = np.sin(10*xy[:,0]) * np.cos(10*xy[:,1])
grid = np.meshgrid(
np.linspace(0, 1, 512),
np.linspace(0, 1, 512)
)
# Interpolate
result1 = scipy.interpolate.griddata(xy, z, tuple(grid), 'linear')
# Show
plt.figimage(result1)
plt.show()
# Remove rectangular window
mask = np.logical_and.reduce((xy[:,0] > 0.2, xy[:,0] < 0.8, xy[:,1] > 0.2, xy[:,1] < 0.8))
xy, z = xy[~mask], z[~mask]
# Interpolate
result2 = scipy.interpolate.griddata(xy, z, tuple(grid), 'linear')
# Show
plt.figimage(result2)
plt.show()
这将生成以下两个图像。由于数据中心缺少矩形 window,请注意强烈的插值伪影。
现在如果我们运行下面的代码在相同的示例数据上,得到下面的图像。
THRESHOLD = 0.01
from scipy.interpolate.interpnd import _ndim_coords_from_arrays
from scipy.spatial import cKDTree
# Construct kd-tree, functionality copied from scipy.interpolate
tree = cKDTree(xy)
xi = _ndim_coords_from_arrays(tuple(grid), ndim=xy.shape[1])
dists, indexes = tree.query(xi)
# Copy original result but mask missing values with NaNs
result3 = result2[:]
result3[dists > THRESHOLD] = np.nan
# Show
plt.figimage(result3)
plt.show()
我知道这可能不是您想要的视觉效果。特别是如果你的数据集不是很密集,你需要有一个高距离阈值,以便合法插值的数据不被掩盖。如果您的数据足够密集,您可能可以使用相对较小的半径,或者可以想出一个更智能的截止函数。希望对您有所帮助。