def loss_function(args):


    tau_mean, const_mean, tage_mean, fburst_mean, tburst_mean, logzsol_mean, gas_logz_mean, gas_logu_mean,\
        tau_sig, const_sig, tage_sig, fburst_sig, tburst_sig, logzsol_sig, gas_logz_sig, gas_logu_sig = args

    set_size = 3000

    tau_arr = [
        float(
            priors.ClippedNormal(mean=abs(tau_mean),
                                 sigma=abs(tau_sig),
                                 mini=1.0,
                                 maxi=8.0).sample()) for _ in range(set_size)
    ]

    const_arr = [
        float(
            priors.ClippedNormal(mean=abs(const_mean),
                                 sigma=abs(const_sig),
                                 mini=0.0,
                                 maxi=0.5).sample()) for _ in range(set_size)
    ]

    tage_arr = [
        float(
            priors.ClippedNormal(mean=abs(tage_mean),
                                 sigma=abs(tage_sig),
                                 mini=1.0,
                                 maxi=11.0).sample()) for _ in range(set_size)
    ]

    fburst_arr = [
        float(
            priors.ClippedNormal(mean=abs(fburst_mean),
                                 sigma=abs(fburst_sig),
                                 mini=0.0,
                                 maxi=0.8).sample()) for _ in range(set_size)
    ]

    tburst_arr = [
        float(
            priors.ClippedNormal(mean=abs(tburst_mean),
                                 sigma=abs(tburst_sig),
                                 mini=0.0,
                                 maxi=abs(tage_mean)).sample())
        for _ in range(set_size)
    ]

    logzsol_arr = [
        float(
            priors.ClippedNormal(mean=-1 * abs(logzsol_mean),
                                 sigma=abs(logzsol_sig),
                                 mini=-1.5,
                                 maxi=0.0).sample()) for _ in range(set_size)
    ]

    gas_logz_arr = [
        float(
            priors.ClippedNormal(mean=-1 * abs(gas_logz_mean),
                                 sigma=abs(gas_logz_sig),
                                 mini=-1.5,
                                 maxi=0.0).sample()) for _ in range(set_size)
    ]

    gas_logu_arr = [
        float(
            priors.ClippedNormal(mean=-1 * abs(gas_logu_mean),
                                 sigma=abs(gas_logu_sig),
                                 mini=-4.0,
                                 maxi=-1.0).sample()) for _ in range(set_size)
    ]

    # Fix the fburst + const > 1 issue
    for ii in np.arange(len(const_arr)):
        if const_arr[ii] + fburst_arr[ii] >= 0.95:
            f_over = (const_arr[ii] + fburst_arr[ii]) - 0.95
            if fburst_arr[ii] >= (f_over + 0.01):
                fburst_arr[ii] = fburst_arr[ii] - (f_over + 0.01)
            else:
                const_arr[ii] = const_arr[ii] - (f_over + 0.01)

    # Fixed the rest
    dust1_arr = np.full(set_size, 0.1)
    dust2_arr = np.full(set_size, 0.0)
    sf_trunc_arr = np.full(set_size, 0.0)

    # List of model parameters
    dwarf_sample_parameters = [{
        'dust1': dust1_arr[ii],
        'dust2': dust2_arr[ii],
        'logzsol': logzsol_arr[ii],
        'gas_logz': gas_logz_arr[ii],
        'gas_logu': gas_logu_arr[ii],
        'const': const_arr[ii],
        'tau': tau_arr[ii],
        'tage': tage_arr[ii],
        'sf_trunc': sf_trunc_arr[ii],
        'fburst': fburst_arr[ii],
        'tburst': tburst_arr[ii]
    } for ii in np.arange(set_size)]

    # Double check
    for ii, model in enumerate(dwarf_sample_parameters):
        if model['fburst'] + model['const'] >= 0.99:
            print(ii, model['fburst'], model['const'])

    # Initialize the spop model
    spop_tau = setup_fsps_spop(zcontinuous=1,
                               imf_type=2,
                               sfh=1,
                               dust_type=0,
                               dust_index=-1.3,
                               dust1_index=-1.0)

    # Get the SDSS filters
    sdss_bands = fsps.find_filter('SDSS')

    dwarf_sample_gaussian = generate_dwarf_population(spop_tau,
                                                      dwarf_sample_parameters,
                                                      filters=sdss_bands,
                                                      n_jobs=6)

    # Measure colors and emission line EWs
    # - SDSS_EMLINES is a pre-defined dict of emission lines center wavelength and the
    # wavelength window for measuring EW.
    # - You can save the results in a numpy array
    dwarf_sample_table = measure_color_ew(dwarf_sample_gaussian,
                                          em_list=SDSS_EMLINES,
                                          output=None)

    bin_size = 200

    ur_size = np.linspace(0.0, 2.5, bin_size)
    ug_size = np.linspace(0.0, 2.0, bin_size)
    gr_size = np.linspace(-0.1, 0.8, bin_size)
    gi_size = np.linspace(-0.2, 1.3, bin_size)
    ha_size = np.linspace(0.0, 3.0, bin_size)
    hb_size = np.linspace(-0.5, 2.5, bin_size)
    oiii_size = np.linspace(-1.0, 3.0, bin_size)

    obs_ur = data_to_distribution(
        np.asarray(sdss_use['M_u'] - sdss_use['M_r']), ur_size)
    obs_ug = data_to_distribution(
        np.asarray(sdss_use['M_u'] - sdss_use['M_g']), ug_size)
    obs_gr = data_to_distribution(
        np.asarray(sdss_use['M_g'] - sdss_use['M_r']), gr_size)
    obs_gi = data_to_distribution(
        np.asarray(sdss_use['M_g'] - sdss_use['M_i']), gi_size)
    obs_ha = data_to_distribution(np.log10(abs(sdss_use['H_ALPHA_EQW'])),
                                  ha_size)
    obs_hb = data_to_distribution(np.log10(abs(sdss_use['H_BETA_EQW'])),
                                  hb_size)
    obs_oiii = data_to_distribution(np.log10(abs(sdss_use['OIII_5007_EQW'])),
                                    oiii_size)

    model_ur = data_to_distribution(dwarf_sample_table['ur_color'], ur_size)
    model_ug = data_to_distribution(dwarf_sample_table['ug_color'], ug_size)
    model_gr = data_to_distribution(dwarf_sample_table['gr_color'], gr_size)
    model_gi = data_to_distribution(dwarf_sample_table['gi_color'], gi_size)
    model_ha = data_to_distribution(
        np.log10(abs(dwarf_sample_table['ew_halpha'])), ha_size)
    model_hb = data_to_distribution(
        np.log10(abs(dwarf_sample_table['ew_hbeta'])), hb_size)
    model_oiii = data_to_distribution(
        np.log10(abs(dwarf_sample_table['ew_oiii_5007'])), oiii_size)

    obs_stack = np.transpose(
        np.vstack([obs_ur, obs_ug, obs_gr, obs_gi, obs_ha, obs_hb, obs_oiii]))

    model_stack = np.transpose(
        np.vstack([
            model_ur, model_ug, model_gr, model_gi, model_ha, model_hb,
            model_oiii
        ]))

    total_loss = entropy(obs=obs_stack, model=model_stack)

    return total_loss
