def test_throw_no_filter_error(self): """Test throws error when filter name not in filter dictionary is supplied """ p = phot.PhotCalcs(self.testsed, self.testfilters) z = 1. self.assertRaises(LookupError, lambda: p.kCorrectionXY("junk", self.filterlist[0], z) ) self.assertRaises(LookupError, lambda: p.kCorrectionXY(self.filterlist[0], "junk", z) )
def test_convert_flux_to_mag(self): """Test cannot pass zero or negative flux to any flux to mag converter""" p = phot.PhotCalcs(self.testsed, self.testfilters) flux = 0. self.assertRaises( ValueError, lambda: p.convertFluxToMag(flux, self.filterlist[0]) ) dFluxOverFlux = 0.1 self.assertRaises( ValueError, lambda: p.convertFluxAndErrorToMags(flux, dFluxOverFlux) ) flux = 1. dFluxOverFlux = -0.1 self.assertRaises( ValueError, lambda: p.convertFluxAndErrorToMags(flux, dFluxOverFlux) )
def test_convert_mag_to_flux_positive(self): """Test converting any reasonable magnitude to a flux returns a positive flux""" p = phot.PhotCalcs(self.testsed, self.testfilters) minMag = -100. maxMag = 100. nMag = 100 dmag = (maxMag - minMag)/(nMag - 1.) for i in xrange(nMag): mag = minMag + i*dmag self.assertGreater(p.convertMagToFlux(mag, self.filterlist[0]), 0.)
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)
def get_sed_colors(sedDict, filterDict, ipivot=-1, doPrinting=True): """Calculate the colors for all the SEDs in sedDict given the filters in filterDict, return as pandas data frame @param sedDict dictionary of SEDs @param filterDict dictionary of filters @param ipivot index of filter to reference ALL colors to (if -1 just does usual) @param doPrinting """ nfilters = len(filterDict) ncolors = nfilters - 1 nseds = len(sedDict) if ipivot > ncolors: raise ValueError("Error! pivot filter outside range") # sort based upon effective wavelength filter_order = sedFilter.orderFiltersByLamEff(filterDict) # get names of colors color_names = [] for i in range(ncolors): color_names.append( str(filter_order[i]) + "-" + str(filter_order[i + 1])) # calculate SED colors sed_colors = np.zeros((nseds, ncolors)) sed_names = [] i = 0 tot_time = 0. for sedname, sed in sedDict.items(): if doPrinting: print "Calculating colors for SED:", sedname sed_names.append(sedname) p = phot.PhotCalcs(sed, filterDict) start_time = time.time() if (ipivot >= 0): # all colors in reference to a pivot filter, e.g. u-r, g-r, r-i, r-z, r-y #ii = 0 colors = [] for j in range(nfilters): if (j < ipivot): colors.append( p.computeColor(filter_order[j], filter_order[ipivot], 0.)) #sed_colors[i,ii] = p.computeColor(filter_order[j], filter_order[ipivot], 0.) if (i < 1): #print ii, print "Doing", filter_order[j], "-", filter_order[ ipivot], print p.computeColor(filter_order[j], filter_order[ipivot], 0.) #ii=+1 elif (j > ipivot): #sed_colors[i,ii] = p.computeColor(filter_order[ipivot], filter_order[j], 0.) colors.append( p.computeColor(filter_order[ipivot], filter_order[j], 0.)) if (i < 1): #print ii, print "Doing", filter_order[ipivot], "-", filter_order[ j], print p.computeColor(filter_order[ipivot], filter_order[j], 0.) #ii=+1 # note that nothing is done when filter index j = ipivot if (i < 1): print colors, len(colors) for j in range(ncolors): sed_colors[i, j] = colors[j] else: # traditional color definition: e.g. u-g, g-r, r-i etc for j in range(ncolors): sed_colors[i, j] = p.computeColor(filter_order[j], filter_order[j + 1], 0.) end_time = time.time() if doPrinting: print "Took", end_time - start_time, "to compute", ncolors, "colors" tot_time += (end_time - start_time) i += 1 if doPrinting: print "Total time to compute colors for SEDs =", tot_time # convert to dataframe and return return pd.DataFrame(sed_colors, columns=color_names, index=sed_names)
def get_sed_array(sedDict, minWavelen=2999., maxWavelen=12000., nWavelen=10000, filterDict=None, color_file=None): """Return array of SEDs on same wavelength grid (optionally along with colors as defined by filterDict) If computing colors, first orders filters by effective wavelength, then a color is: color_{i, i+1} = mag_filter_i - mag_filter_i+1 @param sedDict dictionary of SEDs @param minWavelen minimum wavelength of wavelength grid @param maxWavelen maximum wavelength of wavelength grid @param nWavelen number of points in wavelength grid @param filterDict dictionary of filters @param color_file file to save SED colors to or read colors from (if exists) """ doColors = True if (filterDict == None or color_file == None): doColors = False isFileExist = False if doColors: # sort based upon effective wavelength filter_order = sedFilter.orderFiltersByLamEff(filterDict) # check if file exists and need to calculate colors isFileExist = os.path.isfile(color_file) if (isFileExist): print "\nColors already computed,", else: print "\nComputing colors,", print "placing SEDs in array ..." # loop over each SED nSED = len(sedDict) spectra = [] colors = [] for ised, (sedname, spec) in enumerate(sedDict.items()): print "On SED", ised + 1, "of", nSED, sedname # re-grid SEDs onto same wavelengths waveLen, fl = spec.getSedData(lamMin=minWavelen, lamMax=maxWavelen, nLam=nWavelen) # normalise so they sum to 1 norm = np.sum(fl) spectra.append(fl / norm) if doColors: # calculate or read colors cs = [] if (isFileExist): # reading colors colors_in_file = np.loadtxt(color_file) cs = colors_in_file[ised, :] else: # calculating colors spec = sedFilter.SED(waveLen, fl) #/norm) pcalcs = phot.PhotCalcs(spec, filterDict) # in each filter for i in range(len(filterDict) - 1): color = pcalcs.computeColor(filter_order[i], filter_order[i + 1]) if (color == float('inf')): color = 99. cs.append(color) # store colors for this SED colors.append(cs) # conver to np arrays for ease spectra = np.array(spectra) colors = np.array(colors) # if had to calculate, save colors to file to re-use if (not isFileExist and doColors): print "Saving colors to file for future use" np.savetxt(color_file, colors) if doColors: return waveLen, spectra, colors else: return waveLen, spectra
nz, ]) g_minus_r = np.zeros([ nz, ]) r_minus_i = np.zeros([ nz, ]) # load in elliptical CWW template sedname = '../sed_data/El_B2004a.sed' seddata = np.loadtxt(sedname) sed = sedFilter.SED(seddata[:, 0], seddata[:, 1]) # instantiate photometry calculations p = phot.PhotCalcs(sed, filterDict) # loop over redshifts for i in xrange(nz): z = zmin + i * dz zdata[i] = z g_minus_r[i] = p.computeColor("LSST_g", "LSST_r", z) r_minus_i[i] = p.computeColor("LSST_r", "LSST_i", z) fig = plt.figure(figsize=(20, 10)) fig.suptitle('Elliptical galaxy', fontsize=24) # plot of g-r vs z ax = fig.add_subplot(131)
def test_kcorrection_zero(self): """Test k-correction = 0 when observed-frame filter = rest-frame filter and z=0 """ p = phot.PhotCalcs(self.testsed, self.testfilters) self.assertEqual(p.kCorrectionXY(self.filterlist[0], self.filterlist[0], 0), 0.)
def test_throw_neg_z_error(self): """Test throws error with negative redshift """ p = phot.PhotCalcs(self.testsed, self.testfilters) z = -1. self.assertRaises(ValueError, lambda: p.kCorrectionXY(self.filterlist[0], self.filterlist[1], z) )
def test_convert_mag_error_to_flux(self): """Test cannot pass negative magnitude error to flux converter""" p = phot.PhotCalcs(self.testsed, self.testfilters) errorMag = -1. self.assertRaises( ValueError, lambda: p.convertMagErrorToFracFluxError(errorMag) )