Exemplo n.º 1
0
def get_mcmc_params(SAMPLER, burnin=0.25, return_kde=False):
    print("DEPRECATED FUNCTION")
    #ndim = SAMPLER.chain.shape[2]
    #SAMPLES = SAMPLER.chain[:, burnin:, :].reshape((-1, ndim))

    try:  ### SAMPLER is chain

        ndim = SAMPLER.shape[2]
        iter = SAMPLER.shape[1]

    except:
        SAMPLER = SAMPLER.chain
        ndim = SAMPLER.shape[2]
        iter = SAMPLER.shape[1]



    SAMPLES = SAMPLER[:, int(burnin * iter):, :].reshape((-1, ndim))

    MEDIAN = [np.median(array) for array in SAMPLES.T]

    STD =    [MAD.S_MAD(array) for array in SAMPLES.T]

    #modes1 = [mode(np.around(row, decimals = rounding))[0][0] for row, rounding in zip(SAMPLES.T, [0,1,1,3,3, 3])]
    #modes2 = [mode(np.around(row, decimals = rounding))[0][0] for row, rounding in zip(SAMPLES.T, [0,2,2,3,3, 3])]

    ### Let's use the kde_params
    kde_array = [kde_param(row, x0 = x0)['kde'] for row, x0 in zip(SAMPLES.T, MEDIAN)]
    value2 = [kde_param(row, x0 = x0)['result'] for row, x0 in zip(SAMPLES.T, MEDIAN)]

    if ndim == 2:
        dict_keys = ['feh', 'cfe']

    if ndim == 5:
        dict_keys = ['teff', 'feh', 'cfe', 'XI_CA', 'XI_CH']

    elif ndim == 6:
        dict_keys = ['teff', 'feh', 'cfe', 'XI_CA', 'XI_CH', 'XI_C2']


    OUTPUT = {key : [value2[i], MAD.S_MAD(SAMPLES[:, i])] for i, key in enumerate(dict_keys)}

    OUTPUT['AC'] = [ac.ac(OUTPUT['cfe'][0], OUTPUT['feh'][0]), np.sqrt(OUTPUT['cfe'][1]**2 + OUTPUT['feh'][1]**2)]

    KDE_DICT = {key : kde for key, kde in zip(dict_keys, kde_array)}

    if return_kde:
        return OUTPUT, KDE_DICT

    else:
        return OUTPUT
Exemplo n.º 2
0
def get_mcmc_params(SAMPLER, burnin):
    SAMPLES = SAMPLER[0].chain[:, burnin:, :].reshape((-1, SAMPLER[1]))

    MEDIAN = [np.median(array) for array in SAMPLES.T]

    STD = [MAD.S_MAD(array) for array in SAMPLES.T]

    modes1 = [
        mode(np.around(row, decimals=rounding))[0][0]
        for row, rounding in zip(SAMPLES.T, [0, 1, 1, 3, 3, 3])
    ]
    modes2 = [
        mode(np.around(row, decimals=rounding))[0][0]
        for row, rounding in zip(SAMPLES.T, [0, 2, 2, 3, 3, 3])
    ]

    ### Let's use the kde_params
    value2 = [
        kde_param(row, x0=x0)['result'] for row, x0 in zip(SAMPLES.T, MEDIAN)
    ]

    OUTPUT = {
        'teff': [value2[0], MAD.S_MAD(SAMPLES[:, 0])],
        'feh': [value2[1], MAD.S_MAD(SAMPLES[:, 1])],
        'carbon': [value2[2], MAD.S_MAD(SAMPLES[:, 2])],
        'sigmaCAII': [value2[3], MAD.S_MAD(SAMPLES[:, 3])],
        'sigmaCH': [value2[4], MAD.S_MAD(SAMPLES[:, 4])],
        'sigmaC2': [value2[5], MAD.S_MAD(SAMPLES[:, 5])]
    }

    return OUTPUT
