def background_subtract(sample, background_file=None, background_level=None, bg_refine_qmin_index=None, bg_refine_qmax_index=None): """ Wrap the background subtraction optimization with file IO Parameters ---------- :param bg_refine_qmin_index: sample: ndarray Sample I(Q) array background_file: str, optional The file name of the background file, if not specified give an IO gui to select the file, f '' no file is loaded background_level: float, optional The background level if none given, calculate via minimization Returns ------- float: The background level array: The background array array: The background q array str: The name of the background file """ #File IO background_data, background_file = IO.load_chi_file( background_file) #Unpack file IO if background_data is not None: background = background_data[:, 1] background_q = background_data[:, 0] percentile_peak_pick = np.where( background >= np.percentile(background, 98))[0] print len(percentile_peak_pick) print percentile_peak_pick[0] print percentile_peak_pick[-1] if len(percentile_peak_pick) >= 2: bg_refine_qmin_index = percentile_peak_pick[0] bg_refine_qmax_index = percentile_peak_pick[-1] else: if bg_refine_qmax_index is None: plt.plot(background) plt.plot(sample) plt.show() bg_refine_qmax_index = int(raw_input('High end of the ' 'background to fit: ')) if bg_refine_qmin_index is None: plt.plot(background) plt.plot(sample) plt.show() bg_refine_qmin_index = int(raw_input('Low end of the ' 'background to fit: ')) # Fit the background if background_level is None: background_level = Calculate.optimize_background_level(background, sample, bg_refine_qmin_index, bg_refine_qmax_index) print 'background level= ', background_level else: background = None background_q = None return background_level, background_q, background, background_file, \ bg_refine_qmin_index, bg_refine_qmax_index
def write_pdf(chi_file=None, background_file=None, background_level=None, pdf_format='QA', output='@r.@o', qmax='statistics', composition=None, qmaxinst=0.0, qmin=0.0, relative_max_uncertainty=.90, plot_verbose=False, bg_refine_qmin_index=None, bg_refine_qmax_index=None): """ Generates the G(r) and F(q) files, potentially using optimized values for the background subtraction and qmax determination Parameters ---------- chi_file: str Location of the chi file to use as the foreground to generate the PDF background_file: str Location of the chi file to use as the background background_level: float The background level, if not set automatically generate the background scale pdf_format: str The format of the chi file out_put: str The naming convention for the output PDF qmax: float or {'statistics', 'ripple'} If float, the qmax value to be used in the FFT. If statistics, generate qmax by examining the statistical uncertainty of I(Q). If ripple generate the PDFs at various qmax and compare the rippleness between the various qmax values composition: str The compostion of the material to be studied qmaxinst: float The maximum reliable qmax generated by the detector, if 0.0 the max of the chi file qmin: float The minimum q to be used in the FFT relative_max_uncertainty: float [0,1] If qmax is statistics the percentile of the uncertainty to except plot_verbose: bool If true, generate plots bg_refine_qmin_index: int The lower bound on the range of values to fit between the background and foreground for determining the background level. bg_refine_qmax_index: int The upper bound on the range of values to fit between the background and foreground for determining the background level. Returns ------- str: The file name of the output float: The background level float: The qmax int: The background refine lower bound int: The background refine upper bound """ #Get composition if composition is None: composition = pdf_parameters.generate_composition() # load up sample I[Q] chi, chi_file = IO.load_chi_file(chi_file) if os.path.split(chi_file)[0] != '': os.chdir(os.path.split(chi_file)[0]) sample_q, sample, uncertainty = chi[:, 0], chi[:, 1], chi[:, 2] if qmaxinst > np.amax(sample_q): qmaxinst = np.amax(sample_q) elif qmaxinst == 0: qmaxinst = np.amax(sample_q) # perform background subtraction if background_file is not '' and background_level is None: # implies that you want to load a background file background_level, background_q, background, background_file, \ bg_refine_qmin_index, bg_refine_qmax_index = \ pdf_parameters.background_subtract(sample, background_file, background_level, bg_refine_qmin_index=bg_refine_qmin_index, bg_refine_qmax_index=bg_refine_qmax_index) if plot_verbose is True: fig, ax = plt.subplots() ax.plot(sample_q, sample, 'g', label='Sample') ax.legend(loc='upper right') plt.title('Sample I(Q)') plt.xlabel('Q (1/A)') plt.ylabel('Raw Counts') plt.savefig('/home/christopher/sample.png', bbox_inches='tight', transparent=True) plt.show() fig, ax = plt.subplots() ax.plot(sample_q, background, 'b', label='Background') ax.legend(loc='upper right') plt.title('Background (IQ)') plt.xlabel('Q (1/A)') plt.ylabel('Raw Counts') plt.savefig('/home/christopher/background.png', bbox_inches='tight', transparent=True) plt.show() fig, ax = plt.subplots() ax.plot(sample_q, sample, 'g', label='Sample') ax.plot(sample_q, background * background_level, 'b', label='Scaled Background') ax.plot(sample_q, sample - background * background_level, 'k', label='Sample-Background') ax.legend(loc='upper right') plt.title('Background Subtraction') plt.xlabel('Q (1/A)') plt.ylabel('Raw Counts') plt.savefig('/home/christopher/integrated_minus_background.png', bbox_inches='tight', transparent=True) plt.show() # Determine Qmax if qmax is 'statistics': qmax = pdf_parameters.qmax_statistics(sample_q, sample, uncertainty, relative_max_uncertainty) elif qmax is 'ripple': qmax = pdf_parameters.ripple_minima(chi_file, background_file, background_level, composition, qmin, qmaxinst) if plot_verbose is True: plt.plot(sample_q, uncertainty/sample * 100) plt.show() # PDFGETX3 call/ Generate PDF gr_file, background_level, qmax = Calculate.pdfgetx3(chi_file, background_file, background_level, composition, qmax, qmin, qmaxinst, pdf_format, output) return gr_file, background_level, qmax, bg_refine_qmin_index, bg_refine_qmax_index