def bake(zgrid): # get SED list listOfSedsFile = "lsst.seds" sedLib = sedFilter.createSedDict(listOfSedsFile, "../data/sed/") sedList = sorted(sedLib.keys()) nSED = len(sedLib) # get LSST filters listOfFiltersFile = "lsst.filters" filterLib = sedFilter.createFilterDict(listOfFiltersFile, "../data/bandpass/") filterList = sedFilter.orderFiltersByLamEff(filterLib) nFilter = len(filterLib) # instantiate photometric calculations pcalcs = {} for sedname, sed in sedLib.items(): pcalcs[sedname] = phot.PhotCalcs(sed, filterLib) nz = len(zgrid) # record array to return n_rows = nSED*nz dtype = np.dtype([('sedname', str, 300), ('redshift', np.float), ('ug', np.float), ('gr', np.float), ('ri', np.float), ('iz', np.float), ('zy', np.float), ('time', np.float)]) dummy = ('aaaa', 1.0, 1, 1, 1, 1, 1, 1.0) records = np.array([dummy]*n_rows, dtype=dtype) i = 0 # loop over redshifts for z in zgrid: # each SED in lib for sedname in sedList: # time calculation of *all* colors for this redshift+SED start_time = time.time() colors = [] for ifilt in range(nFilter-1): mag = pcalcs[sedname].computeColor(filterList[ifilt], filterList[ifilt+1], z) colors.append(mag) end_time = time.time() rec = np.array([(sedname, z, colors[0], colors[1], colors[2], colors[3], colors[4], end_time-start_time)], dtype=records.dtype) records[i] = rec i += 1 return records
else: return False #### Real code starts here print "\n\n\n ***Warning*** --- this program takes a few minutes to run --- \n\n" # filter set of choice is the LSST filters listOfFilters = 'LSST.filters' pathToFilters = '../filter_data' # create a dictionary: keyword is filter name, value is a Filter object filterDict = sedFilter.createFilterDict(listOfFilters, pathToFilters) # return the filter names filterList = sedFilter.getFilterList(listOfFilters, pathToFilters) print 'Filter list = ', filterList nFilter = len(filterList) # load in elliptical SED (1st col: wavelength in A, 2nd col: fluxes in wl units) seddata = np.loadtxt('../sed_data/El_B2004a.sed') # turn into SED object sed = sedFilter.SED(seddata[:,0], seddata[:,1])
def main(argv): save_stem = 'new_lsst' # files will be saved to filenames beginning `save_stem` perf_lim = 3 # performance limit: min number of colors that should reach LSST sys err color_file = "../tmp/brown_colors_lsst.txt" # File to contain colors or to read colors from listOfFilters = 'LSST.filters' # Filter set to use corr_type = 'cubic' # type of covariance function to use in GP theta0 = 0.2 # parameters for GP covariance function try: opts, args = getopt.getopt(argv,"hs:p:c:f:g:") except getopt.GetoptError as err: # if include option that's not there usage(2) for opt, arg in opts: if opt == '-h': usage(0) elif opt in ("-s"): save_stem = arg elif opt in ("-p"): perf_lim = int(arg) elif opt in ("-c"): color_file = arg elif opt in ("-f"): listOfFilters = arg elif opt in ("-g"): corr_type = arg.split(',')[0] theta0 = float(arg.split(',')[1]) print '\n Command line arguments:' print ' Saving to files ... ', save_stem print ' Reading/saving colors from/to file', color_file print ' Using', listOfFilters ,'filter set' print ' At least', perf_lim ,'colors must meet LSST sys err to be `good`' print ' Covariance function will be', corr_type ,'with parameter', theta0 print '' ### Read SEDs into a dictionary listOfSeds = 'brown_masked.seds' pathToSEDs = '../sed_data' sedDict = sedFilter.createSedDict(listOfSeds, pathToSEDs) nSED = len(sedDict) print "Number of SEDs =", nSED ### Filter set to calculate colors pathToFilters = '../filter_data/' filterDict = sedFilter.createFilterDict(listOfFilters, pathToFilters) filterList = sedFilter.orderFiltersByLamEff(filterDict) nFilters = len(filterList) print "Number of filters =", nFilters ### Wavelength grid to do PCA on minWavelen = 1000. maxWavelen = 12000. nWavelen = 10000 ### Do PCA and train GP ncomp = nSED nfit = -1 pcaGP = sedMapper.PcaGaussianProc(sedDict, filterDict, color_file, ncomp, minWavelen, maxWavelen, nWavelen, nfit, corr_type, theta0) colors = pcaGP._colors spectra = pcaGP._spectra waveLen = pcaGP._waveLen meanSpectrum = pcaGP.meanSpec projected_all = pcaGP.eigenvalue_coeffs print "... done\n" ### Leave out each SED in turn delta_mag = np.zeros((nSED,nFilters)) perf = [] for i, (sedname, spec) in enumerate(sedDict.items()): print "\nOn SED", i+1 ,"of", nSED ### Retrain GP with SED removed nc = nSED-1 pcaGP.reTrainGP(nc, i) ### Reconstruct SED sed_rec = pcaGP.generateSpectrum(colors[i,:]) ### Calculate colors of reconstructed SED pcalcs = phot.PhotCalcs(sed_rec, filterDict) cnt = 0 isBad = False for j in range(nFilters-1): cs = pcalcs.computeColor(filterList[j], filterList[j+1]) delta_mag[i,j] = cs-colors[i,j] if (j<6): print "(", cs, colors[i,j], delta_mag[i,j],")" if (abs(delta_mag[i,j])<0.005): cnt+=1 if (abs(delta_mag[i,j])>0.05): isBad = True print "" ### Get array version of SED back wl, spec_rec = sed_rec.getSedData(lamMin=minWavelen, lamMax=maxWavelen, nLam=nWavelen) ### Plot fig = plt.figure(figsize=(10,10)) ax = fig.add_subplot(111) ax.plot(waveLen, spectra[i,:], color='blue', label='true') ax.plot(wl, spec_rec, color='red', linestyle='dashed', label='estimated') ax.plot(waveLen, meanSpectrum, color='black', linestyle='dotted', label='mean') ax.set_xlabel('wavelength (angstroms)', fontsize=24) ax.set_ylabel('flux', fontsize=24) handles, labels = ax.get_legend_handles_labels() ax.legend(loc='lower right', prop={'size':12}) ax.set_title(sedname, fontsize=24) annotate = "Mean $\Delta$ color = {0:.5f} \n".format(np.mean(delta_mag[i,:])) annotate += "Stdn $\Delta$ color = {0:.5f} ".format(np.std(delta_mag[i,:])) y1, y2 = ax.get_ylim() ax.text(9000, 0.9*y2, annotate, fontsize=12) plt.savefig(save_stem + '_' + 'bad_' + sedname + '.png') #plt.show(block=True) ### Performance check print cnt,"colors within LSST systematic error" perf.append(cnt) perf = np.asarray(perf) ### Save results np.savetxt(save_stem + '_deltamag.txt', delta_mag) ### Plot eigenvalue 1 vs eigenvalue 2 fig = plt.figure(figsize=(10,10)) ax = fig.add_subplot(111) ax.plot(projected_all[:, 0], projected_all[:, 1], linestyle='none', marker='o', color='blue', label='good') ax.plot(projected_all[np.where(perf<perf_lim), 0], projected_all[np.where(perf<perf_lim), 1], linestyle='none', marker='o', color='red', label='bad') ax.set_xlabel('eigenvalue 1', fontsize=24) ax.set_ylabel('eigenvalue 2', fontsize=24) handles, labels = ax.get_legend_handles_labels() ax.legend(handles[:4], labels[:4], loc='lower right', prop={'size':12}) ### Histogram of number of colors per SED better than LSST systematic error fig = plt.figure(figsize=(10,10)) ax = fig.add_subplot(111) ax.hist(perf,20, normed=False, histtype='stepfilled') ax.set_xlabel('number of colors better than sys error', fontsize=24) plt.savefig(save_stem + '_' + 'perf.png') plt.show(block=True) ### Histogram of delta-mags for j in range(nFilters-1): fig = plt.figure(figsize=(10,10)) ax = fig.add_subplot(111) dmag_finite = delta_mag[np.where(abs(delta_mag[:,j])<50),j].T ax.hist(dmag_finite, 20, normed=False, histtype='stepfilled') ax.set_xlabel('$\Delta$color$_{' + str(j) + "}$", fontsize=24) plt.savefig(save_stem + '_color' + str(j) + '.png') plt.show(block=True)
"""Very simple demo program that reads in and then plots the LSST filters """ import sedFilter import matplotlib.pyplot as plt # Read in filters into a dictionary (key=filter name, value=Filter object) listOfFiltersFile = "LSST.filters" pathToFile = "../filter_data/" filterDict = sedFilter.createFilterDict(listOfFiltersFile, pathToFile) # start figure fig = plt.figure(figsize=(20,20)) ifilt = 1 for filtname, filt in filterDict.iteritems(): wavelengths, transmissions = filt.getFilterData() # plot ax = fig.add_subplot(2,3,ifilt) ax.plot(wavelengths, transmissions, linestyle='solid', color='black', label=filtname) ax.set_xlabel('wavelength (angstroms)', fontsize=24) ax.set_ylabel('transmission', fontsize=24) handles, labels = ax.get_legend_handles_labels() ax.legend(labels) ax.legend(loc='lower right',prop={'size':12}) ifilt+=1
"""Compare E2V and ITL filters """ import sedFilter import matplotlib.pyplot as plt ### Read in ITL filters listOfFiltersFile = "LSST_ITL.filters" pathToFile = "../filter_data/" itl = sedFilter.createFilterDict(listOfFiltersFile, pathToFile) itl_list = sedFilter.orderFiltersByLamEff(itl) ### Read in E2V filters listOfFiltersFile = "LSST_E2V.filters" pathToFile = "../filter_data/" e2v = sedFilter.createFilterDict(listOfFiltersFile, pathToFile) e2v_list = sedFilter.orderFiltersByLamEff(e2v) ### start figure fig = plt.figure(figsize=(20,20)) ifilt = 1 for itlname,e2vname in zip(itl_list,e2v_list): wavelengths, transmissions = itl[itlname].getFilterData() wavelengths_e2v, transmissions_e2v = e2v[e2vname].getFilterData()
import os import numpy as np import cphotometry as cphot import sedFilter import cosmo import photErrorModel as pem # Single SED for testing fname = os.path.join(os.path.dirname(__file__), '../sed_data/El_B2004a.sed') seddata = np.loadtxt(fname) TEST_SED = sedFilter.SED(seddata[:,0], seddata[:,1]) # List of filters for testing listOfFilters = 'LSST.filters' pathToFilters = '../filter_data' TEST_FILTERDICT = sedFilter.createFilterDict(listOfFilters, pathToFilters) FILTERLIST = sedFilter.orderFiltersByLamEff(TEST_FILTERDICT) # List of SEDs for testing SEDLIST = [] sedfiles = ['../sed_data/El_B2004a.sed','../sed_data/Sbc_B2004a.sed','../sed_data/Scd_B2004a.sed', '../sed_data/Im_B2004a.sed'] for name in sedfiles: seddata = np.loadtxt(name) SEDLIST.append(sedFilter.SED(seddata[:,0], seddata[:,1])) # Cosmological model COSMOMODEL = cosmo.cosmologyCalculator() class TestPhotCalcs(unittest.TestCase):
def main(argv): save_stem = 'new_lsst' # files will be saved to filenames beginning `save_stem` perf_lim = 3 # performance limit: min number of colors that should reach LSST sys err color_file = "../tmp/brown_colors_lsst.txt" # File to contain colors or to read colors from listOfFilters = 'LSST.filters' # Filter set to use corr_type = 'cubic' # type of covariance function to use in GP theta0 = 0.2 # parameters for GP covariance function try: opts, args = getopt.getopt(argv, "hs:p:c:f:g:") except getopt.GetoptError as err: # if include option that's not there usage(2) for opt, arg in opts: if opt == '-h': usage(0) elif opt in ("-s"): save_stem = arg elif opt in ("-p"): perf_lim = int(arg) elif opt in ("-c"): color_file = arg elif opt in ("-f"): listOfFilters = arg elif opt in ("-g"): corr_type = arg.split(',')[0] theta0 = float(arg.split(',')[1]) print '\n Command line arguments:' print ' Saving to files ... ', save_stem print ' Reading/saving colors from/to file', color_file print ' Using', listOfFilters, 'filter set' print ' At least', perf_lim, 'colors must meet LSST sys err to be `good`' print ' Covariance function will be', corr_type, 'with parameter', theta0 print '' ### Read SEDs into a dictionary listOfSeds = 'brown_masked.seds' pathToSEDs = '../sed_data' sedDict = sedFilter.createSedDict(listOfSeds, pathToSEDs) nSED = len(sedDict) print "Number of SEDs =", nSED ### Filter set to calculate colors pathToFilters = '../filter_data/' filterDict = sedFilter.createFilterDict(listOfFilters, pathToFilters) filterList = sedFilter.orderFiltersByLamEff(filterDict) nFilters = len(filterList) print "Number of filters =", nFilters ### Wavelength grid to do PCA on minWavelen = 1000. maxWavelen = 12000. nWavelen = 10000 ### Do PCA and train GP ncomp = nSED nfit = -1 pcaGP = sedMapper.PcaGaussianProc(sedDict, filterDict, color_file, ncomp, minWavelen, maxWavelen, nWavelen, nfit, corr_type, theta0) colors = pcaGP._colors spectra = pcaGP._spectra waveLen = pcaGP._waveLen meanSpectrum = pcaGP.meanSpec projected_all = pcaGP.eigenvalue_coeffs print "... done\n" ### Leave out each SED in turn delta_mag = np.zeros((nSED, nFilters)) perf = [] for i, (sedname, spec) in enumerate(sedDict.items()): print "\nOn SED", i + 1, "of", nSED ### Retrain GP with SED removed nc = nSED - 1 pcaGP.reTrainGP(nc, i) ### Reconstruct SED sed_rec = pcaGP.generateSpectrum(colors[i, :]) ### Calculate colors of reconstructed SED pcalcs = phot.PhotCalcs(sed_rec, filterDict) cnt = 0 isBad = False for j in range(nFilters - 1): cs = pcalcs.computeColor(filterList[j], filterList[j + 1]) delta_mag[i, j] = cs - colors[i, j] if (j < 6): print "(", cs, colors[i, j], delta_mag[i, j], ")" if (abs(delta_mag[i, j]) < 0.005): cnt += 1 if (abs(delta_mag[i, j]) > 0.05): isBad = True print "" ### Get array version of SED back wl, spec_rec = sed_rec.getSedData(lamMin=minWavelen, lamMax=maxWavelen, nLam=nWavelen) ### Plot fig = plt.figure(figsize=(10, 10)) ax = fig.add_subplot(111) ax.plot(waveLen, spectra[i, :], color='blue', label='true') ax.plot(wl, spec_rec, color='red', linestyle='dashed', label='estimated') ax.plot(waveLen, meanSpectrum, color='black', linestyle='dotted', label='mean') ax.set_xlabel('wavelength (angstroms)', fontsize=24) ax.set_ylabel('flux', fontsize=24) handles, labels = ax.get_legend_handles_labels() ax.legend(loc='lower right', prop={'size': 12}) ax.set_title(sedname, fontsize=24) annotate = "Mean $\Delta$ color = {0:.5f} \n".format( np.mean(delta_mag[i, :])) annotate += "Stdn $\Delta$ color = {0:.5f} ".format( np.std(delta_mag[i, :])) y1, y2 = ax.get_ylim() ax.text(9000, 0.9 * y2, annotate, fontsize=12) plt.savefig(save_stem + '_' + 'bad_' + sedname + '.png') #plt.show(block=True) ### Performance check print cnt, "colors within LSST systematic error" perf.append(cnt) perf = np.asarray(perf) ### Save results np.savetxt(save_stem + '_deltamag.txt', delta_mag) ### Plot eigenvalue 1 vs eigenvalue 2 fig = plt.figure(figsize=(10, 10)) ax = fig.add_subplot(111) ax.plot(projected_all[:, 0], projected_all[:, 1], linestyle='none', marker='o', color='blue', label='good') ax.plot(projected_all[np.where(perf < perf_lim), 0], projected_all[np.where(perf < perf_lim), 1], linestyle='none', marker='o', color='red', label='bad') ax.set_xlabel('eigenvalue 1', fontsize=24) ax.set_ylabel('eigenvalue 2', fontsize=24) handles, labels = ax.get_legend_handles_labels() ax.legend(handles[:4], labels[:4], loc='lower right', prop={'size': 12}) ### Histogram of number of colors per SED better than LSST systematic error fig = plt.figure(figsize=(10, 10)) ax = fig.add_subplot(111) ax.hist(perf, 20, normed=False, histtype='stepfilled') ax.set_xlabel('number of colors better than sys error', fontsize=24) plt.savefig(save_stem + '_' + 'perf.png') plt.show(block=True) ### Histogram of delta-mags for j in range(nFilters - 1): fig = plt.figure(figsize=(10, 10)) ax = fig.add_subplot(111) dmag_finite = delta_mag[np.where(abs(delta_mag[:, j]) < 50), j].T ax.hist(dmag_finite, 20, normed=False, histtype='stepfilled') ax.set_xlabel('$\Delta$color$_{' + str(j) + "}$", fontsize=24) plt.savefig(save_stem + '_color' + str(j) + '.png') plt.show(block=True)
import os import numpy as np import cphotometry as cphot import sedFilter import cosmo import photErrorModel as pem # Single SED for testing fname = os.path.join(os.path.dirname(__file__), '../sed_data/El_B2004a.sed') seddata = np.loadtxt(fname) TEST_SED = sedFilter.SED(seddata[:, 0], seddata[:, 1]) # List of filters for testing listOfFilters = 'LSST.filters' pathToFilters = '../filter_data' TEST_FILTERDICT = sedFilter.createFilterDict(listOfFilters, pathToFilters) FILTERLIST = sedFilter.orderFiltersByLamEff(TEST_FILTERDICT) # List of SEDs for testing SEDLIST = [] sedfiles = [ '../sed_data/El_B2004a.sed', '../sed_data/Sbc_B2004a.sed', '../sed_data/Scd_B2004a.sed', '../sed_data/Im_B2004a.sed' ] for name in sedfiles: seddata = np.loadtxt(name) SEDLIST.append(sedFilter.SED(seddata[:, 0], seddata[:, 1])) # Cosmological model COSMOMODEL = cosmo.cosmologyCalculator()