예제 #1
0
def main(modelid, datestr):

    assert modelid in ['allindivtransit', 'tessindivtransit']
    yval = 'PDCSAP_FLUX' # could be SAP_FLUX 

    OVERWRITE = 0
    REALID = 'TOI_837'
    provenance = 'spoc'
    PLOTDIR = os.path.join(
        RESULTSDIR, '{}_{}_phot_results'.format(REALID, modelid)
    )
    PLOTDIR = os.path.join(PLOTDIR, datestr)

    summarypath = os.path.join(
        PLOTDIR, 'posterior_table_raw_{}.csv'.format(modelid)
    )
    pklpath = os.path.join(
        os.path.expanduser('~'), 'local', 'timmy',
        '{}_model_{}_{}.pkl'.format(REALID, modelid, datestr)
    )
    np.random.seed(42)

    ########################################## 
    # get allindivtransit initialized
    ########################################## 
    provenance = 'spoc' # could be "cdips"
    yval = 'PDCSAP_FLUX' # could be SAP_FLUX 
    x_obs, y_obs, y_err = get_clean_tessphot(provenance, yval, binsize=None,
                                             maskflares=1)
    s = np.isfinite(y_obs) & np.isfinite(x_obs) & np.isfinite(y_err)
    x_obs, y_obs, y_err = x_obs[s], y_obs[s], y_err[s]
    cut_tess = 1
    if cut_tess:
        x_obs, y_obs, y_err = _subset_cut(x_obs, y_obs, y_err, n=3.5)

    ngroups, groupinds = find_lc_timegroups(x_obs, mingap=4.0)
    assert ngroups == 5

    datasets = OrderedDict()
    for ix, g in enumerate(groupinds):
        tess_texp = np.nanmedian(np.diff(x_obs[g]))
        datasets[f'tess_{ix}'] = [x_obs[g], y_obs[g], y_err[g], tess_texp]

    if modelid == 'allindivtransit':
        datestrs = ['20200401', '20200426', '20200521', '20200614']
        for ix, d in enumerate(datestrs):
            x_obs, y_obs, y_err = get_elsauce_phot(datestr=d)
            x_obs -= 2457000 # convert to BTJD
            elsauce_texp = np.nanmedian(np.diff(x_obs))
            datasets[f'elsauce_{ix}'] = [x_obs, y_obs, y_err, elsauce_texp]

        datestrs = ['20200529', '20200614', '20200623']
        for ix, d in enumerate(datestrs):
            x_obs, y_obs, y_err = get_astep_phot(datestr=d)
            x_obs += 2450000 # convert to BJD_TDB
            x_obs -= 2457000 # convert to BTJD
            astep_texp = np.nanmedian(np.diff(x_obs))
            datasets[f'astep_{ix}'] = [x_obs, y_obs, y_err, astep_texp]

    mp = ModelParser(modelid)

    prior_d = initialize_prior_d(mp.modelcomponents, datasets=datasets)
    ########################################## 
    # end intiialization
    ########################################## 

    if not os.path.exists(summarypath):

        m = ModelFitter(modelid, datasets, prior_d, plotdir=PLOTDIR,
                        pklpath=pklpath, overwrite=OVERWRITE)

        # stat_funcsdict = A list of functions or a dict of functions with
        # function names as keys used to calculate statistics. By default, the
        # mean, standard deviation, simulation standard error, and highest
        # posterior density intervals are included.
        stat_funcsdict = {
            'median': np.nanmedian
        }

        df = pm.summary(
            m.trace,
            round_to=10, kind='stats',
            stat_funcs=stat_funcsdict,
            extend=True
        )

        df.to_csv(summarypath, index=True)

    else:
        df = pd.read_csv(summarypath, index_col=0)

    fitted_params = [
        'period', 't0', 'log_r', 'b', 'u[0]', 'u[1]', 'r_star', 'logg_star'
    ]
    for i in range(5):
        fitted_params.append(f'tess_{i}_mean')
        fitted_params.append(f'tess_{i}_a1')
        fitted_params.append(f'tess_{i}_a2')
    if modelid == 'allindivtransit':
        for i in range(4):
            fitted_params.append(f'elsauce_{i}_mean')
            fitted_params.append(f'elsauce_{i}_a1')
            fitted_params.append(f'elsauce_{i}_a2')
        for i in range(3):
            fitted_params.append(f'astep_{i}_mean')
            fitted_params.append(f'astep_{i}_a1')
            fitted_params.append(f'astep_{i}_a2')

    n_fitted = len(fitted_params)

    derived_params = [
        'r', 'rho_star', 'r_planet', 'a_Rs', 'cosi', 'T_14', 'T_13'
    ]
    n_derived = len(derived_params)

    srows = []
    for f in fitted_params:
        srows.append(f)
    for d in derived_params:
        srows.append(d)

    df = df.loc[srows]

    cols = ['median', 'mean', 'sd', 'hpd_3%', 'hpd_97%']

    df = df[cols]

    print(df)

    from timmy.priors import (
        LOGG, LOGG_STDEV, RSTAR, RSTAR_STDEV
    )

    delta_u = 0.15
    pr = {
        'period': normal_str(
            mu=prior_d['period'], sd=1e-1, fmtstr='({:.4f}; {:.4f})'
        ),
        't0': normal_str(
            mu=prior_d['t0'], sd=1e-1, fmtstr='({:.6f}; {:.4f})'
        ),
        'log_r': uniform_str(
            lower=np.log(1e-2), upper=np.log(1), fmtstr='({:.3f}; {:.3f})'
        ),
        'b': r'$\mathcal{U}(0; 1+R_{\mathrm{p}}/R_\star)$',
        #'u[0]': '(2)',
        #'u[1]': '(2)',
        'u[0]': uniform_str(prior_d['u[0]']-delta_u, prior_d['u[0]']+delta_u,
                            fmtstr='({:.3f}; {:.3f})') + '$^{(2)}$',
        'u[1]': uniform_str(prior_d['u[1]']-delta_u, prior_d['u[1]']+delta_u,
                            fmtstr='({:.3f}; {:.3f})') + '$^{(2)}$',
        'r_star': truncnormal_str(
            mu=RSTAR, sd=RSTAR_STDEV, fmtstr='({:.3f}; {:.3f})'
        ),
        'logg_star': normal_str(
            mu=LOGG, sd=LOGG_STDEV, fmtstr='({:.3f}; {:.3f})'
        )
    }
    ufmt = '({:.2f}; {:.2f})'

    delta_trend = 0.05
    for i in range(5):
        pr[f'tess_{i}_mean'] = normal_str(mu=prior_d[f'tess_{i}_mean'],
                                          sd=0.01, fmtstr=ufmt)
        pr[f'tess_{i}_a1'] = uniform_str(lower=-delta_trend, upper=delta_trend, fmtstr=ufmt)
        pr[f'tess_{i}_a2'] = uniform_str(lower=-delta_trend, upper=delta_trend, fmtstr=ufmt)
    if modelid == 'allindivtransit':
        for i in range(4):
            pr[f'elsauce_{i}_mean'] = normal_str(mu=prior_d[f'elsauce_{i}_mean'],
                                                 sd=0.01, fmtstr=ufmt)
            pr[f'elsauce_{i}_a1'] = uniform_str(lower=-delta_trend, upper=delta_trend, fmtstr=ufmt)
            pr[f'elsauce_{i}_a2'] = uniform_str(lower=-delta_trend, upper=delta_trend, fmtstr=ufmt)
        for i in range(3):
            pr[f'astep_{i}_mean'] = normal_str(mu=prior_d[f'astep_{i}_mean'],
                                                 sd=0.01, fmtstr=ufmt)
            pr[f'astep_{i}_a1'] = uniform_str(lower=-delta_trend, upper=delta_trend, fmtstr=ufmt)
            pr[f'astep_{i}_a2'] = uniform_str(lower=-delta_trend, upper=delta_trend, fmtstr=ufmt)


    for d in derived_params:
        pr[d] = '--'

    # round everything. requires a double transpose because df.round
    # operates column-wise
    if modelid in ['allindivtransit', 'tessindivtransit']:
        round_precision = [7, 7, 5, 4, 3, 3, 3, 3]
        n_rp = len(round_precision)
        for i in range(n_fitted - n_rp):
            round_precision.append(4)
    else:
        raise NotImplementedError
    for d in derived_params:
        round_precision.append(2)

    df = df.T.round(
        decimals=dict(
            zip(df.index, round_precision)
        )
    ).T

    df['priors'] = list(pr.values())

    # units
    ud = {
        'period': 'd',
        't0': 'd',
        'log_r': '--',
        'b': '--',
        'u[0]': '--',
        'u[1]': '--',
        'r_star': r'$R_\odot$',
        'logg_star': 'cgs'
    }
    for i in range(5):
        ud[f'tess_{i}_mean'] = '--'
        ud[f'tess_{i}_a1'] = 'd$^{-1}$'
        ud[f'tess_{i}_a2'] = 'd$^{-2}$'
    if modelid == 'allindivtransit':
        for i in range(4):
            ud[f'elsauce_{i}_mean'] = '--'
            ud[f'elsauce_{i}_a1'] = 'd$^{-1}$'
            ud[f'elsauce_{i}_a2'] = 'd$^{-2}$'
        for i in range(3):
            ud[f'astep_{i}_mean'] = '--'
            ud[f'astep_{i}_a1'] = 'd$^{-1}$'
            ud[f'astep_{i}_a2'] = 'd$^{-2}$'

    ud['r'] = '--'
    ud['rho_star'] = 'g$\ $cm$^{-3}$'
    ud['r_planet'] = '$R_{\mathrm{Jup}}$'
    ud['a_Rs'] = '--'
    ud['cosi'] = '--'
    ud['T_14'] = 'hr'
    ud['T_13'] = 'hr'

    df['units'] = list(ud.values())

    df = df[
        ['units', 'priors', 'median', 'mean', 'sd', 'hpd_3%', 'hpd_97%']
    ]

    latexparams = [
        #useful
        r"$P$",
        r"$t_0^{(1)}$",
        r"$\log R_{\rm p}/R_\star$",
        "$b$",
        "$u_1$",
        "$u_2$",
        "$R_\star$",
        "$\log g$"
    ]
    for i in range(5):
        latexparams.append('$a_{'+str(i)+'0;\mathrm{TESS}}$')
        latexparams.append('$a_{'+str(i)+'1;\mathrm{TESS}}$')
        latexparams.append('$a_{'+str(i)+'2;\mathrm{TESS}}$')
    if modelid == 'allindivtransit':
        for i in range(4):
            latexparams.append('$a_{'+str(i)+'0;\mathrm{Sauce}}$')
            latexparams.append('$a_{'+str(i)+'1;\mathrm{Sauce}}$')
            latexparams.append('$a_{'+str(i)+'2;\mathrm{Sauce}}$')
        for i in range(3):
            latexparams.append('$a_{'+str(i)+'0;\mathrm{ASTEP}}$')
            latexparams.append('$a_{'+str(i)+'1;\mathrm{ASTEP}}$')
            latexparams.append('$a_{'+str(i)+'2;\mathrm{ASTEP}}$')

    from billy.convenience import flatten
    dlatexparams = [
        r"$R_{\rm p}/R_\star$",
        r"$\rho_\star$",
        r"$R_{\rm p}$",
        "$a/R_\star$",
        '$\cos i$',
        '$T_{14}$',
        '$T_{13}$'
    ]
    latexparams = flatten([latexparams, dlatexparams])
    df.index = latexparams

    outpath = os.path.join(PLOTDIR,
                           'posterior_table_clean_{}.csv'.format(modelid))
    df.to_csv(outpath, float_format='%.12f', na_rep='NaN')
    print('made {}'.format(outpath))

    # df.to_latex is dumb with float formatting.
    outpath = os.path.join(PLOTDIR,
                           'posterior_table_clean_{}.tex'.format(modelid))
    df.to_csv(outpath, sep=',', line_terminator=' \\\\\n',
              float_format='%.12f', na_rep='NaN')

    with open(outpath, 'r') as f:
        lines = f.readlines()

    for ix, l in enumerate(lines):

        # replace commas with latex ampersands
        thisline = deepcopy(l.replace(',', ' & '))

        # replace quotes with nada
        thisline = thisline.replace('"', '')

        # replace }0 with },0
        thisline = thisline.replace('}0', '},0')
        thisline = thisline.replace('}1', '},1')
        thisline = thisline.replace('}2', '},2')

        if ix == 0:
            lines[ix] = thisline
            continue

        # iteratively replace stupid trailing zeros with whitespace
        while re.search("0{2,10}\ ", thisline) is not None:
            r = re.search("0{2,10}\ ", thisline)
            thisline = thisline.replace(
                thisline[r.start():r.end()],
                ' '
            )

        lines[ix] = thisline

    with open(outpath, 'w') as f:
        f.writelines(lines)

    print('made {}'.format(outpath))
