示例#1
0
def plots(args):
    """
    Generate plots

    Args:
        args (ArgumentParser): command line arguments
    """

    config_file = args.setupfn
    conf_base = os.path.basename(config_file).split('.')[0]
    statfile = os.path.join(
        args.outputdir, "{}_radvel.stat".format(conf_base)
    )

    status = load_status(statfile)

    P, post = radvel.utils.initialize_posterior(config_file,
                                                decorr=args.decorr)

    assert status.getboolean('fit', 'run'), \
        "Must perform max-liklihood fit before plotting"
    post = radvel.posterior.load(status.get('fit', 'postfile'))

    for ptype in args.type:
        print("Creating {} plot for {}".format(ptype, conf_base))

        if ptype == 'rv':
            args.plotkw['uparams'] = post.uparams
            args.plotkw['status'] = status
            if 'saveplot' not in args.plotkw:
                saveto = os.path.join(
                    args.outputdir, conf_base+'_rv_multipanel.pdf'
                )
            else:
                saveto = args.plotkw['saveplot']
                args.plotkw.pop('saveplot')
            P, _ = radvel.utils.initialize_posterior(config_file)
            if hasattr(P, 'bjd0'):
                args.plotkw['epoch'] = P.bjd0

            if args.gp:
                GPPlot = orbit_plots.GPMultipanelPlot(
                    post, saveplot=saveto, **args.plotkw
                )
                GPPlot.plot_multipanel()
            else:
                RVPlot = orbit_plots.MultipanelPlot(
                    post, saveplot=saveto, **args.plotkw
                )
                RVPlot.plot_multipanel()

                # check to make sure that Posterior is not GP, print warning if it is
                if isinstance(post.likelihood, radvel.likelihood.CompositeLikelihood):
                    like_list = post.likelihood.like_list
                else:
                    like_list = [post.likelihood]
                for like in like_list:
                    if isinstance(like, radvel.likelihood.GPLikelihood):
                        print("WARNING: GP Likelihood(s) detected. \
You may want to use the '--gp' flag when making these plots.")
                        break

        if ptype == 'corner' or ptype == 'auto' or ptype == 'trend':
            assert status.getboolean('mcmc', 'run'), \
                "Must run MCMC before making corner, auto, or trend plots"

            chains = pd.read_csv(status.get('mcmc', 'chainfile'))
            autocorr = pd.read_csv(status.get('mcmc', 'autocorrfile'))

        if ptype == 'auto':
            saveto = os.path.join(args.outputdir, conf_base+'_auto.pdf')
            Auto = mcmc_plots.AutoPlot(autocorr, saveplot=saveto)
            Auto.plot()

        if ptype == 'corner':
            saveto = os.path.join(args.outputdir, conf_base+'_corner.pdf')
            Corner = mcmc_plots.CornerPlot(post, chains, saveplot=saveto)
            Corner.plot()

        if ptype == 'trend':
            nwalkers = status.getint('mcmc', 'nwalkers')
            nensembles = status.getint('mcmc', 'nensembles')

            saveto = os.path.join(args.outputdir, conf_base+'_trends.pdf')
            Trend = mcmc_plots.TrendPlot(post, chains, nwalkers, nensembles, saveto)
            Trend.plot()

        if ptype == 'derived':
            assert status.has_section('derive'), \
                "Must run `radvel derive` before plotting derived parameters"

            P, _ = radvel.utils.initialize_posterior(config_file)
            chains = pd.read_csv(status.get('derive', 'chainfile'))
            saveto = os.path.join(
                args.outputdir, conf_base+'_corner_derived_pars.pdf'
            )

            Derived = mcmc_plots.DerivedPlot(chains, P, saveplot=saveto)
            Derived.plot()

        savestate = {'{}_plot'.format(ptype): os.path.relpath(saveto)}
        save_status(statfile, 'plot', savestate)
