def do_chi2v3(tof, tof_roi, N_lower, N_upper): tof1 = tof[0::2] tof1_idxs = np.array(range(0, tof1.size)) * 2 tof2 = tof[1::2] tof2_idxs = np.array(range(0, tof2.size)) * 2 + 1 N = N_upper - N_lower + 1 slicings = np.logspace(N_lower, N_upper, N, base=2) opt_res = np.zeros(N) time_res = np.zeros(N) for idx, cts_per_slice in enumerate(slicings): t_start = time.time() pointwise_scales, piecewise_scales = scaling_correction.get_all_scale_coeffs( tof1, m2q_roi=tof_roi, cts_per_slice=cts_per_slice, max_scale=1.075, delta_ly=1e-4) t_end = time.time() time_res[idx] = t_end - t_start print('Total Time = ', time_res[idx]) f = scipy.interpolate.interp1d(tof1_idxs, pointwise_scales, fill_value='extrapolate') # Compute corrected data tof_corr = tof1 / f(tof1_idxs) _, ys = bin_dat(tof_corr, isBinAligned=True, bin_width=0.1, user_roi=tof_roi) opt_res[idx] = chi2(ys) print(opt_res[idx]) print(slicings) print(opt_res / np.max(opt_res)) print(time_res) return (slicings, opt_res)
def do_wid(tof, tof_roi, N_lower, N_upper, t_guess, t_width): tof1 = tof[0::2] tof2 = tof[1::2] N = N_upper - N_lower + 1 slicings = np.logspace(N_lower, N_upper, N, base=2) opt_res = np.zeros(N) time_res = np.zeros(N) curr_res = None for idx, cts_per_slice in enumerate(slicings): prev_res = curr_res t_start = time.time() pointwise_scales, piecewise_scales = scaling_correction.get_all_scale_coeffs( tof1, m2q_roi=tof_roi, cts_per_slice=cts_per_slice, max_scale=1.075, delta_ly=2e-4) t_end = time.time() time_res[idx] = t_end - t_start print('Total Time = ', time_res[idx]) # Compute corrected data tof_corr = tof2 / pointwise_scales # curr_res = fit_to_g_off(tof_corr,t_guess,t_width) # opt_res[idx] = curr_res[1]/curr_res[2] curr_res = get_wid(tof_corr, t_guess, t_width) print(curr_res) opt_res[idx] = 1 / curr_res # print(opt_res[idx]) print(slicings) print(opt_res) print(time_res) return (slicings, opt_res)
def do_chi2(tof, tof_roi, N_lower, N_upper): tof1 = tof[0::2] tof2 = tof[1::2] N = N_upper - N_lower + 1 slicings = np.logspace(N_lower, N_upper, N, base=2) opt_res = np.zeros(N) time_res = np.zeros(N) for idx, cts_per_slice in enumerate(slicings): t_start = time.time() pointwise_scales, piecewise_scales = scaling_correction.get_all_scale_coeffs( tof1, m2q_roi=tof_roi, cts_per_slice=cts_per_slice, max_scale=1.075, delta_ly=2e-4) t_end = time.time() time_res[idx] = t_end - t_start print('Total Time = ', time_res[idx]) # Compute corrected data tof_corr = tof2 / pointwise_scales _, ys = bin_dat(tof_corr, isBinAligned=True, bin_width=0.1, user_roi=tof_roi) opt_res[idx] = chi2(ys) print(opt_res[idx]) print(slicings) print(opt_res / np.max(opt_res)) print(time_res) return (slicings, opt_res / np.max(opt_res))
def ceria_histo(): def shaded_plot(ax, x, y, idx, col_idx=None, min_val=None): if col_idx is None: col_idx = idx if min_val is None: min_val = np.min(y) cols = [ '#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf' ] xlim = ax.get_xlim() idxs = np.nonzero((x >= xlim[0]) & (x <= xlim[1])) ax.fill_between(x[idxs], y[idxs], min_val, color=cols[col_idx], linestyle='None', lw=0) # ax.plot(x,y+idx*sc, color='k') return fn = r"Q:\NIST_Projects\EUV_APT_IMS\BWC\R45_data\R45_00504-v56.epos" epos = apt_fileio.read_epos_numpy(fn) red_epos = epos[100000::10] # Voltage and bowl correct ToF data p_volt = np.array([]) p_bowl = np.array([]) t_i = time.time() _, p_volt, p_bowl = do_voltage_and_bowl(red_epos, p_volt, p_bowl) print("time to voltage and bowl correct: " + str(time.time() - t_i) + " seconds") tof_vcorr = voltage_and_bowl.mod_full_voltage_correction( p_volt, epos['tof'], epos['v_dc']) tof_corr = voltage_and_bowl.mod_geometric_bowl_correction( p_bowl, tof_vcorr, epos['x_det'], epos['y_det']) m2q_roi = [10, 250] m2q_to_tof = 1025 / np.sqrt(172) tof_roi = [m2q_roi[0] * m2q_to_tof, m2q_roi[1] * m2q_to_tof] tof_roi = [200, 1200] cts_per_slice = 2**9 #m2q_roi = [0.9,190] # tof_roi = [0, 1000] t_start = time.time() pointwise_scales, piecewise_scales = scaling_correction.get_all_scale_coeffs( tof_corr, m2q_roi=tof_roi, cts_per_slice=cts_per_slice, max_scale=1.075) t_end = time.time() print('Total Time = ', t_end - t_start) # fake_tof_corr = fake_tof/np.sqrt(pointwise_scales) q_tof_corr = tof_corr / pointwise_scales OVERALL_CALIB_FACTOR = 0.9956265249773827 m2q_corr = OVERALL_CALIB_FACTOR * m2q_to_tof**-2 * q_tof_corr**2 m2q_vbcorr = OVERALL_CALIB_FACTOR * m2q_to_tof**-2 * tof_corr**2 fig = plt.figure(constrained_layout=True, figsize=(FIGURE_SCALE_FACTOR * 3.14961, FIGURE_SCALE_FACTOR * 3.14961), num=8, dpi=100) plt.clf() gs = plt.GridSpec(2, 4, figure=fig) ax0 = fig.add_subplot(gs[0, :]) # identical to ax1 = plt.subplot(gs.new_subplotspec((0, 0), colspan=3)) ax1 = fig.add_subplot(gs[1, 0:2]) #ax2 = fig.add_subplot(gs[1,1]) ax3 = fig.add_subplot(gs[1, 2:4]) dat = m2q_vbcorr user_bin_width = 0.02 user_xlim = [0, 200] ax0.set(xlim=user_xlim) dat = m2q_corr xs, ys = bin_dat(dat, isBinAligned=True, bin_width=user_bin_width, user_roi=user_xlim) shaded_plot(ax0, xs, 10 * (1 + ys), 1, min_val=10) dat = m2q_vbcorr xs, ys = bin_dat(dat, isBinAligned=True, bin_width=user_bin_width, user_roi=user_xlim) shaded_plot(ax0, xs, 1 + ys, 0, min_val=1) ax0.set(xlabel='m/z (Da)', ylabel='counts', xlim=user_xlim) ax0.set_yscale('log') ax0.set(ylim=[10, None]) # user_bin_width = 0.02 user_xlim = [75, 85] ax1.set(xlim=user_xlim) dat = m2q_corr xs, ys = bin_dat(dat, isBinAligned=True, bin_width=user_bin_width, user_roi=user_xlim) shaded_plot(ax1, xs, 10 * (1 + ys), 1, min_val=10) dat = m2q_vbcorr xs, ys = bin_dat(dat, isBinAligned=True, bin_width=user_bin_width, user_roi=user_xlim) shaded_plot(ax1, xs, 1 + ys, 0, min_val=1) ax1.set(xlabel='m/z (Da)', ylabel='counts', xlim=user_xlim) ax1.set_yscale('log') ax1.set(ylim=[10, None]) # # ##user_bin_width = 0.01 #user_xlim = [30,34] #ax2.set(xlim=user_xlim) # # #dat = m2q_corr #xs, ys = bin_dat(dat,isBinAligned=True,bin_width=user_bin_width,user_roi=user_xlim) #shaded_plot(ax2,xs,100*(1+ys),1,min_val=100) # # #dat = epos['m2q'] #xs, ys = bin_dat(dat,isBinAligned=True,bin_width=user_bin_width,user_roi=user_xlim) #shaded_plot(ax2,xs,1+ys,0,min_val=1) # # #ax2.set(xlabel='m/z (Da)', ylabel='counts', xlim=user_xlim) #ax2.set_yscale('log') # user_bin_width = 0.03 user_xlim = [154, 170] ax3.set(xlim=user_xlim) dat = m2q_corr xs, ys = bin_dat(dat, isBinAligned=True, bin_width=user_bin_width, user_roi=user_xlim) shaded_plot(ax3, xs, 10 * (1 + ys), 1, min_val=10) dat = m2q_vbcorr xs, ys = bin_dat(dat, isBinAligned=True, bin_width=user_bin_width, user_roi=user_xlim) shaded_plot(ax3, xs, 1 + ys, 0, min_val=1) ax3.set(xlabel='m/z (Da)', ylabel='counts', xlim=user_xlim) ax3.set_yscale('log') ax3.set(ylim=[10, 1e4]) ax3.set_xticks(np.arange(154, 170 + 1, 4)) fig.tight_layout() fig.savefig( r'Q:\users\bwc\APT\scale_corr_paper\Ceria_NUV_corrected_hist.pdf', format='pdf', dpi=600) return 0
def sio2_R45_histo(): def shaded_plot(ax, x, y, idx, col_idx=None, min_val=None): if col_idx is None: col_idx = idx if min_val is None: min_val = np.min(y) sc = 150 cols = [ '#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf' ] xlim = ax.get_xlim() idxs = np.nonzero((x >= xlim[0]) & (x <= xlim[1])) ax.fill_between(x[idxs], y[idxs], min_val, color=cols[col_idx], linestyle='None', lw=0) # ax.plot(x,y+idx*sc, color='k') return fn = r"Q:\NIST_Projects\EUV_APT_IMS\BWC\R45_data\R45_04472-v03.epos" epos = apt_fileio.read_epos_numpy(fn) epos = epos[25000:] epos = epos[:400000] # Voltage and bowl correct ToF data p_volt = np.array([]) p_bowl = np.array([]) t_i = time.time() tof_corr, p_volt, p_bowl = do_voltage_and_bowl(epos, p_volt, p_bowl) print("time to voltage and bowl correct: " + str(time.time() - t_i) + " seconds") # fake_tof = np.sqrt((296/312)*epos['m2q']/1.393e-4) m2q_roi = [0.8, 80] m2q_to_tof = 613 / np.sqrt(59) tof_roi = [m2q_roi[0] * m2q_to_tof, m2q_roi[1] * m2q_to_tof] cts_per_slice = 2**7 #m2q_roi = [0.9,190] # tof_roi = [0, 1000] t_start = time.time() pointwise_scales, piecewise_scales = scaling_correction.get_all_scale_coeffs( tof_corr, m2q_roi=tof_roi, cts_per_slice=cts_per_slice, max_scale=1.075) t_end = time.time() print('Total Time = ', t_end - t_start) # fake_tof_corr = fake_tof/np.sqrt(pointwise_scales) q_tof_corr = tof_corr / pointwise_scales OVERALL_CALIB_FACTOR = 1.0047693561704287 m2q_corr = OVERALL_CALIB_FACTOR * m2q_to_tof**-2 * q_tof_corr**2 m2q_vbcorr = OVERALL_CALIB_FACTOR * m2q_to_tof**-2 * tof_corr**2 fig = plt.figure(constrained_layout=True, figsize=(FIGURE_SCALE_FACTOR * 3.14961, FIGURE_SCALE_FACTOR * 3.14961), num=7, dpi=100) plt.clf() gs = plt.GridSpec(2, 3, figure=fig) ax0 = fig.add_subplot(gs[0, :]) # identical to ax1 = plt.subplot(gs.new_subplotspec((0, 0), colspan=3)) ax1 = fig.add_subplot(gs[1, 0:2]) #ax2 = fig.add_subplot(gs[1,1]) ax3 = fig.add_subplot(gs[1, 2]) dat = m2q_vbcorr user_bin_width = 0.02 user_xlim = [0, 65] ax0.set(xlim=user_xlim) dat = m2q_corr xs, ys = bin_dat(dat, isBinAligned=True, bin_width=user_bin_width, user_roi=user_xlim) shaded_plot(ax0, xs, 100 * (1 + ys), 1, min_val=100) dat = m2q_vbcorr xs, ys = bin_dat(dat, isBinAligned=True, bin_width=user_bin_width, user_roi=user_xlim) shaded_plot(ax0, xs, 1 + ys, 0, min_val=1) ax0.set(xlabel='m/z (Da)', ylabel='counts', xlim=user_xlim) ax0.set_yscale('log') # user_bin_width = 0.02 user_xlim = [13, 19] ax1.set(xlim=user_xlim) dat = m2q_corr xs, ys = bin_dat(dat, isBinAligned=True, bin_width=user_bin_width, user_roi=user_xlim) shaded_plot(ax1, xs, 100 * (1 + ys), 1, min_val=100) dat = m2q_vbcorr xs, ys = bin_dat(dat, isBinAligned=True, bin_width=user_bin_width, user_roi=user_xlim) shaded_plot(ax1, xs, 1 + ys, 0, min_val=1) ax1.set(xlabel='m/z (Da)', ylabel='counts', xlim=user_xlim) ax1.set_yscale('log') # # ##user_bin_width = 0.01 #user_xlim = [30,34] #ax2.set(xlim=user_xlim) # # #dat = m2q_corr #xs, ys = bin_dat(dat,isBinAligned=True,bin_width=user_bin_width,user_roi=user_xlim) #shaded_plot(ax2,xs,100*(1+ys),1,min_val=100) # # #dat = epos['m2q'] #xs, ys = bin_dat(dat,isBinAligned=True,bin_width=user_bin_width,user_roi=user_xlim) #shaded_plot(ax2,xs,1+ys,0,min_val=1) # # #ax2.set(xlabel='m/z (Da)', ylabel='counts', xlim=user_xlim) #ax2.set_yscale('log') #user_bin_width = 0.01 user_xlim = [58, 64] ax3.set(xlim=user_xlim) dat = m2q_corr xs, ys = bin_dat(dat, isBinAligned=True, bin_width=user_bin_width, user_roi=user_xlim) shaded_plot(ax3, xs, 100 * (1 + ys), 1, min_val=100) dat = m2q_vbcorr xs, ys = bin_dat(dat, isBinAligned=True, bin_width=user_bin_width, user_roi=user_xlim) shaded_plot(ax3, xs, 1 + ys, 0, min_val=1) ax3.set(xlabel='m/z (Da)', ylabel='counts', xlim=user_xlim) ax3.set_yscale('log') ax0.set(ylim=[1, None]) ax1.set(ylim=[1, None]) # ax2.set(ylim=[1,None]) ax3.set(ylim=[1, None]) fig.tight_layout() fig.savefig( r'Q:\users\bwc\APT\scale_corr_paper\SiO2_NUV_corrected_hist.pdf', format='pdf', dpi=600) return 0
def sio2_R45_corr(): fn = r"Q:\NIST_Projects\EUV_APT_IMS\BWC\R45_data\R45_04472-v03.epos" epos = apt_fileio.read_epos_numpy(fn) epos = epos[25000:] epos = epos[:400000] # Voltage and bowl correct ToF data p_volt = np.array([]) p_bowl = np.array([]) t_i = time.time() tof_corr, p_volt, p_bowl = do_voltage_and_bowl(epos, p_volt, p_bowl) print("time to voltage and bowl correct: " + str(time.time() - t_i) + " seconds") # fake_tof = np.sqrt((296/312)*epos['m2q']/1.393e-4) m2q_roi = [0.8, 80] m2q_to_tof = 613 / np.sqrt(59) tof_roi = [m2q_roi[0] * m2q_to_tof, m2q_roi[1] * m2q_to_tof] cts_per_slice = 2**7 #m2q_roi = [0.9,190] # tof_roi = [0, 1000] t_start = time.time() pointwise_scales, piecewise_scales = scaling_correction.get_all_scale_coeffs( tof_corr, m2q_roi=tof_roi, cts_per_slice=cts_per_slice, max_scale=1.075) t_end = time.time() print('Total Time = ', t_end - t_start) # fake_tof_corr = fake_tof/np.sqrt(pointwise_scales) q_tof_corr = tof_corr / pointwise_scales # m2q_corr = epos['m2q']/pointwise_scales # Plot histogram for sio2 fig = plt.figure(figsize=(FIGURE_SCALE_FACTOR * 3.14961, FIGURE_SCALE_FACTOR * 3.14961), num=6, dpi=100) plt.clf() ax1, ax2 = fig.subplots(2, 1, sharex=True) N, x_edges, y_edges = create_histogram(tof_corr, y_roi=[280, 360], cts_per_slice=2**9, delta_y=.25) im = plot_2d_histo(ax1, N, x_edges, y_edges) plt.colorbar(im) ax1.set(ylabel='flight time (ns)') ax1twin = ax1.twinx() ax1twin.plot(pointwise_scales, '-', linewidth=1, color=mcd.XKCD_COLORS['xkcd:white']) ax1twin.set(ylabel='correction factor, c', ylim=[0.98, 1.2], xlim=[0, 400000]) N, x_edges, y_edges = create_histogram(q_tof_corr, y_roi=[280, 360], cts_per_slice=2**9, delta_y=.25) im = plot_2d_histo(ax2, N, x_edges, y_edges) plt.colorbar(im) ax2.set(xlabel='ion sequence', ylabel='corrected flight time (ns)') fig.tight_layout() fig.savefig(r'Q:\users\bwc\APT\scale_corr_paper\SiO2_NUV_corrected.pdf', format='pdf', dpi=600) return 0
m2q_corr2 = f(m2q_corr) epos['m2q'] = m2q_corr2[:] # # # # #apt_fileio.write_epos_numpy(epos,r'C:\Users\capli\Google Drive\NIST\pos_and_epos_files\SiO2_Nov092020\R44_03376-v01_vb.epos') import sys sys.exit(0) import scaling_correction pointwise_scales, piecewise_scales = scaling_correction.get_all_scale_coeffs( m2q_corr, m2q_roi=[0.5, 75], cts_per_slice=2**10, max_scale=1.15) # Compute corrected data m2q_corr_q = m2q_corr / pointwise_scales # Convert back to tof tof_corr_q = np.sqrt(m2q_corr_q / (p_m2q[0] * 1e-4)) + p_m2q[1] # Plot TOF vs event index with voltage overlaid to show if voltage corr went ok ax = plotting_stuff.plot_TOF_vs_time(tof_corr_q, epos, 2) ax.set_title('voltage and bowl corrected') # Plot slices from the detector to show if bowl corr went ok plotting_stuff.plot_bowl_slices(tof_corr_q, epos, 3, clearFigure=True,
y = np.array([1, 1]) * chi2( bin_dat(q_tof[0::2], isBinAligned=True, bin_width=0.1, user_roi=tof_roi)[1]) plt.plot(x, y, label='ref1') x = [2**3, 2**10] y = np.array([1, 1]) * chi2( bin_dat(q_tof[1::2], isBinAligned=True, bin_width=0.1, user_roi=tof_roi)[1]) plt.plot(x, y, label='ref2') plt.figure(32132313) pointwise_scales, piecewise_scales = scaling_correction.get_all_scale_coeffs( varying_tof, m2q_roi=tof_roi, cts_per_slice=2**6, max_scale=1.075, delta_ly=1e-4) ax = plotting_stuff.plot_histo(q_tof, 4, user_xlim=[0, 800], user_bin_width=0.1, user_label='orig') ax = plotting_stuff.plot_histo(varying_tof, 4, clearFigure=False, user_xlim=[0, 800], user_bin_width=0.1, user_label='messed') ax = plotting_stuff.plot_histo(varying_tof / pointwise_scales,