Exemplo n.º 1
0
def plot_histo(dat,fig_idx,user_label='histo',clearFigure=True,user_xlim=[0,100],user_bin_width=0.01, scale_factor=1, user_color=None):
       
    fig = plt.figure(num=fig_idx)
    if clearFigure:
        fig.clear()
    ax = fig.gca()

    xs, ys = bin_dat(dat,isBinAligned=True,bin_width=user_bin_width,user_roi=user_xlim)
#    ys_sm = ppd.do_smooth_with_gaussian(ys, std=5)

    
    if user_color is None:
        ax.plot(xs,scale_factor*ys,label=user_label,linewidth=1)
    else:
        ax.plot(xs,scale_factor*ys,label=user_label,linewidth=1, color=user_color)
    
    ax.set(xlabel='m/z (Da)', ylabel='counts', xlim=user_xlim)

    ax.grid()

    fig.tight_layout()
    fig.canvas.manager.window.raise_()

    ax.set_yscale('log')
    
    ax.legend()
    
    plt.pause(0.1)
    
    return ax
def do_counting(epos, pk_params, glob_bg_param):
    
    xs, _ = bin_dat(epos['m2q'],user_roi=[0,150],isBinAligned=True)
    
    glob_bg = physics_bg(xs,glob_bg_param)    

    
    cts = np.full(pk_params.size,-1,dtype=[('total','f4'),
                                            ('local_bg','f4'),
                                            ('global_bg','f4')])
    
    for idx,pk_param in enumerate(pk_params):
        
        pk_rng = [pk_param['pre_rng'],pk_param['post_rng']]
        
#        print('pk rng',pk_rng)
        local_bg_cts = pk_param['loc_bg']*(pk_rng[1]-pk_rng[0])/0.001
        global_bg_cts = np.sum(glob_bg[(xs>=pk_rng[0]) & (xs<=pk_rng[1])])
        tot_cts = np.sum((epos['m2q']>=pk_rng[0]) & (epos['m2q']<=pk_rng[1]))
#        print('tot counts',tot_cts)
        
        cts['total'][idx] = tot_cts
        cts['local_bg'][idx] = local_bg_cts
        cts['global_bg'][idx] = global_bg_cts
        
    return cts 
Exemplo n.º 3
0
def log_xcorr_est_c(ref_m2q, tof):
    # This assumes that the t0 is not crazy off
    l_ref = np.log10(ref_m2q[ref_m2q > 0.5])

    tmp = np.square(tof)
    # PROBLEM HERE... new 4000 goes out to 40 us and 3000 went to 5 us!
    c_est = np.max(ref_m2q) / np.max(tmp)
    tmp = tmp * c_est
    l_q = np.log10(tmp[tmp > 0.5])

    xs_l_ref, ys_l_ref = bin_dat(l_ref,
                                 0.01,
                                 user_roi=[-0.5, 4],
                                 isBinAligned=True,
                                 isDensity=True)
    ys_l_ref = ys_l_ref - medfilt(ys_l_ref, kernel_size=15)

    xs_l_q, ys_l_q = bin_dat(l_q,
                             0.01,
                             user_roi=[-0.5, 4],
                             isBinAligned=True,
                             isDensity=True)
    ys_l_q = ys_l_q - medfilt(ys_l_q, kernel_size=15)

    res = np.convolve(ys_l_ref, ys_l_q[::-1], mode='same')

    # We know the remaining shift should be small
    N4 = res.size // 4
    res[0:N4] = 0
    res[3 * N4:-1] = 0

    x_c = np.mean(xs_l_q)
    x_max = xs_l_q[np.argmax(res)]

    c_refine = 10**(x_c - x_max)

    c_guess = c_est / c_refine

    #    fig = plt.figure(num=113)
    #    fig.clear()
    #    ax = fig.gca()
    #
    #    ax.plot(xs_l_ref,res,label='conv')
    #    plt.pause(1)

    return c_guess
