def plot_nonlinear_iterations(**kwargs): steps = extract_steps(**kwargs) fig = Figure() ax = fig.add_subplot(111) title = fig.suptitle(kwargs.get('title'), fontsize = 14, fontweight = 'bold') canvas = FigureCanvas(fig) step_numbers = [ step.step_number for step in steps if step.status_convergence == 1] num_iters_nonlinear = [ step.num_iters_nonlinear for step in steps if step.status_convergence == 1] ax.bar( step_numbers, num_iters_nonlinear) ax.set_xlabel('Continuation Step') ax.set_ylabel('Nonlinear Iterations') set_num_ticks(ax, integer = (True, True)) canvas.print_figure( 'nonlinear_iterations.pdf', bbox_extra_artists = [title], bbox_inches = 'tight')
def draw_plot(meta, data1, data2, fn): """ """ fig = Figure(figsize = (10,10)) axis = fig.add_subplot(1,1,1) axis.set_title(meta) axis.set_xlabel("MLOD") axis.set_ylabel("Markers") axis.grid(False) axis.autoscale(enable = True) axis.axhline(0, color = 'k') x = [info[0] for info in data1] y = [info[2] for info in data1] axis.plot(x, y, color='r', label = 'sse') x = [info[0] for info in data2] y = [info[2] for info in data2] axis.plot(x, y, color='b', label = 'gxe') box = axis.get_position() axis.set_position([box.x0, box.y0, box.width * 0.8, box.height]) axis.legend(loc = 'center left', bbox_to_anchor=(1.0,0.5)) canvas = FigureCanvas(fig) print "Saving file..." canvas.print_figure(fn)
def graph(args): # Get data points f = open("%s/http-data.txt" % (args.dir, )) data = map(lambda x: x.split(','), f.readlines()) f.close() xdata = map(lambda x: float(x[0]), data) ydata = map(lambda x: float(x[1]), data) # Create a figure with size 6 x 6 inches. fig = Figure(figsize=(6, 6)) # Create a canvas and add the figure to it. canvas = FigureCanvas(fig) # Added various information ax = fig.add_subplot(111) ax.set_title("Impact on HTTP Flows", fontsize=14) ax.set_xlabel("File Size (packets)", fontsize=12) ax.set_ylabel("Response Time (Normalized)", fontsize=12) ax.set_xscale('log') ax.set_yscale('log') # Display Grid. ax.grid(True, linestyle='-', color='0.75') # Generate and save the Scatter Plot. ax.scatter(xdata, ydata, s=20, color='tomato'); canvas.print_figure(args.out, dpi=500)
def plot_ast_fields(fields, matches, ast_centers=None): fig = Figure(figsize=(3.5, 3.5), frameon=False) canvas = FigureCanvas(fig) gs = gridspec.GridSpec(1, 1, left=0.15, right=0.95, bottom=0.15, top=0.95, wspace=None, hspace=None, width_ratios=None, height_ratios=None) basemap = load_galex_map() ax = setup_galex_axes(fig, gs[0], basemap) plot_patch_footprints(ax, alpha=0.8, edgecolor='dodgerblue') for n, m in matches.iteritems(): footprint = np.array(m['poly']) patch = Polygon(footprint, closed=True, transform=ax.get_transform('world'), facecolor='y', alpha=1, edgecolor='k', lw=0.5, zorder=10) ax.add_patch(patch) x = footprint[:, 0].mean() y = footprint[:, 1].mean() ax.annotate('{0:d}'.format(n), xy=(x, y), xycoords=ax.get_transform('world'), xytext=(3, -3), textcoords="offset points", size=8, bbox=dict(boxstyle="round", fc=(1., 1., 1., 0.8), edgecolor='None')) if ast_centers is not None: ax.scatter(ast_centers[:, 0], ast_centers[:, 1], marker='*', c='y', transform=ax.get_transform('world')) gs.tight_layout(fig, pad=1.08, h_pad=None, w_pad=None, rect=None) ax.coords[0].ticklabels.set_size(11) ax.coords[1].ticklabels.set_size(11) canvas.print_figure("phat_ast_fields.pdf", format="pdf")
def draw_pt_bins(in_file, out_dir, eff=0.7, rej_flavor='U', ext='.pdf', subset=None): fig = Figure(figsize=(8,6)) canvas = FigureCanvas(fig) ax = fig.add_subplot(1,1,1) ax.grid(which='both') ax.set_xscale('log') for tagger in tagschema.get_taggers(in_file, subset): pt_bins = tagschema.get_pt_bins(in_file['B/btag/ptBins']) eff_group = in_file['B/btag/ptBins'] rej_group = in_file['{}/btag/ptBins'.format(rej_flavor.upper())] x_vals, y_vals, x_err, y_err = _get_pt_xy( eff_group, rej_group, pt_bins, eff, tagger=tagger) with tagschema.ColorScheme('colors.yml') as colors: ax.errorbar( x_vals, y_vals, label=tagger, #xerr=x_err, yerr=y_err, color=colors[tagger]) ax.legend(numpoints=1, loc='upper left') ax.set_xlim(20, np.max(x_vals) * 1.1) ax.set_xlabel('$p_{\mathrm{T}}$ [GeV]', x=0.98, ha='right') ax.set_ylabel(rej_label(rej_flavor, eff), y=0.98, ha='right') x_formatter = FuncFormatter(tick_format) ax.xaxis.set_minor_formatter(x_formatter) ax.xaxis.set_major_formatter(x_formatter) out_name = '{}/{}Rej{}_ptbins{}'.format( out_dir, rej_flavor.lower(), int(eff*100), ext) canvas.print_figure(out_name, bbox_inches='tight')
def plotting(zic1,comparators): """docstring for plotting""" from mapping import probe_map for key in comparators.keys(): corr = pearsonr(zic1, comparators[key]) #the string of correlation stats s = 'R = '+str(corr[0])+'\nP = '+str(corr[1]) # Create a figure with size 6 x 6 inches. fig = Figure(figsize=(6,6)) # Create a canvas and add the figure to it. canvas = FigureCanvas(fig) # Create a subplot. ax = fig.add_subplot(111) # Set the title. ax.set_title(s,fontsize=10) # Set the X Axis label. ax.set_xlabel('Samples',fontsize=8) # Set the Y Axis label. ax.set_ylabel('Normalized Expression',fontsize=8) # Display Grid. ax.grid(True,linestyle='-',color='0.75') # Generate the Scatter Plot. ax.plot(range(1,25), zic1, 'go-', label=probe_map['206373_at']) ax.plot(range(1,25), comparators[key], 'r^-', label=probe_map[key]) # add the legend ax.legend() #ax.text(0.1,max(zic1),s) # Save the generated Scatter Plot to a PNG file. canvas.print_figure('correlations/'+key+'.png',dpi=500)
def getImage(self): ddict=self.fitresult try: fig = Figure(figsize=(6,3)) # in inches canvas = FigureCanvas(fig) ax = fig.add_axes([.15, .15, .8, .8]) ax.set_axisbelow(True) logplot = self.plotDict.get('logy', True) if logplot: axplot = ax.semilogy else: axplot = ax.plot axplot(ddict['result']['energy'], ddict['result']['ydata'], 'k', lw=1.5) axplot(ddict['result']['energy'], ddict['result']['continuum'], 'g', lw=1.5) legendlist = ['spectrum', 'continuum', 'fit'] axplot(ddict['result']['energy'], ddict['result']['yfit'], 'r', lw=1.5) fontproperties = FontProperties(size=8) if ddict['result']['config']['fit']['sumflag']: axplot(ddict['result']['energy'], ddict['result']['pileup'] + ddict['result']['continuum'], 'y', lw=1.5) legendlist.append('pileup') if matplotlib_version < '0.99.0': legend = ax.legend(legendlist,0, prop = fontproperties, labelsep=0.02) else: legend = ax.legend(legendlist,0, prop = fontproperties, labelspacing=0.02) except ValueError: fig = Figure(figsize=(6,3)) # in inches canvas = FigureCanvas(fig) ax = fig.add_axes([.15, .15, .8, .8]) ax.set_axisbelow(True) ax.plot(ddict['result']['energy'], ddict['result']['ydata'], 'k', lw=1.5) ax.plot(ddict['result']['energy'], ddict['result']['continuum'], 'g', lw=1.5) legendlist = ['spectrum', 'continuum', 'fit'] ax.plot(ddict['result']['energy'], ddict['result']['yfit'], 'r', lw=1.5) fontproperties = FontProperties(size=8) if ddict['result']['config']['fit']['sumflag']: ax.plot(ddict['result']['energy'], ddict['result']['pileup'] + ddict['result']['continuum'], 'y', lw=1.5) legendlist.append('pileup') if matplotlib_version < '0.99.0': legend = ax.legend(legendlist,0, prop = fontproperties, labelsep=0.02) else: legend = ax.legend(legendlist,0, prop = fontproperties, labelspacing=0.02) ax.set_xlabel('Energy') ax.set_ylabel('Counts') legend.draw_frame(False) outfile = self.outdir+"/"+self.outfile+".png" try: os.remove(outfile) except: pass canvas.print_figure(outfile) return self.__getFitImage(self.outfile+".png")
def test_plot(request): import matplotlib from mpl_toolkits.basemap import Basemap import numpy as np from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas from matplotlib.figure import Figure # llcrnrlat,llcrnrlon,urcrnrlat,urcrnrlon # are the lat/lon values of the lower left and upper right corners # of the map. # lat_ts is the latitude of true scale. # resolution = 'c' means use crude resolution coastlines. # #mercator # m = Basemap(projection='merc',llcrnrlat=-80,urcrnrlat=80,llcrnrlon=-180,urcrnrlon=180,lat_ts=20,resolution='c') m = Basemap(width=36000000,height=27000000,rsphere=(6378137.00,6356752.3142), resolution='l',area_thresh=1000.,projection='lcc', lat_1=30.,lat_0=0.,lon_0=0.) fig = Figure() canvas = FigureCanvas(fig) m.ax = fig.add_axes([0, 0, 1, 1]) # m.bluemarble(scale=0.5) m.drawcoastlines() m.drawmapboundary(fill_color='aqua') m.fillcontinents(color='coral',lake_color='aqua') m.drawparallels(np.arange(-90.,91.,30.)) m.drawmeridians(np.arange(-180.,181.,60.)) response = HttpResponse(content_type='image/png') canvas.print_figure(response, dpi=100) return response
def plot_sfh(model_sfh, mock_sfh, plot_path): labels = {'lewis': r'ACS-MS', 'oir_all': r'OIR-ALL'} colors = {'lewis': 'dodgerblue', 'oir_all': 'maroon'} fig = Figure(figsize=(3.5, 3.5), frameon=False) canvas = FigureCanvas(fig) gs = gridspec.GridSpec(1, 1, left=0.18, right=0.95, bottom=0.15, top=0.95, wspace=None, hspace=None, width_ratios=None, height_ratios=None) ax = fig.add_subplot(gs[0]) for plane_key in ['lewis', 'oir_all']: if plane_key not in model_sfh['sfh'].keys(): continue plot_single_sfh_line(ax, model_sfh['sfh'][plane_key], label=labels[plane_key], color=colors[plane_key], drawstyle='steps-mid') _plot_mean_age(ax, model_sfh['sfh'][plane_key].attrs['mean_age'], c=colors[plane_key]) # plot_single_sfh_line(ax, model_sfh, label='Model', color='k') # print model_sfh['sfr'] _plot_mock_sfh(ax, mock_sfh, lw=1.5, c='k', label='Mock') _plot_mean_age(ax, mock_sfh.attrs['mean_age']) ax.legend(loc='lower left', fontsize=8, frameon=True) gs.tight_layout(fig, pad=1.08, h_pad=None, w_pad=None, rect=None) canvas.print_figure(plot_path + ".pdf", format="pdf")
class Canvas: default_name = 'test.pdf' def __init__(self, out_path=None, figsize=(5.0,5.0*3/4), ext=None): # lazy import from matplotlib.backends.backend_agg import FigureCanvasAgg from matplotlib.figure import Figure self.fig = Figure(figsize) self.canvas = FigureCanvasAgg(self.fig) self.ax = self.fig.add_subplot(1,1,1) self.out_path = out_path self.ext = ext def save(self, out_path=None, ext=None): output = out_path or self.out_path assert output, "an output file name is required" out_dir, out_file = os.path.split(output) if ext: out_file = '{}.{}'.format(out_file, ext.lstrip('.')) if out_dir and not os.path.isdir(out_dir): os.makedirs(out_dir) self.canvas.print_figure(output, bbox_inches='tight') def __enter__(self): if not self.out_path: self.out_path = self.default_name return self def __exit__(self, extype, exval, extb): if extype: return None self.save(self.out_path, ext=self.ext) return True
def pie_chart(tels, cols, num_obs, ndata, event_id): from pylab import figure, rcParams, title, legend, savefig, close, axes, pie from local_conf import get_conf from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas import os fig = figure(num=11, figsize=[10, 10]) ax = fig.add_subplot(111) rcParams['axes.titlesize'] = 10.0 rcParams['xtick.labelsize'] = 14.0 rcParams['legend.fontsize'] = 22.0 rcParams['font.size'] = 22.0 colors=cols fracs=num_obs patches = ax.pie(fracs, colors=cols, labels=tels, labeldistance=0.95, explode=None, autopct='%1.f%%', shadow=False) for pie_wedge in patches[0]: pie_wedge.set_edgecolor('white') title = "Observations: "+str(ndata) legend([k[0]+': '+str(k[1]) for k in zip(tels, num_obs)],loc=(-.12,-.12), framealpha=0.4) canvas = FigureCanvas(fig) filename = settings.MEDIA_ROOT+str(event_id)+".png" if (os.path.exists(filename) ): os.remove(filename) # save the new file canvas.print_figure(filename) # close the figure close(11)
def save(self, name, log=False, vrange=None): if self.imdict['X'].sum() == 0.0 and log: warn("can't plot {}, in log mode".format(name), RuntimeWarning, stacklevel=2) return fig = Figure(figsize=(8,6)) canvas = FigureCanvas(fig) ax = fig.add_subplot(1,1,1) divider = make_axes_locatable(ax) cax = divider.append_axes("right", "5%", pad="1.5%") if log: norm=LogNorm() else: norm=Normalize() if vrange: self.imdict['vmin'], self.imdict['vmax'] = vrange im = ax.imshow(norm=norm,**self.imdict) cb_dict = {'cax':cax} if log: cb_dict['ticks'] = LogLocator(10, np.arange(0.1,1,0.1)) cb_dict['format'] = LogFormatterMathtext(10) try: cb = plt.colorbar(im, **cb_dict) except ValueError: print self.imdict['X'].sum() raise ax.set_xlabel(self.x_label, x=0.98, ha='right') ax.set_ylabel(self.y_label, y=0.98, ha='right') if self.cb_label: cb.set_label(self.cb_label, y=0.98, ha='right') canvas.print_figure(name, bbox_inches='tight')
def save_plotSpectrum(y,Fs,image_name): """ Plots a Single-Sided Amplitude Spectrum of y(t) """ fig = Figure(linewidth=0.0) fig.set_size_inches(fig_width,fig_length, forward=True) Figure.subplots_adjust(fig, left = fig_left, right = fig_right, bottom = fig_bottom, top = fig_top, hspace = fig_hspace) n = len(y) # length of the signal _subplot = fig.add_subplot(2,1,1) print "Fi" _subplot.plot(arange(0,n),y) xlabel('Time') ylabel('Amplitude') _subploti_2=fig.add_subplot(2,1,2) k = arange(n) T = n/Fs frq = k/T # two sides frequency range frq = frq[range(n/2)] # one side frequency range Y = fft(y)/n # fft computing and normalization Y = Y[range(n/2)] _subplot_2.plot(frq,abs(Y),'r') # plotting the spectrum xlabel('Freq (Hz)') ylabel('|Y(freq)|') print "here" canvas = FigureCanvasAgg(fig) if '.eps' in outfile_name: canvas.print_eps(outfile_name, dpi = 110) if '.png' in outfile_name: canvas.print_figure(outfile_name, dpi = 110)
def save_plot(fname, plot_name, plot): from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas from matplotlib.figure import Figure from matplotlib.dates import DateFormatter fig = Figure() ax = fig.add_subplot(111) ax.xaxis.set_major_formatter(DateFormatter('%H:%M')) ax.set_xlabel("Time") ax.set_ylabel(plot_name) fig.set_figheight(20) fig.set_figwidth(30) fig.autofmt_xdate() handles = [] labels = [] for graph in plot: x, y = plot[graph] handles.append(ax.plot(x, y)) labels.append(graph) fig.legend(handles, labels, 1, shadow=True) canvas = FigureCanvas(fig) canvas.print_figure(fname, dpi=80)
def plot_jumps(stream, datafilename, line_name): # L1 and L2 data is collected at a rate of 6.0064028254118895 times per second # HF spectra data is collected at a rate of 0.9375005859378663 times per second median_stream = ndimage.filters.median_filter(stream, 6.0064028254118895) # smooth with width 1 second of data smooth_stream = ndimage.filters.gaussian_filter1d(median_stream, 1.0) # smooth combined_jumps = find_jumps(stream) stream_time_length = (stream.index[-1] - stream.index[0]).total_seconds() n_plot_rows = int(np.ceil(stream_time_length/1800.)) fig, axes = plt.subplots(n_plot_rows, 1, figsize=(40,n_plot_rows*5)) for n in range(n_plot_rows): stream_time_start = stream.index[0] + timedelta(seconds=1800*n) stream_time_end = stream.index[0] + timedelta(seconds=1800*(n+1)) stream_trimmed_values = smooth_stream[(stream.index>stream_time_start) & (stream.index<stream_time_end)] stream_trimmed_timeticks = stream.index[(stream.index>stream_time_start)&(stream.index<stream_time_end)] axes[n].plot(stream_trimmed_timeticks, stream_trimmed_values, c="purple", lw=2) for jump in combined_jumps: if jump[2][1] > stream_time_start and jump[2][0] < stream_time_end: axes[n].plot([jump[2][1], jump[2][1]], [0, 1000+jump[1][0]], color="red") axes[n].text(jump[2][1], 1000+jump[1][0], str(int(round(jump[1][0]))) + " +/- " + str(round(jump[1][1],2)), rotation=45, va="bottom", ha="left") axes[n].set_ylim(10,4000) axes[n].set_xlim(stream_time_start, stream_time_end) axes[n].set_ylabel("Line Amplitude") axes[n].set_xlabel("Timestamp") canvas = FigureCanvas(fig) canvas.print_figure("full_plots/" + datafilename.split(".")[0] + "_" + line_name + "_jumps.png", dpi=72, bbox_inches='tight') close("all")
def main(): filename = "/Users/dalke/databases/compounds_500001_510000.sdf.gz" ifs = oemolistream(filename) # The figure will be 3 inches by 3 inches # Ths size is important because the text is defined relative to # inches and not pixels. In a smaller image the text is more # cramped and likely to overlap. In a larger image the text is # not big enough. This works well for my plot. fig = Figure(figsize=(4,4)) ax = fig.add_subplot(111) cids, weights, xlogps = read_data(ifs, 100) ax.scatter(weights, xlogps) center, radii = calculate_ellipse_data(weights, xlogps) ax.add_patch(Ellipse(center, radii, fill=0, edgecolor="blue")) cids, weights, xlogps = read_data(ifs, 100) ax.scatter(weights, xlogps, marker = "^", color="red") center, radii = calculate_ellipse_data(weights, xlogps) ax.add_patch(Ellipse(center, radii, fill=0, edgecolor="red")) ax.set_xlabel("Atomic weight") ax.set_ylabel("CACTVS XLogP") # Make the PNG canvas = FigureCanvasAgg(fig) # The size * the dpi gives the final image size # a4"x4" image * 80 dpi ==> 320x320 pixel image canvas.print_figure("mw_v_xlogp_ellipses.png", dpi=80)
def plotSolarRadiationAgainstMonth(filename): trainRowReader = csv.reader(open(filename, 'rb'), delimiter=',') month_most_common_list = [] Solar_radiation_64_list = [] for row in trainRowReader: month_most_common = row[3] Solar_radiation_64 = row[6] month_most_common_list.append(month_most_common) Solar_radiation_64_list.append(Solar_radiation_64) #convert all elements in the list to float while skipping the first element for the 1st element is a description of the field. month_most_common_list = [float(i) for i in prepareList(month_most_common_list)[1:] ] Solar_radiation_64_list = [float(i) for i in prepareList(Solar_radiation_64_list)[1:] ] fig=Figure() ax=fig.add_subplot(111) title='Scatter Diagram of solar radiation against month of the year' ax.set_xlabel('Most common month') ax.set_ylabel('Solar Radiation') fig.suptitle(title, fontsize=14) try: ax.scatter(month_most_common_list, Solar_radiation_64_list) #it is possible to make other kind of plots e.g bar charts, pie charts, histogram except ValueError: pass canvas = FigureCanvas(fig) canvas.print_figure('solarRadMonth.png',dpi=500)
def draw_simple_rejrej(in_file, out_dir, ext='.pdf', tagger='gaia', official=False, approval='Internal', points=[]): """ Draw iso-efficiency contours for one tagger (no colors). """ fig = Figure(figsize=_fig_size) canvas = FigureCanvas(fig) ax = fig.add_subplot(1,1,1) ds = in_file[tagger + '/all'] label_rejrej_axes(ax, ds) levels = np.linspace(0.2, 0.5, 7) add_contour(ax, ds, opts=dict(levels=levels, textsize=10)) zax = long_particle_names[ds.attrs['xyz'][2]] if official: _add_rejrej_official(ax, approval, zax=zax, size=9) for y, x, z in points: ax.scatter(x, y, s=20, c='yellow') ax.annotate( r'$\epsilon_{{c}}$ = {:.2f}'.format(z), (x,y), xytext=(-8,0), textcoords='offset points', size='x-small', bbox = dict(boxstyle = 'round', fc = 'yellow', alpha = 1), # arrowprops = dict(arrowstyle = '->'), ha='right', va='top') out_name = '{}/rejrej-simple{}'.format(out_dir, ext) canvas.print_figure(out_name, bbox_inches='tight')
def _plot_baseline_subtracted(self, x, y, raw=True, baseline=True): """Plot the baseline-subtracted data""" from matplotlib.figure import Figure from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas figure = Figure() canvas = FigureCanvas(figure) axes1 = figure.add_subplot(1, 1, 1, axisbg='whitesmoke') # Points for fit axes1.plot(x, y, 'o', color='deepskyblue', markersize=2, alpha=1, label='Baseline-subtracted data') axes1.set_xlabel('time (s)') axes1.set_ylabel(r' corr. differential power ($\mu$cal / s)') axes1.legend(loc='upper center', bbox_to_anchor=(0.2, 0.95), ncol=1, fancybox=True, shadow=True, markerscale=3, prop={'size': 6}) if raw: axes2 = axes1.twinx() axes2.plot(x, self.differential_power, 'o', color='gray', markersize=2, alpha=.3, label='Raw data') axes2.set_ylabel(r'raw differential power ($\mu$cal / s)') axes2.legend(loc='upper center', bbox_to_anchor=(0.8, 0.95), ncol=1, fancybox=True, shadow=True, markerscale=3, prop={'size': 6}) if baseline: axes2.plot(x, self.baseline_power, '-', color='black', alpha=.3, label='baseline') axes1.set_title(self.data_filename) canvas.print_figure(self.name + '-subtracted.png', dpi=500)
def plot_lm(d, snrs, l1s, m1s, outroot): """ Plot the lm coordinates (relative to phase center) for all candidates. """ outname = os.path.join(d["workdir"], "plot_" + outroot + "_impeak.png") snrmin = 0.8 * min(d["sigma_image1"], d["sigma_image2"]) fig4 = plt.Figure(figsize=(10, 10)) ax4 = fig4.add_subplot(111) # plot positive good = n.where(snrs > 0) sizes = (snrs[good] - snrmin) ** 5 # set scaling to give nice visual sense of SNR xarr = 60 * n.degrees(l1s[good]) yarr = 60 * n.degrees(m1s[good]) ax4.scatter(xarr, yarr, s=sizes, facecolor="none", alpha=0.5, clip_on=False) # plot negative good = n.where(snrs < 0) sizes = (n.abs(snrs[good]) - snrmin) ** 5 # set scaling to give nice visual sense of SNR xarr = 60 * n.degrees(l1s[good]) yarr = 60 * n.degrees(m1s[good]) ax4.scatter(xarr, yarr, s=sizes, marker="x", edgecolors="k", alpha=0.5, clip_on=False) ax4.set_xlabel("Dec Offset (amin)") ax4.set_ylabel("RA Offset (amin)") fov = n.degrees(1.0 / d["uvres"]) * 60.0 ax4.set_xlim(fov / 2, -fov / 2) ax4.set_ylim(-fov / 2, fov / 2) canvas4 = FigureCanvasAgg(fig4) canvas4.print_figure(outname)
def draw_ctag_rejrej(in_file, out_dir, ext='.pdf'): """ Basic heatmap of efficiency vs two rejections. """ fig = Figure(figsize=_fig_size) canvas = FigureCanvas(fig) ax = fig.add_subplot(1,1,1) ds = in_file['gaia/all'] eff_array, extent = _get_arr_extent(ds) label_rejrej_axes(ax, ds) im = ax.imshow(eff_array.T, extent=extent, origin='lower', aspect='auto') ax.set_xscale('log') ax.set_yscale('log') ax.grid(which='both') # add_contour(ax,ds) divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) cb = Colorbar(ax=cax, mappable=im) out_name = '{}/rejrej{}'.format(out_dir, ext) # ignore complaints about not being able to log scale images with warnings.catch_warnings(): warnings.simplefilter("ignore") canvas.print_figure(out_name, bbox_inches='tight')
def simple_plot(request, correlation_id): import django from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas from matplotlib.figure import Figure fig = Figure() ax = fig.add_subplot(111) correlation = Correlation.objects.get(pk=correlation_id) x = [] y = [] for i in correlation.get_xdata_list(): x.append(i) for i in correlation.get_ydata_list(): y.append(i) ax.scatter(x, y) ax.set_xlabel(correlation.xlabel, size=15, backgroundcolor='w') ax.set_ylabel(correlation.ylabel, size=15) ax.set_title(correlation.title, size=20) canvas = FigureCanvas(fig) response = django.http.HttpResponse(content_type='image/png') canvas.print_figure(response, facecolor='w', dpi=80) return response
def plot_normprob(d, snrs, outroot): """ Normal quantile plot compares observed SNR to expectation given frequency of occurrence. Includes negative SNRs, too. """ outname = os.path.join(d["workdir"], "plot_" + outroot + "_normprob.png") # define norm quantile functions Z = lambda quan: n.sqrt(2) * erfinv(2 * quan - 1) quan = lambda ntrials, i: (ntrials + 1 / 2.0 - i) / ntrials # calc number of trials npix = d["npixx"] * d["npixy"] if d.has_key("goodintcount"): nints = d["goodintcount"] else: nints = d["nints"] ndms = len(d["dmarr"]) dtfactor = n.sum([1.0 / i for i in d["dtarr"]]) # assumes dedisperse-all algorithm ntrials = npix * nints * ndms * dtfactor logger.info("Calculating normal probability distribution for npix*nints*ndms*dtfactor = %d" % (ntrials)) # calc normal quantile if len(n.where(snrs > 0)[0]): snrsortpos = n.array(sorted(snrs[n.where(snrs > 0)], reverse=True)) # high-res snr Zsortpos = n.array([Z(quan(ntrials, j + 1)) for j in range(len(snrsortpos))]) logger.info("SNR positive range = (%.1f, %.1f)" % (snrsortpos[-1], snrsortpos[0])) logger.info("Norm quantile positive range = (%.1f, %.1f)" % (Zsortpos[-1], Zsortpos[0])) if len(n.where(snrs < 0)[0]): snrsortneg = n.array(sorted(n.abs(snrs[n.where(snrs < 0)]), reverse=True)) # high-res snr Zsortneg = n.array([Z(quan(ntrials, j + 1)) for j in range(len(snrsortneg))]) logger.info("SNR negative range = (%.1f, %.1f)" % (snrsortneg[-1], snrsortneg[0])) logger.info("Norm quantile negative range = (%.1f, %.1f)" % (Zsortneg[-1], Zsortneg[0])) # plot fig3 = plt.Figure(figsize=(10, 10)) ax3 = fig3.add_subplot(111) if len(n.where(snrs < 0)[0]) and len(n.where(snrs > 0)[0]): logger.info("Plotting positive and negative cands") ax3.plot(snrsortpos, Zsortpos, "k.") ax3.plot(snrsortneg, Zsortneg, "kx") refl = n.linspace( min(snrsortpos.min(), Zsortpos.min(), snrsortneg.min(), Zsortneg.min()), max(snrsortpos.max(), Zsortpos.max(), snrsortneg.max(), Zsortneg.max()), 2, ) elif len(n.where(snrs > 0)[0]): logger.info("Plotting positive cands") refl = n.linspace(min(snrsortpos.min(), Zsortpos.min()), max(snrsortpos.max(), Zsortpos.max()), 2) ax3.plot(snrsortpos, Zsortpos, "k.") elif len(n.where(snrs < 0)[0]): logger.info("Plotting negative cands") refl = n.linspace(min(snrsortneg.min(), Zsortneg.min()), max(snrsortneg.max(), Zsortneg.max()), 2) ax3.plot(snrsortneg, Zsortneg, "kx") ax3.plot(refl, refl, "k--") ax3.set_xlabel("SNR") ax3.set_ylabel("Normal quantile SNR") canvas = FigureCanvasAgg(fig3) canvas.print_figure(outname)
def pro2cap(name='C2'): fig=Figure() fig = plt.figure(figsize=(5, 3.75)) ax=fig.add_axes([0.16, 0.13, 0.74, 0.77]) ax.grid(True) cv=FigureCanvas(fig) yield_disp=6 data=np.loadtxt(r'TestRES\\'+name+'.out',skiprows =1) disp=data[:,1]/yield_disp force=data[:,2] bb=bacbone(name) x1=bb[:,1]/yield_disp f1=bb[:,2] (Vp,Vs,Va)=SheerEQNS(name,fc=58.0) bbplot=ax.plot(x1,f1,linewidth=1.5,color='k') priplot=ax.plot(Vp[:,0],Vp[:,1]/1000,-Vp[:,0],-Vp[:,1]/1000,linewidth=1,color='r') senplot=ax.plot(Vs[:,0],Vs[:,1]/1000,-Vs[:,0],-Vs[:,1]/1000,linewidth=1,color='b') aciplot=ax.plot(Va[:,0],Va[:,1]/1000,-Va[:,0],-Va[:,1]/1000,linewidth=1,color='g') #df=ax.plot(disp,force,linewidth=1) ax.set_xlabel(u'延性系数') ax.set_ylabel(u'侧向力[kN]') le=ax.legend([bbplot[0],aciplot[0],priplot[0],senplot[0]], [u'骨架曲线',u'ACI318-规范',u'Priestly抗剪能力',u'Sezen抗剪能力'], loc='upper left', fancybox=True, shadow=True,numpoints=1) cv.print_figure(name+'.png',dpi=300) return
def generateChart(self): u_genes = self.getUniqueGenes() data = dict() for gene, tags in u_genes.iteritems(): if not data.has_key(len(tags)): data[len(tags)] = 0 data[len(tags)] += 1 data[0] = self._genes_c - len(u_genes.keys()) xs = list() ys = list() # Convert the values to % for k, v in data.iteritems(): xs.append(k) ys.append((float(v) / self._genes_c)) fig = Figure() ax = fig.add_subplot(111) ax.bar(xs, ys, width=0.5, align='center') fig.get_axes()[0].set_ylabel('% of genes') fig.get_axes()[0].set_xlabel('# of unique tags') #fig.get_axes()[0].set_yscale('log') canvas = FigureCanvasAgg(fig) canvas.print_figure('enzyme-%s-length-%i.png' % \ (self.enzyme, self._original_tag_length), dpi=96) return data
def draw_cut_plane(hdf_file, out_dir, ext, tagger='jfc', maxcut=0.5, approval='Internal'): with h5py.File(hdf_file) as in_file: planes = {x: CountPlane(in_file[x][tagger]) for x in 'BUC'} xlims = ANTI_LIGHT_RANGE ylims = ANTI_B_RANGE rgb = np.dstack([planes[x].crop(xlims, ylims) for x in 'BCU']) rgb = np.log(rgb + 1) for iii in range(rgb.shape[2]): maxval = (rgb[:,:,iii].max() * maxcut) rgb[:,:,iii] = np.minimum(rgb[:,:,iii] / maxval, 1.0) fig = Figure(figsize=(5.0,5.0*3/4)) canvas = FigureCanvas(fig) ax = fig.add_subplot(1,1,1) imextent = list(xlims) + list(ylims) # transpose arrays so they draw properly (weird property of imshow) ax.imshow(rgb.swapaxes(0,1), origin='lower', extent=imextent, aspect='auto') ax.set_xlim(*xlims) ax.set_ylim(*ylims) _label_axes(ax) _add_legend(ax) _add_atlas(ax, 0.02, 0.98, approval=approval) _add_sim_info(ax, 0.02, 0.38, size=10) xcut, ycut = ANTI_LIGHT_CUT, ANTI_B_CUT cutcolor = 'DarkGreen' _add_cut(ax, xcut, ycut, color=cutcolor) _annotate_cut(ax, xy=(3.5, ycut), xyt=(0.95, 0.05), color=cutcolor) if not os.path.isdir(out_dir): os.mkdir(out_dir) canvas.print_figure('{}/2d-cut{}'.format(out_dir, ext), bbox_inches='tight')
def make_1d_plots(in_file_name, out_dir, ext, b_eff=0.1, reject='U'): textsize=_text_size taggers = {} with h5py.File(in_file_name, 'r') as in_file: for tag in ['gaia', mv1uc_name, 'jfc', 'jfit']: taggers[tag] = get_c_vs_u_const_beff( in_file, tag, b_eff=b_eff, reject=reject) fig = Figure(figsize=_fig_size) canvas = FigureCanvas(fig) ax = fig.add_subplot(1,1,1) for tname, (vc, vu) in taggers.items(): label, color = leg_labels_colors.get(tname, (tname, 'k')) ax.plot(vc, vu, label=label, color=color, linewidth=_line_width) leg = ax.legend(title='$b$-rejection = {}'.format(1/b_eff), prop={'size':textsize}) leg.get_title().set_fontsize(textsize) setup_1d_ctag_legs(ax, textsize, reject=reject) fig.tight_layout(pad=0, h_pad=0, w_pad=0) if not isdir(out_dir): os.mkdir(out_dir) file_name = '{}/{rej}Rej-vs-cEff-brej{}{}'.format( out_dir, int(1.0/b_eff), ext, rej=reject.lower()) canvas.print_figure(file_name, bbox_inches='tight')
def plot_nmean(self, fname=None): if fname: from matplotlib.backends.backend_agg import Figure, FigureCanvasAgg f = Figure() c = FigureCanvasAgg(f) else: from matplotlib import pyplot f = pyplot.gcf() ax_vi = f.add_subplot(111) ax_vi.set_xscale("log") ax_n = ax_vi.twinx() l2 = ax_vi.plot(self.gamma, self.VI, color="blue") l3 = ax_vi.plot(self.gamma, self.In, "--", color="red") l = ax_n.plot(self.gamma, self.n_mean, ":", c="black") ax_n.legend((l2, l3, l), ("$VI$", "$I_n$", "$\langle n \\rangle$"), loc=0) ax_n.set_ylabel("$\langle n \\rangle$") ax_n.set_yscale("log") # ax_n.set_ylim(bottom=0) ax_vi.set_ylabel("$VI$ and $I_n$") ax_vi.set_xlabel("$\gamma$") if fname: c.print_figure(fname, bbox_inches="tight")
def lambert_conformal(request): import matplotlib from mpl_toolkits.basemap import Basemap import numpy as np from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas from matplotlib.figure import Figure width = float(request.GET.get('width', 6000000)) height = float(request.GET.get('height', 4500000)) lat = float(request.GET.get('lat',-7)) lon = float(request.GET.get('lon',107)) true_lat1 = float(request.GET.get('true_lat1',5)) true_lat2 = float(request.GET.get('true_lat2',5)) m = Basemap(width=width,height=height, rsphere=(6378137.00,6356752.3142),\ resolution=None,projection='lcc',\ lat_1=true_lat1,lat_2=true_lat2,lat_0=lat,lon_0=lon) fig = Figure() canvas = FigureCanvas(fig) m.ax = fig.add_axes([0, 0, 1, 1]) m.drawlsmask(land_color='gray',ocean_color='white',lakes=True) m.drawparallels(np.arange(-90.,91.,30.), color='black') m.drawmeridians(np.arange(-180.,181.,60.), color='black') x, y = m(lon, lat) m.plot(x, y, 'ro') response = HttpResponse(content_type='image/png') canvas.print_figure(response, dpi=100) return response
def make_1d_overlay(in_file_name, out_dir, ext, subset, b_effs=[0.1, 0.2]): textsize = _text_size - 2 b_eff_styles = _b_eff_styles taggers = {x:{} for x in b_effs} with h5py.File(in_file_name, 'r') as in_file: for b_eff in taggers: for tag in (subset or _default_overlay_1d): taggers[b_eff][tag] = get_c_vs_u_const_beff( in_file, tag, b_eff=b_eff) fig = Figure(figsize=_fig_size) canvas = FigureCanvas(fig) ax = fig.add_subplot(1,1,1) for b_eff, linestyle in zip(b_effs, b_eff_styles): for tname, (vc, vu) in taggers[b_eff].items(): label, color = leg_labels_colors.get(tname, (tname, 'k')) lab = '$1 / \epsilon_{{ b }} = $ {rej:.0f}, {tname}'.format( rej=1/b_eff, tname=label) ax.plot(vc, vu, label=lab, color=color, linewidth=_line_width, linestyle=linestyle) ax.set_xlim(0.1, 0.5) legprops = {'size':textsize} leg = ax.legend(prop=legprops) leg.get_title().set_fontsize(textsize) setup_1d_ctag_legs(ax, textsize) fig.tight_layout(pad=0, h_pad=0, w_pad=0) if not isdir(out_dir): os.mkdir(out_dir) file_name = '{}/ctag-1d-brej-overlay{}'.format( out_dir, ext) canvas.print_figure(file_name, bbox_inches='tight')
def gsea_plot(rank_metric, enrich_term, hit_ind, nes, pval, fdr, RES, phenoPos, phenoNeg, figsize, format, outdir, module): """This is the main function for reproducing the gsea plot. :param rank_metric: pd.Series for rankings, rank_metric.values. :param enrich_term: gene_set name :param hit_ind: hits indices of rank_metric.index presented in gene set S. :param nes: Normalized enrichment scores. :param pval: nominal p-value. :param fdr: false discovery rate. :param RES: running enrichment scores. :param phenoPos: phenotype label, positive correlated. :param phenoNeg: phenotype label, negative correlated. :param figsize: matplotlib figsize. :return: """ # plt.style.use('classic') # center color map at midpoint = 0 norm = _MidpointNormalize(midpoint=0) #dataFrame of ranked matrix scores x = np.arange(len(rank_metric)) rankings = rank_metric.values # figsize = (6,6) phenoP_label = phenoPos + ' (Positively Correlated)' phenoN_label = phenoNeg + ' (Negatively Correlated)' zero_score_ind = np.abs(rankings).argmin() z_score_label = 'Zero score at ' + str(zero_score_ind) nes_label = 'NES: ' + "{:.3f}".format(float(nes)) pval_label = 'Pval: ' + "{:.3f}".format(float(pval)) fdr_label = 'FDR: ' + "{:.3f}".format(float(fdr)) im_matrix = np.tile(rankings, (2, 1)) # output truetype plt.rcParams.update({'pdf.fonttype': 42, 'ps.fonttype': 42}) # in most case, we will have mangy plots, so do not display plots # It's also convinient to run this script on command line. # GSEA Plots gs = plt.GridSpec(16, 1) # fig = plt.figure(figsize=figsize) fig = Figure(figsize=figsize) canvas = FigureCanvas(fig) # Ranked Metric Scores Plot ax1 = fig.add_subplot(gs[11:]) if module == 'ssgsea': nes_label = 'ES: ' + "{:.3f}".format(float(nes)) pval_label = 'Pval: ' fdr_label = 'FDR: ' ax1.fill_between(x, y1=np.log(rankings), y2=0, color='#C9D3DB') ax1.set_ylabel("log ranked metric", fontsize=14) else: ax1.fill_between(x, y1=rankings, y2=0, color='#C9D3DB') ax1.set_ylabel("Ranked list metric", fontsize=14) ax1.text(.05, .9, phenoP_label, color='red', horizontalalignment='left', verticalalignment='top', transform=ax1.transAxes) ax1.text(.95, .05, phenoN_label, color='Blue', horizontalalignment='right', verticalalignment='bottom', transform=ax1.transAxes) # the x coords of this transformation are data, and the y coord are axes trans1 = transforms.blended_transform_factory(ax1.transData, ax1.transAxes) ax1.vlines(zero_score_ind, 0, 1, linewidth=.5, transform=trans1, linestyles='--', color='grey') ax1.text(zero_score_ind, 0.5, z_score_label, horizontalalignment='right' if module == 'ssgsea' else 'center', verticalalignment='center', transform=trans1) ax1.set_xlabel("Rank in Ordered Dataset", fontsize=14) ax1.spines['top'].set_visible(False) ax1.tick_params(axis='both', which='both', top='off', right='off', left='off') ax1.locator_params(axis='y', nbins=5) ax1.yaxis.set_major_formatter( plt.FuncFormatter( lambda tick_loc, tick_num: '{:.1f}'.format(tick_loc))) # use round method to control float number # ax1.yaxis.set_major_formatter(plt.FuncFormatter(lambda tick_loc,tick_num : round(tick_loc, 1) )) # gene hits ax2 = fig.add_subplot(gs[8:10], sharex=ax1) # the x coords of this transformation are data, and the y coord are axes trans2 = transforms.blended_transform_factory(ax2.transData, ax2.transAxes) ax2.vlines(hit_ind, 0, 1, linewidth=.5, transform=trans2) ax2.spines['bottom'].set_visible(False) ax2.tick_params(axis='both', which='both', bottom='off', top='off', labelbottom='off', right='off', left='off', labelleft='off') # colormap ax3 = fig.add_subplot(gs[10], sharex=ax1) ax3.imshow(im_matrix, aspect='auto', norm=norm, cmap=plt.cm.seismic, interpolation='none') # cm.coolwarm ax3.spines['bottom'].set_visible(False) ax3.tick_params(axis='both', which='both', bottom='off', top='off', labelbottom='off', right='off', left='off', labelleft='off') # Enrichment score plot ax4 = fig.add_subplot(gs[:8], sharex=ax1) ax4.plot(x, RES, linewidth=4, color='#88C544') ax4.text(.1, .1, fdr_label, transform=ax4.transAxes) ax4.text(.1, .2, pval_label, transform=ax4.transAxes) ax4.text(.1, .3, nes_label, transform=ax4.transAxes) # the y coords of this transformation are data, and the x coord are axes trans4 = transforms.blended_transform_factory(ax4.transAxes, ax4.transData) ax4.hlines(0, 0, 1, linewidth=.5, transform=trans4, color='grey') ax4.set_ylabel("Enrichment score (ES)", fontsize=14) ax4.set_xlim(min(x), max(x)) ax4.tick_params(axis='both', which='both', bottom='off', top='off', labelbottom='off', right='off') ax4.locator_params(axis='y', nbins=5) # FuncFormatter need two argment, I don't know why. this lambda function used to format yaxis tick labels. ax4.yaxis.set_major_formatter( plt.FuncFormatter( lambda tick_loc, tick_num: '{:.1f}'.format(tick_loc))) # fig adjustment fig.suptitle(enrich_term, fontsize=16) fig.subplots_adjust(hspace=0) # fig.tight_layout() # plt.close(fig) enrich_term = enrich_term.replace('/', '_').replace(":", "_") canvas.print_figure( '{0}/{1}.{2}.{3}'.format(outdir, enrich_term, module, format), bbox_inches='tight', dpi=300, ) return
#!/usr/bin/python env # -*- coding: utf-8 -*- import sys import matplotlib matplotlib.use('Agg') from matplotlib.backends.backend_agg import FigureCanvasAgg from matplotlib.figure import Figure import pylab fig = Figure() canvas = FigureCanvasAgg(fig) ax = fig.add_subplot(111) x = pylab.randn(1000) ax.hist(x, 100) ax.set_title('Gate One Inline Matplotlib Test') canvas.print_figure(sys.stdout)
def make_plot(root): import matplotlib.pyplot as plt import unicorn.galfit plt.rcParams['image.cmap'] = 'gray' im = pyfits.open(root + '.fits') seg = pyfits.open(root + '_seg.fits') if USE_PLOT_GUI: fig = plt.figure(figsize=[6, 2.3], dpi=100) else: fig = Figure(figsize=[6, 2.3], dpi=100) fig.subplots_adjust(wspace=0.0, hspace=0.0, left=0.02, bottom=0.02, right=0.98, top=0.87) # im_max = im[1].data.max() im[1].data = 0 - im[1].data im[2].data = 0 - im[2].data im[3].data = 0 - im[3].data #im_max *= -1 scl = 0.8 ax = fig.add_subplot(131) ax.imshow(im[1].data, vmin=-1 * im_max * scl, vmax=0.1 * im_max * scl, interpolation='nearest') axis_ticks(im, ax) ax = fig.add_subplot(132) ax.imshow(im[2].data, vmin=-1 * im_max * scl, vmax=0.1 * im_max * scl, interpolation='nearest') axis_ticks(im, ax) scl = scl * 0.2 ax = fig.add_subplot(133) ax.imshow(im[3].data, vmin=-1 * im_max * scl, vmax=0.1 * im_max * scl, interpolation='nearest') axis_ticks(im, ax) #### Segmentation mask # ax = fig.add_subplot(133) # #obj = seg[0].data > 0 # #seg[0].data[obj] = -1 # mask = 0-seg[0].data*1./seg[0].data.max() # plt.imshow(mask, vmin=-1, vmax=0, interpolation='nearest') id = root.split('_')[1] id = root.replace('-G141', '').replace('_galfit', '') x0, y0, mag, re, n, bovera, chi2 = unicorn.galfit.read_log(root + '.log') re = '%5.2f' % (np.float(re.replace('*', '')) * 0.06) label = '#' + id + r' $r_e$=' + re + r'$^{\prime\prime}$ $n$=' + n + r' $b/a$=' + bovera + r' $\chi^2_\nu$=' + chi2 #### Smarter label log = unicorn.galfit.GalfitLogfile(root + '.log') label = id + r' $\log\ \chi^2_\nu $=%.2f' % (np.log10(log.chi2)) NCOMP = 0 ax.text(-2, 1.08, label, horizontalalignment='left', verticalalignment='center', transform=ax.transAxes) y0 = 1.08 for comp in log.list: label = '' if comp.type == 'sersic': label = r'$r_e=%5.2f^{\prime\prime}$ $n$=%4.2f $b/a$=%4.2f' % ( comp.re.value * 0.06, comp.n.value, comp.ba.value) NCOMP += 1 if comp.type == 'psf': label = 'PSF ' NCOMP += 1 if NCOMP > 1: y0 -= 0.1 if label: ax.text(1, y0, label, horizontalalignment='right', verticalalignment='center', transform=ax.transAxes) if USE_PLOT_GUI: fig.savefig(root + '.png', dpi=100, transparent=False) plt.close() else: canvas = FigureCanvasAgg(fig) canvas.print_figure(root + '.png', dpi=100, transparent=False)
def doChooch(self, elt, edge, scan_directory, archive_directory, prefix): scan_file_prefix = str(os.path.join(scan_directory, prefix)) archive_file_prefix = str(os.path.join(archive_directory, prefix)) if os.path.exists(scan_file_prefix + ".raw"): i = 1 while os.path.exists(scan_file_prefix + "%d.raw" % i): i = i + 1 scan_file_prefix += "_%d" % i archive_file_prefix += "_%d" % i scan_file_raw_filename = \ os.path.extsep.join((scan_file_prefix, "raw")) archive_file_raw_filename = \ os.path.extsep.join((archive_file_prefix, "raw")) scan_file_efs_filename = \ os.path.extsep.join((scan_file_prefix, "efs")) archive_file_efs_filename = \ os.path.extsep.join((archive_file_prefix, "efs")) scan_file_png_filename = \ os.path.extsep.join((scan_file_prefix, "png")) archive_file_png_filename = \ os.path.extsep.join((archive_file_prefix, "png")) try: if not os.path.exists(scan_directory): os.makedirs(scan_directory) if not os.path.exists(archive_directory): os.makedirs(archive_directory) except: logging.getLogger("HWR").exception( "EMBLEnergyScan: could not create results directory.") self.store_energy_scan() self.emit("energyScanFailed", ()) return try: scan_file_raw = open(scan_file_raw_filename, "w") archive_file_raw = open(archive_file_raw_filename, "w") except: logging.getLogger("HWR").exception( "EMBLEnergyScan: could not create results raw file") self.store_energy_scan() self.emit("energyScanFailed", ()) return else: scanData = [] x_array = [] y_array = [] for i in range(len(self.scan_data)): x = float(self.scan_data[i][0]) x = x < 1000 and x * 1000.0 or x y = float(self.scan_data[i][1]) scanData.append((x, y)) x_array.append(x / 1000.) y_array.append(y) scan_file_raw.write("%f,%f\r\n" % (x, y)) archive_file_raw.write("%f,%f\r\n" % (x, y)) scan_file_raw.close() archive_file_raw.close() self.scan_info["scanFileFullPath"] = str(scan_file_raw_filename) try: p = subprocess.Popen( [self.chooch_cmd, scan_file_raw_filename, elt, edge], stdout=subprocess.PIPE) chooch_results_list = p.communicate()[0].split("\n") chooch_results_list.remove("") pk, fppPeak, fpPeak, ip, fppInfl, fpInfl = \ map(float, chooch_results_list[-2].split(" ")) chooch_graph_data = eval(chooch_results_list[-1]) except: self.store_energy_scan() logging.getLogger("GUI").error("Energy scan: Chooch failed") return None, None, None, None, None, None, None, [], [], [], None rm = (pk + 30) / 1000.0 pk = pk / 1000.0 savpk = pk ip = ip / 1000.0 comm = "" self.scan_info['edgeEnergy'] = 0.1 self.th_edge = self.scan_info['edgeEnergy'] logging.getLogger("GUI").info( "Energy Scan: Chooch results are pk=%.2f, ip=%.2f, rm=%.2f" % (pk, ip, rm)) try: fi = open(scan_file_efs_filename) fo = open(archive_file_efs_filename, "w") except: pass #self.store_energy_scan() #self.emit("energyScanFailed", ()) #return else: fo.write(fi.read()) fi.close() fo.close() self.scan_info["peakEnergy"] = pk self.scan_info["inflectionEnergy"] = ip self.scan_info["remoteEnergy"] = rm self.scan_info["peakFPrime"] = fpPeak self.scan_info["peakFDoublePrime"] = fppPeak self.scan_info["inflectionFPrime"] = fpInfl self.scan_info["inflectionFDoublePrime"] = fppInfl self.scan_info["comments"] = comm self.scan_info["choochFileFullPath"] = scan_file_efs_filename self.scan_info["filename"] = archive_file_raw_filename self.scan_info["workingDirectory"] = archive_directory chooch_graph_x, chooch_graph_y1, chooch_graph_y2 = \ zip(*chooch_graph_data) chooch_graph_x = list(chooch_graph_x) for i in range(len(chooch_graph_x)): chooch_graph_x[i] = chooch_graph_x[i] / 1000.0 #logging.getLogger("HWR").info("EMBLEnergyScan: Saving png" ) # prepare to save png files title = "%s %s %s\n%.4f %.2f %.2f\n%.4f %.2f %.2f" % \ ("energy", "f'", "f''", pk, fpPeak, fppPeak, ip, fpInfl, fppInfl) fig = Figure(figsize=(15, 11)) ax = fig.add_subplot(211) ax.set_title("%s\n%s" % (scan_file_efs_filename, title)) ax.grid(True) ax.plot(x_array, y_array, **{"color": 'black'}) ax.set_xlabel("Energy (keV)") ax.set_ylabel("MCA counts") ax.set_xticklabels( np.round( np.linspace(min(x_array), max(x_array), len(ax.get_xticklabels()), endpoint=True), 3)) ax2 = fig.add_subplot(212) ax2.grid(True) ax2.set_xlabel("Energy (keV)") ax2.set_ylabel("") handles = [] handles.append(ax2.plot(chooch_graph_x, chooch_graph_y1, color='blue')) handles.append(ax2.plot(chooch_graph_x, chooch_graph_y2, color='red')) ax2.set_xticklabels( np.round( np.linspace(min(x_array), max(x_array), len(ax.get_xticklabels()), endpoint=True), 3)) ax2.axvline(pk, linestyle='--', color='blue') ax2.axvline(ip, linestyle='--', color='red') canvas = FigureCanvasAgg(fig) self.scan_info["jpegChoochFileFullPath"] = \ str(archive_file_png_filename) try: logging.getLogger("HWR").info( "Rendering energy scan and Chooch " + "graphs to PNG file : %s", scan_file_png_filename) canvas.print_figure(scan_file_png_filename, dpi=80) except: logging.getLogger("HWR").exception("could not print figure") try: logging.getLogger("HWR").info( "Saving energy scan to archive " + "directory for ISPyB : %s", archive_file_png_filename) canvas.print_figure(archive_file_png_filename, dpi=80) except: logging.getLogger("HWR").exception("could not save figure") self.store_energy_scan() self.emit('choochFinished', (pk, fppPeak, fpPeak, ip, fppInfl, fpInfl, rm, chooch_graph_x, chooch_graph_y1, chooch_graph_y2, title)) return pk, fppPeak, fpPeak, ip, fppInfl, fpInfl, rm, chooch_graph_x, \ chooch_graph_y1, chooch_graph_y2, title
import numpy as np import matplotlib.pyplot as plt from matplotlib.backends.backend_agg import FigureCanvasAgg fig = plt.figure() canvas = FigureCanvasAgg(fig) linear_data = np.array([1,2,3,4,5,6,7,8]) quadratic_data = linear_data**2 xvals = range(len(linear_data)) plt.barh(xvals, linear_data, height=0.3, color='g') #horizontally stacked bar graphs plt.barh(xvals, quadratic_data, height=0.3, left=linear_data, color='red') #changing width to height and bottom to left make the bars stacked sideways canvas.print_figure('images/week2_19_barcharts.png')
lw=0.5, mew=2, fmt='o', linestyle='-', elinewidth=1.5) except Exception, e: print e ax.grid(True) ax.set_xlabel('MJD') ax.set_ylabel('MHz') ax.set_ylim(0, 5 * np.median(fdif)) #1.05*max) ds = dss[0] fig.suptitle("%s @ %s %d MHz Diffractive Bandwidth Estimates" % (psr, ds.telescope, band)) canvas = FigureCanvasAgg(fig) canvas.print_figure(os.path.join(plotdir, ('%s_fdif_%d.pdf' % (psr, band)))) canvas.print_figure(os.path.join(plotdir, ('%s_fdif_%d.png' % (psr, band)))) def plotPulsarFiles(files, maxn=6, band=820, dsfig=None, acffig=None, ssfig=None, discard=1e-2): if dsfig is None: dsfig = plt.figure() if acffig is None: acffig = plt.figure()
def plot_comparison(timestamp_list, data1, data2, label1='data1', label2='data2', title='data1 x data2', basename=None, show=True): """ Plot comparison between two datasets with same temporal resolution (same number of elements and corresponding timestamps) :param timestamp_list: list of timestamps (datetime objects) :type timestamp_list: list :param data1: data for first data set to be compared :type data1: numpy.ndarray :param data2: data for second data set to be compared :type data2: numpy.ndarray :param label1: label for first data set :type label1: str :param label2: label for second data set :type label2: str :param title: plot title :type title: str :param basename: filename (path) to be used to save figure :type basename: str :param show: flag indicating if interactive plot should be shown :type show: bool """ # mask of comparable data points on both datasets mask = ~numpy.isnan(data1) & ~numpy.isnan(data2) # if nothing to compare, plot is meaningless if not numpy.any(mask): _log.error("Nothing to plot '{b}', '{l1}', '{l2}'".format(b=basename, l1=label1, l2=label2)) return # main figure setup figure = pyplot.figure() figure.set_figwidth(16) figure.set_figheight(12) canvas = FigureCanvasAgg(figure) gs = gridspec.GridSpec(18, 3) gs.update(left=0.06, right=0.98, top=0.88, bottom=0.05, wspace=0.18, hspace=0.40) # main timeseries axis_main = pyplot.subplot(gs[0:7, :]) axis_main.set_title(title, x=0.5, y=1.30) p1, = axis_main.plot_date(timestamp_list, data1, linewidth=1.0, linestyle='', marker='.', markersize=3, color='#8080ff', markeredgecolor='#8080ff', alpha=1.0) p2, = axis_main.plot_date(timestamp_list, data2, linewidth=1.0, linestyle='', marker='.', markersize=3, color='#ff8080', markeredgecolor='#ff8080', alpha=1.0) legend = axis_main.legend([p1, p2], [label1, label2], numpoints=1, markerscale=8.0, fancybox=True) legend.get_frame().set_alpha(0.7) legend.get_frame().set_edgecolor('none') axis_main.xaxis.tick_top() for t in axis_main.get_xmajorticklabels(): t.set(rotation=90) props1 = dict(boxstyle='round', facecolor='#d8d8ff', edgecolor='none', alpha=0.7) props2 = dict(boxstyle='round', facecolor='#ffe5e5', edgecolor='none', alpha=0.7) msg1 = '{v}: $mean={a}$ $median={d}$ $std={s}$ $N={n}$'.format( v=label1, a=numpy.nanmean(data1), d=numpy.nanmedian(data1), s=numpy.nanstd(data1), n=numpy.sum(~numpy.isnan(data1))) msg2 = '{v}: $mean={a}$ $median={d}$ $std={s}$ $N={n}$'.format( v=label2, a=numpy.nanmean(data2), d=numpy.nanmedian(data2), s=numpy.nanstd(data2), n=numpy.sum(~numpy.isnan(data2))) axis_main.text(0.02, 0.94, msg1, transform=axis_main.transAxes, fontsize=11, fontweight='bold', color='#8080ff', bbox=props1) axis_main.text(0.02, 0.86, msg2, transform=axis_main.transAxes, fontsize=11, fontweight='bold', color='#ff8080', bbox=props2) tmin, tmax = axis_main.get_xlim() # gaps axis_avail = pyplot.subplot(gs[7, :]) xmin, xmax = numpy.nanmin(data1), numpy.nanmax(data1) ymin, ymax = numpy.nanmin(data2), numpy.nanmax(data2) vmin, vmax = min(xmin, ymin), max(xmax, ymax) m1 = numpy.isnan(data1) m2 = numpy.isnan(data2) axis_avail.vlines(timestamp_list, ymin=m1 * vmin, ymax=m1 * vmax, linewidth=0.1, color='blue', alpha=0.5) axis_avail.vlines(timestamp_list, ymin=m2 * vmin, ymax=m2 * vmax, linewidth=0.1, color='red', alpha=0.5) axis_avail.set_ylabel('gaps') axis_avail.set_ylim(ymin, ymax) axis_avail.tick_params(bottom='off', top='off', left='off', right='off', labelleft='off', labelbottom='off') if numpy.sum(m1) + numpy.sum(m2) == 0: axis_avail.text(0.5, 0.25, 'NO GAPS', transform=axis_avail.transAxes, fontsize=16, color='black') axis_avail.set_xlim(tmin, tmax) # difference axis_diff = pyplot.subplot(gs[8:11, :]) axis_diff.axhline(linewidth=0.7, linestyle='-', color='black') data_zero = numpy.zeros_like(data1) data_diff = data1 - data2 axis_diff.fill_between(timestamp_list, data_zero, data_diff, where=data_diff >= data_zero, color='#8080ff', alpha=1.0) axis_diff.fill_between(timestamp_list, data_zero, data_diff, where=data_diff <= data_zero, color='#ff8080', alpha=1.0) axis_diff.set_ylabel('difference') axis_diff.tick_params(labelbottom='off') axis_diff.set_xlim(tmin, tmax) yticks = axis_diff.get_yticks().tolist() yticks = [abs(i) for i in yticks] axis_diff.set_yticklabels(yticks) # regression gradient, intercept, r_value, p_value, std_err = stats.linregress( data1[mask], data2[mask]) rsq = r_value * r_value ymin_r, ymax_r = (gradient * xmin + intercept, gradient * xmax + intercept) diff = (vmax - vmin) * 0.1 vmin, vmax = vmin - diff, vmax + diff axis_regr = pyplot.subplot(gs[11:, 0]) axis_regr.plot((vmin, vmax), (vmin, vmax), linestyle='-', linewidth=1, marker='', markersize=4, color='black', markeredgecolor='black', alpha=1.0) axis_regr.plot(data1, data2, linewidth=1.0, linestyle='', marker='.', markersize=3, color='#559977', markeredgecolor='#559977', alpha=1.0) axis_regr.plot((xmin, xmax), (ymin_r, ymax_r), linestyle='-', linewidth=1, marker='', markersize=4, color='red', markeredgecolor='red', alpha=1.0) axis_regr.set_xlim(vmin, vmax) axis_regr.set_ylim(vmin, vmax) axis_regr.set_xlabel(label1) axis_regr.set_ylabel(label2) axis_regr.text(0.9, 0.96, '1:1', transform=axis_regr.transAxes, fontsize=10, color='black') props = dict(boxstyle='round', facecolor='#eae3dd', edgecolor='none', alpha=0.7) msgr = '$y={g:.4f}*x {sig} {i:.4f}$\n$r^2={r:.4f}$'.format( r=rsq, g=gradient, i=abs(intercept), sig=('-' if intercept < 0 else '+')) axis_regr.text(0.04, 0.88, msgr, transform=axis_regr.transAxes, fontsize=12, color='#997755', bbox=props) # histogram (density) axis_hist = pyplot.subplot(gs[11:, 1]) hist_range = [vmin, vmax] h1, bins1, patches1 = axis_hist.hist(data1, bins=80, histtype='stepfilled', range=hist_range, normed=True, color='blue', edgecolor='none', alpha=0.5, label=label1) h2, bins2, patches2 = axis_hist.hist(data2, bins=80, histtype='stepfilled', range=hist_range, normed=True, color='red', edgecolor='none', alpha=0.5, label=label2) axis_hist.set_ylabel('probability density') legend = axis_hist.legend(fancybox=True) legend.get_frame().set_alpha(0.7) legend.get_frame().set_edgecolor('none') for leg in legend.legendHandles: leg.set_edgecolor('none') # histogram (cumulative density) axis_cumden = pyplot.subplot(gs[11:, 2]) hist_range = [vmin, vmax] h1, bins1, patches1 = axis_cumden.hist(data1, bins=200, cumulative=True, histtype='stepfilled', range=hist_range, normed=True, color='blue', edgecolor='none', alpha=0.5, label=label1) h2, bins2, patches2 = axis_cumden.hist(data2, bins=200, cumulative=True, histtype='stepfilled', range=hist_range, normed=True, color='red', edgecolor='none', alpha=0.5, label=label2) axis_cumden.set_ylim(0.0, 1.0) axis_cumden.set_ylabel('cumulative probability density') legend = axis_cumden.legend(loc='lower right', fancybox=True) legend.get_frame().set_alpha(0.7) legend.get_frame().set_edgecolor('none') for leg in legend.legendHandles: leg.set_edgecolor('none') # save figure if basename: if ('{l1}' in basename) and ('{l2}' in basename): figure_filename = os.path.abspath( (basename + '.png').format(l1=label1, l2=label2)) else: figure_filename = os.path.abspath('{b}__{l1}_{l2}.png'.format( b=basename, l1=label1, l2=label2)) canvas.print_figure(figure_filename, dpi=100) _log.info("Saved '{f}'".format(f=figure_filename)) # show interactive figure if show: pyplot.show() pyplot.close(figure)
def print_figure(fig, *args, **kwargs): canvas = FigureCanvasAgg(fig) canvas.print_figure(*args, **kwargs)
class beamsplitter_network: '''an object which describes a beamsplitter network''' def __init__(self, nmodes=None, json=None): self.nmodes = nmodes self.name = 'beamsplitter network' self.structure = [] self.phaseshifters = [] self.crossings = [] self.beamsplitters = [] self.input_modes = [] self.unitary = None if json != None: self.from_json(json) def from_json(self, json_filename): ''' build the structure ''' f = open(json_filename) jsondata = json.load(f, object_hook=json_no_unicode) f.close() self.nmodes = jsondata['modes'] self.name = jsondata['name'] self.width = jsondata['width'] things = jsondata['couplers'] + jsondata['shifters'] things = sorted(things, key=lambda thing: thing['x']) for thing in things: if 'phase' in thing: self.add_phaseshifter(thing['x'], thing['y']) elif 'ratio' in thing: self.add_beamsplitter(thing['x'], thing['y'], thing['ratio']) self.get_unitary() def get_ndof(self): '''get the number of degrees of freedom''' return len(self.phaseshifters) + len(self.beamsplitters) def set_input_modes(self, mode_list): ''' set the input modes ''' self.input_modes = mode_list def add_beamsplitter(self, x, y, splitting_ratio=.5): '''add a beamsplitter at position (x,y)''' bs = components.beamsplitter(x, y, len(self.beamsplitters), splitting_ratio) self.structure.append(bs) self.beamsplitters.append(bs) def add_crossing(self, x, y): cross = components.crossing(x, y, len(self.crossings)) self.structure.append(cross) self.crossings.append(cross) def add_phaseshifter(self, x, y, phase=0, invert=False): '''add a beamsplitter at position (x,y)''' ps = components.phaseshifter(x, y, len(self.phaseshifters), phase, invert) self.structure.append(ps) self.phaseshifters.append(ps) def set_phases(self, new_phases): ''' set the phases ''' for shifter, phase in zip(self.phaseshifters, new_phases): shifter.set_phi(phase) self.get_unitary() def set_splitting_ratios(self, new_splitting_ratios): ''' set the phases ''' for splitter, splitting_ratio in zip(self.beamsplitters, new_splitting_ratios): splitter.set_splitting_ratio(splitting_ratio) self.get_unitary() def set_parameters(self, p): ''' set all parameters''' nps = len(self.phaseshifters) self.set_phases(p[:nps]) self.set_splitting_ratios(p[nps:]) self.get_unitary() def get_unitary(self): ''' build the unitary ''' #TODO: this can be optimized by generating columns self.unitary = np.matrix(np.eye(self.nmodes), dtype=complex) for o in reversed(self.structure): u = np.matrix(np.eye(self.nmodes, dtype=complex)) u[o.y:o.y + 2, o.y:o.y + 2] = o.get_unitary() self.unitary *= u return self.unitary def __str__(self): ''' make a string representing the beamsplitter network ''' s = '%d-mode %s\n' % (self.nmodes, self.name) s += '%d phase shifters | ' % len(self.phaseshifters) s += '%d beam splitters | ' % len(self.beamsplitters) s += '%d degrees of freedom\n' % (len(self.phaseshifters) + len(self.beamsplitters)) for i, component in enumerate(self.structure): s += ' (#%d) %s\n' % (i, str(component)) return s def draw(self, filename='figures/out.pdf'): ''' draw the thing ''' #print 'drawing beamsplitter network...', if len(self.structure) == 0: max_x = 1 else: max_x = max([q.x for q in self.structure]) # build a figure and some axes self.figure = Figure(figsize=(10 * (max_x + 2) / 10., 5 * self.nmodes / 10.)) self.canvas = FigureCanvas(self.figure) self.axes = self.figure.add_subplot(111) self.axes.axis('off') # draw and label the modes for i in range(self.nmodes): self.axes.plot([-1, max_x + 2], [i, i], '-', color='#cccccc') self.axes.text(-1.2, i, '%d' % i, va='center', fontsize=8, ha='center') self.axes.text(max_x + 4 - 1.8, i, '%d' % i, va='center', fontsize=8, ha='center') #if i in self.inputs: self.axes.plot([-1.5], [i], 'ro') # draw the input photons for offset, group in enumerate(self.input_modes): x = -1.9 + offset * .1 self.axes.plot([x, x], [0, self.nmodes - 1], '-', color='#ffcccc') old_g = None for g in group: if g == old_g: x += .05 else: x = -1.9 + offset * .1 self.axes.plot(x, g, 'r.') old_g = g # draw all the beamsplitters and phase shifters for object in self.structure: object.draw(axis=self.axes, text=self.nmodes < 20) self.axes.set_ylim((self.nmodes - .5), -1) self.axes.set_xlim(-2, max_x + 3) self.axes.set_aspect(.5) self.canvas.print_figure(filename, bbox_inches='tight')
L1_time_end = L1_Amp.index[0] + timedelta(seconds=1800*(n+1)) L1_amp_trimmed = L1_Amp[(L1_Amp.index>L1_time_start)&(L1_Amp.index<L1_time_end)] axes[n].plot(L1_amp_trimmed.index, L1_amp_trimmed.values, c="purple", lw=2) L2_time_start = L2_Amp.index[0] + timedelta(seconds=1800*n) L2_time_end = L2_Amp.index[0] + timedelta(seconds=1800*(n+1)) L2_amp_trimmed = L2_Amp[(L2_Amp.index>L2_time_start)&(L2_Amp.index<L2_time_end)] axes[n].plot(L2_amp_trimmed.index, L2_amp_trimmed.values, c="orange", lw=2) # amp_plot_limit = min(4000, max(max(L1_amp_trimmed.values)*1.1, max(L2_amp_trimmed.values)*1.1)) amp_plot_limit = 4000 axes[n].set_ylim(0, amp_plot_limit) plot_start_time = min(L2_time_start, L1_time_start) plot_end_time = max(L2_time_end, L1_time_end) axes[n].set_xlim(min(L2_time_start, L1_time_start), max(L2_time_end, L1_time_end)) for classified_event in clean_final_intervals: if classified_event[6] > plot_start_time and classified_event[6] < plot_end_time: axes[n].plot([classified_event[6], classified_event[6]], [0, amp_plot_limit], color="green", lw=2) axes[n].text(classified_event[6], uniform(0.5*amp_plot_limit, 0.85*amp_plot_limit), classified_event[3], va="bottom", ha="left", color="green") if classified_event[7] > plot_start_time and classified_event[7] < plot_end_time: axes[n].plot([classified_event[7], classified_event[7]], [0, amp_plot_limit], color="red", lw=2) axes[n].text(classified_event[7], uniform(0.15*amp_plot_limit, 0.5*amp_plot_limit), classified_event[3], va="bottom", ha="right", color="red") axes[n].set_ylabel("Line Amplitudes") axes[n].set_xlabel("Timestamp") canvas = FigureCanvas(fig) canvas.print_figure("classified_series/" + datafilename.split(".")[0] + "_classified.png", dpi=72, bbox_inches='tight') close("all")
def generate_plots_of_coverage(): universal_cds_coverage=cPickle.load(open("coverage_calc_output/uni_pickled_coverage","rb")) universal_cds_coverage.sort(key=operator.itemgetter(0)) tet_cds_coverage=cPickle.load(open("coverage_calc_output/tet_pickled_coverage","rb")) tet_cds_coverage.sort(key=operator.itemgetter(0)) complete_e_bin_coverage_array = accessions_to_complete_coverage("plotting/bin_accessions/phy_check_e_bin_universal_accessions", "plotting/bin_accessions/phy_check_e_bin_tet_accessions", universal_cds_coverage, tet_cds_coverage) complete_h_bin_coverage_array = accessions_to_complete_coverage("plotting/bin_accessions/phy_check_h_bin_universal_accessions", "plotting/bin_accessions/phy_check_h_bin_tet_accessions", universal_cds_coverage, tet_cds_coverage) secretome_pipeline_e_bin_coverage = accessions_to_complete_coverage("plotting/bin_accessions/Secretome_pipeline_e_bin_uni_accessions", "plotting/bin_accessions/Secretome_pipeline_e_bin_tet_accessions", universal_cds_coverage, tet_cds_coverage) secretome_pipeline_h_bin_coverage = accessions_to_complete_coverage("plotting/bin_accessions/Secretome_pipeline_h_bin_uni_accessions", "plotting/bin_accessions/Secretome_pipeline_h_bin_tet_accessions", universal_cds_coverage, tet_cds_coverage) tmhmm_1_or_more_e_bin_coverage = accessions_to_complete_coverage("plotting/bin_accessions/TMHMM_1ormorepredhel_e_bin_uni_accessions", "plotting/bin_accessions/TMHMM_1ormorepredhel_e_bin_tet_accessions", universal_cds_coverage, tet_cds_coverage) tmhmm_1_or_more_h_bin_coverage = accessions_to_complete_coverage("plotting/bin_accessions/TMHMM_1ormorepredhel_h_bin_uni_accessions", "plotting/bin_accessions/TMHMM_1ormorepredhel_h_bin_tet_accessions", universal_cds_coverage, tet_cds_coverage) tmhmm_4_or_more_e_bin_coverage = accessions_to_complete_coverage("plotting/bin_accessions/TMHMM_4ormorepredhel_e_bin_uni_accessions", "plotting/bin_accessions/TMHMM_4ormorepredhel_e_bin_tet_accessions", universal_cds_coverage, tet_cds_coverage) tmhmm_4_or_more_h_bin_coverage = accessions_to_complete_coverage("plotting/bin_accessions/TMHMM_4ormorepredhel_h_bin_uni_accessions", "plotting/bin_accessions/TMHMM_4ormorepredhel_h_bin_tet_accessions", universal_cds_coverage, tet_cds_coverage) sigp_targetp_or_wolfsortp_e_bin_coverage = accessions_to_complete_coverage("plotting/bin_accessions/SigP_TargetP_or_WolfSortP_e_bin_uni_accessions", "plotting/bin_accessions/SigP_TargetP_or_WolfSortP_e_bin_tet_accessions", universal_cds_coverage, tet_cds_coverage) sigp_targetp_or_wolfsortp_h_bin_coverage = accessions_to_complete_coverage("plotting/bin_accessions/SigP_TargetP_or_WolfSortP_h_bin_uni_accessions", "plotting/bin_accessions/SigP_TargetP_or_WolfSortP_h_bin_tet_accessions", universal_cds_coverage, tet_cds_coverage) (e_bin_five_prime_pos_av_coverages, e_bin_five_prime_pos_std_dev, e_bin_three_prime_pos_av_coverages, e_bin_three_prime_pos_std_dev) = calculate_posistional_average(complete_e_bin_coverage_array) (h_bin_five_prime_pos_av_coverage, h_bin_five_prime_pos_std_dev, h_bin_three_prime_pos_av_coverage, h_bin_three_prime_pos_std_dev) = calculate_posistional_average(complete_h_bin_coverage_array) (uni_total_five_prime_pos_av_coverage, uni_total_five_prime_pos_std_dev, uni_total_three_prime_pos_av_coverage, uni_total_three_prime_pos_std_dev) = calculate_posistional_average(universal_cds_coverage) (tet_total_five_prime_pos_av_coverage, tet_total_five_prime_pos_std_dev, tet_total_three_prime_pos_av_coverage, tet_total_three_prime_pos_std_dev) = calculate_posistional_average(tet_cds_coverage) (secretome_pipeline_e_bin_five_prime_pos_av_coverage, secretome_pipeline_e_bin_five_prime_pos_std_dev, secretome_pipeline_e_bin_three_prime_pos_av_coverage, secretome_pipeline_e_bin_three_prime_pos_std_dev) = calculate_posistional_average(secretome_pipeline_e_bin_coverage) (secretome_pipeline_h_bin_five_prime_pos_av_coverage, secretome_pipeline_h_bin_five_prime_pos_std_dev, secretome_pipeline_h_bin_three_prime_pos_av_coverage, secretome_pipeline_h_bin_three_prime_pos_std_dev) = calculate_posistional_average(secretome_pipeline_h_bin_coverage) (tmhmm_1_or_more_e_bin_five_prime_pos_av_coverage, tmhmm_1_or_more_e_bin_five_prime_pos_std_dev, tmhmm_1_or_more_e_bin_three_prime_pos_average_coverage, tmhmm_1_or_more_e_bin_three_prime_pos_std_dev) = calculate_posistional_average(tmhmm_1_or_more_e_bin_coverage) (tmhmm_1_or_more_h_bin_five_prime_pos_av_coverage, tmhmm_1_or_more_h_bin_five_prime_pos_std_dev, tmhmm_1_or_more_h_bin_three_prime_pos_av_coverage, tmhmm_1_or_more_h_bin_three_prime_pos_std_dev) = calculate_posistional_average(tmhmm_1_or_more_h_bin_coverage) (tmhmm_4_or_more_e_bin_five_prime_pos_av_coverage, tmhmm_4_or_more_e_bin_five_prime_pos_std_dev, tmhmm_4_or_more_e_bin_three_prime_pos_av_coverage, tmhmm_4_or_more_e_bin_three_prime_pos_std_dev) = calculate_posistional_average(tmhmm_4_or_more_e_bin_coverage) (tmhmm_4_or_more_h_bin_five_prime_pos_av_coverage, tmhmm_4_or_more_h_bin_five_prime_pos_std_dev, tmhmm_4_or_more_h_bin_three_prime_pos_av_coverage, tmhmm_4_or_more_h_bin_three_prime_pos_std_dev) = calculate_posistional_average(tmhmm_4_or_more_h_bin_coverage) (sigp_targetp_or_wolfsortp_e_bin_five_prime_pos_av_coverage, sigp_targetp_or_wolfsortp_e_bin_five_prime_pos_std_dev, sigp_targetp_or_wolfsortp_e_bin_three_prime_pos_av_coverage, sigp_targetp_or_wolfsortp_e_bin_three_prime_pos_std_dev) = calculate_posistional_average(sigp_targetp_or_wolfsortp_e_bin_coverage) (sigp_targetp_or_wolfsortp_h_bin_five_prime_pos_av_coverage, sigp_targetp_or_wolfsortp_h_bin_five_prime_pos_std_dev, sigp_targetp_or_wolfsortp_h_bin_three_prime_pos_av_coverage, sigp_targetp_or_wolfsortp_h_bin_three_prime_pos_std_dev) = calculate_posistional_average(sigp_targetp_or_wolfsortp_h_bin_coverage) fig=Figure(figsize=(25,30)) canvas=FigureCanvas(fig) plotting(fig, [e_bin_five_prime_pos_av_coverages, h_bin_five_prime_pos_av_coverage, uni_total_five_prime_pos_av_coverage, tet_total_five_prime_pos_av_coverage], [range(1,101)]*4, 321, "Average Posistional 5' CDS Coverage\n\n Complete Bins", "Average Number of Reads Mapping to CDS Posistion", "Relative posistion in CDS from 5'", ["Endosymbiont Bin CDS [n=3498]", "Host Bin CDS [n=27482]", "All Universal Encoding CDS [n=10997]", "All Tetrahymena Encoding CDS [n=47101]"], ['green','blue','magenta','cyan'], [0,100,0,125]) plotting(fig, [e_bin_three_prime_pos_av_coverages, h_bin_three_prime_pos_av_coverage, uni_total_three_prime_pos_av_coverage, tet_total_three_prime_pos_av_coverage], [range(-99,1)]*4, 322, "Average Posistional 3' CDS Coverage\n\n Complete Bins", " ", "Relative Posistion in CDS from 3'", ["Endosymbiont Bin CDS [n=3498]", "Host Bin CDS [n=27482]", "All Universal Encoding CDS [n=10997]", "All Tetrahymena Encoding CDS [n=47101]"], ['green','blue','magenta','cyan'], [-100,0,0,125]) plotting(fig, [secretome_pipeline_e_bin_five_prime_pos_av_coverage, secretome_pipeline_h_bin_five_prime_pos_av_coverage, sigp_targetp_or_wolfsortp_e_bin_five_prime_pos_av_coverage, sigp_targetp_or_wolfsortp_h_bin_five_prime_pos_av_coverage], [range(1,101)]*4, 323, "Putative Secreted Proteins", "Average Number of Reads Mapping to CDS Posistion", "Relative Posistion in CDS from 5'", ["Secretome Pipeline Output Endosymbiont Bin [n=14]", "Secretome Pipeline Output Host Bin [n=221]", "SigP TargetP or WolfSortP secreted sequences Endosymbiont Bin [n=160]", "SigP TargetP or WolfSortP secreted sequences Host Bin [n=1448]"], ['yellow', 'red', 'purple', 'cyan'], [0,100,0,125]) plotting(fig, [secretome_pipeline_e_bin_three_prime_pos_av_coverage, secretome_pipeline_h_bin_three_prime_pos_av_coverage, sigp_targetp_or_wolfsortp_e_bin_three_prime_pos_av_coverage, sigp_targetp_or_wolfsortp_h_bin_three_prime_pos_av_coverage], [range(-99,1)]*4, 324, "Putative Secreted Proteins", " ", "Rleative Posistion in CDS from 3'", ["Secretome Pipeline Output Endosymbiont Bin [n=14]", "Secretome Pipeline Output Host Bin [n=221]", "SigP TargetP or WolfSortP secreted sequences Endosymbiont Bin [n=160]", "SigP TargetP or WolfSortP secreted sequences Host Bin [n=1448]"], ["yellow","red", "purple", "cyan"], [-100,0,0,125]) plotting(fig, [tmhmm_4_or_more_e_bin_five_prime_pos_av_coverage, tmhmm_4_or_more_h_bin_five_prime_pos_av_coverage, tmhmm_1_or_more_e_bin_five_prime_pos_av_coverage, tmhmm_1_or_more_h_bin_five_prime_pos_av_coverage], [range(1,101)]*4, 325, "Putative Transporter Proteins", "Average Number of Reads Mapping to CDS Posistion", "Relative Posistion in CDS from 5'", ["Sequence with 4 or more TM domain Endosymbiont Bin [n=37]", "Sequence with 4 or more TM domain Host Bin [n=2226]", "Sequences with a TM domain Endosymbiont Bin [n=310]", "Sequences with a TM domain Host Bin [n=7238]"], ['green', 'blue', 'red', 'black'], [0,100,0,125]) plotting(fig, [tmhmm_4_or_more_e_bin_three_prime_pos_av_coverage, tmhmm_4_or_more_h_bin_three_prime_pos_av_coverage, tmhmm_1_or_more_e_bin_three_prime_pos_average_coverage, tmhmm_1_or_more_h_bin_three_prime_pos_av_coverage], [range(-99,1)]*4, 326, "Putative Transport Proteins", " ", "Relative Posistion in CDS from 3'", ["Sequence with 4 or more TM domain Endosymbiont Bin [n=37]", "Sequence with 4 or more TM domain Host Bin [n=2226]", "Sequences with a TM domain Endosymbiont Bin [n=310]", "Sequences with a TM domain Host Bin [n=7238]"], ['green', 'blue', 'red', 'black'], [-100,0,0,125]) fig.suptitle("Average Terminal Coverage for subsets of Paramecium-Chlorella RNA-Seq Data", fontsize=12) canvas.print_figure('plot_output/coverage_plot_median.svg') canvas.print_figure('plot_output/coverage_plot_median.png', dpi=500)
def listen_for_remote(self): while self.parent_conn.poll(None): try: (msg, payload) = self.parent_conn.recv() except EOFError: return if msg != Msg.MOUSE_MOVE_EVENT: logger.debug( "FigureCanvasAggRemote.listen_for_remote :: {}".format( msg, payload)) try: if msg == Msg.DPI: dpi = payload matplotlib.rcParams['figure.dpi'] = dpi matplotlib.pyplot.clf() elif msg == Msg.RESIZE_EVENT: with self.plot_lock: (winch, hinch) = payload self.figure.set_size_inches(winch, hinch) FigureCanvasAgg.resize_event(self) self.draw() elif msg == Msg.MOUSE_PRESS_EVENT: (x, y, button) = payload if self.process_events.is_set(): with self.plot_lock: FigureCanvasAgg.button_press_event( self, x, y, button) elif msg == Msg.MOUSE_DOUBLE_CLICK_EVENT: (x, y, button) = payload if self.process_events.is_set(): with self.plot_lock: FigureCanvasAgg.button_press_event(self, x, y, button, dblclick=True) elif msg == Msg.MOUSE_RELEASE_EVENT: (x, y, button) = payload if self.process_events.is_set(): with self.plot_lock: FigureCanvasAgg.button_release_event( self, x, y, button) elif msg == Msg.MOUSE_MOVE_EVENT: (x, y) = payload if self.process_events.is_set(): with self.plot_lock: FigureCanvasAgg.motion_notify_event(self, x, y) elif msg == Msg.PRINT: (args, kwargs) = payload if self.process_events.is_set(): with self.plot_lock: old_size = self.figure.get_size_inches() width = kwargs.pop('width') height = kwargs.pop('height') self.figure.set_size_inches(width, height) FigureCanvasAgg.print_figure(self, *args, **kwargs) self.figure.set_size_inches( old_size[0], old_size[1]) else: raise RuntimeError( "FigureCanvasAggRemote received bad message {}".format( msg)) except Exception: log_exception()
def getImage(self): ddict = self.fitresult try: fig = Figure(figsize=(6, 3)) # in inches canvas = FigureCanvas(fig) ax = fig.add_axes([.15, .15, .8, .8]) ax.set_axisbelow(True) logplot = self.plotDict.get('logy', True) if logplot: axplot = ax.semilogy else: axplot = ax.plot axplot(ddict['result']['energy'], ddict['result']['ydata'], 'k', lw=1.5) axplot(ddict['result']['energy'], ddict['result']['continuum'], 'g', lw=1.5) legendlist = ['spectrum', 'continuum', 'fit'] axplot(ddict['result']['energy'], ddict['result']['yfit'], 'r', lw=1.5) fontproperties = FontProperties(size=8) if ddict['result']['config']['fit']['sumflag']: axplot(ddict['result']['energy'], ddict['result']['pileup'] + ddict['result']['continuum'], 'y', lw=1.5) legendlist.append('pileup') if matplotlib_version < '0.99.0': legend = ax.legend(legendlist, 0, prop=fontproperties, labelsep=0.02) elif matplotlib_version < '1.5': legend = ax.legend(legendlist, 0, prop=fontproperties, labelspacing=0.02) else: legend = ax.legend(legendlist, loc=0, prop=fontproperties, labelspacing=0.02) except ValueError: fig = Figure(figsize=(6, 3)) # in inches canvas = FigureCanvas(fig) ax = fig.add_axes([.15, .15, .8, .8]) ax.set_axisbelow(True) ax.plot(ddict['result']['energy'], ddict['result']['ydata'], 'k', lw=1.5) ax.plot(ddict['result']['energy'], ddict['result']['continuum'], 'g', lw=1.5) legendlist = ['spectrum', 'continuum', 'fit'] ax.plot(ddict['result']['energy'], ddict['result']['yfit'], 'r', lw=1.5) fontproperties = FontProperties(size=8) if ddict['result']['config']['fit']['sumflag']: ax.plot(ddict['result']['energy'], ddict['result']['pileup'] + ddict['result']['continuum'], 'y', lw=1.5) legendlist.append('pileup') if matplotlib_version < '0.99.0': legend = ax.legend(legendlist, 0, prop=fontproperties, labelsep=0.02) elif matplotlib_version < '1.5': legend = ax.legend(legendlist, 0, prop=fontproperties, labelspacing=0.02) else: legend = ax.legend(legendlist, loc=0, prop=fontproperties, labelspacing=0.02) ax.set_xlabel('Energy') ax.set_ylabel('Counts') legend.draw_frame(False) outfile = self.outdir + "/" + self.outfile + ".png" try: os.remove(outfile) except: pass canvas.print_figure(outfile) return self.__getFitImage(self.outfile + ".png")
http://matplotlib.org/api/colors_api.html """ import numpy as np import matplotlib from matplotlib import figure from matplotlib.backends.backend_agg import ( FigureCanvasAgg as FigureCanvas) import matplotlib.cm as mcm import matplotlib.colors as mcolors a = np.random.permutation(np.arange(10)) b = np.random.permutation(np.arange(10)) c = np.random.permutation(np.arange(10)) d = np.random.permutation(np.arange(0,10)) fig = figure.Figure() canvas = FigureCanvas(fig) cmap = mcm.spring norm = mcolors.BoundaryNorm(np.arange(11), cmap.N) ax = fig.add_subplot(1,1,1) scat = ax.scatter(a,b,c=c, s=(d+10)*50, cmap=cmap,norm=norm) fig.colorbar(scat, ax=ax, fraction=.45) canvas.print_figure('../figures/cmapbubble.png', facecolor='lightgray')
def doChooch(self, scanObject, elt, edge, scanArchiveFilePrefix, scanFilePrefix): symbol = "_".join((elt, edge)) scanArchiveFilePrefix = "_".join((scanArchiveFilePrefix, symbol)) logging.getLogger("HWR").exception("running chooch in mxcube (%s)" % scanArchiveFilePrefix) i = 1 while os.path.isfile(os.path.extsep.join((scanArchiveFilePrefix + str(i), "raw"))): i = i + 1 scanArchiveFilePrefix = scanArchiveFilePrefix + str(i) archiveRawScanFile=os.path.extsep.join((scanArchiveFilePrefix, "raw")) rawScanFile=os.path.extsep.join((scanFilePrefix, "raw")) scanFile=os.path.extsep.join((scanFilePrefix, "efs")) if not os.path.exists(os.path.dirname(scanArchiveFilePrefix)): os.makedirs(os.path.dirname(scanArchiveFilePrefix)) try: f=open(rawScanFile, "w") pyarch_f=open(archiveRawScanFile, "w") except: logging.getLogger("HWR").exception("could not create raw scan files") self.storeEnergyScan() self.emit("energyScanFailed", ()) return else: scanData = [] if scanObject is None: raw_data_file = os.path.join(os.path.dirname(scanFilePrefix), 'data.raw') try: raw_file = open(raw_data_file, 'r') except: self.storeEnergyScan() self.emit("energyScanFailed", ()) return for line in raw_file.readlines()[2:]: (x, y) = line.split('\t') x = float(x.strip()) y = float(y.strip()) x = x < 1000 and x*1000.0 or x scanData.append((x, y)) f.write("%f,%f\r\n" % (x, y)) pyarch_f.write("%f,%f\r\n"% (x, y)) else: for i in range(len(scanObject.x)): x = float(scanObject.x[i]) x = x < 1000 and x*1000.0 or x y = float(scanObject.y[i]) scanData.append((x, y)) f.write("%f,%f\r\n" % (x, y)) pyarch_f.write("%f,%f\r\n"% (x, y)) f.close() pyarch_f.close() self.scanInfo["scanFileFullPath"]=str(archiveRawScanFile) logging.getLogger("HWR").info("th. Edge %s ; calculating results with chooch " % (self.thEdge)) pk, fppPeak, fpPeak, ip, fppInfl, fpInfl, chooch_graph_data = PyChooch.calc(scanData, elt, edge, scanFile) rm=(pk+30)/1000.0 pk=pk/1000.0 savpk = pk ip=ip/1000.0 comm = "" logging.getLogger("HWR").info("th. Edge %s ; chooch results are pk=%f, ip=%f, rm=%f" % (self.thEdge, pk,ip,rm)) if math.fabs(self.thEdge - ip) > self.thEdgeThreshold: pk = 0 ip = 0 rm = self.thEdge + 0.03 comm = 'Calculated peak (%f) is more that 10eV away from the theoretical value (%f). Please check your scan' % (savpk, self.thEdge) logging.getLogger("HWR").warning('EnergyScan: calculated peak (%f) is more that 20eV %s the theoretical value (%f). Please check your scan and choose the energies manually' % (savpk, (self.thEdge - ip) > 0.02 and "below" or "above", self.thEdge)) archiveEfsFile=os.path.extsep.join((scanArchiveFilePrefix, "efs")) try: fi=open(scanFile) fo=open(archiveEfsFile, "w") except: self.storeEnergyScan() self.emit("energyScanFailed", ()) return else: fo.write(fi.read()) fi.close() fo.close() self.scanInfo["peakEnergy"]=pk self.scanInfo["inflectionEnergy"]=ip self.scanInfo["remoteEnergy"]=rm self.scanInfo["peakFPrime"]=fpPeak self.scanInfo["peakFDoublePrime"]=fppPeak self.scanInfo["inflectionFPrime"]=fpInfl self.scanInfo["inflectionFDoublePrime"]=fppInfl self.scanInfo["comments"] = comm chooch_graph_x, chooch_graph_y1, chooch_graph_y2 = zip(*chooch_graph_data) chooch_graph_x = list(chooch_graph_x) for i in range(len(chooch_graph_x)): chooch_graph_x[i]=chooch_graph_x[i]/1000.0 logging.getLogger("HWR").info("<chooch> Saving png" ) # prepare to save png files title="%10s %6s %6s\n%10s %6.2f %6.2f\n%10s %6.2f %6.2f" % ("energy", "f'", "f''", pk, fpPeak, fppPeak, ip, fpInfl, fppInfl) fig=Figure(figsize=(15, 11)) ax=fig.add_subplot(211) ax.set_title("%s\n%s" % (scanFile, title)) ax.grid(True) ax.plot(*(zip(*scanData)), **{"color":'black'}) ax.set_xlabel("Energy") ax.set_ylabel("MCA counts") ax2=fig.add_subplot(212) ax2.grid(True) ax2.set_xlabel("Energy") ax2.set_ylabel("") handles = [] handles.append(ax2.plot(chooch_graph_x, chooch_graph_y1, color='blue')) handles.append(ax2.plot(chooch_graph_x, chooch_graph_y2, color='red')) canvas=FigureCanvasAgg(fig) escan_png = os.path.extsep.join((scanFilePrefix, "png")) escan_archivepng = os.path.extsep.join((scanArchiveFilePrefix, "png")) self.scanInfo["jpegChoochFileFullPath"]=str(escan_archivepng) try: logging.getLogger("HWR").info("Rendering energy scan and Chooch graphs to PNG file : %s", escan_png) canvas.print_figure(escan_png, dpi=80) except: logging.getLogger("HWR").exception("could not print figure") try: logging.getLogger("HWR").info("Saving energy scan to archive directory for ISPyB : %s", escan_archivepng) canvas.print_figure(escan_archivepng, dpi=80) except: logging.getLogger("HWR").exception("could not save figure") self.storeEnergyScan() self.scanInfo=None logging.getLogger("HWR").info("<chooch> returning" ) self.emit('chooch_finished', (pk, fppPeak, fpPeak, ip, fppInfl, fpInfl, rm, chooch_graph_x, chooch_graph_y1, chooch_graph_y2, title)) return pk, fppPeak, fpPeak, ip, fppInfl, fpInfl, rm, chooch_graph_x, chooch_graph_y1, chooch_graph_y2, title
class h5plot(object): def __init__(self, h5_filepath, comment='', save_pdf=False): self.comment = comment self.save_pdf = save_pdf self.path = h5_filepath filepath = os.path.abspath( self.path) #put filepath to platform standards self.filedir = os.path.dirname( filepath ) #return directory component of the given pathname, here filepath self.image_dir = os.path.join(self.filedir, 'images') try: os.mkdir(self.image_dir) except OSError: logging.warning('Error creating image directory.') pass # open the h5 file and get the hdf_lib object self.hf = hdf_lib.Data(path=self.path) # check for datasets for i, pentry in enumerate(self.hf['/entry'].keys()): key = '/entry/' + pentry for j, centry in enumerate(self.hf[key].keys()): try: self.key = '/entry/' + pentry + "/" + centry self.ds = self.hf[self.key] if self.ds.attrs.get('save_plot', True): self.plt() except Exception as e: print "Exception in qkit/gui/plot/plot.py while plotting" print self.key print e #close hf file self.hf.close() print 'Plots saved in', self.image_dir def plt(self): logging.info(" -> plotting dataset: " + str(self.ds.attrs.get('name'))) self.ds_type = self.ds.attrs.get('ds_type', '') self.x_ds_url = self.ds.attrs.get('x_ds_url', '') self.y_ds_url = self.ds.attrs.get('y_ds_url', '') self.z_ds_url = self.ds.attrs.get('z_ds_url', '') self.fig = Figure(figsize=(20, 10), tight_layout=True) self.ax = self.fig.gca() self.ax.set_title(self.hf._filename[:-3]) self.canvas = FigureCanvas(self.fig) if self.ds_type == ds_types['coordinate']: #self.plt_coord() return elif self.ds_type == ds_types['vector']: self.plt_vector() elif self.ds_type == ds_types['matrix']: self.plt_matrix() elif self.ds_type == ds_types['box']: self.plt_box() elif self.ds_type == ds_types['txt']: #self.plt_txt() return elif self.ds_type == ds_types['view']: #self.plt_view() return else: return self.ax.set_xlabel(self.x_label) self.ax.set_ylabel(self.y_label) self.ax.xaxis.label.set_fontsize(20) self.ax.yaxis.label.set_fontsize(20) self.ax.ticklabel_format(useOffset=False) for i in self.ax.get_xticklabels(): i.set_fontsize(16) for i in self.ax.get_yticklabels(): i.set_fontsize(16) save_name = str( os.path.basename(self.filedir))[0:6] + '_' + self.key.replace( '/entry/', '').replace('/', '_') if self.comment: save_name = save_name + '_' + self.comment image_path = str(os.path.join(self.image_dir, save_name)) if self.save_pdf: self.canvas.print_figure(image_path + '.pdf') self.canvas.print_figure(image_path + '.png') """ except Exception as e: print "Exception in qkit/gui/plot/plot.py" print e """ def plt_vector(self): """ dataset is only one-dimensional print data vs. x-coordinate """ self.ds_label = self.ds.attrs.get( 'name', '_name_') + ' / ' + self.ds.attrs.get('unit', '_unit_') self.data_y = np.array(self.ds) self.y_label = self.ds_label try: self.x_ds = self.hf[self.x_ds_url] self.x_label = self.x_ds.attrs.get( 'name', '_xname_') + ' / ' + self.x_ds.attrs.get( 'unit', '_xunit_') except Exception: self.x_ds = np.arange(len(self.data_y)) self.x_label = '_none_ / _none_' if len(self.data_y) == 1: #only one entry, print as cross plt_style = 'x' else: plt_style = '-' try: self.ax.plot( self.x_ds, self.data_y[0:len(self.x_ds)], plt_style ) #JB: avoid crash after pressing the stop button when arrays are of different lengths except TypeError: self.ax.plot(0, self.data_y, plt_style) def plt_matrix(self): """ dataset is two-dimensional print data color-coded y-coordinate vs. x-coordinate """ self.x_ds = self.hf[self.x_ds_url] self.y_ds = self.hf[self.y_ds_url] self.x_label = self.x_ds.attrs.get( 'name', '_xname_') + ' / ' + self.x_ds.attrs.get( 'unit', '_xunit_') self.y_label = self.y_ds.attrs.get( 'name', '_yname_') + ' / ' + self.y_ds.attrs.get( 'unit', '_yunit_') self.ds_label = self.ds.attrs.get( 'name', '_name_') + ' / ' + self.ds.attrs.get('unit', '_unit_') self.data = np.array( self.ds).T #transpose matrix to get x/y axis correct self.xmin = self.x_ds.attrs.get('x0', 0) self.dx = self.x_ds.attrs.get('dx', 1) self.xmax = self.xmin + self.dx * self.x_ds.shape[0] self.ymin = self.y_ds.attrs.get('x0', 0) self.dy = self.y_ds.attrs.get('dx', 1) self.ymax = self.ymin + self.dy * self.y_ds.shape[0] # downsweeps in any direction have to be corrected # this is triggered by dx/dy values < 0 # data-matrix and min/max-values have to be swapped if self.dx < 0: self.data = np.fliplr(self.data) self.xmin, self.xmax = self.xmax, self.xmin if self.dy < 0: self.data = np.flipud(self.data) self.ymin, self.ymax = self.ymax, self.ymin self.cax = self.ax.imshow( self.data, aspect='auto', extent=[self.xmin, self.xmax, self.ymin, self.ymax], origin='lower', vmin=self._get_vrange(self.data, 2)[0], vmax=self._get_vrange(self.data, 2)[1], interpolation='none', cmap=plt.get_cmap('Greys_r')) self.cbar = self.fig.colorbar(self.cax) self.cbar.ax.set_ylabel(self.ds_label) self.cbar.ax.yaxis.label.set_fontsize(20) for i in self.cbar.ax.get_yticklabels(): i.set_fontsize(16) def plt_box(self): """ dataset is two-dimensional print data color-coded y-coordinate vs. x-coordinate """ self.x_ds = self.hf[self.x_ds_url] self.y_ds = self.hf[self.y_ds_url] self.z_ds = self.hf[self.z_ds_url] self.x_label = self.x_ds.attrs.get( 'name', '_xname_') + ' / ' + self.x_ds.attrs.get( 'unit', '_xunit_') self.y_label = self.y_ds.attrs.get( 'name', '_yname_') + ' / ' + self.y_ds.attrs.get( 'unit', '_yunit_') self.ds_label = self.ds.attrs.get( 'name', '_name_') + ' / ' + self.ds.attrs.get('unit', '_unit_') self.nop = self.ds.shape[ 2] #S1 this was ->self.z_ds.shape[0]<- before, but causes problems for some data self.data = np.array( self.ds)[:, :, self.nop / 2].T #transpose matrix to get x/y axis correct self.xmin = self.x_ds.attrs.get('x0', 0) self.dx = self.x_ds.attrs.get('dx', 1) self.xmax = self.xmin + self.dx * self.x_ds.shape[0] self.ymin = self.y_ds.attrs.get('x0', 0) self.dy = self.y_ds.attrs.get('dx', 1) self.ymax = self.ymin + self.dy * self.y_ds.shape[0] # downsweeps in any direction have to be corrected # this is triggered by dx/dy values < 0 # data-matrix and min/max-values have to be swapped if self.dx < 0: self.data = np.fliplr(self.data) self.xmin, self.xmax = self.xmax, self.xmin if self.dy < 0: self.data = np.flipud(self.data) self.ymin, self.ymax = self.ymax, self.ymin self.cax = self.ax.imshow( self.data, aspect='auto', extent=[self.xmin, self.xmax, self.ymin, self.ymax], origin='lower', vmin=self._get_vrange(self.data, 2)[0], vmax=self._get_vrange(self.data, 2)[1], interpolation='none', cmap=plt.get_cmap('Greys_r')) self.cbar = self.fig.colorbar(self.cax) self.cbar.ax.set_ylabel(self.ds_label) self.cbar.ax.yaxis.label.set_fontsize(20) for i in self.cbar.ax.get_yticklabels(): i.set_fontsize(16) def plt_coord(self): # not (yet?) implemented. we'll see ... pass def plt_txt(self): # not (yet?) implemented. we'll see ... pass def plt_view(self): # not (yet?) implemented. we'll see ... pass def _get_vrange(self, data, percent): ''' This function calculates ranges for the colorbar to get rid of spikes in the data. If the data is evenly distributed, this should not change anything in your colorbar. ''' _min = np.percentile(data, percent) _max = np.percentile(data, 100 - percent) _min -= (_max - _min) * percent / (100. - 2 * percent) _max += (_max - _min) * percent / (100. - 2 * percent) return [_min, _max]
""" Example of matplotlib colormaps http://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure.colorbar """ import numpy as np import matplotlib from matplotlib import figure from matplotlib.backends.backend_agg import (FigureCanvasAgg as FigureCanvas) data = np.random.randn(50, 50) fig = figure.Figure(figsize=(12, 6)) canvas = FigureCanvas(fig) #using kwargs ax1 = fig.add_subplot(1, 2, 1) plt1 = ax1.imshow(data, cmap='RdBu') fig.colorbar(plt1, ax=ax1, fraction=0.045) ax2 = fig.add_subplot(1, 2, 2) plt2 = ax2.imshow(data, cmap='RdBu') fig.colorbar(plt2, ax=ax2, fraction=0.04, orientation='horizontal') canvas.print_figure('../figures/movedcmap.png', facecolor='lightgray')
fig = Figure() canvas = FigureCanvas(fig) # create axes instance, leaving room for colorbar at bottom. ax = fig.add_axes([0.125,0.175,0.75,0.75]) # create Basemap instance for Robinson projection. # set 'ax' keyword so pylab won't be imported. m = Basemap(projection='robin',lon_0=0.5*(lons[0]+lons[-1]),ax=ax) # reset figure size to have same aspect ratio as map. # fig will be 8 inches wide. # (don't use createfigure, since that imports pylab). fig.set_figsize_inches((8,m.aspect*8.)) # make filled contour plot. x, y = m(*meshgrid(lons, lats)) cs = m.contourf(x,y,etopo,30,cmap=cm.jet) # draw coastlines. m.drawcoastlines() # draw a line around the map region. m.drawmapboundary() # draw parallels and meridians. m.drawparallels(nx.arange(-60.,90.,30.),labels=[1,0,0,0],fontsize=10) m.drawmeridians(nx.arange(0.,420.,60.),labels=[0,0,0,1],fontsize=10) # add a title. ax.set_title('Robinson Projection') # add a colorbar. cax = fig.add_axes([0.125, 0.05, 0.75, 0.05],frameon=False) fig.colorbar(cs, cax=cax, tickfmt='%d', orientation='horizontal',clabels=cs.levels[::3]) # save image (width 800 pixels with dpi=100 and fig width 8 inches). canvas.print_figure('simpletest',dpi=100) # done. print 'image saved in simpletest.png'
# Convery to a numpy array to allow for more elaborate array slicing pc = numpy.ma.array(p.pc()) print "Vt[0], weights that give PC 0:", p.Vt[0] print "A . Vt[0]:", dot( A, p.Vt[0] ) print "pc:", p.pc() import numpy from matplotlib.figure import Figure from matplotlib.backends.backend_agg import FigureCanvasAgg # Plot the first two PCA components fig = Figure(figsize=(8,6)) ax = fig.add_subplot(111) ax.scatter(pc[:, 0], pc[:, 1]) canvas = FigureCanvasAgg(fig) canvas.print_figure('pca.png') print "\nobs <-> pc <-> x: with fraction=1, diffs should be ~ 0" x = numpy.ones(K) # x = numpy.ones(( 3, K )) print "x:", x pc = p.vars_pc(x) # d' Vt' x print "vars_pc(x):", pc print "back to ~ x:", p.pc_vars(pc) Ax = dot( A, x.T ) pcx = p.obs(x) # U' d' Vt' x print "Ax:", Ax print "A'x:", pcx print "max |Ax - A'x|: %.2g" % numpy.linalg.norm( Ax - pcx, numpy.inf )
class PsychroChart: """Psychrometric chart object handler.""" def __init__(self, styles: Union[dict, str]=None, zones_file: Union[dict, str]=None, logger: Any=None, verbose: bool=False) -> None: """Create the PsychroChart object.""" self._logger = logger self._verbose = verbose self.d_config = {} # type: dict self.figure_params = {} # type: dict self.dbt_min = self.dbt_max = -100 self.w_min = self.w_max = -1 self.temp_step = 1. self.altitude_m = -1 self.chart_params = {} # type: dict self.p_atm_kpa = PRESSURE_STD_ATM_KPA self.constant_dry_temp_data = None # type: Optional[PsychroCurves] self.constant_humidity_data = None # type: Optional[PsychroCurves] self.constant_rh_data = None # type: Optional[PsychroCurves] self.constant_h_data = None # type: Optional[PsychroCurves] self.constant_v_data = None # type: Optional[PsychroCurves] self.constant_wbt_data = None # type: Optional[PsychroCurves] self.saturation = None # type: Optional[PsychroCurves] self.zones = [] # type: List self._fig = None # type: figure.Figure self._canvas = None # type: FigureCanvas self._axes = None # type: Axes self._legend = None # type: Legend self._handlers_annotations = [] # type: List self._make_chart_data(styles, zones_file) def __repr__(self) -> str: """Return a string representation of the PsychroChart object.""" return '<PsychroChart [{:g}->{:g} °C, {:g}->{:g} gr/kg_da]>'.format( self.dbt_min, self.dbt_max, self.w_min, self.w_max) @property def axes(self) -> Axes: """Return the Axes object plotting the chart if necessary.""" if self._axes is None: self.plot() assert isinstance(self._axes, Axes) return self._axes def _make_chart_data(self, styles: Union[dict, str]=None, zones_file: Union[dict, str]=None) -> None: """Generate the data to plot the psychrometric chart.""" # Get styling config = load_config(styles) self.d_config = config self.temp_step = config['limits']['step_temp'] self.figure_params = config['figure'] self.dbt_min, self.dbt_max = config['limits']['range_temp_c'] self.w_min, self.w_max = config['limits']['range_humidity_g_kg'] self.chart_params = config['chart_params'] # Base pressure if config['limits'].get('pressure_kpa') is not None: self.p_atm_kpa = config['limits']['pressure_kpa'] elif config['limits'].get('altitude_m') is not None: self.altitude_m = config['limits']['altitude_m'] self.p_atm_kpa = pressure_by_altitude(self.altitude_m) # Dry bulb constant lines (vertical): if self.chart_params["with_constant_dry_temp"]: step = self.chart_params["constant_temp_step"] style = config['constant_dry_temp'] temps_vl = f_range(self.dbt_min, self.dbt_max, step) heights = [1000 * humidity_ratio( saturation_pressure_water_vapor(t), p_atm_kpa=self.p_atm_kpa) for t in temps_vl] self.constant_dry_temp_data = PsychroCurves( [PsychroCurve([t, t], [self.w_min, h], style, type_curve='constant_dry_temp_data', label=None, logger=self._logger) for t, h in zip(temps_vl, heights)], family_label=self.chart_params["constant_temp_label"]) # Absolute humidity constant lines (horizontal): if self.chart_params["with_constant_humidity"]: step = self.chart_params["constant_humid_step"] style = config['constant_humidity'] ws_hl = f_range(self.w_min + step, self.w_max + step / 10, step) dew_points = solve_curves_with_iteration( 'DEW POINT', [x / 1000 for x in ws_hl], lambda x: dew_point_temperature( water_vapor_pressure( x, p_atm_kpa=self.p_atm_kpa)), lambda x: humidity_ratio( saturation_pressure_water_vapor(x), p_atm_kpa=self.p_atm_kpa)) self.constant_humidity_data = PsychroCurves( [PsychroCurve([t_dp, self.dbt_max], [w, w], style, type_curve='constant_humidity_data', label=None, logger=self._logger) for w, t_dp in zip(ws_hl, dew_points)], family_label=self.chart_params["constant_humid_label"]) # Constant relative humidity curves: if self.chart_params["with_constant_rh"]: rh_perc_values = self.chart_params["constant_rh_curves"] rh_label_values = self.chart_params.get("constant_rh_labels", []) label_loc = self.chart_params.get("constant_rh_labels_loc", .85) style = config["constant_rh"] temps_ct_rh, curves_ct_rh = _gen_list_curves_range_temps( curve_constant_humidity_ratio, self.dbt_min, self.dbt_max, self.temp_step, rh_perc_values, p_atm_kpa=self.p_atm_kpa) self.constant_rh_data = PsychroCurves( [PsychroCurve( temps_ct_rh, curve_ct_rh, style, type_curve='constant_rh_data', label_loc=label_loc, label='RH {:g} %'.format(rh) if round(rh, 1) in rh_label_values else None, logger=self._logger) for rh, curve_ct_rh in zip(rh_perc_values, curves_ct_rh)], family_label=self.chart_params["constant_rh_label"]) # Constant enthalpy lines: if self.chart_params["with_constant_h"]: step = self.chart_params["constant_h_step"] start, end = self.chart_params["range_h"] enthalpy_values = f_range(start, end, step) h_label_values = self.chart_params.get("constant_h_labels", []) label_loc = self.chart_params.get("constant_h_labels_loc", 1.) style = config["constant_h"] temps_max_constant_h = [ dry_temperature_for_enthalpy_of_moist_air( self.w_min / 1000, h) for h in enthalpy_values] sat_points = solve_curves_with_iteration( 'ENTHALPHY', enthalpy_values, lambda x: dry_temperature_for_enthalpy_of_moist_air( self.w_min / 1000 + 0.1, x), lambda x: enthalpy_moist_air( x, saturation_pressure_water_vapor(x), p_atm_kpa=self.p_atm_kpa)) self.constant_h_data = PsychroCurves( [PsychroCurve( [t_sat, t_max], [1000 * humidity_ratio( saturation_pressure_water_vapor(t_sat), self.p_atm_kpa), self.w_min], style, type_curve='constant_h_data', label_loc=label_loc, label='{:g} kJ/kg_da'.format(h) if round(h, 3) in h_label_values else None, logger=self._logger) for t_sat, t_max, h in zip( sat_points, temps_max_constant_h, enthalpy_values)], family_label=self.chart_params["constant_h_label"]) # Constant specific volume lines: if self.chart_params["with_constant_v"]: step = self.chart_params["constant_v_step"] start, end = self.chart_params["range_vol_m3_kg"] vol_values = f_range(start, end, step) vol_label_values = self.chart_params.get("constant_v_labels", []) label_loc = self.chart_params.get("constant_v_labels_loc", 1.) style = config["constant_v"] temps_max_constant_v = [ dry_temperature_for_specific_volume_of_moist_air( 0, specific_vol, p_atm_kpa=self.p_atm_kpa) for specific_vol in vol_values] sat_points = solve_curves_with_iteration( 'CONSTANT VOLUME', vol_values, lambda x: dry_temperature_for_specific_volume_of_moist_air( 0, x, p_atm_kpa=self.p_atm_kpa), lambda x: specific_volume( x, saturation_pressure_water_vapor(x), p_atm_kpa=self.p_atm_kpa)) self.constant_v_data = PsychroCurves( [PsychroCurve( [t_sat, t_max], [1000 * humidity_ratio( saturation_pressure_water_vapor(t_sat), self.p_atm_kpa), 0], style, type_curve='constant_v_data', label_loc=label_loc, label='{:g} m3/kg_da'.format(vol) if round(vol, 3) in vol_label_values else None, logger=self._logger) for t_sat, t_max, vol in zip( sat_points, temps_max_constant_v, vol_values)], family_label=self.chart_params["constant_v_label"]) # Constant wet bulb temperature lines: if self.chart_params["with_constant_wet_temp"]: step = self.chart_params["constant_wet_temp_step"] start, end = self.chart_params["range_wet_temp"] wbt_values = f_range(start, end, step) wbt_label_values = self.chart_params.get( "constant_wet_temp_labels", []) label_loc = self.chart_params.get( "constant_wet_temp_labels_loc", .05) style = config["constant_wet_temp"] w_max_constant_wbt = [humidity_ratio( saturation_pressure_water_vapor(wbt), self.p_atm_kpa) for wbt in wbt_values] self.constant_wbt_data = PsychroCurves( [PsychroCurve( [wbt, self.dbt_max], [1000 * w_max, 1000 * humidity_ratio( saturation_pressure_water_vapor(self.dbt_max) * relative_humidity_from_temps( self.dbt_max, wbt, p_atm_kpa=self.p_atm_kpa), p_atm_kpa=self.p_atm_kpa)], style, type_curve='constant_wbt_data', label_loc=label_loc, label='{:g} °C'.format(wbt) if wbt in wbt_label_values else None, logger=self._logger) for wbt, w_max in zip(wbt_values, w_max_constant_wbt)], family_label=self.chart_params["constant_wet_temp_label"]) # Saturation line: if True: sat_style = config["saturation"] temps_sat_line, w_sat_line = _gen_list_curves_range_temps( curve_constant_humidity_ratio, self.dbt_min, self.dbt_max, self.temp_step, [100], p_atm_kpa=self.p_atm_kpa) self.saturation = PsychroCurves( [PsychroCurve( temps_sat_line, w_sat_line[0], sat_style, type_curve='saturation', logger=self._logger)]) # Zones if self.chart_params["with_zones"] or zones_file is not None: self.append_zones(zones_file) def append_zones(self, zones: Union[dict, str]=None) -> None: """Append zones as patches to the psychrometric chart.""" if zones is None: # load default 'Comfort' zones (Spain RITE) d_zones = load_zones() else: d_zones = load_zones(zones) zones_ok = [_make_zone( zone_conf, self.temp_step, self.p_atm_kpa, logger=self._logger) for zone_conf in d_zones['zones'] if _valid_zone_type(zone_conf['zone_type'])] if zones_ok: self.zones.append(PsychroCurves(zones_ok)) def plot_points_dbt_rh(self, points: Dict, connectors: list=None, convex_groups: list=None, scatter_style: dict=None) -> Dict: """Append individual points, connectors and groups to the plot. * Pass a specific style dict to do a scatter plot: `scatter_style={'s': 5, 'alpha': .1, 'color': 'darkorange'}` * if you are plotting series of points, pass them as numpy arrays: `points={'points_series_name': (temp_array, humid_array)}` - The syntax to add points is: ``` points = { 'point_1_name': { 'label': 'label_for_legend', 'style': {'color': [0.855, 0.004, 0.278, 0.8], 'marker': 'X', 'markersize': 15}, 'xy': (31.06, 32.9)}, 'point_2_name': { 'label': 'label_for_legend', 'style': {'color': [0.573, 0.106, 0.318, 0.5], 'marker': 'x', 'markersize': 10}, 'xy': (29.42, 52.34)}, # ... } # Or, using the default style: points = { 'point_1_name': (31.06, 32.9), 'point_2_name': (29.42, 52.34), # ... } ``` - The syntax to add connectors between pairs of given points is: ``` connectors = [ {'start': 'point_1_name', 'end': 'point_2_name', 'style': {'color': [0.573, 0.106, 0.318, 0.7], "linewidth": 2, "linestyle": "-."}}, {'start': 'point_2_name', 'end': 'point_3_name', 'style': {'color': [0.855, 0.145, 0.114, 0.8], "linewidth": 2, "linestyle": ":"}}, # ... ] ``` - The syntax to add groups of given points (with more than 3 points) to plot a styled convex hull area is: ``` interior_zones = [ # Zone 1: ([point_1_name, point_2_name, point_3_name, ...], # list of points {"color": 'darkgreen', "lw": 0, ...}, # line style {"color": 'darkgreen', "lw": 0, ...}), # filling style # Zone 2: ([point_7_name, point_8_name, point_9_name, ...], # list of points {"color": 'darkorange', "lw": 0, ...}, # line style {"color": 'darkorange', "lw": 0, ...}), # filling style # ... ] ``` """ use_scatter, points_plot = False, {} default_style = {'marker': 'o', 'markersize': 10, 'color': [1, .8, 0.1, .8], 'linewidth': 0} if scatter_style is not None: default_style = scatter_style use_scatter = True for key, point in points.items(): plot_params = default_style.copy() if isinstance(point, dict): plot_params.update(point.get('style', {})) plot_params['label'] = point.get('label') point = point['xy'] temp = point[0] if isinstance(temp, Iterable): w_g_ka = curve_constant_humidity_ratio( temp, rh_percentage=point[1], p_atm_kpa=self.p_atm_kpa) points_plot[key] = temp, w_g_ka, plot_params else: w_g_ka = curve_constant_humidity_ratio( [temp], rh_percentage=point[1], p_atm_kpa=self.p_atm_kpa)[0] points_plot[key] = [temp], [w_g_ka], plot_params if connectors is not None: for i, d_con in enumerate(connectors): if (d_con['start'] in points_plot and d_con['end'] in points_plot): x_start = points_plot[d_con['start']][0][0] y_start = points_plot[d_con['start']][1][0] x_end = points_plot[d_con['end']][0][0] y_end = points_plot[d_con['end']][1][0] x_line = [x_start, x_end] y_line = [y_start, y_end] style = d_con.get('style', points_plot[d_con['start']][2]) line_label = d_con.get('label') self._handlers_annotations.append( self.axes.plot( x_line, y_line, label = line_label,dash_capstyle='round', **style)) self._handlers_annotations.append( self.axes.plot( x_line, y_line, color=list(style['color'][:3]) + [.15], lw=50, solid_capstyle='round')) for point in points_plot.values(): func_append = self.axes.scatter if use_scatter else self.axes.plot self._handlers_annotations.append( func_append(point[0], point[1], **point[2])) if (ConvexHull is not None and convex_groups and points_plot and (isinstance(convex_groups[0], list) or isinstance(convex_groups[0], tuple)) and len(convex_groups[0]) == 3): for convex_hull_zone, style_line, style_fill in convex_groups: int_points = np.array( [(point[0][0], point[1][0]) for name, point in points_plot.items() if name in convex_hull_zone]) if len(int_points) < 3: continue try: hull = ConvexHull(int_points) except QhullError: # pragma: no cover self._print_err('QhullError with points: %s', int_points) continue # noinspection PyUnresolvedReferences for simplex in hull.simplices: self._handlers_annotations.append( self.axes.plot(int_points[simplex, 0], int_points[simplex, 1], **style_line)) self._handlers_annotations.append( self.axes.fill(int_points[hull.vertices, 0], int_points[hull.vertices, 1], **style_fill)) return points_plot def plot_arrows_dbt_rh(self, points_pairs: Dict) -> Dict: """Append individual points to the plot.""" points_plot = {} default_style = { "linewidth": 0, "color": [1, .8, 0.1, .8], "arrowstyle": 'wedge'} for key, pair_point in points_pairs.items(): plot_params = default_style.copy() if isinstance(pair_point, dict): if 'style' in pair_point and "color" in pair_point['style']: plot_params['color'] = mod_color( pair_point['style']['color'], .6) # set alpha point1, point2 = pair_point['xy'] else: point1, point2 = pair_point temp1 = point1[0] temp2 = point2[0] w_g_ka1 = curve_constant_humidity_ratio( [temp1], rh_percentage=point1[1], p_atm_kpa=self.p_atm_kpa)[0] w_g_ka2 = curve_constant_humidity_ratio( [temp2], rh_percentage=point2[1], p_atm_kpa=self.p_atm_kpa)[0] self._handlers_annotations.append( self.axes.annotate( '', (temp2, w_g_ka2), xytext=(temp1, w_g_ka1), arrowprops=plot_params)) points_plot[key] = (temp1, w_g_ka1), (temp2, w_g_ka2), plot_params return points_plot def plot_vertical_dry_bulb_temp_line( self, temp: float, style: dict=None, label: str=None, reverse: bool=False, **label_params) -> None: """Append a vertical line from w_min to w_sat.""" w_max = 1000 * humidity_ratio( saturation_pressure_water_vapor(temp), self.p_atm_kpa) style_curve = style or self.d_config.get("constant_dry_temp") path_y = [w_max, self.w_min] if reverse else [self.w_min, w_max] curve = PsychroCurve( [temp, temp], path_y, style=style_curve, logger=self._logger) curve.plot(self.axes) if label is not None: curve.add_label(self.axes, label, **label_params) def plot_legend( self, loc: str='upper left', markerscale: float=.9, frameon: bool=True, fancybox: bool=True, edgecolor: Union[str, Iterable]='darkgrey', fontsize: float=15., labelspacing: float=1.5, **params) -> None: """Append a legend to the psychrochart plot.""" self._legend = self.axes.legend( loc=loc, markerscale=markerscale, frameon=frameon, edgecolor=edgecolor, fontsize=fontsize, fancybox=fancybox, labelspacing=labelspacing, **params) def plot(self, ax: Axes=None) -> Axes: """Plot the psychrochart and return the matplotlib Axes instance.""" def _apply_spines_style(axes, style, location='right'): for key in style: if (key == 'color') or (key == 'c'): axes.spines[location].set_color(style[key]) elif (key == 'linewidth') or (key == 'lw'): axes.spines[location].set_linewidth(style[key]) elif (key == 'linestyle') or (key == 'ls'): axes.spines[location].set_linestyle(style[key]) else: # pragma: no cover try: getattr(axes.spines[location], 'set_{}'.format(key))(style[key]) except Exception as exc: self._print_err( "Error trying to apply spines attrs: %s. (%s)", exc, dir(axes.spines[location])) # Prepare fig & axis fig_params = self.figure_params.copy() figsize = fig_params.pop('figsize', (16, 9)) position = fig_params.pop('position', [0.025, 0.075, 0.925, 0.875]) fontsize = fig_params.pop('fontsize', 10) x_style = fig_params.pop('x_axis', {}) x_style_labels = fig_params.pop('x_axis_labels', {}) x_style_ticks = fig_params.pop('x_axis_ticks', {}) y_style = fig_params.pop('y_axis', {}) y_style_labels = fig_params.pop('y_axis_labels', {}) y_style_ticks = fig_params.pop('y_axis_ticks', {}) partial_axis = fig_params.pop('partial_axis', True) # Create figure and format axis self._fig = figure.Figure(figsize=figsize, dpi=150, frameon=False) self._canvas = FigureCanvas(self._fig) if ax is None: ax = self._fig.gca(position=position) ax.yaxis.tick_right() ax.yaxis.set_label_position("right") ax.set_xlim(self.dbt_min, self.dbt_max) ax.set_ylim(self.w_min, self.w_max) ax.grid(False, which='major', axis='both') ax.grid(False, which='minor', axis='both') # Apply axis styles if fig_params['x_label'] is not None: style_axis = x_style_labels.copy() style_axis['fontsize'] *= 1.2 ax.set_xlabel(fig_params['x_label'], **style_axis) if fig_params['y_label'] is not None: style_axis = y_style_labels.copy() style_axis['fontsize'] *= 1.2 ax.set_ylabel(fig_params['y_label'], **style_axis) if fig_params['title'] is not None: ax.set_title(fig_params['title'], fontsize=fontsize * 1.5, fontweight='bold') _apply_spines_style(ax, y_style, location='right') _apply_spines_style(ax, x_style, location='bottom') if partial_axis: # Hide left and top axis ax.spines['left'].set_visible(False) ax.spines['top'].set_visible(False) else: _apply_spines_style(ax, y_style, location='left') _apply_spines_style(ax, x_style, location='top') if x_style_ticks: ax.tick_params(axis='x', **x_style_ticks) if y_style_ticks: ax.tick_params(axis='y', **y_style_ticks) if self.chart_params.get("with_constant_dry_temp", True): step_label = self.chart_params.get( "constant_temp_label_step", None) if step_label: # Explicit xticks ticks = f_range(self.dbt_min, self.dbt_max + step_label / 10, step_label) if not self.chart_params.get( "constant_temp_label_include_limits", True): ticks = [t for t in ticks if t not in [self.dbt_min, self.dbt_max]] ax.set_xticks(ticks) ax.set_xticklabels( ['{:g}'.format(t) for t in ticks], **x_style_labels) else: ax.set_xticks([]) if self.chart_params.get("with_constant_humidity", True): step_label = self.chart_params.get( "constant_humid_label_step", None) if step_label: # Explicit xticks ticks = f_range(self.w_min, self.w_max + step_label / 10, step_label) if not self.chart_params.get( "constant_humid_label_include_limits", True): ticks = [t for t in ticks if t not in [self.w_min, self.w_max]] ax.set_yticks(ticks) ax.set_yticklabels( ['{:g}'.format(t) for t in ticks], **y_style_labels) else: ax.set_yticks([]) # Plot curves: [getattr(self, curve_family).plot(ax) for curve_family in PSYCHRO_CURVES_KEYS if getattr(self, curve_family) is not None] # Plot zones: [zone.plot(ax=ax) for zone in self.zones] # Set the Axes object self._axes = ax return ax def remove_annotations(self) -> None: """Remove the annotations made in the chart to reuse it.""" for line in self._handlers_annotations: try: line[0].remove() except TypeError: line.remove() self._handlers_annotations = [] def remove_legend(self) -> None: """Remove the legend of the chart.""" if self._legend is not None: self._legend.remove() self._legend = None def save(self, path_dest: Any, **params: Any) -> None: """Write the chart to disk.""" if self._axes is None: self.plot() self._canvas.print_figure(path_dest, **params) gc.collect() def close_fig(self) -> None: """Close the figure plot.""" if self._axes is not None: self.remove_annotations() self.remove_legend() self._axes.remove() self._axes = None self._fig.clear() self._fig = None self._canvas = None gc.collect() def _print_err(self, *args: Any) -> None: if self._logger is not None: # pragma: no cover self._logger.error(*args) # pragma: no cover elif self._verbose: # pragma: no cover print(args[0] % args[1:]) # pragma: no cover
def spectrum_command_finished(self): """ Descript. : """ with cleanup(self.ready_event.set): self.spectrum_info["endTime"] = time.strftime("%Y-%m-%d %H:%M:%S") self.spectrum_running = False xmin = 0 xmax = 0 mca_data = [] calibrated_data = [] try: spectrum_file_raw = open(self.spectrum_info["scanFilePath"], "w") except BaseException: logging.getLogger("HWR").exception( "XRFSpectrum: could not create spectrum result raw file %s" % self.spectrum_info["scanFilePath"]) try: archive_file_raw = open(self.spectrum_info["scanFileFullPath"], "w") except BaseException: logging.getLogger("HWR").exception( "XRFSpectrum: could not create spectrum result raw file %s" % self.spectrum_info["scanFileFullPath"]) for n, value in enumerate(self.spectrum_data): energy = (self.mca_calib[2] + self.mca_calib[1] * n + self.mca_calib[0] * n * n) / 1000 if energy < 20: if energy > xmax: xmax = value if energy < xmin: xmin = value calibrated_data.append([energy, value]) mca_data.append((n / 1000.0, value)) if spectrum_file_raw: spectrum_file_raw.write("%f,%f\r\n" % (energy, value)) if archive_file_raw: archive_file_raw.write("%f,%f\r\n" % (energy, value)) if spectrum_file_raw: spectrum_file_raw.close() if archive_file_raw: archive_file_raw.close() calibrated_array = numpy.array(calibrated_data) if self.transmission_hwobj is not None: self.spectrum_info[ "beamTransmission"] = self.transmission_hwobj.getAttFactor( ) self.spectrum_info["energy"] = self.get_current_energy() if self.beam_info_hwobj is not None: beam_size = self.beam_info_hwobj.get_beam_size() self.spectrum_info["beamSizeHorizontal"] = int(beam_size[0] * 1000) self.spectrum_info["beamSizeVertical"] = int(beam_size[1] * 1000) mca_config = {} mca_config["legend"] = self.spectrum_info["filename"] mca_config["file"] = self.config_filename mca_config["min"] = xmin mca_config["max"] = xmax mca_config["htmldir"] = self.spectrum_info["htmldir"] self.spectrum_info.pop("htmldir") self.spectrum_info.pop("scanFilePath") fig = Figure(figsize=(15, 11)) ax = fig.add_subplot(111) ax.set_title(self.spectrum_info["jpegScanFileFullPath"]) ax.grid(True) ax.plot(*(zip(*calibrated_array)), **{"color": "black"}) ax.set_xlabel("Energy") ax.set_ylabel("Counts") canvas = FigureCanvasAgg(fig) logging.getLogger().info( "XRFSpectrum: Rendering spectrum to PNG file : %s", self.spectrum_info["jpegScanFileFullPath"], ) canvas.print_figure(self.spectrum_info["jpegScanFileFullPath"], dpi=80) # logging.getLogger().debug("Copying .fit file to: %s", a_dir) # tmpname=filename.split(".") # logging.getLogger().debug("finished %r", self.spectrum_info) self.store_xrf_spectrum() self.emit("xrfSpectrumFinished", (mca_data, self.mca_calib, mca_config))
def candplot(canddatalist, snrs=[], outname=''): """ Takes output of search_thresh (CandData objects) to make candidate plots. Expects pipeline state, candidate location, image, and phased, dedispersed data (cut out in time, dual-pol). snrs is array for an (optional) SNR histogram plot. Written by Bridget Andersen and modified by Casey for rfpipe. """ if not isinstance(canddatalist, list): logger.debug('Wrapping solo CandData object') canddatalist = [canddatalist] logger.info('Making {0} candidate plots.'.format(len(canddatalist))) for i in xrange(len(canddatalist)): canddata = canddatalist[i] st = canddata.state candloc = canddata.loc im = canddata.image data = canddata.data logger.info('Plotting for (image, data) shapes: ({0}, {1})'.format( str(im.shape), str(data.shape))) scan = st.metadata.scan segment, candint, dmind, dtind, beamnum = candloc # calc source location imstd = util.madtostd(im) snrmin = im.min() / imstd snrmax = im.max() / imstd if snrmax > -1 * snrmin: l1, m1 = st.pixtolm(np.where(im == im.max())) snrobs = snrmax else: l1, m1 = st.pixtolm(np.where(im == im.min())) snrobs = snrmin pt_ra, pt_dec = st.metadata.radec src_ra, src_dec = source_location(pt_ra, pt_dec, l1, m1) logger.info('Peak (RA, Dec): %s, %s' % (src_ra, src_dec)) # convert l1 and m1 from radians to arcminutes l1arcm = np.degrees(l1) * 60 m1arcm = np.degrees(m1) * 60 # build overall plot fig = plt.Figure(figsize=(12.75, 8)) # add metadata in subfigure ax = fig.add_subplot(2, 3, 1, facecolor='white') # calculate the overall dispersion delay: dd f1 = st.metadata.freq_orig[0] f2 = st.metadata.freq_orig[-1] dd = 4.15 * st.dmarr[dmind] * (f1**(-2) - f2**(-2)) # add annotating info # set spacing and location of the annotating information start = 1.1 space = 0.07 left = 0.0 ax.text(left, start, st.fileroot, fontname='sans-serif', transform=ax.transAxes, fontsize='small') ax.text(left, start - space, 'Peak (arcmin): (' + str(np.round(l1arcm, 3)) + ', ' + str(np.round(m1arcm, 3)) + ')', fontname='sans-serif', transform=ax.transAxes, fontsize='small') # split the RA and Dec and display in a nice format ra = src_ra.split() dec = src_dec.split() ax.text(left, start - 2 * space, 'Peak (RA, Dec): (' + ra[0] + ':' + ra[1] + ':' + ra[2][0:4] + ', ' + dec[0] + ':' + dec[1] + ':' + dec[2][0:4] + ')', fontname='sans-serif', transform=ax.transAxes, fontsize='small') ax.text(left, start - 3 * space, 'Source: ' + str(st.metadata.source), fontname='sans-serif', transform=ax.transAxes, fontsize='small') ax.text(left, start - 4 * space, 'scan: ' + str(scan), fontname='sans-serif', transform=ax.transAxes, fontsize='small') ax.text(left, start - 5 * space, 'segment: ' + str(segment), fontname='sans-serif', transform=ax.transAxes, fontsize='small') ax.text(left, start - 6 * space, 'integration: ' + str(candint), fontname='sans-serif', transform=ax.transAxes, fontsize='small') ax.text(left, start - 7 * space, 'DM = ' + str(st.dmarr[dmind]) + ' (index ' + str(dmind) + ')', fontname='sans-serif', transform=ax.transAxes, fontsize='small') ax.text(left, start - 8 * space, 'dt = ' + str(np.round(st.inttime * st.dtarr[dtind], 3) * 1e3) + ' ms' + ' (index ' + str(dtind) + ')', fontname='sans-serif', transform=ax.transAxes, fontsize='small') ax.text(left, start - 9 * space, 'disp delay = ' + str(np.round(dd, 1)) + ' ms', fontname='sans-serif', transform=ax.transAxes, fontsize='small') ax.text(left, start - 10 * space, 'SNR: ' + str(np.round(snrobs, 1)), fontname='sans-serif', transform=ax.transAxes, fontsize='small') # set the plot invisible so that it doesn't interfere with annotations ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) ax.spines['bottom'].set_color('white') ax.spines['top'].set_color('white') ax.spines['right'].set_color('white') ax.spines['left'].set_color('white') # plot full dynamic spectra left, width = 0.75, 0.2 * 2. / 3. bottom, height = 0.2, 0.7 # three rectangles for each panel of the spectrum (RR, RR+LL, LL) rect_dynsp1 = [left, bottom, width / 3., height] rect_dynsp2 = [left + width / 3., bottom, width / 3., height] rect_dynsp3 = [left + 2. * width / 3., bottom, width / 3., height] rect_lc1 = [left, bottom - 0.1, width / 3., 0.1] rect_lc2 = [left + width / 3., bottom - 0.1, width / 3., 0.1] rect_lc3 = [left + 2. * width / 3., bottom - 0.1, width / 3., 0.1] rect_sp = [left + width, bottom, 0.1 * 2. / 3., height] ax_dynsp1 = fig.add_axes(rect_dynsp1) # sharey so that axes line up ax_dynsp2 = fig.add_axes(rect_dynsp2, sharey=ax_dynsp1) ax_dynsp3 = fig.add_axes(rect_dynsp3, sharey=ax_dynsp1) # hide RR+LL and LL dynamic spectra y labels to avoid overlap [label.set_visible(False) for label in ax_dynsp2.get_yticklabels()] [label.set_visible(False) for label in ax_dynsp3.get_yticklabels()] ax_sp = fig.add_axes(rect_sp, sharey=ax_dynsp3) [label.set_visible(False) for label in ax_sp.get_yticklabels()] ax_lc1 = fig.add_axes(rect_lc1) ax_lc2 = fig.add_axes(rect_lc2, sharey=ax_lc1) ax_lc3 = fig.add_axes(rect_lc3, sharey=ax_lc1) [label.set_visible(False) for label in ax_lc2.get_yticklabels()] [label.set_visible(False) for label in ax_lc3.get_yticklabels()] # now actually plot the data spectra = np.swapaxes(data.real, 0, 1) dd1 = spectra[..., 0] dd2 = spectra[..., 0] + spectra[..., 1] dd3 = spectra[..., 1] colormap = 'viridis' logger.debug('{0}'.format(dd1.shape)) logger.debug('{0}'.format(dd2.shape)) logger.debug('{0}'.format(dd3.shape)) _ = ax_dynsp1.imshow(dd1, origin='lower', interpolation='nearest', aspect='auto', cmap=plt.get_cmap(colormap)) _ = ax_dynsp2.imshow(dd2, origin='lower', interpolation='nearest', aspect='auto', cmap=plt.get_cmap(colormap)) _ = ax_dynsp3.imshow(dd3, origin='lower', interpolation='nearest', aspect='auto', cmap=plt.get_cmap(colormap)) ax_dynsp1.set_yticks(range(0, len(st.freq), 30)) ax_dynsp1.set_yticklabels(st.freq[::30]) ax_dynsp1.set_ylabel('Freq (GHz)') ax_dynsp1.set_xlabel('RR') ax_dynsp1.xaxis.set_label_position('top') ax_dynsp2.set_xlabel('RR+LL') ax_dynsp2.xaxis.set_label_position('top') ax_dynsp3.set_xlabel('LL') ax_dynsp3.xaxis.set_label_position('top') # hide xlabels invisible so that they don't interefere with lc plots [label.set_visible(False) for label in ax_dynsp1.get_xticklabels()] # This one y label was getting in the way ax_dynsp1.get_yticklabels()[0].set_visible(False) # plot stokes I spectrum of the candidate pulse (assume middle bin) # select stokes I middle bin spectrum = spectra[:, len(spectra[0]) // 2].mean(axis=1) ax_sp.plot(spectrum, range(len(spectrum)), 'k.') # plot 0 Jy dotted line ax_sp.plot(np.zeros(len(spectrum)), range(len(spectrum)), 'r:') xmin, xmax = ax_sp.get_xlim() ax_sp.set_xticks(np.linspace(xmin, xmax, 3).round(2)) ax_sp.set_xlabel('Flux (Jy)') # plot mean flux values for each time bin lc1 = dd1.mean(axis=0) lc2 = dd2.mean(axis=0) lc3 = dd3.mean(axis=0) lenlc = len(data) ax_lc1.plot(range(0, lenlc), list(lc1)[:lenlc], 'k.') ax_lc2.plot(range(0, lenlc), list(lc2)[:lenlc], 'k.') ax_lc3.plot(range(0, lenlc), list(lc3)[:lenlc], 'k.') # plot 0 Jy dotted line for each plot ax_lc1.plot(range(0, lenlc), list(np.zeros(lenlc)), 'r:') ax_lc2.plot(range(0, lenlc), list(np.zeros(lenlc)), 'r:') ax_lc3.plot(range(0, lenlc), list(np.zeros(lenlc)), 'r:') ax_lc2.set_xlabel('Integration (rel)') ax_lc1.set_ylabel('Flux (Jy)') ax_lc1.set_xticks([0, 0.5 * lenlc, lenlc]) # only show the '0' label for one of the plots to avoid messy overlap ax_lc1.set_xticklabels(['0', str(lenlc // 2), str(lenlc)]) ax_lc2.set_xticks([0, 0.5 * lenlc, lenlc]) ax_lc2.set_xticklabels(['', str(lenlc // 2), str(lenlc)]) ax_lc3.set_xticks([0, 0.5 * lenlc, lenlc]) ax_lc3.set_xticklabels(['', str(lenlc // 2), str(lenlc)]) ymin, ymax = ax_lc1.get_ylim() ax_lc1.set_yticks(np.linspace(ymin, ymax, 3).round(2)) # adjust the x tick marks to line up with the lc plots ax_dynsp1.set_xticks([0, 0.5 * lenlc, lenlc]) ax_dynsp2.set_xticks([0, 0.5 * lenlc, lenlc]) ax_dynsp3.set_xticks([0, 0.5 * lenlc, lenlc]) # plot second set of dynamic spectra left, width = 0.45, 0.1333 bottom, height = 0.1, 0.4 rect_dynsp1 = [left, bottom, width / 3., height] rect_dynsp2 = [left + width / 3., bottom, width / 3., height] rect_dynsp3 = [left + 2. * width / 3., bottom, width / 3., height] rect_sp = [left + width, bottom, 0.1 * 2. / 3., height] ax_dynsp1 = fig.add_axes(rect_dynsp1) ax_dynsp2 = fig.add_axes(rect_dynsp2, sharey=ax_dynsp1) ax_dynsp3 = fig.add_axes(rect_dynsp3, sharey=ax_dynsp1) # hide RR+LL and LL dynamic spectra y labels [label.set_visible(False) for label in ax_dynsp2.get_yticklabels()] [label.set_visible(False) for label in ax_dynsp3.get_yticklabels()] ax_sp = fig.add_axes(rect_sp, sharey=ax_dynsp3) [label.set_visible(False) for label in ax_sp.get_yticklabels()] # calculate the channels to average together for SNR=2 n = int((2. * (len(spectra))**0.5 / snrobs)**2) if n == 0: # if n==0 then don't average dd1avg = dd1 dd3avg = dd3 else: # otherwise, add zeros onto the data so that it's length is cleanly # divisible by n (makes it easier to average over) dd1zerotemp = np.concatenate( (np.zeros((n - len(spectra) % n, len(spectra[0])), dtype=dd1.dtype), dd1), axis=0) dd3zerotemp = np.concatenate( (np.zeros((n - len(spectra) % n, len(spectra[0])), dtype=dd3.dtype), dd3), axis=0) # make masked arrays so appended zeros do not affect average zeros = np.zeros((len(dd1), len(dd1[0]))) ones = np.ones((n - len(spectra) % n, len(dd1[0]))) masktemp = np.concatenate((ones, zeros), axis=0) dd1zero = np.ma.masked_array(dd1zerotemp, mask=masktemp) dd3zero = np.ma.masked_array(dd3zerotemp, mask=masktemp) # average together the data dd1avg = np.array([], dtype=dd1.dtype) for i in range(len(spectra[0])): temp = dd1zero[:, i].reshape(-1, n) tempavg = np.reshape(np.mean(temp, axis=1), (len(temp), 1)) # repeats the mean values to create more pixels # (easier to properly crop when it is finally displayed) temprep = np.repeat(tempavg, n, axis=0) if i == 0: dd1avg = temprep else: dd1avg = np.concatenate((dd1avg, temprep), axis=1) dd3avg = np.array([], dtype=dd3.dtype) for i in range(len(spectra[0])): temp = dd3zero[:, i].reshape(-1, n) tempavg = np.reshape(np.mean(temp, axis=1), (len(temp), 1)) temprep = np.repeat(tempavg, n, axis=0) if i == 0: dd3avg = temprep else: dd3avg = np.concatenate((dd3avg, temprep), axis=1) dd2avg = dd1avg + dd3avg # add together to get averaged RR+LL spectrum colormap = 'viridis' # if n==0 then don't crop the spectra because no zeroes were appended if n == 0: dd1avgcrop = dd1avg dd2avgcrop = dd2avg dd3avgcrop = dd3avg else: # otherwise, crop off the appended zeroes dd1avgcrop = dd1avg[len(ones):len(dd1avg), :] dd2avgcrop = dd2avg[len(ones):len(dd2avg), :] dd3avgcrop = dd3avg[len(ones):len(dd3avg), :] logger.debug('{0}'.format(dd1avgcrop.shape)) logger.debug('{0}'.format(dd2avgcrop.shape)) logger.debug('{0}'.format(dd3avgcrop.shape)) _ = ax_dynsp1.imshow(dd1avgcrop, origin='lower', interpolation='nearest', aspect='auto', cmap=plt.get_cmap(colormap)) _ = ax_dynsp2.imshow(dd2avgcrop, origin='lower', interpolation='nearest', aspect='auto', cmap=plt.get_cmap(colormap)) _ = ax_dynsp3.imshow(dd3avgcrop, origin='lower', interpolation='nearest', aspect='auto', cmap=plt.get_cmap(colormap)) ax_dynsp1.set_yticks(range(0, len(st.freq), 30)) ax_dynsp1.set_yticklabels(st.freq[::30]) ax_dynsp1.set_ylabel('Freq (GHz)') ax_dynsp1.set_xlabel('RR') ax_dynsp1.xaxis.set_label_position('top') ax_dynsp2.set_xlabel('Integration (rel)') ax2 = ax_dynsp2.twiny() ax2.set_xlabel('RR+LL') [label.set_visible(False) for label in ax2.get_xticklabels()] ax_dynsp3.set_xlabel('LL') ax_dynsp3.xaxis.set_label_position('top') # plot stokes I spectrum of the candidate pulse from middle integration ax_sp.plot(dd2avgcrop[:, len(dd2avgcrop[0]) // 2] / 2., range(len(dd2avgcrop)), 'k.') ax_sp.plot(np.zeros(len(dd2avgcrop)), range(len(dd2avgcrop)), 'r:') xmin, xmax = ax_sp.get_xlim() ax_sp.set_xticks(np.linspace(xmin, xmax, 3).round(2)) ax_sp.get_xticklabels()[0].set_visible(False) ax_sp.set_xlabel('Flux (Jy)') # readjust the x tick marks on the dynamic spectra ax_dynsp1.set_xticks([0, 0.5 * lenlc, lenlc]) ax_dynsp1.set_xticklabels(['0', str(lenlc // 2), str(lenlc)]) ax_dynsp2.set_xticks([0, 0.5 * lenlc, lenlc]) ax_dynsp2.set_xticklabels(['', str(lenlc // 2), str(lenlc)]) ax_dynsp3.set_xticks([0, 0.5 * lenlc, lenlc]) ax_dynsp3.set_xticklabels(['', str(lenlc // 2), str(lenlc)]) # plot the image and zoomed cutout ax = fig.add_subplot(2, 3, 4) fov = np.degrees(1. / st.uvres) * 60. _ = ax.imshow(im.transpose(), aspect='equal', origin='upper', interpolation='nearest', extent=[fov / 2, -fov / 2, -fov / 2, fov / 2], cmap=plt.get_cmap('viridis'), vmin=0, vmax=0.5 * im.max()) ax.set_xlabel('RA Offset (arcmin)') ax.set_ylabel('Dec Offset (arcmin)') # to set scale when we plot the triangles that label the location ax.autoscale(False) # add markers on the axes at measured position of the candidate ax.scatter(x=[l1arcm], y=[-fov / 2], c='#ffff00', s=60, marker='^', clip_on=False) ax.scatter(x=[fov / 2], y=[m1arcm], c='#ffff00', s=60, marker='>', clip_on=False) # makes it so the axis does not intersect the location triangles ax.set_frame_on(False) # add a zoomed cutout image of the candidate (set width at 5*beam) sbeam = np.mean(st.beamsize_deg) * 60 # figure out the location to center the zoomed image on xratio = len(im[0]) / fov # pix/arcmin yratio = len(im) / fov # pix/arcmin mult = 5 # sets how many times the synthesized beam the zoomed FOV is xmin = max(0, int(len(im[0]) // 2 - (m1arcm + sbeam * mult) * xratio)) xmax = int(len(im[0]) // 2 - (m1arcm - sbeam * mult) * xratio) ymin = max(0, int(len(im) // 2 - (l1arcm + sbeam * mult) * yratio)) ymax = int(len(im) // 2 - (l1arcm - sbeam * mult) * yratio) left, width = 0.231, 0.15 bottom, height = 0.465, 0.15 rect_imcrop = [left, bottom, width, height] ax_imcrop = fig.add_axes(rect_imcrop) logger.debug('{0}'.format(im.transpose()[xmin:xmax, ymin:ymax].shape)) logger.debug('{0} {1} {2} {3}'.format(xmin, xmax, ymin, ymax)) _ = ax_imcrop.imshow(im.transpose()[xmin:xmax, ymin:ymax], aspect=1, origin='upper', interpolation='nearest', extent=[-1, 1, -1, 1], cmap=plt.get_cmap('viridis'), vmin=0, vmax=0.5 * im.max()) # setup the axes ax_imcrop.set_ylabel('Dec (arcmin)') ax_imcrop.set_xlabel('RA (arcmin)') ax_imcrop.xaxis.set_label_position('top') ax_imcrop.xaxis.tick_top() xlabels = [ str(np.round(l1arcm + sbeam * mult / 2, 1)), '', str(np.round(l1arcm, 1)), '', str(np.round(l1arcm - sbeam * mult / 2, 1)) ] ylabels = [ str(np.round(m1arcm - sbeam * mult / 2, 1)), '', str(np.round(m1arcm, 1)), '', str(np.round(m1arcm + sbeam * mult / 2, 1)) ] ax_imcrop.set_xticklabels(xlabels) ax_imcrop.set_yticklabels(ylabels) # change axis label loc of inset to avoid the full picture ax_imcrop.get_yticklabels()[0].set_verticalalignment('bottom') # create SNR versus N histogram for the whole observation # (properties for each candidate in the observation given by prop) if len(snrs): left, width = 0.45, 0.2 bottom, height = 0.6, 0.3 rect_snr = [left, bottom, width, height] ax_snr = fig.add_axes(rect_snr) pos_snrs = snrs[snrs >= 0] neg_snrs = snrs[snrs < 0] if not len(neg_snrs): # if working with subset and only pos snrs neg_snrs = pos_snrs nonegs = True else: nonegs = False minval = 5.5 maxval = 8.0 # determine the min and max values of the x axis if min(pos_snrs) < min(np.abs(neg_snrs)): minval = min(pos_snrs) else: minval = min(np.abs(neg_snrs)) if max(pos_snrs) > max(np.abs(neg_snrs)): maxval = max(pos_snrs) else: maxval = max(np.abs(neg_snrs)) # positive SNR bins are in blue # absolute values of negative SNR bins are taken and plotted as # red x's on top of positive blue bins for compactness n, b, patches = ax_snr.hist(pos_snrs, 50, (minval, maxval), facecolor='blue', zorder=1) vals, bin_edges = np.histogram(np.abs(neg_snrs), 50, (minval, maxval)) bins = np.array([(bin_edges[i] + bin_edges[i + 1]) / 2. for i in range(len(vals))]) vals = np.array(vals) if not nonegs: ax_snr.scatter(bins[vals > 0], vals[vals > 0], marker='x', c='orangered', alpha=1.0, zorder=2) ax_snr.set_xlabel('SNR') ax_snr.set_xlim(left=minval - 0.2) ax_snr.set_xlim(right=maxval + 0.2) ax_snr.set_ylabel('N') ax_snr.set_yscale('log') # draw vertical line where the candidate SNR is ax_snr.axvline(x=np.abs(snrobs), linewidth=1, color='y', alpha=0.7) if not outname: outname = os.path.join( st.prefs.workdir, 'cands_{}_sc{}-seg{}-i{}-dm{}-dt{}.png'.format( st.fileroot, scan, segment, candint, dmind, dtind)) try: from matplotlib.backends.backend_agg import FigureCanvasAgg canvas = FigureCanvasAgg(fig) canvas.print_figure(outname) except ValueError: logger.warn('Could not write figure to %s' % outname)
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas from matplotlib.figure import Figure m = Basemap(llcrnrlon= -126, \ llcrnrlat=23, \ urcrnrlon= -65, \ urcrnrlat = 50, \ resolution = 'l', \ projection = 'tmerc', \ lon_0 = -100, \ lat_0 = 37) fig = Figure() canvas = FigureCanvas(fig) m.ax = fig.add_axes([0, 0, 1, 1]) fig.set_size_inches((8 / m.aspect, 8.)) lats = [41.38, 43.18, 48.87, 43.60, 46.52, 43.28, 46.20] lons = [2.18, 3.00, 2.32, 1.43, 6.63, 5.37, 6.15] name = [ 'Barcelona', 'Narbonne', 'Paris', 'Toulouse', 'Lausanne', 'Marseille', 'Geneva' ] m.drawcoastlines(color='black') m.drawcountries(color='black') m.drawstates(color='gray') x, y = m(lons, lats) m.plot(x, y, 'bo') canvas.print_figure('map.ps', dpi=100)
def spectrumFinished(self, mca_data, calib, config): a = str(self.directoryInput.text()).split(os.path.sep) print 'a', a dirctr = str( self.directoryInput.text()) #let's have string instead of QString print 'self.directoryInput.text()', self.directoryInput.text() if dirctr[-1] == '/': a_dir = dirctr[:-1] else: a_dir = dirctr #a = str(self.directoryInput.text()).split(os.path.sep) #suffix_path=os.path.join(*a[4:]) #if 'inhouse' in a : #a_dir = os.path.join('/data/pyarch/', a[2], suffix_path) #else: #a_dir = os.path.join('/data/pyarch/',a[4],a[3],*a[5:]) #if a_dir[-1]!=os.path.sep: #a_dir+=os.path.sep print "a_dir --------------------------->", a_dir if not os.path.exists(os.path.dirname(a_dir)): os.makedirs(os.path.dirname(a_dir)) filename_pattern = os.path.join( a_dir, "%s_%s_%%02d" % (str(self.prefixInput.text()), time.strftime("%d_%b_%Y"))) filename_pattern = os.path.extsep.join((filename_pattern, "png")) filename = filename_pattern % 1 i = 2 while os.path.isfile(filename): filename = filename_pattern % i i = i + 1 try: a = float(calib[0]) b = float(calib[1]) c = float(calib[2]) except: a = 0 b = 1 c = 0 calibrated_data = [] for line in mca_data: channel = line[0] counts = line[1] energy = a + b * channel + c * channel * channel calibrated_line = [energy, counts] calibrated_data.append(calibrated_line) calibrated_array = numpy.array(calibrated_data) fig = Figure(figsize=(15, 11)) ax = fig.add_subplot(111) ax.set_title(filename) ax.grid(True) #ax.plot(*(zip(*mca_data)), **{"color":'black'}) ax.plot(*(zip(*calibrated_array)), **{"color": 'black'}) #ax.set_xlabel("MCA channel") #ax.set_ylabel("MCA counts") ax.set_xlabel("Energy") ax.set_ylabel("Counts") canvas = FigureCanvasAgg(fig) logging.getLogger().info("Rendering spectrum to PNG file : %s", filename) canvas.print_figure(filename, dpi=80) logging.getLogger().debug("Copying PNG file to: %s", a_dir) #shutil.copy (filename, a_dir) try: shutil.copy(filename, str(a_dir) + '/') logging.getLogger().debug("Copying .fit file to: %s", a_dir) except: print 'Problem copying file ', filename, 'to ', a_dir logging.getLogger().debug("Problem copying .fit file to: %s", a_dir) logging.getLogger().debug("Copying .fit file to: %s", a_dir) #tmpname=filename.split(".") color = XfeSpectrumBrick.STATES['ok'] self.statusBox.setTitle("Xfe spectrum status") config['max'] = 'max_user' #config['max_user'] config['htmldir'] = a_dir try: self.emit(PYSIGNAL("xfeSpectrumDone"), (mca_data, calib, config)) except: logging.getLogger().exception( "XfeSpectrumBrick: problem updating embedded PyMCA") print traceback.print_exc traceback.print_exc self.spectrumStatus.setPaletteBackgroundColor(QColor(color)) self.startSpectrumButton.commandDone() self.emit(PYSIGNAL("xfeSpectrumRun"), (False, )) self.parametersBox.setEnabled(True)
class PsychroChart: """Psychrometric chart object handler.""" def __init__(self, styles: Union[dict, str]=None, zones_file: Union[dict, str]=None, logger: Any=None) -> None: """Create the PsychroChart object.""" self._logger = logger self.d_config = {} # type: dict self.figure_params = {} # type: dict self.dbt_min = self.dbt_max = -100 self.w_min = self.w_max = -1 self.temp_step = 1. self.altitude_m = -1 self.chart_params = {} # type: dict self.p_atm_kpa = PRESSURE_STD_ATM_KPA self.constant_dry_temp_data = None # type: PsychroCurves self.constant_humidity_data = None # type: PsychroCurves self.constant_rh_data = None # type: PsychroCurves self.constant_h_data = None # type: PsychroCurves self.constant_v_data = None # type: PsychroCurves self.constant_wbt_data = None # type: PsychroCurves self.saturation = None # type: PsychroCurves self.zones = [] # type: List self._fig = None # type: figure.Figure self._canvas = None # type: FigureCanvas self._axes = None # type: Axes self._legend = None # type: Legend self._handlers_annotations = [] # type: List self._make_chart_data(styles, zones_file) def __repr__(self) -> str: """Return a string representation of the PsychroChart object.""" return '<PsychroChart [{:g}->{:g} °C, {:g}->{:g} gr/kg_da]>'.format( self.dbt_min, self.dbt_max, self.w_min, self.w_max) @property def axes(self) -> Axes: """Return the Axes object plotting the chart if necessary.""" if self._axes is None: self.plot() assert isinstance(self._axes, Axes) return self._axes def _make_chart_data(self, styles: Union[dict, str]=None, zones_file: Union[dict, str]=None) -> None: """Generate the data to plot the psychrometric chart.""" # Get styling config = load_config(styles) self.d_config = config self.temp_step = config['limits']['step_temp'] self.figure_params = config['figure'] self.dbt_min, self.dbt_max = config['limits']['range_temp_c'] self.w_min, self.w_max = config['limits']['range_humidity_g_kg'] self.chart_params = config['chart_params'] # Base pressure if config['limits'].get('pressure_kpa') is not None: self.p_atm_kpa = config['limits']['pressure_kpa'] elif config['limits'].get('altitude_m') is not None: self.altitude_m = config['limits']['altitude_m'] self.p_atm_kpa = pressure_by_altitude(self.altitude_m) # Dry bulb constant lines (vertical): if self.chart_params["with_constant_dry_temp"]: step = self.chart_params["constant_temp_step"] style = config['constant_dry_temp'] temps_vl = f_range(self.dbt_min, self.dbt_max, step) heights = [1000 * humidity_ratio( saturation_pressure_water_vapor(t), p_atm_kpa=self.p_atm_kpa) for t in temps_vl] self.constant_dry_temp_data = PsychroCurves( [PsychroCurve([t, t], [self.w_min, h], style, type_curve='constant_dry_temp_data', label=None, logger=self._logger) for t, h in zip(temps_vl, heights)], family_label=self.chart_params["constant_temp_label"]) # Absolute humidity constant lines (horizontal): if self.chart_params["with_constant_humidity"]: step = self.chart_params["constant_humid_step"] style = config['constant_humidity'] ws_hl = f_range(self.w_min + step, self.w_max + step / 10, step) dew_points = [ iter_solver( dew_point_temperature( water_vapor_pressure( w / 1000, p_atm_kpa=self.p_atm_kpa)), w / 1000., lambda x: humidity_ratio( saturation_pressure_water_vapor(x), p_atm_kpa=self.p_atm_kpa), initial_increment=0.25, num_iters_max=100, precision=0.00001) for w in ws_hl] self.constant_humidity_data = PsychroCurves( [PsychroCurve([t_dp, self.dbt_max], [w, w], style, type_curve='constant_humidity_data', label=None, logger=self._logger) for w, t_dp in zip(ws_hl, dew_points)], family_label=self.chart_params["constant_humid_label"]) # Constant relative humidity curves: if self.chart_params["with_constant_rh"]: rh_perc_values = self.chart_params["constant_rh_curves"] rh_label_values = self.chart_params.get("constant_rh_labels", []) label_loc = self.chart_params.get("constant_rh_labels_loc", .85) style = config["constant_rh"] temps_ct_rh, curves_ct_rh = _gen_list_curves_range_temps( curve_constant_humidity_ratio, self.dbt_min, self.dbt_max, self.temp_step, rh_perc_values, p_atm_kpa=self.p_atm_kpa) self.constant_rh_data = PsychroCurves( [PsychroCurve( temps_ct_rh, curve_ct_rh, style, type_curve='constant_rh_data', label_loc=label_loc, label='RH {:g} %'.format(rh) if round(rh, 1) in rh_label_values else None, logger=self._logger) for rh, curve_ct_rh in zip(rh_perc_values, curves_ct_rh)], family_label=self.chart_params["constant_rh_label"]) # Constant enthalpy lines: if self.chart_params["with_constant_h"]: step = self.chart_params["constant_h_step"] start, end = self.chart_params["range_h"] enthalpy_values = f_range(start, end, step) h_label_values = self.chart_params.get("constant_h_labels", []) label_loc = self.chart_params.get("constant_h_labels_loc", 1.) style = config["constant_h"] temps_max_constant_h = [ dry_temperature_for_enthalpy_of_moist_air( self.w_min / 1000, h) for h in enthalpy_values] sat_points = [ iter_solver( dry_temperature_for_enthalpy_of_moist_air( self.w_min / 1000 + 0.1, h), h, lambda x: enthalpy_moist_air( x, saturation_pressure_water_vapor(x), p_atm_kpa=self.p_atm_kpa), initial_increment=15, num_iters_max=100, precision=0.0005) for h in enthalpy_values] self.constant_h_data = PsychroCurves( [PsychroCurve( [t_sat, t_max], [1000 * humidity_ratio( saturation_pressure_water_vapor(t_sat), self.p_atm_kpa), self.w_min], style, type_curve='constant_h_data', label_loc=label_loc, label='{:g} kJ/kg_da'.format(h) if round(h, 3) in h_label_values else None, logger=self._logger) for t_sat, t_max, h in zip( sat_points, temps_max_constant_h, enthalpy_values)], family_label=self.chart_params["constant_h_label"]) # Constant specific volume lines: if self.chart_params["with_constant_v"]: step = self.chart_params["constant_v_step"] start, end = self.chart_params["range_vol_m3_kg"] vol_values = f_range(start, end, step) vol_label_values = self.chart_params.get("constant_v_labels", []) label_loc = self.chart_params.get("constant_v_labels_loc", 1.) style = config["constant_v"] temps_max_constant_v = [ dry_temperature_for_specific_volume_of_moist_air( 0, specific_vol, p_atm_kpa=self.p_atm_kpa) for specific_vol in vol_values] sat_points = [ iter_solver( t_max - 5, specific_vol, lambda x: specific_volume( x, saturation_pressure_water_vapor(x), p_atm_kpa=self.p_atm_kpa), initial_increment=2, num_iters_max=100, precision=0.00005) for t_max, specific_vol in zip(temps_max_constant_v, vol_values)] self.constant_v_data = PsychroCurves( [PsychroCurve( [t_sat, t_max], [1000 * humidity_ratio( saturation_pressure_water_vapor(t_sat), self.p_atm_kpa), 0], style, type_curve='constant_v_data', label_loc=label_loc, label='{:g} m3/kg_da'.format(vol) if round(vol, 3) in vol_label_values else None, logger=self._logger) for t_sat, t_max, vol in zip( sat_points, temps_max_constant_v, vol_values)], family_label=self.chart_params["constant_v_label"]) # Constant wet bulb temperature lines: if self.chart_params["with_constant_wet_temp"]: step = self.chart_params["constant_wet_temp_step"] start, end = self.chart_params["range_wet_temp"] wbt_values = f_range(start, end, step) wbt_label_values = self.chart_params.get( "constant_wet_temp_labels", []) label_loc = self.chart_params.get( "constant_wet_temp_labels_loc", .05) style = config["constant_wet_temp"] w_max_constant_wbt = [humidity_ratio( saturation_pressure_water_vapor(wbt), self.p_atm_kpa) for wbt in wbt_values] self.constant_wbt_data = PsychroCurves( [PsychroCurve( [wbt, self.dbt_max], [1000 * w_max, 1000 * humidity_ratio( saturation_pressure_water_vapor(self.dbt_max) * relative_humidity_from_temps( self.dbt_max, wbt, p_atm_kpa=self.p_atm_kpa), p_atm_kpa=self.p_atm_kpa)], style, type_curve='constant_wbt_data', label_loc=label_loc, label='{:g} °C'.format(wbt) if wbt in wbt_label_values else None, logger=self._logger) for wbt, w_max in zip(wbt_values, w_max_constant_wbt)], family_label=self.chart_params["constant_wet_temp_label"]) # Saturation line: if True: sat_style = config["saturation"] temps_sat_line, w_sat_line = _gen_list_curves_range_temps( curve_constant_humidity_ratio, self.dbt_min, self.dbt_max, self.temp_step, [100], p_atm_kpa=self.p_atm_kpa) self.saturation = PsychroCurves( [PsychroCurve( temps_sat_line, w_sat_line[0], sat_style, type_curve='saturation', logger=self._logger)]) # Zones if self.chart_params["with_zones"] or zones_file is not None: self.append_zones(zones_file) def append_zones(self, zones: Union[dict, str]=None) -> None: """Append zones as patches to the psychrometric chart.""" if zones is None: # load default 'Comfort' zones (Spain RITE) d_zones = load_zones() else: d_zones = load_zones(zones) self.zones.append( PsychroCurves( list(filter( lambda x: x is not None, [_make_zone(zone_conf, self.temp_step, self.p_atm_kpa, logger=self._logger) for zone_conf in d_zones['zones']])))) def plot_points_dbt_rh(self, points: Dict, connectors: list=None) -> Dict: """Append individual points to the plot.""" points_plot = {} default_style = {'marker': 'o', 'markersize': 10, 'color': [1, .8, 0.1, .8], 'linewidth': 0} for key, point in points.items(): plot_params = default_style.copy() if isinstance(point, dict): plot_params.update(point.get('style', {})) plot_params['label'] = point.get('label') point = point['xy'] temp = point[0] w_g_ka = curve_constant_humidity_ratio( [temp], rh_percentage=point[1], p_atm_kpa=self.p_atm_kpa)[0] points_plot[key] = [temp], [w_g_ka], plot_params if connectors is not None: for i, d_con in enumerate(connectors): if (d_con['start'] in points_plot and d_con['end'] in points_plot): x_start = points_plot[d_con['start']][0][0] y_start = points_plot[d_con['start']][1][0] x_end = points_plot[d_con['end']][0][0] y_end = points_plot[d_con['end']][1][0] x_line = [x_start, x_end] y_line = [y_start, y_end] style = d_con.get('style', points_plot[d_con['start']][2]) self._handlers_annotations.append( self.axes.plot( x_line, y_line, dash_capstyle='round', **style)) self._handlers_annotations.append( self.axes.plot( x_line, y_line, color=list(style['color'][:3]) + [.15], lw=50, solid_capstyle='round')) for point in points_plot.values(): self._handlers_annotations.append( self.axes.plot(point[0], point[1], **point[2])) return points_plot def plot_arrows_dbt_rh(self, points_pairs: Dict) -> Dict: """Append individual points to the plot.""" points_plot = {} default_style = { "linewidth": 0, "color": [1, .8, 0.1, .8], "arrowstyle": 'wedge'} for key, pair_point in points_pairs.items(): plot_params = default_style.copy() if isinstance(pair_point, dict): if 'style' in pair_point and "color" in pair_point['style']: plot_params['color'] = mod_color( pair_point['style']['color'], .6) # set alpha point1, point2 = pair_point['xy'] else: point1, point2 = pair_point temp1 = point1[0] temp2 = point2[0] w_g_ka1 = curve_constant_humidity_ratio( [temp1], rh_percentage=point1[1], p_atm_kpa=self.p_atm_kpa)[0] w_g_ka2 = curve_constant_humidity_ratio( [temp2], rh_percentage=point2[1], p_atm_kpa=self.p_atm_kpa)[0] self._handlers_annotations.append( self.axes.annotate( '', (temp2, w_g_ka2), xytext=(temp1, w_g_ka1), arrowprops=plot_params)) points_plot[key] = (temp1, w_g_ka1), (temp2, w_g_ka2), plot_params return points_plot def plot_vertical_dry_bulb_temp_line( self, temp: float, style: Optional[Dict]=None, label: Optional[str]=None, reverse: bool=False, **label_params) -> None: """Append a vertical line from w_min to w_sat.""" w_max = 1000 * humidity_ratio( saturation_pressure_water_vapor(temp), self.p_atm_kpa) style_curve = style or self.d_config.get("constant_dry_temp") path_y = [w_max, self.w_min] if reverse else [self.w_min, w_max] curve = PsychroCurve( [temp, temp], path_y, style=style_curve, logger=self._logger) curve.plot(self.axes) if label is not None: curve.add_label(self.axes, label, **label_params) def plot_legend( self, loc: str='upper left', markerscale: float=.9, frameon: bool=True, fancybox: bool=True, edgecolor: Union[str, Iterable]='darkgrey', fontsize: float=15., labelspacing: float=1.5, **params) -> None: """Append a legend to the psychrochart plot.""" self._legend = self.axes.legend( loc=loc, markerscale=markerscale, frameon=frameon, edgecolor=edgecolor, fontsize=fontsize, fancybox=fancybox, labelspacing=labelspacing, **params) def plot(self) -> Axes: """Plot the psychrochart and return the matplotlib Axes instance.""" def _apply_spines_style(axes, style, location='right'): for key in style: if (key == 'color') or (key == 'c'): axes.spines[location].set_color(style[key]) elif (key == 'linewidth') or (key == 'lw'): axes.spines[location].set_linewidth(style[key]) elif (key == 'linestyle') or (key == 'ls'): axes.spines[location].set_linestyle(style[key]) else: try: getattr(axes.spines[location], 'set_{}'.format(key))(style[key]) except Exception as exc: self._print_err( "Error trying to apply spines attrs: %s. (%s)", exc, dir(axes.spines[location])) # Prepare fig & axis fig_params = self.figure_params.copy() figsize = fig_params.pop('figsize', (16, 9)) position = fig_params.pop('position', [0.025, 0.075, 0.925, 0.875]) fontsize = fig_params.pop('fontsize', 10) x_style = fig_params.pop('x_axis', {}) x_style_labels = fig_params.pop('x_axis_labels', {}) x_style_ticks = fig_params.pop('x_axis_ticks', {}) y_style = fig_params.pop('y_axis', {}) y_style_labels = fig_params.pop('y_axis_labels', {}) y_style_ticks = fig_params.pop('y_axis_ticks', {}) partial_axis = fig_params.pop('partial_axis', True) # Create figure and format axis self._fig = figure.Figure(figsize=figsize, dpi=150, frameon=False) self._canvas = FigureCanvas(self._fig) ax = self._fig.gca(position=position) ax.yaxis.tick_right() ax.yaxis.set_label_position("right") ax.set_xlim([self.dbt_min, self.dbt_max]) ax.set_ylim([self.w_min, self.w_max]) ax.grid(False, which='major', axis='both') ax.grid(False, which='minor', axis='both') # Apply axis styles if fig_params['x_label'] is not None: style_axis = x_style_labels.copy() style_axis['fontsize'] *= 1.2 ax.set_xlabel(fig_params['x_label'], **style_axis) if fig_params['y_label'] is not None: style_axis = y_style_labels.copy() style_axis['fontsize'] *= 1.2 ax.set_ylabel(fig_params['y_label'], **style_axis) if fig_params['title'] is not None: ax.set_title(fig_params['title'], fontsize=fontsize * 1.5, fontweight='bold') _apply_spines_style(ax, y_style, location='right') _apply_spines_style(ax, x_style, location='bottom') if partial_axis: # Hide left and top axis ax.spines['left'].set_visible(False) ax.spines['top'].set_visible(False) else: _apply_spines_style(ax, y_style, location='left') _apply_spines_style(ax, x_style, location='top') if x_style_ticks: ax.tick_params(axis='x', **x_style_ticks) if y_style_ticks: ax.tick_params(axis='y', **y_style_ticks) if self.chart_params.get("with_constant_dry_temp", True): step_label = self.chart_params.get( "constant_temp_label_step", None) if step_label: # Explicit xticks ticks = f_range(self.dbt_min, self.dbt_max + step_label / 10, step_label) if not self.chart_params.get( "constant_temp_label_include_limits", True): ticks = [t for t in ticks if t not in [self.dbt_min, self.dbt_max]] ax.set_xticks(ticks) ax.set_xticklabels( ['{:g}'.format(t) for t in ticks], **x_style_labels) else: ax.set_xticks([]) if self.chart_params.get("with_constant_humidity", True): step_label = self.chart_params.get( "constant_humid_label_step", None) if step_label: # Explicit xticks ticks = f_range(self.w_min, self.w_max + step_label / 10, step_label) if not self.chart_params.get( "constant_humid_label_include_limits", True): ticks = [t for t in ticks if t not in [self.w_min, self.w_max]] ax.set_yticks(ticks) ax.set_yticklabels( ['{:g}'.format(t) for t in ticks], **y_style_labels) else: ax.set_yticks([]) # Plot curves: [getattr(self, curve_family).plot(ax) for curve_family in PSYCHRO_CURVES_KEYS if getattr(self, curve_family) is not None] # Plot zones: [zone.plot(ax=ax) for zone in self.zones] # Set the Axes object self._axes = ax return ax def remove_annotations(self) -> None: """Remove the annotations made in the chart to reuse it.""" for line in self._handlers_annotations: try: line[0].remove() except TypeError: line.remove() self._handlers_annotations = [] def remove_legend(self) -> None: """Remove the legend of the chart.""" if self._legend is not None: self._legend.remove() self._legend = None def save(self, path_dest: Any, **params: Any) -> None: """Write the chart to disk.""" if self._axes is None: self.plot() self._canvas.print_figure(path_dest, **params) gc.collect() def close_fig(self) -> None: """Close the figure plot.""" if self._axes is not None: self.remove_annotations() self.remove_legend() self._axes.remove() self._axes = None self._fig.clear() self._fig = None self._canvas = None gc.collect() def _print_err(self, *args: Any) -> None: if self._logger is not None: self._logger.error(*args) else: print(args[0] % args[1:])
def main(): fileName = args.resultsFile fn = fileName.split(".")[0] fh = open(fileName, "r") lines = fh.readlines() jtitle = json.loads(lines[0]) jfooter = json.loads(lines[len(lines) - 1]) try: resultsFile = p.read_json(fileName, orient='records', lines=True) except ValueError as e: print "oops %s" % (e) return # convert bytes to MB # resultsFile['memused'] = (resultsFile['memtotal'] - (resultsFile['memfree'])) / 1024 / 1024 resultsFile['cached'] = resultsFile['cached'] / 1024 / 1024 resultsFile['mapped'] = resultsFile['mapped'] / 1024 / 1024 resultsFile['processMem'] = resultsFile['processMem'] / 1024 / 1024 resultsFile['mossSize'] = resultsFile['num_bytes_used_disk'] / 1024 / 1024 resultsFile['dbSize'] = (resultsFile['totalKeyBytes'] + resultsFile['totalValBytes']) / 1024 / 1024 # # delta_ms is the time slice between samples in ms # resultsFile['delta_ms'] = ( resultsFile['cpu_user'] + resultsFile['cpu_idle'] + resultsFile['cpu_iowait'] + resultsFile['cpu_system']) * 1000 / jtitle['ncpus'] / 100 resultsFile['numKeysRead'] = resultsFile[ 'numKeysRead'] * 1000 / resultsFile['delta_ms'] resultsFile['numKeysWrite'] = resultsFile[ 'numKeysWrite'] * 1000 / resultsFile['delta_ms'] resultsFile[ 'read_ios'] = resultsFile['read_ios'] * 1000 / resultsFile['delta_ms'] resultsFile['write_ios'] = resultsFile['write_ios'] * 1000 / resultsFile[ 'delta_ms'] resultsFile['read_mbs'] = (resultsFile['read_sectors'] * 512 * 1000 / resultsFile['delta_ms']) / 1024 / 1024 resultsFile['write_mbs'] = (resultsFile['write_sectors'] * 512 * 1000 / resultsFile['delta_ms']) / 1024 / 1024 resultsFile['queuelen'] = resultsFile['avq'] / resultsFile['delta_ms'] resultsFile['iops'] = ( (resultsFile['read_ios'] + resultsFile['write_ios']) * 1000) / resultsFile['delta_ms'] d = resultsFile.describe(percentiles=[.0001, .999]) title = bldTitle(fn, jtitle, jfooter) footer = bldFooter(jfooter, jtitle, d) sdl() fig = Figure(figsize=(10, 12)) # # key reads/writes # ax = fig.add_subplot(911) minval = setMinVal('numKeysRead', d) maxval = setMaxVal('numKeysRead', d) y1 = np.array(resultsFile['numKeysRead']) x1 = np.arange(1, y1.size + 1) ax.plot(x1, y1, label='Key Reads', color='red', alpha=0.7) ax.set_ylabel('Key Reads', color='red') ax.set_ylim([minval, maxval]) ax.set_xticks([]) ax2 = ax.twinx() minval = setMinVal('numKeysRead', d) maxval = setMaxVal('numKeysRead', d) y1 = np.array(resultsFile['numKeysWrite']) x1 = np.arange(1, y1.size + 1) ax2.plot(x1, y1, label='Key Writes', color='blue', alpha=0.7) ax2.set_ylabel('Key Writes', color='blue') ax2.set_ylim([minval, maxval]) ax2.set_xticks([]) # # disk reads/writes # ax = fig.add_subplot(912) minval = setMinVal('read_ios', d) maxval = setMaxVal('read_ios', d) y1 = np.array(resultsFile['read_ios']) x1 = np.arange(1, y1.size + 1) ax.plot(x1, y1, label='Disk Reads', color='red', alpha=0.7) ax.set_ylabel('Disk Reads', color='red') ax.set_ylim([minval, maxval]) ax.set_xticks([]) ax2 = ax.twinx() minval = setMinVal('write_ios', d) maxval = setMaxVal('write_ios', d) y1 = np.array(resultsFile['write_ios']) x1 = np.arange(1, y1.size + 1) ax2.plot(x1, y1, label='Disk Writes', color='blue', alpha=0.7) ax2.set_ylabel('Disk Writes', color='blue') ax2.set_ylim([minval, maxval]) ax2.set_xticks([]) # # Read/Write Mb/s # ax = fig.add_subplot(913) minval = setMinVal('read_mbs', d) maxval = setMaxVal('read_mbs', d) y1 = np.array(resultsFile['read_mbs']) x1 = np.arange(1, y1.size + 1) ax.plot(x1, y1, label='Read Mb/s', color='red', alpha=0.7) ax.set_ylabel('Read Mb/s', color='red') ax.set_ylim([minval, maxval]) ax.set_xticks([]) ax2 = ax.twinx() minval = setMinVal('write_mbs', d) maxval = setMaxVal('write_mbs', d) y1 = np.array(resultsFile['write_mbs']) x1 = np.arange(1, y1.size + 1) ax2.plot(x1, y1, label='Write Mb/s', color='blue', alpha=0.7) ax2.set_ylabel('Write Mb/s', color='blue') ax2.set_ylim([minval, maxval]) ax2.set_xticks([]) # # iops and ioq # ax = fig.add_subplot(914) minval = setMinVal('iops', d) maxval = setMaxVal('iops', d) y1 = np.array(resultsFile['iops']) x1 = np.arange(1, y1.size + 1) ax.plot(x1, y1, label='iops', color='green', alpha=0.7) ax.set_ylabel('iops', color='green') ax.set_ylim([minval, maxval]) ax.set_xticks([]) ax2 = ax.twinx() minval = setMinVal('queuelen', d) maxval = setMaxVal('queuelen', d) y1 = np.array(resultsFile['queuelen']) x1 = np.arange(1, y1.size + 1) ax2.plot(x1, y1, label='I/O Queue', color='yellow', alpha=0.7) ax2.set_ylabel('I/O Queue', color='yellow') ax2.set_ylim([minval, maxval]) ax2.set_xticks([]) # # memory # ax = fig.add_subplot(915) minval = min(d['memused']['min'], d['cached']['min'], d['mapped']['min'], d['processMem']['min']) maxval = max(d['memused']['max'], d['cached']['max'], d['mapped']['max'], d['processMem']['max']) y1 = np.array(resultsFile['memused']) y2 = np.array(resultsFile['cached']) y3 = np.array(resultsFile['mapped']) y4 = np.array(resultsFile['processMem']) x1 = np.arange(1, y1.size + 1) ax.plot(x1, y1, label='Total Sys Memory', color='red', alpha=0.7) ax.plot(x1, y2, label='Total FS Cache', color='blue', alpha=0.7) ax.plot(x1, y3, label='Total MMap', color='green', alpha=0.7) ax.plot(x1, y4, label='Process Mem', color='yellow', alpha=0.7) ax.set_ylim([minval, maxval]) ax.legend(loc='upper center', fontsize="small", bbox_to_anchor=(0.5, 1.20), ncol=4, fancybox=True) ax.set_xticks([]) # # database size # ax = fig.add_subplot(916) minval = min(d['dbSize']['min'], d['mossSize']['min']) maxval = max(d['dbSize']['max'], d['mossSize']['max']) y1 = np.array(resultsFile['dbSize']) y2 = np.array(resultsFile['mossSize']) x1 = np.arange(1, y1.size + 1) ax.plot(x1, y1, label='DB Size', color='red', alpha=0.7) ax.plot(x1, y2, label='Moss Size', color='blue', alpha=0.7) ax.legend(loc='upper center', fontsize="small", bbox_to_anchor=(0.5, 1.20), ncol=2, fancybox=True) ax.set_ylim([minval, maxval]) ax.set_xticks([]) # # cpu time # ax = fig.add_subplot(917) minval = min(setMinVal('cpu_user', d), setMinVal('cpu_system', d), setMinVal('cpu_idle', d), setMinVal('cpu_iowait', d)) maxval = min(setMaxVal('cpu_user', d), setMaxVal('cpu_system', d), setMaxVal('cpu_idle', d), setMaxVal('cpu_iowait', d)) y1 = np.array(resultsFile['cpu_user']) y2 = np.array(resultsFile['cpu_system']) y3 = np.array(resultsFile['cpu_iowait']) y4 = np.array(resultsFile['cpu_idle']) x1 = np.arange(1, y1.size + 1) ax.plot(x1, y1, label='User', color='blue', alpha=0.7) ax.plot(x1, y2, label='System', color='red', alpha=0.7) ax.plot(x1, y3, label='IOwait', color='green', alpha=0.7) ax.plot(x1, y4, label='Idle', color='yellow', alpha=0.7) ax.legend(loc='upper center', fontsize="small", bbox_to_anchor=(0.5, 1.20), ncol=4, fancybox=True) ax.set_ylim([minval, maxval]) ax.set_xticks([]) # # moss statistics # ax = fig.add_subplot(918) minval = min(d['mhBlocks']['min'], d['total_persists']['min'], d['num_segments']['min'], d['num_files']['min']) maxval = max(d['mhBlocks']['max'], d['total_persists']['max'], d['num_segments']['max'], d['num_files']['max']) y1 = np.array(resultsFile['mhBlocks']) y2 = np.array(resultsFile['total_persists']) y3 = np.array(resultsFile['num_files']) y4 = np.array(resultsFile['num_segments']) x1 = np.arange(1, y1.size + 1) ax.plot(x1, y1, label='Blocks', color='red', alpha=0.7) ax.plot(x1, y2, label='Persists', color='green', alpha=0.7) ax.plot(x1, y3, label='Files', color='magenta', alpha=0.7) ax.plot(x1, y4, label='Segments', color='black', alpha=0.7) ax.legend(loc='upper center', fontsize="small", bbox_to_anchor=(0.5, 1.20), ncol=4, fancybox=True) ax.set_ylim([minval, maxval]) ax.set_xticks([]) ax2 = ax.twinx() minval = 0 maxval = d['total_compactions']['max'] y1 = np.array(resultsFile['total_compactions']) x1 = np.arange(1, y1.size + 1) ax2.plot(x1, y1, label='Compactions', color='blue', alpha=0.7) ax2.set_ylabel('Compactions', color='blue') ax2.set_ylim([minval, maxval]) ax2.set_xticks([]) fig.suptitle(title, fontsize=12) fig.text(0.1, 0, footer, fontsize=11) canvas = FigureCanvasAgg(fig) outFile = "%s.png" % (fn) canvas.print_figure(outFile, dpi=80)
def print_figure(self, *args, **kwargs): FigureCanvasAgg.print_figure(self, *args, **kwargs) self.draw()
def nvnReportPipe( self): #Executes functions that generate result.html file try: steps = int(self.paraSteps.get()) hrr = int(self.paraHrr.get()) except: self.printer( "You need to enter integer values for HRR and step parameters.\n" ) if self.listbox.curselection() != (): query = self.genelist[int(self.listbox.curselection()[0])][0] #### Plot expression profile function try: plotDatabase = codecs.open( open('dbconf.txt', 'r').read().rstrip().split(".")[0] + ".plt", mode='r', encoding='ASCII', errors='ignore').readlines() ticka = [""] + plotDatabase[0].replace( ",", "\n").lstrip().rstrip().split("\t") for i in plotDatabase: if query in i: query = i data = query.split()[1:] temp = [] for i in range(len(data)): temp.append([ map(float, data[i].replace("-", "\t").rstrip().split()), average( map(float, data[i].replace("-", "\t").rstrip().split())) ]) fig = plt.figure(figsize=(12, 7)) ax = fig.add_subplot(111) plt.subplots_adjust(left=0.1, right=0.97, top=0.93, bottom=0.3) ax.set_ylabel("Signal value") ax.set_title(query.split()[0]) ax.grid(True) plt.xticks(range(len(ticka) + 1), ticka, rotation=90, fontsize="small", horizontalalignment="center") ax.plot([0], [0]) crossX = [] crossY = [] for i in range(len(temp)): ax.plot([i + 1] * len(temp[i][0]), temp[i][0], "g.") crossX.append([i + 1]) crossY.append(temp[i][1]) ax.plot(crossX, crossY, "-ro") ax.plot([i + 2], [0]) canvas = FigureCanvasAgg(fig) canvas.print_figure("profile.png") plt.clf() except: self.printer( "Failed to generate an expression profile of your gene of interes.\nThe expression matrix used for plotting of expression profiles must be present and named " + open('dbconf.txt', 'r').read().rstrip().split(".")[0] + ".plt!") ###Call network creator try: networkViewer.makeNetwork(query.split()[0], steps, hrr) except: self.printer( "Failed to generate an co-expression network of your gene of interes.\nThe HRR network file used must be present named " + open('dbconf.txt', 'r').read().rstrip().split(".")[0] + ".hrr!") ### Calculate PCC of a gene to all genes in database try: query = self.queries[int( self.listbox.curselection()[0])].split("\t") expVector = map(float, query[5:]) expVector = numpy.array(expVector) nomi = expVector - (numpy.sum(expVector) / len(expVector)) denomi = numpy.sqrt(numpy.sum(nomi**2)) rValues = numpy.dot(self.nominator, nomi) / numpy.dot( self.denominator, denomi) displayList = [] for i in range(len(rValues)): displayList.append([rValues[i], self.annoDict[i]]) displayList.sort(reverse=True) except: displayList = [] self.printer( "Failed to calculate Pearson correlation co-efficient list.\n" ) ###Create html document with results header = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">\n<html>\n<head>\n<!-- blarg -->\n</head>\n<body>\n' try: header += '<big><b>Summary page for: %s</b></big>\n<br><b>Expression profile:</b>\n<IMG SRC="profile.png"><br>\n' % ( displayList[0][1].split()[0] + "\t" + displayList[0][1].split()[1]) except: pass header += '<b>HRR based co-expression network:</b>\nGreen, organge and red edges represent HRR values of %s, %s and %s, respectively.</b><br>\n' % ( int(hrr) / 3, (int(hrr) / 3) * 2, hrr) header += '<embed src="network.svg" width="1200" height="1200" type="image/svg+xml" pluginspage="http://www.adobe.com/svg/viewer/install/" />\n<br>' #header += '<iframe src="network.svg" width="1500" height="1500">\n</iframe>\n' header += "<br><b>MapMan ontology analysis of the above network:</b>\n" try: header += open("mapRes.mapman", "r").read() except: pass header += "\n<br><b>Pearson correlation based co-expression analysis:</b>\n<pre>" v = open("result.html", "w") v.close() v = open("result.html", "a") v.write(header) for i in range(len(self.annoDict)): v.write( str(displayList[i][0])[:6] + "\t" + displayList[i][1] + "\n") v.write("</pre>") v.close() self.printer( "Probeset specific result calculated and available in result.html file.\n" )
import matplotlib from matplotlib import figure from matplotlib.backends.backend_agg import (FigureCanvasAgg as FigureCanvas) import matplotlib.cm as mcm import matplotlib.colors as mcolors data = np.random.randn(50, 50) fig = figure.Figure() canvas = FigureCanvas(fig) cmap = mcm.RdBu ax1 = fig.add_subplot(1, 2, 1) plt1 = ax1.imshow(data, interpolation='nearest', cmap=cmap) cbar1 = fig.colorbar(plt1, ax=ax1, fraction=0.045) norm = mcolors.BoundaryNorm(np.arange(-2, 2.5, .5), cmap.N) ax2 = fig.add_subplot(1, 2, 2) plt2 = ax2.imshow(data, interpolation='nearest', cmap='RdBu', norm=norm) cbar2 = fig.colorbar(plt2, ax=ax2, fraction=0.045, extend='both') cbar2.cmap.set_over('green') cbar2.cmap.set_under('orange') cbar2.set_label("Random Data") fig.subplots_adjust(wspace=.4) canvas.print_figure('../figures/cmapdiscrete.png', facecolor='lightgray')
borderpad=0.2, handlelength=3) leg2_frame = leg2.get_frame() leg2_frame.set_edgecolor("white") # This code draws major and minor tick lines. Major ticks get number labels. majorLocator_x = MultipleLocator(3333.333333333) minorLocator_x = MultipleLocator(250) ax1.xaxis.set_major_locator(majorLocator_x) ax1.xaxis.set_minor_locator(minorLocator_x) majorLocator_y1 = MultipleLocator(100) minorLocator_y1 = MultipleLocator(20) ax1.yaxis.set_major_locator(majorLocator_y1) ax1.yaxis.set_minor_locator(minorLocator_y1) majorLocator_y2 = MultipleLocator(50) minorLocator_y2 = MultipleLocator(10) ax2.yaxis.set_major_locator(majorLocator_y2) ax2.yaxis.set_minor_locator(minorLocator_y2) # This allows us to set the font size of the tick number labels. x_gridlines = ax1.xaxis.get_gridlines() for tl in ax1.get_xticklabels(): tl.set_fontsize(12) # Ok, all done so we write out the image. canvas = FigureCanvas(fig) canvas.print_figure("stocks2.png", dpi=72.0) plt.show()