コード例 #1
0
    def approximate_planetary_params(self, nrun=500, nwalkers=30):
        """
        Approximate the planetary mass and semi-major axis from the estimated planetary parameters implemented in
        keplerian_analysis. The mass, semi-major axis, and period are stored in a csv file labeled
        _PlanetaryProperties in the directory specified, and the rest of the orbital parameters are stored in a
        seperate csv labeled _MCMCAnalysisPlanetaryParameters.csv in the directory specified.
        :param nrun: the MCMC number of iterations
        :param nwalkers: the number of MCMC walkers
        """
        df = radvel.mcmc(self.post,
                         nwalkers=100,
                         nrun=nrun,
                         savename='rawchains.h5')  # Start MCMC analysis

        # Some cool plots
        Corner = mcmc_plots.CornerPlot(self.post,
                                       df,
                                       saveplot=self.dir + "TestCornerPlot")
        Corner.plot()  # A corner plot
        trendplot = radvel.plot.mcmc_plots.TrendPlot(self.post,
                                                     df,
                                                     nwalkers,
                                                     outfile=self.dir +
                                                     'TestTrendFile')
        trendplot.plot()  # A trend plot
        multipanel = radvel.plot.orbit_plots.MultipanelPlot(self.post,
                                                            saveplot=self.dir +
                                                            'TestMultipanel')
        multipanel.plot_multipanel(
        )  # A multipanel plot with the estimated parameters

        # Make the planetary properties pandas DataFrame.
        mass = []
        a = []

        for per in list(self.periods):
            mass_ = radvel.utils.Msini(np.average(self.y.values),
                                       per,
                                       self.smass,
                                       e=0.0)
            mass.append(mass_)
            a_ = radvel.utils.semi_major_axis(per, self.smass)
            a.append(a_)

        params_df = pd.DataFrame({
            "Plantary Period (JD)": list(self.periods),
            "Semi Major Axis (AU)": list(a),
            "Planetary Mass (Earth masses)": list(mass)
        })

        print("Saving the computed planetary properties at the directory " +
              self.dir + "...")
        params_df.to_csv(self.dir + self.name + '_PlanetaryProperties.csv')
        print("Done!")
        self.paramsdf = params_df
        print("Saving the computed orbital parameters at the directory " +
              self.dir + "...")
        df.to_csv(self.dir + self.name +
                  '_MCMCAnalysisPlanetaryParameters.csv')
        print("Done!")
コード例 #2
0
ファイル: driver.py プロジェクト: megbedell/radvel
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)