Пример #1
0
def generate_synthetic(spectrum):
    ### Generates the best synth spectrum, given the KDE MCMC params

    ### grab those params
    ### NEED TO FINISH!!!!!!!!!!!!!!!!!

    SYNTH_FLUX = INTERPOLATOR[spectrum.get_gravity_class()](spectrum.MCMC_COARSE['TEFF'][0],
                                                            spectrum.MCMC_REFINE['FEH'][0],
                                                            spectrum.MCMC_REFINE['CFE'][0])


    spectrum.set_synth_spectrum(pd.DataFrame({'wave' : SYNTH_WAVE, 'norm' : SYNTH_FLUX.T}))


    return
Пример #2
0
def mcmc_determination(spectrum, mode='COARSE', pool=4):

    ### Precondition: must have run archetype_classification
    ### spectrum: spectrum.Spectrum() object

    # mode here means either "coarse" or "fine" , 09/02/2020, J. Yoon
    print("\t MCMC run mode =  ", mode)
    # spectrum and its info
    print('\t ' + spectrum.get_name().ljust(20) + ":  " + spectrum.get_gravity_class() + " : " + spectrum.get_carbon_mode() + " : " + spectrum.print_KP_bounds())


    ## FOR initial FEH and CFE values, for temp use photometric temp.
    PARAMS = ARCHETYPE_PARAMS[spectrum.get_environ_mode()][spectrum.get_arch_group()]

    #### MAIN MODE BRANCH

    if mode=='COARSE':
        ## if it's coarse, then you need the photometric teff and the Sigma/Xi
        print('\t initializing with archetype parameters: ', PARAMS)
        photo_teff = spectrum.get_photo_temp()
        # inserted 01/04/2022 for comparison J.Yoon
        print('Teff : %.0F  [Fe/H] : %.2F   [C/Fe] : %.2F  A(C): %.2F'% (photo_teff[0], PARAMS['FEH'], PARAMS['CFE'], PARAMS['AC']))
        initial = [photo_teff[0], PARAMS['FEH'], PARAMS['CFE']]

        ARGS = (spectrum.regions, SYNTH_WAVE, photo_teff[0], photo_teff[1],
                spectrum.get_SN_dict(), spectrum.get_gravity_class())

        initial = np.concatenate([initial,
                                 [spectrum.SN_DICT['CA']['XI_AVG'],
                                  spectrum.SN_DICT['CH']['XI_AVG']]])



        if spectrum.get_carbon_mode() == "CH+C2":
            print("\t running with carbon mode: CH+C2")

            ### add the beta params
            initial = np.concatenate([initial,
                                     [spectrum.SN_DICT['C2']['XI_AVG']]])
        else: print("\t running with carbon mode: CH only")

    elif mode == 'REFINE':
        ### In this case we want to use the params determined from the COARSE run
        PARAMS_0 = spectrum.get_mcmc_dict(mode = 'COARSE')
        print('\t initializing with COARSE run result parameters:')
        print(' Teff : %.0F  [Fe/H] : %.2F   [C/Fe] : %.2F   A(C) : %.2F' % (PARAMS_0['TEFF'][0],PARAMS_0['FEH'][0], PARAMS_0['CFE'][0],PARAMS_0['AC'][0]) )

        ARGS = (spectrum.regions, SYNTH_WAVE,
                PARAMS_0, spectrum.get_gravity_class())

        initial = [spectrum.MCMC_COARSE['FEH'][0], spectrum.MCMC_COARSE['CFE'][0]]

    else:
        print("Invalid mode")

    ############################################################################
    ### PREPARE SPECTRA SLICES
    ############################################################################

    ### Select the correct likelihood function
    LL_FUNCTION = LL_FUNCTION_DICT[mode][spectrum.get_carbon_mode()]

    #if pool == 'MAX':
        #cpu_cores = cpu_count()

    #else:
    #    cpu_cores = pool

    #print("\t running on ", cpu_cores, " cores")
    #pool_init = Pool(cpu_cores)

    pos = initial + initial * (2e-2*np.random.rand(25, len(initial)))
    nwalkers, ndim = pos.shape
    bounds = 'default'


    print("\t running for ", spectrum.get_MCMC_iterations(), " iterations...")


    sampler = emcee.EnsembleSampler(nwalkers, ndim,
                                    LL_FUNCTION,
                                    args=(ARGS))
    ####

    _ = sampler.run_mcmc(pos, spectrum.get_MCMC_iterations())
    # want to print out the latest result from mcmc, 12/13/2021
    #print('\t the latest result from sampler() after MCMC runs:    ', _ )

    spectrum.set_sampler(sampler, mode=mode)

    print("\t\t mcmc mode = ", mode)

    return
