我有一个n维的点网格,但其中有孔,我想获取一个缺少的网格点的列表。但是,我不想扩大现有网格的边界。
例如在2D中,如果在OR上下左右有任何值,我只想要网格点坐标。这是一个卡通,它o
是一个现有点,x
也是我想要的坐标。
o o o o o
o o x o o x o
o x x o o
o x o o
o o o o
数据不在网格中。这只是坐标列表,即
coords = [(1000,3.5), (1000,4.0), (1000,4.5), (1000,5.0), (1000,5.5),
(1100,4.5), (1100,5.5), (1200,4.0), (1200,4.5), (1200,5.0), (1200,5.5),
(1300,3.5), (1300,4.0), (1300,4.5)]
所以我想要的值是[(1100,3.5), (1100,4.0), (1100,5.0), (1200,3.5)]
。
我尝试获取每个参数的最小值和最大值并创建一个新轴numpy.arange(min(param1),max(param1),100)
,然后通过将其与旧值进行比较,numpy.setdiff1d()
但这在不必要时使网格变为矩形。
关于如何有效地做到这一点的任何想法?
我认为最简单的方法是将网格映射到矩形阵列。因为那样,确定哪些点在标准之内相对简单快捷。不利的一面是RAM使用最终可能会成为一个问题,尤其是对于稀疏的网格。
尚有争议的一点是如何定义网格。当前,其他答案使用沿着元素之间的尺寸的最小差异作为网格在该方向上的步长。但是,这在极少数情况下会带来问题。例如,如果已知坐标为:
2, 4, 6, 9, 11
然后步长将等于2
,但是显然这是错误的9
。也许最好采用最大的连续差异除数?例如,借助此答案。在我的代码中,我采用了另一种方法:仅使用已知坐标中存在的“勾号”来构建网格。
对于2D情况,可以满足以下要求:
def find_holes_2d(coords):
coords = np.asanyarray(coords)
# determine grid and transform coordinates
uniq_x, labels_x = np.unique(coords[:,0], return_inverse=True)
uniq_y, labels_y = np.unique(coords[:,1], return_inverse=True)
# layout the known grid in an array
grid = np.zeros([len(uniq_x), len(uniq_y)], bool)
grid[labels_x, labels_y] = True
# see which grid points are inside known coordinates
x_fwd = np.logical_or.accumulate(grid, axis=0)
x_bkwd = np.logical_or.accumulate(grid[::-1], axis=0)[::-1]
y_fwd = np.logical_or.accumulate(grid, axis=1)
y_bkwd = np.logical_or.accumulate(grid[:,::-1], axis=1)[:,::-1]
# select the holes according to the criteria
holes = ~grid & (x_fwd & x_bkwd | y_fwd & y_bkwd)
# Transform positions back to original coordinates
I,J = np.where(holes)
return np.column_stack([uniq_x[I], uniq_y[J]])
相同的方法可以应用于ND案例,例如:
def find_holes(coords):
coords = np.asanyarray(coords)
uniq, labels = zip(*[np.unique(c, return_inverse=True) for c in coords.T])
grid = np.zeros(map(len, uniq), bool)
grid[labels] = True
candidates = np.zeros_like(grid)
for dim in range(grid.ndim):
grid0 = np.rollaxis(grid, dim)
inside = np.logical_or.accumulate(grid0, axis=0) &
np.logical_or.accumulate(grid0[::-1], axis=0)[::-1]
candidates |= np.rollaxis(inside, 0, dim+1)
holes = candidates & ~grid
hole_labels = np.where(holes)
return np.column_stack([u[h] for u, h in zip(uniq, hole_labels)])
最后,这个玩具示例还显示了一个问题:
o x o o
x x o
o o o o
在这里,孔仍然“未被发现”。通过将找到的孔的坐标添加x
到原始坐标并进行第二次迭代,可以轻松解决此问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句