def fit_func(t, T0_offset): return rv.radial_velocity(t, orbital_parameters[0], orbital_parameters[1], orbital_parameters[2], orbital_parameters[3], orbital_parameters[4] + T0_offset)
p0=[0]) print("T0_offset Serval = ", popt_s) T0_offset_s = popt_s[0] sigma_ser = np.nanstd( sigma_clip(ser_rvc[:, 1] - np.nanmean(ser_rvc[:, 1]) - fit_func(ser_rvc[:, 0], T0_offset_s), sigma=5)) xlst = np.linspace(w_dates[0], w_dates[0] + orbital_parameters[1] * 0.99999, num=100) ylst = [ rv.radial_velocity(t, orbital_parameters[0], orbital_parameters[1], orbital_parameters[2], orbital_parameters[3], orbital_parameters[4] + T0_offset) for t in xlst ] #sort by xlst pltlst = [[xlst[j], ylst[j]] for j in range(len(xlst))] def mod_sort(elem): return elem[0] % orbital_parameters[1] pltlst = sorted(pltlst, key=mod_sort) pltlst = np.asarray(pltlst) pltlst = [pltlst[:, 0], pltlst[:, 1]] #to_print = pltlst[0] % i[3] #print(to_print)
def compare_results(file_list, parameter_change_list, bary_starname, orbital_parameters, objects, servaldir): """ Compares result files to find the best rv scatter around literature fit returns change in parameter that yielded best results file_list: list of `str` list containing the file paths of the files to be compared parameter_change_list: list of `int` list containing the parameter exponent shifts used to create the files in file_list orbital_parameters : list of `float` orbital_parameters = [K, P, e, omega, T0] parameters of the keplerian fit to be used as "true" baseline """ sigma_list = np.zeros(len(file_list)) + 100 # 100 is a fudge factor plots = True if plots: rec_loop_directory, key_name = os.path.split( os.path.split(file_list[0]) [0]) # HACK go up 2 directories to loop directory plot_directory = rec_loop_directory + "/compare_plots" os.makedirs(plot_directory, exist_ok=True) pp = PdfPages(plot_directory + "/" + key_name + ".pdf") fig = plt.figure(figsize=(15, 9), dpi=200) mpl.rc('font', size=16) plt.clf() fig.clf() ax1 = plt.gca() for f, fil in enumerate(file_list): #assumes order of file_listand parameter_change_list are matched. (maybe extract from file name?) wobble_res = h5py.File(fil, 'r') w_dates = wobble_res['dates'][()] w_dates_utc = wobble_res['dates_utc'][()] w_RVs = wobble_res['star_time_rvs'][()] w_RVs_original = w_RVs w_RVs_er = wobble_res['star_time_sigmas'][()] #barycorr for wobble_orig from scipy.constants import codata lightvel = codata.value('speed of light in vacuum') #for barycorr # CAHA Coordinates for barycorr _lat = 37.2236 _lon = -2.54625 _elevation = 2168. w_RVs_original_barycorr = np.zeros(len(w_dates)) for n in tqdm(range(len(w_RVs_original_barycorr))): w_RVs_original_barycorr[n] = bary.get_BC_vel( w_dates_utc[n], starname=bary_starname, lat=_lat, longi=_lon, alt=_elevation, zmeas=w_RVs_original[n] / lightvel)[0] #Serval Correction #read in SERVAL ser_rvc = np.loadtxt(servaldir + objects[1] + "/" + objects[1] + ".rvc.dat") # remove entries with nan in drift ind_finitedrift = np.isfinite(ser_rvc[:, 3]) ser_rvc = ser_rvc[ind_finitedrift] ser_corr = -ser_rvc[:, 8] - ser_rvc[:, 3] #match wobble and serval indices_serval = [] indices_wobble = [] for n in range(len(w_dates)): ind_jd = np.where( np.abs(ser_rvc[:, 0] - w_dates[n]) == np.nanmin( np.abs(ser_rvc[:, 0] - w_dates[n])))[0][0] if (ser_rvc[ind_jd, 0] - w_dates[n] ) * 24 * 60 < 20.: #only takes matches closer than 20 minutes indices_serval.append(ind_jd) indices_wobble.append(n) print("#serval_ind:" + str(len(indices_serval)), "#wobble_ind:" + str(len(indices_wobble))) #now set up all the data according to the indices ser_rvc = ser_rvc[indices_serval] ser_corr = ser_corr[indices_serval] w_dates = w_dates[indices_wobble] w_dates_utc = w_dates_utc[indices_wobble] w_RVs_original_barycorr = w_RVs_original_barycorr[ indices_wobble] + ser_corr w_RVs_er = w_RVs_er[indices_wobble] def fit_func(t, T0_offset): return rv.radial_velocity(t, orbital_parameters[0], orbital_parameters[1], orbital_parameters[2], orbital_parameters[3], orbital_parameters[4] + T0_offset) #fit to Wobble xdata = w_dates ydata = w_RVs_original_barycorr - np.nanmean(w_RVs_original_barycorr) popt, pcov = sp.optimize.curve_fit(fit_func, xdata, ydata, sigma=w_RVs_er, absolute_sigma=True) print("T0_offset Wobble = ", popt) T0_offset = popt[0] #make these weighted (maybe: thsi may not be a good idea if residuals are not strongly correlated to error (as with wobble results)) sigma_wob = np.nanstd( sigma_clip(w_RVs_original_barycorr - np.nanmean(w_RVs_original_barycorr) - fit_func(w_dates, T0_offset), sigma=5)) sigma_list[f] = sigma_wob sigma_wob_noclip = np.nanstd(w_RVs_original_barycorr - np.nanmean(w_RVs_original_barycorr) - fit_func(w_dates, T0_offset)) if plots: #fit to serval: xdata = ser_rvc[:, 0] ydata = ser_rvc[:, 1] - np.nanmean(ser_rvc[:, 1]) popt_s, pcov_s = sp.optimize.curve_fit(fit_func, xdata, ydata, sigma=ser_rvc[:, 2], absolute_sigma=True) print("T0_offset Serval = ", popt_s) T0_offset_s = popt_s[0] sigma_ser = np.nanstd( sigma_clip(ser_rvc[:, 1] - np.nanmean(ser_rvc[:, 1]) - fit_func(ser_rvc[:, 0], T0_offset_s), sigma=5)) sigma_ser_noclip = np.nanstd(ser_rvc[:, 1] - np.nanmean(ser_rvc[:, 1]) - fit_func(ser_rvc[:, 0], T0_offset_s)) xlst = np.linspace(w_dates[0], w_dates[0] + orbital_parameters[1] * 0.99999, num=100) ylst = [ rv.radial_velocity(t, orbital_parameters[0], orbital_parameters[1], orbital_parameters[2], orbital_parameters[3], orbital_parameters[4] + T0_offset) for t in xlst ] #sort by xlst pltlst = [[xlst[j], ylst[j]] for j in range(len(xlst))] def mod_sort(elem): return elem[0] % orbital_parameters[1] pltlst = sorted(pltlst, key=mod_sort) pltlst = np.asarray(pltlst) pltlst = [pltlst[:, 0], pltlst[:, 1]] ax1.plot(pltlst[0] % orbital_parameters[1], pltlst[1], "r-", label="literature orbit (Wobble T0_offset)") ax1.errorbar( (w_dates) % orbital_parameters[1], (w_RVs_original_barycorr - np.nanmean(w_RVs_original_barycorr)), yerr=w_RVs_er, fmt="x", label="Wobble_Corr, clipped_sigma = {0:.3f}, noclip = {1:.3f} " .format(sigma_wob, sigma_wob_noclip)) ax1.errorbar( (ser_rvc[:, 0]) % orbital_parameters[1], ser_rvc[:, 1] - np.nanmean(ser_rvc[:, 1]), yerr=ser_rvc[:, 2], fmt="x", label="SERVAL_Corr, clipped_sigma = {0:.3f}, noclip = {1:.3f}". format(sigma_ser, sigma_ser_noclip), color="C2") ax1.set_ylabel("RVs [m/s]") ax1.set_xlabel('jd') # add the parameter change to the title title_pre = os.path.split(os.path.split(fil)[0])[1] plt.title(title_pre + ", Phased (" + str(orbital_parameters[1]) + "d) RVs for " + objects[0] + " (" + objects[2] + ") " + " - " + objects[1] + ";") plt.grid(True) plt.tight_layout() plt.legend(shadow=True) plt.savefig(pp, format='pdf') plt.clf() fig.clf() ax1 = plt.gca() if plots: # include some nice progress plots. TODO make it not crudely placed inside this function? plt.close(fig) pp.close() best_index = np.argmin(sigma_list) return parameter_change_list[best_index]
ser_residuals = np.absolute(ser_residuals_sign) sigma_ser = np.nanstd(sigma_clip( ser_residuals_sign ,sigma = 5) ) sigma_ser_no_clip = np.nanstd( ser_residuals_sign) #residual vs error clipping ind_bad_ser = np.where(ser_residuals > error_clipping_kappa * ser_rvc[:,2])[0] ind_good_ser = np.where(ser_residuals <= error_clipping_kappa * ser_rvc[:,2])[0] sigma_ser_error_clipped = np.nanstd(ser_residuals_sign[ind_good_ser]) xlst = np.linspace(w_dates[0], w_dates[0] + orbital_parameters[1]*0.99999, num=100) ylst = [rv.radial_velocity(t , orbital_parameters[0], orbital_parameters[1], orbital_parameters[2],orbital_parameters[3], orbital_parameters[4] + T0_offset) for t in xlst] #sort by xlst pltlst = [[xlst[j],ylst[j]] for j in range(len(xlst))] def mod_sort(elem): return elem[0] % orbital_parameters[1] pltlst = sorted(pltlst, key = mod_sort) pltlst = np.asarray(pltlst) pltlst = [pltlst[:,0],pltlst[:,1]] #to_print = pltlst[0] % i[3] #print(to_print) ax1.plot(pltlst[0] % i[3], pltlst[1], "r-", label = "literature orbit (Wobble T0_offset)") #ax1.plot(w_dates % i[3], (w_RVs_original+w_bervs-np.nanmean(w_RVs_original+w_bervs)),"x", label="Wobble_orig") ax1.errorbar((w_dates) % i[3], (w_RVs_original_barycorr-np.nanmean(w_RVs_original_barycorr)), yerr = w_RVs_er,fmt = "x", label="Wobble_Corr, no_clip ={0:.3f},clipped_sigma = {1:.3f}, err_clipped = {2:.3f} ".format(sigma_wob_no_clip, sigma_wob, sigma_wob_error_clipped)) ax1.plot((ser_rvc[:,0]) % i[3], ser_rvc[:,5] - np.nanmean(ser_rvc[:,5]), "x", label= "SERVAL") ax1.errorbar((ser_rvc[:,0]) % i[3], ser_rvc[:,1] - np.nanmean(ser_rvc[:,1]),yerr = ser_rvc[:,2] ,fmt = "x", label= "SERVAL_Corr, no_clip ={0:.3f}, clipped_sigma = {1:.3f}, err_clipped = {2:.3f}".format(sigma_ser_no_clip, sigma_ser, sigma_ser_error_clipped))