繁体   English   中英

在Python中将不规则间隔的数据重新采样到常规网格

[英]Resampling irregularly spaced data to a regular grid in Python

我需要将2D数据重新采样到常规网格。

这就是我的代码:

import matplotlib.mlab as ml
import numpy as np

y = np.zeros((512,115))
x = np.zeros((512,115))

# Just random data for this test:
data = np.random.randn(512,115)

# filling the grid coordinates:    
for i in range(512):
    y[i,:]=np.arange(380,380+4*115,4)

for i in range(115):
    x[:,i] = np.linspace(-8,8,512)
    y[:,i] -=  np.linspace(-0.1,0.2,512)

# Defining the regular grid
y_i = np.arange(380,380+4*115,4)
x_i = np.linspace(-8,8,512)

resampled_data = ml.griddata(x,y,data,x_i,y_i)

(512,115)是2D数据的形状,我已经安装了mpl_toolkits.natgrid。

我的问题是我得到了一个蒙面数组,其中大多数条目是nan,而不是一个主要由常规条目组成的数组,而边界只有nan。

有人能指出我做错了吗?

谢谢!

将您的代码示例与您的问题标题进行比较,我认为您有点困惑......

在您的示例代码中,您将定期创建网格化随机数据,然后将其重新采样到另一个常规网格中 您的示例中的任何位置都没有不规则数据...

(此外,代码不会按原样运行,您应该查看meshgrid而不是循环以生成x&y网格。)

如果您想要重新采样已经定期采样的网格,就像您在示例中所做的那样,有更多有效的方法,而不是griddata或我将在下面描述的任何内容。 scipy.ndimage.map_coordinates非常适合您的问题,就是这种情况。)

但是,基于您的问题,听起来您想要插入到常规网格上的数据不规则。

在这种情况下,您可能会有以下几点:

import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

# Bounds and number of the randomly generated data points
ndata = 20
xmin, xmax = -8, 8
ymin, ymax = 380, 2428

# Generate random data
x = np.random.randint(xmin, xmax, ndata)
y = np.random.randint(ymin, ymax, ndata)
z = np.random.random(ndata)

# Plot the random data points
plt.scatter(x,y,c=z)
plt.axis([xmin, xmax, ymin, ymax])
plt.colorbar()
plt.show()

随机生成的数据

然后,您可以像以前一样插入数据...(继续上面的代码片段...)

# Size of regular grid
ny, nx = 512, 115

# Generate a regular grid to interpolate the data.
xi = np.linspace(xmin, xmax, nx)
yi = np.linspace(ymin, ymax, ny)
xi, yi = np.meshgrid(xi, yi)

# Interpolate using delaunay triangularization 
zi = mlab.griddata(x,y,z,xi,yi)

# Plot the results
plt.figure()
plt.pcolormesh(xi,yi,zi)
plt.scatter(x,y,c=z)
plt.colorbar()
plt.axis([xmin, xmax, ymin, ymax])
plt.show()

插值不良的数据

但是,你会注意到你在网格中获得了大量的工件。 这是因为你的x坐标范围从-8到8,而y坐标范围从~300到~2500。 插值算法试图使各向同性,而您可能需要高度各向异性插值(以便在绘制网格时它看起来是各向同性的)。

要纠正这个问题,你需要创建一个新的坐标系来进行插值。没有一种正确的方法可以做到这一点。 我在下面使用的是可行的,但“最佳”方式在很大程度上取决于您的数据实际代表什么。

(换句话说,使用您对数据测量系统的了解来决定如何操作。插值总是如此!除非您知道结果应该是什么样的 ,并且对使用先验信息对您有利的插值算法!!还有比Griddata默认使用的Delaunay三角剖分更灵活的插值算法,但它对于一个简单的例子来说很好......)

无论如何,一种方法是重新缩放x和y坐标,使它们的范围大致相同。 在这种情况下。 我们将它们从0重新缩放到1 ......(原谅意大利面条字符串代码......我只是想以此为例......)

# (Continued from examples above...)
# Normalize coordinate system
def normalize_x(data):
    data = data.astype(np.float)
    return (data - xmin) / (xmax - xmin)

def normalize_y(data):
    data = data.astype(np.float)
    return (data - ymin) / (ymax - ymin)

x_new, xi_new = normalize_x(x), normalize_x(xi)
y_new, yi_new = normalize_y(y), normalize_y(yi)

# Interpolate using delaunay triangularization 
zi = mlab.griddata(x_new, y_new, z, xi_new, yi_new)

# Plot the results
plt.figure()
plt.pcolormesh(xi,yi,zi)
plt.scatter(x,y,c=z)
plt.colorbar()
plt.axis([xmin, xmax, ymin, ymax])
plt.show()

在标准化坐标系中插值的数据

希望无论如何都有帮助......对不起答案的长度!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM