def plot_influenced_path_pyastar( self, start: Union[Tuple[int, int], Point2], goal: Union[Tuple[int, int], Point2], weight_array: ndarray, allow_diagonal=False, name: Optional[str] = None, fontdict: dict = None, ) -> None: import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1 import make_axes_locatable from matplotlib.cm import ScalarMappable if not fontdict: fontdict = {"family": "serif", "weight": "bold", "size": 20} plt.style.use(["ggplot", "bmh"]) org = "lower" if name is None: name = self.map_data.map_name arr = weight_array.copy() path = self.map_data.pathfind_pyastar(start, goal, grid=arr, sensitivity=1, allow_diagonal=allow_diagonal) ax: plt.Axes = plt.subplot(1, 1, 1) if path is not None: path = np.flipud(path) # for plot align logger.info("Found") x, y = zip(*path) ax.scatter(x, y, s=3, c="green") else: logger.info("Not Found") x, y = zip(*[start, goal]) ax.scatter(x, y) influence_cmap = plt.cm.get_cmap("afmhot") ax.text(start[0], start[1], f"Start {start}") ax.text(goal[0], goal[1], f"Goal {goal}") ax.imshow(self.map_data.path_arr, alpha=0.5, origin=org) ax.imshow(self.map_data.terrain_height, alpha=0.5, origin=org, cmap="bone") arr = np.where(arr == np.inf, 0, arr).T ax.imshow(arr, origin=org, alpha=0.3, cmap=influence_cmap) divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) sc = ScalarMappable(cmap=influence_cmap) sc.set_array(arr) sc.autoscale() cbar = plt.colorbar(sc, cax=cax) cbar.ax.set_ylabel("Pathing Cost", rotation=270, labelpad=25, fontdict=fontdict) plt.title(f"{name}", fontdict=fontdict, loc="right") plt.grid()
def render_movie(self): '''Visualize the volume as a movie''' from matplotlib import animation from matplotlib.cm import ScalarMappable import matplotlib.pyplot as plt # # Some of this is from a recipe # http://jakevdp.github.io/blog/2013/05/12/ # embedding-matplotlib-animations/ # image_target, prob_target, seeds_target, seg_target = list( self.input()) image_volume = image_target.imread() prob_volume = prob_target.imread() seeds_volume = seeds_target.imread() perm = np.random.RandomState(1234).permutation( np.max(seeds_volume) + 1) seg_volume = seg_target.imread() figure = plt.figure(figsize=(self.size_x_inches, self.size_y_inches)) ax = figure.add_subplot(1, 1, 1) sm = ScalarMappable(cmap='jet') sm.set_array(np.unique(seeds_volume)) sm.autoscale() prob_sm = ScalarMappable(cmap='jet') prob_sm.set_array(np.linspace(0, 255, 256)) prob_sm.autoscale() offset = self.first_frame end = self.end_frame if self.end_frame != 0 else image_volume.shape[0] def init_fig(): ax.clear() ax.set_ylim(self.volume.height, 0) ax.set_xlim(0, self.volume.width) def animate(i1): i = i1 - offset rh_logger.logger.report_event("Processing frame %d" % i) ax.imshow(image_volume[i], cmap='gray') sv = sm.to_rgba(perm[seeds_volume[i]]) sv[seeds_volume[i] == 0, 3] = 0 ax.imshow(sv) sv = sm.to_rgba(perm[seg_volume[i]]) sv[seg_volume[i] == 0, 3] = 0 sv[:, :, 3] *= .4 ax.imshow(sv) prob = prob_sm.to_rgba(prob_volume[i]) prob[:, :, 3] *= .4 prob[prob_volume[i] < 128, 3] = 0 ax.imshow(prob) anim = animation.FuncAnimation(figure, animate, init_func=init_fig, frames=end - offset, interval=1) plt.close(figure) anim.save(self.output().path, fps=1, extra_args=['-vcodec', 'libx264'])
def color_mapping(arr, cmap): sm = ScalarMappable(cmap=cmap) sm.set_array(arr) sm.autoscale() return map(rgb2hex, sm.to_rgba(arr))