예제 #2
0
def main(modelid):

    make_threadsafe = 0
    cut_tess = 1

    fitindiv = 1
    phaseplot = 1
    grounddepth = 1
    cornerplot = 1
    subsetcorner = 1

    N_samples = 30000  # took 2h 20m, but Rhat=1.0 for all
    # N_samples = 2000 # took 16m, 14s. but Rhat=1.01 for b, rp/rs, + a few a1/a2s
    target_accept = 0.9

    OVERWRITE = 1
    REALID = 'TOI_837'

    PLOTDIR = os.path.join(RESULTSDIR,
                           '{}_{}_phot_results'.format(REALID, modelid))
    if not os.path.exists(PLOTDIR):
        os.mkdir(PLOTDIR)
    datestr = '20200711'
    PLOTDIR = os.path.join(PLOTDIR, datestr)

    ##########################################

    assert modelid in ['allindivtransit']

    print(42 * '#')
    print(modelid)
    print(42 * '#')

    if not os.path.exists(PLOTDIR):
        os.mkdir(PLOTDIR)

    pklpath = os.path.join(os.path.expanduser('~'), 'local', 'timmy',
                           f'{REALID}_model_{modelid}_{datestr}.pkl')
    np.random.seed(42)

    # get tess data
    provenance = 'spoc'  # could be "cdips"
    yval = 'PDCSAP_FLUX'  # could be SAP_FLUX
    x_obs, y_obs, y_err = get_clean_tessphot(provenance,
                                             yval,
                                             binsize=None,
                                             maskflares=1)
    #y_flat, y_trend = detrend_tessphot(x_obs, y_obs, y_err)
    s = np.isfinite(y_obs) & np.isfinite(x_obs) & np.isfinite(y_err)
    x_obs, y_obs, y_err = x_obs[s], y_obs[s], y_err[s]
    if cut_tess:
        x_obs, y_obs, y_err = _subset_cut(x_obs, y_obs, y_err, n=3.5)

    ngroups, groupinds = find_lc_timegroups(x_obs, mingap=4.0)
    assert ngroups == 5

    datasets = OrderedDict()
    for ix, g in enumerate(groupinds):
        tess_texp = np.nanmedian(np.diff(x_obs[g]))
        datasets[f'tess_{ix}'] = [x_obs[g], y_obs[g], y_err[g], tess_texp]

    datestrs = ['20200401', '20200426', '20200521', '20200614']
    for ix, d in enumerate(datestrs):
        x_obs, y_obs, y_err = get_elsauce_phot(datestr=d)
        x_obs -= 2457000  # convert to BTJD
        elsauce_texp = np.nanmedian(np.diff(x_obs))
        datasets[f'elsauce_{ix}'] = [x_obs, y_obs, y_err, elsauce_texp]

    datestrs = ['20200529', '20200614', '20200623']
    for ix, d in enumerate(datestrs):
        x_obs, y_obs, y_err = get_astep_phot(datestr=d)
        x_obs += 2450000  # convert to BJD_TDB
        x_obs -= 2457000  # convert to BTJD
        astep_texp = np.nanmedian(np.diff(x_obs))
        datasets[f'astep_{ix}'] = [x_obs, y_obs, y_err, astep_texp]

    mp = ModelParser(modelid)

    prior_d = initialize_prior_d(mp.modelcomponents, datasets=datasets)

    m = ModelFitter(modelid,
                    datasets,
                    prior_d,
                    plotdir=PLOTDIR,
                    pklpath=pklpath,
                    overwrite=OVERWRITE,
                    N_samples=N_samples,
                    target_accept=target_accept)

    print(pm.summary(m.trace, var_names=list(prior_d.keys())))
    summdf = pm.summary(m.trace,
                        var_names=list(prior_d.keys()),
                        round_to=10,
                        kind='stats',
                        stat_funcs={'median': np.nanmedian},
                        extend=True)
    rp_limit = np.percentile(m.trace.r_planet, 1 - 0.9973)
    print(42 * '-')
    print(f'Rp limit: {rp_limit:.3f} Rjup')
    print(42 * '-')

    if make_threadsafe:
        pass

    else:
        if grounddepth:
            outpath = join(PLOTDIR, f'{REALID}_{modelid}_grounddepth.png')
            tp.plot_grounddepth(m,
                                summdf,
                                outpath,
                                modelid=modelid,
                                showerror=0)

        if subsetcorner:
            outpath = join(PLOTDIR, f'{REALID}_{modelid}_subsetcorner.png')
            tp.plot_subsetcorner(m, outpath)

        if phaseplot:
            outpath = join(PLOTDIR, f'{REALID}_{modelid}_phaseplot.png')
            tp.plot_phasefold(m, summdf, outpath, modelid=modelid, inppt=1)

        if fitindiv:
            outpath = join(PLOTDIR, f'{REALID}_{modelid}_fitindiv.png')
            tp.plot_fitindiv(m, summdf, outpath, modelid=modelid)

        if cornerplot:
            outpath = join(PLOTDIR, f'{REALID}_{modelid}_cornerplot.png')
            tp.plot_cornerplot(prior_d, m, outpath)
