def calcMagNorm(self, objectMags, sedObj, bandpassDict, mag_error=None, redshift=None, filtRange=None): """ This will find the magNorm value that gives the closest match to the magnitudes of the object using the matched SED. Uses scipy.optimize.leastsq to find the values of fluxNorm that minimizes the function: ((flux_obs - (fluxNorm*flux_model))/flux_error)**2. @param [in] objectMags are the magnitude values for the object with extinction matching that of the SED object. In the normal case using the selectSED routines above it will be dereddened mags. @param [in] sedObj is an Sed class instance that is set with the wavelength and flux of the matched SED @param [in] bandpassDict is a BandpassDict class instance with the Bandpasses set to those for the magnitudes given for the catalog object @param [in] mag_error are provided error values for magnitudes in objectMags. If none provided then this defaults to 1.0. This should be an array of the same length as objectMags. @param [in] redshift is the redshift of the object if the magnitude is observed @param [in] filtRange is a selected range of filters specified by their indices in the bandpassList to match up against. Used when missing data in some magnitude bands. @param [out] bestMagNorm is the magnitude normalization for the given magnitudes and SED """ import scipy.optimize as opt sedTest = Sed() sedTest.setSED(sedObj.wavelen, flambda=sedObj.flambda) if redshift is not None: sedTest.redshiftSED(redshift) imSimBand = Bandpass() imSimBand.imsimBandpass() zp = -2.5 * np.log10(3631) #Note using default AB zeropoint flux_obs = np.power(10, (objectMags + zp) / (-2.5)) sedTest.resampleSED(wavelen_match=bandpassDict.values()[0].wavelen) sedTest.flambdaTofnu() flux_model = sedTest.manyFluxCalc(bandpassDict.phiArray, bandpassDict.wavelenStep) if filtRange is not None: flux_obs = flux_obs[filtRange] flux_model = flux_model[filtRange] if mag_error is None: flux_error = np.ones(len(flux_obs)) else: flux_error = np.abs(flux_obs * (np.log(10) / (-2.5)) * mag_error) bestFluxNorm = opt.leastsq( lambda x: ((flux_obs - (x * flux_model)) / flux_error), 1.0)[0][0] sedTest.multiplyFluxNorm(bestFluxNorm) bestMagNorm = sedTest.calcMag(imSimBand) return bestMagNorm
def calcMagNorm(self, objectMags, sedObj, bandpassDict, mag_error = None, redshift = None, filtRange = None): """ This will find the magNorm value that gives the closest match to the magnitudes of the object using the matched SED. Uses scipy.optimize.leastsq to find the values of fluxNorm that minimizes the function: ((flux_obs - (fluxNorm*flux_model))/flux_error)**2. @param [in] objectMags are the magnitude values for the object with extinction matching that of the SED object. In the normal case using the selectSED routines above it will be dereddened mags. @param [in] sedObj is an Sed class instance that is set with the wavelength and flux of the matched SED @param [in] bandpassDict is a BandpassDict class instance with the Bandpasses set to those for the magnitudes given for the catalog object @param [in] mag_error are provided error values for magnitudes in objectMags. If none provided then this defaults to 1.0. This should be an array of the same length as objectMags. @param [in] redshift is the redshift of the object if the magnitude is observed @param [in] filtRange is a selected range of filters specified by their indices in the bandpassList to match up against. Used when missing data in some magnitude bands. @param [out] bestMagNorm is the magnitude normalization for the given magnitudes and SED """ import scipy.optimize as opt sedTest = Sed() sedTest.setSED(sedObj.wavelen, flambda = sedObj.flambda) if redshift is not None: sedTest.redshiftSED(redshift) imSimBand = Bandpass() imSimBand.imsimBandpass() zp = -2.5*np.log10(3631) #Note using default AB zeropoint flux_obs = np.power(10,(objectMags + zp)/(-2.5)) sedTest.resampleSED(wavelen_match=bandpassDict.values()[0].wavelen) sedTest.flambdaTofnu() flux_model = sedTest.manyFluxCalc(bandpassDict.phiArray, bandpassDict.wavelenStep) if filtRange is not None: flux_obs = flux_obs[filtRange] flux_model = flux_model[filtRange] if mag_error is None: flux_error = np.ones(len(flux_obs)) else: flux_error = np.abs(flux_obs*(np.log(10)/(-2.5))*mag_error) bestFluxNorm = opt.leastsq(lambda x: ((flux_obs - (x*flux_model))/flux_error), 1.0)[0][0] sedTest.multiplyFluxNorm(bestFluxNorm) bestMagNorm = sedTest.calcMag(imSimBand) return bestMagNorm
def testCalcMagNorm(self): """Tests the calculation of magnitude normalization for an SED with the given magnitudes in the given bandpasses.""" testUtils = matchBase() bandpassDir = os.path.join(lsst.utils.getPackageDir('throughputs'), 'sdss') testPhot = BandpassDict.loadTotalBandpassesFromFiles( self.filterList, bandpassDir=bandpassDir, bandpassRoot='sdss_') unChangedSED = Sed() unChangedSED.readSED_flambda( str(self.galDir + os.listdir(self.galDir)[0])) imSimBand = Bandpass() imSimBand.imsimBandpass() testSED = Sed() testSED.setSED(unChangedSED.wavelen, flambda=unChangedSED.flambda) magNorm = 20.0 redVal = 0.1 testSED.redshiftSED(redVal) fluxNorm = testSED.calcFluxNorm(magNorm, imSimBand) testSED.multiplyFluxNorm(fluxNorm) sedMags = testPhot.magListForSed(testSED) stepSize = 0.001 testMagNorm = testUtils.calcMagNorm(sedMags, unChangedSED, testPhot, redshift=redVal) # Test adding in mag_errors. If an array of np.ones is passed in we should get same result testMagNormWithErr = testUtils.calcMagNorm(sedMags, unChangedSED, testPhot, mag_error=np.ones( len(sedMags)), redshift=redVal) # Also need to add in test for filtRange sedMagsIncomp = sedMags sedMagsIncomp[1] = None filtRangeTest = [0, 2, 3, 4] testMagNormFiltRange = testUtils.calcMagNorm(sedMagsIncomp, unChangedSED, testPhot, redshift=redVal, filtRange=filtRangeTest) self.assertAlmostEqual(magNorm, testMagNorm, delta=stepSize) self.assertAlmostEqual(magNorm, testMagNormWithErr, delta=stepSize) self.assertAlmostEqual(magNorm, testMagNormFiltRange, delta=stepSize)
def testCalcMagNorm(self): """Tests the calculation of magnitude normalization for an SED with the given magnitudes in the given bandpasses.""" testUtils = matchBase() bandpassDir = os.path.join(lsst.utils.getPackageDir('throughputs'), 'sdss') testPhot = BandpassDict.loadTotalBandpassesFromFiles(self.filterList, bandpassDir = bandpassDir, bandpassRoot = 'sdss_') unChangedSED = Sed() unChangedSED.readSED_flambda(str(self.galDir + os.listdir(self.galDir)[0])) imSimBand = Bandpass() imSimBand.imsimBandpass() testSED = Sed() testSED.setSED(unChangedSED.wavelen, flambda = unChangedSED.flambda) magNorm = 20.0 redVal = 0.1 testSED.redshiftSED(redVal) fluxNorm = testSED.calcFluxNorm(magNorm, imSimBand) testSED.multiplyFluxNorm(fluxNorm) sedMags = testPhot.magListForSed(testSED) stepSize = 0.001 testMagNorm = testUtils.calcMagNorm(sedMags, unChangedSED, testPhot, redshift = redVal) # Test adding in mag_errors. If an array of np.ones is passed in we should get same result testMagNormWithErr = testUtils.calcMagNorm(sedMags, unChangedSED, testPhot, mag_error = np.ones(len(sedMags)), redshift = redVal) # Also need to add in test for filtRange sedMagsIncomp = sedMags sedMagsIncomp[1] = None filtRangeTest = [0, 2, 3, 4] testMagNormFiltRange = testUtils.calcMagNorm(sedMagsIncomp, unChangedSED, testPhot, redshift = redVal, filtRange = filtRangeTest) self.assertAlmostEqual(magNorm, testMagNorm, delta = stepSize) self.assertAlmostEqual(magNorm, testMagNormWithErr, delta = stepSize) self.assertAlmostEqual(magNorm, testMagNormFiltRange, delta = stepSize)
def matchToObserved(self, sedList, catMags, catRedshifts, catRA = None, catDec = None, mag_error = None, bandpassDict = None, dzAcc = 2, reddening = True, extCoeffs = (4.239, 3.303, 2.285, 1.698, 1.263)): """ This will find the closest match to the magnitudes of a galaxy catalog if those magnitudes are in the observed frame and can correct for reddening from within the milky way as well if needed. In order to make things faster it first calculates colors for all model SEDs at redshifts between the minimum and maximum redshifts of the catalog objects provided with a grid spacing in redshift defined by the parameter dzAcc. Objects without magnitudes in at least two adjacent bandpasses will return as none and print out a message. @param [in] sedList is the set of spectral objects from the models SEDs provided by loadBC03 or other custom loader routine. @param [in] catMags is an array of the magnitudes of catalog objects to be matched with a model SED. It should be organized so that there is one object's magnitudes along each row. @param [in] catRedshifts is an array of the redshifts of each catalog object. @param [in] catRA is an array of the RA positions for each catalog object. @param [in] catDec is an array of the Dec position for each catalog object. @param [in] mag_error are provided error values for magnitudes in objectMags. If none provided then this defaults to 1.0. This should be an array of the same size as catMags. @param [in] bandpassDict is a BandpassDict with which to calculate magnitudes. If left equal to None it will by default load the SDSS [u,g,r,i,z] bandpasses and therefore agree with default extCoeffs. @param [in] dzAcc is the number of decimal places you want to use when building the redshift grid. For example, dzAcc = 2 will create a grid between the minimum and maximum redshifts with colors calculated at every 0.01 change in redshift. @param [in] reddening is a boolean that determines whether to correct catalog magnitudes for dust in the milky way. By default, it is True. If true, this uses calculateEBV from EBV.py to find an EBV value for the object's ra and dec coordinates and then uses the coefficients provided by extCoeffs which should come from Schlafly and Finkbeiner (2011) for the correct filters and in the same order as provided in bandpassDict. If false, this means it will not run the dereddening procedure. @param [in] extCoeffs are the Schlafly and Finkbeiner (2011) (ApJ, 737, 103) coefficients for the given filters from bandpassDict and need to be in the same order as bandpassDict. The default given are the SDSS [u,g,r,i,z] values. @param [out] sedMatches is a list with the name of a model SED that matches most closely to each object in the catalog. @param [out] magNormMatches are the magnitude normalizations for the given magnitudes and matched SED. @param [out] matchErrors contains the Mean Squared Error between the colors of each object and the colors of the matched SED. """ #Set up photometry to calculate model Mags if bandpassDict is None: galPhot = BandpassDict.loadTotalBandpassesFromFiles(['u','g','r','i','z'], bandpassDir = os.path.join(lsst.utils.getPackageDir('throughputs'),'sdss'), bandpassRoot = 'sdss_') else: galPhot = bandpassDict #Calculate ebv from ra, dec coordinates if needed if reddening == True: #Check that catRA and catDec are included if catRA is None or catDec is None: raise RuntimeError("Reddening is True, but catRA and catDec are not included.") calcEBV = ebv() raDec = np.array((catRA,catDec)) #If only matching one object need to reshape for calculateEbv if len(raDec.shape) == 1: raDec = raDec.reshape((2,1)) ebvVals = calcEBV.calculateEbv(equatorialCoordinates = raDec) objMags = self.deReddenMags(ebvVals, catMags, extCoeffs) else: objMags = catMags minRedshift = np.round(np.min(catRedshifts), dzAcc) maxRedshift = np.round(np.max(catRedshifts), dzAcc) dz = np.power(10., (-1*dzAcc)) redshiftRange = np.round(np.arange(minRedshift - dz, maxRedshift + (2*dz), dz), dzAcc) numRedshifted = 0 sedMatches = [None] * len(catRedshifts) magNormMatches = [None] * len(catRedshifts) matchErrors = [None] * len(catRedshifts) redshiftIndex = np.argsort(catRedshifts) numOn = 0 notMatched = 0 lastRedshift = -100 print 'Starting Matching. Arranged by redshift value.' for redshift in redshiftRange: if numRedshifted % 10 == 0: print '%i out of %i redshifts gone through' % (numRedshifted, len(redshiftRange)) numRedshifted += 1 colorSet = [] for galSpec in sedList: sedColors = [] fileSED = Sed() fileSED.setSED(wavelen = galSpec.wavelen, flambda = galSpec.flambda) fileSED.redshiftSED(redshift) sedColors = self.calcBasicColors([fileSED], galPhot, makeCopy = True) colorSet.append(sedColors) colorSet = np.transpose(colorSet) for currentIndex in redshiftIndex[numOn:]: matchMags = objMags[currentIndex] if lastRedshift < np.round(catRedshifts[currentIndex],dzAcc) <= redshift: colorRange = np.arange(0, len(galPhot)-1) matchColors = [] for colorNum in colorRange: matchColors.append(matchMags[colorNum] - matchMags[colorNum+1]) #This is done to handle objects with incomplete magnitude data filtNums = np.arange(0, len(galPhot)) if np.isnan(np.amin(matchColors))==True: colorRange = np.where(np.isnan(matchColors)==False)[0] filtNums = np.unique([colorRange, colorRange+1]) #Pick right filters in calcMagNorm if len(colorRange) == 0: print 'Could not match object #%i. No magnitudes for two adjacent bandpasses.' \ % (currentIndex) notMatched += 1 #Don't need to assign 'None' here in result array, b/c 'None' is default value else: distanceArray = [np.zeros(len(sedList))] for colorNum in colorRange: distanceArray += np.power((colorSet[colorNum] - matchColors[colorNum]),2) matchedSEDNum = np.nanargmin(distanceArray) sedMatches[currentIndex] = sedList[matchedSEDNum].name magNormVal = self.calcMagNorm(np.array(matchMags), sedList[matchedSEDNum], galPhot, mag_error = mag_error, redshift = catRedshifts[currentIndex], filtRange = filtNums) magNormMatches[currentIndex] = magNormVal matchErrors[currentIndex] = (distanceArray[0,matchedSEDNum]/len(colorRange)) numOn += 1 else: break lastRedshift = redshift print 'Done Matching. Matched %i of %i catalog objects to SEDs' % (len(catMags)-notMatched, len(catMags)) if notMatched > 0: print '%i objects did not get matched.' % (notMatched) return sedMatches, magNormMatches, matchErrors
def applyIGM(self, redshift, sedobj): """ Apply IGM extinction to already redshifted sed with redshift between zMin and zMax defined by range of lookup tables @param [in] redshift is the redshift of the incoming SED object @param [in] sedobj is the SED object to which IGM extinction will be applied. This object will be modified as a result of this. """ if self.IGMisInitialized == False: self.initializeIGM() #First make sure redshift is in range of lookup tables. if (redshift < self.zMin) or (redshift > self.zMax): warnings.warn( str("IGM Lookup tables only applicable for " + str(self.zMin) + " < z < " + str(self.zMax) + ". No action taken")) return #Now read in closest two lookup tables for given redshift lowerSed = Sed() upperSed = Sed() for lower, upper in zip(self.zRange[:-1], self.zRange[1:]): if lower <= redshift <= upper: lowerSed.setSED(self.meanLookups[str(lower)][:, 0], flambda=self.meanLookups[str(lower)][:, 1]) upperSed.setSED(self.meanLookups[str(upper)][:, 0], flambda=self.meanLookups[str(upper)][:, 1]) break #Redshift lookup tables to redshift of source, i.e. if source redshift is 1.78 shift lookup #table for 1.7 and lookup table for 1.8 to up and down to 1.78, respectively zLowerShift = ((1.0 + redshift) / (1.0 + lower)) - 1.0 zUpperShift = ((1.0 + redshift) / (1.0 + upper)) - 1.0 lowerSed.redshiftSED(zLowerShift) upperSed.redshiftSED(zUpperShift) #Resample lower and upper transmission data onto same wavelength grid. minWavelen = 300. #All lookup tables are usable above 300nm maxWavelen = np.amin([lowerSed.wavelen[-1], upperSed.wavelen[-1] ]) - 0.01 lowerSed.resampleSED(wavelen_min=minWavelen, wavelen_max=maxWavelen, wavelen_step=0.01) upperSed.resampleSED(wavelen_match=lowerSed.wavelen) #Now insert this into a transmission array of 1.0 beyond the limits of current application #So that we can get an sed back that extends to the longest wavelengths of the incoming sed finalWavelen = np.arange(300., sedobj.wavelen[-1] + 0.01, 0.01) finalFlambdaExtended = np.ones(len(finalWavelen)) #Weighted Average of Transmission from each lookup table to get final transmission #table at desired redshift dzGrid = self.zDelta #Step in redshift between transmission lookup table files finalSed = Sed() finalFlambda = (lowerSed.flambda * (1.0 - ((redshift - lower) / dzGrid)) + upperSed.flambda * (1.0 - ((upper - redshift) / dzGrid))) finalFlambdaExtended[0:len(finalFlambda)] = finalFlambda finalSed.setSED(wavelen=finalWavelen, flambda=finalFlambdaExtended) #Resample incoming sed to new grid so that we don't get warnings from multiplySED #about matching wavelength grids sedobj.resampleSED(wavelen_match=finalSed.wavelen) #Now multiply transmission curve by input SED to get final result and make it the new flambda #data in the original sed which also is now on a new grid starting at 300 nm test = sedobj.multiplySED(finalSed) sedobj.flambda = test.flambda
def testMatchToObserved(self): """Test that Galaxy SEDs with extinction or redshift are matched correctly""" rng = np.random.RandomState(42) galPhot = BandpassDict.loadTotalBandpassesFromFiles() imSimBand = Bandpass() imSimBand.imsimBandpass() testMatching = selectGalaxySED(galDir = self.testSpecDir) testSEDList = testMatching.loadBC03() testSEDNames = [] testRA = [] testDec = [] testRedshifts = [] testMagNormList = [] magNormStep = 1 extCoeffs = [1.8140, 1.4166, 0.9947, 0.7370, 0.5790, 0.4761] testMags = [] testMagsRedshift = [] testMagsExt = [] for testSED in testSEDList: # As a check make sure that it matches when no extinction and no redshift are present getSEDMags = Sed() testSEDNames.append(testSED.name) getSEDMags.setSED(wavelen = testSED.wavelen, flambda = testSED.flambda) testMags.append(galPhot.magListForSed(getSEDMags)) # Check Extinction corrections sedRA = rng.uniform(10, 170) sedDec = rng.uniform(10, 80) testRA.append(sedRA) testDec.append(sedDec) raDec = np.array((sedRA, sedDec)).reshape((2, 1)) ebvVal = ebv().calculateEbv(equatorialCoordinates = raDec) extVal = ebvVal*extCoeffs testMagsExt.append(galPhot.magListForSed(getSEDMags) + extVal) # Setup magnitudes for testing matching to redshifted values getRedshiftMags = Sed() testZ = np.round(rng.uniform(1.1, 1.3), 3) testRedshifts.append(testZ) testMagNorm = np.round(rng.uniform(20.0, 22.0), magNormStep) testMagNormList.append(testMagNorm) getRedshiftMags.setSED(wavelen = testSED.wavelen, flambda = testSED.flambda) getRedshiftMags.redshiftSED(testZ) fluxNorm = getRedshiftMags.calcFluxNorm(testMagNorm, imSimBand) getRedshiftMags.multiplyFluxNorm(fluxNorm) testMagsRedshift.append(galPhot.magListForSed(getRedshiftMags)) # Will also test in passing of non-default bandpass testNoExtNoRedshift = testMatching.matchToObserved(testSEDList, testMags, np.zeros(8), reddening = False, bandpassDict = galPhot) testMatchingEbvVals = testMatching.matchToObserved(testSEDList, testMagsExt, np.zeros(8), catRA = testRA, catDec = testDec, reddening = True, extCoeffs = extCoeffs, bandpassDict = galPhot) # Substitute in nan values to simulate incomplete data and make sure magnorm works too. testMagsRedshift[0][1] = np.nan testMagsRedshift[0][3] = np.nan testMagsRedshift[0][4] = np.nan testMagsRedshift[1][1] = np.nan testMatchingRedshift = testMatching.matchToObserved(testSEDList, testMagsRedshift, testRedshifts, dzAcc = 3, reddening = False, bandpassDict = galPhot) self.assertEqual(testSEDNames, testNoExtNoRedshift[0]) self.assertEqual(testSEDNames, testMatchingEbvVals[0]) self.assertEqual(None, testMatchingRedshift[0][0]) self.assertEqual(testSEDNames[1:], testMatchingRedshift[0][1:]) self.assertEqual(None, testMatchingRedshift[1][0]) np.testing.assert_almost_equal(testMagNormList[1:], testMatchingRedshift[1][1:], decimal = magNormStep) # Test Match Errors errMag = testMagsRedshift[2] errRedshift = testRedshifts[2] errMags = np.array((errMag, errMag, errMag, errMag)) errRedshifts = np.array((errRedshift, errRedshift, errRedshift, errRedshift)) errMags[1, 1] += 1. # Total MSE will be 2/(5 colors) = 0.4 errMags[2, 0:2] = np.nan errMags[2, 3] += 1. # Total MSE will be 2/(3 colors) = 0.667 errMags[3, :] = None errSED = testSEDList[2] testMatchingResultsErrors = testMatching.matchToObserved([errSED], errMags, errRedshifts, reddening = False, bandpassDict = galPhot, dzAcc = 3) np.testing.assert_almost_equal(np.array((0.0, 0.4, 2./3.)), testMatchingResultsErrors[2][0:3], decimal = 2) # Give a little more leeway due to redshifting effects self.assertEqual(None, testMatchingResultsErrors[2][3])
def testAlternateBandpassesGalaxies(self): """ the same as testAlternateBandpassesStars, but for galaxies """ obs_metadata_pointed = ObservationMetaData(mjd=50000.0, boundType='circle', pointingRA=0.0, pointingDec=0.0, boundLength=10.0) dtype = np.dtype([('galid', np.int), ('ra', np.float), ('dec', np.float), ('uTotal', np.float), ('gTotal', np.float), ('rTotal', np.float), ('iTotal', np.float), ('zTotal', np.float), ('uBulge', np.float), ('gBulge', np.float), ('rBulge', np.float), ('iBulge', np.float), ('zBulge', np.float), ('uDisk', np.float), ('gDisk', np.float), ('rDisk', np.float), ('iDisk', np.float), ('zDisk', np.float), ('uAgn', np.float), ('gAgn', np.float), ('rAgn', np.float), ('iAgn', np.float), ('zAgn', np.float), ('bulgeName', str, 200), ('bulgeNorm', np.float), ('bulgeAv', np.float), ('diskName', str, 200), ('diskNorm', np.float), ('diskAv', np.float), ('agnName', str, 200), ('agnNorm', np.float), ('redshift', np.float)]) test_cat = cartoonGalaxies(self.galaxy, obs_metadata=obs_metadata_pointed) with lsst.utils.tests.getTempFilePath('.txt') as catName: test_cat.write_catalog(catName) catData = np.genfromtxt(catName, dtype=dtype, delimiter=', ') self.assertGreater(len(catData), 0) cartoonDir = getPackageDir('sims_photUtils') cartoonDir = os.path.join(cartoonDir, 'tests', 'cartoonSedTestData') sedDir = getPackageDir('sims_sed_library') testBandpasses = {} keys = ['u', 'g', 'r', 'i', 'z'] for kk in keys: testBandpasses[kk] = Bandpass() testBandpasses[kk].readThroughput(os.path.join(cartoonDir, "test_bandpass_%s.dat" % kk)) imsimBand = Bandpass() imsimBand.imsimBandpass() specMap = defaultSpecMap ct = 0 for line in catData: bulgeMagList = [] diskMagList = [] agnMagList = [] if line['bulgeName'] == 'None': for bp in keys: np.testing.assert_equal(line['%sBulge' % bp], np.NaN) bulgeMagList.append(np.NaN) else: ct += 1 dummySed = Sed() dummySed.readSED_flambda(os.path.join(sedDir, specMap[line['bulgeName']])) fnorm = dummySed.calcFluxNorm(line['bulgeNorm'], imsimBand) dummySed.multiplyFluxNorm(fnorm) a_int, b_int = dummySed.setupCCM_ab() dummySed.addDust(a_int, b_int, A_v=line['bulgeAv']) dummySed.redshiftSED(line['redshift'], dimming=True) dummySed.resampleSED(wavelen_match=testBandpasses['u'].wavelen) for bpName in keys: mag = dummySed.calcMag(testBandpasses[bpName]) self.assertAlmostEqual(mag, line['%sBulge' % bpName], 10) bulgeMagList.append(mag) if line['diskName'] == 'None': for bp in keys: np.assert_equal(line['%sDisk' % bp], np.NaN) diskMagList.append(np.NaN) else: ct += 1 dummySed = Sed() dummySed.readSED_flambda(os.path.join(sedDir, specMap[line['diskName']])) fnorm = dummySed.calcFluxNorm(line['diskNorm'], imsimBand) dummySed.multiplyFluxNorm(fnorm) a_int, b_int = dummySed.setupCCM_ab() dummySed.addDust(a_int, b_int, A_v=line['diskAv']) dummySed.redshiftSED(line['redshift'], dimming=True) dummySed.resampleSED(wavelen_match=testBandpasses['u'].wavelen) for bpName in keys: mag = dummySed.calcMag(testBandpasses[bpName]) self.assertAlmostEqual(mag, line['%sDisk' % bpName], 10) diskMagList.append(mag) if line['agnName'] == 'None': for bp in keys: np.testing.assert_true(line['%sAgn' % bp], np.NaN) agnMagList.append(np.NaN) else: ct += 1 dummySed = Sed() dummySed.readSED_flambda(os.path.join(sedDir, specMap[line['agnName']])) fnorm = dummySed.calcFluxNorm(line['agnNorm'], imsimBand) dummySed.multiplyFluxNorm(fnorm) dummySed.redshiftSED(line['redshift'], dimming=True) dummySed.resampleSED(wavelen_match=testBandpasses['u'].wavelen) for bpName in keys: mag = dummySed.calcMag(testBandpasses[bpName]) self.assertAlmostEqual(mag, line['%sAgn' % bpName], 10) agnMagList.append(mag) totalMags = PhotometryGalaxies().sum_magnitudes(bulge=np.array(bulgeMagList), disk=np.array(diskMagList), agn=np.array(agnMagList)) for testMag, bpName in zip(totalMags, keys): if np.isnan(line['%sTotal' % bpName]): np.testing.assert_equal(testMag, np.NaN) else: self.assertAlmostEqual(testMag, line['%sTotal' % bpName], 10) self.assertGreater(ct, 0)
def testAlternateBandpassesGalaxies(self): """ the same as testAlternateBandpassesStars, but for galaxies """ obs_metadata_pointed = ObservationMetaData(mjd=50000.0, boundType='circle', pointingRA=0.0, pointingDec=0.0, boundLength=10.0) dtype = np.dtype([('galid', np.int), ('ra', np.float), ('dec', np.float), ('uTotal', np.float), ('gTotal', np.float), ('rTotal', np.float), ('iTotal', np.float), ('zTotal', np.float), ('uBulge', np.float), ('gBulge', np.float), ('rBulge', np.float), ('iBulge', np.float), ('zBulge', np.float), ('uDisk', np.float), ('gDisk', np.float), ('rDisk', np.float), ('iDisk', np.float), ('zDisk', np.float), ('uAgn', np.float), ('gAgn', np.float), ('rAgn', np.float), ('iAgn', np.float), ('zAgn', np.float), ('bulgeName', str, 200), ('bulgeNorm', np.float), ('bulgeAv', np.float), ('diskName', str, 200), ('diskNorm', np.float), ('diskAv', np.float), ('agnName', str, 200), ('agnNorm', np.float), ('redshift', np.float)]) test_cat = cartoonGalaxies(self.galaxy, obs_metadata=obs_metadata_pointed) with lsst.utils.tests.getTempFilePath('.txt') as catName: test_cat.write_catalog(catName) catData = np.genfromtxt(catName, dtype=dtype, delimiter=', ') self.assertGreater(len(catData), 0) cartoonDir = getPackageDir('sims_photUtils') cartoonDir = os.path.join(cartoonDir, 'tests', 'cartoonSedTestData') sedDir = getPackageDir('sims_sed_library') testBandpasses = {} keys = ['u', 'g', 'r', 'i', 'z'] for kk in keys: testBandpasses[kk] = Bandpass() testBandpasses[kk].readThroughput( os.path.join(cartoonDir, "test_bandpass_%s.dat" % kk)) imsimBand = Bandpass() imsimBand.imsimBandpass() specMap = defaultSpecMap ct = 0 for line in catData: bulgeMagList = [] diskMagList = [] agnMagList = [] if line['bulgeName'] == 'None': for bp in keys: np.testing.assert_equal(line['%sBulge' % bp], np.NaN) bulgeMagList.append(np.NaN) else: ct += 1 dummySed = Sed() dummySed.readSED_flambda( os.path.join(sedDir, specMap[line['bulgeName']])) fnorm = dummySed.calcFluxNorm(line['bulgeNorm'], imsimBand) dummySed.multiplyFluxNorm(fnorm) a_int, b_int = dummySed.setupCCM_ab() dummySed.addDust(a_int, b_int, A_v=line['bulgeAv']) dummySed.redshiftSED(line['redshift'], dimming=True) dummySed.resampleSED(wavelen_match=testBandpasses['u'].wavelen) for bpName in keys: mag = dummySed.calcMag(testBandpasses[bpName]) self.assertAlmostEqual(mag, line['%sBulge' % bpName], 10) bulgeMagList.append(mag) if line['diskName'] == 'None': for bp in keys: np.assert_equal(line['%sDisk' % bp], np.NaN) diskMagList.append(np.NaN) else: ct += 1 dummySed = Sed() dummySed.readSED_flambda( os.path.join(sedDir, specMap[line['diskName']])) fnorm = dummySed.calcFluxNorm(line['diskNorm'], imsimBand) dummySed.multiplyFluxNorm(fnorm) a_int, b_int = dummySed.setupCCM_ab() dummySed.addDust(a_int, b_int, A_v=line['diskAv']) dummySed.redshiftSED(line['redshift'], dimming=True) dummySed.resampleSED(wavelen_match=testBandpasses['u'].wavelen) for bpName in keys: mag = dummySed.calcMag(testBandpasses[bpName]) self.assertAlmostEqual(mag, line['%sDisk' % bpName], 10) diskMagList.append(mag) if line['agnName'] == 'None': for bp in keys: np.testing.assert_true(line['%sAgn' % bp], np.NaN) agnMagList.append(np.NaN) else: ct += 1 dummySed = Sed() dummySed.readSED_flambda( os.path.join(sedDir, specMap[line['agnName']])) fnorm = dummySed.calcFluxNorm(line['agnNorm'], imsimBand) dummySed.multiplyFluxNorm(fnorm) dummySed.redshiftSED(line['redshift'], dimming=True) dummySed.resampleSED(wavelen_match=testBandpasses['u'].wavelen) for bpName in keys: mag = dummySed.calcMag(testBandpasses[bpName]) self.assertAlmostEqual(mag, line['%sAgn' % bpName], 10) agnMagList.append(mag) totalMags = PhotometryGalaxies().sum_magnitudes( bulge=np.array(bulgeMagList), disk=np.array(diskMagList), agn=np.array(agnMagList)) for testMag, bpName in zip(totalMags, keys): if np.isnan(line['%sTotal' % bpName]): np.testing.assert_equal(testMag, np.NaN) else: self.assertAlmostEqual(testMag, line['%sTotal' % bpName], 10) self.assertGreater(ct, 0)
def testMatchToObserved(self): """Test that Galaxy SEDs with extinction or redshift are matched correctly""" np.random.seed(42) galPhot = BandpassDict.loadTotalBandpassesFromFiles() imSimBand = Bandpass() imSimBand.imsimBandpass() testMatching = selectGalaxySED(galDir = self.testSpecDir) testSEDList = testMatching.loadBC03() testSEDNames = [] testRA = [] testDec = [] testRedshifts = [] testMagNormList = [] magNormStep = 1 extCoeffs = [1.8140, 1.4166, 0.9947, 0.7370, 0.5790, 0.4761] testMags = [] testMagsRedshift = [] testMagsExt = [] for testSED in testSEDList: #As a check make sure that it matches when no extinction and no redshift are present getSEDMags = Sed() testSEDNames.append(testSED.name) getSEDMags.setSED(wavelen = testSED.wavelen, flambda = testSED.flambda) testMags.append(galPhot.magListForSed(getSEDMags)) #Check Extinction corrections sedRA = np.random.uniform(10,170) sedDec = np.random.uniform(10,80) testRA.append(sedRA) testDec.append(sedDec) raDec = np.array((sedRA, sedDec)).reshape((2,1)) ebvVal = ebv().calculateEbv(equatorialCoordinates = raDec) extVal = ebvVal*extCoeffs testMagsExt.append(galPhot.magListForSed(getSEDMags) + extVal) #Setup magnitudes for testing matching to redshifted values getRedshiftMags = Sed() testZ = np.round(np.random.uniform(1.1,1.3),3) testRedshifts.append(testZ) testMagNorm = np.round(np.random.uniform(20.0,22.0),magNormStep) testMagNormList.append(testMagNorm) getRedshiftMags.setSED(wavelen = testSED.wavelen, flambda = testSED.flambda) getRedshiftMags.redshiftSED(testZ) fluxNorm = getRedshiftMags.calcFluxNorm(testMagNorm, imSimBand) getRedshiftMags.multiplyFluxNorm(fluxNorm) testMagsRedshift.append(galPhot.magListForSed(getRedshiftMags)) #Will also test in passing of non-default bandpass testNoExtNoRedshift = testMatching.matchToObserved(testSEDList, testMags, np.zeros(20), reddening = False, bandpassDict = galPhot) testMatchingEbvVals = testMatching.matchToObserved(testSEDList, testMagsExt, np.zeros(20), catRA = testRA, catDec = testDec, reddening = True, extCoeffs = extCoeffs, bandpassDict = galPhot) #Substitute in nan values to simulate incomplete data and make sure magnorm works too. testMagsRedshift[0][1] = np.nan testMagsRedshift[0][3] = np.nan testMagsRedshift[0][4] = np.nan testMagsRedshift[1][1] = np.nan testMatchingRedshift = testMatching.matchToObserved(testSEDList, testMagsRedshift, testRedshifts, dzAcc = 3, reddening = False, bandpassDict = galPhot) self.assertEqual(testSEDNames, testNoExtNoRedshift[0]) self.assertEqual(testSEDNames, testMatchingEbvVals[0]) self.assertEqual(None, testMatchingRedshift[0][0]) self.assertEqual(testSEDNames[1:], testMatchingRedshift[0][1:]) self.assertEqual(None, testMatchingRedshift[1][0]) np.testing.assert_almost_equal(testMagNormList[1:], testMatchingRedshift[1][1:], decimal = magNormStep) #Test Match Errors errMag = testMagsRedshift[2] errRedshift = testRedshifts[2] errMags = np.array((errMag, errMag, errMag, errMag)) errRedshifts = np.array((errRedshift, errRedshift, errRedshift, errRedshift)) errMags[1,1] += 1. #Total MSE will be 2/(5 colors) = 0.4 errMags[2, 0:2] = np.nan errMags[2, 3] += 1. #Total MSE will be 2/(3 colors) = 0.667 errMags[3, :] = None errSED = testSEDList[2] testMatchingResultsErrors = testMatching.matchToObserved([errSED], errMags, errRedshifts, reddening = False, bandpassDict = galPhot, dzAcc = 3) np.testing.assert_almost_equal(np.array((0.0, 0.4, 2./3.)), testMatchingResultsErrors[2][0:3], decimal = 2) #Give a little more leeway due to redshifting effects self.assertEqual(None, testMatchingResultsErrors[2][3])
def applyIGM(self, redshift, sedobj): """ Apply IGM extinction to already redshifted sed with redshift between zMin and zMax defined by range of lookup tables @param [in] redshift is the redshift of the incoming SED object @param [in] sedobj is the SED object to which IGM extinction will be applied. This object will be modified as a result of this. """ if self.IGMisInitialized == False: self.initializeIGM() # First make sure redshift is in range of lookup tables. if (redshift < self.zMin) or (redshift > self.zMax): warnings.warn( str( "IGM Lookup tables only applicable for " + str(self.zMin) + " < z < " + str(self.zMax) + ". No action taken" ) ) return # Now read in closest two lookup tables for given redshift lowerSed = Sed() upperSed = Sed() for lower, upper in zip(self.zRange[:-1], self.zRange[1:]): if lower <= redshift <= upper: lowerSed.setSED(self.meanLookups[str(lower)][:, 0], flambda=self.meanLookups[str(lower)][:, 1]) upperSed.setSED(self.meanLookups[str(upper)][:, 0], flambda=self.meanLookups[str(upper)][:, 1]) break # Redshift lookup tables to redshift of source, i.e. if source redshift is 1.78 shift lookup # table for 1.7 and lookup table for 1.8 to up and down to 1.78, respectively zLowerShift = ((1.0 + redshift) / (1.0 + lower)) - 1.0 zUpperShift = ((1.0 + redshift) / (1.0 + upper)) - 1.0 lowerSed.redshiftSED(zLowerShift) upperSed.redshiftSED(zUpperShift) # Resample lower and upper transmission data onto same wavelength grid. minWavelen = 300.0 # All lookup tables are usable above 300nm maxWavelen = np.amin([lowerSed.wavelen[-1], upperSed.wavelen[-1]]) - 0.01 lowerSed.resampleSED(wavelen_min=minWavelen, wavelen_max=maxWavelen, wavelen_step=0.01) upperSed.resampleSED(wavelen_match=lowerSed.wavelen) # Now insert this into a transmission array of 1.0 beyond the limits of current application # So that we can get an sed back that extends to the longest wavelengths of the incoming sed finalWavelen = np.arange(300.0, sedobj.wavelen[-1] + 0.01, 0.01) finalFlambdaExtended = np.ones(len(finalWavelen)) # Weighted Average of Transmission from each lookup table to get final transmission # table at desired redshift dzGrid = self.zDelta # Step in redshift between transmission lookup table files finalSed = Sed() finalFlambda = lowerSed.flambda * (1.0 - ((redshift - lower) / dzGrid)) + upperSed.flambda * ( 1.0 - ((upper - redshift) / dzGrid) ) finalFlambdaExtended[0 : len(finalFlambda)] = finalFlambda finalSed.setSED(wavelen=finalWavelen, flambda=finalFlambdaExtended) # Resample incoming sed to new grid so that we don't get warnings from multiplySED # about matching wavelength grids sedobj.resampleSED(wavelen_match=finalSed.wavelen) # Now multiply transmission curve by input SED to get final result and make it the new flambda # data in the original sed which also is now on a new grid starting at 300 nm test = sedobj.multiplySED(finalSed) sedobj.flambda = test.flambda
def matchToObserved(self, sedList, catMags, catRedshifts, catRA=None, catDec=None, mag_error=None, bandpassDict=None, dzAcc=2, reddening=True, extCoeffs=(4.239, 3.303, 2.285, 1.698, 1.263)): """ This will find the closest match to the magnitudes of a galaxy catalog if those magnitudes are in the observed frame and can correct for reddening from within the milky way as well if needed. In order to make things faster it first calculates colors for all model SEDs at redshifts between the minimum and maximum redshifts of the catalog objects provided with a grid spacing in redshift defined by the parameter dzAcc. Objects without magnitudes in at least two adjacent bandpasses will return as none and print out a message. @param [in] sedList is the set of spectral objects from the models SEDs provided by loadBC03 or other custom loader routine. @param [in] catMags is an array of the magnitudes of catalog objects to be matched with a model SED. It should be organized so that there is one object's magnitudes along each row. @param [in] catRedshifts is an array of the redshifts of each catalog object. @param [in] catRA is an array of the RA positions for each catalog object. @param [in] catDec is an array of the Dec position for each catalog object. @param [in] mag_error are provided error values for magnitudes in objectMags. If none provided then this defaults to 1.0. This should be an array of the same size as catMags. @param [in] bandpassDict is a BandpassDict with which to calculate magnitudes. If left equal to None it will by default load the SDSS [u,g,r,i,z] bandpasses and therefore agree with default extCoeffs. @param [in] dzAcc is the number of decimal places you want to use when building the redshift grid. For example, dzAcc = 2 will create a grid between the minimum and maximum redshifts with colors calculated at every 0.01 change in redshift. @param [in] reddening is a boolean that determines whether to correct catalog magnitudes for dust in the milky way. By default, it is True. If true, this uses calculateEBV from EBV.py to find an EBV value for the object's ra and dec coordinates and then uses the coefficients provided by extCoeffs which should come from Schlafly and Finkbeiner (2011) for the correct filters and in the same order as provided in bandpassDict. If false, this means it will not run the dereddening procedure. @param [in] extCoeffs are the Schlafly and Finkbeiner (2011) (ApJ, 737, 103) coefficients for the given filters from bandpassDict and need to be in the same order as bandpassDict. The default given are the SDSS [u,g,r,i,z] values. @param [out] sedMatches is a list with the name of a model SED that matches most closely to each object in the catalog. @param [out] magNormMatches are the magnitude normalizations for the given magnitudes and matched SED. @param [out] matchErrors contains the Mean Squared Error between the colors of each object and the colors of the matched SED. """ #Set up photometry to calculate model Mags if bandpassDict is None: galPhot = BandpassDict.loadTotalBandpassesFromFiles( ['u', 'g', 'r', 'i', 'z'], bandpassDir=os.path.join( lsst.utils.getPackageDir('throughputs'), 'sdss'), bandpassRoot='sdss_') else: galPhot = bandpassDict #Calculate ebv from ra, dec coordinates if needed if reddening == True: #Check that catRA and catDec are included if catRA is None or catDec is None: raise RuntimeError( "Reddening is True, but catRA and catDec are not included." ) calcEBV = ebv() raDec = np.array((catRA, catDec)) #If only matching one object need to reshape for calculateEbv if len(raDec.shape) == 1: raDec = raDec.reshape((2, 1)) ebvVals = calcEBV.calculateEbv(equatorialCoordinates=raDec) objMags = self.deReddenMags(ebvVals, catMags, extCoeffs) else: objMags = catMags minRedshift = np.round(np.min(catRedshifts), dzAcc) maxRedshift = np.round(np.max(catRedshifts), dzAcc) dz = np.power(10., (-1 * dzAcc)) redshiftRange = np.round( np.arange(minRedshift - dz, maxRedshift + (2 * dz), dz), dzAcc) numRedshifted = 0 sedMatches = [None] * len(catRedshifts) magNormMatches = [None] * len(catRedshifts) matchErrors = [None] * len(catRedshifts) redshiftIndex = np.argsort(catRedshifts) numOn = 0 notMatched = 0 lastRedshift = -100 print 'Starting Matching. Arranged by redshift value.' for redshift in redshiftRange: if numRedshifted % 10 == 0: print '%i out of %i redshifts gone through' % ( numRedshifted, len(redshiftRange)) numRedshifted += 1 colorSet = [] for galSpec in sedList: sedColors = [] fileSED = Sed() fileSED.setSED(wavelen=galSpec.wavelen, flambda=galSpec.flambda) fileSED.redshiftSED(redshift) sedColors = self.calcBasicColors([fileSED], galPhot, makeCopy=True) colorSet.append(sedColors) colorSet = np.transpose(colorSet) for currentIndex in redshiftIndex[numOn:]: matchMags = objMags[currentIndex] if lastRedshift < np.round(catRedshifts[currentIndex], dzAcc) <= redshift: colorRange = np.arange(0, len(galPhot) - 1) matchColors = [] for colorNum in colorRange: matchColors.append(matchMags[colorNum] - matchMags[colorNum + 1]) #This is done to handle objects with incomplete magnitude data filtNums = np.arange(0, len(galPhot)) if np.isnan(np.amin(matchColors)) == True: colorRange = np.where( np.isnan(matchColors) == False)[0] filtNums = np.unique([ colorRange, colorRange + 1 ]) #Pick right filters in calcMagNorm if len(colorRange) == 0: print 'Could not match object #%i. No magnitudes for two adjacent bandpasses.' \ % (currentIndex) notMatched += 1 #Don't need to assign 'None' here in result array, b/c 'None' is default value else: distanceArray = [np.zeros(len(sedList))] for colorNum in colorRange: distanceArray += np.power( (colorSet[colorNum] - matchColors[colorNum]), 2) matchedSEDNum = np.nanargmin(distanceArray) sedMatches[currentIndex] = sedList[matchedSEDNum].name magNormVal = self.calcMagNorm( np.array(matchMags), sedList[matchedSEDNum], galPhot, mag_error=mag_error, redshift=catRedshifts[currentIndex], filtRange=filtNums) magNormMatches[currentIndex] = magNormVal matchErrors[currentIndex] = ( distanceArray[0, matchedSEDNum] / len(colorRange)) numOn += 1 else: break lastRedshift = redshift print 'Done Matching. Matched %i of %i catalog objects to SEDs' % ( len(catMags) - notMatched, len(catMags)) if notMatched > 0: print '%i objects did not get matched.' % (notMatched) return sedMatches, magNormMatches, matchErrors