Пример #1
0
import time, sys, os
import numpy as np
np.errstate(invalid='ignore')

from prospect.models import model_setup
from prospect.io import write_results
from prospect.io import read_results as pr
from prospect import fitting
from prospect.likelihood import lnlike_spec, lnlike_phot, write_log, chi_spec, chi_phot

# --------------
# Read command line arguments
# --------------
sargv = sys.argv
argdict = {'restart_from': '', 'niter': 1024}
clargs = model_setup.parse_args(sargv, argdict=argdict)

# ----------
# Result object and Globals
# ----------
result, global_obs, global_model = pr.results_from(clargs["restart_from"])
is_emcee = (len(result["chain"].shape) == 3) & (result["chain"].shape[0] > 1)
assert is_emcee, "Result file does not have a chain of the proper shape."

# SPS Model instance (with libraries check)
sps = pr.get_sps(result)
run_params = result["run_params"]
run_params.update(clargs)

# Noise model (this should be doable via read_results)
from prospect.models.model_setup import import_module_from_string
Пример #2
0
def post_processing(out_file, param_file, full_h5file=True, out_incl=False, **kwargs):
    """
    Driver. Loads output, runs post-processing routine.
    """

    # Dense, complex, terrible bookkeeping on my part here based on my naming system for prospector output files
    obj = ''
    field = ''
    base = ''
    count = 0
    slash = 0
    for i in kwargs['outname']:
        if i == '/':
            slash += 1
        elif i == '_':
            count += 1

        elif out_incl:
            if slash == 2 and count == 1:
                obj += i
            elif count == 2:
                field += i
            elif count == 3:
                base += i
            elif count == 4:
                break

        elif full_h5file:
            if slash == 13 and count == 1:
                obj += i
            elif count == 2:
                field += i
            elif count == 3:
                base += i
            elif count == 4:
                break

        elif count == 0:
            obj += i
        elif count == 1:
            field += i
        elif count == 2:
            base += i
        elif count == 3 and not outy:
            break

    print(obj, field)
    full_base = obj + '_' + field + '_' + base
    img_base = 'img/' + full_base  # /home/jonathan/img' +
    print(full_base, img_base)
    pkl = 'out.pkl'

    res, pr, mod = bread.results_from(out_file)
    print('bread')

    # create flatchain, run post-processing!
    res['flatchain'] = prosp_dutils.chop_chain(res['chain'], **res['run_params'])
    res['flatprob'] = prosp_dutils.chop_chain(res['lnprobability'], **res['run_params'])
    extra_output = calc_extra_quantities(res, **kwargs)
    print('extra calculated')
    print(base)

    # choose correct folder where .h5 file is stored based on param file name
    if base == 'corr':
        folder = 'pkl_ecorr/'  # 'pkl_ncorr/'
    elif base == 'fico':
        folder = 'pkl_efico/'  # 'pkl_nfico/'
    elif base == 'masstest':
        folder = 'pkl_masstest/'
    elif base == 'tt':
        folder = 'pkl_tt/'
    else:
        folder = 'pkl_simsfh/'

    '''
    if base == 'thirty':
        folder = 'etpkls/'
    elif base == 'nth':
        folder = 'ntpkls/'
    elif base == 'fixedmet':
        folder = 'pkls/'
    elif base == 'otherbins':
        folder = 'opkls/'
    elif base == 'noelg':
        folder = 'nmpkls/'
    elif base == 'nother':
        folder = 'nopkls/'
    elif base == 'vary':
        folder = 'ecorr_pkl/'
        # folder = 'evar_pkl/'
    elif base == 'noneb' or 'evarnoneb':
        folder = 'nonebpkls/'
    elif base == 'efifty2':
        folder = 'efifty2_pkls/'
    elif base == 'evar2':
        folder = 'evar2_pkls/'
    elif base == 'masstest':
        folder = 'pkl_masstest'
    '''

    # folder = 'evar2_pkls/'  # 'efifty2_pkls/'
    # pkl extra output!
    extra = folder + full_base + '_extra_' + pkl  # full_base + '_extra_' + pkl
    print(extra)
    with open(extra, 'wb') as newfile:  # 'wb' because binary format
        pickle.dump(extra_output, newfile, pickle.HIGHEST_PROTOCOL)
    print('extra pickled')
    
    # PRINT TRACE SHOWING HOW ITERATIONS PROGRESS FOR EACH PARAMETER
    # I edited param_evol to also store lnprob, but this is a silly and long-obsolete way of doing this
    tracefig = bread.param_evol(res)  # prints tracefig
    plt.title(full_base)  # BUCKET just added
    # plt.savefig(img_base + '_tracefig.png', bbox_inches='tight')
    # plt.show()

    # FIND WALKER, ITERATION THAT GIVE MAX PROBABILITY
    # a result of the silly, long-obsolete way I'm grabbing lnprob above
    prob = res['lnprobability'][..., 0:]
    print('max', prob.max())
    row = prob.argmax() / len(prob[0])
    col = prob.argmax() - row * len(prob[0])
    walker, iteration = row, col
    print(walker, iteration)

    # PRINT CORNERFIG CONTOURS/HISTOGRAMS FOR EACH PARAMETER
    bread.subtriangle(res, start=-1000, thin=5, show_titles=True)
    plt.title(full_base)  # BUCKET just added
    # plt.savefig(img_base + '_cornerfig.png', bbox_inches='tight')
    # plt.show()
    # For FAST: truths=[mass, age, tau, dust2] (for 1824: [9.78, 0.25, -1., 0.00])

    # We need the correct sps object to generate models
    sargv = sys.argv
    argdict = {'param_file': param_file}
    clargs = model_setup.parse_args(sargv, argdict=argdict)
    run_params = model_setup.get_run_params(argv=sargv, **clargs)
    sps = model_setup.load_sps(**run_params)
    print('sps')

    # GET MODELED SPECTRA AND PHOTOMETRY
    # These have the same shape as the obs['spectrum'] and obs['maggies'] arrays.
    spec, phot, mfrac = mod.mean_model(res['chain'][walker, iteration, :], obs=res['obs'], sps=sps)
    print('spec')

    # PLOT SPECTRUM
    wave = [f.wave_effective for f in res['obs']['filters']]
    wave = np.asarray(wave)

    # CHANGING OBSERVED TO REST FRAME WAVELENGTH
    # grabbing data from catalogs
    if field == 'cdfs':
        datname = '/home/jonathan/cdfs/cdfs.v1.6.11.cat'
        zname = '/home/jonathan/cdfs/cdfs.v1.6.9.awk.zout'
    elif field == 'cosmos':
        datname = '/home/jonathan/cosmos/cosmos.v1.3.8.cat'  # main catalog
        zname = '/home/jonathan/cosmos/cosmos.v1.3.6.awk.zout'  # redshift catalog
    elif field == 'uds':
        datname = '/home/jonathan/uds/uds.v1.5.10.cat'
        zname = '/home/jonathan/uds/uds.v1.5.8.awk.zout'
    elif field == 'sim':  # hacking for now
        datname = '/home/jonathan/cosmos/cosmos.v1.3.8.cat'  # main catalog
        zname = '/home/jonathan/cosmos/cosmos.v1.3.6.awk.zout'  # redshift catalog

    # photometry catalog
    with open(datname, 'r') as f:
        hdr = f.readline().split()
    dtype = np.dtype([(hdr[1], 'S20')] + [(n, np.float) for n in hdr[2:]])
    dat = np.loadtxt(datname, comments='#', delimiter=' ', dtype=dtype)

    # redshift catalog
    with open(zname, 'r') as fz:
        hdr_z = fz.readline().split()
    dtype_z = np.dtype([(hdr_z[1], 'S20')] + [(n, np.float) for n in hdr_z[2:]])
    zout = np.loadtxt(zname, comments='#', delimiter=' ', dtype=dtype_z)

    # if z_spec exists, use it; else use best-fit z_phot
    idx = dat['id'] == obj  # array filled with: False for all dat['id'] != obj, True for dat['id'] == obj
    zred = zout['z_spec'][idx][0]
    if zred == -99:
        zred = zout['z_peak'][idx][0]
    print('redshift', zred)

    # convert stored wavelengths to rest frame wavelengths
    wave_rest = []
    for j in range(len(wave)):
        wave_rest.append(wave[j]/(1 + zred))  # 1 + z = l_obs / l_emit --> l_emit = l_obs / (1 + z)
    wave_rest = np.asarray(wave_rest)

    # OUTPUT SED results to pkls
    full_base = folder + full_base
    write_res = full_base + '_res_' + pkl  # results
    with open(write_res, 'wb') as newfile:  # 'wb' because binary format
        pickle.dump(res, newfile, pickle.HIGHEST_PROTOCOL)  # res includes res['obs']['maggies'], ['maggies_unc'], etc.
    write_sed = full_base + '_sed_' + pkl  # model sed
    with open(write_sed, 'wb') as newfile:  # 'wb' because binary format
        pickle.dump(phot, newfile, pickle.HIGHEST_PROTOCOL)
    write_restwave = full_base + '_restwave_' + pkl  # rest frame wavelengths
    with open(write_restwave, 'wb') as newfile:  # 'wb' because binary format
        pickle.dump(wave_rest, newfile, pickle.HIGHEST_PROTOCOL)
    write_spec = full_base + '_spec_' + pkl  # spectrum
    with open(write_spec, 'wb') as newfile:  # 'wb' because binary format
        pickle.dump(spec, newfile, pickle.HIGHEST_PROTOCOL)
    write_sps = full_base + '_spswave_' + pkl  # wavelengths that go with spectrum
    with open(write_sps, 'wb') as newfile:  # 'wb' because binary format
        pickle.dump(sps.wavelengths, newfile, pickle.HIGHEST_PROTOCOL)

    # OUTPUT chi_sq results to pkls
    chi_sq = ((res['obs']['maggies'] - phot) / res['obs']['maggies_unc']) ** 2
    write_chisq = full_base + '_chisq_' + pkl
    with open(write_chisq, 'wb') as newfile:  # 'wb' because binary format
        pickle.dump(chi_sq, newfile, pickle.HIGHEST_PROTOCOL)
    write_justchi = full_base + '_justchi_' + pkl
    with open(write_justchi, 'wb') as newfile:
        pickle.dump((res['obs']['maggies'] - phot) / res['obs']['maggies_unc'], newfile, pickle.HIGHEST_PROTOCOL)

    # PLOT chi_sq
    plt.plot(wave_rest, chi_sq, 'o', color='b')
    plt.title(str(obj) + r' $\chi^2$')
    plt.xlabel('Rest frame wavelength [angstroms]')
    plt.ylabel(r'$\chi^2$')
    # plt.savefig(img_base + '_chisq.png', bbox_inches='tight')
    # plt.show()

    # HOW CONVERGED IS THE CODE?? LET'S FIND OUT! --> kl test
    parnames = np.array(res['model'].theta_labels())
    fig, kl_ax = plt.subplots(1, 1, figsize=(7, 7))
    for l in xrange(parnames.shape[0]):
        kl_ax.plot(res['kl_iteration'], np.log10(res['kl_divergence'][:, l]), 'o', label=parnames[l], lw=1.5,
                   linestyle='-', alpha=0.6)

    # OUTPUT kl test to pkls
    write_klit = full_base + '_klit_' + pkl
    with open(write_klit, 'wb') as newfile:
        pickle.dump(res['kl_iteration'], newfile, pickle.HIGHEST_PROTOCOL)
    write_kldvg = full_base + '_kldvg_' + pkl
    with open(write_kldvg, 'wb') as newfile:
        pickle.dump(res['kl_divergence'], newfile, pickle.HIGHEST_PROTOCOL)

    kl_ax.set_ylabel('log(KL divergence)')
    kl_ax.set_xlabel('iteration')
    # kl_ax.set_xlim(0, nsteps*1.1)
    kl_div_lim = res['run_params'].get('convergence_kl_threshold', 0.018)
    kl_ax.axhline(np.log10(kl_div_lim), linestyle='--', color='red', lw=2, zorder=1)
    kl_ax.legend(prop={'size': 5}, ncol=2, numpoints=1, markerscale=0.7)
    plt.title(str(obj) + ' kl')