예제 #3
0
def main(modelid):

    make_threadsafe = 0
    cut_tess = 1

    fitindiv = 1
    phaseplot = 1
    cornerplot = 1
    subsetcorner = 1
    grounddepth = 1

    N_samples = 30000
    target_accept = 0.9

    OVERWRITE = 1
    REALID = 'TOI_837'

    PLOTDIR = os.path.join(RESULTSDIR,
                           '{}_{}_phot_results'.format(REALID, modelid))
    if not os.path.exists(PLOTDIR):
        os.mkdir(PLOTDIR)
    datestr = '20200711'
    PLOTDIR = os.path.join(PLOTDIR, datestr)

    ##########################################

    assert modelid in ['tessindivtransit']

    print(42 * '#')
    print(modelid)
    print(42 * '#')

    if not os.path.exists(PLOTDIR):
        os.mkdir(PLOTDIR)

    pklpath = os.path.join(os.path.expanduser('~'), 'local', 'timmy',
                           f'{REALID}_model_{modelid}_{datestr}.pkl')
    np.random.seed(42)

    # get tess data
    provenance = 'spoc'  # could be "cdips"
    yval = 'PDCSAP_FLUX'  # could be SAP_FLUX
    x_obs, y_obs, y_err = get_clean_tessphot(provenance,
                                             yval,
                                             binsize=None,
                                             maskflares=1)
    #y_flat, y_trend = detrend_tessphot(x_obs, y_obs, y_err)
    s = np.isfinite(y_obs) & np.isfinite(x_obs) & np.isfinite(y_err)
    x_obs, y_obs, y_err = x_obs[s], y_obs[s], y_err[s]
    if cut_tess:
        x_obs, y_obs, y_err = _subset_cut(x_obs, y_obs, y_err, n=3.5)

    ngroups, groupinds = find_lc_timegroups(x_obs, mingap=4.0)
    assert ngroups == 5

    datasets = OrderedDict()
    for ix, g in enumerate(groupinds):
        tess_texp = np.nanmedian(np.diff(x_obs[g]))
        datasets[f'tess_{ix}'] = [x_obs[g], y_obs[g], y_err[g], tess_texp]

    mp = ModelParser(modelid)

    prior_d = initialize_prior_d(mp.modelcomponents, datasets=datasets)

    m = ModelFitter(modelid,
                    datasets,
                    prior_d,
                    plotdir=PLOTDIR,
                    pklpath=pklpath,
                    overwrite=OVERWRITE,
                    N_samples=N_samples,
                    target_accept=target_accept)

    print(pm.summary(m.trace, var_names=list(prior_d.keys())))
    summdf = pm.summary(m.trace,
                        var_names=list(prior_d.keys()),
                        round_to=10,
                        kind='stats',
                        stat_funcs={'median': np.nanmedian},
                        extend=True)

    printparams = ['r_planet', 'b']
    print(42 * '-')
    for p in printparams:
        med = np.percentile(m.trace[p], 50)
        up = np.percentile(m.trace[p], 84)
        low = np.percentile(m.trace[p], 36)
        print(f'{p} limit: {med:.3f} +{up-med:.3f} -{med-low:.3f}')
    print(42 * '-')

    if make_threadsafe:
        pass

    else:
        # if grounddepth:
        #     outpath = join(PLOTDIR, f'{REALID}_{modelid}_grounddepth.png')
        #     tp.plot_grounddepth(m, summdf, outpath, modelid=modelid,
        #                         showerror=0)

        if subsetcorner:
            outpath = join(PLOTDIR, f'{REALID}_{modelid}_subsetcorner.png')
            tp.plot_subsetcorner(m, outpath)

        if phaseplot:
            outpath = join(PLOTDIR, f'{REALID}_{modelid}_phaseplot.png')
            tp.plot_phasefold(m, summdf, outpath, modelid=modelid, inppt=1)

        if fitindiv:
            outpath = join(PLOTDIR, f'{REALID}_{modelid}_fitindiv.png')
            tp.plot_fitindiv(m, summdf, outpath, modelid=modelid)

        if cornerplot:
            outpath = join(PLOTDIR, f'{REALID}_{modelid}_cornerplot.png')
            tp.plot_cornerplot(prior_d, m, outpath)
