Exemple #1
0
def fit_spectrophotometry(igal,
                          sim='lgal',
                          noise='bgs0_legacy',
                          method='ifsps',
                          model='emulator',
                          nwalkers=100,
                          burnin=100,
                          niter='adaptive',
                          maxiter=200000,
                          opt_maxiter=100,
                          overwrite=False,
                          postprocess=False,
                          justplot=False):
    ''' Fit Lgal spectra. `noise` specifies whether to fit spectra without noise or 
    with BGS-like noise. Produces an MCMC chain and, if not on nersc, a corner plot of the posterior. 

    :param igal: 
        index of Lgal galaxy within the spectral_challenge 
    :param noise: 
        If 'bgs1'...'bgs8', fit BGS-like spectra. (default: 'none') 
    :param justplot: 
        If True, skip the fitting and plot the best-fit. This is mainly implemented 
        because I'm having issues plotting in NERSC. (default: False) 
    '''
    noise_spec = noise.split('_')[0]
    noise_photo = noise.split('_')[1]
    # read noiseless Lgal spectra of the spectral_challenge mocks
    specs, meta = Data.Spectra(sim=sim,
                               noise=noise_spec,
                               lib='bc03',
                               sample='mini_mocha')
    # read Lgal photometry of the mini_mocha mocks
    photo, _ = Data.Photometry(sim=sim,
                               noise=noise_photo,
                               lib='bc03',
                               sample='mini_mocha')

    if meta['redshift'][igal] < 0.101:
        # current Speculator wavelength doesn't extend far enough
        # current Speculator wavelength doesn't extend far enough
        # current Speculator wavelength doesn't extend far enough
        # current Speculator wavelength doesn't extend far enough
        # current Speculator wavelength doesn't extend far enough
        return None

    w_obs = specs['wave']
    flux_obs = specs['flux'][igal]
    ivar_obs = specs['ivar'][igal]
    if method == 'ispeculator':
        photo_obs = photo['flux'][igal, :3]
        photo_ivar_obs = photo['ivar'][igal, :3]
    else:
        photo_obs = photo['flux'][igal, :5]
        photo_ivar_obs = photo['ivar'][igal, :5]

    # get fiber flux factor prior range based on measured fiber flux
    f_fiber_true = (photo['fiberflux_r_true'][igal] /
                    photo['flux_r_true'][igal])
    prior_width = np.max([
        0.05,
        5. * photo['fiberflux_r_ivar'][igal]**-0.5 / photo['flux'][igal, 1]
    ])
    f_fiber_min = (
        photo['fiberflux_r_meas'][igal]) / photo['flux'][igal, 1] - prior_width
    f_fiber_max = (
        photo['fiberflux_r_meas'][igal]) / photo['flux'][igal, 1] + prior_width
    f_fiber_prior = [f_fiber_min, f_fiber_max]

    print('--- input ---')
    print('z = %f' % meta['redshift'][igal])
    print('log M* total = %f' % meta['logM_total'][igal])
    print('log M* fiber = %f' % meta['logM_fiber'][igal])
    print('f_fiber = %f' % f_fiber_true)
    print('log SFR 100myr = %f' % np.log10(meta['sfr_100myr'][igal]))
    print('log Z_MW = %f' % np.log10(meta['Z_MW'][igal]))

    f_mcmc = os.path.join(
        UT.dat_dir(), 'mini_mocha', method,
        '%s.specphoto.noise_%s.%s.%i.mcmc.hdf5' % (sim, noise, model, igal))

    if method == 'ifsps':
        ifitter = Fitters.iFSPS(model_name=model)
    elif method == 'ispeculator':
        ifitter = Fitters.iSpeculator(model_name=model)

    print('--- mcmc ---')
    if (justplot or not overwrite) and os.path.isfile(f_mcmc):
        print('     reading... %s' % os.path.basename(f_mcmc))
        # read in mcmc chain
        fmcmc = h5py.File(f_mcmc, 'r')
        mcmc = {}
        for k in fmcmc.keys():
            mcmc[k] = fmcmc[k][...]
        fmcmc.close()
    else:
        if os.path.isfile(f_mcmc) and not overwrite:
            print("** CAUTION: %s already exists **" %
                  os.path.basename(f_mcmc))
        print('     writing... %s' % os.path.basename(f_mcmc))
        # initiating fit
        prior = ifitter._default_prior(f_fiber_prior=f_fiber_prior)

        mcmc = ifitter.MCMC_spectrophoto(w_obs,
                                         flux_obs,
                                         ivar_obs,
                                         photo_obs,
                                         photo_ivar_obs,
                                         meta['redshift'][igal],
                                         mask='emline',
                                         prior=prior,
                                         nwalkers=nwalkers,
                                         burnin=burnin,
                                         niter=niter,
                                         maxiter=maxiter,
                                         opt_maxiter=opt_maxiter,
                                         writeout=f_mcmc,
                                         silent=False)

    if postprocess:
        print('--- postprocessing ---')
        f_post = f_mcmc.replace('.mcmc.hdf5', '.postproc.hdf5')
        mcmc = ifitter.postprocess(mcmc_output=mcmc, writeout=f_post)

    i_fib = list(mcmc['theta_names'].astype(str)).index('f_fiber')

    print('log M* total = %f' % mcmc['theta_med'][0])
    print('log M* fiber = %f' %
          (mcmc['theta_med'][0] + np.log10(mcmc['theta_med'][i_fib])))
    print('---------------')
    try:
        # plotting on nersc never works.
        if os.environ['NERSC_HOST'] == 'cori': return None
    except KeyError:

        labels = [lbl_dict[_t] for _t in mcmc['theta_names'].astype(str)]
        truths = [None for _ in labels]
        truths[0] = meta['logM_total'][igal]
        truths[i_fib] = f_fiber_true
        if postprocess:
            i_sfr = list(
                mcmc['theta_names'].astype(str)).index('logsfr.100myr')
            truths[i_sfr] = np.log10(meta['sfr_100myr'][igal])
            i_zw = list(mcmc['theta_names'].astype(str)).index('logz.mw')
            truths[i_zw] = np.log10(meta['Z_MW'][igal])
            print('log SFR 100myr = %f' %
                  np.median(mcmc['mcmc_chain'][:, i_sfr]))
            print('log Z_MW = %f' % np.median(mcmc['mcmc_chain'][:, i_zw]))

        # corner plot of the posteriors
        fig = DFM.corner(mcmc['mcmc_chain'],
                         range=mcmc['prior_range'],
                         quantiles=[0.16, 0.5, 0.84],
                         levels=[0.68, 0.95],
                         nbin=40,
                         smooth=True,
                         truths=truths,
                         labels=labels,
                         label_kwargs={'fontsize': 20})
        if postprocess:
            fig.savefig(f_post.replace('.hdf5', '.png'), bbox_inches='tight')
        else:
            fig.savefig(f_mcmc.replace('.hdf5', '.png'), bbox_inches='tight')
        plt.close()

        fig = plt.figure(figsize=(18, 5))
        gs = mpl.gridspec.GridSpec(1,
                                   2,
                                   figure=fig,
                                   width_ratios=[1, 3],
                                   wspace=0.2)
        sub = plt.subplot(gs[0])
        sub.errorbar(np.arange(len(photo_obs)),
                     photo_obs,
                     yerr=photo_ivar_obs**-0.5,
                     fmt='.k',
                     label='data')
        sub.scatter(np.arange(len(photo_obs)),
                    mcmc['flux_photo_model'],
                    c='C1',
                    label='model')
        sub.legend(loc='upper left',
                   markerscale=3,
                   handletextpad=0.2,
                   fontsize=15)
        sub.set_xticks([0, 1, 2, 3, 4])
        sub.set_xticklabels(['$g$', '$r$', '$z$', 'W1', 'W2'])
        sub.set_xlim(-0.5, len(photo_obs) - 0.5)

        sub = plt.subplot(gs[1])
        sub.plot(w_obs, flux_obs, c='k', lw=1, label='data')
        sub.plot(mcmc['wavelength_model'],
                 mcmc['flux_spec_model'],
                 c='C1',
                 ls='--',
                 lw=1,
                 label=method)
        sub.legend(loc='upper right', fontsize=15)
        sub.set_xlabel('wavelength [$A$]', fontsize=20)
        sub.set_xlim(3600., 9800.)
        sub.set_ylim(-1., 5.)

        if postprocess:
            fig.savefig(f_post.replace('.hdf5', '.bestfit.png'),
                        bbox_inches='tight')
        else:
            fig.savefig(f_mcmc.replace('.hdf5', '.bestfit.png'),
                        bbox_inches='tight')
        plt.close()
    return None
