def clippedcolorbar(CS, **kwargs): from matplotlib.cm import ScalarMappable from numpy import arange, floor, ceil fig = CS.ax.get_figure() vmin = CS.get_clim()[0] vmax = CS.get_clim()[1] m = ScalarMappable(cmap=CS.get_cmap()) m.set_array(CS.get_array()) m.set_clim(CS.get_clim()) step = CS.levels[1] - CS.levels[0] cliplower = CS.zmin < vmin clipupper = CS.zmax > vmax noextend = 'extend' in kwargs.keys() and kwargs['extend'] == 'neither' # set the colorbar boundaries boundaries = arange( (floor(vmin / step) - 1 + 1 * (cliplower and noextend)) * step, (ceil(vmax / step) + 1 - 1 * (clipupper and noextend)) * step, step) kwargs['boundaries'] = boundaries # if the z-values are outside the colorbar range, add extend marker(s) # This behavior can be disabled by providing extend='neither' to the function call if not ('extend' in kwargs.keys()) or kwargs['extend'] in ['min', 'max']: extend_min = cliplower or ('extend' in kwargs.keys() and kwargs['extend'] == 'min') extend_max = clipupper or ('extend' in kwargs.keys() and kwargs['extend'] == 'max') if extend_min and extend_max: kwargs['extend'] = 'both' elif extend_min: kwargs['extend'] = 'min' elif extend_max: kwargs['extend'] = 'max' return fig.colorbar(m, **kwargs)
def make_cmap_sm_norm(d=None, clim=None, cmap=None): if cmap == 'red_blue': cmap = red_blue_cm() if cmap == 'banas_cm': if clim == None: cmap = banas_cm(np.min(d[:]), np.min(d[:]), np.max(d[:]), np.max(d[:])) elif len(clim) == 2: cmap = banas_cm(clim[0], clim[0], clim[1], clim[1]) elif len(clim) == 4: cmap = banas_cm(clim[0], clim[1], clim[2], clim[3]) elif cmap == 'banas_hsv_cm': if clim == None: cmap = banas_hsv_cm(np.min(d[:]), np.min(d[:]), np.max(d[:]), np.max(d[:])) elif len(clim) == 2: cmap = banas_hsv_cm(clim[0], clim[0], clim[1], clim[1]) elif len(clim) == 4: cmap = banas_hsv_cm(clim[0], clim[1], clim[2], clim[3]) norm = Normalize(vmin=clim[0], vmax=clim[-1], clip=False) sm = ScalarMappable(norm=norm, cmap=cmap) sm.set_clim(vmin=clim[0], vmax=clim[-1]) sm.set_array(np.array([0])) return cmap, sm, norm
def plot_ph(ph, vmax=12, vmin=0, cmapstr='viridis', ax=None, curv=False, plot_cb_phname=True, cbar_shrink=1): """Plot predictability horizon with stride 1 colormap.""" ph.name = 'Predictability Horizon [years]' ph_levels = [_ for _ in range(vmin, vmax + 1)] ncolors = len(ph_levels) cmap = _cmap_discretize(cmapstr, ncolors) # cmap.set_under('white') mappable = ScalarMappable(cmap=cmap) mappable.set_array([]) l_min = max(vmin, np.min(ph_levels)) l_max = min(np.max(ph_levels), vmax) ticks = np.arange(l_min, l_max + 1) mappable.set_clim(l_min - 0.5, l_max + 0.5) if ax is None: p = my_plot(ph.where(ph > 0), levels=ph_levels, add_colorbar=False, cmap=cmapstr, curv=curv) else: p = my_plot(ph.where(ph > 0), levels=ph_levels, add_colorbar=False, ax=ax, cmap=cmapstr, curv=curv) cb = plt.colorbar(mappable, ax=ax, shrink=cbar_shrink) if vmax == 20: ticks = [0, 5, 10, 15, 20] cb.set_ticks(ticks) cb.set_ticklabels(ticks) cb.ax.set_ylabel('[years]', fontsize=15) return p
def make_plot(self, val, cct, clm, cmap='jet', cmap_norm=None, cmap_vmin=None, cmap_vmax=None): # make color mapping smap = ScalarMappable(cmap_norm, cmap) smap.set_clim(cmap_vmin, cmap_vmax) smap.set_array(val) bin_colors = smap.to_rgba(val) # make patches patches = [] for i_c, i_clm in enumerate(clm): patches.append( Rectangle((i_clm[0], i_clm[2]), i_clm[1] - i_clm[0], i_clm[3] - i_clm[2])) patches_colle = PatchCollection(patches) patches_colle.set_edgecolor('face') patches_colle.set_facecolor(bin_colors) return patches_colle, smap
def make_plot(self, band=1, window=None, axes=None, vmin=None, vmax=None, cmap='topobathy', levels=None, show=False, title=None, figsize=None, colors=256, cbar_label=None, norm=None, **kwargs): if axes is None: fig = plt.figure(figsize=figsize) axes = fig.add_subplot(111) values = self.values(band, masked=True, window=window) vmin = np.min(values) if vmin is None else float(vmin) vmax = np.max(values) if vmax is None else float(vmax) cmap, norm, levels, col_val = self._get_cmap(values, vmin, vmax, cmap, levels, colors, norm) axes.contourf(self.x(window), self.y(window), values, levels=levels, cmap=cmap, norm=norm, vmin=vmin, vmax=vmax, **kwargs) axes.axis('scaled') # if extent is not None: # axes.axis(extent) if title is not None: axes.set_title(title) mappable = ScalarMappable(cmap=cmap) mappable.set_array([]) mappable.set_clim(vmin, vmax) divider = make_axes_locatable(axes) cax = divider.append_axes("bottom", size="2%", pad=0.5) cbar = plt.colorbar( mappable, cax=cax, # extend=cmap_extend, orientation='horizontal') if col_val != 0: cbar.set_ticks([vmin, vmin + col_val * (vmax - vmin), vmax]) cbar.set_ticklabels([np.around(vmin, 2), 0.0, np.around(vmax, 2)]) else: cbar.set_ticks([vmin, vmax]) cbar.set_ticklabels([np.around(vmin, 2), np.around(vmax, 2)]) if cbar_label is not None: cbar.set_label(cbar_label) if show is True: plt.show() return axes
def _create_colorbar(self, cmap, ncolors, labels, **kwargs): norm = BoundaryNorm(range(0, ncolors), cmap.N) mappable = ScalarMappable(cmap=cmap, norm=norm) mappable.set_array([]) mappable.set_clim(-0.5, ncolors+0.5) colorbar = plt.colorbar(mappable, **kwargs) colorbar.set_ticks(np.linspace(0, ncolors, ncolors+1)+0.5) colorbar.set_ticklabels(range(0, ncolors)) colorbar.set_ticklabels(labels) return colorbar
def _create_colorbar(self, cmap, ncolors, labels, **kwargs): norm = BoundaryNorm(range(0, ncolors), cmap.N) mappable = ScalarMappable(cmap=cmap, norm=norm) mappable.set_array([]) mappable.set_clim(-0.5, ncolors + 0.5) colorbar = plt.colorbar(mappable, **kwargs) colorbar.set_ticks(np.linspace(0, ncolors, ncolors + 1) + 0.5) colorbar.set_ticklabels(range(0, ncolors)) colorbar.set_ticklabels(labels) return colorbar
def plot_net_layerwise(net, x_spacing=5, y_spacing=10, colors=[], use_labels=True, ax=None, cmap='gist_heat', cbar=False, positions={}): if not colors: colors = [1] * net.size() args = { 'ax' : ax, 'node_color' : colors, 'nodelist' : net.nodes(), # ensure that same order is used throughout for parallel data like colors 'vmin' : 0, 'vmax' : 1, 'cmap' : cmap } if not positions: # compute layer-wise positions of nodes (distance from roots) nodes_by_layer = defaultdict(lambda: []) def add_to_layer(n,l): nodes_by_layer[l].append(n) net.bfs_traverse(net.get_roots(), add_to_layer) positions = {} for l, nodes in nodes_by_layer.iteritems(): y = -l*y_spacing # reorder layer lexicographically nodes.sort(key=lambda n: n.get_name()) width = (len(nodes)-1) * x_spacing for i,n in enumerate(nodes): x = x_spacing*i - width/2 positions[n] = (x,y) args['pos'] = positions if use_labels: labels = {n:n.get_name() for n in net.iter_nodes()} args['labels'] = labels if ax is None: ax = plt.figure().add_subplot(1,1,1) nxg = net_to_digraph(net) nx.draw_networkx(nxg, **args) ax.tick_params(axis='x', which='both', bottom='off', top='off', labelbottom='off') ax.tick_params(axis='y', which='both', left='off', right='off', labelleft='off') if cbar: color_map = ScalarMappable(cmap=cmap) color_map.set_clim(vmin=0, vmax=1) color_map.set_array(np.array([0,1])) plt.colorbar(color_map, ax=ax) ax.set_aspect('equal') # zoom out slightly to avoid cropping issues with nodes xl = ax.get_xlim() yl = ax.get_ylim() ax.set_xlim(xl[0]-x_spacing/2, xl[1]+x_spacing/2) ax.set_ylim(yl[0]-y_spacing/2, yl[1]+y_spacing/2)
def plot_mass_transit_fares_inputs(fares_data, bau_fares_data, max_fare, route_ids, name_run): """Plot the Mass Transit Fares input Parameters ---------- See `process_fares_data()` name_run: str Name of the run , e.g. "BAU", "Run 1", "Submission"... Returns ------- ax: matplotlib axes object """ fares = process_fares_data(fares_data, bau_fares_data, max_fare, route_ids) #print(fares.head()) fig, ax = plt.subplots(figsize=(7, 5)) #color map my_cmap = plt.cm.get_cmap('YlOrRd') colors = my_cmap(fares["amount_normalized"]) y = np.array(fares['routeId']) plt.barh(y=y, width=(fares["max_age"] - fares["min_age"]), color=colors, left=min(fares["min_age"])) plt.xlabel("Age") plt.ylabel("Bus route") plt.ylim((1339.5, 1351.5)) plt.xlim((0, 120)) ax.yaxis.set_major_locator(plt.MultipleLocator(1)) ax.xaxis.set_major_locator(plt.MultipleLocator(10)) sm = ScalarMappable(cmap=my_cmap, norm=plt.Normalize(0, np.max(fares["amount"]))) sm.set_array([]) sm.set_clim(0, max_fare) cbar = fig.colorbar(sm, ticks=[i for i in range(0, max_fare + 1)]) cbar.set_label('Fare amount [$]', rotation=270, labelpad=25) # Replace the 0 by 0.01 in the color scale as fares must be greater than 0 y_ticks_labels = ["{0}".format(i) for i in range(0, 10 + 1)] y_ticks_labels[0] = "0.01" cbar.ax.set_yticklabels(y_ticks_labels) plt.title(f"Input - Mass Transit Fares - {name_run}") return ax
def make_plot( self, axes: pyplot.Axes = None, vmin: float = None, vmax: float = None, show: bool = False, title: str = None, # figsize=rcParams["figure.figsize"], extent: (float, float, float, float) = None, cbar_label: str = None, **kwargs ): if vmin is None: vmin = np.min(self.values) if vmax is None: vmax = np.max(self.values) kwargs.update(**fig.get_topobathy_kwargs(self.values, vmin, vmax)) kwargs.pop('col_val') levels = kwargs.pop('levels') if vmin != vmax: self.tricontourf( axes=axes, levels=levels, vmin=vmin, vmax=vmax, **kwargs ) else: self.tripcolor(axes=axes, **kwargs) self.quadface(axes=axes, **kwargs) axes.axis('scaled') if extent is not None: axes.axis(extent) if title is not None: axes.set_title(title) mappable = ScalarMappable(cmap=kwargs['cmap']) mappable.set_array([]) mappable.set_clim(vmin, vmax) divider = make_axes_locatable(axes) cax = divider.append_axes("bottom", size="2%", pad=0.5) cbar = plt.colorbar( mappable, cax=cax, orientation='horizontal' ) cbar.set_ticks([vmin, vmax]) cbar.set_ticklabels([np.around(vmin, 2), np.around(vmax, 2)]) if cbar_label is not None: cbar.set_label(cbar_label) if show: pyplot.show() return axes
def si_fig(d_smi_score, d_smi_idx, fps_embedded, data_dirs, models, portrait=True): zmin = -max(score for score in d_smi_score.values() if score < 0) zmax = -min(d_smi_score.values()) zmin = round((zmin+zmax)/2) n_models = len(data_dirs) n_iters = get_num_iters(data_dirs[0]) if portrait: fig = plt.figure(figsize=(10*1.15, 15), constrained_layout=True) gs = fig.add_gridspec(nrows=n_iters, ncols=n_models) else: fig = plt.figure(figsize=(15*1.15, 10), constrained_layout=True) gs = fig.add_gridspec(nrows=n_models, ncols=n_iters) axs = [] for i, (parent_dir, model) in enumerate(zip(data_dirs, models)): fig, axs_ = add_model_data(fig, gs, parent_dir, i, model, d_smi_idx, fps_embedded, zmin, zmax, portrait, n_models) axs.extend(axs_) ticks = list(range(zmin, round(zmax))) colormap = ScalarMappable(cmap='plasma') colormap.set_clim(zmin, zmax) cbar = plt.colorbar(colormap, ax=axs, aspect=30, ticks=ticks) cbar.ax.set_title('Score') ticks[0] = f'≤{ticks[0]}' cbar.ax.set_yticklabels(ticks) if portrait: fig.text(0.01, 0.5, 'Iteration', ha='center', va='center', rotation='vertical', fontsize=14, fontweight='bold',) fig.text(0.465, 0.9975, 'Model', ha='center', va='top', fontsize=14, fontweight='bold',) else: fig.text(0.01, 0.5, 'Model', ha='center', va='center', rotation='vertical', fontsize=16, fontweight='bold') fig.text(0.48, 0.01, 'Iteration', ha='center', va='center', fontsize=16, fontweight='bold',) plt.savefig(f'umap_fig_si_{"portrait" if portrait else "landscape"}.pdf') plt.clf()
def custom_colorbar(cmap, ncolors, breaks, **kwargs): from matplotlib.colors import BoundaryNorm from matplotlib.cm import ScalarMappable import matplotlib.colors as mplc breaklabels = ['No Counts']+["> %d counts"%(perc) for perc in breaks[:-1]] norm = BoundaryNorm(range(0, ncolors), cmap.N) mappable = ScalarMappable(cmap=cmap, norm=norm) mappable.set_array([]) mappable.set_clim(-0.5, ncolors+0.5) colorbar = plt.colorbar(mappable, **kwargs) colorbar.set_ticks(np.linspace(0, ncolors, ncolors+1)+0.5) colorbar.set_ticklabels(range(0, ncolors)) colorbar.set_ticklabels(breaklabels) return colorbar
def colorbar_index(ncolors, cmap, use_gridspec=False): """Return a discrete colormap with n colors from the continuous colormap cmap and add correct tick labels :param ncolors: number of colors of the colormap :param cmap: continuous colormap to create discrete colormap from :param use_gridspec: optional, use option for colorbar :return: colormap instance """ cmap = cmap_discretize(cmap, ncolors) mappable = ScalarMappable(cmap=cmap) mappable.set_array([]) mappable.set_clim(-0.5, ncolors + 0.5) colorbar = plt.colorbar(mappable, use_gridspec=use_gridspec) colorbar.set_ticks(np.linspace(-0.5, ncolors + 0.5, 2 * ncolors + 1)[1::2]) colorbar.set_ticklabels(list(range(ncolors))) return colorbar
def init_plot(): fig.clear() ax = fig.add_subplot() self._contour_plot = ax.contourf( *x_cartesian_coordinate_grids, y[0, ..., 0], vmin=v_min, vmax=v_max, cmap=color_map) ax.set_xlabel('x0') ax.set_ylabel('x1') ax.axis('scaled') mappable = ScalarMappable(cmap=color_map) mappable.set_clim(v_min, v_max) plt.colorbar(mappable=mappable)
def custom_colorbar(cmap, ncolors, breaks, **kwargs): from matplotlib.colors import BoundaryNorm from matplotlib.cm import ScalarMappable import matplotlib.colors as mplc breaklabels = ['No Counts' ] + ["> %d counts" % (perc) for perc in breaks[:-1]] norm = BoundaryNorm(range(0, ncolors), cmap.N) mappable = ScalarMappable(cmap=cmap, norm=norm) mappable.set_array([]) mappable.set_clim(-0.5, ncolors + 0.5) colorbar = plt.colorbar(mappable, **kwargs) colorbar.set_ticks(np.linspace(0, ncolors, ncolors + 1) + 0.5) colorbar.set_ticklabels(range(0, ncolors)) colorbar.set_ticklabels(breaklabels) return colorbar
def main_fig(d_smi_score, d_smi_idx, fps_embedded, data_dirs, models=None, iters=None): models = ['RF', 'NN', 'MPN'] or models iters = [0, 1, 3, 5] or iters[:4] zmax = -min(d_smi_score.values()) zmin = -max(score for score in d_smi_score.values() if score < 0) zmin = round((zmin+zmax)/2) nrows = 2+len(data_dirs) ncols = 4 fig = plt.figure(figsize=(2*ncols*1.15, 2*nrows), constrained_layout=True) gs = fig.add_gridspec(nrows=nrows, ncols=4) fig, ax1 = add_top1k_panel(fig, gs, d_smi_score, d_smi_idx, fps_embedded) fig, ax2 = add_density_panel(fig, gs, ax1, fps_embedded) axs = [] for i, (data_dir, model) in enumerate(zip(data_dirs, models)): fig, axs_ = add_model_row(fig, gs, data_dir, i+2, iters, model, d_smi_idx, fps_embedded, zmin, zmax) axs.extend(axs_) colormap = ScalarMappable(cmap='plasma') colormap.set_clim(zmin, zmax) ticks = list(range(zmin, round(zmax))) cbar = plt.colorbar(colormap, ax=axs, aspect=30, ticks=ticks) cbar.ax.set_title('Score') ticks[0] = f'≤{ticks[0]}' cbar.ax.set_yticklabels(ticks) fig.text(-0.03, 1.03, 'A', transform=ax1.transAxes, fontsize=16, fontweight='bold', va='center', ha='right') fig.text(-0.0, 1.03, 'B', transform=ax2.transAxes, fontsize=16, fontweight='bold', va='center', ha='left') fig.text(-0.03, -0.075, 'C', transform=ax1.transAxes, fontsize=16, fontweight='bold', va='center', ha='right') fig.text(0.475, 0.005, 'Iteration', ha='center', va='center', fontweight='bold') plt.savefig(f'umap_fig_main_2.pdf') plt.clf()
def custom_colorbar(cmap, ncolors, labels, **kwargs): """Create a custom, discretized colorbar with correctly formatted/aligned labels. cmap: the matplotlib colormap object you plan on using for your graph ncolors: (int) the number of discrete colors available labels: the list of labels for the colorbar. Should be the same length as ncolors. """ from matplotlib.colors import BoundaryNorm from matplotlib.cm import ScalarMappable norm = BoundaryNorm(range(0, ncolors), cmap.N) mappable = ScalarMappable(cmap=cmap, norm=norm) mappable.set_array([]) mappable.set_clim(-0.5, ncolors+0.5) colorbar = plt.colorbar(mappable, **kwargs) colorbar.set_ticks(np.linspace(0, ncolors, ncolors+1)+0.5) colorbar.set_ticklabels(range(0, ncolors)) colorbar.set_ticklabels(labels) return colorbar
def make_plot(self, val, cct, clm, cmap = 'jet', cmap_norm = None, cmap_vmin = None, cmap_vmax = None): # make color mapping smap = ScalarMappable(cmap_norm, cmap) smap.set_clim(cmap_vmin, cmap_vmax) smap.set_array(val) bin_colors = smap.to_rgba(val) # make patches patches = [] for i_c, i_clm in enumerate(clm): patches.append(Rectangle((i_clm[0], i_clm[2]), i_clm[1] - i_clm[0], i_clm[3] - i_clm[2])) patches_colle = PatchCollection(patches) patches_colle.set_edgecolor('face') patches_colle.set_facecolor(bin_colors) return patches_colle, smap
def _custom_colorbar(cmap, ncolors, labels, **kwargs): """Create a custom, discretized colorbar with correctly formatted/aligned labels. It was inspired mostly by the example provided in http://beneathdata.com/how-to/visualizing-my-location-history/ :param cmap: the matplotlib colormap object you plan on using for your graph :param ncolors: (int) the number of discrete colors available :param labels: the list of labels for the colorbar. Should be the same length as ncolors. :return: custom colorbar """ if ncolors <> len(labels): raise MapperError("Number of colors is not compatible with the number of labels") else: norm = BoundaryNorm(range(0, ncolors), cmap.N) mappable = ScalarMappable(cmap=cmap) mappable.set_array([]) mappable.set_clim(-0.5, ncolors+0.5) colorbar = plt.colorbar(mappable, **kwargs) colorbar.set_ticks(np.linspace(0, ncolors, ncolors+1)+0.5) colorbar.set_ticklabels(range(0, ncolors)) colorbar.set_ticklabels(labels) return colorbar
def _custom_colorbar(cmap, ncolors, labels, **kwargs): """Create a custom, discretized colorbar with correctly formatted/aligned labels. It was inspired mostly by the example provided in http://beneathdata.com/how-to/visualizing-my-location-history/ :param cmap: the matplotlib colormap object you plan on using for your graph :param ncolors: (int) the number of discrete colors available :param labels: the list of labels for the colorbar. Should be the same length as ncolors. :return: custom colorbar """ if ncolors <> len(labels): raise MapperError( "Number of colors is not compatible with the number of labels") else: norm = BoundaryNorm(range(0, ncolors), cmap.N) mappable = ScalarMappable(cmap=cmap) mappable.set_array([]) mappable.set_clim(-0.5, ncolors + 0.5) colorbar = plt.colorbar(mappable, **kwargs) colorbar.set_ticks(np.linspace(0, ncolors, ncolors + 1) + 0.5) colorbar.set_ticklabels(range(0, ncolors)) colorbar.set_ticklabels(labels) return colorbar
def make_cmap_sm_norm(d=None,clim=None,cmap=None): if cmap == 'red_blue': cmap = red_blue_cm() if cmap == 'banas_cm': if clim==None: cmap = banas_cm(np.min(d[:]),np.min(d[:]),np.max(d[:]),np.max(d[:])) elif len(clim) == 2: cmap = banas_cm(clim[0],clim[0],clim[1],clim[1]) elif len(clim) == 4: cmap = banas_cm(clim[0],clim[1],clim[2],clim[3]) elif cmap == 'banas_hsv_cm': if clim==None: cmap = banas_hsv_cm(np.min(d[:]),np.min(d[:]),np.max(d[:]),np.max(d[:])) elif len(clim) == 2: cmap = banas_hsv_cm(clim[0],clim[0],clim[1],clim[1]) elif len(clim) == 4: cmap = banas_hsv_cm(clim[0],clim[1],clim[2],clim[3]) norm = Normalize(vmin=clim[0],vmax=clim[-1],clip=False) sm = ScalarMappable(norm=norm,cmap=cmap) sm.set_clim(vmin=clim[0],vmax=clim[-1]) sm.set_array(np.array([0])) return cmap,sm,norm
def plot_per_class_top_five_acc_vs_number_of_samples_aggregated(process_top_5_acc_by_class_df, process_bottlenecks_df, training_top_5_acc_by_class_df=None, training_bottlenecks_df=None, dataset='BOONE', process='Validation', preceding_process='Training'): # https://stackoverflow.com/questions/51204505/python-barplot-with-colorbar fig, ax = plt.subplots() # Remove 100 percent accuracy: process_top_5_acc_by_class_df = process_top_5_acc_by_class_df[process_top_5_acc_by_class_df['top_5_acc'] != 100] joined_df = pd.merge(process_top_5_acc_by_class_df, process_bottlenecks_df, how='inner', sort=False) joined_df['num_class_samples'] = joined_df.groupby(['class'])['top_5_acc'].transform('count') # Remove extra rows: joined_df = joined_df.drop(['bottleneck', 'path'], axis=1) joined_df = joined_df.drop_duplicates(subset=['top_5_acc', 'num_class_samples']) # Normalize for colors: joined_df['data_color'] = joined_df['num_class_samples'].apply(lambda x: x / max(joined_df['num_class_samples'])) # Re-sort joined_df = joined_df.sort_values(['top_5_acc', 'num_class_samples'], ascending=False) print('num_samples_per_class: %s' % joined_df['num_class_samples'].values) print('data colors: %s' % joined_df['data_color'].values) cmap = plt.cm.get_cmap('GnBu') colors = cmap(joined_df['data_color'].values) rects = ax.barh(joined_df['class'], joined_df['top_5_acc'], color=colors) sm = ScalarMappable(cmap=cmap, norm=plt.Normalize(0, max(joined_df['num_class_samples']))) sm.set_clim(vmin=min(joined_df['num_class_samples']), vmax=max(joined_df['num_class_samples'])) cbar = plt.colorbar(sm, drawedges=False) if dataset == 'GoingDeeper': cbar.set_label('Number of Class Samples (%s Set)' % process, rotation=270, labelpad=25, fontsize=24) plt.ylabel('Species/Scientific Name', fontsize=24) plt.xlabel('Top-5 Accuracy', fontsize=24) plt.title('Winning Model: Top-5 Accuracy by Class', fontsize=24) # plt.suptitle('%s %s Set' % (dataset, process)) ax.tick_params(labelsize=16) cbar.ax.tick_params(labelsize=16) # Invert y-axis ax.invert_yaxis() ax.set_yticks([]) elif dataset == 'BOONE': cbar.set_label('Number of Class Samples (%s Set)' % process, rotation=270, labelpad=25, fontsize=24) plt.ylabel('Species/Scientific Name', fontsize=24) plt.xlabel('Top-5 Accuracy', fontsize=24) plt.title('Winning Model: Top-5 Accuracy by Class (Excluding 100% Accurate)', fontsize=24) # plt.suptitle('%s %s Set' % (dataset, process)) ax.tick_params(labelsize=16) cbar.ax.tick_params(labelsize=16) # Invert y-axis ax.invert_yaxis() plt.show() if training_top_5_acc_by_class_df is not None: fig, ax = plt.subplots() joined_training_df = pd.merge(training_top_5_acc_by_class_df, training_bottlenecks_df, how='inner', sort=False) # Remove classes which obtained 100% accuracy on the validation set: joined_training_df_subset = joined_training_df[joined_training_df['class'].isin(joined_df['class'])].dropna() # Calculate the number of training instances belonging to each of the classes: joined_training_df_subset['num_class_samples'] = joined_training_df_subset.groupby(['class'])['top_5_acc'].transform('count') # Drop extraneous rows: joined_training_df_subset = joined_training_df_subset.drop(['bottleneck', 'path'], axis=1) joined_training_df_subset = joined_training_df_subset.drop_duplicates(subset=['class', 'top_5_acc', 'num_class_samples']) # Normalize: joined_training_df_subset['data_color'] = joined_training_df_subset['num_class_samples'].apply(lambda x: x / max(joined_training_df_subset['num_class_samples'])) # Re sort: joined_training_df_subset = joined_training_df_subset.sort_values(['top_5_acc', 'num_class_samples'], ascending=False) print('num_training_samples_per_class: %s' % joined_training_df_subset['num_class_samples'].values) print('training data colors: %s' % joined_training_df_subset['data_color'].values) training_cmap = plt.get_cmap('GnBu') training_colors = cmap(joined_training_df_subset['data_color'].values) rects = ax.barh(joined_df['class'], joined_df['top_5_acc'], color=training_colors) sm = ScalarMappable(cmap=training_cmap, norm=plt.Normalize(0, max(joined_training_df_subset['num_class_samples']))) sm.set_clim(vmin=0, vmax=max(joined_training_df_subset['num_class_samples'])) cbar = plt.colorbar(sm, drawedges=False) if dataset == 'GoingDeeper': cbar.set_label('Number of Class Samples (%s Set)' % preceding_process, rotation=270, labelpad=25, fontsize=24) plt.ylabel('Species/Scientific Name', fontsize=24) plt.xlabel('Top-5 Accuracy', fontsize=24) plt.title('Winning Model: Top-5 Accuracy by Class', fontsize=24) # plt.suptitle('%s %s Set' % (dataset, process)) ax.tick_params(labelsize=16) cbar.ax.tick_params(labelsize=16) # Invert the y-axis so it maches the order of the source dataframe: ax.invert_yaxis() ax.set_yticks([]) elif dataset == 'BOONE': cbar.set_label('Number of Class Samples (%s Set)' % preceding_process, rotation=270, labelpad=25, fontsize=24) plt.ylabel('Species/Scientific Name', fontsize=24) plt.xlabel('Top-5 Accuracy', fontsize=24) plt.title('Winning Model: Top-5 Accuracy by Class (Excluding 100% Accurate)', fontsize=24) # plt.suptitle('%s %s Set' % (dataset, process)) ax.tick_params(labelsize=16) cbar.ax.tick_params(labelsize=16) # Invert the y-axis so it maches the order of the source dataframe: ax.invert_yaxis() plt.show()
def plot_directivity(engine, source, store_id, distance=300 * km, azi_begin=0., azi_end=360., dazi=1., phase_begin='first{stored:any_P}-10%', phase_end='last{stored:any_S}+50', quantity='displacement', envelope=False, component='R', fmin=0.01, fmax=0.1, hillshade=True, cmap=None, plot_mt='full', show_phases=True, show_description=True, reverse_time=False, show_nucleations=True, axes=None, nthreads=0): '''Plot the directivity and radiation characteristics of source models Synthetic seismic traces (R, T or Z) are forward-modelled at a defined radius, covering the full or partial azimuthal range and projected on a polar plot. Difference in the amplitude are enhanced by hillshading the data. :param engine: Forward modelling engine :type engine: :py:class:`~pyrocko.gf.seismosizer.Engine` :param source: Parametrized source model :type source: :py:class:`~pyrocko.gf.seismosizer.Source` :param store_id: Store ID used for forward modelling :type store_id: str :param distance: Distance in [m] :type distance: float :param azi_begin: Begin azimuth in [deg] :type azi_begin: float :param azi_end: End azimuth in [deg] :type azi_end: float :param dazi: Delta azimuth, bin size [deg] :type dazi: float :param phase_begin: Start time of the window :type phase_begin: :py:class:`~pyrocko.gf.meta.Timing` :param phase_end: End time of the window :type phase_end: :py:class:`~pyrocko.gf.meta.Timing` :param quantity: Seismogram quantity, default ``displacement`` :type quantity: str :param envelope: Plot envelop instead of seismic trace :type envelope: bool :param component: Forward modelled component, default ``R``. Choose from `RTZ` :type component: str :param fmin: Bandpass lower frequency [Hz], default ``0.01`` :type fmin: float :param fmax: Bandpass upper frequency [Hz], default ``0.1`` :type fmax: float :param hillshade: Enable hillshading, default ``True`` :type hillshade: bool :param cmap: Matplotlit colormap to use, default ``seismic``. When ``envelope`` is ``True`` the default colormap will be ``Reds``. :type cmap: str :param plot_mt: Plot a centered moment tensor, default ``full``. Choose from ``full, deviatoric, dc or False`` :type plot_mt: str, bool :param show_phases: Show annotations, default ``True`` :type show_phases: bool :param show_description: Show desciption, default ``True`` :type show_description: bool :param reverse_time: Reverse time axis. First phases arrive at the center, default ``False`` :type reverse_time: bool :param show_nucleations: Show nucleation piercing points on the moment tensor, default ``True`` :type show_nucleations: bool :param axes: Give axes to plot into :type axes: :py:class:`matplotlib.axes.Axes` :param nthreads: Number of threads used for forward modelling, default ``0`` - all available cores :type nthreads: int ''' if axes is None: fig = plt.figure() ax = fig.add_subplot(111, polar=True) else: fig = axes.figure ax = axes if envelope and cmap is None: cmap = 'Reds' elif cmap is None: cmap = 'seismic' targets, azimuths = get_azimuthal_targets(store_id, source, distance, azi_begin, azi_end, dazi, components='R', quantity=quantity) store = engine.get_store(store_id) mt = source.pyrocko_moment_tensor(store=store, target=targets[0]) resp = engine.process(source, targets, nthreads=nthreads) data, times = get_seismogram_array(resp, fmin, fmax, component=component, envelope=envelope) timing_begin = Timing(phase_begin) timing_end = Timing(phase_end) nucl_depth = source.depth nucl_distance = distance if hasattr(source, 'nucleation_x') and hasattr(source, 'nucleation_y'): try: iter(source.nucleation_x) nx = float(source.nucleation_x[0]) ny = float(source.nucleation_y[0]) except TypeError: nx = source.nucleation_x ny = source.nucleation_y nucl_distance += nx * source.length / 2. nucl_depth += ny * num.sin(source.dip * d2r) * source.width / 2. if hasattr(source, 'anchor'): anch_x, anch_y = map_anchor[source.anchor] nucl_distance -= anch_x * source.length / 2. nucl_depth -= anch_y * num.sin(source.dip * d2r) * source.width / 2. tbegin = store.t(timing_begin, (nucl_depth, nucl_distance)) tend = store.t(timing_end, (nucl_depth, nucl_distance)) tsel = num.logical_and(times >= tbegin, times <= tend) data = data[:, tsel].T times = times[tsel] duration = times[-1] - times[0] vmax = num.abs(data).max() cmw = ScalarMappable(cmap=cmap) cmw.set_array(data) cmw.set_clim(-vmax, vmax) if envelope: cmw.set_clim(0., vmax) ax.set_theta_zero_location("N") ax.set_theta_direction(-1) strike_label = mt.strike1 if hasattr(source, 'strike'): strike_label = source.strike try: ax.set_rlabel_position(strike_label % 180.) except AttributeError: logger.warn('Old matplotlib version: cannot set label positions') def r_fmt(v, p): if v < tbegin or v > tend: return '' return '%g s' % v ax.yaxis.set_major_formatter(FuncFormatter(r_fmt)) if reverse_time: ax.set_rlim(times[0] - .3 * duration, times[-1]) else: ax.set_rlim(times[-1] + .3 * duration, times[0]) ax.grid(zorder=20) if isinstance(plot_mt, str): mt_size = .15 beachball.plot_beachball_mpl(mt, ax, beachball_type=plot_mt, size=mt_size, size_units='axes', color_t=(0.7, 0.4, 0.4), position=(.5, .5), linewidth=1.) if hasattr(source, 'nucleation_x') and hasattr(source, 'nucleation_y')\ and show_nucleations: try: iter(source.nucleation_x) nucleation_x = source.nucleation_x nucleation_y = source.nucleation_y except TypeError: nucleation_x = [source.nucleation_x] nucleation_y = [source.nucleation_y] for nx, ny in zip(nucleation_x, nucleation_y): angle = float(num.arctan2(ny, nx)) rtp = num.array([[1., angle, (90. - source.strike) * d2r]]) points = beachball.numpy_rtp2xyz(rtp) x, y = beachball.project(points, projection='lambert').T norm = num.sqrt(x**2 + y**2) x = x / norm * mt_size / 2. y = y / norm * mt_size / 2. ax.plot(x + .5, y + .5, 'x', ms=6, mew=2, mec='darkred', mfc='red', transform=ax.transAxes, zorder=10) mesh = ax.pcolormesh(azimuths * d2r, times, data, cmap=cmw.cmap, shading='gouraud', zorder=0) if hillshade: mesh.update_scalarmappable() color = mesh.get_facecolor() color = hillshade_seismogram_array(data, color, shad_lim=(.85, 1.), blend_mode='multiply') mesh.set_facecolor(color) if show_phases: _phase_begin = Timing(phase_begin) _phase_end = Timing(phase_end) for p in (_phase_begin, _phase_end): p.offset = 0. p.offset_is_slowness = False p.offset_is_percent = False tphase_first = store.t(_phase_begin, (nucl_depth, nucl_distance)) tphase_last = store.t(_phase_end, (nucl_depth, nucl_distance)) theta = num.linspace(0, 2 * num.pi, 360) tfirst = num_full_like(theta, tphase_first) tlast = num_full_like(theta, tphase_last) ax.plot(theta, tfirst, color='k', alpha=.3, lw=1.) ax.plot(theta, tlast, color='k', alpha=.3, lw=1.) ax.text(num.pi * 7 / 5, tphase_first, '|'.join(_phase_begin.phase_defs), ha='left', color='k', fontsize='small') ax.text(num.pi * 6 / 5, tphase_last, '|'.join(_phase_end.phase_defs), ha='left', color='k', fontsize='small') description = ('Component {component:s}\n' 'Distance {distance:g} km').format(component=component, distance=distance / km) if show_description: if fmin and fmax: description += '\nBandpass {fmin:g} - {fmax:g} Hz'.format( fmin=fmin, fmax=fmax) elif fmin: description += '\nHighpass {fmin:g} Hz'.format(fmin=fmin) elif fmax: description += '\nLowpass {fmax:g} Hz'.format(fmax=fmax) ax.text(-.05, -.05, description, fontsize='small', ha='left', va='bottom', transform=ax.transAxes) cbar_label = QUANTITY_LABEL[quantity] if envelope: cbar_label = 'Envelope ' + cbar_label cb = fig.colorbar(cmw, ax=ax, orientation='vertical', shrink=.8, pad=0.11) cb.set_label(cbar_label) if axes is None: plt.show() return resp
def calculate_atom_plane_curvature(sublattice, zone_vector_index, func='strain_grad', atom_planes=None, sampling=None, units='pix', vmin=None, vmax=None, cmap='inferno', title='Curvature Map', filename=None, plot_and_return_fits=False, **kwargs): """ Calculates the curvature of the sublattice atom planes along the direction given by `zone_vector_index`. In the case of [1] below, the curvature is the inverse of the radius of curvature, and is effectively equal to the second derivative of the displacement direction of the atoms. Because the first derivative is negligible, the curvature can be calculated as the strain gradient [2]. With the parameter func="strain_grad", this function calculates the strain gradient of the atom planes of a Atomap Sublattice object. Parameters ---------- sublattice : Atomap Sublattice object zone_vector_index : int The index of the zone axis (translation symmetry) found by the Atomap function `construct_zone_axes()`. func : 'strain_grad' or function Function that can be used by `scipy.optimize.curve_fit`. If func='strain_grad', then the `temul.signal_processing.sine_wave_function_strain_gradient` function will be used. atom_planes : tuple, optional The starting and ending atom plane to be computed. Useful if only a section of the image should be fitted with sine waves. Given in the form e.g., (0, 3). sampling : float, optional sampling of an image in units of units/pix units : string, default "pix" Units of sampling, for display purposes. vmin, vmax, cmap : see Matplotlib documentation, default None title : string, default 'Curvature Map' filename : string, default None Name of the file to be saved. plot_and_return_fits : Bool, default False If set to True, each atom plane fitting will be plotted along with its respective atom positions. The fitting parameters (popt) will be returned as a list. **kwargs keyword arguments to be passed to `scipy.optimize.curve_fit`. Examples -------- >>> from temul.dummy_data import sine_wave_sublattice >>> import temul.api as tml >>> sublattice = sine_wave_sublattice() >>> sublattice.construct_zone_axes(atom_plane_tolerance=1) >>> sublattice.plot() >>> sampling = 0.05 # nm/pix >>> cmap='bwr' >>> curvature_map = tml.calculate_atom_plane_curvature(sublattice, ... zone_vector_index=0, sampling=sampling, units='nm', cmap=cmap) Just compute several atom planes: >>> curvature_map = tml.calculate_atom_plane_curvature(sublattice, 0, ... atom_planes=(0,3), sampling=sampling, units='nm', cmap=cmap) You can also provide initial fitting estimations via scipy's curve_fit: >>> p0 = [2, 1, 1, 15] >>> kwargs = {'p0': p0} >>> curvature_map, fittings = tml.calculate_atom_plane_curvature( ... sublattice, zone_vector_index=0, atom_planes=(0,3), ... sampling=sampling, units='nm', cmap=cmap, **kwargs, ... plot_and_return_fits=True) Returns ------- Curvature Map as a Hyperspy Signal2D References ---------- .. [1] Reference: Function adapted from a script written by Dr. Marios Hadjimichael, and used in paper_name. The original MATLAB script can be found in TEMUL/publication_examples/PTO_marios_hadj .. [2] Reference: Landau and Lifshitz, Theory of Elasticity, Vol 7, pp 47-49, 1981 """ if sublattice.zones_axis_average_distances is None: raise Exception("zones_axis_average_distances is empty. " "Has sublattice.construct_zone_axes() been run?") else: zone_vector = sublattice.zones_axis_average_distances[ zone_vector_index] atom_plane_list = sublattice.atom_planes_by_zone_vector[zone_vector] if atom_planes is not None: atom_plane_list = atom_plane_list[atom_planes[0]:atom_planes[1]] if func == 'strain_grad': func = sine_wave_function_strain_gradient curvature = [] x_list, y_list = [], [] fittings_list = [] for atom_plane in atom_plane_list: # fit a sine wave to the atoms in the atom_plane params, _ = curve_fit(func, atom_plane.x_position, atom_plane.y_position, **kwargs) # calculate the second derivative of the sine wave # with respect to x analytically (to extract the strain gradient) second_der = derivative(func, np.asarray(atom_plane.x_position), dx=1e-6, n=2, args=(params)) if plot_and_return_fits: fittings_list.append(params) plt.figure() plt.scatter(atom_plane.x_position, atom_plane.y_position) plt.plot(atom_plane.x_position, func(atom_plane.x_position, *params), 'r-', label=f'fit params: {params}') plt.legend(loc='lower left') plt.show() x_list.extend(atom_plane.x_position) y_list.extend(atom_plane.y_position) curvature.extend(list(second_der)) if sampling is not None: curvature = [i * sampling for i in curvature] curvature_map = sublattice.get_property_map(x_list, y_list, curvature, upscale_map=1) if sampling is not None: curvature_map.axes_manager[0].scale = sampling curvature_map.axes_manager[1].scale = sampling curvature_map.axes_manager[0].units = units curvature_map.axes_manager[1].units = units curvature_map.plot(vmin=vmin, vmax=vmax, cmap=cmap, colorbar=False) # need to put in colorbar axis units like in get_strain_map plt.gca().axes.get_xaxis().set_visible(False) plt.gca().axes.get_yaxis().set_visible(False) plt.title("{} of Index {}".format(title, zone_vector_index)) cbar = ScalarMappable(cmap=cmap) cbar.set_array(curvature) cbar.set_clim(vmin, vmax) plt.colorbar(cbar, fraction=0.046, pad=0.04, label=f"Curvature (1/{units})") plt.tight_layout() if filename is not None: plt.savefig(fname="{}_{}_{}.png".format(filename, title, zone_vector_index), transparent=True, frameon=False, bbox_inches='tight', pad_inches=None, dpi=300, labels=False) curvature_map.save("{}_{}_{}.hspy".format(filename, title, zone_vector_index)) if plot_and_return_fits: return (curvature_map, fittings_list) else: return (curvature_map)
class Plotter(object): def __init__(self, notify, figure, settings): self.notify = notify self.figure = figure self.settings = settings self.axes = None # self.bar = None self.scalarMap = None self.barBase = None self.threadPlot = None self.extent = None self.lines = {} self.labels = {} self.overflowLabels = {} self.overflow = {'left': [], 'right': [], 'top': [], 'bottom': []} self.__setup_plot() self.set_grid(self.settings.grid) def __setup_plot(self): formatter = ScalarFormatter(useOffset=False) gs = GridSpec(1, 2, width_ratios=[9.5, 0.5]) self.axes = self.figure.add_subplot(gs[0], facecolor=self.settings.background) self.axes.set_xlabel("Frequency (MHz)") self.axes.set_ylabel('Level (dB/Hz)') self.axes.xaxis.set_major_formatter(formatter) self.axes.yaxis.set_major_formatter(formatter) self.axes.xaxis.set_minor_locator(AutoMinorLocator(10)) self.axes.yaxis.set_minor_locator(AutoMinorLocator(10)) self.axes.set_xlim(self.settings.start, self.settings.stop) self.axes.set_ylim(-50, 0) self.bar_ax = self.figure.add_subplot(gs[1]) norm = Normalize(vmin=-50, vmax=0) # self.barBase = ColorbarBase(self.bar_ax, norm=norm) self.scalarMap = ScalarMappable(norm=norm) self.barBase = self.figure.colorbar(self.scalarMap, cax=self.bar_ax) self.set_colourmap_use(self.settings.colourMapUse) self.__setup_measure() self.__setup_overflow() self.hide_measure() def __setup_measure(self): dashesAvg = [4, 5, 1, 5, 1, 5] dashesGM = [5, 5, 5, 5, 1, 5, 1, 5] dashesHalf = [1, 5, 5, 5, 5, 5] self.lines[Markers.MIN] = Line2D([0, 0], [0, 0], linestyle='--', color='black') self.lines[Markers.MAX] = Line2D([0, 0], [0, 0], linestyle='-.', color='black') self.lines[Markers.AVG] = Line2D([0, 0], [0, 0], dashes=dashesAvg, color='magenta') self.lines[Markers.GMEAN] = Line2D([0, 0], [0, 0], dashes=dashesGM, color='green') self.lines[Markers.HP] = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='purple') self.lines[Markers.HFS] = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='purple') self.lines[Markers.HFE] = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='purple') self.lines[Markers.OP] = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='#996600') self.lines[Markers.OFS] = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='#996600') self.lines[Markers.OFE] = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='#996600') if matplotlib.__version__ >= '1.3': effect = patheffects.withStroke(linewidth=3, foreground="w", alpha=0.75) self.lines[Markers.MIN].set_path_effects([effect]) self.lines[Markers.MAX].set_path_effects([effect]) self.lines[Markers.AVG].set_path_effects([effect]) self.lines[Markers.GMEAN].set_path_effects([effect]) self.lines[Markers.HP].set_path_effects([effect]) self.lines[Markers.HFS].set_path_effects([effect]) self.lines[Markers.HFE].set_path_effects([effect]) self.lines[Markers.OP].set_path_effects([effect]) self.lines[Markers.OFS].set_path_effects([effect]) self.lines[Markers.OFE].set_path_effects([effect]) for line in list(self.lines.values()): self.axes.add_line(line) bbox = self.axes.bbox box = dict(boxstyle='round', fc='white', ec='black', clip_box=bbox) self.labels[Markers.MIN] = Text(0, 0, 'Min', fontsize='xx-small', ha="right", va="bottom", bbox=box, color='black') self.labels[Markers.MAX] = Text(0, 0, 'Max', fontsize='xx-small', ha="right", va="top", bbox=box, color='black') box['ec'] = 'magenta' self.labels[Markers.AVG] = Text(0, 0, 'Mean', fontsize='xx-small', ha="right", va="center", bbox=box, color='magenta') box['ec'] = 'green' self.labels[Markers.GMEAN] = Text(0, 0, 'GMean', fontsize='xx-small', ha="right", va="center", bbox=box, color='green') box['ec'] = 'purple' self.labels[Markers.HP] = Text(0, 0, '-3dB', fontsize='xx-small', ha="right", va="center", bbox=box, color='purple') self.labels[Markers.HFS] = Text(0, 0, '-3dB Start', fontsize='xx-small', ha="center", va="top", bbox=box, color='purple') self.labels[Markers.HFE] = Text(0, 0, '-3dB End', fontsize='xx-small', ha="center", va="top", bbox=box, color='purple') box['ec'] = '#996600' self.labels[Markers.OP] = Text(0, 0, 'OBW', fontsize='xx-small', ha="right", va="center", bbox=box, color='#996600') self.labels[Markers.OFS] = Text(0, 0, 'OBW Start', fontsize='xx-small', ha="center", va="top", bbox=box, color='#996600') self.labels[Markers.OFE] = Text(0, 0, 'OBW End', fontsize='xx-small', ha="center", va="top", bbox=box, color='#996600') for label in list(self.labels.values()): self.axes.add_artist(label) def __setup_overflow(self): bbox = self.axes.bbox box = dict(boxstyle='round', fc='white', ec='black', alpha=0.5, clip_box=bbox) self.overflowLabels['left'] = Text(0, 0.9, '', fontsize='xx-small', ha="left", va="top", bbox=box, transform=self.axes.transAxes, alpha=0.5) self.overflowLabels['right'] = Text(1, 0.9, '', fontsize='xx-small', ha="right", va="top", bbox=box, transform=self.axes.transAxes, alpha=0.5) self.overflowLabels['top'] = Text(0.9, 1, '', fontsize='xx-small', ha="right", va="top", bbox=box, transform=self.axes.transAxes, alpha=0.5) self.overflowLabels['bottom'] = Text(0.9, 0, '', fontsize='xx-small', ha="right", va="bottom", bbox=box, transform=self.axes.transAxes, alpha=0.5) for label in list(self.overflowLabels.values()): self.axes.add_artist(label) def __clear_overflow(self): for label in self.overflowLabels: self.overflow[label] = [] def __draw_hline(self, marker, y): line = self.lines[marker] label = self.labels[marker] xLim = self.axes.get_xlim() yLim = self.axes.get_ylim() if yLim[0] <= y <= yLim[1]: line.set_visible(True) line.set_xdata([xLim[0], xLim[1]]) line.set_ydata([y, y]) self.axes.draw_artist(line) label.set_visible(True) label.set_position((xLim[1], y)) self.axes.draw_artist(label) elif y is not None and y < yLim[0]: self.overflow['bottom'].append(marker) elif y is not None and y > yLim[1]: self.overflow['top'].append(marker) def __draw_vline(self, marker, x): line = self.lines[marker] label = self.labels[marker] yLim = self.axes.get_ylim() xLim = self.axes.get_xlim() if x is not None: if xLim[0] <= x <= xLim[1]: line.set_visible(True) line.set_xdata([x, x]) line.set_ydata([yLim[0], yLim[1]]) self.axes.draw_artist(line) label.set_visible(True) label.set_position((x, yLim[1])) self.axes.draw_artist(label) elif x < xLim[0]: self.overflow['left'].append(marker) elif x > xLim[1]: self.overflow['right'].append(marker) def __draw_overflow(self): for pos, overflow in list(self.overflow.items()): if len(overflow) > 0: text = '' for measure in overflow: if len(text) > 0: text += '\n' text += self.labels[measure].get_text() label = self.overflowLabels[pos] if pos == 'top': textMath = '$\\blacktriangle$\n' + text elif pos == 'bottom': textMath = '$\\blacktriangledown$\n' + text elif pos == 'left': textMath = '$\\blacktriangleleft$\n' + text elif pos == 'right': textMath = '$\\blacktriangleright$\n' + text label.set_text(textMath) label.set_visible(True) self.axes.draw_artist(label) def draw_measure(self, measure, show): if self.axes.get_renderer_cache() is None: return self.hide_measure() self.__clear_overflow() if show[Measure.MIN]: y = measure.get_min_p()[1] self.__draw_hline(Markers.MIN, y) if show[Measure.MAX]: y = measure.get_max_p()[1] self.__draw_hline(Markers.MAX, y) if show[Measure.AVG]: y = measure.get_avg_p() self.__draw_hline(Markers.AVG, y) if show[Measure.GMEAN]: y = measure.get_gmean_p() self.__draw_hline(Markers.GMEAN, y) if show[Measure.HBW]: xStart, xEnd, y = measure.get_hpw() self.__draw_hline(Markers.HP, y) self.__draw_vline(Markers.HFS, xStart) self.__draw_vline(Markers.HFE, xEnd) if show[Measure.OBW]: xStart, xEnd, y = measure.get_obw() self.__draw_hline(Markers.OP, y) self.__draw_vline(Markers.OFE, xStart) self.__draw_vline(Markers.OFE, xEnd) self.__draw_overflow() def hide_measure(self): for line in list(self.lines.values()): line.set_visible(False) for label in list(self.labels.values()): label.set_visible(False) for label in list(self.overflowLabels.values()): label.set_visible(False) def scale_plot(self, force=False): if self.extent is not None: if self.settings.autoF or force: self.axes.set_xlim(self.extent.get_f()) if self.settings.autoL or force: self.axes.set_ylim(self.extent.get_l()) if self.settings.plotFunc == PlotFunc.VAR and len( self.axes.collections) > 0: norm = self.axes.collections[0].norm self.scalarMap.set_clim((norm.vmin, norm.vmax)) else: self.scalarMap.set_clim(self.extent.get_l()) norm = Normalize(vmin=self.extent.get_l()[0], vmax=self.extent.get_l()[1]) for collection in self.axes.collections: collection.set_norm(norm) try: self.barBase.draw_all() except: pass def redraw_plot(self): if self.figure is not None: post_event(self.notify, EventThread(Event.DRAW)) def get_axes(self): return self.axes def get_axes_bar(self): return self.barBase.ax def get_bar(self): return self.barBase def get_plot_thread(self): return self.threadPlot def set_title(self, title): self.axes.set_title(title, fontsize='medium') def set_plot(self, spectrum, extent, annotate=False): self.extent = extent self.threadPlot = ThreadPlot(self, self.settings, self.axes, spectrum, self.extent, self.scalarMap, self.barBase, annotate) self.threadPlot.start() return self.threadPlot def clear_plots(self): children = self.axes.get_children() for child in children: if child.get_gid() is not None: if child.get_gid() in [ 'plot', 'peak', 'peakText', 'peakShadow', 'peakThres' ]: child.remove() def set_grid(self, on): self.axes.grid(on) self.redraw_plot() def set_bar(self, on): self.barBase.ax.set_visible(on) if on: self.axes.change_geometry(1, 2, 1) self.axes.get_subplotspec().get_gridspec().set_width_ratios( [9.5, 0.5]) else: self.axes.change_geometry(1, 1, 1) self.figure.subplots_adjust() def set_axes(self, on): if on: self.axes.set_axis_on() self.bar_ax.set_axis_on() else: self.axes.set_axis_off() self.bar_ax.set_axis_off() def set_colourmap_use(self, on): self.set_bar(on) if on: colourMap = self.settings.colourMap else: colourMap = get_colours()[0] self.set_colourmap(colourMap) def set_colourmap(self, colourMap): self.settings.colourMap = colourMap for collection in self.axes.collections: collection.set_cmap(colourMap) if get_colours().index(colourMap) < 4: self.set_bar(False) else: self.set_bar(True) # this is deprecated, work on the scalarMap directly # self.barBase.set_cmap(colourMap) self.scalarMap.set_cmap(colourMap) try: self.barBase.draw_all() except: pass def close(self): self.figure.clear() self.figure = None
# for location in locations['features']: # print location locations = locations["features"][20:22] pca = PCA_sklearn() pca.fit(predictors, locations, startdate=startdate, enddate=enddate) # pca.save('pca.nc') # pca.load('pca.nc') reduced_predictors = pca.transform(predictors, startdate=startdate, enddate=enddate) vmin = 0 vmax = 50 mappable = ScalarMappable(cmap="Blues") mappable.set_array(np.arange(vmin, vmax, 0.1)) mappable.set_clim((vmin, vmax)) id = 20 for pred in reduced_predictors: tree = BinaryTree(pred, maxdepth=10) fig = plt.fig = plt.figure(figsize=(10, 10)) ax = fig.add_subplot(111) # ax.scatter(np.array(tree.samples)[:,0], np.array(tree.samples)[:,1], s=1, alpha=0.4, color='black') values = np.ma.masked_less(predictand_data[:, id], 0.1) # ax.scatter(np.array(tree.samples)[:,0], np.array(tree.samples)[:,1], c='grey', s=5, alpha=0.3) # ax.scatter(np.array(tree.samples)[:,0], np.array(tree.samples)[:,1], c=values, s=values, alpha=0.7) tree.plot_density(ax, mappable) plt.show()
def parallel_axes_plot(archive, ax=None, bc_order=None, cmap="magma", linewidth=1.5, alpha=0.8, vmin=None, vmax=None, sort_archive=False, cbar_orientation='horizontal', cbar_pad=0.1): """Visualizes archive entries in behavior space with a parallel axes plot. This visualization is meant to show the coverage of the behavior space at a glance. Each axis represents one behavioral dimension, and each line in the diagram represents one entry in the archive. Three main things are evident from this plot: - **Behavior space coverage,** as determined by the amount of the axis that has lines passing through it. If the lines are passing through all parts of the axis, then there is likely good coverage for that BC. - **Correlation between neighboring BCs.** In the below example, we see perfect correlation between ``behavior_0`` and ``behavior_1``, since none of the lines cross each other. We also see the perfect negative correlation between ``behavior_3`` and ``behavior_4``, indicated by the crossing of all lines at a single point. - **Whether certain values of the behavior dimensions affect the objective value strongly.** In the below example, we see ``behavior_2`` has many entries with high objective near zero. This is more visible when ``sort_archive`` is passed in, as entries with higher objective values will be plotted on top of individuals with lower objective values. Examples: .. plot:: :context: close-figs >>> import numpy as np >>> import matplotlib.pyplot as plt >>> from ribs.archives import GridArchive >>> from ribs.visualize import parallel_axes_plot >>> # Populate the archive with the negative sphere function. >>> archive = GridArchive( ... [20, 20, 20, 20, 20], ... [(-1, 1), (-1, 1), (-1, 1), (-1, 1), (-1, 1)] ... ) >>> archive.initialize(solution_dim=3) >>> for x in np.linspace(-1, 1, 100): ... for y in np.linspace(0, 1, 100): ... for z in np.linspace(-1, 1, 100): ... archive.add( ... solution=np.array([x,y,z]), ... objective_value=-(x**2 + y**2 + z**2), ... behavior_values=np.array([0.5*x,x,y,z,-0.5*z]), ... ) >>> # Plot a heatmap of the archive. >>> plt.figure(figsize=(8, 6)) >>> parallel_axes_plot(archive) >>> plt.title("Negative sphere function") >>> plt.ylabel("axis values") >>> plt.show() Args: archive (ArchiveBase): Any ribs archive. ax (matplotlib.axes.Axes): Axes on which to create the plot. If None, the current axis will be used. bc_order (list of int or list of (int, str)): If this is a list of ints, it specifies the axes order for BCs (e.g. ``[2, 0, 1]``). If this is a list of tuples, each tuple takes the form ``(int, str)`` where the int specifies the BC index and the str specifies a name for the BC (e.g. ``[(1, "y-value"), (2, "z-value"), (0, "x-value)]``). The order specified does not need to have the same number of elements as the number of behaviors in the archive, e.g. ``[1, 3]`` or ``[1, 2, 3, 2]``. cmap (str, list, matplotlib.colors.Colormap): Colormap to use when plotting intensity. Either the name of a colormap, a list of RGB or RGBA colors (i.e. an Nx3 or Nx4 array), or a colormap object. linewidth (float): Line width for each entry in the plot. alpha (float): Opacity of the line for each entry (passing a low value here may be helpful if there are many archive entries, as more entries would be visible). vmin (float): Minimum objective value to use in the plot. If None, the minimum objective value in the archive is used. vmax (float): Maximum objective value to use in the plot. If None, the maximum objective value in the archive is used. sort_archive (boolean): if true, sorts the archive so that the highest performing entries are plotted on top of lower performing entries. .. warning:: This may be slow for large archives. cbar_orientation (str): The orientation of the colorbar. Use either ``'vertical'`` or ``'horizontal'`` cbar_pad (float): The amount of padding to use for the colorbar. Raises: ValueError: ``cbar_orientation`` has an invalid value. ValueError: The bcs provided do not exist in the archive. TypeError: bc_order is not a list of all ints or all tuples. """ # Try getting the colormap early in case it fails. cmap = _retrieve_cmap(cmap) # Check that the orientation input is correct. if cbar_orientation not in ['vertical', 'horizontal']: raise ValueError("cbar_orientation mus be 'vertical' or 'horizontal' " f"but is '{cbar_orientation}'") # If there is no order specified, plot in increasing numerical order. if bc_order is None: cols = [f"behavior_{i}" for i in range(archive.behavior_dim)] axis_labels = cols lower_bounds = archive.lower_bounds upper_bounds = archive.upper_bounds # Use the requested behaviors (may be less than the original number of bcs). else: # Check for errors in specification. if all(isinstance(bc, int) for bc in bc_order): bc_indices = np.array(bc_order) axis_labels = [f"behavior_{i}" for i in bc_indices] elif all( len(bc) == 2 and isinstance(bc[0], int) and isinstance(bc[1], str) for bc in bc_order): bc_indices, axis_labels = zip(*bc_order) bc_indices = np.array(bc_indices) else: raise TypeError("bc_order must be a list of ints or a list of" "tuples in the form (int, str)") if np.max(bc_indices) >= archive.behavior_dim: raise ValueError(f"Invalid Behavior: requested behavior index " f"{np.max(bc_indices)}, but archive only has " f"{archive.behavior_dim} behaviors.") if any(bc < 0 for bc in bc_indices): raise ValueError("Invalid Behavior: requested a negative behavior" " index.") # Find the indices of the requested order. cols = [f"behavior_{i}" for i in bc_indices] lower_bounds = archive.lower_bounds[bc_indices] upper_bounds = archive.upper_bounds[bc_indices] host_ax = plt.gca() if ax is None else ax # Try to get current axis. df = archive.as_pandas(include_solutions=False) if sort_archive: df.sort_values(by=['objective'], inplace=True) vmin = np.min(df['objective']) if vmin is None else vmin vmax = np.max(df['objective']) if vmax is None else vmax norm = matplotlib.colors.Normalize(vmin=vmin, vmax=vmax, clip=True) objectives = df['objective'].to_numpy() ys = df[cols].to_numpy() y_ranges = upper_bounds - lower_bounds # Transform all data to be in the first axis coordinates. normalized_ys = np.zeros_like(ys) normalized_ys[:, 0] = ys[:, 0] normalized_ys[:, 1:] = ( (ys[:, 1:] - lower_bounds[1:]) / y_ranges[1:] * y_ranges[0] + lower_bounds[0]) # Copy the axis for the other bcs. axes = [host_ax] + [host_ax.twinx() for i in range(len(cols) - 1)] for i, axis in enumerate(axes): axis.set_ylim(lower_bounds[i], upper_bounds[i]) axis.spines['top'].set_visible(False) axis.spines['bottom'].set_visible(False) if axis != host_ax: axis.spines['left'].set_visible(False) axis.yaxis.set_ticks_position('right') axis.spines["right"].set_position(("axes", i / (len(cols) - 1))) host_ax.set_xlim(0, len(cols) - 1) host_ax.set_xticks(range(len(cols))) host_ax.set_xticklabels(axis_labels) host_ax.tick_params(axis='x', which='major', pad=7) host_ax.spines['right'].set_visible(False) host_ax.xaxis.tick_top() for archive_entry, objective in zip(normalized_ys, objectives): # Draw straight lines between the axes in the appropriate color. color = cmap(norm(objective)) host_ax.plot(range(len(cols)), archive_entry, c=color, alpha=alpha, linewidth=linewidth) # Create a colorbar. mappable = ScalarMappable(cmap=cmap) mappable.set_clim(vmin, vmax) host_ax.figure.colorbar(mappable, pad=cbar_pad, orientation=cbar_orientation)
class SliceViewWidget(LayoutCanvas): def __init__(self, context, width=11.7, height=8.3, dpi=100, parent=None): """ :type context: segyviewlib.SliceViewContext """ super(SliceViewWidget, self).__init__(width, height, dpi, parent) self._assigned_slice_models = list(context.models) """ :type: list[SliceModel] """ self._slice_views = {} """ :type: dict[matplotlib.axes.Axes,SliceView] """ self._colormappable = ScalarMappable(cmap=context.colormap) self._colormappable.set_array([]) self._context = context context.context_changed.connect(self._context_changed) context.data_changed.connect(self._data_changed) context.data_source_changed.connect(self._layout_changed) self.layout_changed.connect(self._layout_changed) self.subplot_pressed.connect(self._subplot_clicked) self.subplot_scrolled.connect(self._subplot_scrolled) self.subplot_motion.connect(self._subplot_motion) def _create_context(self): return self._context.create_context(self._assigned_slice_models) def _layout_changed(self): fig = self.layout_figure() axes = fig.layout_axes() self._slice_views.clear() for model in self._context.models: model.visible = False for index, ax in enumerate(axes): ax.clear() if index < len(self._assigned_slice_models): model = self._assigned_slice_models[index] model.visible = True self._slice_views[ax] = SliceView(ax, model) self._slice_views[ax].create_slice(self._create_context()) colormap_axes = self.layout_figure().colormap_axes() colorbar = self.layout_figure().colorbar(self._colormappable, cax=colormap_axes, use_gridspec=True) colorbar.ax.tick_params(labelsize=9) self._context.load_data() self._data_changed() def _data_changed(self): context = self._create_context() for slice_view in self._slice_views.values(): slice_view.data_changed(context) self._context_changed() def _context_changed(self): ctx = self._create_context() for slice_view in self._slice_views.values(): slice_view.context_changed(ctx) self._colormappable.set_cmap(ctx['colormap']) self._colormappable.set_clim(ctx['min'], ctx['max']) self.draw() def _create_slice_view_context_menu(self, subplot_index): context_menu = QMenu("Slice Model Reassignment Menu", self) def reassign(model): def fn(): self._assigned_slice_models[subplot_index] = model self._layout_changed() return fn for model in self._context.models: action = QAction(model.title, self) action.triggered.connect(reassign(model)) context_menu.addAction(action) return context_menu def _subplot_clicked(self, event): keys = event['key'] if self._context.indicators and event['button'] == 1 and not keys: x = int(event['x']) y = int(event['y']) slice_model = self._get_slice_view(event).model() self._context.update_index_for_direction(slice_model.x_index_direction, x) self._context.update_index_for_direction(slice_model.y_index_direction, y) elif event['button'] == 3 and (not keys or keys.state(ctrl=True)): subplot_index = event['subplot_index'] context_menu = self._create_slice_view_context_menu(subplot_index) context_menu.exec_(self.mapToGlobal(QPoint(event['mx'], self.height() - event['my']))) def _subplot_scrolled(self, event): keys = event['key'] if not keys: # at least 1 step per event but a maximum of 5 step = max(1, abs(int(event['step']))) step = min(step, 5) step = copysign(step, event['step']) slice_model = self._get_slice_view(event).model() index = int(slice_model.index + step) if 0 <= index < len(slice_model): self._context.update_index_for_direction(slice_model.index_direction, index) elif keys.state(ctrl=True) or keys.state(shift=True): x = event['x'] y = event['y'] step = max(1, abs(int(event['step']))) step = copysign(step / 20.0, event['step']) slice_view = self._get_slice_view(event) if slice_view.zoom(x, y, step): self._context_changed() def _get_slice_view(self, event): subplot_index = event['subplot_index'] fig = self.layout_figure() ax = fig.layout_axes()[subplot_index] slice_view = self._slice_views[ax] return slice_view def _subplot_motion(self, event): keys = event['key'] if keys.state(shift=True) or keys.state(ctrl=True): dx = event['dx'] dy = event['dy'] if dx is not None and dy is not None: slice_view = self._get_slice_view(event) slice_view.pan(dx, dy) self._context_changed()
def contourf(self, band=1, window=None, axes=None, vmin=None, vmax=None, cmap='topobathy', levels=None, show=False, title=None, figsize=None, colors=256, cbar_label=None, norm=None, **kwargs): if axes is None: fig = plt.figure(figsize=figsize) axes = fig.add_subplot(111) values = self.get_values(band=band, masked=True, window=window) vmin = np.min(values) if vmin is None else float(vmin) vmax = np.max(values) if vmax is None else float(vmax) # def get_cmap( # values, # vmin, # vmax, # cmap=None, # levels=None, # colors=256, # norm=None, # ): # colors = int(colors) # if cmap is None: # cmap = plt.cm.get_cmap('jet') # if levels is None: # levels = np.linspace(vmin, vmax, colors) # col_val = 0. # elif cmap == 'topobathy': # if vmax <= 0.: # cmap = plt.cm.seismic # col_val = 0. # levels = np.linspace(vmin, vmax, colors) # else: # wet_count = int(np.floor(colors*(float((values < 0.).sum()) # / float(values.size)))) # col_val = float(wet_count)/colors # dry_count = colors - wet_count # colors_undersea = plt.cm.bwr(np.linspace(1., 0., wet_count)) # colors_land = plt.cm.terrain(np.linspace(0.25, 1., dry_count)) # colors = np.vstack((colors_undersea, colors_land)) # cmap = LinearSegmentedColormap.from_list('cut_terrain', colors) # wlevels = np.linspace(vmin, 0.0, wet_count, endpoint=False) # dlevels = np.linspace(0.0, vmax, dry_count) # levels = np.hstack((wlevels, dlevels)) # else: # cmap = plt.cm.get_cmap(cmap) # levels = np.linspace(vmin, vmax, colors) # col_val = 0. # if vmax > 0: # if norm is None: # norm = FixPointNormalize( # sealevel=0.0, # vmax=vmax, # vmin=vmin, # col_val=col_val # ) # return cmap, norm, levels, col_val # cmap, norm, levels, col_val = get_cmap( # values, vmin, vmax, cmap, levels, colors, norm) cmap, norm, levels, col_val = figures.get_topobathy_kwargs() axes.contourf(self.get_x(window), self.get_y(window), values, levels=levels, cmap=cmap, norm=norm, vmin=vmin, vmax=vmax, **kwargs) axes.axis('scaled') # if extent is not None: # axes.axis(extent) if title is not None: axes.set_title(title) mappable = ScalarMappable(cmap=cmap) mappable.set_array([]) mappable.set_clim(vmin, vmax) divider = make_axes_locatable(axes) cax = divider.append_axes("bottom", size="2%", pad=0.5) cbar = plt.colorbar( mappable, cax=cax, # extend=cmap_extend, orientation='horizontal') if col_val != 0: cbar.set_ticks([vmin, vmin + col_val * (vmax - vmin), vmax]) cbar.set_ticklabels([np.around(vmin, 2), 0.0, np.around(vmax, 2)]) else: cbar.set_ticks([vmin, vmax]) cbar.set_ticklabels([np.around(vmin, 2), np.around(vmax, 2)]) if cbar_label is not None: cbar.set_label(cbar_label) if show is True: plt.show() return axes
def plot_incentives_inputs(incentives_data, max_incentive, max_age, max_income, name_run): """Plot the incentives input Parameters ---------- incentives_data: pandas DataFrame from the ModeIncentives.csv input file max_incentive: float Maximum amount allowed for an incentive as defined in the Starter Kit "Inputs Specifications" page max_age: int Maximum age of any resident in Sioux Faux as defined in the Starter Kit "Inputs Specifications" page max_income: int Maximum income of any resident in Sioux Faux as defined in the Starter Kit "Inputs Specifications" page name_run: str Name of the run , e.g. "BAU", "Run 1", "Submission"... Returns ------- ax: matplotlib axes object """ incentives = process_incentives_data(incentives_data, max_incentive) fig, ax = plt.subplots(1, 2, figsize=(14, 5), sharey=True, gridspec_kw={'width_ratios': [4, 5]}) # color map my_cmap = plt.cm.get_cmap('YlOrRd') colors = my_cmap(incentives["amount_normalized"]) print(incentives.head()) # plot ax[0].barh(incentives["mode"], incentives["max_age"] - incentives["min_age"], color=colors, left=min(incentives["min_income"])) ax[1].barh(incentives["mode"], incentives["max_income"] - incentives["min_income"], color=colors, left=min(incentives["min_income"])) ax[0].set_xlabel("age") ax[0].set_xlim((0, max_age)) ax[1].set_xlabel("income") ax[1].set_xlim((0, max_income)) plt.suptitle(f"Input - Incentives by age and income group - {name_run}", fontsize=15, fontweight="bold") sm = ScalarMappable(cmap=my_cmap, norm=plt.Normalize(0, np.max(incentives["amount"]))) sm.set_array([]) sm.set_clim(0, max_incentive) cbar = fig.colorbar(sm, ticks=[i for i in range(0, max_incentive + 1, 10)]) cbar.set_label('Incentive amount [$/person-trip]', rotation=270, labelpad=25) return ax
class HyperSpec3DH5View(HyperSpectralBaseView): name = 'hyperspec_3d_h5' supported_measurements = [ 'oo_asi_hyperspec_3d_scan', 'andor_asi_hyperspec_3d_scan', ] def scan_specific_setup(self): pass def setup(self): self.settings.New('sample', dtype=str, initial='') self.settings.New('z_slice', dtype=float, choices=[0.0], initial=0.0) self.settings.New('show_3d', dtype=bool, initial=False) self.settings.New('vol_alpha', dtype=float, vmin=0.0, vmax=1.0, initial=0.5) self.settings.New('vol_colormap', dtype=str, initial='viridis', choices=[ 'viridis', 'plasma', 'inferno', 'magma', 'cividis', 'Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds', 'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu', 'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn' ]) # self.settings.New('vol_percentile', dtype=int, vmin=0, vmax=49, # initial=5) self.settings.New('vol_percentile_min', dtype=int, vmin=0, vmax=100, initial=5) self.settings.New('vol_percentile_max', dtype=int, vmin=0, vmax=100, initial=95) self.settings.New('vol_transparent_percentile', dtype=int, vmin=0, vmax=100, initial=5) self.settings.New('vol_transparent_min', dtype=bool, initial=False) self.settings.z_slice.updated_choice_index_value.connect( self.on_update_zslice_choice) # self.settings.vol_colormap.updated_value.connect(self.calculate_volume) # self.settings.vol_alpha.updated_value.connect(self.calculate_volume) HyperSpectralBaseView.setup(self) voldata = np.empty((1, 1, 1, 4), dtype=np.ubyte) voldata[0, 0, 0, :] = [255, 255, 255, 0] self.volitem = GLVolumeItem(data=voldata) self.glview = GLViewWidget() self.glaxis = GLAxisItem() self.glgrid = GLGridItem() self.glview.addItem(self.glgrid) self.glview.addItem(self.glaxis) self.glview.addItem(self.volitem) self.gldock = self.dockarea.addDock(name='3D', widget=self.glview, position='below', relativeTo=self.image_dock) self.calculate_3d_pushButton = QPushButton(text='calculate_3d') self.settings_ui.layout().addWidget(self.calculate_3d_pushButton) self.calculate_3d_pushButton.clicked.connect(self.calculate_volume) self.image_dock.raiseDock() def is_file_supported(self, fname): return np.any([(meas_name in fname) for meas_name in self.supported_measurements]) def reset(self): if hasattr(self, 'dat'): self.dat.close() del self.dat if hasattr(self, 'spec_map'): del self.spec_map if hasattr(self, 'scalebar'): self.imview.getView().removeItem(self.scalebar) del self.scalebar if hasattr(self, 'volume'): spoof_data = np.zeros((1, 1, 1, 4), dtype=np.ubyte) self.volitem.setData(spoof_data) del self.volume self.settings.show_3d.update_value(False) self.image_dock.raiseDock() def load_data(self, fname): self.dat = h5py.File(fname) for meas_name in self.supported_measurements: if meas_name in self.dat['measurement']: self.M = self.dat['measurement'][meas_name] for map_name in ['hyperspectral_map', 'spec_map']: if map_name in self.M: self.spec_map = np.array(self.M[map_name]) self.h_span = self.M['settings'].attrs['h_span'] self.x_array = np.array(self.M['h_array']) self.z_array = np.array(self.M['z_array']) units = self.M['settings/units'].attrs['h_span'] if units == 'mm': self.h_span = self.h_span * 1e-3 self.z_span = self.z_array * 1e-3 self.settings.z_slice.change_unit('mm') if 'dark_indices' in list(self.M.keys()): print('dark indices found') dark_indices = self.M['dark_indices'] if dark_indices.len() == 0: self.spec_map = np.delete(self.spec_map, list(dark_indices.shape), -1) else: self.spec_map = np.delete(self.spec_map, np.array(dark_indices), -1) else: print('no dark indices') self.hyperspec_data = self.spec_map[0, :, :, :] self.display_image = self.hyperspec_data.sum(axis=-1) self.settings.z_slice.change_choice_list(self.z_array.tolist()) self.settings.z_slice.update_value(self.z_array[0]) self.spec_x_array = np.arange(self.hyperspec_data.shape[-1]) for x_axis_name in [ 'wavelength', 'wls', 'wave_numbers', 'raman_shifts' ]: if x_axis_name in self.M: x_array = np.array(self.M[x_axis_name]) if 'dark_indices' in list(self.M.keys()): dark_indices = self.M['dark_indices'] # The following is to read a dataset I initialized # incorrectly for dark pixels. This can be replaced with # the else statement entirely now that the measurement is # fixed, but I still have a long measurement that will # benefit from this. if dark_indices.len() == 0: x_array = np.delete(x_array, list(dark_indices.shape), 0) else: x_array = np.delete(x_array, np.array(dark_indices), 0) self.add_spec_x_array(x_axis_name, x_array) self.x_axis.update_value(x_axis_name) sample = self.dat['app/settings'].attrs['sample'] self.settings.sample.update_value(sample) self.calculate_volume() def on_update_zslice_choice(self, index): if hasattr(self, 'spec_map'): self.hyperspec_data = self.spec_map[index, :, :, :] self.display_images['default'] = self.hyperspec_data self.display_images['sum'] = self.hyperspec_data.sum(axis=-1) self.spec_x_arrays['default'] = self.spec_x_array self.spec_x_arrays['index'] = np.arange( self.hyperspec_data.shape[-1]) self.recalc_bandpass_map() self.recalc_median_map() self.update_display() def calculate_volume(self): if not self.settings['show_3d']: print('calculate_volume called without show_3d') return print('calculating 3d volume') t0 = time.time() if hasattr(self, 'volume'): del self.volume if hasattr(self, 'mappable'): self.mappable.set_cmap(self.settings['vol_colormap']) else: self.mappable = ScalarMappable(cmap=self.settings['vol_colormap']) z_span = self.z_array[-1] - self.z_array[0] dx = self.x_array[1] - self.x_array[0] z_interp_array = np.linspace(np.amin(self.z_array), np.amax(self.z_array), num=z_span / dx) z_interp = None self.volume = None nz = len(z_interp_array) if self.settings['display_image'] == 'bandpass_map': print('bandpass_map selected') x, slice = self.get_xhyperspec_data(apply_use_x_slice=True) ind_min = np.nonzero(self.spec_x_array == x[0])[0][0] ind_max = np.nonzero(self.spec_x_array == x[-1])[0][0] data = np.zeros((len(self.z_array), ) + slice.shape) data = self.spec_map[:, :, :, ind_min:ind_max] # for kk in range(len(self.z_array)): # print( # 'grabbing bandpass layer %d of %d' % (kk, len(self.z_array))) # self.settings.z_slice.update_value(self.z_array[kk]) # x, data[kk, :, :, :] = self.get_xhyperspec_data( # apply_use_x_slice=True) z_interp = interp1d(self.z_array, data, axis=0) else: z_interp = interp1d(self.z_array, self.spec_map, axis=0) data = z_interp(z_interp_array) self.volume = np.zeros(data.shape[:-1] + (4, ), dtype=np.ubyte) pmin = self.settings['vol_percentile_min'] pmax = self.settings['vol_percentile_max'] self.mappable.set_array(data.sum(axis=-1)) vmin = np.percentile(data.sum(axis=-1), pmin) vmax = np.percentile(data.sum(axis=-1), pmax) tmin = np.percentile(data.sum(axis=-1), self.settings['vol_transparent_percentile']) self.mappable.set_clim(vmin=vmin, vmax=vmax) # self.mappable.autoscale() for kk in range(nz): print('calculating rgba vals for %d of %d layers' % (kk, nz)) sum_data = data[kk, :, :, :].sum(axis=-1) # print(sum_data.shape, self.volume.shape) self.volume[kk, :, :, :] = self.mappable.to_rgba( sum_data, alpha=self.settings['vol_alpha'], bytes=True) if self.settings['vol_transparent_min']: self.volume[kk, :, :, 3][np.nonzero(sum_data <= tmin)] = 0 print('3d volume calculation complete') t1 = time.time() print('time elapsed: %0.3f s' % (t1 - t0)) kwargs = {'x': len(self.x_array), 'y': len(self.x_array), 'z': nz} self.glaxis.setSize(**kwargs) self.glgrid.setSize(**kwargs) self.glgrid.setSpacing(x=1 / dx * 5, y=1 / dx * 5, z=1 / dx * 5) # print(self.mappable.get_cmap().name) # print(data.shape, self.volume.shape) def update_display(self): if hasattr(self, 'scalebar'): self.imview.getView().removeItem(self.scalebar) if self.display_image is not None: # pyqtgraph axes are x,y, but data is stored in (frame, y,x, time), # so we need to transpose self.imview.getImageItem().setImage(self.display_image.T) nn = self.display_image.shape if hasattr(self, 'h_span'): span = self.h_span else: span = -1 self.scalebar = ConfocalScaleBar(span=span, num_px=nn[0]) self.scalebar.setParentItem(self.imview.getView()) self.scalebar.anchor((1, 1), (1, 1), offset=(-20, -20)) if hasattr(self, 'volume') and self.settings['show_3d']: self.volitem.setData(np.swapaxes(self.volume, 0, 2)) self.on_change_rect_roi() self.on_update_circ_roi()
class SpiroGraph(object): ''' Spirograph drawer with matplotlib slider widgets to change parameters. Parameters of line are: R: The radius of the big circle r: The radius of the small circle which rolls along the inside of the bigger circle p: distance from centre of smaller circle to point in the circle where the pen hole is. tmax: the angle through which the smaller circle is rotated to draw the spirograph tstep: how often matplotlib plots a point a, b, c: parameters of the linewidth equation. ''' # kwargs for each of the matplotlib sliders slider_kwargs = ( {'label': 't_max', 'valmin': np.pi, 'valmax': 200 * np.pi, 'valinit': tmax0, 'valfmt': PiString()}, {'label': 't_step', 'valmin': 0.01, 'valmax': 10, 'valinit': tstep0}, {'label': 'R', 'valmin': 1, 'valmax': 200, 'valinit': R0}, {'label': 'r', 'valmin': 1, 'valmax': 200, 'valinit': r0}, {'label': 'p', 'valmin': 1, 'valmax': 200, 'valinit': p0}, {'label': 'colour', 'valmin': 0, 'valmax': 1, 'valinit': 1}, {'label': 'width_a', 'valmin': 0.5, 'valmax': 10, 'valinit': 1}, {'label': 'width_b', 'valmin': 0, 'valmax': 10, 'valinit': 0}, {'label': 'width_c', 'valmin': 0, 'valmax': 10, 'valinit': 0.5}) rbutton_kwargs = ( {'labels': ('black', 'white'), 'activecolor': 'white', 'active': 0}, {'labels': ('solid', 'variable'), 'activecolor': 'white', 'active': 0}) def __init__(self, colormap, figsize=(7, 10)): self.colormap_name = colormap self.variable_color = False # Use ScalarMappable to map full colormap to range 0 - 1 self.colormap = ScalarMappable(cmap=colormap) self.colormap.set_clim(0, 1) # set up main axis onto which to draw spirograph self.figsize = figsize plt.rcParams['figure.figsize'] = figsize self.fig, self.mainax = plt.subplots() plt.subplots_adjust(bottom=0.3) title = self.mainax.set_title('Spirograph Drawer!', size=20, color='white') self.text = [title, ] # set up slider axes self.slider_axes = [plt.axes([0.25, x, 0.65, 0.015]) for x in np.arange(0.05, 0.275, 0.025)] # same again for radio buttons self.rbutton_axes = [plt.axes([0.025, x, 0.1, 0.15]) for x in np.arange(0.02, 0.302, 0.15)] # use log scale for tstep slider self.slider_axes[1].set_xscale('log') # turn off frame, ticks and tick labels for all axes for ax in chain(self.slider_axes, self.rbutton_axes, [self.mainax, ]): ax.axis('off') # use axes and kwargs to create list of sliders/rbuttons self.sliders = [Slider(ax, **kwargs) for ax, kwargs in zip(self.slider_axes, self.slider_kwargs)] self.rbuttons = [RadioButtons(ax, **kwargs) for ax, kwargs in zip(self.rbutton_axes, self.rbutton_kwargs)] self.update_figcolors() # set up initial line self.t = np.arange(0, tmax0, tstep0) x, y = spiro_linefunc(self.t, R0, r0, p0) self.linecollection = LineCollection( segments(x, y), linewidths=spiro_linewidths(self.t, a0, b0, c0), color=self.colormap.to_rgba(col0)) self.mainax.add_collection(self.linecollection) # creates the plot and connects sliders to various update functions self.run() def update_figcolors(self, bgcolor='black'): ''' function run by background color radiobutton. Sets all labels, text, and sliders to foreground color, all axes to background color ''' fgcolor = 'white' if bgcolor == 'black' else 'black' self.fig.set_facecolor(bgcolor) self.mainax.set_axis_bgcolor(bgcolor) for ax in chain(self.slider_axes, self.rbutton_axes): ax.set_axis_bgcolor(bgcolor) # set fgcolor elements to black or white, mostly elements of sliders for item in chain(map(attrgetter('label'), self.sliders), map(attrgetter('valtext'), self.sliders), map(attrgetter('poly'), self.sliders), self.text, *map(attrgetter('labels'), self.rbuttons)): item.set_color(fgcolor) self.update_radiobutton_colors() plt.draw() def update_linewidths(self, *args): ''' function run by a, b and c parameter sliders. Sets width of each line in linecollection according to sine function ''' a, b, c = (s.val for s in self.sliders[6:]) self.linecollection.set_linewidths(spiro_linewidths(self.t, a, b, c)) plt.draw() def update_linecolors(self, *args): ''' function run by color slider and indirectly by variable/solid color radiobutton. Updates colors of each line in linecollection using the set colormap. ''' # get current color value (a value between 1 and 0) col_val = self.sliders[5].val if not self.variable_color: # if solid color, convert color value to rgb and set the color self.linecollection.set_color(self.colormap.to_rgba(col_val)) else: # create values between 0 and 1 for each line segment colors = (self.t / max(self.t)) + col_val # use color value to roll colors colors[colors > 1] -= 1 self.linecollection.set_color( [self.colormap.to_rgba(i) for i in colors]) plt.draw() def update_lineverts(self, *args): ''' function run by R, r, p, tmax and tstep sliders to update line vertices ''' tmax, tstep, R, r, p = (s.val for s in self.sliders[:5]) self.t = np.arange(0, tmax, tstep) x, y = spiro_linefunc(self.t, R, r, p) self.linecollection.set_verts(segments(x, y)) # change axis limits to pad new line nicely self.mainax.set(xlim=(min(x) - 5, max(x) + 5), ylim=(min(y) - 5, max(y) + 5)) plt.draw() def update_linecolor_setting(self, val): ''' function run by solid/variable colour slider, alters variable_color attribute then calls update_linecolors ''' if val == 'variable': self.variable_color = True elif val == 'solid': self.variable_color = False # need to update radiobutton colors here. self.update_radiobutton_colors() self.update_linecolors() def update_radiobutton_colors(self): ''' makes radiobutton colors correct even on a changing axis background ''' bgcolor = self.rbuttons[0].value_selected fgcolor = 'white' if bgcolor == 'black' else 'black' for i, b in enumerate(self.rbuttons): # find out index of the active button active_idx = self.rbutton_kwargs[i]['labels'].index( b.value_selected) # set button colors accordingly b.circles[not active_idx].set_color(bgcolor) b.circles[active_idx].set_color(fgcolor) def run(self): ''' set up slider functions ''' verts_func = self.update_lineverts colors_func = self.update_linecolors widths_func = self.update_linewidths # create iterable of slider funcs to zip with sliders slider_update_funcs = chain(repeat(verts_func, 5), [colors_func, ], repeat(widths_func, 3)) # set slider on_changed functions for s, f in zip(self.sliders, slider_update_funcs): s.on_changed(f) self.rbuttons[0].on_clicked(self.update_figcolors) self.rbuttons[1].on_clicked(self.update_linecolor_setting) plt.show()
class Spectrogram(object): def __init__(self, notify, figure, settings): self.notify = notify self.figure = figure self.settings = settings self.data = [[], [], []] self.axes = None self.plot = None self.extent = None # self.bar = None self.scalarMap = None self.barBase = None self.lines = {} self.labels = {} self.overflowLabels = {} self.overflow = {'left': [], 'right': []} self.threadPlot = None self.__setup_plot() self.set_grid(self.settings.grid) def __setup_plot(self): gs = GridSpec(1, 2, width_ratios=[9.5, 0.5]) self.axes = self.figure.add_subplot(gs[0], facecolor=self.settings.background) self.axes.set_xlabel("Frequency (MHz)") self.axes.set_ylabel('Time') numFormatter = ScalarFormatter(useOffset=False) timeFormatter = DateFormatter("%H:%M:%S") self.axes.xaxis.set_major_formatter(numFormatter) self.axes.yaxis.set_major_formatter(timeFormatter) self.axes.xaxis.set_minor_locator(AutoMinorLocator(10)) self.axes.yaxis.set_minor_locator(AutoMinorLocator(10)) self.axes.set_xlim(self.settings.start, self.settings.stop) now = time.time() self.axes.set_ylim(utc_to_mpl(now), utc_to_mpl(now - 10)) self.bar_ax = self.figure.add_subplot(gs[1]) norm = Normalize(vmin=-50, vmax=0) # self.barBase = ColorbarBase(self.bar, norm=norm, # cmap=cm.get_cmap(self.settings.colourMap)) self.scalarMap = ScalarMappable(norm=norm, cmap=cm.get_cmap( self.settings.colourMap)) self.barBase = self.figure.colorbar(self.scalarMap, cax=self.bar_ax) self.__setup_measure() self.__setup_overflow() self.hide_measure() def __setup_measure(self): dashesHalf = [1, 5, 5, 5, 5, 5] self.lines[Markers.HFS] = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='purple') self.lines[Markers.HFE] = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='purple') self.lines[Markers.OFS] = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='#996600') self.lines[Markers.OFE] = Line2D([0, 0], [0, 0], dashes=dashesHalf, color='#996600') if matplotlib.__version__ >= '1.3': effect = patheffects.withStroke(linewidth=3, foreground="w", alpha=0.75) self.lines[Markers.HFS].set_path_effects([effect]) self.lines[Markers.HFE].set_path_effects([effect]) self.lines[Markers.OFS].set_path_effects([effect]) self.lines[Markers.OFE].set_path_effects([effect]) for line in list(self.lines.values()): self.axes.add_line(line) bbox = self.axes.bbox box = dict(boxstyle='round', fc='white', ec='purple', clip_box=bbox) self.labels[Markers.HFS] = Text(0, 0, '-3dB', fontsize='xx-small', ha="center", va="top", bbox=box, color='purple') self.labels[Markers.HFE] = Text(0, 0, '-3dB', fontsize='xx-small', ha="center", va="top", bbox=box, color='purple') box['ec'] = '#996600' self.labels[Markers.OFS] = Text(0, 0, 'OBW', fontsize='xx-small', ha="center", va="top", bbox=box, color='#996600') self.labels[Markers.OFE] = Text(0, 0, 'OBW', fontsize='xx-small', ha="center", va="top", bbox=box, color='#996600') for label in list(self.labels.values()): self.axes.add_artist(label) def __setup_overflow(self): bbox = self.axes.bbox box = dict(boxstyle='round', fc='white', ec='black', alpha=0.5, clip_box=bbox) self.overflowLabels['left'] = Text(0, 0.9, '', fontsize='xx-small', ha="left", va="top", bbox=box, transform=self.axes.transAxes, alpha=0.5) self.overflowLabels['right'] = Text(1, 0.9, '', fontsize='xx-small', ha="right", va="top", bbox=box, transform=self.axes.transAxes, alpha=0.5) for label in list(self.overflowLabels.values()): self.axes.add_artist(label) def __clear_overflow(self): for label in self.overflowLabels: self.overflow[label] = [] def __draw_vline(self, marker, x): line = self.lines[marker] label = self.labels[marker] yLim = self.axes.get_ylim() xLim = self.axes.get_xlim() if xLim[0] < x < xLim[1]: line.set_visible(True) line.set_xdata([x, x]) line.set_ydata([yLim[0], yLim[1]]) self.axes.draw_artist(line) label.set_visible(True) label.set_position((x, yLim[1])) self.axes.draw_artist(label) elif x is not None and x < xLim[0]: self.overflow['left'].append(marker) elif x is not None and x > xLim[1]: self.overflow['right'].append(marker) def __draw_overflow(self): for pos, overflow in list(self.overflow.items()): if len(overflow) > 0: text = '' for measure in overflow: if len(text) > 0: text += '\n' text += self.labels[measure].get_text() label = self.overflowLabels[pos] if pos == 'left': textMath = '$\\blacktriangleleft$\n' + text elif pos == 'right': textMath = '$\\blacktriangleright$\n' + text label.set_text(textMath) label.set_visible(True) self.axes.draw_artist(label) def draw_measure(self, measure, show): if self.axes.get_renderer_cache() is None: return self.hide_measure() self.__clear_overflow() if show[Measure.HBW]: xStart, xEnd, _y = measure.get_hpw() self.__draw_vline(Markers.HFS, xStart) self.__draw_vline(Markers.HFE, xEnd) if show[Measure.OBW]: xStart, xEnd, _y = measure.get_obw() self.__draw_vline(Markers.OFS, xStart) self.__draw_vline(Markers.OFE, xEnd) self.__draw_overflow() def hide_measure(self): for line in list(self.lines.values()): line.set_visible(False) for label in list(self.labels.values()): label.set_visible(False) for label in list(self.overflowLabels.values()): label.set_visible(False) def scale_plot(self, force=False): if self.figure is not None and self.plot is not None: extent = self.plot.get_extent() if self.settings.autoF or force: if extent[0] == extent[1]: extent[1] += 1 self.axes.set_xlim(extent[0], extent[1]) if self.settings.autoL or force: vmin, vmax = self.plot.get_clim() self.scalarMap.set_clim(vmin, vmax) try: self.barBase.draw_all() except: pass if self.settings.autoT or force: self.axes.set_ylim(extent[2], extent[3]) def redraw_plot(self): if self.figure is not None: post_event(self.notify, EventThread(Event.DRAW)) def get_axes(self): return self.axes def get_axes_bar(self): return self.barBase.ax def get_bar(self): return self.barBase def get_plot_thread(self): return self.threadPlot def set_title(self, title): self.axes.set_title(title, fontsize='medium') def set_plot(self, spectrum, extent, annotate=False): self.extent = extent self.threadPlot = ThreadPlot(self, self.settings, self.axes, spectrum, self.extent, self.barBase, annotate) self.threadPlot.start() def clear_plots(self): children = self.axes.get_children() for child in children: if child.get_gid() is not None: if child.get_gid() in [ 'plot', 'peak', 'peakText', 'peakShadow', 'peakThres' ]: child.remove() def set_grid(self, on): if on: self.axes.grid(True, color='w') else: self.axes.grid(False) self.redraw_plot() def set_colourmap(self, colourMap): if self.plot is not None: self.plot.set_cmap(colourMap) self.barBase.set_cmap(colourMap) try: self.barBase.draw_all() except: pass def close(self): self.figure.clear() self.figure = None
def add_colorbar(ax, cmap, norm): sm = ScalarMappable(cmap=cmap, norm=norm) sm.set_clim(norm.vmin, norm.vmax) plt.colorbar(sm, ax=ax)
def cvt_archive_heatmap(archive, ax=None, plot_centroids=True, plot_samples=False, transpose_bcs=False, cmap="magma", square=False, ms=1, lw=0.5, vmin=None, vmax=None): """Plots heatmap of a :class:`~ribs.archives.CVTArchive` with 2D behavior space. Essentially, we create a Voronoi diagram and shade in each cell with a color corresponding to the objective value of that cell's elite. Depending on how many bins are in the archive, ``ms`` and ``lw`` may need to be tuned. If there are too many bins, the Voronoi diagram and centroid markers will make the entire image appear black. In that case, try turning off the centroids with ``plot_centroids=False`` or even removing the lines completely with ``lw=0``. Examples: .. plot:: :context: close-figs >>> import numpy as np >>> import matplotlib.pyplot as plt >>> from ribs.archives import CVTArchive >>> from ribs.visualize import cvt_archive_heatmap >>> # Populate the archive with the negative sphere function. >>> archive = CVTArchive(100, [(-1, 1), (-1, 1)]) >>> archive.initialize(solution_dim=2) >>> for x in np.linspace(-1, 1, 100): ... for y in np.linspace(-1, 1, 100): ... archive.add(solution=np.array([x,y]), ... objective_value=-(x**2 + y**2), ... behavior_values=np.array([x,y])) >>> # Plot a heatmap of the archive. >>> plt.figure(figsize=(8, 6)) >>> cvt_archive_heatmap(archive) >>> plt.title("Negative sphere function") >>> plt.xlabel("x coords") >>> plt.ylabel("y coords") >>> plt.show() Args: archive (CVTArchive): A 2D CVTArchive. ax (matplotlib.axes.Axes): Axes on which to plot the heatmap. If None, the current axis will be used. plot_centroids (bool): Whether to plot the cluster centroids. plot_samples (bool): Whether to plot the samples used when generating the clusters. transpose_bcs (bool): By default, the first BC in the archive will appear along the x-axis, and the second will be along the y-axis. To switch this (i.e. to transpose the axes), set this to True. cmap (str, list, matplotlib.colors.Colormap): Colormap to use when plotting intensity. Either the name of a colormap, a list of RGB or RGBA colors (i.e. an Nx3 or Nx4 array), or a colormap object. square (bool): If True, set the axes aspect ratio to be "equal". ms (float): Marker size for both centroids and samples. lw (float): Line width when plotting the voronoi diagram. vmin (float): Minimum objective value to use in the plot. If None, the minimum objective value in the archive is used. vmax (float): Maximum objective value to use in the plot. If None, the maximum objective value in the archive is used. Raises: ValueError: The archive is not 2D. """ # pylint: disable = too-many-locals if archive.behavior_dim != 2: raise ValueError("Cannot plot heatmap for non-2D archive.") # Try getting the colormap early in case it fails. cmap = _retrieve_cmap(cmap) # Retrieve data from archive. lower_bounds = archive.lower_bounds upper_bounds = archive.upper_bounds centroids = archive.centroids samples = archive.samples if transpose_bcs: lower_bounds = np.flip(lower_bounds) upper_bounds = np.flip(upper_bounds) centroids = np.flip(centroids, axis=1) samples = np.flip(samples, axis=1) # Retrieve and initialize the axis. ax = plt.gca() if ax is None else ax ax.set_xlim(lower_bounds[0], upper_bounds[0]) ax.set_ylim(lower_bounds[1], upper_bounds[1]) if square: ax.set_aspect("equal") # Add faraway points so that the edge regions of the Voronoi diagram are # filled in. Refer to # https://stackoverflow.com/questions/20515554/colorize-voronoi-diagram # for more info. interval = upper_bounds - lower_bounds scale = 1000 faraway_pts = [ upper_bounds + interval * scale, # Far upper right. upper_bounds + interval * [-1, 1] * scale, # Far upper left. lower_bounds + interval * [-1, -1] * scale, # Far bottom left. lower_bounds + interval * [1, -1] * scale, # Far bottom right. ] vor = Voronoi(np.append(centroids, faraway_pts, axis=0)) # Calculate objective value for each region. `vor.point_region` contains # the region index of each point. region_obj = [None] * len(vor.regions) min_obj, max_obj = np.inf, -np.inf pt_to_obj = _get_pt_to_obj(archive) for pt_idx, region_idx in enumerate( vor.point_region[:-4]): # Exclude faraway_pts. if region_idx != -1 and pt_idx in pt_to_obj: obj = pt_to_obj[pt_idx] min_obj = min(min_obj, obj) max_obj = max(max_obj, obj) region_obj[region_idx] = obj # Override objective value range. min_obj = min_obj if vmin is None else vmin max_obj = max_obj if vmax is None else vmax # Shade the regions. for region, objective in zip(vor.regions, region_obj): # This check is O(n), but n is typically small, and creating # `polygon` is also O(n) anyway. if -1 not in region: if objective is None: color = "white" else: normalized_obj = np.clip( (objective - min_obj) / (max_obj - min_obj), 0.0, 1.0) color = cmap(normalized_obj) polygon = [vor.vertices[i] for i in region] ax.fill(*zip(*polygon), color=color, ec="k", lw=lw) # Create a colorbar. mappable = ScalarMappable(cmap=cmap) mappable.set_clim(min_obj, max_obj) ax.figure.colorbar(mappable, ax=ax, pad=0.1) # Plot the sample points and centroids. if plot_samples: ax.plot(samples[:, 0], samples[:, 1], "o", c="gray", ms=ms) if plot_centroids: ax.plot(centroids[:, 0], centroids[:, 1], "ko", ms=ms)
def parameterScape(fig, X, Y, C, fmts, sizes, cmaps, vranges, **kwargs): """ """ ax = fig.gca() defaultMargin = 1.0 xmargin = kwargs.pop('xmargin', defaultMargin) ymargin = kwargs.pop('ymargin', defaultMargin) legend = kwargs.pop('legend', None) colorbars = kwargs.pop('colorbars', None) sm = [] colors = [] for cIdx, c in enumerate(C): m = ScalarMappable(cmap=cmaps[cIdx]) m.set_clim(vranges[cIdx]) m.set_array(c) colors.append(m.to_rgba(c)) sm.append(m) for yIdx, y in enumerate(Y): for xIdx, x in enumerate(X): for cIdx, c in enumerate(C): if (np.isnan(c[yIdx, xIdx])): continue ax.plot(x, y, fmts[cIdx], color=colors[cIdx][yIdx, xIdx, :], markersize=sizes[cIdx], **kwargs) globalAxesSettings(ax) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.spines['bottom'].set_visible(False) ax.spines['left'].set_visible(False) ax.tick_params(bottom='off', top='off', left='off', right='off', length=0, pad=-5) ax.axis('scaled') dx = X[1] - X[0] dy = Y[1] - Y[0] ax.set_xlim([X[0] - xmargin * dx, X[-1] + xmargin * dx]) ax.set_ylim([Y[0] - ymargin * dy, Y[-1] + ymargin * dy]) if (legend is not None): legend_strings = legend[0] legend_kw = legend[1] if (legend_kw is None): legend_kw = {} parameterScapeLegend(ax, legend_strings, fmts, sizes, cmaps, kwargs, **legend_kw) if (colorbars is not None): cax = plotColorbars(fig, sm, **colorbars) else: cax = None return ax, cax
def __init__( self, y: np.ndarray, mesh: Mesh, vertex_oriented: bool, n_frames: int = 100, interval: int = 100, color_map: Colormap = cm.viridis, v_min: Optional[float] = None, v_max: Optional[float] = None, marker_shape: str = 'o', marker_size: Union[float, np.ndarray] = 20., marker_opacity: float = 1., **_): """ :param y: an array representing the solution of the 3D partial differential equation :param mesh: the spatial mesh over which the solution is evaluated :param vertex_oriented: whether the solution is evaluated over the vertices or the cell centers of the mesh :param n_frames: the number of frames to display :param interval: the number of milliseconds to pause between each frame :param color_map: the color map to use to map the values of the solution scalar field to colors :param v_min: the lower limit of the color map; if None, the limit is set to the minimum of the solution :param v_max: the upper limit of the color map; if None, the limit is set to the maximum of the solution :param marker_shape: the shape of the point markers :param marker_size: the size of the point markers :param marker_opacity: the opacity of the point markers :param _: any ignored extra arguments """ self._verify_pde_solution_shape_matches_problem( y, mesh, vertex_oriented, 3, False) x_cartesian_coordinate_grids = \ mesh.cartesian_coordinate_grids(vertex_oriented) mappable = ScalarMappable(cmap=color_map) mappable.set_clim( np.min(y) if v_min is None else v_min, np.max(y) if v_max is None else v_max) self._scatter_plot: Optional[PathCollection] = None fig = plt.figure() ax = fig.add_subplot(projection='3d') def init_plot(): ax.clear() ax.set_xlabel('x0') ax.set_ylabel('x1') ax.set_zlabel('x2') ax.set_box_aspect(( np.ptp(x_cartesian_coordinate_grids[0]), np.ptp(x_cartesian_coordinate_grids[1]), np.ptp(x_cartesian_coordinate_grids[2]))) self._scatter_plot = ax.scatter( *x_cartesian_coordinate_grids, c=mappable.to_rgba(y[0, ..., 0].flatten()), marker=marker_shape, s=marker_size, alpha=marker_opacity) def update_plot(time_step: int): self._scatter_plot.set_color( mappable.to_rgba(y[time_step, ..., 0].flatten())) super(ScatterPlot, self).__init__( fig, init_plot, update_plot, y.shape[0], n_frames, interval)
def style_all_tags(soup, container, background=True, margin=None, border=False, css=True, images=True, only_container=False): """Only used for debugging""" #if only_container: #soup = container (min_score, max_score) = get_min_and_max_scores(soup) min_score = -log(-min_score) max_score = log(max_score) sm = ScalarMappable(cmap=RdYlGn) sm.set_clim(0,1) if not css: for link in soup.find_all("link"): link.decompose() if not images: for img in soup.find_all("img"): img.decompose() for tag in soup.find_all(): if not isinstance(tag, element.Tag): continue if css: try: style = tag["style"].split(";") except: style = [] else: style = [] if margin is not None: if margin > 0: style.append("margin: %dpx" % margin) try: score = sum(tag.scores.values()) rgb = score_to_rgb(score, sm, min_score, max_score) tag.scores['total'] = score except: score = -1 rgb = (230, 230, 230) if background: style.append("background-color: rgb(%d,%d,%d)" % rgb) try: if tag in container.descendants and score > -30: style.append("color: #000000") else: style.append("color: #666666") except: pass if tag == container: style.append("border: 3px dashed #0000CC") style.append("color: #000000") else: if border: style.append("border: 1px solid #333333") try: tag['style'] = "; ".join(style) tag['scores'] = repr(tag.scores) del tag.scores['total'] except: pass