예제 #4
0
def main(modelid):

    make_threadsafe = 0
    cut_tess = 1

    fitindiv = 0
    phaseplot = 0
    grounddepth = 0
    cornerplot = 0
    subsetcorner = 0

    N_samples = 30000 # took 2h 20m, but Rhat=1.0 for all
    # N_samples = 100 # testing
    target_accept = 0.9

    OVERWRITE = 1
    REALID = 'TOI_837'

    PLOTDIR = os.path.join(
        RESULTSDIR, '{}_{}_phot_results'.format(REALID, modelid)
    )
    if not os.path.exists(PLOTDIR):
        os.mkdir(PLOTDIR)
    datestr = '20200805'
    PLOTDIR = os.path.join(PLOTDIR, datestr)

    ##########################################

    assert modelid in ['evenindivtransit', 'oddindivtransit']

    print(42*'#')
    print(modelid)
    print(42*'#')

    if not os.path.exists(PLOTDIR):
        os.mkdir(PLOTDIR)

    pklpath = os.path.join(
        os.path.expanduser('~'), 'local', 'timmy',
        f'{REALID}_model_{modelid}_{datestr}.pkl'
    )
    np.random.seed(42)

    # get tess data
    provenance = 'spoc' # could be "cdips"
    yval = 'PDCSAP_FLUX' # could be SAP_FLUX 
    x_obs, y_obs, y_err = get_clean_tessphot(provenance, yval, binsize=None,
                                             maskflares=1)

    s = np.isfinite(y_obs) & np.isfinite(x_obs) & np.isfinite(y_err)
    x_obs, y_obs, y_err = x_obs[s], y_obs[s], y_err[s]
    if cut_tess:
        if 'even' in modelid:
            onlyeven = True
            onlyodd = False
        elif 'odd' in modelid:
            onlyeven = False
            onlyodd = True
        else:
            raise NotImplementedError

        x_obs, y_obs, y_err = _subset_cut(
            x_obs, y_obs, y_err, n=3.5, onlyeven=onlyeven, onlyodd=onlyodd
        )

    ngroups, groupinds = find_lc_timegroups(x_obs, mingap=4.0)
    assert ngroups in [2,3]

    datasets = OrderedDict()
    for ix, g in enumerate(groupinds):
        tess_texp = np.nanmedian(np.diff(x_obs[g]))
        datasets[f'tess_{ix}'] = [x_obs[g], y_obs[g], y_err[g], tess_texp]

    # see /doc/20200805_ephemeris_counting.txt
    if 'even' in modelid:
        datestrs = ['20200401', '20200521']
    elif 'odd' in modelid:
        datestrs = ['20200426', '20200614']
    for ix, d in enumerate(datestrs):
        x_obs, y_obs, y_err = get_elsauce_phot(datestr=d)
        x_obs -= 2457000 # convert to BTJD
        elsauce_texp = np.nanmedian(np.diff(x_obs))
        datasets[f'elsauce_{ix}'] = [x_obs, y_obs, y_err, elsauce_texp]

    if 'even' in modelid:
        datestrs = ['20200623']
    elif 'odd' in modelid:
        datestrs = ['20200529', '20200614']
    for ix, d in enumerate(datestrs):
        x_obs, y_obs, y_err = get_astep_phot(datestr=d)
        x_obs += 2450000 # convert to BJD_TDB
        x_obs -= 2457000 # convert to BTJD
        astep_texp = np.nanmedian(np.diff(x_obs))
        datasets[f'astep_{ix}'] = [x_obs, y_obs, y_err, astep_texp]

    mp = ModelParser(modelid)

    prior_d = initialize_prior_d(mp.modelcomponents, datasets=datasets)

    m = ModelFitter(modelid, datasets, prior_d, plotdir=PLOTDIR,
                    pklpath=pklpath, overwrite=OVERWRITE, N_samples=N_samples,
                    target_accept=target_accept)

    print(pm.summary(m.trace, var_names=list(prior_d.keys())))
    summdf = pm.summary(m.trace, var_names=list(prior_d.keys()), round_to=10,
                        kind='stats', stat_funcs={'median':np.nanmedian},
                        extend=True)

    printparams = ['r_planet', 'b', 'log_r', 'log_r_sq']
    print(42*'-')
    print(modelid)
    for p in printparams:
        if p != 'log_r_sq':
            med = np.percentile(m.trace[p], 50)
            up = np.percentile(m.trace[p], 84)
            low = np.percentile(m.trace[p], 36)
        else:
            med = np.percentile(np.exp(m.trace['log_r'])**2, 50)
            up = np.percentile(np.exp(m.trace['log_r'])**2, 84)
            low = np.percentile(np.exp(m.trace['log_r'])**2, 36)
        print(f'{p} limit: {med:.6f} +{up-med:.6f} -{med-low:.6f}')
    print(42*'-')


    if make_threadsafe:
        pass

    else:
        if grounddepth:
            outpath = join(PLOTDIR, f'{REALID}_{modelid}_grounddepth.png')
            tp.plot_grounddepth(m, summdf, outpath, modelid=modelid,
                                showerror=0)

        if subsetcorner:
            outpath = join(PLOTDIR, f'{REALID}_{modelid}_subsetcorner.png')
            tp.plot_subsetcorner(m, outpath)

        if phaseplot:
            outpath = join(PLOTDIR, f'{REALID}_{modelid}_phaseplot.png')
            tp.plot_phasefold(m, summdf, outpath, modelid=modelid, inppt=1)

        if fitindiv:
            outpath = join(PLOTDIR, f'{REALID}_{modelid}_fitindiv.png')
            tp.plot_fitindiv(m, summdf, outpath, modelid=modelid)

        if cornerplot:
            outpath = join(PLOTDIR, f'{REALID}_{modelid}_cornerplot.png')
            tp.plot_cornerplot(prior_d, m, outpath)