示例#2
0
    def run_search(self,
                   fixed_threshold=None,
                   outdir=None,
                   mkoutdir=True,
                   running=True):
        """Run an iterative search for planets not given in posterior.

        Args:
            fixed_threshold (float): (optional) use a fixed delta BIC threshold
            mkoutdir (bool): create the output directory?
        """
        if outdir is None:
            outdir = os.path.join(os.getcwd(), self.starname)
        if mkoutdir and not os.path.exists(outdir):
            os.mkdir(outdir)

        if self.trend:
            self.trend_test()

        run = True
        while run:
            if self.num_planets != 0:
                self.add_planet()

            perioder = periodogram.Periodogram(self.post,
                                               basebic=self.basebic,
                                               minsearchp=self.min_per,
                                               maxsearchp=self.max_per,
                                               fap=self.fap,
                                               manual_grid=self.manual_grid,
                                               oversampling=self.oversampling,
                                               baseline=self.baseline,
                                               eccentric=self.eccentric,
                                               workers=self.workers,
                                               verbose=self.verbose)
            # Run the periodogram, store arrays and threshold (if computed).
            perioder.per_bic()
            self.periodograms[self.num_planets] = perioder.power[self.crit]
            if self.num_planets == 0 or self.pers is None:
                self.pers = perioder.pers

            if fixed_threshold is None:
                perioder.eFAP()
                self.eFAPs[self.num_planets] = perioder.fap_min
            else:
                perioder.bic_thresh = fixed_threshold
            self.bic_threshes[self.num_planets] = perioder.bic_thresh
            self.best_bics[self.num_planets] = perioder.best_bic

            if self.save_outputs:
                perioder.plot_per()
                perioder.fig.savefig(outdir +
                                     '/dbic{}.pdf'.format(self.num_planets +
                                                          1))

            # Check whether there is a detection. If so, fit free and proceed.
            if perioder.best_bic > perioder.bic_thresh:
                self.num_planets += 1
                for k in self.post.params.keys():
                    self.post.params[k].value = perioder.bestfit_params[k]

                # Generalize tc reset to each new discovery.
                tckey = 'tc{}'.format(self.num_planets)
                if self.post.params[tckey].value < np.amin(self.data.time):
                    self.post.params[tckey].value = np.median(self.data.time)
                    for n in np.arange(1, self.num_planets + 1):
                        self.post.params['k{}'.format(n)].vary = False
                        self.post.params['per{}'.format(n)].vary = False
                        self.post.params['secosw{}'.format(n)].vary = False
                        self.post.params['secosw{}'.format(n)].vary = False
                        if n != self.num_planets:
                            self.post.params['tc{}'.format(n)].vary = False

                    self.post = radvel.fitting.maxlike_fitting(self.post,
                                                               verbose=False)

                    for n in np.arange(1, self.num_planets + 1):
                        self.post.params['k{}'.format(n)].vary = True
                        self.post.params['per{}'.format(n)].vary = True
                        self.post.params['secosw{}'.format(n)].vary = True
                        self.post.params['secosw{}'.format(n)].vary = True
                        self.post.params['tc{}'.format(n)].vary = True

                self.fit_orbit()
                self.all_params.append(self.post.params)
                self.basebic = self.post.likelihood.bic()
            else:
                self.sub_planet()
                # 8/3: Update the basebic anyway, for injections.
                self.basebic = self.post.likelihood.bic()
                run = False
            if self.num_planets >= self.max_planets:
                run = False

            # If any jitter values are negative, flip them.
            for key in self.post.params.keys():
                if 'jit' in key:
                    if self.post.params[key].value < 0:
                        self.post.params[
                            key].value = -self.post.params[key].value

            # Generate an orbit plot.
            if self.save_outputs:
                rvplot = orbit_plots.MultipanelPlot(
                    self.post,
                    saveplot=outdir +
                    '/orbit_plot{}.pdf'.format(self.num_planets))
                multiplot_fig, ax_list = rvplot.plot_multipanel()
                multiplot_fig.savefig(
                    outdir + '/orbit_plot{}.pdf'.format(self.num_planets))

        # Generate running periodograms.
        if running:
            self.running_per()

        # Run MCMC on final posterior, save new parameters and uncertainties.
        if self.mcmc == True and (self.num_planets != 0
                                  or self.post.params['dvdt'].vary == True):
            self.post.uparams = {}
            self.post.medparams = {}
            self.post.maxparams = {}
            # Use recommended parameters for mcmc.
            nensembles = np.min([self.workers, 16])
            if os.cpu_count() < nensembles:
                nensembles = os.cpu_count()
            # Set custom mcmc scales for e/w parameters.
            for n in np.arange(1, self.num_planets + 1):
                self.post.params['secosw{}'.format(n)].mcmcscale = 0.005
                self.post.params['sesinw{}'.format(n)].mcmcscale = 0.005

            # Sample in log-period space.
            logpost = copy.deepcopy(self.post)
            logparams = logpost.params.basis.to_any_basis(
                logpost.params, 'logper tc secosw sesinw k')
            logpost = utils.initialize_post(self.data, params=logparams)

            # Run MCMC. #self.post #logpost
            chains = radvel.mcmc(logpost,
                                 nwalkers=50,
                                 nrun=25000,
                                 burnGR=1.03,
                                 maxGR=1.0075,
                                 minTz=2000,
                                 minAfactor=15,
                                 maxArchange=0.07,
                                 burnAfactor=15,
                                 minsteps=12500,
                                 minpercent=50,
                                 thin=5,
                                 save=False,
                                 ensembles=nensembles)

            # Convert chains to per, e, w basis.
            synthchains = logpost.params.basis.to_synth(chains)
            synthquants = synthchains.quantile([0.159, 0.5, 0.841])
            logpost = None

            # Compress, thin, and save chains, in fitting and synthetic bases.
            csvfn = outdir + '/chains.csv.tar.bz2'
            synthchains.to_csv(csvfn, compression='bz2')

            # Retrieve e and w medians & uncertainties from synthetic chains.
            for n in np.arange(1, self.num_planets + 1):
                e_key = 'e{}'.format(n)
                w_key = 'w{}'.format(n)
                # Add period if it's a synthetic parameter.
                per_key = 'per{}'.format(n)
                logper_key = 'logper{}'.format(n)

                med_e = synthquants[e_key][0.5]
                high_e = synthquants[e_key][0.841] - med_e
                low_e = med_e - synthquants[e_key][0.159]
                err_e = np.mean([high_e, low_e])
                err_e = radvel.utils.round_sig(err_e)
                med_e, err_e, errhigh_e = radvel.utils.sigfig(med_e, err_e)
                max_e, err_e, errhigh_e = radvel.utils.sigfig(
                    self.post.params[e_key].value, err_e)

                med_w = synthquants[w_key][0.5]
                high_w = synthquants[w_key][0.841] - med_w
                low_w = med_w - synthquants[w_key][0.159]
                err_w = np.mean([high_w, low_w])
                err_w = radvel.utils.round_sig(err_w)
                med_w, err_w, errhigh_w = radvel.utils.sigfig(med_w, err_w)
                max_w, err_w, errhigh_w = radvel.utils.sigfig(
                    self.post.params[w_key].value, err_w)

                self.post.uparams[e_key] = err_e
                self.post.uparams[w_key] = err_w
                self.post.medparams[e_key] = med_e
                self.post.medparams[w_key] = med_w
                self.post.maxparams[e_key] = max_e
                self.post.maxparams[w_key] = max_w

            # Retrieve medians & uncertainties for the fitting basis parameters.
            for par in self.post.params.keys():
                if self.post.params[par].vary:
                    med = synthquants[par][0.5]
                    high = synthquants[par][0.841] - med
                    low = med - synthquants[par][0.159]
                    err = np.mean([high, low])
                    err = radvel.utils.round_sig(err)
                    med, err, errhigh = radvel.utils.sigfig(med, err)
                    max, err, errhigh = radvel.utils.sigfig(
                        self.post.params[par].value, err)

                    self.post.uparams[par] = err
                    self.post.medparams[par] = med
                    self.post.maxparams[par] = max

            # Add uncertainties on derived parameters, if mass is provided.
            if self.mstar is not None:
                self.post = utils.derive(self.post, synthchains, self.mstar,
                                         self.mstar_err)

            if self.save_outputs:
                # Generate a corner plot, sans nuisance parameters.
                labels = []
                for n in np.arange(1, self.num_planets + 1):
                    labels.append('per{}'.format(n))
                    labels.append('tc{}'.format(n))
                    labels.append('k{}'.format(n))
                    labels.append('secosw{}'.format(n))
                    labels.append('sesinw{}'.format(n))
                if self.post.params['dvdt'].vary == True:
                    labels.append('dvdt')
                if self.post.params['curv'].vary == True:
                    labels.append('curv')
                texlabels = [
                    self.post.params.tex_labels().get(l, l) for l in labels
                ]

                plot = corner.corner(synthchains[labels],
                                     labels=texlabels,
                                     label_kwargs={"fontsize": 14},
                                     plot_datapoints=False,
                                     bins=30,
                                     quantiles=[0.16, 0.5, 0.84],
                                     title_kwargs={"fontsize": 14},
                                     show_titles=True,
                                     smooth=True)
                pl.savefig(outdir +
                           '/{}_corner_plot.pdf'.format(self.starname))

                # Generate an orbit plot wth median parameters and uncertainties.
                rvplot = orbit_plots.MultipanelPlot(
                    self.post,
                    saveplot=outdir +
                    '/orbit_plot_mc_{}.pdf'.format(self.starname),
                    uparams=self.post.uparams)
                multiplot_fig, ax_list = rvplot.plot_multipanel()
                multiplot_fig.savefig(
                    outdir + '/orbit_plot_mc_{}.pdf'.format(self.starname))

        if self.save_outputs:
            self.save(filename=outdir + '/post_final.pkl')
            pickle_out = open(outdir + '/search.pkl', 'wb')
            pickle.dump(self, pickle_out)
            pickle_out.close()

            periodograms_plus_pers = np.append([self.pers],
                                               list(
                                                   self.periodograms.values()),
                                               axis=0).T
            np.savetxt(outdir + '/pers_periodograms.csv',
                       periodograms_plus_pers,
                       header='period  BIC_array')

            threshs_bics_faps = np.append(
                [list(self.bic_threshes.values())],
                [list(self.best_bics.values()),
                 list(self.eFAPs.values())],
                axis=0).T

            np.savetxt(outdir + '/thresholds_bics_faps.csv',
                       threshs_bics_faps,
                       header='threshold  best_bic  fap')
