def ripple_minima(chi_file, background_file, background_level, composition, qmin, qmaxinst): pwd = os.getcwd() if os.path.exists(os.path.join(pwd, 'ripple_minima_test')) is False: os.mkdir(os.path.join(pwd, 'ripple_minima_test')) ripple_list = [] gr_list = [] qmax_min = 18.5 qmax_max = qmaxinst qmax_step = .01 print 'Start qmax refinement' for qmax in np.arange(qmax_min, qmax_max, qmax_step): print qmax gr_file, bg, q=Calculate.pdfgetx3( chi_file, background_file, background_level, composition, qmax, qmin, qmax, 'QA', os.path.join(pwd, 'ripple_minima_test', '@b_ripple_test_'+str( qmax).ljust(5,'0')+'.@o')) gr_file = os.path.join(pwd, 'ripple_minima_test', os.path.split( os.path.splitext(chi_file)[0])[1]+'_ripple_test_'+str(qmax).ljust(5,'0')+'.gr') x, y = IO.load_gr_file(gr_file) w = y - np.convolve(y, np.ones(3)/3, 'same') ripple_sum = np.sum(abs(w)) ripple_list.append(ripple_sum) gr_list.append(gr_file) t = np.arange(qmax_min, qmax_max, qmax_step) ripple_array=np.array(ripple_list) minima_index = signal.argrelextrema(ripple_array, np.greater_equal, order=2*len(ripple_array)/100)[0] minima = ripple_array[minima_index] minima_q = t[minima_index] maxima_index = signal.argrelextrema(ripple_array, np.less_equal, order=2*len(ripple_array)/100)[0] maxima = ripple_array[maxima_index] maxima_q = t[maxima_index] plt.plot(t, ripple_array, 'g') plt.plot(minima_q, minima, 'o', markerfacecolor='none', mec='r') plt.plot(maxima_q, maxima, 'o', markerfacecolor='none', mec='b') plt.title('Ripples in PDF') plt.xlabel('Qmax (1/A)') plt.ylabel('Ripple Cumulative Sum') plt.show() plt.cla() # data_list = [] # key_list = [] # for minima_q_point in minima_q: # data = IO.load_gr_file([x for x in gr_list if str(qmax) in x] [0]) # data_list_element = data # key_list_element = minima_q_point # data_list.append(data_list_element) # key_list.append(key_list_element) # plot_stack(data_list, key_list) ziped_minima = zip(minima_q, minima) while True: for q, value in ziped_minima: print 'q= ', q,'rippleness= ', value qmax= float(raw_input('Please pick a qmax. qmax= ')) gr_file = [x for x in gr_list if str(qmax) in x] [0] print gr_file x, y = IO.load_gr_file(gr_file) plt.plot(x, y) plt.show() if raw_input('Is this the file you wanted? y/[n] ') == 'y': break return qmax
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