编程语言
首页 > 编程语言> > python – 附近scipy.spatial.Delaunay遗漏的点数

python – 附近scipy.spatial.Delaunay遗漏的点数

作者:互联网

当比较scipy(0.9.0)和matplotlib(1.0.1)Delaunay三角测量程序时,我注意到一个无法解释的行为.我的点是存储在numpy.array中的UTM坐标([[easting,northing],[easting,northing],[easting,northing]]). Scipy的边缘缺少一些我的观点,而matplotlib就在那里.有没有修复,或者我做错了什么?

import scipy
import numpy
from scipy.spatial import Delaunay
import matplotlib.delaunay


def delaunay_edges(points):
    d = scipy.spatial.Delaunay(points)
    s = d.vertices
    return numpy.vstack((s[:,:2], s[:,1:], s[:,::-2]))


def delaunay_edges_matplotlib(points):
        cens, edges, tri, neig = matplotlib.delaunay.delaunay(points[:,0], points[:,1])
        return edges


points = numpy.array([[500000.25, 6220000.25],[500000.5, 6220000.5],[500001.0, 6220001.0],[500002.0, 6220003.0],[500003.0, 6220005.0]])

edges1 = delaunay_edges(points)
edges2 = delaunay_edges_matplotlib(points)

numpy.unique(edges1).shape # Some points missing, presumably nearby ones
numpy.unique(edges2).shape # Includes all points

解决方法:

scipy.spatial.Delaunay的这种行为可能与浮点运算的impresicion有关.

您可能知道,scipy.spatial.Delaunay使用C qhull库来计算Delaunay三角剖分.反过来,Qhull是Quickhull algorithm的实现,作者在this论文(1)中详细描述了这一点.您也可能知道计算机中使用的浮点运算是使用IEEE 754标准执行的(例如,您可以在Wikipedia中阅读它).根据该标准,每个有限数最简单地由三个整数描述:s =符号(零或一),c =有效数(或’系数’),q =指数.用于表示这些整数的位数因数据类型而异.因此,显而易见的是,数字轴上浮点数分布的密度不是恒定的 – 数字越大,分布越松散.即使使用Google计算器也可以看到 – 你可以从3333333333333334和get 0中减去3333333333333333.这是因为3333333333333333和3333333333333334都四舍五入到相同的浮点数.

现在,了解舍入错误,我们可能想要阅读题为“使用impresicion进行复制”的论文(1)的第4章.在本章中,描述了一种处理舍入误差的算法:

Quickhull partitions a point and determines its horizon facets by computing 
whether the point is above or below a hyperplane. We have assumed that 
computations return consistent results ... With floating-point arithmetic, we 
cannot prevent errors from occurring, but we can repair the damage after 
processing a point. We use brute force: if adjacent facets are nonconvex, one of 
the facets is merged into a neighbor. Quickhull merges the facet that minimizes 
the maximum distance of a vertex to the neighbor.

这就是可能发生的事情 – Quickhull无法区分两个附近的点,因此它合并了两个方面,从而成功地消除了其中一个方面.例如,要消除这些错误,您可以尝试移动坐标原点:

co = points[0]
points = points - co

edges1 = delaunay_edges(points)
edges2 = delaunay_edges_matplotlib(points)

print numpy.unique(edges1) 
>>> [0 1 2 3 4]
print numpy.unique(edges2)
>>> [0 1 2 3 4]

这将计算移动到浮点数相当密集的区域.

标签:python,numpy,scipy,matplotlib,delaunay
来源: https://codeday.me/bug/20190826/1732598.html