Beispiel #1
0
def test_checkplot_pickle_update():
    '''
    Tests if a checkplot pickle can be made, read back, and updated.

    '''

    outpath = os.path.join(os.path.dirname(LCPATH),
                           'test-checkplot.pkl')

    lcd, msg = hatlc.read_and_filter_sqlitecurve(LCPATH)
    gls = periodbase.pgen_lsp(lcd['rjd'], lcd['aep_000'], lcd['aie_000'])

    assert isinstance(gls, dict)
    assert_allclose(gls['bestperiod'], 1.54289477)

    pdm = periodbase.stellingwerf_pdm(lcd['rjd'],
                                      lcd['aep_000'],
                                      lcd['aie_000'])

    assert isinstance(pdm, dict)
    assert_allclose(pdm['bestperiod'], 3.08578956)

    # test write
    cpf = checkplot.checkplot_pickle(
        [gls, pdm],
        lcd['rjd'], lcd['aep_000'], lcd['aie_000'],
        outfile=outpath,
        objectinfo=lcd['objectinfo']
    )

    assert os.path.exists(outpath)

    # test read back
    cpd = checkplot._read_checkplot_picklefile(cpf)

    assert isinstance(cpd, dict)

    cpdkeys = set(list(cpd.keys()))
    testset = {'0-gls', '1-pdm', 'comments', 'externalplots',
               'finderchart', 'magseries', 'neighbors', 'normmingap',
               'normto', 'objectid', 'objectinfo', 'pfmethods', 'sigclip',
               'signals', 'status', 'varinfo'}
    assert (testset - cpdkeys) == set()

    assert_allclose(cpd['0-gls']['bestperiod'], 1.54289477)
    assert_allclose(cpd['1-pdm']['bestperiod'], 3.08578956)

    # test update write to pickle
    cpd['comments'] = ('this is a test of the checkplot pickle '
                       'update mechanism. this is only a test.')
    cpfupdated = checkplot.checkplot_pickle_update(cpf, cpd)

    cpdupdated = checkplot._read_checkplot_picklefile(cpfupdated)

    assert cpdupdated['comments'] == cpd['comments']
Beispiel #2
0
def test_checkplot_with_multiple_same_pfmethods():
    '''
    This tests running the same period-finder for different period ranges.

    '''

    outpath = os.path.join(os.path.dirname(LCPATH), 'test-checkplot.pkl')

    lcd, msg = hatlc.read_and_filter_sqlitecurve(LCPATH)

    gls_1 = periodbase.pgen_lsp(lcd['rjd'],
                                lcd['aep_000'],
                                lcd['aie_000'],
                                startp=0.01,
                                endp=0.1)
    gls_2 = periodbase.pgen_lsp(lcd['rjd'],
                                lcd['aep_000'],
                                lcd['aie_000'],
                                startp=0.1,
                                endp=300.0)
    pdm_1 = periodbase.stellingwerf_pdm(lcd['rjd'],
                                        lcd['aep_000'],
                                        lcd['aie_000'],
                                        startp=0.01,
                                        endp=0.1)
    pdm_2 = periodbase.stellingwerf_pdm(lcd['rjd'],
                                        lcd['aep_000'],
                                        lcd['aie_000'],
                                        startp=0.1,
                                        endp=300.0)

    assert isinstance(gls_1, dict)
    assert isinstance(gls_2, dict)
    assert isinstance(pdm_1, dict)
    assert isinstance(pdm_2, dict)

    cpf = checkplot.checkplot_pickle([gls_1, gls_2, pdm_1, pdm_2],
                                     lcd['rjd'],
                                     lcd['aep_000'],
                                     lcd['aie_000'],
                                     outfile=outpath,
                                     objectinfo=lcd['objectinfo'])

    assert os.path.exists(cpf)
    assert os.path.abspath(cpf) == os.path.abspath(outpath)

    cpd = checkplot._read_checkplot_picklefile(cpf)
    pfmethods = list(cpd['pfmethods'])

    assert len(pfmethods) == 4

    assert '0-gls' in pfmethods
    assert '1-gls' in pfmethods
    assert '2-pdm' in pfmethods
    assert '3-pdm' in pfmethods
def key_worker(task):
    '''
    This gets the required keys from the requested file.

    '''
    cpf, keys = task

    cpd = _read_checkplot_picklefile(cpf)

    resultkeys = []

    for k in keys:

        try:
            resultkeys.append(dict_get(cpd, k))
        except:
            resultkeys.append(np.nan)

    return resultkeys
