Пример #1
0
def calc_sfr(nsample=40000):
	
	### setup SPS
	run_params = model_setup.get_run_params(param_file=param_file)
	sps = model_setup.load_sps(**run_params)
	model = model_setup.load_model(**run_params)
	obs = return_fake_obs({})
	nbins = model.params['sfr_fraction'].shape[0]

	#### create chain to sample from
	flatchain = np.random.dirichlet(tuple(1.0 for x in xrange(6)),nsample)
	flatchain = flatchain[:,:-1]

	### define time array for SFHs
	in_years = 10**model.params['agebins']/1e9
	t = in_years.sum(axis=1)/2.

	### output bins
	sfr = np.zeros(shape=(t.shape[0],nsample))

	#### sample the posterior
	for jj in xrange(nsample):
		
		if jj % 100 == 0:
			print float(jj)/nsample

		##### model call, to set parameters
		thetas = flatchain[jj,:]
		_,_,sm = model.mean_model(thetas, obs, sps=sps)

		##### extract sfh parameters
		# pass stellar mass to avoid extra model call
		sfh_params = prosp_dutils.find_sfh_params(model,thetas,
			                                       obs,sps,sm=sm)

		#### SFR
		sfr[:,jj] = prosp_dutils.return_full_sfh(t, sfh_params,minsfr=-np.inf)

	out = {}
	out['sfr'] = sfr
	out['flatchain'] = flatchain[:nsample,:]
	out['model'] = model

	return out
Пример #2
0
def cloudy_spectrum(ax):

	from prospect.models import model_setup

	param_file = '/Users/joel/code/python/prospector_alpha/parameter_files/brownseds_np/brownseds_np_params.py'
	
	run_params = model_setup.get_run_params(param_file=param_file)
	sps = model_setup.load_sps(**run_params)
	model = model_setup.load_model(**run_params)
	model.params['dust2'] = np.array(0.0)
	obs = model_setup.load_obs(**run_params)
	spec,_,_ = model.mean_model(model.initial_theta, obs, sps=sps)
	model.params['add_neb_emission'] = np.array(False)
	model.params['add_neb_continuum'] = np.array(False)
	spec_neboff,_,_ = model.mean_model(model.initial_theta, obs, sps=sps)

	spec_neb = (spec-spec_neboff)*3e18/sps.wavelengths**2
	in_plot = (sps.wavelengths/1e4 > 6) & (sps.wavelengths/1e4 < 30)
	spec_neb_smooth = smooth_spectrum(sps.wavelengths[in_plot], spec_neb[in_plot], 3500)
	spec_neb_smooth *= 1e5 / spec_neb_smooth.max()

	'''
	neb_color = '0.7'
	alpha = 0.7
	ax.fill_between(sps.wavelengths[in_plot]/1e4, np.zeros_like(spec_neb_smooth), spec_neb_smooth, 
                    color=neb_color,
                    alpha=alpha)

	### label H2 + [ArII] (second is in cloudy but very small)
	ax.fill_between([9.55,9.85],[0,0],[1,1],color=neb_color,alpha=alpha)
	ax.fill_between([6.8,7.1],[0,0],[1,1],color=neb_color,alpha=alpha)
	'''
	lines = ['[ArII]',r'H$_2$','[SIV]','[NeIII]','[SIII]']
	lam = [6.95,9.7,10.45,15.5,18.7]
	# removed because they're too weak, just distracting
	#lines = ['[ArIII]','[NeII]']
	#lam = [9.0,12.8]

	for ii in xrange(len(lines)):
		ax.text(lam[ii]*1.008,0.14,lines[ii],
			    ha='left',fontsize=9.5)
	for ii in xrange(len(lam)): ax.plot([lam[ii],lam[ii]],[0,1e5],linestyle='--',lw=1.5,color='k')