Exemplo n.º 4
0
def plot_spectrum_gory_detail(epos, user_roi, pk_params, bg_rois,
                              glob_bg_param, is_peak, fig_idx):
    from histogram_functions import bin_dat
    import peak_param_determination as ppd

    xs, ys = bin_dat(epos['m2q'], user_roi=user_roi, isBinAligned=True)
    #ys_sm = ppd.do_smooth_with_gaussian(ys,30)
    ys_sm = ppd.moving_average(ys, 30)

    glob_bg = ppd.physics_bg(xs, glob_bg_param)

    fig = plt.figure(num=fig_idx)
    fig.clear()
    ax = fig.gca()

    ax.plot(xs, ys_sm, label='hist')
    ax.plot(xs, glob_bg, label='global bg')

    ax.set(xlabel='m/z (Da)', ylabel='counts')
    ax.grid()
    fig.tight_layout()
    fig.canvas.manager.window.raise_()
    ax.set_yscale('log')
    ax.legend()

    for idx, pk_param in enumerate(pk_params):

        if is_peak[idx]:
            ax.plot(
                np.array([1, 1]) * pk_param['pre_rng'],
                np.array([0.5, (pk_param['amp'] + pk_param['off'])]), 'k--')
            ax.plot(
                np.array([1, 1]) * pk_param['post_rng'],
                np.array([0.5, (pk_param['amp'] + pk_param['off'])]), 'k--')
            ax.plot(
                np.array([1, 1]) * pk_param['pre_bg_rng'],
                np.array([0.5, (pk_param['amp'] + pk_param['off'])]), 'm--')
            ax.plot(
                np.array([1, 1]) * pk_param['post_bg_rng'],
                np.array([0.5, (pk_param['amp'] + pk_param['off'])]), 'm--')

            ax.plot(
                np.array([pk_param['pre_bg_rng'], pk_param['post_bg_rng']]),
                np.ones(2) * pk_param['loc_bg'], 'g--')
        else:
            ax.plot(
                np.array([1, 1]) * pk_param['x0_mean_shift'],
                np.array([0.5, (pk_param['amp'] + pk_param['off'])]), 'r--')

    for roi in bg_rois:
        xbox = np.array([roi[0], roi[0], roi[1], roi[1]])
        ybox = np.array([0.1, np.max(ys_sm) / 10, np.max(ys_sm) / 10, 0.1])

        ax.fill(xbox, ybox, 'b', alpha=0.2)

    plt.pause(0.1)

    return None
Exemplo n.º 5
0
def bin_and_smooth_spectrum(epos, user_roi, bin_wid_mDa=30, smooth_wid_mDa=-1):
    from histogram_functions import bin_dat
    import peak_param_determination as ppd

    xs, ys = bin_dat(epos['m2q'],
                     user_roi=user_roi,
                     isBinAligned=True,
                     bin_width=bin_wid_mDa / 1000.0)
    if smooth_wid_mDa > 0:
        ys_sm = ppd.moving_average(ys, smooth_wid) * smooth_wid
    else:
        ys_sm = ys
    return (xs, ys_sm)
Exemplo n.º 6
0
    def get_wid(tof, t_guess, t_width):
        t_ms = ppd.mean_shift_peak_location(tof, user_std=1, user_x0=t_guess)
        xs, ys = bin_dat(tof,
                         bin_width=0.1,
                         user_roi=[t_ms - t_width / 2, t_ms + t_width / 2],
                         isBinAligned=False,
                         isDensity=False)
        pk_idx = np.argmin(np.abs(t_ms - xs))
        pk_val = ys[pk_idx]

        fig = plt.figure(num=89,
                         figsize=(FIGURE_SCALE_FACTOR * 3.14961,
                                  FIGURE_SCALE_FACTOR * 3.14961 * 0.7))
        fig.clear()
        ax = fig.gca()

        ax.plot(xs, ys, '-')

        plt.pause(2)

        rhs = 1e6
        for idx in np.arange(pk_idx, ys.size):
            if ys[idx] < 0.5 * pk_val:
                # compute
                x1 = xs[idx - 1]
                x2 = xs[idx]
                y1 = ys[idx - 1]
                y2 = ys[idx]
                m = (y2 - y1) / (x2 - x1)
                b = y1 - m * x1
                rhs = (0.5 * pk_val - b) / m
                break

        lhs = -1e6
        for idx in np.arange(pk_idx, 0, -1):
            if ys[idx] < 0.5 * pk_val:
                # compute
                x1 = xs[idx + 1]
                x2 = xs[idx]
                y1 = ys[idx + 1]
                y2 = ys[idx]
                m = (y2 - y1) / (x2 - x1)
                b = y1 - m * x1
                lhs = (0.5 * pk_val - b) / m
                break

        wid = rhs - lhs

        return wid