Esempio n. 2
0
def loss_function(args):

    tau_mean, const_mean, tage_mean, fburst_mean, tburst_mean, logzsol_mean, gas_logz_mean, gas_logu_mean = args

    set_size = 3000

    tau_arr = [
        float(
            priors.ClippedNormal(mean=tau_mean,
                                 sigma=tau_sig,
                                 mini=1.0,
                                 maxi=8.0).sample()) for _ in range(set_size)
    ]
    const_arr = [
        float(
            priors.ClippedNormal(mean=const_mean,
                                 sigma=const_sig,
                                 mini=0.0,
                                 maxi=0.5).sample()) for _ in range(set_size)
    ]
    tage_arr = [
        float(
            priors.ClippedNormal(mean=tage_mean,
                                 sigma=tage_sig,
                                 mini=1.0,
                                 maxi=11.0).sample()) for _ in range(set_size)
    ]
    fburst_arr = [
        float(
            priors.ClippedNormal(mean=fburst_mean,
                                 sigma=fbust_sig,
                                 mini=0.0,
                                 maxi=0.8).sample()) for _ in range(set_size)
    ]
    tburst_arr = [
        float(
            priors.ClippedNormal(mean=tburst_mean,
                                 sigma=tburst_sig,
                                 mini=0.0,
                                 maxi=8.0).sample()) for _ in range(set_size)
    ]
    logzsol_arr = [
        float(
            priors.ClippedNormal(mean=logzsol_mean,
                                 sigma=logzsol_sig,
                                 mini=-1.5,
                                 maxi=0.0).sample()) for _ in range(set_size)
    ]
    gas_logz_arr = [
        float(
            priors.ClippedNormal(mean=gas_logz_mean,
                                 sigma=gas_logz_sig,
                                 mini=-1.5,
                                 maxi=0.0).sample()) for _ in range(set_size)
    ]
    gas_logu_arr = [
        float(
            priors.ClippedNormal(mean=gas_logu_mean,
                                 sigma=gas_logu_sig,
                                 mini=-4.0,
                                 maxi=-1.0).sample()) for _ in range(set_size)
    ]

    # Fix the fburst + const > 1 issue
    for ii in np.arange(len(const_arr)):
        if const_arr[ii] + fburst_arr[ii] >= 0.95:
            f_over = (const_arr[ii] + fburst_arr[ii]) - 0.95
            if fburst_arr[ii] >= (f_over + 0.01):
                fburst_arr[ii] = fburst_arr[ii] - (f_over + 0.01)
            else:
                const_arr[ii] = const_arr[ii] - (f_over + 0.01)

    # Fixed the rest
    dust1_arr = np.full(set_size, 0.1)
    dust2_arr = np.full(set_size, 0.0)
    sf_trunc_arr = np.full(set_size, 0.0)

    # List of model parameters
    dwarf_sample_parameters = [{
        'dust1': dust1_arr[ii],
        'dust2': dust2_arr[ii],
        'logzsol': logzsol_arr[ii],
        'gas_logz': gas_logz_arr[ii],
        'gas_logu': gas_logu_arr[ii],
        'const': const_arr[ii],
        'tau': tau_arr[ii],
        'tage': tage_arr[ii],
        'sf_trunc': sf_trunc_arr[ii],
        'fburst': fburst_arr[ii],
        'tburst': tburst_arr[ii]
    } for ii in np.arange(set_size)]

    # Double check
    for ii, model in enumerate(dwarf_sample_parameters):
        if model['fburst'] + model['const'] >= 0.99:
            print(ii, model['fburst'], model['const'])

    # Initialize the spop model
    spop_tau = setup_fsps_spop(zcontinuous=1,
                               imf_type=2,
                               sfh=1,
                               dust_type=0,
                               dust_index=-1.3,
                               dust1_index=-1.0)

    # Get the SDSS filters
    sdss_bands = fsps.find_filter('SDSS')

    dwarf_sample_gaussian = generate_dwarf_population(spop_tau,
                                                      dwarf_sample_parameters,
                                                      filters=sdss_bands,
                                                      n_jobs=4)

    # Measure colors and emission line EWs
    # - SDSS_EMLINES is a pre-defined dict of emission lines center wavelength and the
    # wavelength window for measuring EW.
    # - You can save the results in a numpy array
    dwarf_sample_table = measure_color_ew(dwarf_sample_gaussian,
                                          em_list=SDSS_EMLINES,
                                          output=None)

    bin_size = 200

    ur_loss = loss(dwarf_sample_table['ur_color'],
                   np.asarray(sdss_use['M_u'] - sdss_use['M_r']),
                   np.linspace(0, 2.5, bin_size))
    ug_loss = loss(dwarf_sample_table['ug_color'],
                   np.asarray(sdss_use['M_u'] - sdss_use['M_g']),
                   np.linspace(0, 1.75, bin_size))
    gr_loss = loss(dwarf_sample_table['gr_color'],
                   np.asarray(sdss_use['M_g'] - sdss_use['M_r']),
                   np.linspace(-0.1, 0.8, bin_size))
    gi_loss = loss(dwarf_sample_table['gi_color'],
                   np.asarray(sdss_use['M_g'] - sdss_use['M_i']),
                   np.linspace(-0.2, 1.2, bin_size))
    OIII_loss = loss(np.log10(dwarf_sample_table['ew_oiii_5007']),
                     np.log10(-1.0 * (sdss_use['OIII_5007_EQW'])),
                     np.linspace(-1, 3, bin_size))
    Ha_loss = loss(np.log10(np.log10(dwarf_sample_table['ew_halpha'])),
                   np.log10(-1.0 * sdss_use['H_ALPHA_EQW']),
                   np.linspace(0, 3, bin_size))
    Hb_loss = loss(np.log10(dwarf_sample_table['ew_hbeta']),
                   np.log10(-1.0 * sdss_use['H_BETA_EQW']),
                   np.linspace(-0.5, 2.5, bin_size))

    total_loss = (ur_loss + ug_loss + gr_loss + gi_loss + OIII_loss + Ha_loss +
                  Hb_loss) / 7

    part_loss = [
        ur_loss, ug_loss, gr_loss, gi_loss, OIII_loss, Ha_loss, Hb_loss
    ]

    return total_loss