Exemple #2
0
def fit_photometry(igal,
                   sim='lgal',
                   noise='legacy',
                   method='ifsps',
                   model='emulator',
                   nwalkers=100,
                   burnin=100,
                   niter='adaptive',
                   maxiter=200000,
                   opt_maxiter=100,
                   overwrite=False,
                   postprocess=False,
                   justplot=False):
    ''' Fit simulated photometry. `noise` specifies whether to fit spectra without noise or 
    with legacy-like noise. `dust` specifies whether to if spectra w/ dust or not. 
    Produces an MCMC chain and, if not on nersc, a corner plot of the posterior. 

    :param igal: 
        index of Lgal galaxy within the spectral_challenge 

    :param noise: 
        If 'none', fit noiseless photometry. 
        If 'legacy', fit Legacy-like photometry. (default: 'none') 

    :param justplot: 
        If True, skip the fitting and plot the best-fit. This is mainly implemented 
        because I'm having issues plotting in NERSC. (default: False) 
    '''
    # read Lgal photometry of the mini_mocha mocks
    photo, meta = Data.Photometry(sim=sim,
                                  noise=noise,
                                  lib='bc03',
                                  sample='mini_mocha')

    if meta['redshift'][igal] < 0.101:
        # current Speculator wavelength doesn't extend far enough
        # current Speculator wavelength doesn't extend far enough
        # current Speculator wavelength doesn't extend far enough
        # current Speculator wavelength doesn't extend far enough
        # current Speculator wavelength doesn't extend far enough
        return None

    print('--- input ---')
    print('z = %f' % meta['redshift'][igal])
    print('log M* total = %f' % meta['logM_total'][igal])
    print('log SFR 100myr = %f' % np.log10(meta['sfr_100myr'][igal]))
    print('log Z MW = %f' % np.log10(meta['Z_MW'][igal]))

    if method == 'ispeculator':
        photo_obs = photo['flux'][igal, :3]
        ivar_obs = photo['ivar'][igal, :3]
    else:
        photo_obs = photo['flux'][igal, :5]
        ivar_obs = photo['ivar'][igal, :5]

    f_mcmc = os.path.join(
        UT.dat_dir(), 'mini_mocha', method,
        '%s.photo.noise_%s.%s.%i.mcmc.hdf5' % (sim, noise, model, igal))

    # initiating fit
    if method == 'ifsps':
        ifitter = Fitters.iFSPS(model_name=model)
    elif method == 'ispeculator':
        ifitter = Fitters.iSpeculator(model_name=model)

    print('--- bestfit ---')
    if (justplot or not overwrite) and os.path.isfile(f_mcmc):
        # read in best-fit file with mcmc chain
        print('    reading in %s' % f_mcmc)
        fmcmc = h5py.File(f_mcmc, 'r')
        mcmc = {}
        for k in fmcmc.keys():
            mcmc[k] = fmcmc[k][...]
        fmcmc.close()
    else:
        if os.path.isfile(f_mcmc) and not overwrite:
            print("** CAUTION: %s already exists **" % os.path.basename(f_bf))

        print('    writing %s' % f_mcmc)
        prior = ifitter._default_prior(f_fiber_prior=None)

        mcmc = ifitter.MCMC_photo(photo_obs,
                                  ivar_obs,
                                  meta['redshift'][igal],
                                  bands='desi',
                                  prior=prior,
                                  nwalkers=nwalkers,
                                  burnin=burnin,
                                  niter=niter,
                                  maxiter=maxiter,
                                  opt_maxiter=opt_maxiter,
                                  writeout=f_mcmc,
                                  silent=False)

    if postprocess:
        print('--- postprocessing ---')
        f_post = f_mcmc.replace('.mcmc.hdf5', '.postproc.hdf5')
        mcmc = ifitter.postprocess(mcmc_output=mcmc, writeout=f_post)

    print('log M* total = %f' % mcmc['theta_med'][0])
    print('---------------')

    try:
        # plotting on nersc never works.
        if os.environ['NERSC_HOST'] == 'cori': return None
    except KeyError:
        labels = [lbl_dict[_t] for _t in mcmc['theta_names'].astype(str)]
        truths = [None for _ in labels]
        truths[0] = meta['logM_total'][igal]
        if postprocess:
            i_sfr = list(
                mcmc['theta_names'].astype(str)).index('logsfr.100myr')
            truths[i_sfr] = np.log10(meta['sfr_100myr'][igal])
            i_zmw = list(mcmc['theta_names'].astype(str)).index('logz.mw')
            truths[i_zmw] = np.log10(meta['Z_MW'][igal])
            print('log SFR 100myr = %f' %
                  np.median(mcmc['mcmc_chain'][:, i_sfr]))
            print('log Z MW = %f' % np.median(mcmc['mcmc_chain'][:, i_zmw]))

        fig = DFM.corner(mcmc['mcmc_chain'],
                         range=mcmc['prior_range'],
                         quantiles=[0.16, 0.5, 0.84],
                         levels=[0.68, 0.95],
                         nbin=40,
                         smooth=True,
                         truths=truths,
                         labels=labels,
                         label_kwargs={'fontsize': 20})
        if postprocess:
            fig.savefig(f_post.replace('.hdf5', '.png'), bbox_inches='tight')
        else:
            fig.savefig(f_mcmc.replace('.hdf5', '.png'), bbox_inches='tight')
        plt.close()

        fig = plt.figure(figsize=(5, 3))
        sub = fig.add_subplot(111)
        sub.errorbar(np.arange(len(photo_obs)),
                     photo_obs,
                     yerr=ivar_obs**-0.5,
                     fmt='.k',
                     label='data')
        sub.scatter(np.arange(len(photo_obs)),
                    mcmc['flux_photo_model'],
                    c='C1',
                    label='model')
        sub.legend(loc='upper left',
                   markerscale=3,
                   handletextpad=0.2,
                   fontsize=15)
        sub.set_xticks([0, 1, 2])
        sub.set_xticklabels(['$g$', '$r$', '$z$'])
        sub.set_xlim(-0.5, len(photo_obs) - 0.5)
        if postprocess:
            fig.savefig(f_post.replace('.hdf5', '.bestfit.png'),
                        bbox_inches='tight')
        else:
            fig.savefig(f_mcmc.replace('.hdf5', '.bestfit.png'),
                        bbox_inches='tight')
        plt.close()
    return None
