def __init__(self, bands=None, MAIN_OUTPUT_FILE='photz', OUTPUT_DIRECTORY='./OUTPUT', CACHE_FILE='Same', zout=None, f_lambda=True): from scipy import interpolate import threedhst.eazyPy as eazy #### Read the files from the specified output tempfilt, coeffs, temp_seds, pz = eazy.readEazyBinary(MAIN_OUTPUT_FILE=MAIN_OUTPUT_FILE, OUTPUT_DIRECTORY=OUTPUT_DIRECTORY, CACHE_FILE = CACHE_FILE) if bands is None: self.bands = np.arange(tempfilt['NFILT']) else: self.bands = np.array(bands) self.band_names = ['' for b in self.bands] if zout is not None: param = eazy.EazyParam(PARAM_FILE=zout.filename.replace('.zout','.param')) self.band_names = [f.name for f in param.filters] self.bands = np.array([f.fnumber-1 for f in param.filters]) self.NFILT = len(self.bands) self.NTEMP = tempfilt['NTEMP'] self.lc = tempfilt['lc'][self.bands] self.sed = temp_seds self.templam = self.sed['templam'] self.temp_seds = self.sed['temp_seds'] self.in_zgrid = tempfilt['zgrid'] self.tempfilt = tempfilt['tempfilt'][self.bands, :, :] if f_lambda: for i in range(self.NFILT): self.tempfilt[i,:,:] /= (self.lc[i]/5500.)**2 ###### IGM absorption self.igm_wave = [] self.igm_wave.append(self.templam < 912) self.igm_wave.append((self.templam >= 912) & (self.templam < 1026)) self.igm_wave.append((self.templam >= 1026) & (self.templam < 1216)) self._spline_da = interpolate.InterpolatedUnivariateSpline(self.in_zgrid, temp_seds['da']) self._spline_db = interpolate.InterpolatedUnivariateSpline(self.in_zgrid, temp_seds['db']) #### Make a 2D list of the spline interpolators self._interpolators = [range(self.NTEMP) for i in range(self.NFILT)] for i in range(self.NFILT): for j in range(self.NTEMP): self._interpolators[i][j] = interpolate.InterpolatedUnivariateSpline(self.in_zgrid, self.tempfilt[i, j, :]) # self.output = None self.zout = None
def init_nmf(obj, iz, MAIN_OUTPUT_FILE='photz', OUTPUT_DIRECTORY='./OUTPUT', CACHE_FILE='Same', verbose=True): import threedhst.eazyPy as eazy MAIN_OUTPUT_FILE = 'cosmos-1.deblend.v5.1' OUTPUT_DIRECTORY = './cosmos-1.deblend.redshifts' CACHE_FILE='Same' obj = 2407 iz = coeffs['izbest'][obj] eazy.param = eazy.EazyParam(PARAM_FILE=OUTPUT_DIRECTORY+'/'+MAIN_OUTPUT_FILE+'.param') try: eazy.NTEMP = eazy.tempfilt['NTEMP'] eazy.NFILT = eazy.tempfilt['NFILT'] eazy.NZ = eazy.tempfilt['NZ'] except: if verbose: print 'Read EAZY binary files (%s/%s)....' %(OUTPUT_DIRECTORY, MAIN_OUTPUT_FILE) eazy.tempfilt, eazy.coeffs, eazy.temp_sed, eazy.pz = eazy.readEazyBinary(MAIN_OUTPUT_FILE=MAIN_OUTPUT_FILE, OUTPUT_DIRECTORY=OUTPUT_DIRECTORY, CACHE_FILE=CACHE_FILE) eazy.werr, eazy.terr = np.loadtxt('templates/TEMPLATE_ERROR.eazy_v1.0', unpack=True) # fnu = tempfilt['fnu'][:,obj] efnu = tempfilt['efnu'][:,obj] lc_rest = tempfilt['lc']/(1+eazy.tempfilt['zgrid'][iz]) template_error = np.interp(lc_rest, eazy.werr, eazy.terr) var = (fnu*eazy.param['SYS_ERR'])**2++(fnu*template_error)**2+efnu**2 sig = np.sqrt(var) mask = (fnu > -99) & (efnu > 0) maskNFILT = len(mask[mask]) #### A matrix for NMF aa = tempfilt['tempfilt'][:,:,iz]/np.dot(sig.reshape(eazy.NFILT, 1), np.ones((1, eazy.NTEMP))) eazy.amatrix = np.dot(aa[mask,:].transpose(), aa[mask,:]) eazy.bvector = -np.dot((fnu[mask]/var[mask]).reshape(1,maskNFILT), tempfilt['tempfilt'][mask,:,iz]).reshape(eazy.NTEMP) nit, coeffs_fit = eazy.nonneg_fact() #### Test obs_fit = np.dot(tempfilt['tempfilt'][:,:,iz], coeffs_fit) plt.plot(lc[mask], fnu[mask]/(lc[mask]/5500.)**2) plt.plot(lc[mask], obs_fit[mask]/(lc[mask]/5500.)**2)
def plotZPoffs(): tempfilt, coeffs, temp_seds, pz = eazy.readEazyBinary(MAIN_OUTPUT_FILE=root, OUTPUT_DIRECTORY='OUTPUT',CACHE_FILE = 'Same')
def show_fit_residuals(root='photz_v1.7.fullz', PATH='./OUTPUT/', savefig=None, adjust_zeropoints='zphot.zeropoint', fix_filter=None): """ Plot the EAZY fit residuals to evaluate zeropoint updates """ import threedhst import threedhst.eazyPy as eazy if not PATH.endswith('/'): PATH += '/' ##### Read the param file param = EazyParam('%s%s.param' %(PATH, root)) ##### Read template fluxes and coefficients tempfilt, coeffs, temp_seds, pz = eazy.readEazyBinary(MAIN_OUTPUT_FILE=root, OUTPUT_DIRECTORY=PATH,CACHE_FILE = 'Same') param = eazy.EazyParam(PARAM_FILE=PATH+'/'+root+'.param') fnumbers = np.zeros(len(param.filters), dtype=np.int) for i in range(len(fnumbers)): fnumbers[i] = int(param.filters[i].fnumber) zpfile = PATH+'/'+root+'.zeropoint' if os.path.exists(zpfile): zpfilts, zpf_file = np.loadtxt(zpfile, unpack=True, dtype=np.str) zpf = np.ones(tempfilt['NFILT']) for i in range(len(zpfilts)): match = fnumbers == int(zpfilts[i][1:]) zpf[match] = np.float(zpf_file[i]) else: zpf = np.ones(tempfilt['NFILT']) zpfactors = np.dot(zpf.reshape(tempfilt['NFILT'],1), np.ones(tempfilt['NOBJ']).reshape(1,tempfilt['NOBJ'])) tempfilt['fnu'] *= zpfactors tempfilt['efnu'] *= zpfactors obs_sed = np.zeros((tempfilt['NFILT'], tempfilt['NOBJ']), dtype=np.float) for i in xrange(tempfilt['NOBJ']): obs_sed[:,i] = np.dot(tempfilt['tempfilt'][:,:,coeffs['izbest'][i]], coeffs['coeffs'][:,i]) zi = tempfilt['zgrid'][coeffs['izbest']] lc = tempfilt['lc'] reference_filter = None if reference_filter is None: dl = np.abs(lc-2.e4) reference_filter = fnumbers[dl == dl.min()][0] resid = (obs_sed-tempfilt['fnu']) / obs_sed + 1 signoise = tempfilt['fnu']/tempfilt['efnu'] #### Plot colors colors = range(tempfilt['NFILT']) for ci, i in enumerate(np.argsort(lc)): colors[i] = threedhst.utils.color_table((ci+1.)/tempfilt['NFILT']*250, table='rainbow.rgb') fig = plt.figure(figsize=(10,4)) fig.subplots_adjust(wspace=0.0, hspace=0.0, left=0.09, bottom=0.10, right=0.98, top=0.98) ax = fig.add_axes((0.06, 0.12, 0.6, 0.86)) #### Plot the residuals xx, yy, ss = [], [], [] for i in np.argsort(lc): keep = signoise[i,:] > 0 sc = ax.plot(lc[i]/(1+zi[keep]), resid[i,keep], marker='.', alpha=0.03, linestyle='None', color=colors[i]) #xm, ym, ys, nn = threedhst.utils.runmed(lc[i]/(1+zi[keep]), resid[i,keep], NBIN=int(len(keep)/1000.)) xm, ym, ys, nn = threedhst.utils.runmed(lc[i]/(1+zi[keep]), resid[i,keep], NBIN=np.maximum(int(keep.sum()/1000.), 5)) xx.append(xm) yy.append(ym) ss.append(ys) for ci, i in enumerate(np.argsort(lc)): ax.plot(xx[ci], yy[ci], alpha=0.2, color='black', linewidth=4) ax.plot(xx[ci], yy[ci], alpha=0.8, color=colors[i], linewidth=2) #### Adjustments to zeropoints lcfull = [] residfull = [] for i in np.argsort(lc): keep = signoise[i,:] > 3 keep = signoise[i,:] > 0 lcfull.extend(lc[i]/(1+zi[keep])) residfull.extend(resid[i,keep]) xmfull, ymfull, ysfull, nnfull = threedhst.utils.runmed(np.array(lcfull), np.array(residfull), NBIN=np.maximum(int(len(residfull)/5000.), 10)) ax.plot(xmfull, ymfull, color='black', alpha=0.3, linewidth=2) if not os.path.exists(adjust_zeropoints): fp = open(adjust_zeropoints,'w') for filt in param.filters: fp.write('F%0d 1.0\n' %(filt.fnumber)) fp.close() zpfilt, zpval = np.loadtxt(adjust_zeropoints, dtype=np.str, unpack=True) zpval = np.cast[float](zpval) offsets = lc*0. for ci, i in enumerate(np.argsort(lc)): yint = np.interp(xx[ci], xmfull, ymfull) offsets[i] = 1./(np.sum(yy[ci]*yint/ss[ci]**2)/np.sum(yy[ci]**2/ss[ci]**2)) ## Normalize to F140W offsets /= offsets[fnumbers == reference_filter] ref_offset = 1. if fix_filter is not None: for ci, i in enumerate(np.argsort(lc)): if param.filters[i].fnumber == fix_filter: ref_offset = offsets[i] print 'Filt %d: %f' %(param.filters[i].fnumber, offsets[i]) ## Write a new zphot.translate file for ci, i in enumerate(np.argsort(lc)): mat = zpfilt == 'F%0d' %(param.filters[i].fnumber) if (len(mat[mat]) > 0): # & (param.filters[i].lambda_c < 3.e4): zpval[mat] *= offsets[i]/ref_offset fp = open(adjust_zeropoints,'w') for i in range(len(zpval)): fp.write('%s %.4f\n' %(zpfilt[i], zpval[i])) fp.close() #### Plot things ax.set_xlim(800,1.e5) ax.semilogx() ax.set_ylim(0.5,1.5) ax.set_xlabel(r'$\lambda_\mathrm{rest}\ [\mu\mathrm{m}]$') ax.set_ylabel(r'(temp - phot) / temp') ax.set_xticklabels([0.1,0.5,1,5]) ytick = ax.set_xticks([1000,5000,1.e4,5e4]) #### Add labels for filters for ci, i in enumerate(np.argsort(lc)): ax.text(0.98, 0.92-0.05*ci, '%s %.2f' %(os.path.basename(param.filters[i].name).replace('_','\_'), offsets[i]/ref_offset), transform = ax.transAxes, color='0.5', horizontalalignment='right', fontsize=9) ax.text(0.98, 0.92-0.05*ci, '%s %.2f' %(os.path.basename(param.filters[i].name).replace('_','\_'), offsets[i]/ref_offset), transform = ax.transAxes, color=colors[i], alpha=0.8, horizontalalignment='right', fontsize=9) #### Add zphot zspec plot ax = fig.add_axes((0.67, 0.12, 0.32, 0.86)) zout = catIO.Readfile('%s/%s.zout' %(PATH, root)) dz = (zout.z_peak-zout.z_spec)/(1+zout.z_spec) keep = (zout.z_spec > 0) sigma = threedhst.utils.nmad(dz[keep]) ax.plot(np.log10(1+zout.z_spec), np.log10(1+zout.z_peak), marker='.', alpha=0.1, linestyle='None') ax.plot([0,10],[0,10], color='white', alpha=0.4, linewidth=3) ax.plot([0,10],[0,10], color='black', alpha=0.4, linewidth=1) ax.set_xticklabels([0,1,2,3,4]) xtick = ax.set_xticks(np.log10(1+np.array([0,1,2,3,4]))) ax.set_yticklabels([]) ytick = ax.set_yticks(np.log10(1+np.array([0,1,2,3,4]))) ax.text(0.5, 0.9, r'$\sigma_\mathrm{nmad}=%.3f$' %(sigma), transform = ax.transAxes, color='black', horizontalalignment='center', fontsize=10) ax.set_xlim(0,np.log10(1+4)) ax.set_ylim(0,np.log10(1+4)) #### Save an output image if savefig is not None: fig.savefig(savefig) plt.close()