Пример #3
0
import numpy as np
import matplotlib.pyplot as plt
import spot_utils as pread
from prospect.io.read_results import results_from
from prospect.models import model_setup

paramfile = 'ad_params.py'
clargs = {'param_file': paramfile}
run_params = model_setup.get_run_params(argv=paramfile, **clargs)
print(run_params)

obs = model_setup.load_obs(**run_params)
sps = model_setup.load_sps(**run_params)
model = model_setup.load_model(**run_params)

wspec = sps.csp.wavelengths  # *restframe* spectral wavelengths
a = 1.0 + model.params.get('zred', 0.6)  # cosmological redshifting
wphot = np.array([f.wave_effective for f in obs['filters']])

# In [21]
# grab results, powell results, and our corresponding models
#res, pr, mod = results_from("{}_mcmc.h5".format(outroot))
res, pr, mod = results_from("demo_galphot_1508433060_mcmc.h5")

# In [22]
# To see how our MCMC samples look, we can examine a few traces
choice = np.random.choice
tracefig = pread.param_evol(res,
                            figsize=(20, 10),
                            chains=choice(128, size=10, replace=False))
Пример #4
0
import numpy as np
np.errstate(invalid='ignore')

from prospect.models import model_setup
from prospect.io import write_results
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 = {'param_file': ''}
clargs = model_setup.parse_args(sargv, argdict=argdict)
run_params = model_setup.get_run_params(argv=sargv, **clargs)

# --------------
# Globals
# --------------
# SPS Model instance as global
sps = model_setup.load_sps(**run_params)
# GP instances as global
spec_noise, phot_noise = model_setup.load_gp(**run_params)
# Model as global
global_model = model_setup.load_model(**run_params)
# Obs as global
global_obs = model_setup.load_obs(**run_params)

# -----------------
# LnP function as global
Пример #5
0
##### modify nebon status
# we want to be able to turn it on and off at will
if sample_results['model'].params['add_neb_emission'] == 2:
	sample_results['model'].params['add_neb_emission'] = np.array(True)

##### initialize sps
sps = model_setup.load_sps(**sample_results['run_params'])


###### MAXPROB_MODEL
# grab maximum probability, plus the thetas that gave it
maxprob = np.max(sample_results['lnprobability'])
probind = sample_results['lnprobability'] == maxprob
thetas = sample_results['chain'][probind,:]
if type(thetas[0]) != np.dtype('float64'):
	thetas = thetas[0]


###### TEST LIKELIHOOD
run_params = model_setup.get_run_params(param_file=param_name)
gp_spec, gp_phot = model_setup.load_gp(**run_params)
likefn = LikelihoodFunction()
mu, phot, x = model.mean_model(model.initial_theta, sample_results['obs'], sps=sps)

mu, phot, x = model.mean_model(thetas, sample_results['obs'], sps = sps)
lnp_phot = likefn.lnlike_phot(phot, obs=sample_results['obs'], gp=gp_phot)
lnp_prior = model.prior_product(thetas)

print lnp_phot + lnp_prior

