daq_labels, sacla_converter, t0=0, selection=sel) # get laser on/off tags is_laser_on_tags = df[df.is_laser == 1].index.tolist() is_laser_off_tags = df[df.is_laser == 0].index.tolist() # get spectra from Von Hamos, using laser on / off tags #roi = [[0, 1024], [325, 335]] # X, Y ap = ImagesProcessor(facility="SACLA") ap.add_analysis('get_projection', args={"axis": 1}) ap.add_analysis('get_mean_std') ap.set_dataset('/run_%s/detector_2d_1' % run) ap.add_preprocess("set_thr", args={"thr_low": 65}) # get the total spectra results_on = ap.analyze_images(fname, tags=is_laser_on_tags) spectrum_on = results_on["get_projection"]["spectra"].sum(axis=0) results_off = ap.analyze_images(fname, tags=is_laser_off_tags) spectrum_off = results_off["get_projection"]["spectra"].sum(axis=0) spectrum_off = spectrum_off / spectrum_off.sum() spectrum_on = spectrum_on / spectrum_on.sum() # this is the average image from the Von Hamos sum_image_on = results_on["get_mean_std"]["images_mean"] # Plot! plt.subplot(1, 2, 1)
thr = 50 # pixel's threshold value roi = [[450, 520], [240, 280]] # SL [[xmin xmax], [ymin ymax]] # roi = [[420, 470], [190, 230]] # Bragg Peak [[xmin xmax], [ymin ymax]] # bkgRoi = np.array(roi) #+ np.array([[-40, 40], [-40, 40]]) bkgRoi = np.array(roi) # create ImagesProcessor object ip = ImagesProcessor(facility="SACLA") # if you want a flat dict as a result ip.flatten_results = True # PREPROCESS FUNCTIONS (bkg sub, masks, ...) # (comment out for loading a background image) dark = np.load('/home/usov_i/SACLA Dec2015/python_scripts2016/analysis/dark_439011and02comb.npy') ip.add_preprocess("subtract_correction", args={"sub_image": dark}) ip.add_preprocess("set_thr", args={"thr_low": thr}) # ANALYSIS FUNCTIONS ip.add_analysis("get_mean_std") # , args={'thr_low': thr}) bins = np.arange(-50, 600, 2) ip.add_analysis("get_histo_counts", args={'bins': bins, 'roi': roi}) ip.add_analysis("roi_bkgRoi", args={'roi': roi, 'bkg_roi': bkgRoi}) for run in runs: rname = str(run) fname = DIR + rname + ".h5" print('\nAnalyzing run ' + rname + '\n') """ Analyze images and integrate roi and bkgRoi. Can take a lot of time The results are saved in a pickle file in the folder analyzed_runs/imgAna.
def bin_tt_COM(df, bin_edges, rname, fname, calibration=0.01, roi=[[235, 270], [500, 540]]): """ Bin data according to the timing tool and perform a center of mass analysis of the roi This script is somewhat redundant with the image analysis, as it loops again through all the images. """ # create corrected delay df["dl_corr"] = df.delay + calibration * df.tt bin_size = bin_edges[1] - bin_edges[0] df_xon = df[df.x_status == 1] df_lon = df_xon[df.laser_status == 1] df_loff = df_xon[df.laser_status == 0] bin_center = bin_edges[:-1] + 0.5 * bin_size df_out = pd.DataFrame(bin_center, columns=["time"]) if len(df_lon) != 0: binned_int_lon = stats.binned_statistic(df_lon.dl_corr, df_lon.intensity, bins=bin_edges, statistic="mean") binned_bkg_lon = stats.binned_statistic(df_lon.dl_corr, df_lon.bkg, bins=bin_edges, statistic="mean") binned_I0_lon = stats.binned_statistic(df_lon.dl_corr, df_lon.I0, bins=bin_edges, statistic="mean") df_out["intensity_lon"] = binned_int_lon.statistic df_out["bkg_lon"] = binned_bkg_lon.statistic df_out["I0_lon"] = binned_I0_lon.statistic else: print ("No laser ON shots") if len(df_loff) != 0: binned_int_loff = stats.binned_statistic(df_loff.dl_corr, df_loff.intensity, bins=bin_edges, statistic="mean") binned_bkg_loff = stats.binned_statistic(df_loff.dl_corr, df_loff.bkg, bins=bin_edges, statistic="mean") binned_I0_loff = stats.binned_statistic(df_loff.dl_corr, df_loff.I0, bins=bin_edges, statistic="mean") df_out["I0_loff"] = binned_I0_loff.statistic df_out["bkg_loff"] = binned_bkg_loff.statistic df_out["intensity_loff"] = binned_int_loff.statistic else: print ("No laser OFF shots") """ COM analysis COM analysis loops through the bins and load the images corresponding for each bin. The COM of the averaged images in the bin is taken and written in the df_out dataframe """ binnumber = binned_int_lon.binnumber peakCOM = np.zeros([len(df_out.time), 2]) dataset_name = "/run_" + rname + "/detector_2d_1" ip = ImagesProcessor(facility="SACLA") ip.flatten_results = True ip.set_dataset(dataset_name) ip.add_preprocess("set_roi", args={"roi": roi}) ip.add_analysis("get_mean_std") for ii in range(len(df_out.time)): n = ii + 1 ismember = binnumber == n tagList = df.index[ismember] results = ip.analyze_images(fname, n=-1, tags=tagList) if "images_mean" in results: peakCOM[ii, :] = ndimage.measurements.center_of_mass(results["images_mean"]) else: peakCOM[ii, :] = np.NaN del results print ("bin number %s" % n) df_out["COMx"] = peakCOM[:, 0] df_out["COMy"] = peakCOM[:, 1] return df_out
thr = 50 # pixel's threshold value roi = [[450, 520], [240, 280]] # SL [[xmin xmax], [ymin ymax]] # roi = [[420, 470], [190, 230]] # Bragg Peak [[xmin xmax], [ymin ymax]] # bkgRoi = np.array(roi) #+ np.array([[-40, 40], [-40, 40]]) bkgRoi = np.array(roi) # create ImagesProcessor object ip = ImagesProcessor(facility="SACLA") # if you want a flat dict as a result ip.flatten_results = True # PREPROCESS FUNCTIONS (bkg sub, masks, ...) # (comment out for loading a background image) dark = np.load('/home/usov_i/SACLA Dec2015/python_scripts2016/analysis/dark_439011and02comb.npy') ip.add_preprocess("subtract_correction", args={"sub_image": dark}) ip.add_preprocess("set_thr", args={"thr_low": thr}) # ANALYSIS FUNCTIONS ip.add_analysis("get_mean_std") # , args={'thr_low': thr}) bins = np.arange(-50, 600, 2) ip.add_analysis("get_histo_counts", args={'bins': bins, 'roi': roi}) ip.add_analysis("roi_bkgRoi", args={'roi': roi, 'bkg_roi': bkgRoi}) for run in runs: rname = str(run) fname = DIR + rname + ".h5" print(('\nAnalyzing run ' + rname + '\n')) """ Analyze images and integrate roi and bkgRoi. Can take a lot of time The results are saved in a pickle file in the folder analyzed_runs/imgAna.
def bin_tt_COM(df, bin_edges, rname, fname, calibration=0.01, roi=[[235, 270], [500, 540]]): """ Bin data according to the timing tool and perform a center of mass analysis of the roi This script is somewhat redundant with the image analysis, as it loops again through all the images. """ # create corrected delay df['dl_corr'] = df.delay + calibration * df.tt bin_size = bin_edges[1] - bin_edges[0] df_xon = df[df.x_status == 1] df_lon = df_xon[df.laser_status == 1] df_loff = df_xon[df.laser_status == 0] bin_center = bin_edges[:-1] + 0.5 * bin_size df_out = pd.DataFrame(bin_center, columns=['time']) if len(df_lon) != 0: binned_int_lon = stats.binned_statistic(df_lon.dl_corr, df_lon.intensity, bins=bin_edges, statistic='mean') binned_bkg_lon = stats.binned_statistic(df_lon.dl_corr, df_lon.bkg, bins=bin_edges, statistic='mean') binned_I0_lon = stats.binned_statistic(df_lon.dl_corr, df_lon.I0, bins=bin_edges, statistic='mean') df_out['intensity_lon'] = binned_int_lon.statistic df_out['bkg_lon'] = binned_bkg_lon.statistic df_out['I0_lon'] = binned_I0_lon.statistic else: print('No laser ON shots') if len(df_loff) != 0: binned_int_loff = stats.binned_statistic(df_loff.dl_corr, df_loff.intensity, bins=bin_edges, statistic='mean') binned_bkg_loff = stats.binned_statistic(df_loff.dl_corr, df_loff.bkg, bins=bin_edges, statistic='mean') binned_I0_loff = stats.binned_statistic(df_loff.dl_corr, df_loff.I0, bins=bin_edges, statistic='mean') df_out['I0_loff'] = binned_I0_loff.statistic df_out['bkg_loff'] = binned_bkg_loff.statistic df_out['intensity_loff'] = binned_int_loff.statistic else: print('No laser OFF shots') """ COM analysis COM analysis loops through the bins and load the images corresponding for each bin. The COM of the averaged images in the bin is taken and written in the df_out dataframe """ binnumber = binned_int_lon.binnumber peakCOM = np.zeros([len(df_out.time), 2]) dataset_name = "/run_" + rname + "/detector_2d_1" ip = ImagesProcessor(facility="SACLA") ip.flatten_results = True ip.set_dataset(dataset_name) ip.add_preprocess("set_roi", args={'roi': roi}) ip.add_analysis("get_mean_std") for ii in range(len(df_out.time)): n = ii + 1 ismember = (binnumber == n) tagList = df.index[ismember] results = ip.analyze_images(fname, n=-1, tags=tagList) if 'images_mean' in results: peakCOM[ii, :] = ndimage.measurements.center_of_mass( results['images_mean']) else: peakCOM[ii, :] = np.NaN del results print(('bin number %s' % n)) df_out['COMx'] = peakCOM[:, 0] df_out['COMy'] = peakCOM[:, 1] return df_out