Exemple #3
0
def fit_spectrophotometry(igal, noise='none', nwalkers=100, burnin=100, niter=1000, overwrite=False, justplot=False): 
    ''' Fit Lgal spectra. `noise` specifies whether to fit spectra without noise or 
    with BGS-like noise. Produces an MCMC chain and, if not on nersc, a corner plot of the posterior. 

    :param igal: 
        index of Lgal galaxy within the spectral_challenge 

    :param noise: 
        If 'none', fit noiseless spectra. 
        If 'bgs1'...'bgs8', fit BGS-like spectra. (default: 'none') 

    :param justplot: 
        If True, skip the fitting and plot the best-fit. This is mainly implemented 
        because I'm having issues plotting in NERSC. (default: False) 
    '''
    if noise != 'none': 
        noise_spec = noise.split('_')[0]
        noise_photo = noise.split('_')[1]
    else: 
        noise_spec = 'none'
        noise_photo = 'none'
    # read noiseless Lgal spectra of the spectral_challenge mocks 
    specs, meta = Data.Spectra(sim='lgal', noise=noise_spec, lib='bc03', sample='mini_mocha') 
    
    # read Lgal photometry of the mini_mocha mocks 
    photo, _ = Data.Photometry(sim='lgal', noise=noise_photo, lib='bc03', sample='mini_mocha') 

    model       = 'vanilla'
    w_obs       = specs['wave']
    flux_obs    = specs['flux'][igal]
    if noise_spec != 'none': 
        ivar_obs = specs['ivar'][igal]
    else:  
        ivar_obs = np.ones(len(w_obs)) 
    photo_obs   = photo['flux'][igal,:5]
    if noise_photo != 'none': 
        photo_ivar_obs = photo['ivar'][igal,:5]
    else:  
        photo_ivar_obs = np.ones(photo_obs.shape[0]) 

    # get fiber flux factor prior range based on measured fiber flux 
    f_fiber_true = (photo['fiberflux_r_meas'][igal]/photo['flux_r_true'][igal]) 
    f_fiber_min = (photo['fiberflux_r_meas'][igal] - 3.*photo['fiberflux_r_ivar'][igal]**-0.5)/photo['flux'][igal,1]
    f_fiber_max = (photo['fiberflux_r_meas'][igal] + 3.*photo['fiberflux_r_ivar'][igal]**-0.5)/photo['flux'][igal,1]
    f_fiber_prior = [f_fiber_min, f_fiber_max]
    print(f_fiber_prior) 
    print(f_fiber_true) 
    print((photo['fiberflux_r_true'][igal]/photo['flux_r_true'][igal])) 

    truths      = [meta['logM_total'][igal], np.log10(meta['Z_MW'][igal]), meta['t_age_MW'][igal], None, None, f_fiber_true]
    labels      = ['$\log M_*$', '$\log Z$', r'$t_{\rm age}$', 'dust2', r'$\tau$', r'$f_{\rm fiber}$']

    print('--- input ---') 
    print('z = %f' % meta['redshift'][igal])
    print('log M* total = %f' % meta['logM_total'][igal])
    print('log M* fiber = %f' % meta['logM_fiber'][igal])
    print('MW Z = %f' % meta['Z_MW'][igal]) 
    print('MW tage = %f' % meta['t_age_MW'][igal]) 

    f_bf = os.path.join(UT.dat_dir(), 'mini_mocha', 'ifsps', 'lgal.specphoto.noise_%s.%s.%i.hdf5' % (noise, model, igal))
    if not justplot: 
        if os.path.isfile(f_bf): 
            if not overwrite: 
                print("** CAUTION: %s already exists **" % os.path.basename(f_bf)) 
        # initiating fit
        ifsps = Fitters.iFSPS(model_name=model, prior=None) 
        bestfit = ifsps.MCMC_spectrophoto(
                w_obs, 
                flux_obs, 
                ivar_obs, 
                photo_obs, 
                photo_ivar_obs, 
                meta['redshift'][igal], 
                f_fiber_prior=f_fiber_prior, 
                mask='emline', 
                nwalkers=nwalkers, 
                burnin=burnin, 
                niter=niter, 
                writeout=f_bf,
                silent=False)
    else: 
        # read in best-fit file with mcmc chain
        fbestfit = h5py.File(f_bf, 'r')  
        bestfit = {} 
        for k in fbestfit.keys(): 
            bestfit[k] = fbestfit[k][...]

    print('--- bestfit ---') 
    print('written to %s' % f_bf) 
    print('log M* total = %f' % bestfit['theta_med'][0])
    print('log M* fiber = %f' % (bestfit['theta_med'][0] + np.log10(bestfit['theta_med'][-1])))
    print('log Z = %f' % bestfit['theta_med'][1]) 
    print('---------------') 
    
    try: 
        # plotting on nersc never works.
        if os.environ['NERSC_HOST'] == 'cori': return None 
    except KeyError: 
        # corner plot of the posteriors 
        fig = DFM.corner(bestfit['mcmc_chain'], range=bestfit['priors'], quantiles=[0.16, 0.5, 0.84], 
                levels=[0.68, 0.95], nbin=40, smooth=True, 
                truths=truths, labels=labels, label_kwargs={'fontsize': 20}) 
        fig.savefig(f_bf.replace('.hdf5', '.png'), bbox_inches='tight') 
    return None 