Exemplo n.º 7
0
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)
Exemplo n.º 8
0
    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))
Exemplo n.º 9
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
Exemplo n.º 10
0
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 = epos['m2q']
user_bin_width = 0.03
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 = epos['m2q']
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.01
user_xlim = [13, 19]
Exemplo n.º 11
0
    2 * ed['N'].isotopes[14][0], (1 / 2) * ed['Ga'].isotopes[69][0],
    (1 / 2) * ed['Ga'].isotopes[71][0], ed['Ga'].isotopes[69][0],
    ed['Ga'].isotopes[71][0]
])

# Perform 'linearization' m2q calibration
m2q_corr2 = m2q_calib.calibrate_m2q_by_peak_location(m2q_corr, ref_pk_m2qs)

# Plot the reference spectrum, (c, t0) corr spectrum and linearized spectrum
#     to confirm that mass calibration went ok

# Save the data as a new epos file

full_roi = np.array([0, 100])
xs_full_1mDa, ys_full_1mDa = bin_dat(m2q_corr,
                                     user_roi=full_roi,
                                     isBinAligned=True)
ys_full_5mDa_sm = ppd.do_smooth_with_gaussian(ys_full_1mDa, std=5)

xs_full_1mDa_ref, ys_full_1mDa_ref = bin_dat(ref_epos['m2q'],
                                             user_roi=full_roi,
                                             isBinAligned=True)
ys_full_5mDa_sm_ref = ppd.do_smooth_with_gaussian(ys_full_1mDa_ref, std=5)

fig = plt.figure(num=1)
fig.clear()
ax = plt.axes()
ax.plot(xs_full_1mDa_ref, ys_full_5mDa_sm_ref, label='ref')
ax.plot(xs_full_1mDa, ys_full_5mDa_sm, label='dat')

# Perform linear calibration over known peaks
def testing():
    
    # Load data
#    fn = r"Q:\NIST_Projects\EUV_APT_IMS\BWC\R45_data\R45_00504-v56.epos"
#    fn = r"\\cfs2w.campus.nist.gov\647\NIST_Projects\EUV_APT_IMS\BWC\R45_data\R45_04472-v02_allVfromAnn.epos"
    fn = r"C:\Users\bwc\Documents\NetBeansProjects\R44_03115\recons\recon-v02\default\R44_03115-v02.epos"
    
    epos = apt_fileio.read_epos_numpy(fn)
#    epos = epos[0:1000000]    
    
    epos1 = epos[0:-1:2]
    epos2 = epos[1::2]


    
    
    N_lower = 8
    N_upper = 16
    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):
        m2q_roi = [0.9,180]
        
        import time
        t_start = time.time()
        pointwise_scales,piecewise_scales = get_all_scale_coeffs(epos1['m2q'],
                                                                 m2q_roi=m2q_roi,
                                                                 cts_per_slice=cts_per_slice,
                                                                 max_scale=1.15)
        t_end = time.time()
        time_res[idx] = t_end-t_start
        print('Total Time = ',time_res[idx])
        
        
        # Compute corrected data
        m2q_corr = epos2['m2q']/pointwise_scales
            
        _, ys = bin_dat(m2q_corr,isBinAligned=True,bin_width=0.01,user_roi=[0,250])
    
        opt_res[idx] = chi2(ys)