Пример #3
0
def ems(param_file, out_file, objname='21442', field='cdfs', enames=None):
    res, pr, model = bread.results_from(out_file)

    # get lnprob, based on bread.param_evol()
    chain = res['chain'][..., 0:, :]
    lnprob = res['lnprobability'][..., 0:]
    # deal with single chain (i.e. nested sampling) results
    if len(chain.shape) == 2:
        lnprob = lnprob[None, ...]
    # tracefig, prob = bread.param_evol(res)  # store probability
    # plt.show()
    print('max', lnprob.max())
    row = lnprob.argmax() / len(lnprob[0])
    col = lnprob.argmax() - row * len(lnprob[0])
    walker, iteration = row, col
    print(walker, iteration)

    # Get emission lines!
    # We need the correct sps object to generate models
    sargv = sys.argv
    argdict = {'param_file': param_file}
    clargs = model_setup.parse_args(sargv, argdict=argdict)
    run_params = model_setup.get_run_params(argv=sargv, **clargs)
    sps = model_setup.load_sps(**run_params)
    print('sps')
    # spec, mags, sm = model.mean_model(res['chain'][walker, iteration, :], obs=res['obs'], sps=sps)  # spec [maggies/Hz]
    print('mean model')
    w = sps.wavelengths

    ### save redshift, lumdist
    z = model.params.get('zred', np.array(0.0))
    lumdist = model.params.get('lumdist', np.array(0.0))
    nebinspec = model.params.get('nebemlineinspec', True)
    model.params['zred'] = np.array(0.0)
    if lumdist:
        model.params['lumdist'] = np.array(1e-5)
    if nebinspec == False:
        model.params['nebemlineinspec'] = True

    ### if we want restframe optical photometry, generate fake obs file
    ### else generate NO obs file (don't do extra filter convolutions if not necessary)
    obs = {'filters': [], 'wavelength': None}
    ### calculate SED. comes out as maggies per Hz, @ 10pc
    spec, mags, sm = model.mean_model(res['chain'][walker, iteration, :],
                                      obs=obs,
                                      sps=sps)  # maggies/Hz at 10pc
    w = sps.wavelengths

    ### reset model
    model.params['zred'] = z
    if lumdist:
        model.params['lumdist'] = lumdist
    if nebinspec == False:
        model.params['nebemlineinspec'] = False

    spec *= dfactor_10pc / constants.L_sun.cgs.value * to_ergs  # spec * cm^2 * (s/erg) * erg/maggies = s*cm^2 / Hz
    # note erg = [g cm^2 / s^2]
    # according to measure_restframe_properties(), above line converts to Lsun / Hz
    to_flam = 3e18 / w**2  # for f_nu in erg s^-1 Hz^-1 cm^-2: to_flam [(Ang / s^-1) * (1 / Ang^2)]
    spec_flam = spec * to_flam  # erg cm^-2 s^-1 ang^-1

    smooth_spec = smooth_spectrum(w, spec_flam, 250.0, minlam=3e3, maxlam=7e3)

    ### load fsps emission line list
    loc = os.getenv('SPS_HOME') + '/data/emlines_info.dat'
    dat = np.loadtxt(loc,
                     delimiter=',',
                     dtype={
                         'names': ('lam', 'name'),
                         'formats': ('f16', 'S40')
                     })
    print('env')

    print(type(enames))
    ### define emission lines
    # legacy code compatible
    if type(enames) == bool:
        lines = np.array(
            ['Hdelta', 'Hbeta', '[OIII]1', '[OIII]2', 'Halpha', '[NII]'])
        fsps_name = np.array([
            'H delta 4102', 'H beta 4861', '[OIII]4960', '[OIII]5007',
            'H alpha 6563', '[NII]6585'
        ])
    else:
        lines = enames
        fsps_name = enames

    print(lines, enames)  # None, None
    # lines = np.array(['Hdelta', 'Hbeta', '[OIII]1', '[OIII]2', 'Halpha', '[NII]'])
    # fsps_name = np.array(['H delta 4102', 'H beta 4861', '[OIII]4960', '[OIII]5007', 'H alpha 6563', '[NII]6585'])
    lines = np.array([
        '[OII]1', '[OII]2', 'Hdelta', 'Hbeta', '[OIII]1', '[OIII]2', 'Halpha',
        '[NII]'
    ])
    fsps_name = np.array([
        '[OII]3726', '[OII]3729', 'H delta 4102', 'H beta 4861', '[OIII]4960',
        '[OIII]5007', 'H alpha 6563', '[NII]6585'
    ])
    ##### measure emission line flux + EQW
    out = {}
    # print(1, sps.emline_wavelengths)  # big long array, 900 to 6*1e6
    for jj in xrange(len(lines)):

        # if we don't do nebular emission, zero this out
        if not hasattr(sps, 'get_nebline_luminosity'):
            print('hi')
            out[lines[jj]] = {'flux': 0.0, 'eqw': 0.0}
            continue

        ### calculate luminosity (in Lsun)
        idx = fsps_name[jj] == dat['name']
        # L_so = 3.84*10**33 erg/s
        print(sps.params['mass'].sum())
        eflux = float(sps.get_nebline_luminosity[idx] *
                      sps.params['mass'].sum())  # L_sun
        elam = float(sps.emline_wavelengths[idx])  # Angstroms!
        print(sps.get_nebline_luminosity[idx])
        print(eflux, 'e luminosity')  # typically 10**40 to 10**42
        print(elam, 'elam')  # Angstroms

        # simple continuum estimation
        tidx = np.abs(
            sps.wavelengths - elam
        ) < 100  # True for sps.wavelengths within 100 Angstroms of elam wavelengths
        eqw = eflux / np.median(
            smooth_spec[tidx]
        )  # (erg/s) / (erg cm^-2 s^-1 Ang^-1) = Ang * cm**2

        eflux *= 3.84 * 10**33  # erg/s (Lum, not flux)

        out[lines[jj]] = {'flux': eflux, 'eqw': eqw}

    return out, fsps_name