Beispiel #4
0
def _load_cp_picklefile(hatid):
    '''
    For the HAT LC corresponding to above sqlitecurve, read in the results from
    periodicty analysis of 02_get_EB_checkplots.py.
    '''
    #TODO: fix varperiod/varepoch to actually be read for general case

    ## Loading steps to get checkplot dictionary. N.b. this is _already normalized_ mags
    detachedEBpath = '../data/detachedEBs/checkplots/checkplot-'
    ptail = '.pkl.gz'

    cpd = checkplot._read_checkplot_picklefile(detachedEBpath + hatid + ptail)
    ## checkplot dictionary should have varperiod and varepoch
    cpd['varinfo']['varperiod'] = 2.101282
    cpd['varinfo']['varepoch'] = 53956.94080
    # Results from EB periodicity analysis:
    varperiod = cpd['varinfo']['varperiod']
    varepoch = cpd['varinfo']['varepoch']

    return cpd, varperiod, varepoch
Beispiel #5
0
def _subtract_median_EB_signal(hatid, varperiod, varepoch):
    '''
    Once injected planet periodograms have been run, get the median EB
    signal and subtract it to get residuals.

    Args:
        hatid (str): HAT-XXX-XXXXXXX
        varperiod (float): best EB period from PDM in periodicity analysis
        varepoch (float): epoch of folded LC
    Returns:
        residualmags (np array): data + injection - median model
    '''
    #TODO Propagate errors properly.

    #confirm EB period.
    injEBpath = '../data/detachedEBs/injs/checkplot-inj-'
    ptail = '.pkl.gz'
    cpd = checkplot._read_checkplot_picklefile(injEBpath + hatid + ptail)

    #unphased LC times, mags, errs
    times = cpd['magseries']['times']
    mags = cpd['magseries']['mags']
    errs = cpd['magseries']['errs']
    #phased data, then binned data (blue points)
    phase, phasedmags = cpd['pdm'][0]['phase'], cpd['pdm'][0]['phasedmags']
    binphase, binphasedmags = cpd['pdm'][0]['binphase'], cpd['pdm'][0][
        'binphasedmags']

    twophasetimes = varepoch + (
        binphase * varperiod
    )  #times over two phases (stored in binphase originally)

    #now extend twophasetimes to beginning and end of baseline
    modeltimes = np.array(twophasetimes)
    modelmags = binphasedmags

    exitf, upf, downf = False, True, True
    diff = modeltimes[-1] - modeltimes[0]

    while not exitf:

        while upf:
            onetimeup = np.array(modeltimes[-1] - modeltimes[0] +
                                 twophasetimes)[1:]
            onemagup = np.array(binphasedmags[1:])
            #oneerrup = np.array() #obnoxiously, these aren't kept thru the phase curve-making. should be needed to propagate uncertainties

            modeltimes = np.append(modeltimes, onetimeup)
            modelmags = np.append(modelmags, onemagup)
            if max(modeltimes) > max(times):
                upf = False

        k = 1
        while downf:
            #careful of lengths
            onetimedown = np.array(-k * diff + twophasetimes)[:-1]
            onemagdown = np.array(binphasedmags[:-1])
            #oneerrup = np.array() #not kept thru phase curve making

            modeltimes = np.insert(modeltimes, 0, onetimedown)
            modelmags = np.insert(modelmags, 0, onemagdown)

            k += 1

            if min(modeltimes) < min(times):
                downf = False
                exitf = True

    f = interp1d(modeltimes, modelmags, kind='linear')

    interpmodelmags = f(times)

    residualmags = mags - interpmodelmags  # n.b. these mags are the injected ones

    #save figure showing model and residuals
    plt.close('all')
    f, axs = plt.subplots(3, sharex=True, figsize=(15, 8 * 1.5))
    axs[0].scatter(times, mags, lw=0, c='black', s=10)
    axs[0].set(ylabel='DEB data (w/ planet injected)')
    axs[1].scatter(times, interpmodelmags, lw=0, c='black', s=10)
    axs[1].set(
        ylabel='median model\n(medians from binned phase-folded DEB LC)')
    axs[2].scatter(times, residualmags, lw=0, c='black', s=10)
    axs[2].set(ylabel='data - model')
    f.tight_layout()
    f.savefig(
        '../data/detachedEBs/plots/4_median_EB_modelLC_and_residuals.png',
        dpi=150)
    plt.close('all')
    print('\nMade figure of median EB model and residuals...\n')

    return times, residualmags, errs