#        ys = ys/np.sum(ys)
#        opt_res[idx] = np.sum(np.square(ys))
#        opt_res[idx] = test_1d_vec(m2q_corr[(m2q_corr>0.8) & (m2q_corr<80)])
        print(opt_res[idx])
        
    print(slicings)
    print(opt_res/np.max(opt_res))
    print(time_res)
    
    
    
    fig = plt.figure(num=666)
    fig.clear()
    ax = fig.gca()

    ax.plot(tmpx,tmpy,'s-', 
            markersize=8,label='SiO2')

    ax.plot(slicings,opt_res/np.max(opt_res),'o-', 
            markersize=8,label='ceria')
    ax.set(xlabel='N (events per chunk)', ylabel='compactness metric (normalized)')
    ax.set_xscale('log')    

    
    ax.legend()
       
    ax.set_xlim(5,1e5)
    ax.set_ylim(0.15, 1.05)


    fig.tight_layout()
    
    
    return 0   
def __main__():
    
    
    plt.close('all')
    #
    # Load data
    fn = r"Q:\NIST_Projects\EUV_APT_IMS\BWC\R45_data\R45_00504-v56.epos"
    fn = r"\\cfs2w.campus.nist.gov\647\NIST_Projects\EUV_APT_IMS\BWC\R45_data\R45_04472-v02_allVfromAnn.epos"

    #fn = r"Q:\NIST_Projects\EUV_APT_IMS\BWC\GaN epos files\R20_07148-v01_vbm_corr.epos"
    #fn = r"D:\Users\clifford\Documents\Python Scripts\NIST_DATA\R20_07094-v03.epos"
    #fn = r"D:\Users\clifford\Documents\Python Scripts\NIST_DATA\R45_04472-v03.epos"
    #fn = r"Q:\NIST_Projects\EUV_APT_IMS\BWC\Final EPOS for APL Mat Paper\R20_07263-v02.epos"
    #fn = r"Q:\NIST_Projects\EUV_APT_IMS\BWC\Final EPOS for APL Mat Paper\R20_07080-v01.epos"
    #fn = r"Q:\NIST_Projects\EUV_APT_IMS\BWC\Final EPOS for APL Mat Paper\R20_07086-v01.epos"
    #fn = r"Q:\NIST_Projects\EUV_APT_IMS\BWC\Final EPOS for APL Mat Paper\R20_07276-v03.epos"
#    fn = r"Q:\NIST_Projects\EUV_APT_IMS\BWC\R45_data\R45_04472-v03.epos"
    #fn = r"Q:\NIST_Projects\EUV_APT_IMS\BWC\R45_data\R45_04472-v02.epos"
#    fn = r"Q:\NIST_Projects\EUV_APT_IMS\BWC\GaN epos files\R20_07148-v01.epos" # Mg doped
#    fn = fn[:-5]+'_vbm_corr.epos'
    
    
    epos = apt_fileio.read_epos_numpy(fn)
        
#    plotting_stuff.plot_TOF_vs_time(epos['m2q'],epos,1,clearFigure=True,user_ylim=[0,150])
#    epos = epos[0:2**20]
    
    cts_per_slice=2**10
    m2q_roi = [0.8,75]
    
    import time
    t_start = time.time()
    pointwise_scales,piecewise_scales = get_all_scale_coeffs(epos['m2q'],
                                                             m2q_roi=m2q_roi,
                                                             cts_per_slice=cts_per_slice,
                                                             max_scale=1.15)
    t_end = time.time()
    print('Total Time = ',t_end-t_start)
    
    
    
    
    
    # Plot histogram in log space
    lys_corr = np.log(epos['m2q'])-np.log(pointwise_scales)
    N,x_edges,ly_edges = create_histogram(lys_corr,y_roi=m2q_roi,cts_per_slice=cts_per_slice)
    
