Testing parts of the Voronoi code - starting by just loading modules


In [1]:
%load_ext autoreload
%autoreload 2
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
import numpy
import scipy
import voronoi_utility
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import pandas
import math

Generating data


In [2]:
#generate a random distribution of points on the unit sphere (http://mathworld.wolfram.com/SpherePointPicking.html)
import numpy.random
#go for 1000 random points
n=1000
u = numpy.random.random((n,)) #200 points on interval [0,1); ideally want (0,1), but perhaps close enough?
v = numpy.random.random((n,))
theta_array = 2 * math.pi * u
phi_array = numpy.arccos((2*v - 1.0))
r_array = numpy.ones((n,))
spherical_polar_coord_array = numpy.column_stack((r_array,theta_array,phi_array))
#convert to Cartesian coordinates
cartesian_coord_array = voronoi_utility.convert_spherical_array_to_cartesian_array(spherical_polar_coord_array)
#alternative method based on internal code, fundamentally same but removes duplicates and is seedable
#prng = numpy.random.RandomState(117)
#cartesian_coord_array = voronoi_utility.generate_random_array_spherical_generators(n,1,prng)

In [3]:
#test plot the random points on unit sphere
fig4 = plt.figure()
fig4.set_size_inches(14,14)
ax = fig4.add_subplot(111, projection='3d')
ax.scatter(cartesian_coord_array[...,0],cartesian_coord_array[...,1],cartesian_coord_array[...,2])
ax.set_ylim(-1,1)     
ax.set_xlim(-1,1)     
ax.set_zlim(-1,1)


Out[3]:
(-1, 1)

And doing the tesselation


In [4]:
random_dist_voronoi_instance = voronoi_utility.Voronoi_Sphere_Surface(cartesian_coord_array)

In [5]:
random_dictionary_sorted_Voronoi_point_coordinates_for_each_generator = random_dist_voronoi_instance.voronoi_region_vertices_spherical_surface()

In [6]:
#plot the results
import matplotlib.colors as colors
fig5 = plt.figure()
fig5.set_size_inches(14,14)
ax = fig5.add_subplot(111, projection='3d')
for generator_index,voronoi_polygon_vertices in random_dictionary_sorted_Voronoi_point_coordinates_for_each_generator.iteritems():    
    random_color = colors.rgb2hex(scipy.rand(3))
    generator_coordinate = cartesian_coord_array[generator_index]
    #ax.scatter(generator_coordinate[...,0],generator_coordinate[...,1],generator_coordinate[...,2],facecolor=random_color,lw=0,s=50,)
    ax.plot(voronoi_polygon_vertices[...,0],voronoi_polygon_vertices[...,1],voronoi_polygon_vertices[...,2],c='black')
    #want to close the polygons as well
    connecting_vertex_array = numpy.vstack((voronoi_polygon_vertices[0,...],voronoi_polygon_vertices[-1,...]))
    ax.plot(connecting_vertex_array[...,0],connecting_vertex_array[...,1],connecting_vertex_array[...,2],c='black')
    polygon = Poly3DCollection([voronoi_polygon_vertices],alpha=1.0)
    polygon.set_color(random_color)
    ax.add_collection3d(polygon)
ax.set_ylim(-1,1)     
ax.set_xlim(-1,1)     
ax.set_zlim(-1,1)


Out[6]:
(-1, 1)

Retrying the percent area recovery test with new L'Huilier's Theorem algorithm


In [7]:
dictionary_voronoi_polygon_surface_areas = random_dist_voronoi_instance.voronoi_region_surface_areas_spherical_surface()
theoretical_surface_area_unit_sphere = 4 * math.pi
reconstituted_surface_area_Voronoi_regions = sum(dictionary_voronoi_polygon_surface_areas.itervalues())
percent_area_recovery = round((reconstituted_surface_area_Voronoi_regions / theoretical_surface_area_unit_sphere) * 100., 5)
print percent_area_recovery


99.11718

In [ ]: