I need to estimate the volumes associated with a set of Voronoi cells in a multi-dimensional space. From this question Volume of Voronoi cell (python) I tested this answer and it works, but it is really slow.
In the code below, obtaining the volumes takes up almost 90% of the time:
import numpy as np
from scipy.spatial import Voronoi
from scipy.spatial import ConvexHull
import time as t
points = np.random.uniform(0., 100., (5000, 4))
s = t.time()
v = Voronoi(points)
print(t.time() - s)
s = t.time()
vol = np.zeros(v.npoints)
for i, reg_num in enumerate(v.point_region):
indices = v.regions[reg_num]
if -1 in indices: # non-closed regions
vol[i] = np.inf
else:
vol[i] = ConvexHull(v.vertices[indices]).volume
print(t.time() - s)
That 90% is almost entirely used by the calls to scipy.spatial.ConvexHull
.
Can this be improved in any way?