#    fig = plt.figure(figsize=(8,8))
#    plt.imshow(np.log1p(np.transpose(N)), aspect='auto', interpolation='none',
#               extent=extents(x_edges) + extents(ly_edges), origin='lower')
#    
    # Compute corrected data
    m2q_corr = epos['m2q']/pointwise_scales
    
    # Plot data uncorrected and corrected
    TEST_PEAK = 32
    ax = plotting_stuff.plot_TOF_vs_time(epos['m2q'],epos,111,clearFigure=True,user_ylim=[0,75])
    ax.plot(pointwise_scales*TEST_PEAK)
    plotting_stuff.plot_TOF_vs_time(m2q_corr,epos,222,clearFigure=True,user_ylim=[0,75])
    
    # Plot histograms uncorrected and corrected
    plotting_stuff.plot_histo(m2q_corr,333,user_xlim=[0, 75],user_bin_width=0.01)
    plotting_stuff.plot_histo(epos['m2q'],333,user_xlim=[0, 75],clearFigure=False,user_bin_width=0.01)
    
#    epos['m2q'] = m2q_corr
#    apt_fileio.write_epos_numpy(epos,'Q:\\NIST_Projects\\EUV_APT_IMS\\BWC\\GaN epos files\\R20_07148-v01_vbmq_corr.epos')
    
    
    _, ys = bin_dat(m2q_corr,isBinAligned=True,bin_width=0.01,user_roi=[0,75])

    print(np.sum(np.square(ys)))
    

    
    return 0
Exemplo n.º 14
0
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 get_peak_ranges(epos, peak_m2qs,peak_height_fraction=0.01, glob_bg_param=0):

    # Initialize a peak paramter array
    pk_params = np.full(peak_m2qs.size,-1,dtype=[('x0_nominal','f4'),
                                                 ('x0_g_fit','f4'),
                                                 ('x0_mean_shift','f4'),
                                                ('std_fit','f4'),
                                                ('off','f4'),
                                                ('amp','f4'),
                                                ('pre_rng','f4'),
                                                ('post_rng','f4'),
                                                ('pre_bg_rng','f4'),
                                                ('post_bg_rng','f4'),
                                                ('loc_bg','f4')])
    pk_params['x0_nominal'] = peak_m2qs
    
    
    
    
    
    
    full_roi = np.array([0, 150])
    xs_full_1mDa, ys_full_1mDa = bin_dat(epos['m2q'],user_roi=full_roi,isBinAligned=True)
    ys_full_5mDa_sm = do_smooth_with_gaussian(ys_full_1mDa, std=5)
    
    N_kern = 250;
    ys_full_fwd_sm = forward_moving_average(ys_full_1mDa,n=N_kern)
    ys_full_bwd_sm = forward_moving_average(ys_full_1mDa,n=N_kern,reverse=True)
    
    ys_glob_bg_1mDa = physics_bg(xs_full_1mDa,glob_bg_param)
    
    # Get estiamtes for x0, amp, std_fit
    for idx,pk_param in enumerate(pk_params):
        # Select peak roi
        roi_half_wid = 0.5
        pk_roi = np.array([-roi_half_wid, roi_half_wid])+pk_param['x0_nominal']
        pk_dat = epos['m2q'][(epos['m2q']>pk_roi[0]) & (epos['m2q']<pk_roi[1])]
        
        # Fit to gaussian
        smooth_param = 5
        popt = fit_to_g_off(pk_dat,user_std=smooth_param)
        pk_param['amp'] = popt[0]
        pk_param['x0_g_fit'] = popt[1]
        pk_param['std_fit'] = popt[2]
        pk_param['off'] = popt[3]
#        print('gaussian peak loc',pk_param['x0_g_fit'])
        
        
        pk_param['x0_mean_shift'] = mean_shift_peak_location(pk_dat,user_std=pk_param['std_fit'],user_x0=pk_param['x0_g_fit'])
        
