def count_and_get_compositions(epos, pk_data, pk_params, glob_bg_param, bg_frac=1, noise_threshhold=2): import peak_param_determination as ppd # Count the peaks, local bg, and global bg cts = ppd.do_counting(epos, pk_params, glob_bg_param) cts['local_bg'] = cts['local_bg'] * bg_frac cts['global_bg'] = cts['global_bg'] * bg_frac # Test for peak S/N and throw out craptastic peaks B = np.max(np.c_[cts['local_bg'][:, None], cts['global_bg'][:, None]], 1)[:, None] T = cts['total'][:, None] S = T - B std_S = np.sqrt(T + B) # Make up a threshold for peak detection... for the most part this won't matter # since weak peaks don't contribute to stoichiometry much... except for Mg! is_peak = S > (noise_threshhold * np.sqrt(2 * B)) for idx, ct in enumerate(cts): if not is_peak[idx]: for i in np.arange(len(ct)): ct[i] = 0 # Calculate compositions compositions = ppd.do_composition(pk_data, cts) return (cts, compositions, is_peak)
# Test for peak S/N and throw out craptastic peaks B = np.max(np.c_[cts['local_bg'][:, None], cts['global_bg'][:, None]], 1)[:, None] T = cts['total'][:, None] S = T - B std_S = np.sqrt(T + B) # Make up a threshold for peak detection... for the most part this won't matter # since weak peaks don't contribute to stoichiometry much... except for Mg! is_peak = S > 2 * np.sqrt(2 * B) for idx, ct in enumerate(cts): if not is_peak[idx]: for i in np.arange(len(ct)): ct[i] = 0 # Calculate compositions compositions = ppd.do_composition(pk_data, cts) ppd.pretty_print_compositions(compositions, pk_data) 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]))))
def CSR_plot(run_number, comp_csr_fig_idx, spec_fig_num, multi_fig_num): # Read in data epos = GaN_fun.load_epos(run_number=run_number, epos_trim=[5000, 5000], fig_idx=plt.figure().number) pk_data = GaN_type_peak_assignments.GaN() bg_rois = [[0.4, 0.9]] pk_params, glob_bg_param, Ga1p_idxs, Ga2p_idxs = GaN_fun.fit_spectrum( epos=epos, pk_data=pk_data, peak_height_fraction=0.1, bg_rois=bg_rois) cts, compositions, is_peak = GaN_fun.count_and_get_compositions( epos=epos, pk_data=pk_data, pk_params=pk_params, glob_bg_param=glob_bg_param, bg_frac=1, noise_threshhold=2) ppd.pretty_print_compositions(compositions, pk_data) # Plot the full spectrum xs, ys_sm = GaN_fun.bin_and_smooth_spectrum(epos=epos, user_roi=[0, 150], bin_wid_mDa=30, smooth_wid_mDa=-1) fig = plt.figure(num=spec_fig_num) ax = fig.gca() scale_factor = 1E4**next(counter) ax.plot(xs, scale_factor * (ys_sm + 1), lw=1, label=run_number) glob_bg = ppd.physics_bg(xs, glob_bg_param) ax.plot(xs, scale_factor * (glob_bg + 1), lw=1, label=run_number + ' (bg)', alpha=1) # Plot some detector hitmaps GaN_fun.create_det_hit_plots(epos, pk_data, pk_params, fig_idx=plt.figure().number) # Find the pole center and show it ax = plt.gcf().get_axes()[0] m2q_roi = [3, 100] sel_idxs = np.where((epos['m2q'] > m2q_roi[0]) & (epos['m2q'] < m2q_roi[1])) xc, yc = GaN_fun.mean_shift(epos['x_det'][sel_idxs], epos['y_det'][sel_idxs]) a_circle = plt.Circle((xc[-1], yc[-1]), 10, facecolor='none', edgecolor='k', lw=2, ls='-') ax.add_artist(a_circle) # Chop data up by Radius AND time time_chunk_centers, r_centers, idxs_list = GaN_fun.chop_data_rad_and_time( epos, [xc[-1], yc[-1]], time_chunk_size=2**16, N_ann_chunks=3) N_time_chunks = time_chunk_centers.size N_ann_chunks = r_centers.size # Charge state ratios csr = np.full([N_time_chunks, N_ann_chunks], -1.0) # Gallium atomic % and standard deviation (no bg) Ga_comp = np.full([N_time_chunks, N_ann_chunks], -1.0) Ga_comp_std = np.full([N_time_chunks, N_ann_chunks], -1.0) # Gallium atomic % and standard deviation (global bg) Ga_comp_glob = np.full([N_time_chunks, N_ann_chunks], -1.0) Ga_comp_std_glob = np.full([N_time_chunks, N_ann_chunks], -1.0) # Multiplicy hit_multiplicity = np.full([N_time_chunks, N_ann_chunks], -1.0) tot_cts = np.full([N_time_chunks, N_ann_chunks], -1.0) keys = list(pk_data.dtype.fields.keys()) keys.remove('m2q') Ga_idx = keys.index('Ga') for t_idx in np.arange(N_time_chunks): for a_idx in np.arange(N_ann_chunks): idxs = idxs_list[t_idx][a_idx] sub_epos = epos[idxs] bg_frac_roi = [120, 150] bg_frac = np.sum((sub_epos['m2q']>bg_frac_roi[0]) & (sub_epos['m2q']<bg_frac_roi[1])) \ / np.sum((epos['m2q']>bg_frac_roi[0]) & (epos['m2q']<bg_frac_roi[1])) cts, compositions, is_peak = GaN_fun.count_and_get_compositions( epos=sub_epos, pk_data=pk_data, pk_params=pk_params, glob_bg_param=glob_bg_param, bg_frac=bg_frac, noise_threshhold=2) csr[t_idx, a_idx] = np.sum(cts['total'][Ga2p_idxs] - cts['global_bg'][Ga2p_idxs]) / np.sum( cts['total'][Ga1p_idxs] - cts['global_bg'][Ga1p_idxs]) Ga_comp[t_idx, a_idx] = ppd.do_composition(pk_data, cts)[0][0][Ga_idx] Ga_comp_std[t_idx, a_idx] = ppd.do_composition(pk_data, cts)[0][1][Ga_idx] Ga_comp_glob[t_idx, a_idx] = ppd.do_composition(pk_data, cts)[2][0][Ga_idx] Ga_comp_std_glob[t_idx, a_idx] = ppd.do_composition(pk_data, cts)[2][1][Ga_idx] low_mz_idxs = np.where(sub_epos['m2q'] < 100)[0] hit_multiplicity[t_idx, a_idx] = np.sum( sub_epos[low_mz_idxs]['ipp'] != 1) / sub_epos[low_mz_idxs].size compositions = ppd.do_composition(pk_data, cts) # ppd.pretty_print_compositions(compositions,pk_data) # print('COUNTS IN CHUNK: ',np.sum(cts['total'])) tot_cts[t_idx, a_idx] = np.sum(cts['total']) fig = plt.figure(num=comp_csr_fig_idx) #fig.clear() ax = fig.gca() #ax.errorbar(csr.flatten(),Ga_comp.flatten(),yerr=Ga_comp_std.flatten(),fmt='.',capsize=4,label='det based (radial)') ax.errorbar(csr.flatten(), Ga_comp_glob.flatten(), yerr=Ga_comp_std_glob.flatten(), fmt='.', capsize=4, label=run_number) fig = plt.figure(num=multi_fig_num) #fig.clear() ax = fig.gca() ax.plot(csr.flatten(), hit_multiplicity.flatten(), '.', label=run_number) # Write data to console for copy/pasting print('CSR', '\t', 'Ga comp (glob bg)', '\t', 'Ga comp std (glob bg)') for i in np.arange(csr.size): print(csr.flatten()[i], '\t', Ga_comp_glob.flatten()[i], '\t', Ga_comp_std_glob.flatten()[i]) return fig
# tot_cts_roi[t_idx,a_idx] = np.sum(ys_sm) tot_cts_roi[t_idx, a_idx] = sub_epos.size cts, compositions, is_peak = GaN_fun.count_and_get_compositions( epos=sub_epos, pk_data=pk_data, pk_params=pk_params, glob_bg_param=glob_bg_param, bg_frac=bg_frac, noise_threshhold=2) csr[t_idx, a_idx] = np.sum(cts['total'][Ga2p_idxs] - cts['global_bg'] [Ga2p_idxs]) / np.sum(cts['total'][Ga1p_idxs] - cts['global_bg'][Ga1p_idxs]) Ga_comp[t_idx, a_idx] = ppd.do_composition(pk_data, cts)[0][0][Ga_idx] Ga_comp_std[t_idx, a_idx] = ppd.do_composition(pk_data, cts)[0][1][Ga_idx] Ga_comp_glob[t_idx, a_idx] = ppd.do_composition(pk_data, cts)[2][0][Ga_idx] Ga_comp_std_glob[t_idx, a_idx] = ppd.do_composition(pk_data, cts)[2][1][Ga_idx] N_comp_glob[t_idx, a_idx] = ppd.do_composition(pk_data, cts)[2][0][N_idx] N_comp_std_glob[t_idx, a_idx] = ppd.do_composition(pk_data, cts)[2][1][N_idx] Al_comp_glob[t_idx, a_idx] = ppd.do_composition(pk_data, cts)[2][0][Al_idx]
bg_frac_roi = [120, 150] bg_frac = np.sum((sub_epos['m2q']>bg_frac_roi[0]) & (sub_epos['m2q']<bg_frac_roi[1])) \ / np.sum((epos['m2q']>bg_frac_roi[0]) & (epos['m2q']<bg_frac_roi[1])) cts, compositions, is_peak = GaN_fun.count_and_get_compositions( epos=sub_epos, pk_data=pk_data, pk_params=pk_params, glob_bg_param=glob_bg_param, bg_frac=bg_frac, noise_threshhold=2) csr[z_idx] = np.sum(cts['total'][Ga2p_idxs] - cts['global_bg'][Ga2p_idxs] ) / np.sum(cts['total'][Ga1p_idxs] - cts['global_bg'][Ga1p_idxs]) Ga_comp[z_idx] = ppd.do_composition(pk_data, cts)[0][0][Ga_idx] Ga_comp_std[z_idx] = ppd.do_composition(pk_data, cts)[0][1][Ga_idx] N_comp[z_idx] = ppd.do_composition(pk_data, cts)[0][0][N_idx] N_comp_std[z_idx] = ppd.do_composition(pk_data, cts)[0][1][N_idx] Ga_comp_glob[z_idx] = ppd.do_composition(pk_data, cts)[2][0][Ga_idx] Ga_comp_std_glob[z_idx] = ppd.do_composition(pk_data, cts)[2][1][Ga_idx] N_comp_glob[z_idx] = ppd.do_composition(pk_data, cts)[2][0][N_idx] N_comp_std_glob[z_idx] = ppd.do_composition(pk_data, cts)[2][1][N_idx] low_mz_idxs = np.where(sub_epos['m2q'] < 100)[0] hit_multiplicity[z_idx] = np.sum( sub_epos[low_mz_idxs]['ipp'] != 1) / sub_epos[low_mz_idxs].size