def rebin_profile(self, nbins=300, plot=False): x, y, e = bu.rebin(self.profile[0], self.profile[1], \ errs=self.profile[2], nbins=nbins, \ plot=plot) self.profile = [x, y, e] self.prof_dx = np.abs(x[1] - x[0]) x2, y2, e2 = bu.rebin(self.integral[0], self.integral[1], \ errs=self.integral[2], nbins=nbins, \ plot=plot) self.integral = [x2, y2, e2] self.int_dx = np.abs(x2[1] - x2[0])
def profile(fname, data_column=0, plot=False, nbins=200): df = bu.DataFile() df.load(fname, skip_fpga=True) df.load_other_data() df.calibrate_stage_position() dt = 1.0 / df.fsamp if 'ysweep' in fname: stage_column = 1 if not ysign: sign = 1.0 else: sign = ysign else: stage_column = 0 sign = 1.0 b, a = sig.butter(1, 0.5) if plot: shape = np.shape(df.other_data) for i in range(shape[0]): plt.plot(df.other_data[i, :], label=str(i)) plt.title('Data Columns') plt.legend() plt.tight_layout() plt.figure() for j in range(3): plt.plot(df.cant_data[j, :], label=str(j)) plt.title('Attractor Coordinates') plt.legend() plt.tight_layout() plt.show() input() h = np.mean(df.cant_data[2, :]) h_round = bu.round_sig(h, sig=3) if h_round < 10.0: h_round = bu.round_sig(h_round, sig=2) int_filt = sig.filtfilt(b, a, df.other_data[data_column]) proft = np.gradient(int_filt) # proft = np.gradient(df.other_data[data_column]) #plt.plot(df.other_data[0]) #plt.show() stage_filt = sig.filtfilt(b, a, df.cant_data[stage_column, :]) dir_sign = np.sign(np.gradient(stage_filt)) * sign dir_sign = np.sign(np.gradient(df.cant_data[stage_column])) * sign xvec = df.cant_data[stage_column, :] yvec = (proft - proft * dir_sign) * 0.5 - (proft + proft * dir_sign) * 0.5 # sort_inds = np.argsort(xvec) # b, y, e = bu.spatial_bin(xvec, yvec, dt, nbins=300, nharmonics=300, add_mean=True) b, y, e = bu.rebin(xvec[vec_inds[0]:vec_inds[1]], \ yvec[vec_inds[0]:vec_inds[1]], \ nbins=nbins, plot=False, correlated_errs=True) return b, y, e, h_round
if not first: lower_ind = np.argmax(np.abs(lib)) first = True else: lower_ind = 0 if initial_offset and times[i] < initial_offset: continue upper_ind = np.argmin(np.abs(tvec + times[i] - ringdown_fit_time)) fit_time = tvec[lower_ind:upper_ind] + times[i] fit_amp = amp[lower_ind:upper_ind] fit_time_rebin, fit_amp_rebin, fit_err_rebin = \ bu.rebin(fit_time, fit_amp, nbins=nbins, plot=plot_rebin, \ correlated_errs=True) fit_times.append(fit_time_rebin) fit_amps.append(fit_amp_rebin) fit_errs.append(fit_err_rebin) print('Fitting ringdown...', end=' ') sys.stdout.flush() fit_x = np.concatenate(fit_times) fit_y = np.concatenate(fit_amps) fit_err = np.concatenate(fit_errs) plot_x = np.linspace(fit_x[0], fit_x[-1], 500) fit_func = lambda x, amp0, t0, tau, c: amp0 * np.exp(-1.0 *
def analyze_file(fname, nbins=500, grad_thresh=10, use_highp_bara=True, plot_pressures=False, \ plot_raw_data=False, find_dipole=True): phases = get_delta_phi(fname) phase_errs = get_delta_phi_err(fname) pressures = get_pressure(fname) times = get_time(fname) field_data = get_field_data(fname) rot_freq = np.mean(field_data['field_freq']) rot_freq_err = np.std(field_data['field_freq']) rot_amp = np.mean(field_data['field_amp']) rot_amp_err = np.std(field_data['field_amp']) # plt.figure() # plt.plot(field_data['field_amp']) # plt.figure() # plt.plot(field_data['field_freq']) # plt.show() pressures_real, pressures_smooth = build_full_pressure(pressures, plot=plot_pressures, \ use_highp_bara=use_highp_bara) sort_inds = np.argsort(pressures_real) phases = phases[sort_inds] phase_errs = phase_errs[sort_inds] pressures_real = pressures_real[sort_inds] #plt.errorbar(pressures_real, phases, yerr=phase_errs, ms=2) #plt.show() # phases_start = phases[:50] # phases_start = np.unwrap( 2.0 * phases_start ) / 2.0 # plt.plot(phases[:50]) # plt.plot(phases_start) # plt.show() # Compute the initial phase for offsetting so arcsin(phi0) = 0 phi0 = np.mean(phases[:5]) # Find where we lose lock by looking for sharp derivative raw_grad = np.gradient(np.unwrap(2.0 * phases)) #plt.show() init_ind = int(np.max([10.0, 0.01*len(raw_grad)])) raw_grad_init = np.std(raw_grad[:init_ind]) raw_grad -= np.mean(raw_grad[:init_ind]) bad_inds = np.array(list(range(len(raw_grad))))[np.abs(raw_grad) > grad_thresh * raw_grad_init] lock_lost_ind = -1 # Make sure we didn't just find an anomolous fluctuation for indind, ind in enumerate(bad_inds): if ind == bad_inds[-2]: lock_lost_ind = -1 break delta = np.abs(ind - bad_inds[indind+1]) if delta < 10: delta2 = np.abs(bad_inds[indind+1] - bad_inds[indind+2]) if delta2 < 10: lock_lost_ind = ind break p_outgassing = (times[lock_lost_ind] - times[0]) * 1e-9 * outgassing_rate # Reconstruct phase difference of fundamental rotation by # unwrapping data prior to losing lock, then using the raw # data after losing lock uphases = np.unwrap(2.0*phases) / 2.0 init_offset = np.mean(uphases[:10]) uphases -= init_offset uphases[lock_lost_ind:] = phases[lock_lost_ind:] if plot_raw_data: plt.scatter(pressures_real, uphases, s=100) plt.axvline(pressures_real[lock_lost_ind]) plt.show() fit_pressures = pressures_real[:lock_lost_ind-2] fit_uphases = uphases[:lock_lost_ind-2] fit_errs = phase_errs[:lock_lost_ind-2] fit_pressures_2, fit_uphases_2, fit_errs_2 = \ bu.rebin(fit_pressures, fit_uphases, errs=fit_errs, nbins=50) #plt.errorbar(pressures_real, uphases, yerr=phase_errs) #plt.errorbar(fit_pressures_2, fit_uphases_2, yerr=fit_errs_2) #plt.axvline(pressures_real[lock_lost_ind-2]) #plt.show() zero_inds = np.where(fit_errs_2 == 0.0) non_zero_inds = np.invert(zero_inds) fit_errs_2[zero_inds] += np.sqrt(np.mean( fit_errs_2[non_zero_inds]**2 )) p0 = [1.1*pressures_real[lock_lost_ind-2], 0] if not np.sum(np.isnan(fit_uphases_2)): fit_pressures = fit_pressures_2 fit_uphases = fit_uphases_2 fit_errs = fit_errs_2 pphi, covphi = curve_fit(phi_ffun, fit_pressures, fit_uphases, sigma=fit_errs, p0 = p0, \ bounds=([0.005, -0.5], [1.5*pressures_real[lock_lost_ind-2], 0.5]), \ maxfev=10000) param_arr = np.linspace(pphi[0]*0.99, pphi[0]*1.01, 200) def nll(param): inds = fit_pressures < param resid = np.abs(fit_uphases[inds]-pphi[1] - phi_ffun(fit_pressures[inds], param, 0)) return (1. / (np.sum(inds) - 1)) * np.sum(resid**2 / fit_errs[inds]**2) pmax, pmax_err, min_chi = bu.minimize_nll(nll, param_arr, plot=False) uphases -= pphi[1] cut_inds = pressures_real< 1.2*pmax pressures_cut = pressures_real[cut_inds] uphases_cut = uphases[cut_inds] rand_phase_ind = np.argmin( np.abs(pressures_cut - pphi[0]) ) #print rand_phase_ind #print filname #plt.plot(pressures_cut, uphases_cut) #plt.show() pressures_out_1, uphases_out_1, errs_out_1 = bu.rebin(pressures_cut[:rand_phase_ind], \ uphases_cut[:rand_phase_ind], \ nbins=nbins) pressures_out_2 = pressures_cut[rand_phase_ind:] uphases_out_2 = uphases_cut[rand_phase_ind:] # print 'pmax uncertainty: ', pmax_err / pmax return np.array([np.concatenate((pressures_out_1, pressures_out_2)), \ np.concatenate((uphases_out_1, uphases_out_2))]), \ pmax, pmax_err, p_outgassing, rot_freq, rot_freq_err, \ rot_amp, rot_amp_err
guess=7e-3) #x_d, x_prof, x_popt = chopfuncs.profile(xfilobj, raw_dat_col = 0, \ # return_pos = True, numbins = 500, \ # fit_intensity = True, plot_peaks=False) #y_d, y_prof, y_popt = chopfuncs.profile(yfilobj, raw_dat_col = 0, \ # return_pos = True, numbins = 500, \ # fit_intensity = True, plot_peaks=False) x_prof = x_prof / x_popt[0] y_prof = y_prof / y_popt[0] x_popt[0] = 1.0 y_popt[0] = 1.0 binned_x_d, binned_x_prof, x_errs = bu.rebin(x_d, x_prof, nbins=200) binned_y_d, binned_y_prof, y_errs = bu.rebin(y_d, y_prof, nbins=200) print("X diam (2 * waist): ", x_popt[-1] * 1e3 * 2) print("Y diam (2 * waist): ", y_popt[-1] * 1e3 * 2) print() print("X waist: ", x_popt[-1] * 1e3) print("Y waist: ", y_popt[-1] * 1e3) print() #LP_p0 = [1, 0.001] #final_x_LP_popt, final_x_LP_pcov = \ # opti.curve_fit(chopfuncs.bessel_intensity, \