Пример #6
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')
Пример #7
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
def make_all_plots(filebase=None,
                   extra_output=None,
                   outfolder=os.getenv('APPS')+'/prospector_alpha/plots/',
                   sample_results=None,
                   param_name=None,
                   plt_chain=True,
                   plt_corner=True,
                   plt_sed=True):

    '''
    Driver. Loads output, makes all plots for a given galaxy.
    '''

    # make sure the output folder exists
    if not os.path.isdir(outfolder):
        os.makedirs(outfolder)

    if sample_results is None:
        try:
            sample_results, powell_results, model, extra_output = load_prospector_data(filebase, hdf5=True, load_extra_output=True)
        except TypeError:
            return
    else: # if we already have sample results, but want powell results
        try:
            _, powell_results, model, extra_output = load_prospector_data(filebase,no_sample_results=True, hdf5=True)
        except TypeError:
            return  

    run_params = model_setup.get_run_params(param_file=param_name)
    sps = model_setup.load_sps(**run_params)

    # BEGIN PLOT ROUTINE
    print 'MAKING PLOTS FOR ' + filebase.split('/')[-1] + ' in ' + outfolder
    objname = sample_results['run_params'].get('objname','galaxy')

    # chain plot
    flatchain = prosp_dutils.chop_chain(sample_results['chain'],**sample_results['run_params'])
    if plt_chain: 
        print 'MAKING CHAIN PLOT'

        show_chain(sample_results,outname=outfolder+objname)

    # corner plot
    if plt_corner: 
        print 'MAKING CORNER PLOT'
        subcorner(sample_results, sps, copy.deepcopy(sample_results['model']),
                  extra_output,flatchain,outname=outfolder+objname)

    # sed plot
    if plt_sed:
        print 'MAKING SED PLOT'
        
        # FAST fit?
        try:
            sample_results['run_params']['fastname']
            fast=1
        except:
            fast=0

        # plot
        pfig = sed_figure(sresults = [sample_results], extra_output=[extra_output],
                          outname=outfolder+objname+'.sed.png')
