Esempio n. 1
0
 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)
Esempio n. 2
0
 def fit(self):
     shutil.copy(self.fit_dir + "/lc.csv", self.ttvs_dir + "/lc.csv")
     shutil.copy(self.fit_dir + "/params.csv", self.ttvs_dir + "/params.csv")
     shutil.copy(self.fit_dir + "/params_star.csv", self.ttvs_dir + "/params_star.csv")
     shutil.copy(self.fit_dir + "/settings.csv", self.ttvs_dir + "/settings.csv")
     self._tune_settings()
     self._fix_time_params()
     self._prepare_ttv_params()
     allesfitter.show_initial_guess(ttvs_dir)
     if not args.only_initial:
         logging.info("Running dynamic nested sampling")
         allesfitter.ns_fit(ttvs_dir)
         allesfitter.ns_output(ttvs_dir)
Esempio n. 3
0
Cambridge, MA 02109, 
USA
Email: [email protected]
"""

import allesfitter
from allesfitter import ns_plot_bayes_factors, ns_plot_violins
'''
uncomment whatever you want to run
'''

###############################################################################
#::: run the fit
###############################################################################
#allesfitter.ns_fit('allesfit_flares')
allesfitter.ns_output('allesfit_flares')

################################################################################
##::: plot bayes factors
################################################################################
#datadirs = [ 'allesfit_TMNT', 'allesfit_TMNT_TTVs']
#labels = [ 'No TTVs', 'TTVs' ]
#
#collection_of_run_names = (datadirs)
#collection_of_labels = (labels)
#
#fig, ax = ns_plot_bayes_factors(collection_of_run_names, collection_of_labels)
#fig.savefig('pub/bayes_factors.pdf', bbox_inches='tight')
#
#
#
Esempio n. 4
0
 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")
Esempio n. 5
0
Massachusetts Institute of Technology,
77 Massachusetts Avenue,
Cambridge, MA 02109, 
USA
Email: [email protected]
"""

import allesfitter
reload(allesfitter)
'''
uncomment whichever model you want to run
'''

#allesfitter.ns_fit('allesfit_Leonardo_unif')
#allesfitter.ns_output('allesfit_Leonardo_unif')

#allesfitter.ns_fit('allesfit_Leonardo_rwalk')
#allesfitter.ns_output('allesfit_Leonardo_rwalk')

#allesfitter.ns_fit('allesfit_all_TMNT_unif')
#allesfitter.ns_output('allesfit_all_TMNT_unif')

#allesfitter.ns_fit('allesfit_all_TMNT_rwalk')
#allesfitter.ns_output('allesfit_all_TMNT_rwalk')

#allesfitter.ns_fit('allesfit_all_TMNT_rslice')
#allesfitter.ns_output('allesfit_all_TMNT_rslice')

allesfitter.ns_fit('allesfit_all_TMNT_hslice')
allesfitter.ns_output('allesfit_all_TMNT_hslice')
Esempio n. 6
0
#!/usr/bin/env python3
# -*- 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')

allesfitter.show_initial_guess('allesfit')
allesfitter.ns_fit('allesfit')
allesfitter.ns_output('allesfit')
Esempio n. 7
0
#!/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

#::: lienar ephemerides fit
# allesfitter.show_initial_guess('allesfit_linear_ephemerides')
# allesfitter.ns_fit('allesfit_linear_ephemerides')
# allesfitter.ns_output('allesfit_linear_ephemerides')

#::: TTV fit
# allesfitter.prepare_ttv_fit('allesfit_with_ttvs')
# allesfitter.show_initial_guess('allesfit_with_ttvs')
# allesfitter.ns_fit('allesfit_with_ttvs')
allesfitter.ns_output('allesfit_with_ttvs')
Esempio n. 8
0
#!/usr/bin/env python3
# -*- 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]
"""


import allesfitter

allesfitter.ns_fit('allesfit_all_TMNT')
allesfitter.ns_output('allesfit_all_TMNT')
Esempio n. 9
0
from lcbuilder.eleanor import TargetData
from sherlockpipe import constants as const
from sherlockpipe import tpfplotter
from sherlockpipe.nbodies.megno import MegnoStabilityCalculator
from sherlockpipe.nbodies.stability_calculator import StabilityCalculator
from sherlockpipe.sherlock import Sherlock

# sherlock = Sherlock(None)
# sherlock = sherlock.filter_multiplanet_ois()
# sherlock.ois.to_csv("multiplanet_ois.csv")

#Allesfitter stuff
alles = allesfitter.allesclass("/mnt/0242522242521AAD/dev/workspaces/git_repositories/sherlockpipe/TIC305048087_[2]_bck/fit_2/")
alles.posterior_samples("lc", "SOI_2_period")
allesfitter.ns_output("/mnt/0242522242521AAD/dev/workspaces/git_repositories/sherlockpipe/run_tests/analysis/TIC142748283_all/fit_0/ttvs_0")
results = pickle.load(open('/mnt/0242522242521AAD/dev/workspaces/git_repositories/sherlockpipe/run_tests/analysis/dietrich/TIC467179528_all/fit_2/results/ns_derived_samples.pickle', 'rb'))
logging.info(results)

#Stability plots
# stability_dir = "/mnt/0242522242521AAD/dev/workspaces/git_repositories/sherlockpipe/run_tests/analysis/dietrich/TIC467179528_all/fit_2/stability_0/"
# df = pandas.read_csv(stability_dir + "stability_megno.csv")
# df = df[(df["megno"] < 3)]
# stability_calc = MegnoStabilityCalculator(5e2)
# for key, row in df.iterrows():
#     stability_calc.plot_simulation(row, stability_dir, str(key))

# filename = "/home/martin/Downloads/resimplepulsations/P2.5_R1.28_1354.197892531283.csv"
# df = pandas.read_csv(filename, float_precision='round_trip', sep=',',
#                                      usecols=['#time', 'flux', 'flux_err'])
# df["flux"] = df["flux"] + 1
Esempio n. 10
0
##############################################################################
#::: fit a cicular model
##############################################################################

# allesfitter.show_initial_guess('allesfit_circular_model')
# allesfitter.mcmc_fit('allesfit_circular_model')
# allesfitter.mcmc_output('allesfit_circular_model')

# allesfitter.show_initial_guess('allesfit_circular_model')
# allesfitter.ns_fit('allesfit_circular_model')
# allesfitter.ns_output('allesfit_circular_model')

##############################################################################
#::: fit an eccentric model
##############################################################################

# allesfitter.show_initial_guess('allesfit_eccentric_model')
# allesfitter.mcmc_fit('allesfit_eccentric_model')
# allesfitter.mcmc_output('allesfit_eccentric_model')

allesfitter.show_initial_guess('allesfit_eccentric_model')
allesfitter.ns_fit('allesfit_eccentric_model')
allesfitter.ns_output('allesfit_eccentric_model')

##############################################################################
#::: compare Bayesian evidence
##############################################################################

# fig, ax = allesfitter.ns_plot_bayes_factors(['allesfit_circular_model','allesfit_eccentric_model'])
# fig.savefig('Bayesian_evidence.pdf', bbox_inches='tight')
Esempio n. 11
0
 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")