def get_spectrum(data, dark = None, cencol_calibration_data = None, cold_calibration_data = None, pxwidth = 3, bg_sub = True, calib_load_path = None, calib_save_path = None, kalpha_kbeta_calibration = True, eltname = ''): """ Return the XES spectrum corresponding to the given data and element Inputs: eltname: string of the abbreviated element name data: 2d array of CSPAD data cold_calibration_data: data to use for determination of the energy scale. If None, the first argument is used for this. cencol_calibration_data: data to use for location of the spectrometer's line of focus. If None, the first argument is used for this. pxwidth: width of the CSPAD lineout from which the spectrum is constructed peak_width: TODO: deprecate or not? bg_sub: if True, perform a constant subtraction. The subtracted constant is the 5th percentile of the spectrum after smoothing with a gaussian kernel of standard deviation 5 calib_load_path: path to a file with an energy calibration to load calib_save_path: File to which to save an energy calibration if calib_load_path is None kalpha_kbeta_calibration: If calib_load_path is None, use k alpha and k beta peak locations to determine an energy scale. If None and calib_load_path is also None, do not perform an energy calibration at all. Output: array, array -> energy or index, normalized intensity """ if np.shape(data) != (391, 370): print "WARNING: array dimensions differ from those of CSPAD140k" if kalpha_kbeta_calibration or calib_load_path: peak_width = 150 else: peak_width = 15 if dark is not None: # copy without mutating the original array data = np.array(data, copy = True) - dark if cencol_calibration_data is None: cencol_calibration_data = data cencol = center_col(cencol_calibration_data) intensities = lineout(data, cencol, pxwidth = pxwidth) if calib_load_path: x = load_calib(calib_load_path) elif kalpha_kbeta_calibration and eltname and (cold_calibration_data is not None): x = get_energies(cold_calibration_data, cencol, save_path = calib_save_path, eltname = eltname) else: if kalpha_kbeta_calibration and not eltname: print "No element identifier provided; skipping energy calibration." elif kalpha_kbeta_calibration and not cold_calibration_data: print "No file for calibration specified; skipping energy calibration" x = np.array(range(len(intensities))) if bg_sub: smoothed = filt(intensities, 5) floor = np.percentile(smoothed, 5) intensities -= floor norm = get_normalization(x, intensities, peak_width) return x, intensities / norm
def clean_profile(profile, window, spline_smooth=0, median_smooth_perc=0.05): sl = slice(window[0], window[1]) mask = ~profile.M[sl].isnull().values y = profile.M[sl][mask].values x = profile.R[sl][mask].values tck = interpolate.splrep(x,y,s=spline_smooth) xnew = np.linspace(x[0],x[-1], len(x)) f_size = len(x) * median_smooth_perc if f_size < 2: f_size = 2 ynew = filt(interpolate.splev(xnew,tck,der=0), f_size) return ynew, xnew
def get_spectrum(data, dark=None, cencol_calibration_data=None, cold_calibration_data=None, pxwidth=3, bg_sub=True, calib_load_path=None, calib_save_path=None, energy_ref1_energy_ref2_calibration=True, eltname=''): """ Return the XES spectrum corresponding to the given data and element Inputs: eltname: string of the abbreviated element name data: 2d array of CSPAD data cold_calibration_data: data to use for determination of the energy scale. If None, the first argument is used for this. cencol_calibration_data: data to use for location of the spectrometer's line of focus. If None, the first argument is used for this. pxwidth: width of the CSPAD lineout from which the spectrum is constructed peak_width: TODO: deprecate or not? bg_sub: if True, perform a constant subtraction. The subtracted constant is the 5th percentile of the spectrum after smoothing with a gaussian kernel of standard deviation 5 calib_load_path: path to a file with an energy calibration to load calib_save_path: File to which to save an energy calibration if calib_load_path is None energy_ref1_energy_ref2_calibration: If calib_load_path is None, use k alpha and k beta peak locations to determine an energy scale. If None and calib_load_path is also None, do not perform an energy calibration at all. Output: array, array -> energy or index, normalized intensity """ if np.shape(data) != (391, 370): print "WARNING: array dimensions differ from those of CSPAD140k" if energy_ref1_energy_ref2_calibration or calib_load_path: peak_width = 150 else: peak_width = 15 if dark is not None: # copy without mutating the original array data = np.array(data, copy=True) - dark if cencol_calibration_data is None: cencol_calibration_data = data cencol = center_col(cencol_calibration_data) intensities = lineout(data, cencol, pxwidth=pxwidth) # if calib_load_path: # x = load_calib(calib_load_path) if calib_load_path: x = load_calib(calib_load_path) elif energy_ref1_energy_ref2_calibration and eltname and ( cold_calibration_data is not None): x = energies_from_data(cold_calibration_data, cencol, save_path=calib_save_path, eltname=eltname) else: if energy_ref1_energy_ref2_calibration and not eltname: print "No element identifier provided; skipping energy calibration." elif energy_ref1_energy_ref2_calibration and not cold_calibration_data: print "No file for calibration specified; skipping energy calibration" x = np.array(range(len(intensities))) if bg_sub: smoothed = filt(intensities, 5) floor = np.percentile(smoothed, 5) intensities -= floor norm = get_normalization(x, intensities, peak_width) return x, intensities / norm
def get_spectrum_one_label(eltname, detid, label, cold_calibration_label, \ cencol_calibration_label, pxwidth = 10, default_bg = None,\ bg_sub = True, calib_load_path = None,\ calib_save_path = None, kalpha_kbeta_calibration = True, run_label_filename = 'labels.txt'): """ Return the XES spectrum corresponding to the given label, detector ID, and element Inputs: eltname: string of the abbreviated element name detid: CSPAD detector ID (1 or 2) label: string reference to a group of runs cold_calibration_label: label to use for determination of the energy scale cencol_calibration_label: label to use for location of the spectrometer's line of focus pxwidth: width of the CSPAD lineout from which the spectrum is constructed default_bg: label to use for background subtraction if embedded blank frames are missing peak_width: TODO: deprecate or not? bg_sub: if True, perform a constant subtraction. The subtracted constant is the 5th percentile of the spectrum after smoothing with a gaussian kernel of standard deviation 5 calib_load_path: path to a file with an energy calibration to load calib_save_path: File to which to save an energy calibration if calib_load_path is None kalpha_kbeta_calibration: If calib_load_path is None, use k alpha and k beta peak locations to determine an energy scale. If None and calib_load_path is also None, do not perform an energy calibration at all. Output: array, array -> energy or index, normalized intensity """ if kalpha_kbeta_calibration or calib_load_path: peak_width = 150 else: peak_width = 15 cencol = center_col(cencol_calibration_label, detid, run_label_filename=run_label_filename) intensities = lineout(label, detid, cencol, pxwidth=pxwidth, default_bg=default_bg, run_label_filename=run_label_filename) if calib_load_path: x = load_calib(calib_load_path) elif kalpha_kbeta_calibration: x = get_energies(cold_calibration_label, detid, eltname, cencol, save_path=calib_save_path, run_label_filename=run_label_filename) else: x = np.array(range(len(intensities))) # if lin_bg_sub: # peak_ranges = [[e - peak_width, e + peak_width] for e in emission[eltname]] # intensities = linear_bg_subtraction(x, intensities, peak_ranges) if bg_sub: smoothed = filt(intensities, 5) floor = np.percentile(smoothed, 5) intensities -= floor norm = get_normalization(x, intensities, peak_width) #plt.plot(x, smoothed/norm) return x, intensities / norm
def get_spectrum_one_label( eltname, detid, label, cold_calibration_label, cencol_calibration_label, pxwidth=10, default_bg=None, bg_sub=True, calib_load_path=None, calib_save_path=None, kalpha_kbeta_calibration=True, run_label_filename="labels.txt", ): """ Return the XES spectrum corresponding to the given label, detector ID, and element Inputs: eltname: string of the abbreviated element name detid: CSPAD detector ID (1 or 2) label: string reference to a group of runs cold_calibration_label: label to use for determination of the energy scale cencol_calibration_label: label to use for location of the spectrometer's line of focus pxwidth: width of the CSPAD lineout from which the spectrum is constructed default_bg: label to use for background subtraction if embedded blank frames are missing peak_width: TODO: deprecate or not? bg_sub: if True, perform a constant subtraction. The subtracted constant is the 5th percentile of the spectrum after smoothing with a gaussian kernel of standard deviation 5 calib_load_path: path to a file with an energy calibration to load calib_save_path: File to which to save an energy calibration if calib_load_path is None kalpha_kbeta_calibration: If calib_load_path is None, use k alpha and k beta peak locations to determine an energy scale. If None and calib_load_path is also None, do not perform an energy calibration at all. Output: array, array -> energy or index, normalized intensity """ if kalpha_kbeta_calibration or calib_load_path: peak_width = 150 else: peak_width = 15 cencol = center_col(cencol_calibration_label, detid, run_label_filename=run_label_filename) intensities = lineout( label, detid, cencol, pxwidth=pxwidth, default_bg=default_bg, run_label_filename=run_label_filename ) if calib_load_path: x = load_calib(calib_load_path) elif kalpha_kbeta_calibration: x = get_energies( cold_calibration_label, detid, eltname, cencol, save_path=calib_save_path, run_label_filename=run_label_filename, ) else: x = np.array(range(len(intensities))) # if lin_bg_sub: # peak_ranges = [[e - peak_width, e + peak_width] for e in emission[eltname]] # intensities = linear_bg_subtraction(x, intensities, peak_ranges) if bg_sub: smoothed = filt(intensities, 5) floor = np.percentile(smoothed, 5) intensities -= floor norm = get_normalization(x, intensities, peak_width) # plt.plot(x, smoothed/norm) return x, intensities / norm