예제 #5
0
def main(modelid):

    make_threadsafe = 0
    cut_tess = 1

    phaseplot = 1
    grounddepth = 1
    fittedzoom = 0
    cornerplot = 0

    OVERWRITE = 1
    REALID = 'TOI_837'

    PLOTDIR = os.path.join(RESULTSDIR,
                           '{}_{}_phot_results'.format(REALID, modelid))
    if not os.path.exists(PLOTDIR):
        os.mkdir(PLOTDIR)
    PLOTDIR = os.path.join(PLOTDIR, '20200617')

    ##########################################

    assert modelid in [
        'alltransit', 'alltransit_quad', 'alltransit_quaddepthvar'
    ]

    print(42 * '#')
    print(modelid)
    print(42 * '#')

    if not os.path.exists(PLOTDIR):
        os.mkdir(PLOTDIR)

    pklpath = os.path.join(os.path.expanduser('~'), 'local', 'timmy',
                           '{}_model_{}.pkl'.format(REALID, modelid))
    np.random.seed(42)

    # get tess data
    provenance = 'spoc'  # could be "cdips"
    yval = 'PDCSAP_FLUX'  # could be SAP_FLUX
    x_obs, y_obs, y_err = get_clean_tessphot(provenance,
                                             yval,
                                             binsize=None,
                                             maskflares=1)
    y_flat, y_trend = detrend_tessphot(x_obs, y_obs, y_err)
    s = np.isfinite(y_flat) & np.isfinite(x_obs) & np.isfinite(y_err)
    x_obs, y_flat, y_err = x_obs[s], y_flat[s], y_err[s]
    if cut_tess:
        x_obs, y_flat, y_err = _subset_cut(x_obs, y_flat, y_err)
    tess_texp = np.nanmedian(np.diff(x_obs))

    datasets = OrderedDict()
    datasets['tess'] = [x_obs, y_flat, y_err, tess_texp]

    datestrs = ['20200401', '20200426', '20200521', '20200614']
    for ix, d in enumerate(datestrs):
        x_obs, y_obs, y_err = get_elsauce_phot(datestr=d)
        x_obs -= 2457000  # convert to BTJD
        elsauce_texp = np.nanmedian(np.diff(x_obs))
        datasets[f'elsauce_{ix}'] = [x_obs, y_obs, y_err, elsauce_texp]

    # note: we're fitting the detrended data
    mp = ModelParser(modelid)

    prior_d = initialize_prior_d(mp.modelcomponents, datasets=datasets)

    m = ModelFitter(modelid,
                    datasets,
                    prior_d,
                    plotdir=PLOTDIR,
                    pklpath=pklpath,
                    overwrite=OVERWRITE)

    print(pm.summary(m.trace, var_names=list(prior_d.keys())))
    summdf = pm.summary(m.trace,
                        var_names=list(prior_d.keys()),
                        round_to=10,
                        kind='stats',
                        stat_funcs={'median': np.nanmedian},
                        extend=True)

    if modelid == 'alltransit_quaddepthvar':
        var_names = [
            'tess_roughdepth', 'elsauce_0_roughdepth', 'elsauce_1_roughdepth',
            'elsauce_2_roughdepth', 'elsauce_3_roughdepth'
        ]
        ddf = pm.summary(m.trace,
                         var_names=var_names,
                         round_to=10,
                         kind='stats',
                         stat_funcs={'median': np.nanmedian},
                         extend=True)
        print(ddf)

    if make_threadsafe:
        pass

    else:
        if cornerplot:
            outpath = join(PLOTDIR, f'{REALID}_{modelid}_cornerplot.png')
            tp.plot_cornerplot(prior_d, m, outpath)

        if phaseplot:
            outpath = join(PLOTDIR, f'{REALID}_{modelid}_phaseplot.png')
            tp.plot_phasefold(m, summdf, outpath, modelid=modelid, inppt=1)

        if fittedzoom:
            outpath = join(PLOTDIR,
                           '{}_{}_fittedzoom.png'.format(REALID, modelid))
            tp.plot_fitted_zoom(m, summdf, outpath, modelid=modelid)

        if grounddepth:
            outpath = join(PLOTDIR, f'{REALID}_{modelid}_grounddepth.png')
            tp.plot_grounddepth(m, summdf, outpath, modelid=modelid)
