def subtract_background(signal, background, plot=False): x, y = smooth_spectrum.interpolate(background[0], background[1]) y_fitted = smoothing.savitzky_golay_filter(x, y, half_window=32, degree=3)[1] signal_x, signal_y = signal signal_x, signal_y = smooth_spectrum.interpolate(signal[0], signal[1]) x_interp_size = x.size() for i, x_i in enumerate(reversed(x)): if x_i not in signal[0]: assert x[x_interp_size - i - 1] == x_i del signal_x[x_interp_size - i - 1] del signal_y[x_interp_size - i - 1] del y_fitted[x_interp_size - i - 1] background_subtracted = signal_y - y_fitted if plot: from matplotlib import pyplot pyplot.plot(signal[0], signal[1], linewidth=2, label="signal+background") pyplot.plot(background[0], background[1], linewidth=2, label="background") pyplot.plot(signal_x, y_fitted, linewidth=2, label="background_fit") pyplot.plot(signal_x, background_subtracted, linewidth=2, label="signal") pyplot.legend(loc=2) pyplot.ylabel("Intensity", fontsize=15) pyplot.xlabel("Pixel column", fontsize=15) pyplot.show() return signal_x, background_subtracted
def run(args): processed = iotbx.phil.process_command_line( args=args, master_string=master_phil_str) args = processed.remaining_args work_params = processed.work.extract().xes processed.work.show() assert len(args) == 1 output_dirname = work_params.output_dirname roi = cspad_tbx.getOptROI(work_params.roi) bg_roi = cspad_tbx.getOptROI(work_params.bg_roi) gain_map_path = work_params.gain_map estimated_gain = work_params.estimated_gain nproc = work_params.nproc photon_threshold = work_params.photon_threshold method = work_params.method print output_dirname if output_dirname is None: output_dirname = os.path.join(os.path.dirname(args[0]), "finalise") print output_dirname hist_d = easy_pickle.load(args[0]) if len(hist_d.keys())==2: hist_d = hist_d['histogram'] pixel_histograms = view_pixel_histograms.pixel_histograms( hist_d, estimated_gain=estimated_gain) result = xes_from_histograms( pixel_histograms, output_dirname=output_dirname, gain_map_path=gain_map_path, estimated_gain=estimated_gain, method=method, nproc=nproc, photon_threshold=photon_threshold, roi=roi, run=work_params.run) if bg_roi is not None: bg_outdir = os.path.normpath(output_dirname)+"_bg" bg_result = xes_from_histograms( pixel_histograms, output_dirname=bg_outdir, gain_map_path=gain_map_path, estimated_gain=estimated_gain, method=method, nproc=nproc, photon_threshold=photon_threshold, roi=bg_roi) from xfel.command_line.subtract_background import subtract_background signal = result.spectrum background = bg_result.spectrum signal = (signal[0].as_double(), signal[1]) background = (background[0].as_double(), background[1]) signal_x, background_subtracted = subtract_background(signal, background, plot=True) f = open(os.path.join(output_dirname, "background_subtracted.txt"), "wb") print >> f, "\n".join(["%i %f" %(x, y) for x, y in zip(signal_x, background_subtracted)]) f.close() else: from xfel.command_line import smooth_spectrum from scitbx.smoothing import savitzky_golay_filter x, y = result.spectrum[0].as_double(), result.spectrum[1] x, y = smooth_spectrum.interpolate(x, y) x, y_smoothed = savitzky_golay_filter( x, y, 20, 4) smooth_spectrum.estimate_signal_to_noise(x, y, y_smoothed)
def run(args): processed = iotbx.phil.process_command_line(args=args, master_string=master_phil_str) args = processed.remaining_args work_params = processed.work.extract().xes processed.work.show() assert len(args) == 1 output_dirname = work_params.output_dirname roi = cspad_tbx.getOptROI(work_params.roi) bg_roi = cspad_tbx.getOptROI(work_params.bg_roi) gain_map_path = work_params.gain_map estimated_gain = work_params.estimated_gain nproc = work_params.nproc photon_threshold = work_params.photon_threshold method = work_params.method print output_dirname if output_dirname is None: output_dirname = os.path.join(os.path.dirname(args[0]), "finalise") print output_dirname hist_d = easy_pickle.load(args[0]) if len(hist_d.keys()) == 2: hist_d = hist_d['histogram'] pixel_histograms = view_pixel_histograms.pixel_histograms( hist_d, estimated_gain=estimated_gain) result = xes_from_histograms(pixel_histograms, output_dirname=output_dirname, gain_map_path=gain_map_path, estimated_gain=estimated_gain, method=method, nproc=nproc, photon_threshold=photon_threshold, roi=roi, run=work_params.run) if bg_roi is not None: bg_outdir = os.path.normpath(output_dirname) + "_bg" bg_result = xes_from_histograms(pixel_histograms, output_dirname=bg_outdir, gain_map_path=gain_map_path, estimated_gain=estimated_gain, method=method, nproc=nproc, photon_threshold=photon_threshold, roi=bg_roi) from xfel.command_line.subtract_background import subtract_background signal = result.spectrum background = bg_result.spectrum signal = (signal[0].as_double(), signal[1]) background = (background[0].as_double(), background[1]) signal_x, background_subtracted = subtract_background(signal, background, plot=True) f = open(os.path.join(output_dirname, "background_subtracted.txt"), "wb") print >> f, "\n".join([ "%i %f" % (x, y) for x, y in zip(signal_x, background_subtracted) ]) f.close() else: from xfel.command_line import smooth_spectrum from scitbx.smoothing import savitzky_golay_filter x, y = result.spectrum[0].as_double(), result.spectrum[1] x, y = smooth_spectrum.interpolate(x, y) x, y_smoothed = savitzky_golay_filter(x, y, 20, 4) smooth_spectrum.estimate_signal_to_noise(x, y, y_smoothed)