Пример #3
0
def mcmc_determination(spectrum, mode='COARSE'):

    ### Precondition: must have run archetype_classification
    ### spectrum: spectrum.Spectrum() object

    print('\t ' + spectrum.get_name().ljust(20) + ":  " +
          spectrum.get_gravity_class() + " : " + spectrum.get_carbon_mode() +
          " : " + spectrum.print_KP_bounds())

    try:
        interp = INTERPOLATOR[spectrum.get_gravity_class()]
    except:
        print("Error in mcmc_determination with gravity class:  ",
              spectrum.get_gravity_class())

    ## FOR initial FEH and CFE values, for temp use photo
    PARAMS = ARCHETYPE_PARAMS[spectrum.get_environ_mode()][
        spectrum.get_arch_group()]
    print('\t initializing with archetype parameters: ', PARAMS)

    #### MAIN MODE BRANCH

    if mode == 'COARSE':
        ## if it's coarse, then you need the photo teff and the Sigma/Xi

        photo_teff = spectrum.get_photo_temp()
        initial = [photo_teff[0], PARAMS['FEH'], PARAMS['CFE']]

        ARGS = (spectrum.regions, interp, SYNTH_WAVE, photo_teff[0],
                photo_teff[1], spectrum.get_SN_dict())

        initial = np.concatenate([
            initial,
            [
                spectrum.SN_DICT['CA']['XI_AVG'],
                spectrum.SN_DICT['CH']['XI_AVG']
            ]
        ])

        if spectrum.get_carbon_mode() == "CH+C2":
            print("\t running with carbon mode: C2")

            ### add the beta params
            initial = np.concatenate(
                [initial, [spectrum.SN_DICT['C2']['XI_AVG']]])

    elif mode == 'REFINE':
        ### In this case we want to use the params determined from the COARSE run
        PARAMS_0 = spectrum.get_mcmc_dict(mode='COARSE')

        ARGS = (spectrum.regions, interp, SYNTH_WAVE, PARAMS_0)

        initial = [
            spectrum.MCMC_COARSE['FEH'][0], spectrum.MCMC_COARSE['CFE'][0]
        ]

    else:
        print("Invalid mode")

    ############################################################################
    ### PREPARE SPECTRA SLICES
    ############################################################################

    ### Select the correct likelihood function
    LL_FUNCTION = LL_FUNCTION_DICT[mode][spectrum.get_carbon_mode()]
    cpu_cores = cpu_count()
    pool = Pool(cpu_cores)
    print("\t running on ", cpu_cores, " cores")

    pos = initial + initial * (2e-2 * np.random.rand(25, len(initial)))
    nwalkers, ndim = pos.shape
    bounds = 'default'

    print("\t running for ", spectrum.get_MCMC_iterations(), " iterations...")

    sampler = emcee.EnsembleSampler(nwalkers,
                                    ndim,
                                    LL_FUNCTION,
                                    args=(ARGS),
                                    pool=pool)
    ####

    _ = sampler.run_mcmc(pos, spectrum.get_MCMC_iterations())

    spectrum.set_sampler(sampler, mode=mode)

    print()

    return