예제 #6
0
def main(modelid):

    seldate = '20200426'
    bp = 'Rband'
    N_samples = 12000  # nb: 7.5mins, decent but not perfect convergence

    # seldate = '20200614'
    # bp = 'Bband'
    # Nsamples = 12000 # 7min: gets rhat = 1.0 for all parameters.

    cornerplot = 1
    phaseplot = 0
    grounddepth = 0
    fittedzoom = 0

    make_threadsafe = 0
    cut_tess = 1
    OVERWRITE = 1
    REALID = 'TOI_837'

    PLOTDIR = os.path.join(RESULTSDIR,
                           f'{REALID}_{modelid}_{seldate}_{bp}_phot_results')
    if not os.path.exists(PLOTDIR):
        os.mkdir(PLOTDIR)
    PLOTDIR = os.path.join(PLOTDIR, f'20200617_{bp}')

    ##########################################

    assert modelid in ['onetransit']

    print(42 * '#')
    print(modelid)
    print(42 * '#')

    if not os.path.exists(PLOTDIR):
        os.mkdir(PLOTDIR)

    pklpath = os.path.join(os.path.expanduser('~'), 'local', 'timmy',
                           f'{REALID}_model_{modelid}_{seldate}_{bp}.pkl')
    np.random.seed(42)

    # get tess data
    provenance = 'spoc'  # could be "cdips"
    yval = 'PDCSAP_FLUX'  # could be SAP_FLUX
    x_obs, y_obs, y_err = get_clean_tessphot(provenance,
                                             yval,
                                             binsize=None,
                                             maskflares=1)
    y_flat, y_trend = detrend_tessphot(x_obs, y_obs, y_err)
    s = np.isfinite(y_flat) & np.isfinite(x_obs) & np.isfinite(y_err)
    x_obs, y_flat, y_err = x_obs[s], y_flat[s], y_err[s]
    if cut_tess:
        x_obs, y_flat, y_err = _subset_cut(x_obs, y_flat, y_err)
    tess_texp = np.nanmedian(np.diff(x_obs))

    datasets = OrderedDict()
    datasets['tess'] = [x_obs, y_flat, y_err, tess_texp]

    datestrs = ['20200401', '20200426', '20200521', '20200614']
    for ix, d in enumerate(datestrs):
        x_obs, y_obs, y_err = get_elsauce_phot(datestr=d)
        x_obs -= 2457000  # convert to BTJD
        elsauce_texp = np.nanmedian(np.diff(x_obs))
        datasets[f'elsauce_{ix}'] = [x_obs, y_obs, y_err, elsauce_texp]

    if seldate not in datestrs:
        raise NotImplementedError
    else:
        usedata = OrderedDict()
        tra_ind = int(np.argwhere(np.array(datestrs) == seldate))
        usedata[f'elsauce_{tra_ind}'] = (deepcopy(
            datasets[f'elsauce_{tra_ind}']))

    # note: we're fitting the detrended data
    mp = ModelParser(modelid)

    prior_d = initialize_prior_d(mp.modelcomponents, datasets=usedata)

    m = ModelFitter(modelid,
                    usedata,
                    prior_d,
                    plotdir=PLOTDIR,
                    pklpath=pklpath,
                    overwrite=OVERWRITE,
                    N_samples=N_samples)

    print(pm.summary(m.trace, var_names=list(prior_d.keys())))
    summdf = pm.summary(m.trace,
                        var_names=list(prior_d.keys()),
                        round_to=10,
                        kind='stats',
                        stat_funcs={'median': np.nanmedian},
                        extend=True)

    var_names = ['roughdepth']
    ddf = pm.summary(m.trace,
                     var_names=var_names,
                     round_to=10,
                     kind='stats',
                     stat_funcs={'median': np.nanmedian},
                     extend=True)
    print(ddf)
    d_1sig = np.percentile(m.trace.roughdepth, 50 - (68 / 2))
    d_2sig = np.percentile(m.trace.roughdepth, 50 - (95 / 2))
    print(f'1 sigma depth lower limit: {d_1sig:.6f}')
    print(f'2 sigma depth lower limit: {d_2sig:.6f}')

    if make_threadsafe:
        pass

    else:
        if cornerplot:
            outpath = join(PLOTDIR, f'{REALID}_{modelid}_cornerplot.png')
            prior_d['roughdepth'] = 4600e-6
            tp.plot_cornerplot(prior_d, m, outpath)