def plot_vertex_values(self, values, layout=None, num_subplots=1): if layout is None: layout = (num_subplots, 1) self.fig = plt.figure(figsize=(3 * layout[1], 3 * layout[0])) values = np.array(values) if len(values.shape) == 1: values = values[np.newaxis, :] values -= np.min(values) values = values / np.max(values) sv = SphericalVoronoi(self.vertices, self.radius, self.center) sv.sort_vertices_of_regions() for i in range(num_subplots): ax = self.fig.add_subplot(layout[0], layout[1], i + 1, projection='3d') vertex_colors = [self.cm(val) for val in values[i]] for j, region in enumerate(sv.regions): color = vertex_colors[j] polygon = Poly3DCollection([sv.vertices[region]], alpha=1.0) polygon.set_color(color) ax.add_collection3d(polygon) ax.set_xlim(-1, 1) ax.set_ylim(-1, 1) ax.set_zlim(-1, 1) ax.elev = 20
def calculate_weight(self): radius = 1.0 center = np.array([0, 0, 0]) self.points = self._transfer_coordinate(radius, center) #for _i in range(self.nstations): # print("%10s: [%10.5f, %10.5f] -- [%10.5f, %10.5f, %10.5f]" # % (self.station_tag[_i], self.station_loc[_i][0], # self.station_loc[_i][1], self.points[_i][0], # self.points[_i][1], self.points[_i][2])) self.sv = SphericalVoronoi(self.points, 1, center) self.sv.sort_vertices_of_regions() surface_area = self.sv.compute_surface_area() self.weight = surface_area ** self.order self.weight = self._normalize(self.weight)
class VoronoiWeight(WeightBase): def __init__(self, stations, event, order=1.0): WeightBase.__init__(self, stations, event) self.points = None self.sv = None self.order = order def _transfer_coordinate(self, radius, center): """ Transfer (longitude, latitude) to (x, y, z) on sphere(radius, center) """ sphere_loc = np.zeros([self.nstations, 3]) for _i in range(self.station_loc.shape[0]): lat = np.deg2rad(self.station_loc[_i, 0]) lon = np.deg2rad(self.station_loc[_i, 1]) sphere_loc[_i, 0] = radius * cos(lat) * cos(lon) + center[0] sphere_loc[_i, 1] = radius * cos(lat) * sin(lon) + center[1] sphere_loc[_i, 2] = radius * sin(lat) + center[2] return sphere_loc def calculate_weight(self): radius = 1.0 center = np.array([0, 0, 0]) self.points = self._transfer_coordinate(radius, center) #for _i in range(self.nstations): # print("%10s: [%10.5f, %10.5f] -- [%10.5f, %10.5f, %10.5f]" # % (self.station_tag[_i], self.station_loc[_i][0], # self.station_loc[_i][1], self.points[_i][0], # self.points[_i][1], self.points[_i][2])) self.sv = SphericalVoronoi(self.points, 1, center) self.sv.sort_vertices_of_regions() surface_area = self.sv.compute_surface_area() self.weight = surface_area ** self.order self.weight = self._normalize(self.weight) def plot_sphere(self): points = self.points sv = self.sv fig = plt.figure(figsize=(10, 10)) ax = fig.add_subplot(111, projection='3d') # plot the unit sphere for reference (optional) u = np.linspace(0, 2 * np.pi, 100) v = np.linspace(0, np.pi, 100) x = np.outer(np.cos(u), np.sin(v)) y = np.outer(np.sin(u), np.sin(v)) z = np.outer(np.ones(np.size(u)), np.cos(v)) ax.plot_surface(x, y, z, color='y', alpha=0.05) # plot Voronoi vertices #ax.scatter(sv.vertices[:, 0], sv.vertices[:, 1], sv.vertices[:, 2], # c='g') # indicate Voronoi regions (as Euclidean polygons) for region in sv.regions: random_color = colors.rgb2hex(np.random.rand(3)) polygon = Poly3DCollection([sv.vertices[region]], alpha=1.0) polygon.set_color(random_color) ax.add_collection3d(polygon) # plot generator points ax.scatter(points[:, 0], points[:, 1], points[:, 2], c='b') ax.set_xlim(-1,1) ax.set_ylim(-1,1) ax.set_zlim(-1,1); ax.set_xticks([-1,1]) ax.set_yticks([-1,1]) ax.set_zticks([-1,1]); plt.tick_params(axis='both', which='major', labelsize=6) plt.xlabel("X") plt.ylabel("Y") plt.show() def plot_weight_histogram(self, outputdir=None, figformat="png"): if outputdir is None: figname = None else: figname = os.path.join(outputdir, "voronoi_weight_hist.%s" % figformat) self._plot_histogram(self.weight, title="Voronoi Weight", figname=figname)