Пример #4
0
def post_processing(out_file, param_file, out_incl=False, full_h5file=False):  # , **kwargs):
    """
    Driver. Loads output, runs post-processing routine.
    """

    obj = ''
    field = ''
    base = ''
    count = 0
    slash = 0
    for i in out_file:
        if i == '/':
            slash += 1
        elif i == '_':
            count += 1

        elif out_incl:
            if slash == 1 and count == 1:
                obj += i
            elif count == 2:
                field += i
            elif count == 3:
                base += i
            elif count == 4:
                break

        elif full_h5file:
            if slash == 13 and count == 1:  # slash=12
                obj += i
            elif count == 2:
                field += i
            elif count == 3:
                base += i
            elif count == 4:
                break

        elif count == 0:
            obj += i
        elif count == 1:
            field += i
        elif count == 2:
            base += i
        elif count == 3 and not outy:
            break
    print(field)
    git = '/home/jonathan/.conda/envs/snowflakes/lib/python2.7/site-packages/prospector/git/'
    full_base = 'pkl_tfn/' + obj + '_' + field + '_' + base  # 'pkl_efastnoem/'  # 'pkl_efico/'
    pkl = 'out.pkl'

    res, pr, mod = bread.results_from(out_file)
    print('bread')

    # create flatchain, run post-processing
    res['flatchain'] = prosp_dutils.chop_chain(res['chain'], **res['run_params'])
    res['flatprob'] = prosp_dutils.chop_chain(res['lnprobability'], **res['run_params'])
    '''
    extra_output = calc_extra_quantities(res)  # , **kwargs)
    print('extra calculated')
    extra = full_base + '_extra_' + pkl
    with open(extra, 'wb') as newfile:  # 'wb' because binary format
        pickle.dump(extra_output, newfile, pickle.HIGHEST_PROTOCOL)
    print('extra pickled')
    '''
    prob = res['lnprobability'][:, 0:]
    # PRINT TRACE SHOWING HOW ITERATIONS CONVERGE FOR EACH PARAMETER
    # tracefig, prob = bread.param_evol(res)  # print tracefig, store probability
    # plt.title(full_base)  # BUCKET just added
    # plt.savefig('img/' + full_base + '_tracefig.png', bbox_inches='tight')
    # plt.show()

    # FIND WALKER, ITERATION THAT GIVE MAX PROBABILITY
    print('max', prob.max())
    row = prob.argmax() / len(prob[0])
    col = prob.argmax() - row * len(prob[0])
    walker, iteration = row, col
    print(walker, iteration)

    # PRINT CORNERFIG CONTOURS/HISTOGRAMS FOR EACH PARAMETER
    '''
    bread.subtriangle(res, start=0, thin=5, show_titles=True)
    plt.title(full_base)  # BUCKET just added
    plt.savefig('img/' + full_base + '_cornerfig.png', bbox_inches='tight')
    # plt.show()
    '''
    # For FAST: truths=[mass, age, tau, dust2] (for 1824: [9.78, 0.25, -1., 0.00])

    # We need the correct sps object to generate models
    sargv = sys.argv
    argdict = {'param_file': param_file}
    clargs = model_setup.parse_args(sargv, argdict=argdict)
    run_params = model_setup.get_run_params(argv=sargv, **clargs)
    sps = model_setup.load_sps(**run_params)
    print('sps')

    # GET MODELED SPECTRA AND PHOTOMETRY
    # These have the same shape as the obs['spectrum'] and obs['maggies'] arrays.
    spec, phot, mfrac = mod.mean_model(res['chain'][walker, iteration, :], obs=res['obs'], sps=sps)
    print('spec')

    # PLOT SPECTRUM
    wave = [f.wave_effective for f in res['obs']['filters']]
    wave = np.asarray(wave)

    # CHANGING OBSERVED TO REST FRAME WAVELENGTH
    if field == 'cdfs':
        datname = '/home/jonathan/cdfs/cdfs.v1.6.11.cat'
        zname = '/home/jonathan/cdfs/cdfs.v1.6.9.awk.zout'
    elif field == 'cosmos':
        datname = '/home/jonathan/cosmos/cosmos.v1.3.8.cat'  # main catalog
        zname = '/home/jonathan/cosmos/cosmos.v1.3.6.awk.zout'  # redshift catalog
    elif field == 'uds':
        datname = '/home/jonathan/uds/uds.v1.5.10.cat'
        zname = '/home/jonathan/uds/uds.v1.5.8.awk.zout'

    with open(datname, 'r') as f:
        hdr = f.readline().split()
    dtype = np.dtype([(hdr[1], 'S20')] + [(n, np.float) for n in hdr[2:]])
    dat = np.loadtxt(datname, comments='#', delimiter=' ', dtype=dtype)

    with open(zname, 'r') as fz:
        hdr_z = fz.readline().split()
    dtype_z = np.dtype([(hdr_z[1], 'S20')] + [(n, np.float) for n in hdr_z[2:]])
    zout = np.loadtxt(zname, comments='#', delimiter=' ', dtype=dtype_z)

    idx = dat['id'] == obj  # array filled: False when dat['id'] != obj, True when dat['id'] == obj
    print(obj)
    zred = zout['z_spec'][idx][0]  # z = z_spec
    if zred == -99:
        zred = zout['z_peak'][idx][0]  # if z_spec does not exist, z = z_phot
    print('redshift', zred)

    wave_rest = []  # REST FRAME WAVELENGTH
    for j in range(len(wave)):
        wave_rest.append(wave[j] / (1 + zred))  # 1 + z = l_obs / l_emit --> l_emit = l_obs / (1 + z)
    wave_rest = np.asarray(wave_rest)

    # OUTPUT SED results to files
    write_res = full_base + '_res_' + pkl  # results
    with open(write_res, 'wb') as newfile:  # 'wb' because binary format
        pickle.dump(res, newfile, pickle.HIGHEST_PROTOCOL)  # res includes res['obs']['maggies'] and ...['maggies_unc']
    write_sed = full_base + '_sed_' + pkl  # model sed
    with open(write_sed, 'wb') as newfile:  # 'wb' because binary format
        pickle.dump(phot, newfile, pickle.HIGHEST_PROTOCOL)
    write_restwave = full_base + '_restwave_' + pkl  # rest frame wavelengths
    with open(write_restwave, 'wb') as newfile:  # 'wb' because binary format
        pickle.dump(wave_rest, newfile, pickle.HIGHEST_PROTOCOL)
    write_spec = full_base + '_spec_' + pkl  # spectrum
    with open(write_spec, 'wb') as newfile:  # 'wb' because binary format
        pickle.dump(spec, newfile, pickle.HIGHEST_PROTOCOL)
    write_sps = full_base + '_spswave_' + pkl  # wavelengths that go with spectrum
    with open(write_sps, 'wb') as newfile:  # 'wb' because binary format
        try:
            wlengths = sps.wavelengths
        except AttributeError:
            wlengths = sps.csp.wavelengths
        pickle.dump(wlengths, newfile, pickle.HIGHEST_PROTOCOL)
        # pickle.dump(sps.wavelengths, newfile, pickle.HIGHEST_PROTOCOL)

    # OUTPUT CHI_SQ results to files
    chi_sq = ((res['obs']['maggies'] - phot) / res['obs']['maggies_unc']) ** 2
    write_chisq = full_base + '_chisq_' + pkl
    with open(write_chisq, 'wb') as newfile:  # 'wb' because binary format
        pickle.dump(chi_sq, newfile, pickle.HIGHEST_PROTOCOL)
    write_justchi = full_base + '_justchi_' + pkl
    with open(write_justchi, 'wb') as newfile:
        pickle.dump((res['obs']['maggies'] - phot) / res['obs']['maggies_unc'], newfile, pickle.HIGHEST_PROTOCOL)

    # PLOT CHISQ
    '''
    plt.plot(wave_rest, chi_sq, 'o', color='b')
    plt.title(str(obj) + r' $\chi^2$')
    plt.xlabel('Rest frame wavelength [angstroms]')
    plt.ylabel(r'$\chi^2$')
    plt.savefig('img/' + full_base + '_chisq.png', bbox_inches='tight')
    # plt.show()
    '''

    # HOW CONVERGED IS THE CODE?? LET'S FIND OUT!
    '''
    parnames = np.array(res['model'].theta_labels())
    fig, kl_ax = plt.subplots(1, 1, figsize=(7, 7))
    for l in xrange(parnames.shape[0]):
        kl_ax.plot(res['kl_iteration'], np.log10(res['kl_divergence'][:, l]), 'o', label=parnames[l], lw=1.5,
                   linestyle='-', alpha=0.6)
    '''
    write_klit = full_base + '_klit_' + pkl
    with open(write_klit, 'wb') as newfile:
        pickle.dump(res['kl_iteration'], newfile, pickle.HIGHEST_PROTOCOL)
    write_kldvg = full_base + '_kldvg_' + pkl
    with open(write_kldvg, 'wb') as newfile:
        pickle.dump(res['kl_divergence'], newfile, pickle.HIGHEST_PROTOCOL)
    '''
Пример #5
0
import numpy as np
np.errstate(invalid='ignore')

from prospect.models import model_setup
from prospect.io import write_results
from prospect.io import read_results as pr
from prospect import fitting
from prospect.likelihood import lnlike_spec, lnlike_phot, write_log, chi_spec, chi_phot


# --------------
# Read command line arguments
# --------------
sargv = sys.argv
argdict = {'restart_from': '', 'niter': 1024}
clargs = model_setup.parse_args(sargv, argdict=argdict)

# ----------
# Result object and Globals
# ----------
result, global_obs, global_model = pr.results_from(clargs["restart_from"])
is_emcee = (len(result["chain"].shape) == 3) & (result["chain"].shape[0] > 1)
assert is_emcee, "Result file does not have a chain of the proper shape."

# SPS Model instance (with libraries check)
sps = pr.get_sps(result)
run_params = result["run_params"]
run_params.update(clargs)

# Noise model (this should be doable via read_results)
from prospect.models.model_setup import import_module_from_string
Пример #6
0
def sed(objname, field, res, mod, walker, iteration, param_file, **kwargs):
    # PRINT MODEL SED FOR GALAXY
    # We need the correct sps object to generate models
    sargv = sys.argv
    argdict = {'param_file': param_file}
    clargs = model_setup.parse_args(sargv, argdict=argdict)
    run_params = model_setup.get_run_params(argv=sargv, **clargs)
    sps = model_setup.load_sps(**run_params)

    # GET MODELED SPECTRA AND PHOTOMETRY
    # These have the same shape as the obs['spectrum'] and obs['maggies'] arrays.
    spec, phot, mfrac = mod.mean_model(res['chain'][walker, iteration, :], obs=res['obs'], sps=sps)

    mean = ((res['obs']['maggies']-phot)/res['obs']['maggies']).mean()
    print(mean, 'mean')  # print normalized mean difference between model and observations

    # PLOT SPECTRUM
    wave = [f.wave_effective for f in res['obs']['filters']]
    wave = np.asarray(wave)
    print('len', len(sps.wavelengths), len(spec))

    ''' #
    plt.plot(sps.wavelengths, spec)
    plt.xlabel('Wavelength [angstroms]')
    plt.title(str(objname) + ' spec')
    plt.show()

    # ''' #
    ''' #
    # HOW CONVERGED IS THE CODE?? LET'S FIND OUT!
    parnames = np.array(res['model'].theta_labels())
    fig, kl_ax = plt.subplots(1, 1, figsize=(7, 7))
    for i in xrange(parnames.shape[0]):
        kl_ax.plot(res['kl_iteration'], np.log10(res['kl_divergence'][:, i]),
                   'o', label=parnames[i], lw=1.5, linestyle='-', alpha=0.6)

    kl_ax.set_ylabel('log(KL divergence)')
    kl_ax.set_xlabel('iteration')
    # kl_ax.set_xlim(0, nsteps*1.1)

    kl_div_lim = res['run_params'].get('convergence_kl_threshold', 0.018)
    kl_ax.axhline(np.log10(kl_div_lim), linestyle='--', color='red', lw=2, zorder=1)

    kl_ax.legend(prop={'size': 5}, ncol=2, numpoints=1, markerscale=0.7)
    plt.title(str(objname) + ' kl')
    plt.show()
    # ''' #

    # field = kwargs['field']
    # CHANGING OBSERVED TO REST FRAME WAVELENGTH
    if field == 'cdfs':
        datname = '/home/jonathan/cdfs/cdfs.v1.6.11.cat'
        zname = '/home/jonathan/cdfs/cdfs.v1.6.9.awk.zout'
    elif field == 'cosmos':
        datname = '/home/jonathan/cosmos/cosmos.v1.3.8.cat'  # main catalog
        zname = '/home/jonathan/cosmos/cosmos.v1.3.6.awk.zout'  # redshift catalog
    elif field == 'uds':
        datname = '/home/jonathan/uds/uds.v1.5.10.cat'
        zname = '/home/jonathan/uds/uds.v1.5.8.zout'

    with open(datname, 'r') as f:
        hdr = f.readline().split()
    dtype = np.dtype([(hdr[1], 'S20')] + [(n, np.float) for n in hdr[2:]])
    dat = np.loadtxt(datname, comments='#', delimiter=' ', dtype=dtype)

    with open(zname, 'r') as fz:
        hdr_z = fz.readline().split()
    dtype_z = np.dtype([(hdr_z[1], 'S20')] + [(n, np.float) for n in hdr_z[2:]])
    zout = np.loadtxt(zname, comments='#', delimiter=' ', dtype=dtype_z)

    idx = dat['id'] == objname  # array filled: False when dat['id'] != objname, True when dat['id'] == objname
    zred = zout['z_spec'][idx][0]  # z = z_spec
    if zred == -99:
        zred = zout['z_peak'][idx][0]  # if z_spec does not exist, z = z_phot
    print(zred)

    wave_rest = []  # REST FRAME WAVELENGTH
    for i in range(len(wave)):
        wave_rest.append(wave[i]/(1 + zred))  # 1 + z = l_obs / l_emit --> l_emit = l_obs / (1 + z)

    # PLOT MODEL SED BEST FIT, INPUT PHOT
    yerr = res['obs']['maggies_unc']
    plt.subplot(111, xscale="log", yscale="log")
    plt.errorbar(wave_rest, res['obs']['maggies'], yerr=yerr, marker='o', linestyle='', color='purple',
                 label='Observed photometry')
    plt.plot(wave_rest, phot, 'D', label='Model', color='b', markerfacecolor='None', markersize=10,
             markeredgewidth=1.25, markeredgecolor='k')  # label='Model at {},{}'.format(walker, iteration)
    plt.legend(loc="best", fontsize=20)
    plt.title(str(field) + '-' + str(objname) + ' SED')
    plt.plot(sps.wavelengths, spec, color='k', alpha=0.5)
    plt.xlabel(r'Wavelength (Rest) [$\AA$]')
    plt.ylabel('Maggies')
    plt.xlim(10**3, 2.5*10**4)
    plt.ylim(10**-5, 4*10**3)
    plt.show()

    # ''' #
    # PLOT CHI_SQ BESTFIT
    chi_sq = ((res['obs']['maggies'] - phot) / res['obs']['maggies_unc']) ** 2
    plt.plot(wave_rest, chi_sq, 'o', color='b')
    plt.title(str(objname) + r' $\chi^2$')
    plt.xlabel('Rest frame wavelength [angstroms]')
    plt.ylabel(r'$\chi^2$')
    plt.show()