def fit(self, candidate_df, star_df, cpus, allesfit_dir): candidate_row = candidate_df.iloc[0] sherlock_star_file = self.object_dir + "/params_star.csv" star_file = allesfit_dir + "/params_star.csv" params_file = allesfit_dir + "/params.csv" settings_file = allesfit_dir + "/settings.csv" if candidate_row["number"] is None or np.isnan(candidate_row["number"]): lc_file = "/" + candidate_row["lc"] else: lc_file = "/" + str(candidate_row["number"]) + "/lc_" + str(candidate_row["curve"]) + ".csv" shutil.copyfile(self.object_dir + lc_file, allesfit_dir + "/lc.csv") if os.path.exists(sherlock_star_file) and os.path.isfile(sherlock_star_file): shutil.copyfile(sherlock_star_file, star_file) shutil.copyfile(resources_dir + "/resources/allesfitter/params.csv", params_file) shutil.copyfile(resources_dir + "/resources/allesfitter/settings.csv", settings_file) # TODO replace sherlock properties from allesfitter files # TODO only use params_star when the star mass or radius was not assumed with open(settings_file, 'r+') as f: text = f.read() text = re.sub('\\${sherlock:cores}', str(cpus), text) text = re.sub('\\${sherlock:name}', str(candidate_row["name"]), text) detrend_param = "baseline_flux_lc,hybrid_offset" detrend_param = detrend_param if self.detrend else "#" + detrend_param text = re.sub('\\${sherlock:detrend}', detrend_param, text) f.seek(0) f.write(text) f.truncate() with open(params_file, 'r+') as f: text = f.read() text = re.sub('\\${sherlock:t0}', str(candidate_row["t0"]), text) text = re.sub('\\${sherlock:period}', str(candidate_row["period"]), text) rp_rs = candidate_row["rp_rs"] if candidate_row["rp_rs"] != "-" else 0.1 text = re.sub('\\${sherlock:rp_rs}', str(rp_rs), text) sum_rp_rs_a = (candidate_row["rp_rs"] + star_df.iloc[0]['R_star']) / candidate_row["a"] * 0.00465047 \ if candidate_row["rp_rs"] != "-" else 0.2 text = re.sub('\\${sherlock:sum_rp_rs_a}', str(sum_rp_rs_a), text) text = re.sub('\\${sherlock:name}', str(candidate_row["name"]), text) if os.path.exists(sherlock_star_file) and os.path.isfile(sherlock_star_file): text = re.sub('\\${sherlock:ld_a}', str(star_df.iloc[0]["ld_a"]) + ",0", text) text = re.sub('\\${sherlock:ld_b}', str(star_df.iloc[0]["ld_b"]) + ",0", text) else: text = re.sub('\\${sherlock:ld_a}', "0.5,1", text) text = re.sub('\\${sherlock:ld_b}', "0.5,1", text) f.seek(0) f.write(text) f.truncate() allesfitter.show_initial_guess(allesfit_dir) if not self.only_initial and not self.mcmc: allesfitter.ns_fit(allesfit_dir) allesfitter.ns_output(allesfit_dir) elif not self.only_initial and self.mcmc: allesfitter.mcmc_fit(allesfit_dir) allesfitter.mcmc_output(allesfit_dir)
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Wed Sep 12 15:57:05 2018 @author: Maximilian N. Günther MIT Kavli Institute for Astrophysics and Space Research, Massachusetts Institute of Technology, 77 Massachusetts Avenue, Cambridge, MA 02109, USA Email: [email protected] Web: www.mnguenther.com """ import allesfitter allesfitter.show_initial_guess('allesfit') allesfitter.mcmc_fit('allesfit') allesfitter.mcmc_output('allesfit')
def fit(self, candidate_df, star_df, cpus, allesfit_dir, tolerance, fit_orbit): """ Main method to run the allesfitter fit. @param candidate_df: the candidates dataframe. @param star_df: the star dataframe. @param cpus: the number of cpus to be used. @param allesfit_dir: the directory for the fit to be run. @param tolerance: the nested sampling tolerance threshold. @param fit_orbit: whether to fit eccentricity and arg. of periastron. """ logging.info("Preparing fit files") sherlock_star_file = self.object_dir + "/params_star.csv" star_file = allesfit_dir + "/params_star.csv" params_file = allesfit_dir + "/params.csv" settings_file = allesfit_dir + "/settings.csv" lc_file = Fitter.select_lc_file(candidate_df) shutil.copyfile(self.object_dir + lc_file, allesfit_dir + "/lc.csv") if os.path.exists(sherlock_star_file) and os.path.isfile( sherlock_star_file): shutil.copyfile(sherlock_star_file, star_file) shutil.copyfile(resources_dir + "/resources/allesfitter/settings.csv", settings_file) fit_width = Fitter.select_fit_width(candidate_df) # TODO replace sherlock properties from allesfitter files # TODO only use params_star when the star mass or radius was not assumed logging.info("Preparing fit properties") with open(settings_file, 'r+') as f: text = f.read() text = re.sub('\\${sherlock:cores}', str(cpus), text) text = re.sub('\\${sherlock:fit_width}', str(fit_width), text) text = re.sub('\\${sherlock:fit_ttvs}', "False", text) text = re.sub('\\${sherlock:names}', ' '.join(candidate_df["name"].astype('str')), text) text = re.sub('\\${sherlock:tolerance}', str(tolerance), text) if self.detrend == 'hybrid_spline': detrend_param = "baseline_flux_lc,hybrid_spline" elif self.detrend == 'gp': detrend_param = 'baseline_flux_lc,sample_GP_Matern32' else: detrend_param = '' text = re.sub('\\${sherlock:detrend}', detrend_param, text) f.seek(0) f.write(text) f.truncate() with open(params_file, 'w') as f: f.write( Fitter.fill_candidates_params(candidate_df, star_df, fit_orbit, self.detrend)) f.truncate() logging.info("Running initial guess") try: allesfitter.show_initial_guess(allesfit_dir) except Exception as e: logging.exception(str(e)) # TODO fix custom_plot for all candidates # Fitter.custom_plot(candidate_row["name"], candidate_row["period"], fit_width, allesfit_dir, "initial_guess") if not self.only_initial: logging.info("Preparing bayesian fit") if not self.mcmc: logging.info("Running dynamic nested sampling") try: allesfitter.ns_fit(allesfit_dir) allesfitter.ns_output(allesfit_dir) except Exception as e: logging.exception(str(e)) elif self.mcmc: logging.info("Running MCMC") try: allesfitter.mcmc_fit(allesfit_dir) allesfitter.mcmc_output(allesfit_dir) except Exception as e: logging.exception(str(e)) logging.info("Generating custom plots")
""" import numpy as np import matplotlib.pyplot as plt from astropy import constants as c import allesfitter from allesfitter.v2.translator import translate ############################################################################### #::: calculate prior on q (mass ratio) via Stassun+2017 ############################################################################### #Mp = np.random.normal(loc=(11.44*c.M_jup/c.M_sun).value, scale=(1.51*c.M_jup/c.M_sun).value, size=10000) #Ms = np.random.normal(loc=1.46, scale=0.29, size=10000) #q = Mp / Ms #print( np.percentile(q, [16,50,84] ) ) ############################################################################### #::: translate S19 to allesfitter ############################################################################### # translate({'a/R_host':3.562, 'R_companion/R_host':0.09716}) ############################################################################### #::: run the fit ############################################################################### # allesfitter.show_initial_guess('allesfit_sine_physical') # allesfitter.mcmc_fit('allesfit_sine_physical') # allesfitter.mcmc_output('allesfit_sine_physical') # allesfitter.show_initial_guess('allesfit_sine_series') allesfitter.mcmc_fit('allesfit_sine_series') allesfitter.mcmc_output('allesfit_sine_series')
def fit(self, candidate_df, star_df, cpus, allesfit_dir): candidate_row = candidate_df.iloc[0] sherlock_star_file = self.object_dir + "/params_star.csv" star_file = allesfit_dir + "/params_star.csv" params_file = allesfit_dir + "/params.csv" settings_file = allesfit_dir + "/settings.csv" if candidate_row["number"] is None or np.isnan(candidate_row["number"]): lc_file = "/" + candidate_row["lc"] else: lc_file = "/" + str(candidate_row["number"]) + "/lc_" + str(candidate_row["curve"]) + ".csv" shutil.copyfile(self.object_dir + lc_file, allesfit_dir + "/lc.csv") if os.path.exists(sherlock_star_file) and os.path.isfile(sherlock_star_file): shutil.copyfile(sherlock_star_file, star_file) shutil.copyfile(resources_dir + "/resources/allesfitter/params.csv", params_file) shutil.copyfile(resources_dir + "/resources/allesfitter/settings.csv", settings_file) fit_width = 0.3333333 if candidate_row["duration"] is not None: fit_width = float(candidate_row["duration"]) / 60 / 24 * 7 # TODO replace sherlock properties from allesfitter files # TODO only use params_star when the star mass or radius was not assumed with open(settings_file, 'r+') as f: text = f.read() text = re.sub('\\${sherlock:cores}', str(cpus), text) text = re.sub('\\${sherlock:fit_width}', str(fit_width), text) text = re.sub('\\${sherlock:name}', str(candidate_row["name"]), text) detrend_param = "baseline_flux_lc,hybrid_offset" detrend_param = detrend_param if self.detrend else "#" + detrend_param text = re.sub('\\${sherlock:detrend}', detrend_param, text) f.seek(0) f.write(text) f.truncate() with open(params_file, 'r+') as f: text = f.read() text = re.sub('\\${sherlock:t0}', str(candidate_row["t0"]), text) text = re.sub('\\${sherlock:t0_min}', str(candidate_row["t0"] - 0.02), text) text = re.sub('\\${sherlock:t0_max}', str(candidate_row["t0"] + 0.02), text) text = re.sub('\\${sherlock:period}', str(candidate_row["period"]), text) text = re.sub('\\${sherlock:period_min}', str(candidate_row["period"] - candidate_row["per_err"]), text) text = re.sub('\\${sherlock:period_max}', str(candidate_row["period"] + candidate_row["per_err"]), text) rp_rs = candidate_row["rp_rs"] if candidate_row["rp_rs"] != "-" else 0.1 depth = candidate_row["depth"] / 1000 depth_err = depth * 0.2 rp_rs_err = 0.5 / math.sqrt(depth) * depth_err text = re.sub('\\${sherlock:rp_rs}', str(rp_rs), text) # TODO calculate depth error in SHERLOCK maybe given the std deviation from the depths or even using the residuals rp_rs_min = rp_rs - rp_rs_err rp_rs_min = rp_rs_min if rp_rs_min > 0 else 0.0000001 text = re.sub('\\${sherlock:rp_rs_min}', str(rp_rs_min), text) text = re.sub('\\${sherlock:rp_rs_max}', str(rp_rs + rp_rs_err), text) sum_rp_rs_a = (candidate_row["rp_rs"] + star_df.iloc[0]['R_star']) / candidate_row["a"] * 0.00465047 \ if candidate_row["rp_rs"] != "-" else 0.2 rp_err_min = depth ** 0.5 * star_df.iloc[0]["R_star_lerr"] + star_df.iloc[0]["R_star"] / 2 * depth ** (- 0.5) rp_err_max = depth ** 0.5 * star_df.iloc[0]["R_star_uerr"] + star_df.iloc[0]["R_star"] / 2 * depth ** (- 0.5) constant = (6.674e-11 / 4 / (math.pi ** 2)) ** (1 / 3) / 1.48e11 mstar = star_df.iloc[0]["M_star"] * 2e30 mstar_low_err = star_df.iloc[0]["M_star_lerr"] * 2e30 mstar_up_err = star_df.iloc[0]["M_star_uerr"] * 2e30 per = candidate_row["period"] * 24 * 3600 per_err = candidate_row["per_err"] * 24 * 3600 a_err_min = constant * ((mstar ** (1/3)) * 2 / 3 * (per ** (-2/3)) * per_err + per ** (2/3) / 3 * (mstar ** (-2/3)) * mstar_low_err) a_err_max = constant * ((mstar ** (1/3)) * 2 / 3 * (per ** (-2/3)) * per_err + per ** (2/3) / 3 * (mstar ** (-2/3)) * mstar_up_err) a_err_min_rads = a_err_min * 215 a_err_max_rads = a_err_max * 215 a_rads = candidate_row["a"] * 215 radp_rads = candidate_row["rad_p"] / 0.0091577 sum_rp_rs_a_min_err = 1 / a_rads * rp_err_min + 1 / a_rads * star_df.iloc[0]["R_star_lerr"] + \ (radp_rads + star_df.iloc[0]["R_star"]) / (a_rads ** 2) * a_err_min_rads sum_rp_rs_a_max_err = 1 / a_rads * rp_err_max + 1 / a_rads * star_df.iloc[0]["R_star_uerr"] + \ (radp_rads + star_df.iloc[0]["R_star"]) / (a_rads ** 2) * a_err_max_rads sum_rp_rs_a_min_err_aus = sum_rp_rs_a_min_err / 215 sum_rp_rs_a_max_err_aus = sum_rp_rs_a_max_err / 215 sum_rp_rs_a_min = sum_rp_rs_a - sum_rp_rs_a_min_err_aus sum_rp_rs_a_min = sum_rp_rs_a_min if sum_rp_rs_a_min > 0 else 0.0000001 sum_rp_rs_a_max = sum_rp_rs_a + sum_rp_rs_a_max_err_aus text = re.sub('\\${sherlock:sum_rp_rs_a}', str(sum_rp_rs_a), text) text = re.sub('\\${sherlock:sum_rp_rs_a_min}', str(sum_rp_rs_a_min), text) text = re.sub('\\${sherlock:sum_rp_rs_a_max}', str(sum_rp_rs_a_max), text) text = re.sub('\\${sherlock:name}', str(candidate_row["name"]), text) if os.path.exists(sherlock_star_file) and os.path.isfile(sherlock_star_file): text = re.sub('\\${sherlock:ld_a}', str(star_df.iloc[0]["ld_a"]) + ",0", text) text = re.sub('\\${sherlock:ld_b}', str(star_df.iloc[0]["ld_b"]) + ",0", text) else: text = re.sub('\\${sherlock:ld_a}', "0.5,1", text) text = re.sub('\\${sherlock:ld_b}', "0.5,1", text) f.seek(0) f.write(text) f.truncate() allesfitter.show_initial_guess(allesfit_dir) self.custom_plot(candidate_row["name"], candidate_row["period"], fit_width, allesfit_dir, "initial_guess") if not self.only_initial: if not self.mcmc: allesfitter.ns_fit(allesfit_dir) allesfitter.ns_output(allesfit_dir) elif self.mcmc: allesfitter.mcmc_fit(allesfit_dir) allesfitter.mcmc_output(allesfit_dir) self.custom_plot(candidate_row["name"], candidate_row["period"], fit_width, allesfit_dir, "posterior")