示例#3
0
def _hack_radvel_plots(args):
    """
    Hacked version of radvel.driver.plots

    Args:
        args (ArgumentParser): command line arguments
    """

    config_file = args.setupfn
    conf_base = os.path.basename(config_file).split('.')[0]
    statfile = os.path.join(args.inputdir, "{}_radvel.stat".format(conf_base))

    status = load_status(statfile)

    assert status.getboolean('fit', 'run'), \
        "Must perform max-liklihood fit before plotting"
    post = radvel.posterior.load(status.get('fit', 'postfile'))

    # update the plotted posterior to match the median best-fit parameters.
    summarycsv = "../results/rv_fitting/t10onlytwo_LGB_20190228_fix_gammaddot/WASP4_post_summary.csv"
    sdf = pd.read_csv(summarycsv)
    for param in [c for c in sdf.columns if 'Unnamed' not in c]:
        post.params[param] = radvel.Parameter(value=sdf.ix[1][param])

    for ptype in args.type:
        print("Creating {} plot for {}".format(ptype, conf_base))

        if ptype == 'rv':
            args.plotkw['uparams'] = post.uparams
            saveto = os.path.join(args.outputdir,
                                  conf_base + '_rv_multipanel.pdf')
            P, _ = radvel.utils.initialize_posterior(config_file)
            if hasattr(P, 'bjd0'):
                args.plotkw['epoch'] = P.bjd0

            #FIXME FIXME: see line 103 of this for how to access residuals
            model = post.likelihood.model
            rvtimes = post.likelihood.x
            rverr = post.likelihood.errorbars()
            num_planets = model.num_planets

            #FIXME FIXME
            rawresid = post.likelihood.residuals()

            resid = (rawresid + post.params['dvdt'].value *
                     (rvtimes - model.time_base) + post.params['curv'].value *
                     (rvtimes - model.time_base)**2)

            import IPython
            IPython.embed()

            RVPlot = orbit_plots.MultipanelPlot(post,
                                                saveplot=saveto,
                                                **args.plotkw)
            RVPlot.plot_multipanel()

            # check to make sure that Posterior is not GP, print warning if it is
            if isinstance(post.likelihood,
                          radvel.likelihood.CompositeLikelihood):
                like_list = post.likelihood.like_list
            else:
                like_list = [post.likelihood]
            for like in like_list:
                if isinstance(like, radvel.likelihood.GPLikelihood):
                    print("WARNING: GP Likelihood(s) detected."
                          "You may want to use the '--gp' flag"
                          "when making these plots.")
                    break

        else:
            raise NotImplementedError('nope')

        savestate = {'{}_plot'.format(ptype): os.path.relpath(saveto)}
        save_status(statfile, 'plot', savestate)
