def nbo_vs_year(self): """ Plot percent of structures, with different NBO per 1000 atoms levels, from "good" pdb structures (all PDB files with a single model, no unknown atom types and good CRYST1 records) VS. year Second sub plot: the total of "good" structures deposited VS. year """ plt.close('all') # figure parameters # plt.ion() # enables interactive mode max_y = 105 fontsize = 20 fig = plt.figure(figsize=(8,10)) gs = GridSpec(2,1,height_ratios=[2,1]) # first subplot ax1 = plt.subplot(gs[0,0]) ax2 = plt.subplot(gs[1,0]) lines = [] line_type = ['.:','.-','.--'] n = len(line_type) for i,pd in enumerate(self.plot_data_list): lt = line_type[i%n] l, = ax1.plot(pd.years,pd.percent,lt) lines.append(l) ax1.set_ylabel('Percent of PDB structures',fontsize=fontsize) ax1.text(min(self.years)+0.5,max_y-4,'a.',fontsize=fontsize) ax1.tick_params(axis='both',labelsize=fontsize - 2) ax1.axes.get_xaxis().set_visible(False) ax1.set_yticks([5,10,40,70,100]) ax1.set_ylim([0,max_y]) ax1.set_xlim([self.start_year,self.end_year]) # legend labels = ['NBO per 1000 atom > {}']*len(self.nbo_per_1000_atoms) labels = [x.format(y) for x,y in zip(labels,self.nbo_per_1000_atoms)] if self.sym: legend_pos = [0.96,0.70] else: legend_pos = [0.54,0.30] ax1.legend( lines,labels, bbox_to_anchor=legend_pos, loc=1,borderaxespad=0.0) # Second subplot ax2.plot(self.years,self.n_total,'.:g') ax2.set_xlim([self.start_year,self.end_year]) ax2.set_xlabel('Year',fontsize=fontsize) ax2.set_ylabel('Number of structures',fontsize=fontsize) ax2.text(min(self.years)+0.5,max(self.n_total)-5,'b.',fontsize=fontsize) ax2.tick_params(axis='both',labelsize=fontsize - 2) ax2.set_xticks([self.start_year,1990,2000,self.end_year]) ax2.set_yscale('log') ax2.set_yticks([10,100,1000]) # gs.tight_layout(fig) gs.update(hspace=0) s = 'all'*(not self.sym) + 'sym'*self.sym fig_name = 'nbo_vs_year_{}.png'.format(s) plt.savefig(fig_name) fig.show()
def __render_both(self): ''' renders two plots, one with the filtered and one without. ''' times = {'start': time.time()} if self.__left_axes is None: self.__width_ratio = self.__make_width_ratio() gs = GridSpec(1, 2, width_ratios=self.__make_width_ratio(adjusted=True), wspace=0.00) gs.tight_layout(self.__chart.canvas.figure) self.__left_axes = self.__chart.canvas.figure.add_subplot( gs.new_subplotspec((0, 0))) self.__add_grid(self.__left_axes) self.__right_axes = self.__chart.canvas.figure.add_subplot( gs.new_subplotspec((0, 1))) self.__add_grid(self.__right_axes) times['l_axes_init'] = time.time() self.__left_scatter = self.__render_scatter( self.__left_cache, self.__left_axes, self.__left_scatter, self.__ui.maxFilteredFreq.value(), self.left, 'l', times) self.__set_limits(self.__left_axes, self.__ui.minFreq, self.__ui.maxFilteredFreq, self.__ui.minTime, self.__ui.maxTime) times['l_limits'] = time.time() self.__right_scatter = self.__render_scatter( self.__right_cache, self.__right_axes, self.__right_scatter, self.__ui.maxUnfilteredFreq.value(), self.right, 'r', times) self.__right_axes.set_yticklabels([]) self.__right_axes.get_yaxis().set_tick_params(length=0) self.__set_limits(self.__right_axes, self.__ui.minFreq, self.__ui.maxUnfilteredFreq, self.__ui.minTime, self.__ui.maxTime) times['r_limits'] = time.time() return times
def vis(name, obj): data = obj[:] name = "result_" + name raw_image = data.sum(-1) raw_image.T[:] -= raw_image.min(tuple(range(1, raw_image.ndim))) raw_image.T[:] /= raw_image.max(tuple(range(1, raw_image.ndim))) pad_width = ((0, 1), ) + ((0, 0), ) * (raw_image.ndim - 1) raw_image = pad(raw_image, pad_width=pad_width, mode='constant', constant_values=0) raw_image = raw_image.transpose(roll(range(raw_image.ndim), -1)) scale = obj.attrs['scale'][:] * unit_conversion X = (scale * data.shape[1:])[1::-1] extent = [0, X[0], 0, X[1]] for measure, data1 in zip(("Nucleus", "Actin"), data): psmooth0 = psmooth / scale smoothed_data = gaussian_filter(data1, psmooth0).sum(2) smoothed_data -= smoothed_data.min() smoothed_data /= smoothed_data.max() dp = (scale * psmooth0)[:2].mean() clf() fig = figure(figsize(22, 10)) gs = GridSpec(1, 2) ax = [] for gs0 in gs: ax0 = fig.add_subplot(gs0) ax0.set_adjustable("box-forced") ax0.set_xlabel("mm", fontsize="x-large") ax0.set_ylabel("mm", fontsize="x-large") ax.append(ax0) divider = make_axes_locatable(ax[0]) cax = divider.append_axes("right", size="5%", pad=0.05) im = ax[0].imshow(smoothed_data, extent=extent, interpolation="bicubic", origin="lower") cb = colorbar(im, cax=cax) cb.set_label(label="{} Density".format(measure), fontsize="x-large") ax[1].imshow(raw_image, extent=extent, interpolation="bilinear", origin="lower") gs.tight_layout(fig) savefig(join(outdir, name + "_{}.png".format(measure)))
def plot_flux_decomposition(ss, tm, name=None, source_code=None): f = plt.figure(figsize=(18, 6)) gs = GridSpec(2, 3, wspace=0.5, hspace=0.5) ax1 = plt.subplot(gs[0, 0]) ax2 = plt.subplot(gs[0, 1]) ax3 = plt.subplot(gs[0, 2]) ax4 = plt.subplot(gs[1, 0]) ax5 = plt.subplot(gs[1, 1]) ax6 = plt.subplot(gs[1, 2]) first_surface = int(len(ss) / 2) ax1.scatter(range(first_surface), ss[0:first_surface], s=80, c="r") ax1.set_title("First part of steady state eigenvector") ax1.set_ylabel("Population") ax2.scatter(range(first_surface), [tm[i][i + first_surface] for i in range(first_surface)], s=80, c="r") ax2.set_title("Transition probabilities (unbound to bound)") ax2.set_ylabel("Probability") ax3.scatter(range(first_surface), [ss[i] * tm[i][i + first_surface] for i in range(first_surface)], s=80, c="r") ax3.set_title("Steady state eigenvector * transition probabilities (unbound to bound)") ax3.set_ylabel("Population * Probability") ax4.scatter(range(first_surface, 2 * first_surface), ss[first_surface : 2 * first_surface], s=80, c="b") ax4.set_title("Second part of steady state eigenvector") ax4.set_ylabel("Population") ax5.scatter(range(first_surface), [tm[i + first_surface][i] for i in range(first_surface)], s=80, c="b") ax5.set_title("Transition probabilities (bound to unbound)") ax5.set_ylabel("Probability") ax6.scatter( range(first_surface, 2 * first_surface), [ss[i] * tm[i + first_surface][i] for i in range(first_surface)], s=80, c="b", ) ax6.set_title("Steady state eigenvector * transition probabilities (bound to unbound)") ax6.set_ylabel("Population * Probability") st = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") sts = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") if name: if source_code is not None: text = st + " " + name + " in " + source_code else: text = st + " " + name f.text(0.0, 0.0, text, fontsize=12, color="black", ha="left", va="bottom", alpha=0.5) else: f.text(0.0, 0.0, st, fontsize=12, color="black", ha="left", va="bottom", alpha=0.5) gs.tight_layout(f, rect=[0, 0, 1, 1]) plt.savefig("Figures/flux-decomposition-{}-{}".format(sts, name), dpi=150) # plt.show() plt.close()
def plot_steady_state(unbound, bound, pdf_unbound, pdf_bound, ss, name=None, source_code=None): f = plt.figure(figsize=(12, 12)) gs = GridSpec(2, 2, wspace=0.5, hspace=0.5) ax1 = plt.subplot(gs[0, 0]) ax2 = plt.subplot(gs[0, 1]) ax3 = plt.subplot(gs[1, 0]) ax4 = plt.subplot(gs[1, 1]) ax1.scatter(range(len(unbound)), unbound, s=80, c="r") ax1.plot(range(len(unbound)), unbound, lw=2, c="r") ax2.scatter(range(len(bound)), bound, s=80, c="b") ax2.plot(range(len(bound)), bound, lw=2, c="b") ax1.set_title("Unbound energy surface") ax2.set_title("Bound energy surface") bins = int(len(ss) / 2) ax3.plot(range(len(unbound)), ss[0:bins], lw=2, c="r") ax3.scatter(range(len(unbound)), ss[0:bins], s=80, c="r") ax3.plot(range(len(pdf_unbound)), pdf_unbound, lw=4, ls="--", c="k") ax3.set_title("Boltzmann and forward s.s.") ax4.plot(range(len(bound)), ss[bins : 2 * bins], lw=2, c="b") ax4.scatter(range(len(bound)), ss[bins : 2 * bins], s=80, c="b") ax4.plot(range(len(pdf_bound)), pdf_bound, lw=4, ls="--", c="k") ax4.set_title("Boltzmann and forward s.s.") ax4.set_xlabel("Reaction coordinate ($\phi$)") ax3.set_xlabel("Reaction coordinate ($\phi$)") ax1.set_ylabel("Energy (a.u.)") ax2.set_ylabel("Energy (a.u.)") ax3.set_ylabel("Population") ax4.set_ylabel("Population") ax1.set_ylim([-15, 20]) ax2.set_ylim([-15, 20]) ax3.set_ylim([0, 0.6]) ax4.set_ylim([0, 0.6]) st = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") sts = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") if name: if source_code is not None: text = st + " " + name + " in " + source_code else: text = st + " " + name f.text(0.0, 0.0, text, fontsize=12, color="black", ha="left", va="bottom", alpha=0.5) else: f.text(0.0, 0.0, st, fontsize=12, color="black", ha="left", va="bottom", alpha=0.5) gs.tight_layout(f, rect=[0, 0, 1, 1]) # plt.show() plt.savefig("Figures/steady-state-{}-{}".format(sts, name), dpi=150) plt.close()
def plot_images(images, labels): gs = GridSpec(6, 7) gs.update(wspace=0.03, hspace=0.03) # set the spacing between axes. fig = plt.figure(figsize=(12,12)) image_titles = get_label_map('signnames.csv', labels) for i in range(len(images)-1): ax = fig.add_subplot(gs[i]) img = images[i] ax.imshow(img) ax.set_aspect('equal') ax.set_title(image_titles[i],fontsize=7) plt.axis('off') gs.tight_layout(fig) plt.show()
def plot_flux( flux_unbound, unbound_scale, flux_bound, bound_scale, flux_between, between_scale, name=None, source_code=None ): f = plt.figure(figsize=(12, 12)) gs = GridSpec(2, 2, wspace=0.5, hspace=0.5) ax1 = plt.subplot(gs[0, 0]) ax2 = plt.subplot(gs[0, 1]) ax3 = plt.subplot(gs[1, 0]) # ax1.scatter(range(len(flux_unbound)), flux_unbound, s=80, c='r') ax1.plot(range(len(flux_unbound)), flux_unbound * 10 ** unbound_scale, lw=2, c="r") # ax2.scatter(range(len(flux_bound)), flux_bound, s=80, c='b') ax2.plot(range(len(flux_bound)), flux_bound * 10 ** bound_scale, lw=2, c="b") ax1.set_title("Unbound energy surface") ax2.set_title("Bound energy surface") # ax3.scatter(range(len(flux_between)), flux_between, s=80, c='k') ax3.autoscale(enable=False, axis="y") ax3.plot(range(len(flux_between)), flux_between * 10 ** between_scale, lw=4, ls="-", c="k") ax3.set_title("Flux between surfaces") ax3.set_xlabel("Reaction coordinate ($\phi$)") ax1.set_ylabel("Flux ($\\times 10^{{{}}}$)".format(unbound_scale), size=20) ax2.set_ylabel("Flux ($\\times 10^{{{}}}$)".format(bound_scale), size=20) ax3.set_ylabel("Flux ($\\times 10^{{{}}}$)".format(between_scale), size=20) st = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") sts = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") if name: if source_code is not None: text = st + " " + name + " in " + source_code else: text = st + " " + name f.text(0.0, 0.0, text, fontsize=12, color="black", ha="left", va="bottom", alpha=0.5) else: f.text(0.0, 0.0, st, fontsize=12, color="black", ha="left", va="bottom", alpha=0.5) gs.tight_layout(f, rect=[0, 0, 1, 1]) plt.savefig("Figures/flux-{}-{}.png".format(sts, name), dpi=150) # plt.show() plt.close()
def fig2(): grid = WaveSimulation(2**8, 2**8, length_x=1, length_y=1, zmin=0, zmax=2) grid.creat_gaussian_beam(0.05) A_lens, z = grid.lens_phase(focal_length=20, nargout=2) E_until_lens = grid.E0 * A_lens plt.imshow(np.abs(E_until_lens)) plt.imshow(np.angle(E_until_lens)) plt.show() intersity3d = make_intersity3d(grid, E_until_lens, z) print(np.argmax) fig2 = plt.figure(2) gs = GridSpec(4, 4) # 4 rows, 4 columns gs.tight_layout(fig2, rect=[None, None, None, None]) ax1 = fig2.add_subplot(gs[0:2, 0:2]) # First row, first column ax1.imshow(intersity3d[:, :, grid.nz // 2]) ax1.set_title("XY") plt.axis('off') ax2 = fig2.add_subplot(gs[0:2, 2:4]) # First row, second column ax2.imshow((intersity3d[grid.n // 2 - 1, :, :].T)**3) ax2.set_title("XZ") plt.axis('off') ax3 = fig2.add_subplot(gs[2, 0]) # First row, third column plt.axis('off') ax4 = fig2.add_subplot(gs[2, 1]) # Second row, span all columns plt.axis('off') ax5 = fig2.add_subplot(gs[2, 2]) # First row, third column plt.axis('off') ax6 = fig2.add_subplot(gs[2, 3]) # Second row, span all columns plt.axis('off') ax7 = fig2.add_subplot(gs[3, 0]) # First row, third column plt.axis('off') ax8 = fig2.add_subplot(gs[3, 1]) # Second row, span all columns plt.axis('off') ax9 = fig2.add_subplot(gs[3, 2]) # First row, third column plt.axis('off') ax10 = fig2.add_subplot(gs[3, 3]) # Second row, span all columns plt.axis('off') plt.tight_layout() plt.show()
def _decorate_plot(fig, ax, artist, plot_data, color_data, legend, cmap): ax.set_xlabel(plot_data.xlabel) ax.set_ylabel(plot_data.ylabel) if plot_data.xticks is not None: ax.set_xticks(np.arange(len(plot_data.xticks))) ax.set_xticklabels(plot_data.xticks) if plot_data.yticks is not None: ax.set_yticks(np.arange(len(plot_data.yticks))) ax.set_yticklabels(plot_data.yticks) if color_data.needs_cbar: cbar = fig.colorbar(artist) cbar.set_label(color_data.label) return if legend and color_data.names is not None and len(color_data.names) <= 20: # using trick from http://stackoverflow.com/a/19881647/10601 num_colors = len(color_data.names) if cmap == '_auto': colors = islice(cycle(COLOR_CYCLE), num_colors) else: colors = artist.cmap(np.linspace(0, 1, num_colors)) proxies = [Patch(color=c, label=k) for c,k in zip(colors, color_data.names)] # Make a new skinny subplot, then cover it with the legend. # This allows an out-of-axis legend without it getting cut off. # Uses the trick from http://stackoverflow.com/a/22885327/10601 gs = GridSpec(1, 2, width_ratios=(3, 1), wspace=0.02) ax.set_position(gs[0].get_position(fig)) ax.set_subplotspec(gs[0]) lx = fig.add_subplot(gs[1]) lx.legend(handles=proxies, title=color_data.label, loc='upper left', mode='expand', borderaxespad=0, fontsize='small', frameon=False) lx.axis('off') gs.tight_layout(fig, w_pad=0)
def plot_completeness( physgrid_list, noise_model_list, output_plot_filename, param_list=["Av", "Rv", "logA", "f_A", "M_ini", "Z", "distance"], compl_filter="F475W", ): """ Make visualization of the completeness Parameters ---------- physgrid_list : string or list of strings Name of the physics model file. If there are multiple physics model grids (i.e., if there are subgrids), list them all here. noise_model_list : string or list of strings Name of the noise model file. If there are multiple files for physgrid_list (because of subgrids), list the noise model file associated with each physics model file. param_list : list of strings names of the parameters to plot compl_filter : str filter to use for completeness (required for toothpick model) output_plot_filename : string name of the file in which to save the output plot """ n_params = len(param_list) # If there are subgrids, we can't read them all into memory. Therefore, # we'll go through each one and just grab the relevant parts. compl_table_list = [] # make a table for each physics model + noise model for physgrid, noise_model in zip(np.atleast_1d(physgrid_list), np.atleast_1d(noise_model_list)): # get the physics model grid - includes priors modelsedgrid = SEDGrid(str(physgrid)) # get list of filters short_filters = [ filter.split(sep="_")[-1].upper() for filter in modelsedgrid.filters ] if compl_filter.upper() not in short_filters: raise ValueError("requested completeness filter not present") filter_k = short_filters.index(compl_filter.upper()) print("Completeness from {0}".format(modelsedgrid.filters[filter_k])) # read in the noise model noisegrid = noisemodel.get_noisemodelcat(str(noise_model)) # get the completeness model_compl = noisegrid["completeness"] # close the file to save memory noisegrid.close() # put it all into a table table_dict = {x: modelsedgrid[x] for x in param_list} table_dict["compl"] = model_compl[:, filter_k] # append to the list compl_table_list.append(Table(table_dict)) # stack all the tables into one compl_table = vstack(compl_table_list) # import pdb; pdb.set_trace() # figure fig = plt.figure(figsize=(4 * n_params, 4 * n_params)) # label font sizes label_font = 25 tick_font = 22 # load in color map cmap = matplotlib.cm.get_cmap("magma") # iterate through the panels for i, pi in enumerate(param_list): for j, pj in enumerate(param_list[i:], i): print("plotting {0} and {1}".format(pi, pj)) # not along diagonal if i != j: # set up subplot plt.subplot(n_params, n_params, i + j * (n_params) + 1) ax = plt.gca() # create image and labels x_col, x_bins, x_label = setup_axis(compl_table, pi) y_col, y_bins, y_label = setup_axis(compl_table, pj) compl_image, _, _, _ = binned_statistic_2d( x_col, y_col, compl_table["compl"], statistic="mean", bins=(x_bins, y_bins), ) # plot points im = plt.imshow( compl_image.T, # np.random.random((4,4)), extent=( np.min(x_bins), np.max(x_bins), np.min(y_bins), np.max(y_bins), ), cmap="magma", vmin=0, vmax=1, aspect="auto", origin="lower", ) ax.tick_params( axis="both", which="both", direction="in", labelsize=tick_font, bottom=True, top=True, left=True, right=True, ) # axis labels and ticks if i == 0: ax.set_ylabel(y_label, fontsize=label_font) # ax.get_yaxis().set_label_coords(-0.35,0.5) else: ax.set_yticklabels([]) if j == n_params - 1: ax.set_xlabel(x_label, fontsize=label_font) plt.xticks(rotation=-45) else: ax.set_xticklabels([]) # along diagonal if i == j: # set up subplot plt.subplot(n_params, n_params, i + j * (n_params) + 1) ax = plt.gca() # create histogram and labels x_col, x_bins, x_label = setup_axis(compl_table, pi) compl_hist, _, _ = binned_statistic( x_col, compl_table["compl"], statistic="mean", bins=x_bins, ) # make histogram _, _, patches = plt.hist(x_bins[:-1], x_bins, weights=compl_hist) # color each bar by its completeness for c, comp in enumerate(compl_hist): patches[c].set_color(cmap(comp)) patches[c].set_linewidth = 0.1 # make a black outline so it stands out as a histogram plt.hist(x_bins[:-1], x_bins, weights=compl_hist, histtype="step", color="k") # axis ranges plt.xlim(np.min(x_bins), np.max(x_bins)) plt.ylim(0, 1.05) ax.tick_params(axis="y", which="both", length=0, labelsize=tick_font) ax.tick_params(axis="x", which="both", direction="in", labelsize=tick_font) # axis labels and ticks ax.set_yticklabels([]) if i < n_params - 1: ax.set_xticklabels([]) if i == n_params - 1: ax.set_xlabel(x_label, fontsize=label_font) plt.xticks(rotation=-45) # plt.subplots_adjust(wspace=0.05, hspace=0.05) plt.tight_layout() # add a colorbar gs = GridSpec(nrows=20, ncols=n_params) cax = fig.add_subplot(gs[0, 2:]) cbar = plt.colorbar(im, cax=cax, orientation="horizontal") cbar.set_label("Completeness", fontsize=label_font) cbar.ax.tick_params(labelsize=tick_font) gs.tight_layout(fig) fig.savefig(output_plot_filename) plt.close(fig)
ax10.set_xlabel(' ') ax10.set_ylabel(' ') ax10.set_xlim(axlims) ax10.set_ylim(axlims) # ax10.set_title(' ') ax10.patch.set_facecolor('black') for comp_map, ax in zip(comp_maps_multi[1:], [ax11, ax12, ax13, ax14]): try: comp_map.plot(ax, title=' ') except: ValueError ax.set_xlabel(' ') ax.set_ylabel(' ') ax.set_xlim(axlims) ax.set_ylim(axlims) # ax.set_title(' ') ax.patch.set_facecolor('black') for ax, title in zip( [ax00, ax01, ax02, ax03, ax04], ["Briggs -2", "Briggs -1", "Briggs 0", "Briggs 1", "Briggs 2"]): ax.set_title(title) ax00.set_ylabel("No Multiscale (Solar-Y)") ax10.set_ylabel("Multiscale (Solar-Y)") ax10.set_xlabel("Solar-X (arcsec)") gs.tight_layout(fig, h_pad=-2, w_pad=-2) plt.savefig("briggs_comparison.png", dpi=400) plt.show()
norm=MidpointLogNorm(vmin=vmin, vmax=vmax, midpoint=1), edgecolors='face', cmap='bwr') ax[i, j].set_xlim(x[0], x[-1]) ax[i, j].set_ylim(y[0], y[-1]) if i != nrows - 1: ax[i, j].xaxis.set_ticklabels([]) if j == 1: ax[i, j].yaxis.set_ticklabels([]) ax[0, 0].xaxis.set_label_position("top") ax[0, 1].xaxis.set_label_position("top") ax[0, 1].yaxis.set_label_position("right") ax[1, 1].yaxis.set_label_position("right") ax[0, 0].set_xlabel('Windowing') ax[0, 1].set_xlabel('Binning') ax[1, 0].set_xlabel('Observed Frequency [MHz]', labelpad=10) ax[1, 0].xaxis.set_label_coords(1, -0.1) ax[0, 1].set_ylabel('Skewness ($S_3$)') ax[1, 1].set_ylabel('Kurtosis ($S_4$)') ax[1, 0].set_ylabel('Window/Bin Size [MHz]', labelpad=10) ax[1, 0].yaxis.set_label_coords(-0.1, 1) cbar = fig.colorbar(im, cax=cax, orientation='vertical', label='SNR') fig.suptitle(t.upper(), x=0.45) gs0.tight_layout(fig, rect=[0, 0, 1, 1]) fig.canvas.draw() fig.savefig(stats_dir + 'snr_color_chart_{:s}.pdf'.format(t), dpi=200)
def poincareplot(self, steps, extralabel=lambda step: '', lims=None, colorbar=True): """ Make a set of Poincare plots for a given step of sequence thereof. Parameters ---------- steps : sequence of int or a single integer The step(s) to plot extralabel : callable, optional A callable with signature `extralabel(step)` that returns an extra string label to add to the title for a given step number lims : dict, optional A dictionary of limits for the Poincaré plots, with keys `'xy.h', 'xy.v', 's.h', 's.v'`, and values corresponding to the magnitude of the (symmetric) axis limits on the respective plots. The transverse plots share limits to ensure that they are always comparable, and because we expect roughly axisymmetric beams. colorbar : bool, optional Boolean indicating whether or not to draw colorbars Returns ------- poincarefig : Figure """ try: iter(steps) except TypeError: steps = [steps] dx, xp, dy, yp, ds, dps = self.frenet_6D() hvar_x = 1e3 * dx[steps, :] vvar_x = 1e3 * xp[steps, :] hvar_y = 1e3 * dy[steps, :] vvar_y = 1e3 * yp[steps, :] hvar_s = 1e3 * ds[steps, :] vvar_s = dps[steps, :] # sxy_h = max(np.nanstd(hvar_x, axis=1).max(), np.nanstd(hvar_y, axis=1).max()) # sxy_v = max(np.nanstd(vvar_x, axis=1).max(), np.nanstd(vvar_y, axis=1).max()) # ss_h = np.nanstd(hvar_s, axis=1).max() # ss_v = np.nanstd(vvar_s, axis=1).max() sxy_h = max( np.nanstd(hvar_x, axis=1).mean(), np.nanstd(hvar_y, axis=1).mean()) sxy_v = max( np.nanstd(vvar_x, axis=1).mean(), np.nanstd(vvar_y, axis=1).mean()) ss_h = np.nanstd(hvar_s, axis=1).mean() ss_v = np.nanstd(vvar_s, axis=1).mean() _lims = { 'xy.h': 3 * sxy_h, 'xy.v': 3 * sxy_v, 's.h': 3 * ss_h, 's.v': 3 * ss_v, } if lims: _lims.update(lims) N = len(steps) if colorbar: Ncols = 9 else: Ncols = 3 w_hb = 5 h_hb = 6 w_cb = 1 cb_pad = 1 w_fig = 3 * w_hb + int(Ncols > 3) * (w_cb + cb_pad) h_fig = N * h_hb if colorbar: widths = [8, 1, 3, 8, 1, 3, 8, 1, 3] else: widths = [1, 1, 1] poincarefig = plt.figure(figsize=(w_fig, h_fig), facecolor='white') gs = GridSpec(nrows=N, ncols=Ncols, hspace=0.2, width_ratios=widths, figure=poincarefig) for row, step in enumerate(steps): hsl = slice(row * h_hb, (row + 1) * h_hb) if colorbar: ax_x = plt.subplot(gs[row, 0]) cax_x = plt.subplot(gs[row, 1]) ax_y = plt.subplot(gs[row, 3]) cax_y = plt.subplot(gs[row, 4]) ax_s = plt.subplot(gs[row, 6]) cax_s = plt.subplot(gs[row, 7]) else: _w = w_fig // 3 ax_x = plt.subplot(gs[row, 0]) ax_y = plt.subplot(gs[row, 1]) ax_s = plt.subplot(gs[row, 2]) ax_x.set_xlabel(r"$\Delta x$ (mm)") ax_x.set_ylabel(r"$x'$ (mrad)") hlim, vlim = _lims['xy.h'], _lims['xy.v'] hb_x = ax_x.hexbin(hvar_x[row], vvar_x[row], extent=[-hlim, hlim, -vlim, vlim]) if colorbar: Colorbar(mappable=hb_x, ax=cax_x) ax_y.set_xlabel(r"$\Delta y$ (mm)") ax_y.set_ylabel(r"$y'$ (mrad)") hb_y = ax_y.hexbin(hvar_y[row], vvar_y[row], extent=[-hlim, hlim, -vlim, vlim]) ax_y.set_title(f't={self.tns[step]:.2f} ns {extralabel(step)}') if colorbar: Colorbar(mappable=hb_y, ax=cax_y) ax_s.set_xlabel(r"$\Delta s$ (mm)") ax_s.set_ylabel(r"$\Delta p_s (\beta\gamma)$") hlim, vlim = _lims['s.h'], _lims['s.v'] hb_s = ax_s.hexbin(hvar_s[row], vvar_s[row], extent=[-hlim, hlim, -vlim, vlim]) if colorbar: Colorbar( mappable=hb_s, ax=cax_s, ) if not colorbar: gs.tight_layout(poincarefig) return poincarefig
def run(self, show=False): # Load data filename = self.filename d = pyfits.getdata(filename) h = pyfits.getheader(filename) path, filename = os.path.split(filename) # Get wavelength calibration z = np.arange(h['naxis3']) w = h['CRVAL3'] + h['CDELT3'] * (z - h['CRPIX3']) # Signal-to-noise clipping s = d.sum(axis=2) s = s.sum(axis=1) gauss_pw, _ = self.fit_gaussian(z, s) log.debug("Gaussian parameters ---") log.debug("p[0] = %.2f" % gauss_pw[0]) log.debug("p[1] = %.2f" % gauss_pw[1]) log.debug("p[2] = %.2f" % gauss_pw[2]) log.debug("p[3] = %.2f" % gauss_pw[3]) lor_p = self.fit_lorentzian(z, s) log.debug("Lorentzian parameters ---") log.debug("p[0] = %.2f" % lor_p[0]) log.debug("p[1] = %.2f" % lor_p[1]) log.debug("p[2] = %.2f" % lor_p[2]) log.debug("p[3] = %.2f" % lor_p[3]) fwhm = np.abs(gauss_pw[2] * 2 * np.sqrt(2 * np.log(2))) filter_ = np.where(np.abs(z - gauss_pw[1]) < fwhm, True, False) if show: plt.plot(z, self.gaussian(gauss_pw, z), 'r-', lw=2, label='Gaussian Fit') plt.plot(z, self.lorentzian(lor_p, z), 'b-', lw=2, label='Lorentzian Fit') plt.plot(z, s, 'ko') plt.plot(z[filter_], s[filter_], 'ro') plt.title('Cube collapsed in XY and fits.') plt.grid() plt.legend(loc='best') plt.gcf().canvas.mpl_connect('key_press_event', self.on_key_press) plt.show() signal = d[filter_].mean(axis=0) noise = d[np.logical_not(filter_)].mean(axis=0) target_snr = 3 snr = signal / noise snr = ndimage.median_filter(snr, 3) snr_mask = np.where(signal > target_snr * noise, True, False) snr_laplacian = ndimage.morphological_laplace(snr * snr_mask, size=3) snr_mask *= np.where(np.abs(snr_laplacian) < 5.0, True, False) snr_mask = ndimage.binary_opening(snr_mask, iterations=5) snr_mask = ndimage.binary_closing(snr_mask, iterations=5) # SNR MASK Based on circular aperture # aperture_radius = 1 # arcmin # aperture_radius = aperture_radius / 60 # arcmin to deg # aperture_radius = np.abs(aperture_radius / h['CD1_1']) # deg to pix # print(aperture_radius) # c = SkyCoord('7:41:55.400', '-18:12:33.00', frame=h['RADECSYS'].lower(), unit=(u.hourangle, u.deg)) # x, y = np.arange(h['NAXIS1']), np.arange(h['NAXIS2']) # X, Y = np.meshgrid(x, y) # center_wcs = wcs.WCS(h) # center = center_wcs.wcs_world2pix(c.ra.deg, c.dec.deg, 6563, 1) # snr_mask = np.sqrt((X - center[0]) ** 2 + (Y - center[1]) ** 2) # snr_mask = np.where(snr_mask < aperture_radius, True, False) # plt.imshow(snr_mask) # plt.show() # SNR MASK Based on squared area aperture_width = 256 * 4.048e-1 # arcsec (from original image) aperture_width /= 3600 # arcsec to deg aperture_width /= np.abs(h['CD1_1']) # deg to pix c = SkyCoord('7:41:55.197', '-18:12:35.97', frame=h['RADECSYS'].lower(), unit=(u.hourangle, u.deg)) x, y = np.arange(h['NAXIS1']), np.arange(h['NAXIS2']) X, Y = np.meshgrid(x, y) center_wcs = wcs.WCS(h) center = center_wcs.wcs_world2pix(c.ra.deg, c.dec.deg, 6563, 1) print(center, np.abs(X - center[0]), np.abs(Y - center[1]), aperture_width) X = np.where(np.abs(X - center[0]) < aperture_width / 2, True, False) Y = np.where(np.abs(Y - center[1]) < aperture_width / 2, True, False) snr_mask = X * Y plt.imshow(snr_mask) plt.gcf().canvas.mpl_connect('key_press_event', self.on_key_press) plt.show() if show: fig1 = plt.figure(figsize=(20, 5)) plt.title('Signal-to-Noise Ratio') gs = GridSpec(1, 3) ax1 = plt.subplot(gs[0]) ax1.set_title('SNR') im1 = ax1.imshow(snr, cmap='cubehelix', interpolation='nearest', origin='lower', vmin=3, vmax=20) div1 = make_axes_locatable(ax1) cax1 = div1.append_axes("right", size="5%", pad=0.05) cbar1 = plt.colorbar(mappable=im1, cax=cax1, use_gridspec=True, orientation='vertical') ax2 = plt.subplot(gs[1]) ax2.set_title('Mask') im2 = ax2.imshow(np.where(snr_mask, 1, 0), cmap='gray', interpolation='nearest', origin='lower', vmin=0, vmax=1) div2 = make_axes_locatable(ax2) cax2 = div2.append_axes("right", size="5%", pad=0.05) cbar2 = plt.colorbar(mappable=im2, cax=cax2, use_gridspec=True, orientation='vertical') cmap = plt.get_cmap('cubehelix') cmap.set_bad('w', 1.0) ax3 = plt.subplot(gs[2]) ax3.set_title('Masked') im3 = ax3.imshow(np.ma.masked_where(~snr_mask, snr), cmap=cmap, interpolation='nearest', origin='lower', vmin=0) div3 = make_axes_locatable(ax3) cax3 = div3.append_axes("right", size="5%", pad=0.05) cbar3 = plt.colorbar(mappable=im3, cax=cax3, use_gridspec=True, orientation='vertical') plt.gcf().canvas.mpl_connect('key_press_event', self.on_key_press) gs.tight_layout(fig1) plt.show() pyfits.writeto(filename.replace('.', '.SNR.'), snr, h, clobber=True) pyfits.writeto(filename.replace('.', '.SNR_LAPLACIAN.'), snr_laplacian, h, clobber=True) # Adjust continuum continuum = self.fit_continuum(d) # Subtract continuum continuum = np.reshape(continuum, (continuum.size, 1, 1)) continuum = np.repeat(continuum, d.shape[1], axis=1) continuum = np.repeat(continuum, d.shape[2], axis=2) d -= continuum del continuum # Integrate along the planetary nebulae d = d * snr_mask d = d.sum(axis=2) d = d.sum(axis=1) d = d / np.float(h['EXPTIME']) gauss_pw, _ = self.fit_gaussian(w, d) gauss_pc, _ = self.fit_gaussian(z, d) log.info("Gaussian parameters ---") log.info("p[0] = %.4f ADU/s" % gauss_pw[0]) log.info("p[1] = %.4f A = %.4f channels" % (gauss_pw[1], gauss_pc[1])) log.info("p[2] = %.4f A = %.4f channels" % (gauss_pw[2], gauss_pc[2])) log.info("p[3] = %.4f ADU/s" % gauss_pw[3]) # total_flux = (gauss_pc[0] - gauss_pc[3]) * np.sqrt(2 * np.pi) \ # * gauss_pc[2] # log.info("Total flux = (a - d) * sqrt(2pi) * c") # log.info(" %.5E ADU/s" % total_flux) fwhm = np.abs(gauss_pw[2] * 2 * np.sqrt(2 * np.log(2))) filter_ = np.where(np.abs(w - gauss_pw[1]) < fwhm, True, False) # d = d - d[~filter_].mean() if show: plt.plot(w, self.gaussian(gauss_pw, w), 'r-', lw=2, label='Gaussian Fit') # plt.plot(w, self.lorentzian(lor_p, w), 'b-', lw=2, label='Lorentzian Fit') plt.plot(w[~filter_], d[~filter_], 'ko') plt.plot(w[filter_], d[filter_], 'ro') plt.title('Spectral profile of the masked area.') plt.xlabel(u'Wavelenght [$\AA$]') plt.ylabel(u'Integrated Count Level [ADU/s]') plt.grid() plt.legend(loc='best') plt.gcf().canvas.mpl_connect('key_press_event', self.on_key_press) plt.show() integrated_flux = (gauss_pc[0] - gauss_pc[3]) \ * (gauss_pc[2] * np.sqrt(2 * np.pi)) log.info("Total flux: %.4E adu/s" % (integrated_flux)) snr_mask = np.where(snr_mask, 1, 0) pyfits.writeto(os.path.join(path, filename.replace('.fits', '.mask.fits')), snr_mask, h, clobber=True) return
def dustCorrection(self): U = 0.3543 #um G = 0.4770 #um R = 0.6231 #um Rv = 3.1 Cu = self.a(1. / U) + self.b(1. / U) / Rv Cg = self.a(1. / G) + self.b(1. / G) / Rv Cr = self.a(1. / R) + self.b(1. / R) / Rv logging.info("dust correction") mainsequence = np.genfromtxt( "/home/conor/scripts/StarBug/catalogs/mainsequence.cat") mask = (mainsequence[:, 1] < 0.6) * (mainsequence[:, 1] > -0.5) mainsequence = mainsequence[mask] coeffs = np.polyfit(mainsequence[:, 1], mainsequence[:, 2], 4) deriv_coeffs = list( reversed([i * c for i, c in enumerate(reversed(coeffs))]))[:-1] x = np.arange(-0.7, 0.75, 0.001) y = np.polyval(coeffs, x) dy = np.polyval(deriv_coeffs, x) #for s in self.sourcelist: s.mag[:,1] += 0.1 Av = np.linspace(0.0, 2.5, 60) Chivals = np.zeros(Av.shape) #fig = plt.figure() for i, av in enumerate(Av): for s in self.sourcelist: s.construct_colours([2, 0], [0, 1]) if (s.colours[0] < 2 and s.colours[1] > 0.3): s.colours[1] = np.nan s.colours[0] -= ((Cu - Cg) * av) s.colours[1] -= ((Cg - Cr) * av) #schi = ((s.colours[0] - np.polyval(coeffs, s.colours[1]))**2.0)/(s.colourError[0]**2.0 + (np.polyval(deriv_coeffs, s.colours[1])*s.colourError[1])**2.0 ) error = np.sqrt(s.colourError[0]**2.0 + (np.polyval(deriv_coeffs, s.colours[1]) * s.colourError[1])**2.0) schi = chisqd(coeffs, s.colours[1], s.colours[0], error) if (np.isfinite(schi)): Chivals[i] += schi #plt.scatter(s.colours[1], s.colours[0],c='k', s=0.1) #plt.scatter(s.colours[1], np.polyval(coeffs, s.colours[1]), c='k') print(av, Chivals[i], av * (Cu - Cg)) minAv = Av[np.argmin(Chivals)] print(Cu, Cg, Cr) print("E(U,G): %f" % (minAv * (Cu - Cg))) print("E(G,R): %f" % (minAv * (Cg - Cr))) print("E(B-V): %f" % (0.336748 * minAv)) print(minAv) print("Au: %f" % (minAv * Cu)) print("Ar: %f" % (minAv * Cr)) print("Ag: %f" % (minAv * Cg)) avLow = 0 avHigh = 0 for i in range(len(Chivals) - 1): if ((Chivals[i] > (min(Chivals) + 1)) and (Chivals[i + 1] < (min(Chivals) + 1))): avLow = Av[i] if ((Chivals[i] < (min(Chivals) + 1)) and (Chivals[i + 1] > (min(Chivals) + 1))): avHigh = Av[i] chix = [] chiy = [] chiyerr = [] fig = plt.figure(figsize=(6, 8)) gs = GridSpec(5, 3, hspace=0.9) ax = plt.subplot(gs[:3, :]) ax.set_ylim(-1, 3) plt.gca().invert_yaxis() ax2 = plt.subplot(gs[3:, :]) #ax.axvline(-0.526) #ax.axhline(-0.252) ax.scatter(mainsequence[:, 1], mainsequence[:, 2], c='k', s=10) for s in self.sourcelist: s.construct_colours([2, 0], [0, 1]) ax.scatter(s.colours[1], s.colours[0], c='r', marker='*', s=10) s.mag[:, 0] -= (Cg * minAv) s.mag[:, 1] -= (Cr * minAv) #+0.07 s.mag[:, 2] -= (Cu * minAv) s.magerr[:, 0] = np.sqrt(s.magerr[:, 0]**2.0 + (Cg * 0.5 * (avLow + avHigh))**2.0) s.magerr[:, 1] = np.sqrt(s.magerr[:, 1]**2.0 + (Cr * 0.5 * (avLow + avHigh))**2.0) s.magerr[:, 2] = np.sqrt(s.magerr[:, 2]**2.0 + (Cu * 0.5 * (avLow + avHigh))**2.0) s.construct_colours([2, 0], [0, 1]) ax.scatter(s.colours[1], s.colours[0], c='b', marker='*', s=10) #print(get_CHIerr(chix,chiy,chiyerr, coeffs)) ax.plot(x, y) ax.tick_params(direction='in', which='both', right=True, top=True, axis='both') ax2.tick_params(direction='in', which='both', right=True, top=True, axis='both') #plt.axvline(0.3) #plt.axhline(2) ax.set_xlabel("(g-r)") ax.set_ylabel("(u-g)") startx = 0.7 starty = 0 ax.arrow(startx, starty, -((Cg - Cr) * minAv * 0.5), -((Cu - Cg) * minAv * 0.4), head_width=0.02) ax.arrow(startx, starty, 0, -((Cu - Cg) * minAv * 0.4), head_width=0.02) ax.arrow(startx, starty - ((Cu - Cg) * minAv * 0.5), -((Cg - Cr) * minAv * 0.5), 0, head_width=0.02) ax.plot(np.nan, np.nan, marker='*', c='r', label="Reddened") ax.plot(np.nan, np.nan, marker='*', c='b', label="DeReddened") ax.plot(np.nan, np.nan, marker='*', c='k', label="Pleiades") ax.legend(loc=3) ax2.plot(Av, Chivals, c='k', label=r"$Av: %.3f^{+%.3f}_{-%.3f}$" % (minAv, avHigh - minAv, minAv - avLow)) ax2.set_xlabel('Av') ax2.set_ylabel(r'$\chi^2$') ax2.legend() ax2.axvline(avLow, c='r') ax2.axvline(minAv, c='b') ax2.axvline(avHigh, c='g') gs.tight_layout(fig) fig.savefig('out/%s_dust.png' % self.name)
# Axes labels fig.text(0.01, 0.5, ylabels[stat], rotation='vertical', horizontalalignment='left', verticalalignment='center') fig.text(0.5, 0.01, 'Frequency [MHz]', horizontalalignment='center', verticalalignment='bottom') fig.text(0.5, 0.99, 'Ionized Fraction', horizontalalignment='center', verticalalignment='top') # Legend # Legend parameters handlers = [ Line2D([], [], linestyle=':', color='black', linewidth=1), Line2D([], [], linestyle='--', color='black', linewidth=1), Line2D([], [], linestyle='-', color='black', linewidth=1), Patch(color='0.85'), Patch(color='0.7'), Patch(color='0.55') ] labels = ['MWA Phase I Core', 'HERA37', 'HERA331', 'MWA Phase I Core Error', 'HERA37 Error', 'HERA331 Error'] plt.figlegend(handles=handlers, labels=labels, loc=(0.6, 0.8), ncol=1, fontsize='medium') # Tidy up gs.tight_layout(fig, rect=[0.02, 0.02, 0.99, 0.98]) gs.update(wspace=0, hspace=0) fig.canvas.draw() fig.savefig(stats_dir + stat + '_poster.pdf', dpi=200) plt.close()
verticalalignment='top', horizontalalignment='left', transform=pax.transAxes) pax.xaxis.set_major_locator(FixedLocator([-10, -1, 0, 1, 10])) minor_locs = np.hstack( (range(-15, -10), range(-9, -1), np.arange(-0.9, 0, 0.1), np.arange(0.1, 1.0, 0.1), range(2, 10), range(11, 16))) pax.xaxis.set_minor_locator(FixedLocator(minor_locs)) # Label images bbox = dict(boxstyle="round", fc='0.8', alpha=0.8, edgecolor='black') for ax, lb in zip(imax, labels): ax.text(0.03, 0.97, lb, transform=ax.transAxes, horizontalalignment='left', verticalalignment='top', color='black', bbox=bbox, size='small') # Tidy up gs0.tight_layout(fig, rect=[0, -0.01, 1, 0.98]) gs0.update(hspace=0.22) fig.canvas.draw() fig.savefig(pdf_dir + 'mwa128_pdf_maps_xi{:.3f}_{:.3f}MHz.pdf'.format(xi, f), dpi=200) plt.close()
class ConstantViewer(object): """ Class meant to visualize the constants of a log file for the Motion Profiler of Walton Robotics """ def __init__(self, clf, automatically_find_remove_outliers: bool = False, manually_find_remove_outliers: bool = True) -> None: """ :param clf: the model to use to separate the data :param automatically_find_remove_outliers: True if black dots should be placed in the 3d plot to represent the outliers False otherwise """ super().__init__() self.manually_find_remove_outliers = manually_find_remove_outliers self.showing = False self.show_outliers = automatically_find_remove_outliers self.clf = clf self.fig = None self.gs = None self.master_plot = None self.time_power = None self.time_velocity = None self.power_velocity = None self.file_data = None self.headers = None self.new_scaled_features = None self.features = None self.outliers = None self.labels = None self.color_labels = None self.outlier_detector = None self.kV = 1 self.kK = 1 self.kAcc = 1 def show(self): """ Shows the figure """ if not self.showing: self.fig = plt.figure("Scaled 3d data") fig_manager = plt.get_current_fig_manager() fig_manager.window.showMaximized() self.gs = GridSpec(3, 4, self.fig) self.master_plot = self.fig.add_subplot(self.gs[:3, :3], projection='3d') self.time_velocity = self.fig.add_subplot(self.gs[0, -1]) self.time_power = self.fig.add_subplot(self.gs[1, -1]) self.power_velocity = self.fig.add_subplot(self.gs[2, -1]) self.gs.tight_layout(self.fig) self.clear_graphs() plot_subplots( self.new_scaled_features, self.headers, (self.time_velocity, self.time_power, self.power_velocity), self.color_labels) self.plot_3d_plot(self.new_scaled_features, self.headers, self.color_labels) if self.show_outliers: self.master_plot.scatter(self.outliers[:, 0], self.outliers[:, 1], self.outliers[:, 2], c="black") plot_hyperplane(self.outlier_detector, self.master_plot, interval=.04, colors="orange") self.show_constants_graph(self.features, self.file_data, self.labels, c=self.color_labels) self.fig.show() plt.show() self.showing = True def close_all(self): """ Closes the figure """ if self.showing: self.showing = False plt.close(self.fig) def plot_3d_plot(self, features, headers, labels): """ PLots the features in a 3d plot including the hyperplane that separates the data :param features: the features to use to plot in the graph :param headers: the axis titles :param labels: the color of each data point """ self.master_plot.scatter(features[:, 0], features[:, 1], features[:, 2], c=labels) self.master_plot.set_xlabel(headers[0]) self.master_plot.set_ylabel(headers[1]) self.master_plot.set_zlabel(headers[2]) plot_hyperplane(self.clf, self.master_plot, colors='orange') def graph(self, file_data): """ Graphs the features from the log file. Creates a 3D graph with time, average power to motors and velocity as axises. It also decomposes the dimensions into individual 2D graphs. :param file_data: the log file to use to extract the data from """ self.file_data = file_data self.features, self.headers = get_features(file_data) # FIXME make it so that the outliers can be visualized as well self.new_scaled_features, self.features = manipulate_features( self.features, file_data) # features = scaler.inverse_transform(new_scaled_features) if self.show_outliers: self.new_scaled_features, self.outliers, self.outlier_detector = find_and_remove_outliers( self.new_scaled_features) if self.manually_find_remove_outliers: selector = remove_outliers(self.new_scaled_features) self.new_scaled_features = self.new_scaled_features[ selector.indexes] self.features = self.features[selector.indexes] self.labels = self.clf.predict(self.new_scaled_features) self.color_labels = list( map(lambda x: 'r' if x == 0 else 'b', self.labels)) def clear_graphs(self): """ Clears all the axes """ for ax in (self.master_plot, self.time_velocity, self.time_power, self.power_velocity): ax.cla() def show_grid(self): """ Shows the grids for the major ticks in the plot. """ for ax in (self.time_velocity, self.time_power, self.power_velocity): ax.grid(True) def show_constants_graph(self, features, file_data, labels, c=None): """ Creates an addition figure that will display the a graph with the constants on it and also the lines of best fit of the accelerating portion of it, the decelerating portion of it and the average of both of those lines :param features: the features to use to show the graph and find the constants from :param file_data: the whole file data :param labels: the labels to say if a data point is accelerating or not :param c: the color to plot the points :return the constants for the motion profiling kV, kK and kAcc """ if is_straight_line(file_data): easygui.msgbox( "It was determined that the robot was trying to go straight. " "As an ongoing feature the program will be able detect kLag, etc... " "however for the instance this features has not been added") constant_figure = plt.figure("Constants graph") constants_plot = constant_figure.gca() constants_plot.set_xlabel("Velocity") constants_plot.set_ylabel("Average Power") # fig_manager = plt.get_current_fig_manager() # fig_manager.window.showMaximized() constant_figure.canvas.manager.window.showMaximized() x = features[:, 1] y = features[:, 0] constants_plot.scatter(x, y, c=labels if c is None else c) acceleration_mask = labels == visualize.ACCELERATING coef_accelerating, intercept_accelerating = find_linear_best_fit_line( x[acceleration_mask], y[acceleration_mask]) deceleration_mask = labels == visualize.DECELERATING coef_decelerating, intercept_decelerating = find_linear_best_fit_line( x[deceleration_mask], y[deceleration_mask]) x_lim = np.array(constants_plot.get_xlim()) y_lim = np.array(constants_plot.get_ylim()) x, y = get_xy_limited(intercept_accelerating, coef_accelerating, x_lim, y_lim) constants_plot.plot(x, y) x, y = get_xy_limited(intercept_decelerating, coef_decelerating, x_lim, y_lim) constants_plot.plot(x, y) # constants_plot.plot(x_lim, coef_accelerating * x_lim + intercept_accelerating) # constants_plot.plot(x_lim, coef_decelerating * x_lim + intercept_decelerating) average_coef = (coef_accelerating + coef_decelerating) / 2 average_intercept = (intercept_accelerating + intercept_decelerating) / 2 # constants_plot.plot(x_lim, average_coef * x_lim + average_intercept) x, y = get_xy_limited(average_intercept, average_coef, x_lim, y_lim) constants_plot.plot(x, y) acceleration_coefficient = (coef_accelerating - average_coef) acceleration_intercept = (intercept_accelerating - average_intercept) k_acc = ((x.max() + x.min()) / 2) * acceleration_coefficient + acceleration_intercept self.kV = average_coef self.kK = average_intercept self.kAcc = k_acc bbox_props = dict(boxstyle="round,pad=0.3", fc="cyan", ec="b", lw=2) constants_plot.text(x_lim[0], y_lim[1], "kV: {}\nkK: {}\nkAcc: {}".format( average_coef, average_intercept, k_acc), ha="left", va="top", bbox=bbox_props) self.copy_constants() constant_figure.canvas.callbacks.connect('button_press_event', self.handle_mouse_click) return average_coef, average_intercept, k_acc def handle_mouse_click(self, event): if event.dblclick: self.copy_constants() def copy_constants(self): easygui.msgbox( "The constants have been saved into your clipboard. Press Ctrl+V to paste them." ) constants = "public static final double kV = {0};\r\n" \ "public static final double kK = {1};\r\n" \ "public static final double kAcc = {2};\r\n" \ .format(self.kV, self.kK, self.kAcc) pyperclip.copy(constants)
def get_estimator_boundingbox(tiff_file, channel=0, outputdir='.', w0=1, w1=100, h0=10, h1=1000, acut=0.9, aratio_min=2., aratio_max=100., border_pad=5, emin=1.0e-4, debug=False, threshold=None): """ INPUT: * file to a tiff image. * (w0,w1): minimum and maximum width for bounding box in pixels. * (l0,l1): minimum and maximum length for bounding box in pixels. * acut: minimum area/rectangle bounding box ratio. * threshold is a lower threshold (everything below is set to zero). Value must be a float between 0 and 1. 1 is the maximum, eg. 255 or 65535. OUTPUT: * 2D matrix of weights corresponding to the probability that a pixel belongs to a cell. USEFUL DOCUMENTATION: * https://docs.opencv.org/3.4.3/dd/d49/tutorial_py_contour_features.html * https://docs.opencv.org/2.4/modules/imgproc/doc/filtering.html?highlight=blur#blur * https://docs.opencv.org/3.1.0/da/d22/tutorial_py_canny.html * https://docs.opencv.org/3.4/d7/d4d/tutorial_py_thresholding.html * https://docs.opencv.org/3.4/db/d5c/tutorial_py_bg_subtraction.html """ bname = os.path.splitext(os.path.basename(tiff_file))[0] ## read the input tiff_file img = get_tiff2ndarray(tiff_file, channel=channel) #img0 = np.copy(img) # rescale dynamic range linearly (important for OTSU) amin = np.min(img) amax = np.max(img) print "amin = {:.1g} amax = {:.1g}".format(amin, amax) img = (np.float_(img) - amin) / (amax - amin) ## thresholding to binary mask norm8 = float(2**8 - 1) norm16 = float(2**16 - 1) if threshold is None: print "OTSU thresholding" # convert to 8-bit image (Open CV requirement for OTSU) img = np.array(255 * img, np.uint8) img8 = np.copy(img) # OTSU threshold ret, img = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) thres1 = float(ret) / norm8 * (amax - amin) + amin thres8 = np.uint8(thres1 * norm8) thres16 = np.uint16(thres1 * norm16) else: thres1 = threshold thres8 = np.uint8(thres1 * norm8) thres16 = np.uint16(thres1 * norm16) ret = max((thres1 - amin) / (amax - amin), 0) # value in rescaled DNR ret = np.uint8(255 * ret) # uint8 print "thres1 = {:.1g} threshold_rescaled_DNR_uint8 = {:d}".format( thres1, ret) # convert to 8-bit image (Open CV requirement for OTSU) img = np.array(255 * img, np.uint8) img8 = np.copy(img) # input threshold ret1, img = cv2.threshold(img, ret, 255, cv2.THRESH_BINARY) print "thres1 = {:.1g} thres8 = {:d} thres16 = {:d}".format( thres1, thres8, thres16) img_bin = np.copy(img) ## opening/closing operations kernel = np.ones((3, 3), np.uint8) # smoothing kernel img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel) #img_opening = np.copy(img) img = cv2.erode(img, kernel, iterations=1) #img_closing = np.copy(img) img = cv2.dilate(img, kernel, iterations=1) img_morph = np.copy(img) ## find connected components ncomp, labels = cv2.connectedComponents(img) print "Found {:d} objects".format(ncomp) ## compute the bounding box for the identified labels #for n in range(ncomp): height, width = img.shape Y, X = np.mgrid[0:height, 0:width] boundingboxes = [] boundingboxes_upright = [] pointsperbox = [] for n in np.arange(ncomp): idx = (labels == n) # pixels coordinates for the label points = np.transpose([X[idx], Y[idx]]) pointsperbox.append(len(points)) # upright rectangles bb = cv2.boundingRect(points) boundingboxes_upright.append(bb) # rotated rectangles bb = cv2.minAreaRect(points) boundingboxes.append(bb) # estimator matrix ## compute scores scores = [] for n in np.arange(ncomp): bb = boundingboxes[n] bb_upright = boundingboxes_upright[n] area = pointsperbox[n] # get bounding box width and height xymid, wh, angle = bb w, h = wh if w > h: wtp = w w = h h = wtp area_rect = w * h aval = area / area_rect aratio = h / w # print "w={:.1f} h={:.1f} aval={:.2e} aratio={:.1f}".format(w,h,aval,aratio) # computing score score = 1. score *= min(1, np.exp((w - w0))) # penalize w < w0 score *= min(1, np.exp(w1 - w)) # penalize w > w1 score *= min(1, np.exp((h - h0))) # penalize w < w0 score *= min(1, np.exp(h1 - h)) # penalize w > w1 score *= min(1, np.exp(aval - acut)) # penalize area/rect < acut score *= min(1, np.exp(aratio - aratio_min)) # penalize aratio < aratio_min score *= min(1, np.exp(aratio_max - aratio)) # penalize aratio > aratio_max # check that box corners of an upright bounding box are within the image plus some pad x, y, ww, hh = bb_upright x0 = x - border_pad y0 = y - border_pad x1 = x + ww + border_pad y1 = y + hh + border_pad # print "a0 = {:d} b0 = {:d}".format(x,y) # print "a1 = {:d} b1 = {:d}".format(x+ww,y+hh) # print "x0 = {:d} y0 = {:d}".format(x0,y0) # print "x1 = {:d} y1 = {:d}".format(x1,y1) if not (x0 >= 0 and x1 < width and y0 >= 0 and y1 < height): score = 0. # discard small values if score < emin: score = 0. scores.append(score) # estimator matrix eimg = np.zeros(img.shape, dtype=np.float_) for n in np.arange(ncomp): idx = (labels == n) eimg[idx] = scores[n] nz = np.sum(eimg > 0.) ntot = len(np.ravel(eimg)) print "nz = {:d} / {:d} sparcity index = {:.2e}".format( nz, ntot, float(nz) / float(ntot)) efname = bname #efile = os.path.join(outputdir,efname+'.txt') #efile = os.path.join(outputdir,efname+'.pkl') efile = os.path.join(outputdir, efname + '.npz') with open(efile, 'w') as fout: #np.savetxt(fout, eimg) #pkl.dump(eimg,fout) ssp.save_npz(efile, ssp.coo_matrix(eimg), compressed=False) print "{:<20s}{:<s}".format('est. file', efile) if debug: debugdir = os.path.join(outputdir, 'debug') if not os.path.isdir(debugdir): os.makedirs(debugdir) # plots import matplotlib.pyplot as plt import matplotlib.patches from matplotlib.path import Path import matplotlib.collections from matplotlib.gridspec import GridSpec ## rescale dynamic range linearly img_base = np.array(img8, dtype=np.float_) / 255. img_base = (img_base - np.min(img_base)) / (np.max(img_base) - np.min(img_base)) img_base = np.array(img_base * 255, dtype=np.uint8) ncolors = (20 - 1) labels_iterated = np.uint8(labels - np.int_(labels / ncolors) * ncolors) + 1 images = [img_base, img_bin, img_morph, labels_iterated, eimg] titles = [ 'original', 'binary (thres = {:d})'.format(thres8), 'closing/opening', 'bounding box', 'estimator' ] cmaps = ['gray', 'gray', 'gray', 'tab20c', 'viridis'] nfig = len(images) nrow = int(np.ceil(np.sqrt(nfig))) ncol = nfig / nrow if (ncol * nrow < nfig): ncol += 1 fig = plt.figure(num=None, figsize=(ncol * 4, nrow * 3)) gs = GridSpec(nrow, ncol, figure=fig) axes = [] for r in range(nrow): for c in range(ncol): ind = r * ncol + c if not (ind < nfig): break ax = fig.add_subplot(gs[r, c]) axes.append(ax) ax.set_title(titles[ind].upper()) cf = ax.imshow(images[ind], cmap=cmaps[ind]) ax.set_xticks([]), ax.set_yticks([]) if titles[ind] == 'bounding box': # draw bounding boxes rects = [] rects_upright = [] codes = [ Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY ] for n in range(ncomp): bb_upright = boundingboxes_upright[n] bb = boundingboxes[n] # upright rect xlo, ylo, w, h = bb_upright rect = matplotlib.patches.Rectangle((xlo, ylo), width=w, height=h, fill=False) rects_upright.append(rect) # rect verts = cv2.boxPoints(bb) verts = np.concatenate((verts, [verts[0]])) path = Path(verts, codes) rect = matplotlib.patches.PathPatch(path) rects.append(rect) col = matplotlib.collections.PatchCollection( rects_upright, edgecolors='k', facecolors='none', linewidths=0.5) ax.add_collection(col) col = matplotlib.collections.PatchCollection( rects, edgecolors='r', facecolors='none', linewidths=0.5) ax.add_collection(col) if titles[ind] == 'estimator': fig.colorbar(cf, ax=ax) fname = "{}_estimator_debug".format(bname) fileout = os.path.join(debugdir, fname + '.png') gs.tight_layout(fig, w_pad=0) plt.savefig(fileout, dpi=300) print "{:<20s}{:<s}".format('debug file', fileout) plt.close('all') # from agarpad code: end """ """ canny: start #test canny filtering print np.min(img), np.max(img) maxthres=2500 minthres=100 edges = cv2.Canny(img,threshold1=minthres,threshold2=maxthres,apertureSize=5) print np.unique(edges) print "START TEST" import matplotlib.pyplot as plt fig = plt.figure(num=None,figsize=(8,4)) plt.subplot(121) plt.imshow(img, cmap='gray') plt.xticks([]),plt.yticks([]) plt.subplot(122) plt.imshow(edges, cmap='gray') plt.xticks([]),plt.yticks([]) fileout = os.path.join(os.getcwd(),'test_get_estimator_contours.png') fig.tight_layout() plt.savefig(fileout,dpi=300) print "writing: ", fileout plt.close('all') fig = plt.figure(num=None,figsize=(4,4)) hist,hedges = np.histogram(np.ravel(img), bins='auto') plt.bar(hedges[:-1], hist, np.diff(hedges), facecolor='blue', lw=0) fileout = os.path.join(os.getcwd(),'test_get_estimator_contours_histogram.png') fig.tight_layout() plt.savefig(fileout,dpi=300) print "writing: ", fileout plt.close('all') sys.exit() print "END TEST" #test # canny: start """ return os.path.realpath(efile)
def fit_continuum(self, data): """ Use the data and the mask to estimate and fit the continuum levels. :param collapsed_data: data-cube in 3D array. :return: array containing the fitted polynomium. """ collapsed_data = np.mean(data, axis=0) norm = ImageNormalize(vmin=collapsed_data.min(), vmax=collapsed_data.max(), stretch=LogStretch()) fig = plt.figure(figsize=(8, 6)) fig.suptitle('Draw a rectangle using the mouse. \nPress <ENTER> to ' + 'accept it and carry on or "Q" to leave.') gs = GridSpec(3, 3, height_ratios=[1, 12, 3], width_ratios=[1, 10, 1]) ax1 = plt.subplot(gs[4]) im1 = ax1.imshow(collapsed_data, origin='lower', interpolation='nearest', cmap='hot_r', norm=norm) ax1.grid() self.ax2 = plt.subplot(gs[7]) self.ax2.xaxis.set_ticklabels([]) self.ax2.yaxis.set_ticklabels([]) self.RS = MyRectangleSelector(ax1, self.line_select_callback, drawtype='box', useblit=True, button=[1], minspanx=5, minspany=5, spancoords='pixels', rectprops=dict(facecolor='green', edgecolor='green', alpha=0.5, fill=True)) self.RS.set_active(True) self.RS.connect_event('button_press_event', self.on_mouse_click) self.RS.connect_event('button_release_event', lambda e: self.on_mouse_release(e, data)) self.RS.connect_event('key_press_event', self.on_key_press) gs.tight_layout(fig) plt.show() x1 = min(self.x1, self.x2) x2 = max(self.x1, self.x2) y1 = min(self.y1, self.y2) y2 = max(self.y1, self.y2) if x1 == x2: log.warning('x1 and x2 are the same. Using the whole image width.') x1 = 0 x2 = -1 if y1 == y2: log.warning( 'y1 and y2 are the same. Using the whole image height.') y1 = 0 y2 = -1 data = data[:, y1:y2, x1:x2] data = data.mean(axis=1) data = data.mean(axis=1) median, std = np.median(data), np.std(data) c = np.where(np.abs(data - median) < std, True, False) x = np.arange(data.size) p = np.polyfit(x[c], data[c], 3) y = np.polyval(p, x) return y
l1 = dax.plot(x, s, label='Field {:d}'.format(field), zorder=3) l2 = dax.plot(x, s_mean, 'k:', label='Full sky', zorder=3) l3 = dax.fill_between(x, s_mean - s_err, s_mean + s_err, label='Single-field sample variance', color='0.5', alpha=0.5, zorder=2) dax.axhline(0, ls='--', color='k', zorder=0) dax.set_xlim(x[0], x[-1]) dax.spines['top'].set_visible(False) dax.set_xlabel('Observed Frequency [MHz]') dax.set_ylabel('Kurtosis') gs.tight_layout(fig, rect=[0, 0, 0.95, 0.92]) gs.update(hspace=0.1, wspace=0) cbar = fig.colorbar(im, cax=cax) # Draw lines from peaks to spines of images fx1, fy1 = fig.transFigure.inverted().transform( dax.transData.transform([158.435, 3.61])) fx2, fy2 = fig.transFigure.inverted().transform( dax.transData.transform([170.035, 0.025])) m1xl, m1yl = fig.transFigure.inverted().transform( max1.transAxes.transform([0, 0])) m1xr, m1yr = fig.transFigure.inverted().transform( max1.transAxes.transform([1, 0])) m2xl, m2yl = fig.transFigure.inverted().transform( max2.transAxes.transform([0, 0])) m2xr, m2yr = fig.transFigure.inverted().transform(
def run(self, show=False): # Load data filename = self.filename d = pyfits.getdata(filename) h = pyfits.getheader(filename) path, filename = os.path.split(filename) # Get wavelength calibration z = np.arange(h['naxis3']) w = h['CRVAL3'] + h['CDELT3'] * (z - h['CRPIX3']) # Convert wavelength from nm to Angstrom # if w.all() < 1000.: # w *= 10 # Signal-to-noise clipping s = d.sum(axis=2) s = s.sum(axis=1) gauss_p, _ = self.fit_gaussian(z, s) log.debug("Gaussian parameters ---") log.debug("p[0] = %.2f" % gauss_p[0]) log.debug("p[1] = %.2f" % gauss_p[1]) log.debug("p[2] = %.2f" % gauss_p[2]) log.debug("p[3] = %.2f" % gauss_p[3]) lor_p = self.fit_lorentzian(z, s) log.debug("Lorentzian parameters ---") log.debug("p[0] = %.2f" % lor_p[0]) log.debug("p[1] = %.2f" % lor_p[1]) log.debug("p[2] = %.2f" % lor_p[2]) log.debug("p[3] = %.2f" % lor_p[3]) fwhm = np.abs(gauss_p[2] * 2 * np.sqrt(2 * np.log(2))) filter_ = np.where(np.abs(z - gauss_p[1]) < fwhm, True, False) if show: plt.plot(z, self.gaussian(gauss_p, z), 'r-', lw=2, label='Gaussian Fit') plt.plot(z, self.lorentzian(lor_p, z), 'b-', lw=2, label='Lorentzian Fit') plt.plot(z, s, 'ko') plt.plot(z[filter_], s[filter_], 'ro') plt.title('Cube collapsed in XY and fits.') plt.grid() plt.legend(loc='best') plt.gcf().canvas.mpl_connect('key_press_event', self.on_key_press) plt.show() signal = d[filter_].mean(axis=0) noise = d[np.logical_not(filter_)].mean(axis=0) snr = signal / noise snr = ndimage.median_filter(snr, 3) snr_mask = np.where(snr > 2, True, False) snr_laplacian = ndimage.morphological_laplace(snr * snr_mask, size=3) snr_mask *= np.where(np.abs(snr_laplacian) < 5.0, True, False) snr_mask = ndimage.binary_opening(snr_mask, iterations=5) snr_mask = ndimage.binary_closing(snr_mask, iterations=5) if show: fig1 = plt.figure(figsize=(20, 5)) plt.title('Signal-to-Noise Ratio') gs = GridSpec(1, 3) ax1 = plt.subplot(gs[0]) ax1.set_title('SNR') im1 = ax1.imshow(snr, cmap='cubehelix', interpolation='nearest', origin='lower', vmin=0) div1 = make_axes_locatable(ax1) cax1 = div1.append_axes("right", size="5%", pad=0.05) cbar1 = plt.colorbar(mappable=im1, cax=cax1, use_gridspec=True, orientation='vertical') ax2 = plt.subplot(gs[1]) ax2.set_title('Mask') im2 = ax2.imshow(np.where(snr_mask, 1, 0), cmap='gray', interpolation='nearest', origin='lower', vmin=0, vmax=1) div2 = make_axes_locatable(ax2) cax2 = div2.append_axes("right", size="5%", pad=0.05) cbar2 = plt.colorbar(mappable=im2, cax=cax2, use_gridspec=True, orientation='vertical') cmap = plt.get_cmap('cubehelix') cmap.set_bad('w', 1.0) ax3 = plt.subplot(gs[2]) ax3.set_title('Masked') im3 = ax3.imshow(np.ma.masked_where(~snr_mask, snr), cmap=cmap, interpolation='nearest', origin='lower', vmin=0) div3 = make_axes_locatable(ax3) cax3 = div3.append_axes("right", size="5%", pad=0.05) cbar3 = plt.colorbar(mappable=im3, cax=cax3, use_gridspec=True, orientation='vertical') plt.gcf().canvas.mpl_connect('key_press_event', self.on_key_press) gs.tight_layout(fig1) plt.show() pyfits.writeto(filename.replace('.','.SNR.'), snr, h, clobber=True) pyfits.writeto(filename.replace('.','.SNR_LAPLACIAN.'), snr_laplacian, h, clobber=True) # Adjust continuum continuum = self.fit_continuum(d) # Subtract continuum continuum = np.reshape(continuum, (continuum.size, 1, 1)) continuum = np.repeat(continuum, d.shape[1], axis=1) continuum = np.repeat(continuum, d.shape[2], axis=2) d -= continuum del continuum # Save the data-cube with the continuum subtracted pyfits.writeto(os.path.join(path, filename.replace('.fits', '.no_cont.fits')), d, h, clobber=True) # Adjust each pixel to a gaussian flux = np.zeros_like(snr) - np.inf velocity = np.zeros_like(snr) - np.inf width = np.zeros_like(snr) - np.inf continuum = np.zeros_like(snr) - np.inf niter = np.zeros_like(snr) - np.inf fit_gauss_cube = np.zeros_like(d) for (j, i) in itertools.product(range(d.shape[1]), range(d.shape[2])): if snr_mask[j, i]: p, s = self.fit_gaussian(w, d[:,j,i]) else: p, s = [-np.inf, -np.inf, -np.inf, -np.inf], -1 flux[j, i] = p[0] velocity[j, i] = p[1] width[j, i] = p[2] continuum[j, i] = p[3] niter[j,i] = s fit_gauss_cube[:, j, i] = self.gaussian(p, w) log.debug("%04d %04d" % (j, i)) mask = np.ma.masked_equal(snr_mask, False) pyfits.writeto(os.path.join(path, filename.replace('.fits', '.GAUSS.fits')), fit_gauss_cube, h, clobber=True) if show: fig1 = plt.figure(figsize=(16,6)) plt.title('Final Results') gs = GridSpec(2, 3) magma = plt.get_cmap('magma') magma.set_bad('w', 1.0) ax1 = plt.subplot(gs[0]) ax1.set_title('Collapsed cube') im1 = ax1.imshow(np.ma.masked_where(~snr_mask, d.sum(axis=0)), cmap=magma, interpolation='nearest', origin='lower') divider1 = make_axes_locatable(ax1) cax1 = divider1.append_axes("right", size="5%", pad=0.05) cbar1 = plt.colorbar(mappable=im1, cax=cax1, use_gridspec=True, orientation='vertical') viridis = plt.get_cmap('viridis') viridis.set_bad('w', 1.0) ax2 = plt.subplot(gs[1]) ax2.set_title('Fit n-iteractions') im2 = ax2.imshow(np.ma.masked_where(~snr_mask, niter), cmap=viridis, interpolation='nearest', origin='lower') divider2 = make_axes_locatable(ax2) cax2 = divider2.append_axes("right", size="5%", pad=0.05) cbar2 = plt.colorbar(mappable=im2, cax=cax2, use_gridspec=True, orientation='vertical') ax3 = plt.subplot(gs[3]) ax3.set_title('Flux') divider3 = make_axes_locatable(ax3) cax3 = divider3.append_axes("right", size="5%", pad=0.05) im3 = ax3.imshow(flux * mask, cmap=viridis, interpolation='nearest', origin='lower') cbar3 = plt.colorbar(mappable=im3, cax=cax3, use_gridspec=True, orientation='vertical') RdYlBu = plt.get_cmap('RdYlBu') RdYlBu.set_bad('w', 1.0) ax4 = plt.subplot(gs[4]) ax4.set_title('Center') vmin = np.median(velocity * mask) - 2 * np.std(velocity * mask) vmax = np.median(velocity * mask) + 2 * np.std(velocity * mask) im4 = ax4.imshow(velocity * mask, cmap=RdYlBu, interpolation='nearest', origin='lower', vmin=vmin, vmax=vmax) divider4 = make_axes_locatable(ax4) cax4 = divider4.append_axes("right", size="5%", pad=0.05) cbar4 = plt.colorbar(mappable=im4, cax=cax4, use_gridspec=True, orientation='vertical') ax5 = plt.subplot(gs[5]) ax5.set_title('Width') vmin = np.median(width * mask) - 2 * np.std(width * mask) vmax = np.median(width * mask) + 2 * np.std(width * mask) im5 = ax5.imshow(width * mask, cmap=viridis, interpolation='nearest', origin='lower') divider5 = make_axes_locatable(ax5) cax5 = divider5.append_axes("right", size="5%", pad=0.05) cbar5 = plt.colorbar(mappable=im5, cax=cax5, use_gridspec=True, orientation='vertical') plt.gcf().canvas.mpl_connect('key_press_event', self.on_key_press) plt.tight_layout() plt.show() del h['CRVAL3'] del h['CDELT3'] del h['CRPIX3'] del h['CTYPE3'] del h['CUNIT3'] del h['C3_3'] del h['CD3_3'] pyfits.writeto( os.path.join(path, filename.replace('.fits', '.flux.fits')), flux, h, clobber=True) pyfits.writeto( os.path.join(path, filename.replace('.fits', '.velo.fits')), velocity, h, clobber=True) pyfits.writeto( os.path.join(path, filename.replace('.fits', '.width.fits')), width, h, clobber=True) pyfits.writeto( os.path.join(path, filename.replace('.fits', '.cont.fits')), continuum, h, clobber=True) return
def setup_settings(self): setting_fig = plt.figure("Settings") grid = GridSpec(12, 3, setting_fig) textbox_ax = plt.subplot(grid[0, 1:]) self.create_text_box(textbox_ax, "Min Ingot Width", lambda: self.width[0], self.set_min_width) textbox_ax = plt.subplot(grid[1, 1:]) self.create_text_box(textbox_ax, "Max Ingot Width", lambda: self.width[1], self.set_max_width) textbox_ax = plt.subplot(grid[2, 1:]) self.create_text_box(textbox_ax, "Min Ingot Height", lambda: self.height[0], self.set_min_height) textbox_ax = plt.subplot(grid[3, 1:]) self.create_text_box(textbox_ax, "Max Ingot Height", lambda: self.height[1], self.set_max_height) textbox_ax = plt.subplot(grid[4, 1:]) self.create_text_box(textbox_ax, "Number of shelves", lambda: self.number_of_shelves, self.set_number_of_shelves, data_type=int) textbox_ax = plt.subplot(grid[5, 1:]) self.create_text_box(textbox_ax, "Shelve width", lambda: self.shelve_spec[0], self.set_shelve_width) textbox_ax = plt.subplot(grid[6, 1:]) self.create_text_box(textbox_ax, "Shelve height", lambda: self.shelve_spec[1], self.set_shelve_height) textbox_ax = plt.subplot(grid[7, 1:]) self.create_text_box(textbox_ax, "Number of ingots", lambda: self.number_of_ingot, self.set_number_of_ingots, data_type=int) textbox_ax = plt.subplot(grid[8, 1:]) self.create_text_box(textbox_ax, "Air Gap Width", lambda: self.air_gap_width, self.set_air_gap_width) valid_algorithms = list(Viewer.valid_stacking_algorithms.keys()) stacking_algorithm_ax = plt.subplot(grid[9, :]) radio_buttons = MyRadioButtons(stacking_algorithm_ax, valid_algorithms, orientation="horizontal") index = valid_algorithms.index(self.stacking_algorithm) radio_buttons.set_active(index) radio_buttons.on_clicked(self.set_stacking_algorithm) self.setting_widgets.append(radio_buttons) # TODO fix checkbox scaling show_animation_ax = plt.subplot(grid[10, 1]) check = CheckButtons(show_animation_ax, ['Show Animation'], [self.show_animation]) check.on_clicked(self.set_show_animation) self.setting_widgets.append(check) random_ax = plt.subplot(grid[-1, 0]) random_button = Button(random_ax, "Randomize") random_button.on_clicked(self.randomize) self.setting_widgets.append(random_button) apply_ax = plt.subplot(grid[-1, 1]) apply_button = Button(apply_ax, "Apply/Reset") apply_button.on_clicked(self.apply) self.setting_widgets.append(apply_button) run_ax = plt.subplot(grid[-1, 2]) run_button = Button(run_ax, "Run") run_button.on_clicked(self.run) self.setting_widgets.append(run_button) grid.tight_layout(setting_fig, h_pad=.1)
def plot_indiv_pdfs(pdf1d_file, pdf2d_file, starnum): """ Make a triangle/corner plot with the 1D and 2D PDFs of a star Parameters ---------- pdf1d_file : str path+file for the BEAST 1D PDFs pdf2d_file : list of strings path+file for the BEAST 2D PDFs starnum : int the index of the star to plot """ with fits.open(pdf2d_file) as hdu_2d, fits.open(pdf1d_file) as hdu_1d: # start with the 2D PDF file to figure out which parameters to plot # list of extension names # - skip the extension named "PRIMARY" # - skip any extension with a dimension of 1 (means that param wasn't fit) ext_list = [ hdu_2d[i].name for i in range(len(hdu_2d)) if ("PRIMARY" not in hdu_2d[i].name) and ( 1 not in hdu_2d[i].data.shape) ] # grab the parameter names param_list_temp = [i for x in ext_list for i in x.split("+")] # put them in the order we want _plot_order = ["M_ini", "logA", "distance", "Z", "Av", "Rv", "f_A"] param_list = [p for p in _plot_order if p in param_list_temp] # if there are parameters not in the predetermined plot order, append them for p in param_list_temp: if p not in param_list: param_list.append(p) # total number of parameters n_params = len(param_list) # figure fig = plt.figure(figsize=(4 * n_params, 4 * n_params)) # label font sizes label_font = 25 tick_font = 22 # iterate through the panels for i, pi in enumerate(param_list): for j, pj in enumerate(param_list[i:], i): # print('plotting {0} and {1}'.format(pi, pj)) # not along diagonal if i != j: # set up subplot plt.subplot(n_params, n_params, i + j * (n_params) + 1) ax = plt.gca() # find the 2D PDF and make sure it's properly rotated try: image = hdu_2d[pi + "+" + pj].data[starnum, :, :].T except KeyError: image = hdu_2d[pj + "+" + pi].data[starnum, :, :] except Exception: raise # create axis/labels x_bins, x_label = setup_axis(pi, hdu_1d) y_bins, y_label = setup_axis(pj, hdu_1d) # plot 2D PDF image im = plt.imshow( np.log(image / np.max(image)), extent=( np.min(x_bins), np.max(x_bins), np.min(y_bins), np.max(y_bins), ), cmap="magma", vmin=-10, vmax=0, aspect="auto", origin="lower", ) # attempt to plot 1/2/3 sigma contours # (doesn't work if the probability is super concentrated, # which is generally due to the grid being really coarse) try: im_sort = np.sort(image, axis=None)[::-1] cumsum = np.cumsum(im_sort) cumsum /= np.max(cumsum) clevels = [ np.log(im_sort[np.where(cumsum <= p)[0][-1]]) for p in [0.68, 0.95, 0.997] ] plt.contour( x_bins, y_bins, image, levels=clevels, colors="k", linestyles="-", ) except IndexError: # print(" can't make contours for this") pass except ValueError: pass except Exception: raise ax.tick_params( axis="both", which="both", direction="in", labelsize=tick_font, bottom=True, top=True, left=True, right=True, ) # axis labels and ticks if i == 0: ax.set_ylabel(y_label, fontsize=label_font) # ax.get_yaxis().set_label_coords(-0.35,0.5) else: ax.set_yticklabels([]) if j == n_params - 1: ax.set_xlabel(x_label, fontsize=label_font) plt.xticks(rotation=-45) else: ax.set_xticklabels([]) # along diagonal if i == j: # set up subplot plt.subplot(n_params, n_params, i + j * (n_params) + 1) ax = plt.gca() # create axis/labels x_bins, x_label = setup_axis(pi, hdu_1d) # make histogram _pdf = hdu_1d[pi].data[starnum, :] plt.plot( x_bins, _pdf / np.max(_pdf), marker="o", mew=0, color="black", markersize=2, linestyle="-", ) # axis ranges plt.xlim(np.min(x_bins), np.max(x_bins)) plt.ylim(0, 1.05) ax.tick_params(axis="y", which="both", length=0, labelsize=tick_font) ax.tick_params(axis="x", which="both", direction="in", labelsize=tick_font) # axis labels and ticks ax.set_yticklabels([]) if i < n_params - 1: ax.set_xticklabels([]) if i == n_params - 1: ax.set_xlabel(x_label, fontsize=label_font) plt.xticks(rotation=-45) # plt.subplots_adjust(wspace=0.05, hspace=0.05) plt.tight_layout() # add a colorbar gs = GridSpec(nrows=20, ncols=n_params) cax = fig.add_subplot(gs[0, 2:]) cbar = plt.colorbar(im, cax=cax, orientation="horizontal") cbar.set_label("Log Likelihood", fontsize=label_font) cbar.ax.tick_params(labelsize=tick_font) gs.tight_layout(fig) fig.savefig( pdf1d_file.replace("pdf1d.fits", "pdfs_starnum_{0}.pdf".format(starnum))) plt.close(fig)
def calculateDistance(self): """ TMP gets distance to ngc from pleiedes """ pleiades = np.genfromtxt( "/home/conor/scripts/StarBug/catalogs/pleiades_sloane") pleiades = pleiades[np.where(pleiades[:, 0] < 15)] pleiades = pleiades[np.where(pleiades[:, 1] < 0.3)] #pleiades = pleiades[np.where( pleiades[:,1] >-0.7)] pli_distance = 134 #pli_distance = 2327#NOT PLI ig = 0 igr = 1 iug = 2 fig = plt.figure() gs = GridSpec(5, 2) ax0 = plt.subplot(gs[0:3, :]) ax0.scatter(pleiades[:, 1], pleiades[:, ig], c='k', marker='*') for s in self.sourcelist: s.construct_colours([2, 0], [0, 1]) if (s.colours[1] > 0.25 or s.colours[1] < -0.6): s.colours[1] = np.nan ax0.scatter(s.colours[1], s.MAG[0], c='b', marker='*') plt.gca().invert_yaxis() ax2 = plt.subplot(gs[3:, :], sharex=ax0) colourmin = np.nanmin(pleiades[:, 1]) colourmax = np.nanmax(pleiades[:, 1]) sourceG_R = [] sourceG = [] sourcedG = [] for s in self.sourcelist: c = s.colours[1] if (np.isfinite(c)): sourceG_R.append(c) sourceG.append(s.MAG[0]) sourcedG.append(s.MAGERR[0]) if c < colourmin: colourmin = c if c > colourmax: colourmax = c Range = np.linspace(colourmin, colourmax, 50) sourceG = np.array(sourceG) sourcedG = np.array(sourcedG) dm = [] ddm = [] colour = [] for i in range(len(Range[:-1])): c0 = Range[i] c1 = Range[i + 1] pliMask = (pleiades[:, 1] >= c0) * (pleiades[:, 1] < c1) pliMean = np.mean(pleiades[:, ig][pliMask]) catMask = (sourceG_R >= c0) * (sourceG_R < c1) catMean = np.nanmean(sourceG[catMask]) if (np.isfinite(catMean) and np.isfinite(pliMean)): pliErr = np.std(pleiades[:, ig][pliMask]) catErr = np.sqrt( sum([(em / len(sourcedG[catMask]))**2. for em in sourcedG[catMask]])) if (pliErr < 1 and catErr < 1 and Range[i] < 0.17): dm.append(pliMean - catMean) colour.append(Range[i]) ddm.append(np.sqrt(pliErr**2. + catErr**2.)) ax0.scatter(c0, catMean, c='r', marker='x') #ax0.scatter(c0, pliMean, c='r', marker='x') deltaM = np.nanmean(dm) deltaMerr = np.sqrt(np.nansum([(x / len(dm))**2. for x in ddm])) Dn = pli_distance * 10.0**(-deltaM / 5.) dDn = np.sqrt( (((-pli_distance * np.log(10) / 5.) * 10.0**(-deltaM / 5.)) * deltaMerr)**2.0) print(Dn, dDn) print("Distance Modulus: %f" % (-5.0 * np.log10(Dn / 10.0))) ax2.scatter(colour, dm, s=5, c='k') ax2.errorbar(colour, dm, yerr=ddm, linewidth=0, elinewidth=1, c='k') ax0.tick_params(direction='in', labelbottom=False) ax2.tick_params(direction='in') #ax2.axhline(deltaM, c='xkcd:magenta') ax2.axhline(deltaM + deltaMerr, c='xkcd:magenta') ax2.axhline(deltaM - deltaMerr, c='xkcd:magenta') ax2.set_xlabel(r'$(g-r)_0$') ax2.set_ylabel(r'$\Delta$M') ax0.set_ylabel(r'$g_0$') gs.tight_layout(fig) for s in self.sourcelist: s.set_distance(Dn, dDn) s._voidCalcAbsoluteMagnitudes() #plt.show() fig.savefig('out/%s_distance.png' % self.name)
for j in range(ncols): k = i * ncols + j if i == 0 and j == 0: maps_ax[i, j] = fig.add_subplot(gs2[1 + i, j], projection=wcs[k]) else: maps_ax[i, j] = fig.add_subplot(gs2[1 + i, j], sharex=maps_ax[0, 0], sharey=maps_ax[0, 0], projection=wcs[k]) plot_stat(stat_ax) plot_maps(maps_ax) xlocs = [145.115, 148.155, 163.355, 178.555, 187.675, 193.755] ylocs = np.ones_like(xlocs) * 2.0 label_plots(xlocs, ylocs) # fig.text(0.01, (1-1./(nrows+1))*1.2, ylabels[stat], # rotation='vertical', verticalalignment='center') # fig.text(0.5, 0.01, 'Right Ascension [$^{\circ}$]', # horizontalalignment='center') fig.text(0.01, 1. / (nrows + 1), 'Declination [$^{\circ}$]', rotation='vertical', verticalalignment='center') stat_ax.set_ylabel(ylabels[stat]) maps_ax[1, 1].set_xlabel('Right Ascension [$^{\circ}$]') # maps_ax[0, 0].set_ylabel('Declination [$^{\circ}$]') gs0.tight_layout(fig, rect=[-0.02, 0.05, 1, 1]) # plt.tight_layout() # plt.show() plt.savefig('kurt_maps_hera331_bw3MHz.pdf', dpi=200)
def fit_continuum(self, data): """ Use the data and the mask to estimate and fit the continuum levels. :param collapsed_data: data-cube in 3D array. :return: array containing the fitted polynomium. """ collapsed_data = np.mean(data, axis=0) norm = ImageNormalize(vmin=collapsed_data.min(), vmax=collapsed_data.max(), stretch=LogStretch()) fig = plt.figure(figsize=(8, 6)) fig.suptitle('Draw a rectangle using the mouse. \nPress <ENTER> to ' + 'accept it and carry on or "Q" to leave.') gs = GridSpec(3, 3, height_ratios=[1, 12, 3], width_ratios=[1, 10, 1]) ax1 = plt.subplot(gs[4]) im1 = ax1.imshow(collapsed_data, origin='lower', interpolation='nearest', cmap='hot_r', norm=norm) ax1.grid() self.ax2 = plt.subplot(gs[7]) self.ax2.xaxis.set_ticklabels([]) self.ax2.yaxis.set_ticklabels([]) self.RS = MyRectangleSelector(ax1, self.line_select_callback, drawtype='box', useblit=True, button=[1], minspanx=5, minspany=5, spancoords='pixels', rectprops = dict(facecolor='green', edgecolor = 'green', alpha=0.5, fill=True)) self.RS.set_active(True) self.RS.connect_event('button_press_event', self.on_mouse_click) self.RS.connect_event('button_release_event', lambda e: self.on_mouse_release(e, data)) self.RS.connect_event('key_press_event', self.on_key_press) gs.tight_layout(fig) plt.show() x1 = min(self.x1, self.x2) x2 = max(self.x1, self.x2) y1 = min(self.y1, self.y2) y2 = max(self.y1, self.y2) if x1 == x2: log.warning('x1 and x2 are the same. Using the whole image width.') x1 = 0 x2 = -1 if y1 == y2: log.warning('y1 and y2 are the same. Using the whole image height.') y1 = 0 y2 = -1 data = data[:, y1:y2, x1:x2] data = data.mean(axis=1) data = data.mean(axis=1) median, std = np.median(data), np.std(data) c = np.where(np.abs(data - median) < std, True, False) x = np.arange(data.size) p = np.polyfit(x[c], data[c], 3) y = np.polyval(p, x) return y
def __init__(self, nodes=50, coupling_network=nx.complete_graph, distribution=np.random.standard_normal, timesteps=200, t_end=None, symmetric=False, interval=25, **osc_params): """ :param nodes: The number of oscillators to simulate (default 50) :param coupling_network: Networkx graph construction function representing the coupling topology (default is nx.complete_graph). The function should take a single argument, the number of nodes, and return a graph. :param distribution: Function to generate the frequency distribution. It should take an single argument, the number of nodes, and return a numpy array containing the frequency :param timesteps: :param t_end: :param symmetric: :param interval: :param osc_params: """ self.t_end = t_end self.n = nodes if symmetric: freq = list(distribution(int(nodes / 2))) freq += [-f for f in freq] if nodes % 2 == 1: freq.append(0) self.omega = np.array(sorted(freq)) else: self.omega = np.array(sorted(distribution(int(nodes)))) self.graph = coupling_network(nodes) if t_end: self.t = np.linspace(0, t_end, timesteps).T self.z = np.zeros((nodes, timesteps), dtype=np.complex) else: self.z = np.zeros((nodes, 1), dtype=np.complex) self.z[:, 0] = np.random.uniform(low=-1, high=1, size=self.z[:, 0].shape) + 1j * np.random.uniform( low=-1, high=1, size=self.z[:, 0].shape) if t_end: self._solve_ode(**osc_params) max_z = max(min(abs(self.z).max(), 5), 1) #max_z = 2 fig = plt.figure() gs = GridSpec(2, 5) self.ax_phase = plt.subplot(gs[0, :3]) self.ax_complex = plt.subplot(gs[0, 3:]) self.ax_r = plt.subplot(gs[1, :]) self._init_phase_axis() self._init_complex_plane(extent=max_z) if not t_end: self.t_max = 50 else: self.t_max = t_end self.t_max_step = 25 self._init_r_axis(t_max=self.t_max, r_max=max_z) gs.tight_layout(fig) self.o_line = Line2D([], [], color='none', marker='.', markerfacecolor='r') self.ax_phase.add_line(self.o_line) self.c_line = Line2D([], [], color='none', marker='.', markerfacecolor='r') self.cz_line = Line2D([], [], color='none', marker='o', markeredgecolor='b') self.ax_complex.add_line(self.c_line) self.ax_complex.add_line(self.cz_line) self.r_line = Line2D([], [], color='red') self.ax_r.add_line(self.r_line) self._sn = 1./np.sqrt(nodes) if t_end: self.rn_line = Line2D([0, t_end],[self._sn, self._sn], linestyle=':') else: self.rn_line = Line2D([0, self.t_max], [self._sn, self._sn], linestyle=':') self._running = False self.ax_r.add_line(self.rn_line) self._gen_args = osc_params self._timestep = 2 * interval / 1000.0 animation.TimedAnimation.__init__(self, fig, interval=interval, blit=True)
def make_evaluation_plots_from_data( data: Sequence[ExperimentResult], exp_name: str, param_labels: Sequence[str], bar_plot_xlabel: str, fig_save_path: Path = Path('../results/plots'), sim_non_cli_opts: Optional[PandemicSimNonCLIOpts] = None, max_hospital_capacities: Optional[List[int]] = None, show_summary_plots: bool = True, show_cumulative_reward: bool = False, show_time_to_peak: bool = True, show_pandemic_duration: bool = True, show_stage_trials: bool = False, annotate_stages: Union[bool, Sequence[bool]] = False, figsize: Optional[Tuple[int, int]] = None): n_params = len(param_labels) os.makedirs(str(fig_save_path.absolute()), exist_ok=True) annotate_stages = [annotate_stages] * n_params if isinstance( annotate_stages, bool) else annotate_stages sim_non_cli_opts = sim_non_cli_opts or PandemicSimNonCLIOpts( small_town_population_params) hp = sim_non_cli_opts.population_params.location_type_to_params[Hospital] max_hospital_capacities = ([hp.num * hp.visitor_capacity] * len(data) if max_hospital_capacities is None else max_hospital_capacities) gis_legend = [summ.value for summ in sorted_infection_summary] sup_title = f"{' '.join([s.capitalize() for s in exp_name.split(sep='_')])}" plot_ref_labels = string.ascii_lowercase + string.ascii_uppercase plot_ref_label_i = 0 gs1: Optional[GridSpec] = None if show_summary_plots: figsize = figsize if figsize is not None else (( 16, 6) if n_params <= 5 else (20, 12)) fig = plt.figure(num=sup_title, figsize=figsize) gs1 = GridSpec(n_params, 3) axs = np.array([fig.add_subplot(sp) for sp in gs1]).reshape(n_params, 3) for i, (exp_result, param_label, max_hospital_capacity, ann_stages) in enumerate( zip(data, param_labels, max_hospital_capacities, annotate_stages)): plot_global_infection_summary(exp_result, ax=axs[i, 0], annotate_stages=ann_stages) if show_stage_trials: seed_indices = np.random.permutation( len(exp_result.obs_trajectories.stage.shape[1]))[:3] for seed_i in seed_indices: axs[i, 2].plot(exp_result.obs_trajectories.stage[:, seed_i, 0]) axs[i, 2].set_title( f'Stages over Time\n(shown for {len(seed_indices)} trials)' ) axs[i, 2].set_yticks( [0, np.max(exp_result.obs_trajectories.stage)]) axs[i, 2].set_yticklabels([ 'Open\n(Stage-0)', f'Lockdown\n(Stage-{int(np.max(exp_result.obs_trajectories.stage))})' ]) axs[i, 2].set_xlabel('time (days)') else: plot_global_infection_summary(exp_result, testing_summary=True, annotate_stages=ann_stages, ax=axs[i, 1]) plot_critical_summary(exp_result, max_hospitals_capacity=max_hospital_capacity, annotate_stages=ann_stages, ax=axs[i, 1] if show_stage_trials else axs[i, 2]) for j, ax in enumerate(axs[i]): ref_label_offset = 20 ax.yaxis.tick_right() ax.yaxis.set_label_position("right") ax.set_ylabel(ax.get_ylabel(), rotation=-90, labelpad=10) if i < (axs.shape[0] - 1): ax.set_xlabel('') ref_label_offset = 0 if j < (axs.shape[1] - 1): ax.set_ylabel('') if i > 0: ax.set_title('') axs[i, j].annotate(f'({plot_ref_labels[plot_ref_label_i]})', (0.5, 0.), xytext=(0, -25 - ref_label_offset), textcoords='offset points', xycoords='axes fraction', ha='center', va='center', size=14) plot_ref_label_i += 1 axs[i, 0].annotate(f'{param_label}', (0, 0.5), xytext=(-15, 0), textcoords='offset points', xycoords='axes fraction', ha='center', va='center', size=14, rotation=90) handles = (axs[0, 0].get_legend_handles_labels()[0] + axs[0, 2].get_legend_handles_labels()[0][-1:]) axs[0, 0].legend(handles, gis_legend + ['Max hospital capacity', 'cumulative_reward'], fancybox=True, loc='best', fontsize=4 if n_params > 3 else 6) with warnings.catch_warnings(): warnings.simplefilter("ignore", UserWarning) # This raises warnings since tight layout cannot # handle gridspec automatically. We are going to # do that manually so we can filter the warning. gs1.tight_layout(fig, rect=[0.01, None, 0.6, None]) else: figsize = figsize if figsize is not None else (10, 3) fig = plt.figure(num=sup_title, figsize=figsize) bar_plots_2d = (3 + int(show_time_to_peak) + int(show_pandemic_duration) + int(show_cumulative_reward)) bar_plots_3d = 0 total_bar_plots = bar_plots_2d + bar_plots_3d n_cols = int(np.round(np.sqrt(total_bar_plots))) n_rows = int(n_cols) if n_cols**2 >= total_bar_plots else n_cols + 1 gs2 = gridspec.GridSpec(n_rows, n_cols) axs = [] plot_i = 0 for sp in gs2: if plot_i < bar_plots_2d: axs.append(fig.add_subplot(sp)) elif plot_i < (bar_plots_2d + bar_plots_3d): axs.append(fig.add_subplot(sp, projection='3d')) plot_i += 1 plot_multi_params_summary( data, param_labels=param_labels, xlabel=bar_plot_xlabel, max_hospitals_capacities=max_hospital_capacities, show_cumulative_reward_plot=show_cumulative_reward, show_time_to_peak=show_time_to_peak, show_pandemic_duration=show_pandemic_duration, axs=axs) for ax in axs: if ax.axison or isinstance(ax, Axes3D): offset = 20 if max([len(label) for label in param_labels]) < 5 else 30 ax.annotate(f'({plot_ref_labels[plot_ref_label_i]})', (0.5, 0.), xytext=(0, -25 - offset), textcoords='offset points', xycoords='axes fraction', ha='center', va='center', size=14) plot_ref_label_i += 1 if gs1: with warnings.catch_warnings(): # This raises warnings since tight layout cannot # handle gridspec automatically. We are going to # do that manually so we can filter the warning. warnings.simplefilter("ignore", UserWarning) gs2.tight_layout(fig, rect=[0.6, None, None, None]) top = min(gs1.top, gs2.top) bottom = max(gs1.bottom, gs2.bottom) gs1.update(top=top, bottom=bottom) gs2.update(top=top, bottom=bottom) plt.annotate(bar_plot_xlabel, (0.01, 0.5), xytext=(0, 0), textcoords='offset points', xycoords='figure fraction', ha='center', va='center', size=16, rotation=90) else: plt.tight_layout() plt.savefig(fig_save_path / (exp_name + '.pdf'))
def mountains_series(): def format_axes(fig): for i, ax in enumerate(fig.axes): ax.tick_params(labelbottom=False, labelleft=False, bottom=False, left=False) fig = plt.figure(constrained_layout=False, frameon=False) ax = fig.add_axes([0, 0, 1, 1]) ax.set_facecolor((0.88, 0.87, 0.9)) gs = GridSpec(3, 3, figure=fig, wspace=0, hspace=0.05) axes = [] axes.append(fig.add_subplot(gs[0, :])) axes.append(fig.add_subplot(gs[1, :-1])) axes.append(fig.add_subplot(gs[1:, -1])) axes.append(fig.add_subplot(gs[-1, 0])) axes.append(fig.add_subplot(gs[-1, -2])) # sizes_y = [15, 10, 5, 5, 5] # sizes_x = [5, 5, 10, 5, 5] sizes_y = [20, 12, 5, 5, 5] sizes_x = [5, 5, 10, 5, 5] for i in range(5): debug = False rand_color = randomcolor.RandomColor() size_x = sizes_x[i] size_y = sizes_y[i] upscale_factor = 10 n_labels = 5 sigma = 1 buffer = 0.005 hsv_index = 1 segment_spacing = [10, 10] image_rgb = np.random.uniform(0, 1, (size_x, size_y, 3)) labels = np.random.randint(n_labels + 1, size=( size_x, size_y)) * np.random.randint(0, 2, size=(size_x, size_y)) # segment random image segments = random_walker( image_rgb, labels, multichannel=True, beta=250, copy=False, spacing=segment_spacing, ) all_colors = np.array( rand_color.generate( hue="purple", # luminosity='bright', count=n_labels, format_='Array_rgb')) / 256. for color_index in np.unique(segments): color_hsv = color.rgb2hsv(all_colors[color_index - 1]) # color_hsv[2] = 0.6 + (color_index - 1) * 0.4 / n_labels color_rgb = color.hsv2rgb(color_hsv) image_rgb[segments == color_index] = color_rgb # transform segmented image so it is large, preserving blobs, and blurry image_rgb = rescale(image_rgb, upscale_factor, anti_aliasing=False, multichannel=True) image_rgb = gaussian(image_rgb, sigma=sigma, multichannel=True) image_hsv = color.rgb2hsv(image_rgb) if debug: plt.figure() plt.imshow(image_rgb) plt.show() for pix_frac in [0.9]: total_pixels_switched = int(image_rgb.shape[0] * image_rgb.shape[0] * pix_frac) print(total_pixels_switched) for _ in range(total_pixels_switched): rand_x, rand_y = np.random.choice( image_hsv.shape[0]), np.random.choice(image_hsv.shape[1]) orig_rgb = image_rgb[rand_x, rand_y] rand_value = image_hsv[rand_x, rand_y, hsv_index] x, y = np.where( (rand_value * (1 + 0.5 * buffer) >= image_hsv[:, :, hsv_index]) & (rand_value * (1 - 2 * buffer) < image_hsv[:, :, hsv_index])) if len(x) == 0: continue idx = np.random.choice(len(x)) update_rgb = image_rgb[x[idx], y[idx]] image_rgb[x[idx], y[idx]] = orig_rgb image_rgb[rand_x, rand_y] = update_rgb if debug: plt.figure() plt.title(pix_frac) plt.imshow(image_rgb) plt.show() if debug: plt.figure() plt.imshow(image_rgb) plt.show() # filtered_img = prewitt_v(color.rgb2hsv(image_rgb)[:, :, 0]) # filtered_img = meijering(color.rgb2hsv(image_rgb)[:, :, 0]) # filtered_img = sato(color.rgb2hsv(image_rgb)[:, :, 0]) # filtered_details = frangi(color.rgb2hsv(image_rgb)[:, :, 0], # sigmas=range(3, 8, 10), # black_ridges=True, # beta=0.1, # ) filtered_img = frangi( color.rgb2hsv(image_rgb)[:, :, 0], sigmas=range(3, 8, 10), black_ridges=True, # beta=0.1, ) from skimage.filters.rank import median from skimage.morphology import disk, ball filtered_img = median(filtered_img / np.max(filtered_img), disk(10)) filtered_img = filtered_img filtered_img = 1.5 * (filtered_img + np.abs( np.min(filtered_img))) / np.max(filtered_img + np.abs(np.min(filtered_img))) filtered_img += 0.6 if debug: plt.figure() plt.imshow(filtered_img, cmap='gray') plt.colorbar() plt.show() num_erosions = 5 eroded_image = hessian(color.rgb2hsv(image_rgb)[:, :, 0]) eroded_aug = np.zeros_like(eroded_image) for n in range(num_erosions): eroded_aug += 1 * eroded_image eroded_image = erosion(eroded_image) if debug: plt.figure() plt.imshow(image_rgb) plt.show() image_hsv = color.rgb2hsv(image_rgb) image_hsv[:, :, 2] *= filtered_img image_hsv[:, :, 2] *= 1 - (0.8 * eroded_aug / np.max(eroded_aug)) image_rgb_shadow_aug = color.hsv2rgb(image_hsv) # plt.figure() # plt.imshow(image_rgb_shadow_aug) # plt.show() axes[i].imshow(image_rgb_shadow_aug) format_axes(fig) gs.tight_layout(fig, pad=1.2, h_pad=0.5) plt.show()
fig = plt.figure(figsize=(8,2.5)) gs1 = GridSpec(1, 4) p1 = fig.add_subplot(gs1[0]) p2 = fig.add_subplot(gs1[1]) p3 = fig.add_subplot(gs1[2]) p4 = fig.add_subplot(gs1[3]) p1.pie([ss, sd, sh], autopct='%1.1f%%', pctdistance=1.4, colors=["lightgrey", "grey", "darkgrey"]) p1.set_title("Structural") p1.axis("equal") p2.pie([bs, bd, bh], autopct='%1.1f%%', pctdistance=1.4, colors=["lightgrey", "grey", "darkgrey"]) p2.set_title("Behavioural") p2.axis("equal") p3.pie([cs, cd, ch], autopct='%1.1f%%', pctdistance=1.4, colors=["lightgrey", "grey", "darkgrey"]) p3.set_title("Creationnal") p3.axis("equal") gs1.tight_layout(fig) p, _ , _=p4.pie([cs, cd, ch], autopct='%1.1f%%', colors=["lightgrey", "grey", "darkgrey"]) p4.axis("equal") p4.set_visible(False) lgd = fig.legend(p, ["Static", "Dynamic", "Hybrid"], loc="right") fig.savefig("../img/analysis_v_pattern.png") #plt.savefig("../img/creationnal.png", bbox_extra_artists=(lgd,), bbox_inches='tight')
def plot_results_year_round_team_color(results, constructors_df, drivers_df, win_type='race', result_type='Race Wins'): fig = plt.figure(figsize=(8.1, 9)) gs = GridSpec(1, 3, width_ratios=[1.3, 0.4, 0.4], wspace=0.1) team_results = results['team'] driver_results = results['driver'] color_names_sq = np.array( ['white'] * (constructors_df.index.max() + 1), dtype='U16' ) color_names_sq[constructors_df.index] = constructors_df['parent'].apply( lambda t: TEAM_COLORS.get(t, TEAM_COLORS['others'])[0] ) # print(color_names_sq[131]) cmap_sq = ListedColormap(color_names_sq) # Plot ax_results = plt.subplot(gs[0]) # ax_results_right = ax_results.twinx() ax_team_legend = plt.subplot(gs[1]) ax_driver_legend = plt.subplot(gs[2]) xlims = (0.5, team_results.shape[1] + 0.5) ylims = (F1_LATEST_YEAR + 0.5, F1_FIRST_YEAR - 0.5) # Races handles_team = [] labels_team = [] handles_driver = [] labels_driver = [] total_races = np.sum(team_results != 0) remaining_races = total_races ax_results.imshow( team_results, cmap=cmap_sq, vmin=0, vmax=constructors_df.index.max(), extent=(*xlims, *ylims) ) team_ref_view = constructors_df.set_index('constructorRef') driver_ref_view = drivers_df.set_index('driverRef') team_n_races = [] for n, team_id in enumerate(TEAM_COLORS): if team_id == 'others': name = 'Others' team_ids = constructors_df[ ~constructors_df['parent'].isin(TEAM_COLORS) ].index else: name = team_ref_view.loc[team_id]['name'] team_ids = constructors_df[constructors_df['parent'] == team_id].index race_wins = np.isin(team_results, team_ids) wins = np.sum(race_wins[:, :MAX_RACES_YEAR]) y, x = np.nonzero(race_wins) x += 1 # shift round # y += 1950 # shift year if wins == 0: continue colors = TEAM_COLORS[team_id] if wins == 1: wins_label = f'{wins} {win_type}' else: wins_label = f'{wins} {win_type}s' label = f'{name} ({wins_label})' team_n_races.append((n, team_id, wins)) h1, = ax_results.plot(np.nan, 's', ms=7, color=colors[0]) h2, = ax_results.plot(x, y, 'o', ms=2, color=colors[1]) handles_team.append((h1, h2)) labels_team.append(label) driver_n_races = [] for n, driver_ref in enumerate(DRIVER_COLORS): name = driver_ref_view.loc[driver_ref]['name'] driver_ids = drivers_df[drivers_df['driverRef'] == driver_ref].index race_wins = np.isin(driver_results, driver_ids) wins = np.sum(race_wins[:, :MAX_RACES_YEAR]) y, x = np.nonzero(race_wins) x += 1 # shift round # y += 1950 # shift year if wins == 0: continue color = DRIVER_COLORS[driver_ref] if wins == 1: wins_label = f'{wins} {win_type}' else: wins_label = f'{wins} {win_type}s' first_year = min(y) last_year = max(y) label = f'{name}\n{first_year}-{last_year}\n({wins_label})' driver_n_races.append((n, driver_ref, wins)) h, = ax_results.plot(x, y, 's', ms=6, color=color, markeredgewidth=1, markerfacecolor='none') handles_driver.append(h) labels_driver.append(label) team_n_races = np.array(team_n_races, dtype=('u4,U16,u4')) driver_n_races = np.array(driver_n_races, dtype=('u4,U16,u4')) order_team = np.hstack(( np.argsort(team_n_races[ ~np.isin(team_n_races['f1'], ['indy500', 'others']) ]['f2'])[::-1], np.nonzero(team_n_races['f1'] == 'indy500')[0], np.nonzero(team_n_races['f1'] == 'others')[0], )) order_driver = np.argsort(driver_n_races['f2'])[::-1] # ax_results.axis('off') ax_results.invert_yaxis() # ax_results.invert_yaxis() ax_results.set_xticks([1] + [i for i in range(5, 21, 5)]) ax_results.set_yticks( [ i for i in range(F1_FIRST_YEAR, F1_LATEST_YEAR, 5)] + [F1_LATEST_YEAR] ) # ax_results_right.set_yticks( # [ i for i in range(F1_FIRST_YEAR, F1_LATEST_YEAR, 5)] + [F1_LATEST_YEAR] # ) ax_results.set_xlim(*xlims) ax_results.set_ylim(*ylims) # ax_results_right.set_ylim(*ylims) ax_results.set_xlabel('Race #') ax_results.set_ylabel('Year') # ax_results_right.set_ylabel('Year') # ax_results.axis('equal') # ax_results_right.axis('equal') # Champions ax_results.axvline(MAX_RACES_YEAR + 1.5, ls='--', color='k') last_col = team_results.shape[1] def add_line_label(x, y, label, ha='center', va='top', color='black'): signal = 1 if va == 'top' else -1 line = mlines.Line2D( [x, x], [y + 1 * signal, y + 2 * signal], ls='-', color=color ) line.set_clip_on(False) ax_results.add_line(line) ax_results.text(x, y + 2 * signal, label, ha=ha, va=va, color=color) add_line_label(last_col - 4, F1_LATEST_YEAR, 'WCC ', ha='center', va='top', color='cyan') add_line_label(last_col - 3, F1_FIRST_YEAR, 'Constructor\nwith most wins', ha='right', va='bottom', color='blue') add_line_label(last_col - 1, F1_LATEST_YEAR, ' WDC', ha='center', va='top', color='brown') add_line_label(last_col - 0, F1_FIRST_YEAR, 'Driver with\nmost wins', ha='left', va='bottom', color='orange') # Team legend handles_team = [handles_team[n] for n in order_team] labels_team = [labels_team[n] for n in order_team] ax_team_legend.legend(handles_team, labels_team, loc='center', facecolor=(.85, .85, .85)) ax_team_legend.axis('off') # Driver legend # handles_driver = [handles_driver[n] for n in order_driver] # labels_driver = [labels_driver[n] for n in order_driver] ax_driver_legend.legend(handles_driver, labels_driver, loc='center', facecolor=(.75, .75, .75)) ax_driver_legend.axis('off') plt.suptitle(f'F1 {result_type} by Constructor and Driver') gs.tight_layout(fig, rect=(0,0,1,0.95))
class ConstantViewer(object): """ Class meant to visualize the constants of a log file for the Motion Profiler of Walton Robotics """ def __init__(self, clf, show_outliers: bool = False) -> None: """ :param clf: the model to use to separate the data :param show_outliers: True if black dots should be placed in the 3d plot to represent the outliers False otherwise """ super().__init__() self.showing = False self.show_outliers = show_outliers self.clf = clf self.fig = plt.figure("Scaled 3d data") fig_manager = plt.get_current_fig_manager() fig_manager.window.showMaximized() self.gs = GridSpec(3, 4, self.fig) self.master_plot = self.fig.add_subplot(self.gs[:3, :3], projection='3d') self.time_velocity = self.fig.add_subplot(self.gs[0, -1]) self.time_power = self.fig.add_subplot(self.gs[1, -1]) self.power_velocity = self.fig.add_subplot(self.gs[2, -1]) def show(self): """ Shows the figure """ if not self.showing: self.gs.tight_layout(self.fig) self.fig.show() self.showing = True def close_all(self): """ Closes the figure """ if self.showing: self.showing = False plt.close(self.fig) def plot_3d_plot(self, features, headers, labels): """ PLots the features in a 3d plot including the hyperplane that separates the data :param features: the features to use to plot in the graph :param headers: the axis titles :param labels: the color of each data point """ self.master_plot.scatter(features[:, 0], features[:, 1], features[:, 2], c=labels) self.master_plot.set_xlabel(headers[0]) self.master_plot.set_ylabel(headers[1]) self.master_plot.set_zlabel(headers[2]) plot_hyperplane(self.clf, self.master_plot, colors='orange') def manipulate_features(self, features: np.ndarray, file_data: np.ndarray) -> (np.ndarray, np.ndarray): """ Return the features manipulated in a way as to make the algorithm for separating the data more accurate. :param features: the features to use :param file_data: the log file's data :return: the manipulated features array, the outliers of the data set and the data scaler """ if contains_key(file_data, "motionState"): moving_mask = file_data["motionState"] == "MOVING" features = features[moving_mask] file_data = file_data[moving_mask] new_features = None scalers = {} if contains_key(file_data, "pathNumber"): for i in range(file_data["pathNumber"].min(), file_data["pathNumber"].max() + 1): min_max_scaler = MinMaxScaler() path_number = file_data["pathNumber"] == i scalers[min_max_scaler] = path_number features_at_path = features[path_number] half = features_at_path.shape[0] // 2 coefficient, _ = find_linear_best_fit_line(features_at_path[:half, 2], features_at_path[:half, 0]) if coefficient < 0: features_at_path[:, 0] *= - 1 features_at_path = min_max_scaler.fit_transform(features_at_path) outliers_free_features = features_at_path if new_features is None: new_features = outliers_free_features else: new_features = np.concatenate((new_features, outliers_free_features), 0) else: min_max_scaler = MinMaxScaler() scalers[min_max_scaler] = np.full(features.shape[0], True) new_features = min_max_scaler.fit_transform(features) outlier_detector = OneClassSVM(gamma=10) # Seems to work best outlier_detector.fit(new_features) outlier_prediction = outlier_detector.predict(new_features) outliers = new_features[outlier_prediction == -1] new_features = new_features[outlier_prediction == 1] features = self.reverse_scalling(new_features, scalers, outlier_prediction) if self.show_outliers: plot_hyperplane(outlier_detector, self.master_plot, interval=.04, colors="orange") return new_features, outliers, features def reverse_scalling(self, features, scalers, outlier_prediction): features = np.copy(features) for scaler, index in zip(scalers.keys(), scalers.values()): index = index[outlier_prediction == 1] features[index] = scaler.inverse_transform(features[index]) return features def graph(self, file_data): """ Graphs the features from the log file. Creates a 3D graph with time, average power to motors and velocity as axises. It also decomposes the dimensions into individual 2D graphs. :param file_data: the log file to use to extract the data from """ self.clear_graphs() features, headers = get_features(file_data) new_scaled_features, outliers, features = self.manipulate_features(features, file_data) # features = scaler.inverse_transform(new_scaled_features) labels = self.clf.predict(new_scaled_features) color_labels = list(map(lambda x: 'r' if x == 0 else 'b', labels)) self.plot_3d_plot(new_scaled_features, headers, color_labels) if self.show_outliers: self.master_plot.scatter(outliers[:, 0], outliers[:, 1], outliers[:, 2], c="black") self.show_constants_graph(features, file_data, labels, c=color_labels) plot_subplots(new_scaled_features, headers, (self.time_velocity, self.time_power, self.power_velocity), color_labels) plt.draw() def clear_graphs(self): """ Clears all the axes """ for ax in (self.master_plot, self.time_velocity, self.time_power, self.power_velocity): ax.cla() def show_grid(self): """ Shows the grids for the major ticks in the plot. """ for ax in (self.time_velocity, self.time_power, self.power_velocity): ax.grid(True) def show_constants_graph(self, features, file_data, labels, c=None): """ Creates an addition figure that will display the a graph with the constants on it and also the lines of best fit of the accelerating portion of it, the decelerating portion of it and the average of both of those lines :param features: the features to use to show the graph and find the constants from :param file_data: the whole file data :param labels: the labels to say if a data point is accelerating or not :param c: the color to plot the points :return the constants for the motion profiling kV, kK and kAcc """ if is_straight_line(file_data): easygui.msgbox("It was determined that the robot was trying to go straight. " "As an ongoing feature the program will be able detect kLag, etc... " "however for the instance this features has not been added") figure = plt.figure("Constants graph") constants_plot = figure.gca() constants_plot.set_xlabel("Velocity") constants_plot.set_ylabel("Average Power") fig_manager = plt.get_current_fig_manager() fig_manager.window.showMaximized() x = features[:, 1] y = features[:, 0] constants_plot.scatter(x, y, c=labels if c is None else c) acceleration_mask = labels == visualize.ACCELERATING coef_accelerating, intercept_accelerating = find_linear_best_fit_line(x[acceleration_mask], y[acceleration_mask]) deceleration_mask = labels == visualize.DECELERATING coef_decelerating, intercept_decelerating = find_linear_best_fit_line(x[deceleration_mask], y[deceleration_mask]) x_lim = np.array(constants_plot.get_xlim()) y_lim = np.array(constants_plot.get_ylim()) x, y = get_xy_limited(intercept_accelerating, coef_accelerating, x_lim, y_lim) constants_plot.plot(x, y) x, y = get_xy_limited(intercept_decelerating, coef_decelerating, x_lim, y_lim) constants_plot.plot(x, y) # constants_plot.plot(x_lim, coef_accelerating * x_lim + intercept_accelerating) # constants_plot.plot(x_lim, coef_decelerating * x_lim + intercept_decelerating) average_coef = (coef_accelerating + coef_decelerating) / 2 average_intercept = (intercept_accelerating + intercept_decelerating) / 2 # constants_plot.plot(x_lim, average_coef * x_lim + average_intercept) x, y = get_xy_limited(average_intercept, average_coef, x_lim, y_lim) constants_plot.plot(x, y) acceleration_coefficient = (coef_accelerating - average_coef) acceleration_intercept = (intercept_accelerating - average_intercept) k_acc = ((x.max() + x.min()) / 2) * acceleration_coefficient + acceleration_intercept bbox_props = dict(boxstyle="round,pad=0.3", fc="cyan", ec="b", lw=2) constants_plot.text(x_lim[0], y_lim[1], "kV: {}\nkK: {}\nkAcc: {}".format(average_coef, average_intercept, k_acc), ha="left", va="top", bbox=bbox_props) return average_coef, average_intercept, k_acc
def run(self, show=False): # Load data filename = self.filename d = pyfits.getdata(filename) h = pyfits.getheader(filename) path, filename = os.path.split(filename) # Get wavelength calibration z = np.arange(h['naxis3']) w = h['CRVAL3'] + h['CDELT3'] * (z - h['CRPIX3']) # Signal-to-noise clipping s = d.sum(axis=2) s = s.sum(axis=1) gauss_pw, _ = self.fit_gaussian(z, s) log.debug("Gaussian parameters ---") log.debug("p[0] = %.2f" % gauss_pw[0]) log.debug("p[1] = %.2f" % gauss_pw[1]) log.debug("p[2] = %.2f" % gauss_pw[2]) log.debug("p[3] = %.2f" % gauss_pw[3]) lor_p = self.fit_lorentzian(z, s) log.debug("Lorentzian parameters ---") log.debug("p[0] = %.2f" % lor_p[0]) log.debug("p[1] = %.2f" % lor_p[1]) log.debug("p[2] = %.2f" % lor_p[2]) log.debug("p[3] = %.2f" % lor_p[3]) fwhm = np.abs(gauss_pw[2] * 2 * np.sqrt(2 * np.log(2))) filter_ = np.where(np.abs(z - gauss_pw[1]) < fwhm, True, False) if show: plt.plot(z, self.gaussian(gauss_pw, z), 'r-', lw=2, label='Gaussian Fit') plt.plot(z, self.lorentzian(lor_p, z), 'b-', lw=2, label='Lorentzian Fit') plt.plot(z, s, 'ko') plt.plot(z[filter_], s[filter_], 'ro') plt.title('Cube collapsed in XY and fits.') plt.grid() plt.legend(loc='best') plt.gcf().canvas.mpl_connect('key_press_event', self.on_key_press) plt.show() signal = d[filter_].mean(axis=0) noise = d[np.logical_not(filter_)].mean(axis=0) target_snr = 3 snr = signal / noise snr = ndimage.median_filter(snr, 3) snr_mask = np.where(signal > target_snr * noise, True, False) snr_laplacian = ndimage.morphological_laplace(snr * snr_mask, size=3) snr_mask *= np.where(np.abs(snr_laplacian) < 5.0, True, False) snr_mask = ndimage.binary_opening(snr_mask, iterations=5) snr_mask = ndimage.binary_closing(snr_mask, iterations=5) # SNR MASK Based on circular aperture # aperture_radius = 1 # arcmin # aperture_radius = aperture_radius / 60 # arcmin to deg # aperture_radius = np.abs(aperture_radius / h['CD1_1']) # deg to pix # print(aperture_radius) # c = SkyCoord('7:41:55.400', '-18:12:33.00', frame=h['RADECSYS'].lower(), unit=(u.hourangle, u.deg)) # x, y = np.arange(h['NAXIS1']), np.arange(h['NAXIS2']) # X, Y = np.meshgrid(x, y) # center_wcs = wcs.WCS(h) # center = center_wcs.wcs_world2pix(c.ra.deg, c.dec.deg, 6563, 1) # snr_mask = np.sqrt((X - center[0]) ** 2 + (Y - center[1]) ** 2) # snr_mask = np.where(snr_mask < aperture_radius, True, False) # plt.imshow(snr_mask) # plt.show() # SNR MASK Based on squared area aperture_width = 256 * 4.048e-1 # arcsec (from original image) aperture_width /= 3600 # arcsec to deg aperture_width /= np.abs(h['CD1_1']) # deg to pix c = SkyCoord('7:41:55.197', '-18:12:35.97', frame=h['RADECSYS'].lower(), unit=(u.hourangle, u.deg)) x, y = np.arange(h['NAXIS1']), np.arange(h['NAXIS2']) X, Y = np.meshgrid(x, y) center_wcs = wcs.WCS(h) center = center_wcs.wcs_world2pix(c.ra.deg, c.dec.deg, 6563, 1) print(center, np.abs(X - center[0]), np.abs(Y - center[1]), aperture_width) X = np.where(np.abs(X - center[0]) < aperture_width / 2, True, False) Y = np.where(np.abs(Y - center[1]) < aperture_width / 2, True, False) snr_mask = X * Y plt.imshow(snr_mask) plt.gcf().canvas.mpl_connect('key_press_event', self.on_key_press) plt.show() if show: fig1 = plt.figure(figsize=(20, 5)) plt.title('Signal-to-Noise Ratio') gs = GridSpec(1, 3) ax1 = plt.subplot(gs[0]) ax1.set_title('SNR') im1 = ax1.imshow(snr, cmap='cubehelix', interpolation='nearest', origin='lower', vmin=3, vmax=20) div1 = make_axes_locatable(ax1) cax1 = div1.append_axes("right", size="5%", pad=0.05) cbar1 = plt.colorbar(mappable=im1, cax=cax1, use_gridspec=True, orientation='vertical') ax2 = plt.subplot(gs[1]) ax2.set_title('Mask') im2 = ax2.imshow(np.where(snr_mask, 1, 0), cmap='gray', interpolation='nearest', origin='lower', vmin=0, vmax=1) div2 = make_axes_locatable(ax2) cax2 = div2.append_axes("right", size="5%", pad=0.05) cbar2 = plt.colorbar(mappable=im2, cax=cax2, use_gridspec=True, orientation='vertical') cmap = plt.get_cmap('cubehelix') cmap.set_bad('w', 1.0) ax3 = plt.subplot(gs[2]) ax3.set_title('Masked') im3 = ax3.imshow(np.ma.masked_where(~snr_mask, snr), cmap=cmap, interpolation='nearest', origin='lower', vmin=0) div3 = make_axes_locatable(ax3) cax3 = div3.append_axes("right", size="5%", pad=0.05) cbar3 = plt.colorbar(mappable=im3, cax=cax3, use_gridspec=True, orientation='vertical') plt.gcf().canvas.mpl_connect('key_press_event', self.on_key_press) gs.tight_layout(fig1) plt.show() pyfits.writeto(filename.replace('.','.SNR.'), snr, h, clobber=True) pyfits.writeto(filename.replace('.','.SNR_LAPLACIAN.'), snr_laplacian, h, clobber=True) # Adjust continuum continuum = self.fit_continuum(d) # Subtract continuum continuum = np.reshape(continuum, (continuum.size, 1, 1)) continuum = np.repeat(continuum, d.shape[1], axis=1) continuum = np.repeat(continuum, d.shape[2], axis=2) d -= continuum del continuum # Integrate along the planetary nebulae d = d * snr_mask d = d.sum(axis=2) d = d.sum(axis=1) d = d / np.float(h['EXPTIME']) gauss_pw, _ = self.fit_gaussian(w, d) gauss_pc, _ = self.fit_gaussian(z, d) log.info("Gaussian parameters ---") log.info("p[0] = %.4f ADU/s" % gauss_pw[0]) log.info("p[1] = %.4f A = %.4f channels" % (gauss_pw[1], gauss_pc[1])) log.info("p[2] = %.4f A = %.4f channels" % (gauss_pw[2], gauss_pc[2])) log.info("p[3] = %.4f ADU/s" % gauss_pw[3]) # total_flux = (gauss_pc[0] - gauss_pc[3]) * np.sqrt(2 * np.pi) \ # * gauss_pc[2] # log.info("Total flux = (a - d) * sqrt(2pi) * c") # log.info(" %.5E ADU/s" % total_flux) fwhm = np.abs(gauss_pw[2] * 2 * np.sqrt(2 * np.log(2))) filter_ = np.where(np.abs(w - gauss_pw[1]) < fwhm, True, False) # d = d - d[~filter_].mean() if show: plt.plot(w, self.gaussian(gauss_pw, w), 'r-', lw=2, label='Gaussian Fit') # plt.plot(w, self.lorentzian(lor_p, w), 'b-', lw=2, label='Lorentzian Fit') plt.plot(w[~filter_], d[~filter_], 'ko') plt.plot(w[filter_], d[filter_], 'ro') plt.title('Spectral profile of the masked area.') plt.xlabel(u'Wavelenght [$\AA$]') plt.ylabel(u'Integrated Count Level [ADU/s]') plt.grid() plt.legend(loc='best') plt.gcf().canvas.mpl_connect('key_press_event', self.on_key_press) plt.show() integrated_flux = (gauss_pc[0] - gauss_pc[3]) \ * (gauss_pc[2] * np.sqrt(2 * np.pi)) log.info("Total flux: %.4E adu/s" % (integrated_flux)) snr_mask = np.where(snr_mask, 1, 0) pyfits.writeto( os.path.join(path, filename.replace('.fits', '.mask.fits')), snr_mask, h, clobber=True) return
bounds = np.unique(np.array(bounds)) axes[i].plot(heights, density_sine(heights), '-', color=colors[b_factor]) axes[i].plot(bounds, density_sine(bounds), 'o', color="C1", markersize=4) # Configure axes axes[i].set_ylim(-0.2, 1.2) axes[i].set_yticks([]) axes[i].set_xticks([bottom, top]) axes[i].set_xticklabels([r"$r_1$", r"$r_2$"]) axes[i].set_xlabel("Depth") if i == 0: axes[i].set_title("(b)") # Plot number of tesseroids (sine) # -------------------------------- ax = plt.subplot(outer_grid[2]) ax.plot(b_factors, n_tess, 'o') ax.grid() ax.set_ylim(2, 21) ax.yaxis.tick_right() ax.yaxis.set_label_position("right") ax.set_yticks(np.arange(3, 21, 2)) ax.set_xticks(np.arange(1, 11, 2)) ax.set_xlabel(r"$b$") ax.set_ylabel("Number of tesseroids") ax.set_title("(c)") outer_grid.tight_layout(fig) plt.savefig(figure_fname, dpi=300) plt.show()
def plot_flux_annotated( unbound, bound, flux_unbound, flux_bound, flux_between, k_cat_bin, scaling=None, name=None, source_code=None ): fig = plt.figure(figsize=(6, 6)) gs = GridSpec(1, 1, wspace=0.5, hspace=0.5) ax1 = plt.subplot(gs[0, 0]) bins = len(unbound) ax1.scatter(range(len(bound)), bound, s=80, c="b") ax1.scatter(range(len(unbound)), unbound, s=80, c="r") if not scaling: scaling = 10 ** 3 rng = range(bins) for i in range(bins - 1): if flux_bound[i, 1] < 0: af.right_to_left_bound_arrow(plt, flux_bound[i, 1], i, rng, unbound, bound, scaling) else: af.left_to_right_bound_arrow(plt, flux_bound[i, 1], i, rng, unbound, bound, scaling) for i in range(bins - 1): if flux_unbound[i, 1] < 0: af.right_to_left_unbound_arrow(plt, flux_unbound[i, 1], i, rng, unbound, bound, scaling) else: af.left_to_right_unbound_arrow(plt, flux_unbound[i, 1], i, rng, unbound, bound, scaling) for i in range(bins): # DRS: This is correct. Verified for group meeting 2015-10-05. if flux_between[i, 1] < 0: af.bound_to_unbound_arrow(plt, flux_between[i, 1], i, rng, unbound, bound, scaling) else: af.unbound_to_bound_arrow(plt, flux_between[i, 1], i, rng, unbound, bound, scaling) max_flux = max(abs(flux_between[:, 1])) max_index = np.where(abs(flux_between[:, 1]) == max_flux)[0] ax1.set_title("Flux visualized") ax1.set_xlabel("Reaction coordinate ($\phi$)") ax1.set_ylabel("Energy (a.u.)") bbox_props = dict(fc="white", alpha=0.65) ax1.annotate( af.format_decimal(decimal.Decimal(max_flux)), xy=(max_index, unbound[max_index]), xycoords="data", xytext=(+100, +120), textcoords="offset points", fontsize=22, bbox=bbox_props, arrowprops=dict(arrowstyle="->", facecolor="black", color="black", connectionstyle="arc3,rad=0.4", linewidth=2), ) ax1.annotate( af.format_decimal(decimal.Decimal(flux_between[k_cat_bin, 1])), xy=(k_cat_bin, bound[k_cat_bin]), xycoords="data", xytext=(+100, -40), textcoords="offset points", fontsize=22, bbox=bbox_props, arrowprops=dict( arrowstyle="->", facecolor="black", color="black", connectionstyle="arc3,rad=-0.4", linewidth=2 ), ) st = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") sts = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") if name: if source_code is not None: text = st + " " + name + " in " + source_code else: text = st + " " + name fig.text(0.0, 0.0, text, fontsize=12, color="black", ha="left", va="bottom", alpha=0.5) else: f.text(0.0, 0.0, st, fontsize=12, color="black", ha="left", va="bottom", alpha=0.5) gs.tight_layout(fig, rect=[0, 0, 1, 1]) # plt.savefig('Figures/flux-annotated-{}-{}.png'.format(sts, name), dpi=150) plt.show() plt.close()
ax2 = fig.add_subplot(gs[1, 0], facecolor="cornflowerblue") ax2.scatter(x2, y2, s=20, c="grey", marker="s", linewidths=2, edgecolors="k") ax2.set_ylabel("YLabel10", bbox=box) ax2.set_xlabel("XLabel10", bbox=box) for ticklabel in ax2.get_xticklabels(): ticklabel.set_rotation(45) ax2.yaxis.set_label_coords(-0.25, 0.5) ax2.xaxis.set_label_coords(0.5, -0.25) # subplot(2,2,4) x3 = np.linspace(0, 10, 100) y3 = np.exp(-x3) ax3 = fig.add_subplot(gs[1, 1]) ax3.errorbar(x3, y3, fmt="b-", yerr=0.6 * y3, ecolor="lightsteelblue", elinewidth=2, capsize=0, errorevery=5) ax3.set_ylabel("YLabel11", bbox=box) ax3.set_xlabel("XLabel11", bbox=box) ax3.xaxis.set_label_coords(0.5, -0.25) ax3.set_ylim(-0.1, 1.1) ax3.set_yticks(np.arange(0, 1.1, 0.1)) gs.tight_layout(fig) plt.show()
def plot_flux_single_annotated(surface, flux, scaling=None, name=None, source_code=None): fig = plt.figure(figsize=(12, 12)) gs = GridSpec(1, 1, wspace=0.5, hspace=0.5) ax1 = plt.subplot(gs[0, 0]) bins = len(surface) ax1.scatter(range(len(surface)), surface, s=80, c="k") if not scaling: scaling = 10 ** 3 rng = range(bins) for i in range(bins - 1): if flux[i, 1] < 0: plt.annotate( "", xy=(flux[i, 0], surface[i]), xycoords="data", xytext=(flux[i + 1, 0], surface[i + 1]), textcoords="data", arrowprops=dict( arrowstyle="->, head_width=0.5", color="b", shrinkA=10, shrinkB=10, linewidth=abs(flux[i, 1]) * scaling, ), ) else: plt.annotate( "", xy=(flux[i + 1, 0], surface[i + 1]), xycoords="data", xytext=(flux[i, 0], surface[i]), textcoords="data", arrowprops=dict( arrowstyle="->, head_width=0.5", color="b", shrinkA=10, shrinkB=10, linewidth=abs(flux[i, 1]) * scaling, ), ) ax1.set_title("Flux visualized") ax1.set_xlabel("Reaction coordinate ($\phi$)") ax1.set_ylabel("Energy (a.u.)") st = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") sts = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") if name: if source_code is not None: text = st + " " + name + " in " + source_code else: text = st + " " + name fig.text(0.0, 0.0, text, fontsize=12, color="black", ha="left", va="bottom", alpha=0.5) else: f.text(0.0, 0.0, st, fontsize=12, color="black", ha="left", va="bottom", alpha=0.5) gs.tight_layout(fig, rect=[0, 0, 1, 1]) # plt.savefig(name+'-flux-cat-bin-{}.png'.format(k_cat_bin), dpi=150) plt.show() plt.close()
def _spectrogram(tr, starttime, endtime, is_infrasound, win_dur=5, db_lim=None, freq_lim=None): """ Make a combination waveform and spectrogram plot for an infrasound or seismic signal. Args: tr (:class:`~obspy.core.trace.Trace`): Input data, usually starts before `starttime` and ends after `endtime` (this function expects the response to be removed!) starttime (:class:`~obspy.core.utcdatetime.UTCDateTime`): Start time endtime (:class:`~obspy.core.utcdatetime.UTCDateTime`): End time is_infrasound (bool): `True` if infrasound, `False` if seismic win_dur (int or float): Duration of window [s] (this usually must be adjusted depending upon the total duration of the signal) db_lim (tuple): Tuple specifying colorbar / colormap limits [dB] freq_lim (tuple): Tuple defining frequency limits for spectrogram plot Returns: Tuple of (`fig`, `spec_line`, `wf_line`, `time_box`) """ if is_infrasound: ref_val = REFERENCE_PRESSURE ylab = 'Pressure (Pa)' clab = 'Power (dB$_{%g\ \mathrm{μPa}}$/Hz)' % (REFERENCE_PRESSURE * 1e6) rescale = 1 else: ref_val = REFERENCE_VELOCITY ylab = 'Velocity (μm/s)' clab = 'Power (dB$_{%g\ \mathrm{m/s}}$/Hz)' % REFERENCE_VELOCITY rescale = 1e6 # Converting to μm/s fs = tr.stats.sampling_rate nperseg = int(win_dur * fs) # Samples nfft = np.power(2, int(np.ceil(np.log2(nperseg))) + 1) # Pad fft with zeroes f, t, sxx = signal.spectrogram(tr.data, fs, window='hann', nperseg=nperseg, nfft=nfft) sxx_db = 20 * np.log10(np.sqrt(sxx) / ref_val) # [dB / Hz] t_mpl = tr.stats.starttime.matplotlib_date + (t / mdates.SEC_PER_DAY) fig = plt.figure(figsize=np.array(RESOLUTION) / DPI) # width_ratios effectively controls the colorbar width gs = GridSpec(2, 2, figure=fig, height_ratios=[2, 1], width_ratios=[40, 1]) spec_ax = fig.add_subplot(gs[0, 0]) wf_ax = fig.add_subplot(gs[1, 0], sharex=spec_ax) # Share x-axis with spec cax = fig.add_subplot(gs[0, 1]) wf_ax.plot(tr.times('matplotlib'), tr.data * rescale, 'k', linewidth=0.5) wf_ax.set_ylabel(ylab) wf_ax.grid(linestyle=':') max_value = np.abs(tr.copy().trim(starttime, endtime).data).max() * rescale wf_ax.set_ylim(-max_value, max_value) im = spec_ax.pcolormesh(t_mpl, f, sxx_db, cmap=cc.m_rainbow, rasterized=True) spec_ax.set_ylabel('Frequency (Hz)') spec_ax.grid(linestyle=':') spec_ax.set_ylim(freq_lim) # Tick locating and formatting locator = mdates.AutoDateLocator() wf_ax.xaxis.set_major_locator(locator) wf_ax.xaxis.set_major_formatter(_UTCDateFormatter(locator)) fig.autofmt_xdate() # "Crop" x-axis! wf_ax.set_xlim(starttime.matplotlib_date, endtime.matplotlib_date) # Initialize animated stuff line_kwargs = dict(x=starttime.matplotlib_date, color='red', linewidth=1) spec_line = spec_ax.axvline(**line_kwargs) wf_line = wf_ax.axvline(**line_kwargs) time_box = AnchoredText( s=starttime.strftime('%H:%M:%S'), pad=0.2, loc='lower right', borderpad=0, prop=dict(color='red'), ) spec_ax.add_artist(time_box) # Clip image to db_lim if provided (doesn't clip if db_lim=None) db_min, db_max = im.get_clim() im.set_clim(db_lim) # Automatically determine whether to show triangle extensions on colorbar # (kind of adopted from xarray) if db_lim: min_extend = db_min < db_lim[0] max_extend = db_max > db_lim[1] else: min_extend = False max_extend = False if min_extend and max_extend: extend = 'both' elif min_extend: extend = 'min' elif max_extend: extend = 'max' else: extend = 'neither' fig.colorbar(im, cax, extend=extend, extendfrac=EXTENDFRAC, label=clab) spec_ax.set_title('.'.join([ tr.stats.network, tr.stats.station, tr.stats.location, tr.stats.channel ])) # Repeat tight_layout and update, janky but works... for _ in range(2): gs.tight_layout(fig) gs.update(hspace=0, wspace=0.05) # Finnicky formatting to get extension triangles (if they exist) to extend # above and below the vertical extent of the spectrogram axes pos = cax.get_position() triangle_height = EXTENDFRAC * pos.height ymin = pos.ymin height = pos.height if min_extend and max_extend: ymin -= triangle_height height += 2 * triangle_height elif min_extend and not max_extend: ymin -= triangle_height height += triangle_height elif max_extend and not min_extend: height += triangle_height else: pass cax.set_position([pos.xmin, ymin, pos.width, height]) return fig, spec_line, wf_line, time_box
for ticklabel in ax2.get_xticklabels(): ticklabel.set_rotation(45) ax2.yaxis.set_label_coords(-0.04, 0.5) ax2.xaxis.set_label_coords(0.5, -0.12) # 第三个子图位置 colors = ['#e41a1c', '#377eb8', '#4daf4a', '#984ea3', '#ff7f00', '#00FFFF'] ax3 = fig.add_subplot(gs[1, 1]) #绘制在第2行和第2列 ax3.bar(GDP.index.values, GDP.GDP, width=0.6, align='center', color=colors, tick_label=GDP.Province) ax3.set_xlabel('省份', fontsize=20, labelpad=15) ax3.set_ylabel('GDP产值(万亿)', fontsize=20, labelpad=15) # 添加表格 col_labels = ['GDP(万亿)'] row_labels = GDP.Province table_vals = np.array(GDP.GDP.values).reshape(-1, 1) col_colors = ['#e41a1c', '#377eb8', '#4daf4a', '#984ea3', '#ff7f00', '#00FFFF'] my_table = plt.table(cellText=table_vals, cellLoc='center', colWidths=[0.1] * 6, rowLabels=row_labels, colLabels=col_labels, rowColours=col_colors, bbox=[0.8, 0.7, 0.1, 0.25]) # 显示 gs.tight_layout(fig) #控制子图参数的 plt.show()