Exemplo n.º 3
0
def generate_kde_params(spectrum, mode, burnin=0.25):
    ### main parameter extraction routine following mcmc determination
    ### get chain
    if mode == 'COARSE':
        chain = spectrum.MCMC_COARSE_sampler.chain

    elif mode == 'REFINE':
        chain = spectrum.MCMC_REFINE_sampler.chain

    walkers, iter, ndim = chain.shape

    ### merge walkers and iterations for formatted chain
    chain = chain[:, int(burnin * iter):, :].reshape((-1, ndim))

    ### basic medians
    MEDIAN = [np.median(array) for array in chain.T]
    STD = [np.std(array) for array in chain.T]

    ### Let's use the kde_params
    ### Note: kde is highly susceptible to errors at the boundaries of the grid
    ### I'm going to try a solution involving edge reflection

    results = [kde_param_relection(array) for array in chain.T]

    if ndim == 2:
        dict_keys = ['FEH', 'CFE']

    if ndim == 5:
        dict_keys = ['TEFF', 'FEH', 'CFE', 'XI_CA', 'XI_CH']

    elif ndim == 6:
        dict_keys = ['TEFF', 'FEH', 'CFE', 'XI_CA', 'XI_CH', 'XI_C2']

    ### build outputs
    OUTPUT = {
        key: [results[i]['result'],
              MAD.S_MAD(chain[:, i])]
        for i, key in enumerate(dict_keys)
    }

    OUTPUT['AC'] = [
        ac.ac(OUTPUT['CFE'][0], OUTPUT['FEH'][0]),
        np.sqrt(OUTPUT['CFE'][1]**2 + OUTPUT['FEH'][1]**2)
    ]

    KDE_DICT = {
        key: [element['kde'], element['kde_reflect']]
        for key, element in zip(dict_keys, results)
    }

    spectrum.set_mcmc_results(OUTPUT, mode=mode)
    spectrum.set_kde_functions(KDE_DICT, mode=mode)

    return
Exemplo n.º 4
0
    def estimate_sn(self):

        #### determines the first guess SN estimates for each region of interest
        #### defines SN_DICT member variable

        ### might define SIDEBANDS in another file at some point
        SIDEBANDS = {'CA' : [[3884, 3923], [3995, 4045]],
                     'CH' : [[4000, 4080], [4440, 4500]],
                     'C2' : [[4500, 4600], [4760, 4820]]}

        self.SN_DICT = {key : [] for key in SIDEBANDS.keys()}
        for key in SIDEBANDS.keys():

            if (SIDEBANDS[key][0][0] > min(self.frame['wave'])) and (SIDEBANDS[key][1][1] < max(self.frame['wave'])):

                SN_LEFT  = np.sqrt(self.frame['flux'][self.frame['wave'].between(*SIDEBANDS[key][0], inclusive=True)])
                SN_RIGHT = np.sqrt(self.frame['flux'][self.frame['wave'].between(*SIDEBANDS[key][1], inclusive=True)])

                ### Average the left and right sidebands

                self.SN_DICT[key] = {'SN_AVG' : np.mean([np.median(SN_LEFT), np.median(SN_RIGHT)]),
                                     'SN_STD' : max([MAD.S_MAD(SN_LEFT), MAD.S_MAD(SN_RIGHT)]),
                                     'XI_AVG' : np.mean([np.median(np.divide(1., SN_LEFT)), np.median(np.divide(1. , SN_RIGHT))]),
                                     'XI_STD' : max([MAD.S_MAD(np.divide(1., SN_LEFT)) , MAD.S_MAD(np.divide(1., SN_RIGHT))])}

                #### parameters for the beta distribution prior
                self.SN_DICT[key]['alpha'] = ((self.SN_DICT[key]['XI_AVG']**2)/np.square(self.SN_DICT[key]['XI_STD']))*(1 - self.SN_DICT[key]['XI_AVG']) - self.SN_DICT[key]['XI_AVG']
                self.SN_DICT[key]['beta']  = (1/self.SN_DICT[key]['XI_AVG'] - 1) * self.SN_DICT[key]['alpha']

            else:
                print("band not in wavelength coverage")

                self.SN_DICT[key] = {'SN_AVG' : np.nan,
                                     'SN_STD' : np.nan,
                                     'XI_AVG' : np.nan,
                                     'XI_STD' : np.nan,
                                     'alpha'  : np.nan,
                                     'beta'   : np.nan}


        return