示例#4
0
]
# post.priors += [radvel.prior.EccentricityPrior(3)]

print("Before fitting\n")
print(post, "\n")

# res  = optimize.minimize(post.neglogprob_array, post.get_vary_params(), method='Nelder-Mead' )
res = optimize.minimize(post.neglogprob_array,
                        post.get_vary_params(),
                        method='L-BFGS-B')
# sf.plot_radvel_results(post.likelihood, t)
plt.savefig(fig_name + "%d_afterpriors.png" % err_ind, bbox_inches="tight")
print("After fitting\n")
print(post, "\n")

RVPlot = orbit_plots.MultipanelPlot(post)
RVPlot.plot_multipanel()
# plt.show()
plt.savefig(fig_name + "%d_afterpriors_multi.png" % err_ind,
            bbox_inches="tight")
# plt.close(fig="all")

# df3 = radvel.mcmc(post) # amount of steps = nrun * 8 * nwalkers ?, default nrun is 10000, default nwalkers is 50?
df3 = radvel.mcmc(post, nrun=1000)
df3.to_pickle(fig_name + "%d_mcmc.pkl" % err_ind)

fig = corner.corner(df3[labels],
                    labels=labels,
                    quantiles=[0.16, 0.84],
                    plot_datapoints=False)
plt.savefig(fig_name + "%d_corner.png" % err_ind, bbox_inches="tight")