def plotfuncs(mydata, corde_data, toplabel): x = np.float64(mydata) y = corde_data del_data = x-y fig = plt.figure() ax = fig.add_subplot(1, 2, 1) # mt.hist(del_data, bins=20, range=(-1, 1), ax=ax) mt.hist(del_data, bins=20, ax=ax) mt.addlabel(toplabel=toplabel) # z = np.polyfit(x, y, 1) p2 = np.poly1d([1, 0]) # fig = plt.figure() ax = fig.add_subplot(1, 2, 2) ax.plot(x, y, '.', x, p2(x), 'r') mt.addlabel(toplabel=toplabel)
# Create larger gridspec outergs= gs.GridSpec(3,2) # pdb.set_trace() # ====================================== # Find spot size for each step # ====================================== # mt.figure('Shot') for i,img in enumerate(imgs): # img=np.flipud(np.rot90(f[img[0]],3)) img=np.flipud(np.rot90(f[img[0]])) ax=fig.add_subplot(outergs[i]) ax.imshow(img[350:600,xstart:xstop],aspect='auto',interpolation='none') fig.add_subplot(ax) plt.figure(fig.number) outergs.tight_layout(fig,pad=5) mt.addlabel(toplabel='$\Delta E$={}GeV'.format(stepvalues[i]),xlabel='x [px]',ylabel='y [px]') # plt.show() # Fit individual slices popt,pcov,chisq_red[i] = mt.fitimageslice( img, res_x, res_y, (xstart,xstop), (ystart,ystop), plot=True ) mt.addlabel(toplabel='Gaussian Fit to Spot Profile, $\Delta E$={}GeV'.format(stepvalues[i]),xlabel='x [m]',ylabel='Counts') variance[i] = popt[2] bact = setQS.set_QS_energy_ELANEX(stepvalues[i])
def results2pdf(path, local=False, verbose=False, loglevel=logging.CRITICAL): # ===================================== # Regex-out the dataset number, date # ===================================== dataset_string = re.search('E200_(\d+)', path).groups()[0] date_string_tmp = re.search('\d{8}', path).group() year = np.int(date_string_tmp[0:4]) month = np.int(date_string_tmp[4:6]) day = np.int(date_string_tmp[6:]) date = dt.date(year=year, month=month, day=day) date_string = date.strftime('%b %-d, %Y') # ===================================== # Load data # ===================================== data = E200.E200_load_data(path, local=local, readonly=True) processed_drill = data.wdrill.data.processed scalars_drill = processed_drill.scalars vectors_drill = processed_drill.vectors arrays_drill = processed_drill.arrays scalars_rdrill = data.rdrill.data.raw.scalars # ===================================== # Helper function # ===================================== def get_str(drill): out_str = drill._hdf5 return drill, out_str # ===================================== # get uids, energy axes, images, etc # ===================================== valid = scalars_drill.ss_ELANEX_valid uids = valid.UID[valid.dat is True] # uids = uids[0:4] valid_drill, valid_str = get_str(scalars_drill.ss_ELANEX_valid) valid = E200.E200_api_getdat(valid_str, uids) quadval_drill, quadval_str = get_str(scalars_rdrill.step_value) quadval = E200.E200_api_getdat(quadval_str, uids) emit_n_drill, emit_n_str = get_str(scalars_drill.ss_ELANEX_emit_n) emit_n = E200.E200_api_getdat(emit_n_str, uids) betastar_drill, betastar_str = get_str(scalars_drill.ss_ELANEX_betastar) betastar = E200.E200_api_getdat(betastar_str, uids) sstar_drill, sstar_str = get_str(scalars_drill.ss_ELANEX_sstar) sstar = E200.E200_api_getdat(sstar_str, uids) rect_drill, rect_str = get_str(vectors_drill.ss_ELANEX_rect) rects = E200.E200_api_getdat(rect_str, uids) eaxis_drill, eaxis_str = get_str(vectors_drill.ss_ELANEX_energy_axis) eaxis = E200.E200_api_getdat(eaxis_str, uids) variance_drill, variance_str = get_str(vectors_drill.ss_ELANEX_variance) variance = E200.E200_api_getdat(variance_str, uids) beta_drill, beta_str = get_str(vectors_drill.ss_ELANEX_LLS_beta) beta = E200.E200_api_getdat(beta_str, uids) y_error_drill, y_error_str = get_str(vectors_drill.ss_ELANEX_LLS_y_error) y_error = E200.E200_api_getdat(y_error_str, uids) X_unweighted_drill, X_unweighted_str = get_str(arrays_drill.ss_ELANEX_LLS_X_unweighted) X_unweighted = E200.E200_api_getdat(X_unweighted_str, uids) selected_img_drill, selected_img_str = get_str(arrays_drill.ss_ELANEX_selected_img) selected_img = E200.E200_api_getdat(selected_img_str, uids) oimg_eaxis_drill, oimg_eaxis_str = get_str(vectors_drill.ss_ELANEX_oimg_eaxis) oimg_eaxis = E200.E200_api_getdat(oimg_eaxis_str, uids) elanex_drill, elanex_str = get_str(data.rdrill.data.raw.images.ELANEX) images = E200.E200_load_images(elanex_str, uids) E224_Probe_drill, E224_Probe_str = get_str(data.rdrill.data.raw.images.E224_Probe) # ===================================== # Determine laser on shots # ===================================== laseron = laser_on_off(E224_Probe_str, uids) # ===================================== # Determine number of shots, pages # ===================================== num_shots = len(valid) slots_per_page = 5 pages = np.int(np.ceil(num_shots/np.float(slots_per_page))) # ===================================== # Create filename, pdf, main title # ===================================== now = dt.datetime.now() filename = '{}_processed_{}-{:02d}-{:02d}_{:02d}{:02d}.pdf'.format(dataset_string, now.year, now.month, now.day, now.hour, now.minute) pdf = PdfPages(filename) main_title = 'Single Shot Emittance Analysis, Dataset {dataset}, {datestring}, Page {{}}/{totalpgs}'.format( dataset = dataset_string, datestring = date_string, totalpgs = pages+1 ) layout_rect = [0, 0, 1, 0.95] # ===================================== # Create overview # ===================================== worksheet(data, pdf, main_title) # ===================================== # Basic formatting options # ===================================== linewidth = 1 fontsize = 7 # ===================================== # Create pages with slots_per_page # number of analysis/page # ===================================== for i in range(pages): gs = gridspec.GridSpec(5, 3) fig = plt.figure(figsize=(8.5, 11)) fig.suptitle(main_title.format(i+2)) # ===================================== # Add analysis to each slot # ===================================== for j in range(slots_per_page): indx = i*slots_per_page+j print(indx) # ===================================== # Only add if there are shots left # ===================================== if indx < num_shots: # ===================================== # Plot Energy # ===================================== img_to_plot = np.fliplr(np.rot90(images.images[indx])) uid = images.UID[indx] ax = fig.add_subplot(gs[j, 0]) e_axis = oimg_eaxis.dat[indx] e_axis = np.flipud(e_axis) # ===================================== # Get energy, pixel ranges # ===================================== # x_min = 1 x_max = img_to_plot.shape[0] # x_range = x_max-x_min # y_min = e_axis[-1] # y_max = e_axis[0] # y_max = img_to_plot.shape[1] # y_range = y_max-y_min # aspect = x_range/y_range * img_to_plot.shape[1]/img_to_plot.shape[0] # aspect = np.power(np.float64(img_to_plot.shape[1])/np.float64(img_to_plot.shape[0]), 2) # extent = (x_min, x_max, y_min, y_max) # ===================================== # Show image # ===================================== # ax.imshow(img_to_plot, extent=extent, aspect=aspect, origin='lower') # ax.imshow(img_to_plot, aspect=aspect, origin='lower') # img = mpl.image.NonUniformImage(ax) # img.set_data(e_axis, mt.linspacestep(1, x_max), img_to_plot) pixels = selected_img.dat[indx].flatten() hist, edges = np.histogram(pixels, bins=50) new_edges = edges[1:] max_count = np.max(new_edges[hist > 50]) img = mt.NonUniformImage(e_axis, mt.linspacestep(1, x_max), img_to_plot, ax=ax) img.set_clim([0, max_count]) mt.addlabel(axes = ax, ylabel='Pixels', xlabel='Energy [GeV]') cb = fig.colorbar(img) ax.tick_params(labelsize = fontsize) cb.ax.tick_params(labelsize = fontsize) ax.get_figure().tight_layout() # ===================================== # Get rectangle info # ===================================== thisrect = rects.dat[indx] # ===================================== # Retool y to energy scale # ===================================== # x = thisrect[0] # y = thisrect[1] # width = thisrect[2] # height=thisrect[3] myrect = mt.qt.Rectangle(*thisrect) y0 = myrect.x0 y1 = myrect.x1 temp = np.flipud(e_axis) e_y0 = temp[np.int(np.round(y0))] e_y1 = temp[np.int(np.round(y1))] e_width = e_y1-e_y0 # rect = mt.qt.Rectangle(x, e_y0, e_width, height, axes=ax, alpha=1, fill=False) rect = mt.qt.Rectangle(e_y0, y, e_width, height, axes=ax, alpha=1, fill=False) ax.add_patch(rect.get_rect()) mt.graphics.less_labels(ax, y_fraction=1) mt.graphics.axesfontsize(ax, fontsize) # ===================================== # Plot Fit # ===================================== figlabel = 'Butterfly Fit' ax = fig.add_subplot(gs[j, 1]) ax0 = bt.plotfit( eaxis.dat[indx], variance.dat[indx], beta.dat[indx], X_unweighted.dat[indx], top = 'Emittance and Beam Parameter Fit', figlabel = figlabel, bottom = 'Energy [GeV]', axes = ax, error = y_error.dat[indx], linewidth = linewidth, fontsize = fontsize, markersize = 2 ) mt.graphics.less_labels(ax0, y_fraction=1) mt.graphics.axesfontsize(ax0, fontsize) ax0.get_figure().tight_layout() # ===================================== # Add numbers # ===================================== ax = fig.add_subplot(gs[j, 2]) # Calculate pinch energy size = np.dot(X_unweighted.dat[indx], beta.dat[indx]) argmin = np.argmin(size) pinch_energy = eaxis.dat[indx][argmin] if laseron[indx]: laser_status = 'On' else: laser_status = 'Off' table_begin = r''' \begin{{tabular}}{{ll}} \toprule Date & {} \\ UID & {:0.0f} \\ Quad Offset [GeV] & {:0.3f} \\ \midrule Pinch Energy [GeV] & {:0.3f} \\ Norm Emit [mm-mrad] & {:0.3f} \\ Beta* [m] & {:0.3f} \\ S* [m] & {:0.3f} \\ Laser & {} \\ \bottomrule \end{{tabular}}''' table_begin = table_begin.format( date_string, uid, quadval.dat[indx], pinch_energy, emit_n.dat[indx]*1e6, betastar.dat[indx], sstar.dat[indx], laser_status ) table_begin = re.sub('\\n', ' ', table_begin) # table_begin = r'''\begin{tabular}{ c | c | c | c } & col1 & col2 & col3 \\\hline row1 & 11 & 12 & 13 \\\hline row2 & 21 & 22 & 23 \\\hline row3 & 31 & 32 & 33 \end{tabular}''' ax.text(-0.1, 0.25, table_begin, fontsize=fontsize) ax.set_frame_on(False) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) ax.get_figure().tight_layout() gs.tight_layout(fig, rect=layout_rect) pdf.savefig(fig) plt.close() pdf.close() command = 'open {}'.format(filename) subprocess.call(shlex.split(command))
def coversheet(data, pdf, main_title): gs = gridspec.GridSpec(3, 2) fig = plt.figure(figsize=(8.5, 11)) fig.suptitle(main_title.format(1)) ax = fig.add_subplot(gs[0, 0]) # data=E200.E200_load_data('nas/nas-li20-pm00/E200/2014/20140629/E200_13537', local=True, readonly=True) dats = E200.E200_api_getdat(data.wdrill.data.processed.scalars.ss_ELANEX_valid._hdf5) uids = dats.UID[dats.dat is True] emit_dats = E200.E200_api_getdat(data.wdrill.data.processed.scalars.ss_ELANEX_emit_n._hdf5, UID=uids) imgstr = data.rdrill.data.raw.images.E224_Probe._hdf5 laseron = laser_on_off(imgstr, uids) if not np.any(laseron is True): laser_on_exist = False logger.log(level=loggerlevel, msg='No laser on shots') else: laser_on_exist = True if not np.any(laseron is False): laser_off_exist = False logger.log(level=loggerlevel, msg='No laser off shots') else: laser_off_exist = True # ===================================== # Create figure if no axes specified # ===================================== if ax is None: fig = plt.figure() ax = fig.add_subplot(1, 1, 1) # ===================================== # Get Numbers # ===================================== norm_emit_on = emit_dats.dat[laseron == 1]*1e6 if len(norm_emit_on) > 0: avg_norm_emit_on = '{:0.3f}'.format(np.mean(norm_emit_on)) std_norm_emit_on = '{:0.3f}'.format(np.std(norm_emit_on)) else: avg_norm_emit_on = 'N/A' std_norm_emit_on = 'N/A' norm_emit_off = emit_dats.dat[laseron == 0]*1e6 # ipdb.set_trace() if len(norm_emit_off) > 0: avg_norm_emit_off = '{:0.3f}'.format(np.mean(norm_emit_off)) std_norm_emit_off = '{:0.3f}'.format(np.std(norm_emit_off)) else: avg_norm_emit_off = '(N/A)' std_norm_emit_off = '(N/A)' # ===================================== # Plot logic # ===================================== if laser_on_exist and laser_off_exist: # out_plot = ax.plot(norm_emit_on, '.-', norm_emit_off, '.-') ax.legend(['Laser On', 'Laser Off'], fontsize=fontsize) mt.addlabel(toplabel = 'Effect of Laser on Emittance', axes=ax) elif laser_on_exist and laser_off_exist is False: # out_plot = ax.plot(norm_emit_on, '.-') ax.legend(['Laser On'], fontsize=fontsize) toplabel = 'Effect of Laser on Emittance\nNO LASER OFF DATA' ax.set_title(toplabel, color='red') elif laser_on_exist is False and laser_off_exist: # out_plot = ax.plot(norm_emit_off, '.-') ax.legend(['Laser On'], fontsize=fontsize) toplabel = 'Effect of Laser on Emittance\nNO LASER ON DATA' ax.set_title(toplabel, color='red') else: raise NotImplementedError('Check laser on/off arrays') mt.addlabel( axes = ax, xlabel = 'Unsorted Shots', ylabel = 'Norm. Emittance [mm-mrad]') # fig.tight_layout() # # plt.show() # # fig.savefig('NormEmit_Laser_On_Off_13537.jpg') mt.graphics.axesfontsize(ax, fontsize=7) ax = fig.add_subplot(gs[0, 1]) table_begin = r''' \begin{{tabular}}{{ll}} \toprule \multicolumn{{2}}{{c}}{{Laser On}} \\ \midrule Average [mm-mrad] & {} \\ Std. Dev. [mm-mrad] & {} \\ \toprule \multicolumn{{2}}{{c}}{{Laser Off}} \\ \midrule Average [mm-mrad] & {} \\ Std. Dev. [mm-mrad] & {} \\ \bottomrule \end{{tabular}}''' table_begin = table_begin.format( avg_norm_emit_on, std_norm_emit_on, avg_norm_emit_off, std_norm_emit_off ) table_begin = re.sub('\\n', ' ', table_begin) ax.text(0.5, 1, table_begin, fontsize=fontsize, ha='center', va='top') ax.set_frame_on(False) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) ax.get_figure().tight_layout() gs.tight_layout(fig, rect=layout_rect) pdf.savefig(fig) return pdf
# ===================================== # Plot Emittance # ===================================== y = emit_n*1e6 fig = plt.figure() ax = fig.add_subplot(1, 1, 1) line = ax.plot(result, y, 'o-', linewidth=linewidth) toplabel = 'Ionization-Injected Witness Emittance' windowlabel = toplabel xlabel = 'Result #' ylabel = 'Emittance [mm-mrad]' mt.addlabel(fig=fig, axes=ax, windowlabel=windowlabel, toplabel=toplabel, xlabel=xlabel, ylabel=ylabel) fig.tight_layout() mt.graphics.savefig(filename=toplabel, path=figpath) # ===================================== # Plot Spot size # ===================================== y = np.sqrt(emit*betastar)*1e6 fig = plt.figure() ax = fig.add_subplot(1, 1, 1) line = ax.plot(result, y, 'o-', linewidth=linewidth) windowlabel = 'Spot Size'
yy = yy[shot17.ystart:shot17.ystop] xx = xx[shot17.xstart:shot17.xstop]*shot17.res*1e6/np.sqrt(2) xx = xx-np.mean(xx) vmax = 1600 p1 = ax.pcolormesh(xx, yy, shot17.img.transpose(), vmax=vmax, cmap=cm.RdBu_r) # p1 = ax.contourf(xx, yy, shot17.img.transpose(), vmax=1500, cmap=cm.Blues) cbar = fig.colorbar(p1) p2 = ax.contour(xx, yy, shot17.img.transpose(), levels=np.linspace(0, vmax, 6), colors='Black', linewidths=linewidth) ax.axhspan(ymin=yy[36*5], ymax=yy[37*5], color='Red', fill=False, linewidth=4) ax.axis([xx.min(), xx.max(), yy.min(), yy.max()]) toplabel = 'Ionization-Injected Witness Profile at Lanex' windowlabel = 'Lanex' mt.addlabel(fig=fig, axes=ax, windowlabel=windowlabel, toplabel=toplabel, ylabel='Energy [GeV]', xlabel='Horizontal Axis $x$ [$\mu$m]', cb=cbar, clabel='Counts') mt.graphics.less_labels(ax, x_fraction=1) mt.graphics.axesfontsize(ax, fontsize) fig.tight_layout() mt.graphics.savefig(filename=toplabel, path=figpath) # ===================================== # Plot Gauss # ===================================== gfit = shot17.gaussfits[36] fig2 = plt.figure() ax2 = fig2.add_subplot(1, 1, 1)
# y_scorde, energy_scorde = ce.Energy_Axis_CMOS_FAR(13438, 1.5) else: raise NotImplementedError('Camera name is not available: {}'.format(camname)) energy_scorde = np.flipud(energy_scorde) # ====================================== # Get my energy # ====================================== rf = data.read_file data_rf = rf['data'] # myenergy, myenergy_approx = E200.eaxis(y=y_scorde, uid=uid_scorde[0], camname=camname, hdf5_data=data_rf, E0=20.35, etay=0, etapy=0) myenergy_approx = E200.Energy_Axis(camname=camname, hdf5_data=data_rf, uid=uid_scorde[0]).energy(ypx=y_scorde) fig_en = plt.figure() mygs = gs.GridSpec(1, 1) ax1 = fig_en.add_subplot(mygs[0]) plots = ax1.plot(y_scorde, energy_scorde, 'b', y_scorde, myenergy_approx, 'r') # plots=ax1.plot(y_scorde, myenergy_approx, 'g') # plots=ax1.plot(y_scorde, energy_scorde, 'b') # plots[0].set_label('Sebastien') # plots[1].set_label('Joel') # ax1.legend() # ax1.set_yscale('log') mt.addlabel(toplabel=camname, xlabel='Pixels', ylabel='Energy [GeV]', axes=ax1) plt.show()
def calcR12(E): beamline.gamma = E/emass return beamline.R[0, 1] R11 = beamline.R[0, 0] R12 = beamline.R[0, 1] R11p = derivative(calcR11, val) R12p = derivative(calcR12, val) Eres2[i] = val - (R11*R11p*twiss.beta**2+R12*R12p)/(R11p**2*twiss.beta**2 + R12p**2) print('Energy should be {}'.format(Eres2[i])) fig = plt.figure() plt.plot(Escan, Eres, '.-') mt.addlabel('Pinch Energy Proportional to Imaging Energy', 'Image Energy', 'Pinch Energy') pp.savefig(fig) pp.close() for i, val in enumerate(Escan): Eres3[i] = betafromE(20.35, val) Eres4[i] = alphafromE(20.35, val) plt.figure() plt.plot(Escan, Eres3, '.-') plt.figure() plt.plot(Escan, Eres4, '.-') plt.tight_layout() # plt.show()