#        ax = plt.gca()
#        ax.plot(np.array([1,1])*pk_param['x0_mean_shift'],np.array([0, pk_param['amp']+pk_param['off']]),'k--')
        
#        plt.pause(0.001)
#        plt.pause(2)
    
        # select starting locations
        pk_idx = np.argmin(np.abs(pk_param['x0_mean_shift']-xs_full_1mDa))
        pk_lhs_idx = np.argmin(np.abs((pk_param['x0_mean_shift']-pk_param['std_fit'])-xs_full_1mDa))    
        pk_rhs_idx = np.argmin(np.abs((pk_param['x0_mean_shift']+pk_param['std_fit'])-xs_full_1mDa))
        
        # Create a peak amplitude estimate.  I use the average of the gaussian amplitude and the 5 mDa smoothed data.
        # Notice the gaussian amplitude has the baseline removed, and therefor I subtract the global background from the smoothed data.
        pk_amp = 0.5*(pk_param['amp'] + ys_full_5mDa_sm[pk_idx]-ys_glob_bg_1mDa[pk_idx])
        
        curr_val = ys_full_5mDa_sm[pk_idx]
        
        for i in np.arange(pk_lhs_idx,-1,-1):
            curr_val = ys_full_5mDa_sm[i]        
            
            # Note that the global background is subtracted off the current value.  One day I should make this more general...
            if ((curr_val-ys_glob_bg_1mDa[i]) < peak_height_fraction*pk_amp) and (pk_param['pre_rng']<0):
                # This is a range limit
                pk_param['pre_rng'] = xs_full_1mDa[i]
            
            if curr_val<ys_full_bwd_sm[i]:
                # Assume we are at the prepeak baseline noise
                if pk_param['pre_rng']<0:
                    pk_param['pre_rng'] = xs_full_1mDa[i]
                pk_param['pre_bg_rng'] = xs_full_1mDa[i]
                break
            
        curr_val = ys_full_5mDa_sm[pk_idx]
        for i in np.arange(pk_rhs_idx,xs_full_1mDa.size):
            curr_val = ys_full_5mDa_sm[i]        
                
            if ((curr_val-ys_glob_bg_1mDa[i]) < peak_height_fraction*pk_amp) and (pk_param['post_rng']<0):
                # This is a range limit
                pk_param['post_rng'] = xs_full_1mDa[i]
            
            if curr_val<ys_full_fwd_sm[i]:
                # Assume we are at the prepeak baseline noise
                if pk_param['post_rng']<0:
                    pk_param['post_rng'] = xs_full_1mDa[i]
                pk_param['post_bg_rng'] = xs_full_1mDa[i]
                break      
        
        pre_pk_rng = [pk_param['pre_bg_rng']-0.22,pk_param['pre_bg_rng']-0.02]
        pk_param['loc_bg'] = np.sum((epos['m2q']>=pre_pk_rng[0]) & (epos['m2q']<=pre_pk_rng[1]))*0.001/(pre_pk_rng[1]-pre_pk_rng[0])

        
#        ax.plot(np.array([1,1])*pk_param['pre_rng'] ,np.array([0,1])*(pk_param['amp']+pk_param['off']),'k--')
#        ax.plot(np.array([1,1])*pk_param['post_rng'] ,np.array([0,1])*(pk_param['amp']+pk_param['off']),'k--')
#    #    ax.plot(np.array([1,1])*(pk_param['x0']+1*pk_param['std_fit'])   ,np.array([np.min(ys_smoothed),np.max(ys_smoothed)]),'k--')
#    #    ax.plot(np.array([1,1])*(pk_param['x0']+5*pk_param['std_fit']),np.array([np.min(ys_smoothed),np.max(ys_smoothed)]),'k--')
#        plt.pause(0.1)
    