Пример #9
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)
    '''
Пример #10
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()
Пример #11
0
def compute_specmags(runname=None, outfolder=None):

    '''
    step 1: load observed spectra, model spectra, photometry
    step 2: normalize model spectra to observed spectra (at red end, + blue if using u-band)
    step 3: meld them together
    step 4: calculate u, g, r mags from spectra
    step 5: plot spectral u, g, r versus photometric u, g, r

    dump into pickle file, call perform_wavelength_cal() to apply

    future steps: don't use super low-res spectra
    '''


    print 'THIS FUNCTION IS BROKEN, UNTIL YOU LET THREED_DUTILS USE LUMDIST'
    from bsfh import model_setup

    if runname == None:
        runname = 'brownseds'

    #### load up prospector results
    filebase, parm_basename, ancilname=prosp_dutils.generate_basenames(runname)
    outname = os.getenv('APPS')+'/prospector_alpha/plots/'+runname+'/pcomp/sfrcomp.png'
    alldata = prospector_io.load_alldata(runname=runname)
    sps = prosp_dutils.setup_sps(custom_filter_key=None)

    optphot = np.zeros(shape=(3,len(alldata)))
    obsphot = np.zeros(shape=(3,len(alldata)))
    for ii,dat in enumerate(alldata):

        #### load up model spec
        z = dat['residuals']['phot']['z']
        mod_spec = dat['bfit']['spec']
        mod_wav = sps.wavelengths

        #### load up observed spec
        # arrives in maggies * Hz
        # change to maggies
        spec_dict = prospector_io.load_spectra(dat['objname'])
        opt_idx = spec_dict['source'] == 1
        obs_wav = spec_dict['obs_lam'][opt_idx]
        obs_spec = spec_dict['flux'][opt_idx] / (3e18 / obs_wav)

        #### find adjoining sections in model
        minwav = np.min(obs_wav)
        maxwav = np.max(obs_wav)

        lamlim = (2800,7500)
        lower_join = (mod_wav > lamlim[0]) & (mod_wav < minwav)
        upper_join = (mod_wav < lamlim[1]) & (mod_wav > maxwav)

        #### normalize and combine
        # take 300 angstrom slices on either side
        dellam = 300
        low_obs = obs_wav < minwav+dellam
        low_mod = (mod_wav < minwav + dellam) & (mod_wav > minwav)
        up_obs = obs_wav > maxwav-dellam
        up_mod = (mod_wav > maxwav - dellam) & (mod_wav < maxwav)

        # avoid known emission lines: [OII], [NII], Halpha, [SII]
        elines = np.array([3727,6549,6563,6583,6717,6731])
        lambuff = 22
        for line in elines:
            low_obs[(obs_wav > line - lambuff) & (obs_wav < line + lambuff)] = False
            low_mod[(mod_wav > line - lambuff) & (mod_wav < line + lambuff)] = False
            up_obs[(obs_wav > line - lambuff) & (obs_wav < line + lambuff)] = False
            up_mod[(mod_wav > line - lambuff) & (mod_wav < line + lambuff)] = False

        # calculate mean
        lownorm = np.mean(obs_spec[low_obs]) / np.mean(mod_spec[low_mod])
        upnorm = np.mean(obs_spec[up_obs]) / np.mean(mod_spec[up_mod])

        # combine
        comblam = np.concatenate((mod_wav[lower_join],obs_wav,mod_wav[upper_join]))
        combspec = np.concatenate((mod_spec[lower_join]*lownorm,obs_spec,mod_spec[upper_join]*upnorm))

        # plot conjoined spectra
        if True:
            # observed spectra
            plt.plot(obs_wav,np.log10(obs_spec),color='black')
            #plt.plot(obs_wav[up_obs],np.log10(obs_spec[up_obs]),color='purple')
            #plt.plot(mod_wav[up_mod],np.log10(mod_spec[up_mod]),color='green')

            # post-break model
            plt.plot(mod_wav[lower_join],np.log10(mod_spec[lower_join]*lownorm),color='red')
            plt.plot(mod_wav[upper_join],np.log10(mod_spec[upper_join]*upnorm),color='red')
            plt.plot(mod_wav[lower_join],np.log10(mod_spec[lower_join]),color='grey')
            plt.plot(mod_wav[upper_join],np.log10(mod_spec[upper_join]),color='grey')

            # limits, save
            plt.xlim(2800,7200)
            plt.savefig('/Users/joel/code/python/prospector_alpha/plots/'+runname+'/pcomp/specnorm/'+dat['objname']+'.png',dpi=100)
            plt.close()

        #### convert combspec from maggies to Lsun/Hz
        pc2cm =  3.08568E18
        dfactor = (1+z) / ( 4.0 * np.pi * (dat['residuals']['phot']['lumdist']*1e6*pc2cm)**2)
        combspec *= 3631*1e-23 / constants.L_sun.cgs.value / dfactor # to Jy, to erg/s/cm^2/Hz, to Lsun/cm^2/Hz, to Lsun/Hz

        #### integrate spectra, save mags
        optphot[0,ii],_ = prosp_dutils.integrate_mag(comblam,combspec,'SDSS Camera u',alt_file='/Users/joel/code/fsps/data/allfilters.dat',z=z)
        optphot[1,ii],_ = prosp_dutils.integrate_mag(comblam,combspec,'SDSS Camera g',alt_file='/Users/joel/code/fsps/data/allfilters.dat',z=z)
        optphot[2,ii],_ = prosp_dutils.integrate_mag(comblam,combspec,'SDSS Camera r',alt_file='/Users/joel/code/fsps/data/allfilters.dat',z=z)
        optphot[:,ii] =  10**(-0.4*optphot[:,ii])

        #### save observed mags
        run_params = model_setup.get_run_params(param_file=parm_basename[ii])
        obs = model_setup.load_obs(**run_params)
        obsphot[0,ii] = obs['maggies'][obs['filters'] == 'SDSS_u']
        obsphot[1,ii] = obs['maggies'][obs['filters'] == 'SDSS_g']
        obsphot[2,ii] = obs['maggies'][obs['filters'] == 'SDSS_r']

    ##### plot
    kwargs = {'color':'0.5','alpha':0.8,'histtype':'bar','lw':2,'normed':1,'range':(0.5,2.0)}
    nbins = 80

    x = obsphot / optphot
    t**s = [r'(photometric flux / spectral flux) [u-band]',
            r'(photometric flux / spectral flux) [g-band]',
            r'(photometric flux / spectral flux) [r-band]']

    #### ALL GALAXIES
    fig, axes = plt.subplots(1, 3, figsize = (18.75,6))
    for ii, ax in enumerate(axes):
        num, b, p = ax.hist(x[ii,:],nbins,**kwargs)
        save_xlim = ax.get_xlim()
        b = (b[:-1] + b[1:])/2.
        ax.set_ylabel('N')
        ax.set_xlabel(t**s[ii])
        ax.set_xlim(save_xlim)
        ax.xaxis.set_major_locator(MaxNLocator(5))
    plt.savefig('/Users/joel/code/python/prospector_alpha/plots/'+runname+'/pcomp/spectral_integration.png',dpi=dpi)
    plt.close()

    out = {'obs_phot':obsphot,'spec_phot':optphot}
    prospector_io.save_spec_cal(out,runname=runname)
Пример #12
0
import numpy as np
import matplotlib.pyplot as pl
from matplotlib.ticker import MaxNLocator, NullLocator
from matplotlib.colors import LinearSegmentedColormap, colorConverter
from matplotlib.ticker import ScalarFormatter
#okay that is in, let's try this bullshit

## HERE IS A LIST OF THINGS THAT I REFUSE TO HARD CODE INTO THE PROGRAM!!!
#paramfile = '/Users/sgottlie/github/prospector/demo/demo_mock_params1.py'
paramfile = '/demo_mock_params1.py'
dmp1 = 'demo_mock_params1.py'
photfile = '/demo_photometry1.dat'
### OKAY ALL DONE

clargs = {'param_file': paramfile}
run_params = model_setup.get_run_params(argv=dmp1, **clargs)
print(run_params)

# load sps model (default)
sps = model_setup.load_sps(**run_params)

# load noise model (none)
spec_noise, phot_noise = model_setup.load_gp(**run_params)

# demo model
model = model_setup.load_model(**run_params)

# demo data (generated from the script)
obs = model_setup.load_obs(phottable=photfile, **run_params)
print('Mock S/N={}'.format(obs['mock_snr']))
if run_params['add_noise']:
Пример #13
0
def compare_sed():

	### custom parameter file
	### where mass is a six-parameter thing
	param_file = 'brownseds_np_params.py'

	run_params = model_setup.get_run_params(param_file=param_file)
	sps = model_setup.load_sps(**run_params)
	model = model_setup.load_model(**run_params)
	obs = model_setup.load_obs(**run_params)
	thetas = model.initial_theta

	### create star formation history and metallicity history
	mformed = 1e10
	m_constant = return_declining_sfh(model,mformed, tau=1e12)
	met = return_zt(model)
	#met = np.ones_like(met)
	thetas[model.theta_labels().index('logzsol')] = 0.0

	### simple
	i1, i2 = model.theta_index['mass']
	thetas[i1:i2] = m_constant
	nsamp = 21
	met_comp = np.linspace(-1,0,nsamp)
	spec, phot = [], []
	for m in met_comp:
		thetas[model.theta_labels().index('logzsol')] = m
		specx, photx, x = model.mean_model(thetas, obs, sps=sps)
		spec.append(specx)
		phot.append(photx)

	### complex
	for i in xrange(met.shape[0]):

		# zero out all masses
		thetas[i1:i2] = np.zeros_like(m_constant)

		# fill in masses and metallicities
		thetas[model.theta_labels().index('logzsol')] = np.log10(met[i])
		thetas[i1+i] = m_constant[i]

		# generate spectrum, add to existing spectrum
		sps.ssp.params.dirtiness = 1
		specx, photx, x = model.mean_model(thetas, obs, sps=sps)

		if i == 0:
			spec_agez, phot_agez = specx, photx
		else:
			spec_agez += specx
			phot_agez += photx

	### plot
	fig, ax = plt.subplots(1,1, figsize=(8, 7))
	cmap = get_cmap(nsamp)
	for i in xrange(0,nsamp,4): ax.plot(sps.wavelengths/1e4, np.log10(spec[i] / spec_agez),color=cmap(i), label=met_comp[i])
	ax.axhline(0, linestyle='--', color='0.1')
	ax.legend(loc=4,prop={'size':20},title=r'log(Z/Z$_{\odot}$) [fixed]')

	ax.set_xlim(0.1,10)
	ax.set_xscale('log',nonposx='clip',subsx=(2,5))
	ax.xaxis.set_minor_formatter(minorFormatter)
	ax.xaxis.set_major_formatter(majorFormatter)

	ax.set_ylabel(r'log(f$_{\mathrm{Z}_{\mathrm{fixed}}}$ / f$_{\mathrm{Z(t)}}$)')
	ax.set_xlabel('wavelength [microns]')
	ax.set_ylim(-0.4,0.4)
	plt.tight_layout()
	plt.show()
	print 1/0
Пример #14
0
import numpy as np
np.errstate(invalid='ignore')

from prospect.models import model_setup
from prospect.io import write_results
from prospect import fitting
from prospect.likelihood import lnlike_spec, lnlike_phot, write_log


# --------------
# Read command line arguments
# --------------
sargv = sys.argv
argdict = {'param_file': ''}
clargs = model_setup.parse_args(sargv, argdict=argdict)
run_params = model_setup.get_run_params(argv=sargv, **clargs)

# --------------
# Globals
# --------------
# SPS Model instance as global
sps = model_setup.load_sps(**run_params)
# GP instances as global
spec_noise, phot_noise = model_setup.load_gp(**run_params)
# Model as global
global_model = model_setup.load_model(**run_params)
# Obs as global
global_obs = model_setup.load_obs(**run_params)

# -----------------
# LnP function as global
Пример #15
0
def return_dat(runname, runname_comp, pltcorner=False, pltchain=False):

    #filebase, pfile, ancilname = generate_basenames(runname)
    #filebase2, pfile2, ancilname2 = generate_basenames(runname_comp)
    size = 500000

    filebase = ['/Users/joel/code/python/prospector_alpha/results/guillermo_nestle/guillermo_nestle']
    pfile = ['/Users/joel/code/python/prospector_alpha/parameter_files/guillermo_nestle/guillermo_nestle_params.py']
    filebase2 = ['/Users/joel/code/python/prospector_alpha/results/guillermo/guillermo']
    pfile2 = ['/Users/joel/code/python/prospector_alpha/parameter_files/guillermo/guillermo_params.py']


    #bad =  ['NGC 0584','UGCA 166','Mrk 1450','UM 461','UGC 06850','NGC 4125','NGC 4551','Mrk 0475']

    objname, hinformation, logz, logzerr, dlogz, ncall = [], [], [], [], [], []
    for i, file in enumerate(filebase):
        
        sresults, _, model, _ = load_prospector_data(file,load_extra_output=False)

        ### some of the nestle runs terminated due to time limit
        ### must regenerate the models
        if model is None:
            run_params = model_setup.get_run_params(param_file=pfile)
            run_params['objname'] = file.split('_')[-1]
            model = model_setup.load_model(**run_params)

        sresults_mcmc, _, model_mcmc, _ = load_prospector_data(filebase2[i],load_extra_output=False)
        flatchain = chop_chain(sresults_mcmc['chain'],**sresults_mcmc['run_params'])
        flatchain, pnames_emcee = transform_chain(flatchain, model_mcmc)

        ### save some stuff
        # logz_est.append(sresults['logvol'][sresults['lnprobability'].argmax()]+np.log10(np.exp(sresults['lnprobability'].max()))
        npoints = sresults['run_params']['nestle_npoints']
        logz_remain = np.max(sresults['lnprobability']) - (sresults['chain'].shape[0]/1000.)
        
        dlogz.append(np.logaddexp(sresults['logz'], logz_remain) - sresults['logz'])
        ncall.append(sresults['ncall'])
        hinformation.append(sresults['h_information'][0])
        logz.append(sresults['logz'][0])
        logzerr.append(sresults['logzerr'][0])
        sresults['dlogz'] = dlogz[-1]

        ### define output containers
        if len(objname) == 0:
            parnames = model.theta_labels()
            mcmc, nestle = {}, {}
            percs = ['q50','q84','q16','q2.5','q97.5',]
            for dic in [mcmc, nestle]:
                for par in parnames: 
                    dic[par] = {}
                    for p in percs: dic[par][p] = []
                dic['maxlnprob'] = []

        ### corner plot?
        outname = file.split('results')[0]+'plots/'+runname+'/'+file.split('_')[-1]
        if pltcorner:
            # outname = outname+'.corner.png'
            range = [tuple(np.percentile(flatchain[:,i],[1,99]).tolist()) for i in xrange(flatchain.shape[1])]
            range = None
            fig, nestle_pnames = ncorner(sresults, model, range=range)
            ecorner(sresults_mcmc, flatchain, fig, pnames_emcee, outname+'.corner.png',n_pnames = nestle_pnames, range=range)
        if pltchain:
            plot_chain(sresults, parnames, outname+'.chain.png')

        ### load all data
        for i,par in enumerate(parnames):
            p1 = np.random.choice(sresults['chain'][:,i],size=size, p=sresults['weights'])
            p2 = flatchain[:,i]
            for dic, chain in zip([nestle,mcmc],[p1,p2]): 
                for q in dic[par].keys(): dic[par][q].append(np.percentile(chain, [float(q[1:])])[0])

        nestle['maxlnprob'].append(sresults['lnprobability'].max())
        mcmc['maxlnprob'].append(sresults_mcmc['lnprobability'].max())
        objname.append(sresults['run_params'].get('objname','galaxy'))
        print 1/0
    dat = {'nestle': nestle, 'emcee': mcmc}
    dat['labels'] = model.theta_labels()
    dat['objname'] = objname

    nestle_pars = {
                   'hinformation': hinformation,
                   'logz': logz,
                   'logzerr': logzerr,
                   'dlogz': dlogz,
                   'ncall': ncall
                  }
    dat['nestle_pars'] = nestle_pars
    return dat
Пример #16
0
#obs['filters'] = None # turn off filters for now, though should also check these!

# theta = np.array([10**10.7,11.02,0.23,0.57,0.99,1.57,0.02,-1.96,0.1,0.16,0.16,2.18])
theta = np.array([10**10.7,11.02,0.23,0.57,0.8,0.0,0.02,-1.96,0.1,0.16,0.16,2.18])
spec, phot, x = model.mean_model(theta, obs, sps=sps)
w = sps.wavelengths
print 'generated OLD prospector file!'
param_dict = {}
for k, v in sps.params.iteritems(): param_dict[k] = v
print param_dict

'''
from prospect.models import model_setup

outname = 'new_brownseds.pickle'
run_params = model_setup.get_run_params(param_file='new_brownseds.py')
sps = model_setup.load_sps(**run_params)
model = model_setup.load_model(**run_params)
obs = model_setup.load_obs(**run_params)
#model.params['zred']=np.atleast_1d(0.0)

#obs['filters'] = np.array([]) # turn off filters for now, though should also check these!

# theta = np.array([10**10.7,11.02,0.23,0.57,0.99,1.57,0.02,-1.96,0.1,0.16,0.16,2.18])
theta = np.array([10.7,11.02,0.23,0.57,0.8,0.0,0.02,-1.96,0.1,0.16,0.16,2.18])
spec, phot, x = model.mean_model(theta, obs, sps=sps)
w = sps.csp.wavelengths
print 'generated NEW prospector file!'
param_dict = {}
for k, v in sps.csp.params.iteritems(): param_dict[k] = v
#'''