Scipy中的3D插值——密度网格

4
我有一组坐标,代表着许多物体的3D位置。这些点是从一个3D立方体模拟中获取的。我的目标是在立方体上创建一个3D网格,并将这些点分配到网格上的正确位置,以便我可以找出每个网格部分中物体的密度。例如,我已经查看了插值和网格文档(http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.griddata.html#scipy.interpolate.griddata),但由于没有与这些数据点相关联的函数,因此不确定如何处理它们。我想过使用if语句:给定一组点=[x1,y1,z1],[x2,y2,z2]等,如果points[i][0]-gridpoint1[0]<1: if points[i][1]-gridpoint1[1]<1: if points[i][2]-gridpoint1[2]<1,则points[i]=bin1[i],其中bin1是一个预先制作的零数组。但是,我认为我必须对网格上的每个网格点运行此代码(网格点将位于每个bin的中心),然后找出每个bin中有多少非零元素,这也让我感到困惑。我有一种感觉,可以使用scipy中的某些函数更轻松地完成整个任务,但我仍然不确定如何实现。非常感谢您的帮助!
1个回答

4

如果我理解正确,您有坐标(x[i],y[i],z[i]),i = 0,...,N-1,并且您想计算在3D立方体中给定网格单元中有多少个坐标?

这可以使用numpy.histogramdd完成:

import numpy as np

# some random points in a cube
x = np.random.rand(100)
y = np.random.rand(100)
z = np.random.rand(100);

# specify grid in a cube
xgrid = np.linspace(0.0, 1.0, 5)
ygrid = np.linspace(0.0, 1.0, 6)
zgrid = np.linspace(0.0, 1.0, 7)

# compute counts
counts, edges = np.histogramdd(np.c_[x, y, z], bins=(xgrid, ygrid, zgrid))

print counts.shape # -> (4, 5, 6)

# number of points in the box 
# xgrid[1] <= x < xgrid[2] && ygrid[2] <= y < ygrid[3] && zgrid[0] <= z < zgrid[1]
print counts[1, 2, 0]

如果您想找出每个点所在的网格单元格,可以使用 searchsorted 完成:
ix = np.searchsorted(xgrid, x) - 1
iy = np.searchsorted(ygrid, y) - 1
iz = np.searchsorted(zgrid, z) - 1

# point (x[3], y[3], z[3]) is in the following grid cell
print ix[3], iy[3], iz[3]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接