#    ax.clear()
#    ax.plot(xs_full_1mDa,ys_full_5mDa_sm,label='5 mDa smooth')    
#    for idx,pk_param in enumerate(pk_params):
#        ax.plot(np.array([1,1])*pk_param['pre_rng'] ,np.array([0.5,(pk_param['amp']+pk_param['off'])]),'k--')
#        ax.plot(np.array([1,1])*pk_param['post_rng'] ,np.array([0.5,(pk_param['amp']+pk_param['off'])]),'k--')
#        ax.plot(np.array([1,1])*pk_param['pre_bg_rng'] ,np.array([0.5,(pk_param['amp']+pk_param['off'])]),'r--')
#        ax.plot(np.array([1,1])*pk_param['post_bg_rng'] ,np.array([0.5,(pk_param['amp']+pk_param['off'])]),'r--')
#    
#    ax.set_yscale('log')
#    ax.set(ylim=[0.1,1000])
    
    return pk_params
Exemplo n.º 16
0
print('Total Ranged Ions: ' + str(np.sum(cts['total'])))
print('Total Ranged Local Background Ions: ' + str(np.sum(cts['local_bg'])))
print('Total Ranged Global Background Ions: ' + str(np.sum(cts['global_bg'])))
print('Total Ions: ' + str(epos.size))

print('Overall CSR (no bg)    : ' +
      str(np.sum(cts['total'][Ga2p_idxs]) / np.sum(cts['total'][Ga1p_idxs])))
print('Overall CSR (local bg) : ' + str(
    (np.sum(cts['total'][Ga2p_idxs]) - np.sum(cts['local_bg'][Ga2p_idxs])) /
    (np.sum(cts['total'][Ga1p_idxs]) - np.sum(cts['local_bg'][Ga1p_idxs]))))
print('Overall CSR (global bg): ' + str(
    (np.sum(cts['total'][Ga2p_idxs]) - np.sum(cts['global_bg'][Ga2p_idxs])) /
    (np.sum(cts['total'][Ga1p_idxs]) - np.sum(cts['global_bg'][Ga1p_idxs]))))

# Plot all the things
xs, ys = bin_dat(epos['m2q'], user_roi=[0.5, 100], isBinAligned=True)
#ys_sm = ppd.do_smooth_with_gaussian(ys,10)
ys_sm = ppd.moving_average(ys, 10)

glob_bg = ppd.physics_bg(xs, glob_bg_param)

fig = plt.figure(num=100)
fig.clear()
ax = fig.gca()

ax.plot(xs, ys_sm, label='hist')
ax.plot(xs, glob_bg, label='global bg')

ax.set(xlabel='m/z (Da)', ylabel='counts')
ax.grid()
fig.tight_layout()
Exemplo n.º 17
0
      " seconds")

# Find c and t0 for ToF data based on aligning to reference spectrum
m2q_corr, p_m2q = m2q_calib.align_m2q_to_ref_m2q(ref_epos['m2q'],
                                                 tof_corr,
                                                 nom_voltage=np.mean(
                                                     epos['v_dc']))
print(p_m2q)