Exemple #4
0
    spec_obs_non = specs_non['flux'][0]
    spec_ivar_obs_non = np.ones(w_obs_non.shape[0])

    t0 = time.time()
    # read Lgal photometry of the mini_mocha mocks
    photo, _ = Data.Photometry(sim='lgal',
                               noise='legacy',
                               lib='bc03',
                               sample='mini_mocha')
    print('Data.Photometry takes %.3f sec' % (time.time() - t0))  # 0.019sec

    photo_obs = photo['flux'][0, :5]
    photo_ivar_obs = photo['ivar'][0, :5]

    # initial fitter
    ifsps = Fitters.iFSPS(model_name='vanilla', prior=None)
    # middle of prior theta value
    theta_test = np.average(np.array(ifsps.priors), axis=1)

    # get mask
    t0 = time.time()
    _mask = ifsps._check_mask('emline', w_obs, spec_ivar_obs,
                              meta['redshift'][0])
    print('iFSPS._check_mask takes %.3f sec' % (time.time() - t0))  # <0.001sec

    ifsps._lnPost_spec(theta_test,
                       w_obs,
                       spec_obs,
                       spec_ivar_obs,
                       meta['redshift'][0],
                       mask=_mask,
Exemple #5
0
def fit_photometry(igal, noise='none', nwalkers=100, burnin=100, niter=1000, overwrite=False, justplot=False): 
    ''' Fit Lgal photometry. `noise` specifies whether to fit spectra without noise or 
    with legacy-like noise. `dust` specifies whether to if spectra w/ dust or not. 
    Produces an MCMC chain and, if not on nersc, a corner plot of the posterior. 

    :param igal: 
        index of Lgal galaxy within the spectral_challenge 

    :param noise: 
        If 'none', fit noiseless photometry. 
        If 'legacy', fit Legacy-like photometry. (default: 'none') 

    :param justplot: 
        If True, skip the fitting and plot the best-fit. This is mainly implemented 
        because I'm having issues plotting in NERSC. (default: False) 
    '''
    # read Lgal photometry of the mini_mocha mocks 
    photo, meta = Data.Photometry(sim='lgal', noise=noise, lib='bc03', sample='mini_mocha') 
    
    model       = 'vanilla'
    photo_obs   = photo['flux'][igal,:5]
    if noise != 'none': ivar_obs = photo['ivar'][igal,:5]
    truths      = [meta['logM_total'][igal], np.log10(meta['Z_MW'][igal]), meta['t_age_MW'][igal], None, None]
    labels      = ['$\log M_*$', '$\log Z$', r'$t_{\rm age}$', 'dust2', r'$\tau$']

    if noise == 'none': # no noise 
        ivar_obs = np.ones(photo_obs.shape[0]) 
    
    print('--- input ---') 
    print('z = %f' % meta['redshift'][igal])
    print('log M* total = %f' % meta['logM_total'][igal])
    print('MW Z = %f' % meta['Z_MW'][igal]) 
    print('MW tage = %f' % meta['t_age_MW'][igal]) 
    
    f_bf = os.path.join(UT.dat_dir(), 'mini_mocha', 'ifsps', 'lgal.photo.noise_%s.%s.%i.hdf5' % (noise, model, igal))
    if not justplot: 
        if os.path.isfile(f_bf): 
            if not overwrite: 
                print("** CAUTION: %s already exists **" % os.path.basename(f_bf)) 
                return None 
        # initiate fitting
        ifsps = Fitters.iFSPS(model_name=model, prior=None) 
        bestfit = ifsps.MCMC_photo(
                photo_obs, 
                ivar_obs,
                meta['redshift'][igal], 
                bands='desi', 
                nwalkers=nwalkers, 
                burnin=burnin, 
                niter=niter, 
                writeout=f_bf,
                silent=False)
    else: 
        # read in best-fit file with mcmc chain
        fbestfit = h5py.File(f_bf, 'r')  
        bestfit = {} 
        for k in fbestfit.keys(): 
            bestfit[k] = fbestfit[k][...]
    print('--- bestfit ---') 
    print('written to %s ---' % f_bf)
    print('log M* = %f' % bestfit['theta_med'][0])
    print('log Z = %f' % bestfit['theta_med'][1]) 
    print('---------------') 
    
    try: 
        # plotting on nersc never works.
        if os.environ['NERSC_HOST'] == 'cori': return None 
    except KeyError: 
        fig = DFM.corner(bestfit['mcmc_chain'], range=bestfit['priors'], quantiles=[0.16, 0.5, 0.84], 
                levels=[0.68, 0.95], nbin=40, smooth=True, 
                truths=truths, labels=labels, label_kwargs={'fontsize': 20}) 
        fig.savefig(f_bf.replace('.hdf5', '.png'), bbox_inches='tight') 
    return None