def analyze_matlab( data = None , camname = None , oimg = None , rect = None , fitpts = None , roiaxes = None , plotaxes = None , verbose = False , uid = None ): plt.close() # ====================================== # Log inputs # ====================================== frame = inspect.currentframe() args, temp, temp, values = inspect.getargvalues(frame) logger.log(level=1, msg='Input values:') for i, arg in enumerate(args): # pdb.set_trace() logger.log(level=1, msg='Arg name: {}, arg value: {}'.format(arg, values[arg])) logger.log(level=1, msg='Rectangle: x0: {}, y0: {}, x1: {}, y1: {}, width: {}, height: {}'.format( rect.x0 , rect.y0 , rect.x1 , rect.y1 , rect.get_width() , rect.get_height() ) ) # ====================================== # Load and transfer matlab variables # ====================================== # if (f is None): # infile = 'forpython.mat' # f = h5.File(infile); f = data.file if data is None: data = f['data'] if camname is None: camname = f['camname'] camname = mt.derefstr(camname) # if imgnum is None: # imgnum = f['imgnum'][0, 0] imgstr = data['raw']['images'][str(camname)] res = imgstr['RESOLUTION'][0, 0] res = res*1.0e-6 # ====================================== # Quadrupole set point # ====================================== raw_rf = data['raw'] scalars_rf = raw_rf['scalars'] setQS_str = scalars_rf['step_value'] setQS_dat = E200.E200_api_getdat(setQS_str, uid).dat[0] setQS = mt.hardcode.setQS(setQS_dat) logger.info('setQS_dat is: {}'.format(setQS_dat)) # logger.debug('setQS_dat type is: {}'.format(type(setQS_dat))) # ====================================== # Bend magnet strength # ====================================== B5D36 = data['raw']['scalars']['LI20_LGPS_3330_BDES']['dat'] B5D36 = mt.derefdataset(B5D36, f) B5D36_en = B5D36[0] # new_en = (B5D36_en-4.3) gamma = (B5D36_en/0.5109989)*1e3 # new_gamma = (new_en/0.5109989)*1e3 # ====================================== # Translate into script params # ====================================== n_rows = 5 if rect is None: xstart = 275 xstop = 325 ystart = 1870 ystop = 1900 else: xstart = rect.y0 xstop = rect.y1 ystart = rect.x0 ystop = rect.x1 xstart = np.round(xstart) xstop = np.round(xstop) ystart = np.round(ystart) ystop = np.round(ystop) # ====================================== # Get image # ====================================== # if oimg is None: # oimg = E200.E200_load_images(imgstr, f) # oimg = oimg.image[imgnum-1, :, :] # oimg=np.fliplr(oimg) img = oimg[xstart:xstop, ystart:ystop] # if roiaxes is not None: # imgplot = roiaxes.imshow(img, interpolation='none', origin='lower', aspect='auto') # ====================================== # Create a histogram of std dev # ====================================== hist_vec = mt.linspacestep(ystart, ystop, n_rows) n_groups = np.size(hist_vec) # hist_data = np.zeros([n_groups, 2]) x_pix = np.round(mt.linspacestep(xstart, xstop-1, 1)) x_meter = (x_pix-np.mean(x_pix)) * res if camname == 'ELANEX': logger.log(level=logging.INFO, msg='Lanex is tipped at 45 degrees: dividing x axis by sqrt(2)') x_meter = x_meter/np.sqrt(2) # x_sq = x_meter**2 num_pts = n_groups variance = np.zeros(num_pts) gaussresults = np.empty(num_pts, object) stddev = np.zeros(num_pts) varerr = np.zeros(num_pts) chisq_red = np.zeros(num_pts) y = np.array([]) for i in mt.linspacestep(0, n_groups-1): sum_x = np.sum(img[:, i*n_rows:(i+1)*n_rows], 1) y = np.append(y, ystart+n_rows*i+(n_rows-1.)/2.) # popt, pcov, chisq_red[i] = mt.gaussfit(x_meter, sum_x, sigma_y=np.sqrt(sum_x), plot=False, variance_bool=True, verbose=False, background_bool=True) gaussresults[i] = mt.gaussfit(x_meter, sum_x, sigma_y=np.sqrt(sum_x), plot=False, variance_bool=True, verbose=False, background_bool=True) variance[i] = gaussresults[i].popt[2] # varerr[i] = pcov[2, 2] # stddev[i] = np.sqrt(pcov[2, 2]) # ====================================== # Remove nan from arrays # ====================================== nan_ind = np.logical_not(np.isnan(variance)) variance = variance[nan_ind] stddev = stddev[nan_ind] varerr = varerr[nan_ind] chisq_red = chisq_red[nan_ind] y = y[nan_ind] # ====================================== # Get energy axis # ====================================== if camname == 'ELANEX': ymotor = data['raw']['scalars']['XPS_LI20_DWFA_M5']['dat'] ymotor = mt.derefdataset(ymotor, f) ymotor = ymotor[0]*1e-3 # print 'Ymotor is {}'.format(ymotor) logger.log(level=loggerlevel, msg='First: Original ymotor is: {}'.format(ymotor)) ymotor = setQS.elanex_y_motor()*1e-3 logger.log(level=loggerlevel, msg='First: Reconstructed ymotor is: {ymotor}'.format(ymotor=ymotor)) else: ymotor = None # eaxis = E200.eaxis(camname=camname, y=y, res=res, E0=20.35, etay=0, etapy=0, ymotor=ymotor) EnAxis = E200.Energy_Axis(camname=camname, hdf5_data=data, uid=uid) # eaxis = E200.eaxis(y=y, camname=camname, res=res, E0=20.35, etay=0, etapy=0, ymotor=ymotor) # eaxis = E200.eaxis(y=y, uid=uid, camname=camname, hdf5_data=data) eaxis = EnAxis.energy(ypx=y) yimg = mt.linspacestep(1, img.shape[1]) # imgeaxis = E200.eaxis(camname=camname, y=yimg, res=res, E0=20.35, etay=0, etapy=0, ymotor=ymotor) # imgeaxis = E200.eaxis(y=yimg, uid=uid, camname=camname, hdf5_data=data) imgeaxis = EnAxis.energy(ypx=yimg) yoimg = mt.linspacestep(1, oimg.shape[1]) # oimgeaxis = E200.eaxis(camname=camname, y=yoimg, res=res, E0=20.35, etay=0, etapy=0, ymotor=ymotor) # oimgeaxis = E200.eaxis(y=yoimg, uid=uid, camname=camname, hdf5_data=data) oimgeaxis = EnAxis.energy(ypx=yoimg) # ====================================== # Default Twiss and beam params # ====================================== emitx = 0.001363/gamma # betax = 1 # alphax = 0 # gammax = (1+np.power(alphax, 2))/betax twiss = sltr.BeamParams( beta = 0.5, alpha = 0, emit = emitx ) # ====================================== # Quadrupole values # ====================================== QS1_K1 = setQS.QS1.K1 QS2_K1 = setQS.QS2.K1 logger.log(level=loggerlevel, msg='QS1_K1 is: {}'.format(QS1_K1)) logger.log(level=loggerlevel, msg='QS2_K1 is: {}'.format(QS2_K1)) # ====================================== # Create beamlines # ====================================== # beamline=bt.beamlines.IP_to_cherfar(twiss_x=twiss, twiss_y=twiss, gamma=gamma) if camname == 'ELANEX': beamline = bt.beamlines.IP_to_lanex( beam_x = twiss, beam_y=twiss, QS1_K1 = QS1_K1, QS2_K1 = QS2_K1 ) # from PyQt4.QtCore import pyqtRemoveInputHook # pyqtRemoveInputHook() # pdb.set_trace() else: beamline = bt.beamlines.IP_to_cherfar( beam_x=twiss, beam_y=twiss, QS1_K1 = QS1_K1, QS2_K1 = QS2_K1 ) beamline_array = np.array([]) for i, value in enumerate(eaxis): # beamline.gamma = value/5.109989e-4 beamline.gamma = sltr.GeV2gamma(value) beamline_array = np.append(beamline_array, copy.deepcopy(beamline)) # ====================================== # Fudge error # ====================================== chisq_factor = 1e-28 # used_error = stddev*np.sqrt(chisq_factor) used_error = variance*np.sqrt(chisq_factor) # ====================================== # Fit beamline scan # ====================================== scanresults = bt.fitBeamlineScan(beamline_array, variance, # emitx, error=used_error, verbose=True, plot=False, eaxis=eaxis ) # ====================================== # Plot results # ====================================== mpl.rcParams.update({'font.size': 12}) if plotaxes is not None: bt.plotfit(eaxis, variance, scanresults.fitresults.beta, scanresults.fitresults.X_unweighted, top='Emittance/Twiss Fit to Witness Butterfly', # top='Emittance and Beam Parameter Fit\nto Ionization-Injected Witness Bunch', figlabel='Butterfly Fit', bottom='Energy [GeV]', axes = plotaxes, error=used_error ) plt.show() out = AnalysisResults( gaussfits = gaussresults, scanfit = scanresults , img = img , yimg = yimg , imgeaxis = imgeaxis , oimg = oimg , yoimg = yoimg , oimgeaxis = oimgeaxis , eaxis = eaxis , variance = variance , xstart = xstart , xstop = xstop , ystart = ystart , ystop = ystop , res = res , x_meter = x_meter ) return out
beamline.elements[5].K1 = LGPS_3311[i] beamline_array = np.append(beamline_array,copy.deepcopy(beamline)) # ====================================== # Fudge error # ====================================== chisq_factor = 1e-28 # used_error = stddev*np.sqrt(chisq_factor) used_error = variance*np.sqrt(chisq_factor) # ====================================== # Fit beamline scan # ====================================== out = bt.fitBeamlineScan(beamline_array, variance, emitx, error=used_error, verbose=True) # ====================================== # Plot results # ====================================== mpl.rcParams.update({'font.size':12}) bt.plotfit(stepvalues/20.35, variance, out.beta, out.X_unweighted, top='Emittance/Twiss Fit to Quad Scan', figlabel='Quad Scan Fit', error=used_error) mt.addlabel(xlabel='$\delta$')
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))
scanresults = shot17.scanfit figpath = '/Users/joelfrederico/AAC2014/figs' linewidth = 2 # ===================================== # Plot Fit # ===================================== figlabel = 'Butterfly Fit' ax0 = bt.plotfit(eaxis, variance, scanresults.fitresults.beta, scanresults.fitresults.X_unweighted, top = 'Emittance and Beam Parameter Fit\nto Ionization-Injected Witness Bunch', figlabel = figlabel, # figpath = figpath, bottom = 'Energy [GeV]', # axes = plotaxes, error = used_error, linewidth = linewidth ) mt.graphics.less_labels(ax0, y_fraction=1) mt.graphics.axesfontsize(ax0, fontsize) ax0.get_figure().tight_layout() mt.graphics.savefig(filename=figlabel, path=figpath) # ===================================== # Plot Image # =====================================
image_str = arrays['{}image'.format(head_str)] image = E200.E200_api_getdat(image_str, uid).dat[0] quadval_str = data.read_file['data']['raw']['scalars']['step_value'] quadval = E200.E200_api_getdat(quadval_str, uid).dat[0] # ====================================== # Fit figure # ====================================== fig = plt.figure() ax = fig.add_subplot(1, 1, 1) bt.plotfit( x = energy_axis , y = variance , beta = LLS_beta , X = LLS_X_unweighted , error = LLS_y_error , axes = ax ) filename = '{camname}/{uid:0.0f}/{head_str}min_set_{setnumber}_UID_{uid:0.0f}'.format(camname=camname, setnumber=setnumber, uid=uid, head_str=head_str) mt.graphics.savefig(filename, fig=fig) # plt.close(fig) # ====================================== # Image with ROI # ====================================== # # fig = plt.figure() # # ax = fig.add_subplot(1, 1, 1) # # im=ax.imshow(image, vmin=0, vmax=img_max)
betax = 59.69009e-3 alphax = -0.7705554 gammax = (1+np.power(alphax, 2))/betax twiss = np.array([betax, alphax, gammax]) T = np.array([[betax, -alphax], [-alphax, gammax]]) print('Emittance is {}.'.format(emitx)) print('Normalized emittance is {}.'.format(emitx*40000)) print('Initial beta should be {}.'.format(betax)) print('Initial alpha should be {}.'.format(alphax)) print('Initial gamma should be {}.'.format(gammax)) print('------------------------------') # }}} # Run Cherenkov analysis {{{ out = bt.histcher(x, y, res) [h, xval, davg, filt] = out stddevsq = bt.getstd(res, h, xval) out = bt.fitbowtie(beamline, davg, stddevsq, filt, T, twiss, emitx) # [spotexpected, X, beta] = out spotexpected = out.spotexpected X = out.X beta = out.beta covar = out.covar figcher = plt.figure() top = 'Simulated Cherenkov Emittance Measurement' bt.plotfit(filt, davg, np.sqrt(stddevsq), beta, X, spotexpected, top, elepath) plt.close(figcher)