plotting_stuff.plot_histo(ref_epos['m2q'], 4, user_label='ref')
plotting_stuff.plot_histo(m2q_corr[m2q_corr.size // 2:-1],
                          4,
                          clearFigure=False,
                          user_label='[c,t0] corr')

xs, ys = bin_dat(tof_corr, isDensity=True, bin_width=0.25)

bg_lvl = np.mean(ys[(xs > 1000) & (xs < 3500)])

idxs = np.nonzero((xs > 200) & (xs < 1000))

xs = xs[idxs]
ys = ys[idxs]

fig = plt.figure(num=113)
fig.clear()
ax = fig.gca()
ax.plot(xs, np.cumsum(ys**2))
ax.plot(xs, np.cumsum((ys - bg_lvl)**2))

# Read in data
def fit_to_g_off(dat, user_std, user_p0=np.array([])):
    
    if dat.size<32:
        raise Exception('NEED MORE COUNTS IN PEAK')
        
    std = user_std
        
    xs,ys = bin_dat(dat)
    
    ys_smoothed = do_smooth_with_gaussian(ys,std)
    
    
    opt_fun = lambda p: np.sum(np.square(pk_mod_fun(xs, *p)-ys_smoothed))

    

#    def resid_func(p): 
#        return pk_mod_fun(xs,*p)-ys_smoothed
    
    N4 = ys_smoothed.size//4
    mx_idx = np.argmax(ys_smoothed[N4:(3*N4)])+N4
    
    if(user_p0.size == 0):
        p0 = np.array([ys_smoothed[mx_idx]-np.min(ys_smoothed), xs[mx_idx], 0.015,  np.percentile(ys_smoothed,20)])
    else:
        p0 = user_p0    
        
    # b_model2(x,amp_g,x0,sigma,b):
    lbs = np.array([0,       np.percentile(xs,10),  0.005,   0])
    ubs = np.array([2*p0[0],  np.percentile(xs,90), 0.50,   p0[0]])

    # Force in bounds
    p_guess = np.sort(np.c_[lbs,p0,ubs])[:,1]
    
    
#    popt2, pcov =  = curve_fit(pk_mod_fun, xs, ys_smoothed, p0=p_guess, bounds=(lbs,ubs), verbose=2, ftol=1e-12, max_nfev=2048)
#    popt2, pcov =  = curve_fit(pk_mod_fun, xs, ys_smoothed)
    
    
#    bnds = ((0,2*p0[0]),
#            (np.percentile(xs,10),np.percentile(xs,90)),
#             (0.007,0.1),
#             (0,p0[0]))
#    

#    opts = {'xatol' : 1e-5,
#            'fatol' : 1e-12,
#             'maxiter' : 1024,
#             'maxfev' : 1024,
#             'disp' : True}
    
    
    ret_dict = constrNM(opt_fun,p_guess,lbs,ubs,xtol=1e-5, ftol=1e-12, maxiter=1024, maxfun=1024, full_output=1, disp=0)
    
#    print(p_guess)
#    print(ret_dict['xopt'])
    
#    res = minimize(opt_fun, 
#                   p_guess,
#                   options=opts,
##                   bounds=bnds,
#                   method='Nelder-Mead')  
#
#    if res.x[1]<np.percentile(xs,10) or res.x[1]>np.percentile(xs,90):
#        res.x = p_guess


#    print(np.abs(res.x))
    
    
#    popt2 = least_squares(resid_func, x0=p0, bounds=(lbs,ubs), verbose=2, ftol=1e-12, max_nfev=2048)
#    curve_fit(pk_mod_fun, xs, ys_smoothed, p0=p0)
    
#    popt = least_squares(resid_func, p0, verbose=0, ftol=1e-12, max_nfev=2048)
#
#    fig = plt.figure(num=999)
#    fig.clear()
#    ax = plt.axes()
#    
#    ax.plot(xs,ys,'.',label='raw')
#    ax.plot(xs,ys_smoothed,label='smoothed')
#    
#    mod_y = pk_mod_fun(xs,*p_guess)
#    ax.plot(xs,mod_y,label='guess')
#    
#    
#    mod_y = pk_mod_fun(xs,*res.x)
#    
#    ax.plot(xs,mod_y,label='fit')
#    
#    ax.legend()
#    
#    
#    
#    plt.pause(.001)
#
#    # Find halfway down and up each side
#    max_idx = np.argmax(N)
#    max_val = N[max_idx]
#    
#    lhs_dat = N[0:max_idx]
#    rhs_dat = N[max_idx:]
#    
#    lhs_idx = np.argmin(np.abs(lhs_dat-max_val/2))
#    rhs_idx = np.argmin(np.abs(rhs_dat-max_val/2))+max_idx
##    
#    hwhm_est = (x[rhs_idx]-x[lhs_idx])/2
#
#
    return np.abs(ret_dict['xopt'])
Exemplo n.º 19
0
        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)


x, y = do_chi2v3(varying_tof, tof_roi, 3, 10)
plt.plot(x, y, label='same')

x = [2**3, 2**10]
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,