def testReddeningException(self): """Test that if reddening=True in matchToObserved CatRA & CatDec are defined or exception is raised""" testException = selectGalaxySED(galDir = self.testSpecDir) testSEDList = testException.loadBC03() magnitudes = [[1.0, 2.0, 3.0, 4.0, 5.0], [1.0, 2.0, 3.0, 4.0, 5.0]] redshifts = [1.0, 1.0] self.assertRaises(RuntimeError, testException.matchToObserved, testSEDList, magnitudes, redshifts, reddening = True)
def testMatchToRestFrame(self): """Test that Galaxies with no effects added into catalog mags are matched correctly.""" rng = np.random.RandomState(42) galPhot = BandpassDict.loadTotalBandpassesFromFiles() imSimBand = Bandpass() imSimBand.imsimBandpass() testMatching = selectGalaxySED(galDir = self.testSpecDir) testSEDList = testMatching.loadBC03() testSEDNames = [] testMags = [] testMagNormList = [] magNormStep = 1 for testSED in testSEDList: getSEDMags = Sed() testSEDNames.append(testSED.name) getSEDMags.setSED(wavelen = testSED.wavelen, flambda = testSED.flambda) testMagNorm = np.round(rng.uniform(20.0, 22.0), magNormStep) testMagNormList.append(testMagNorm) fluxNorm = getSEDMags.calcFluxNorm(testMagNorm, imSimBand) getSEDMags.multiplyFluxNorm(fluxNorm) testMags.append(galPhot.magListForSed(getSEDMags)) # Also testing to make sure passing in non-default bandpasses works # Substitute in nan values to simulate incomplete data. testMags[0][1] = np.nan testMags[0][2] = np.nan testMags[0][4] = np.nan testMags[1][1] = np.nan testMatchingResults = testMatching.matchToRestFrame(testSEDList, testMags, bandpassDict = galPhot) self.assertEqual(None, testMatchingResults[0][0]) self.assertEqual(testSEDNames[1:], testMatchingResults[0][1:]) self.assertEqual(None, testMatchingResults[1][0]) np.testing.assert_almost_equal(testMagNormList[1:], testMatchingResults[1][1:], decimal = magNormStep) # Test Match Errors errMags = np.array((testMags[2], testMags[2], testMags[2], testMags[2])) 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.matchToRestFrame([errSED], errMags, bandpassDict = galPhot) np.testing.assert_almost_equal(np.array((0.0, 0.4, 2./3.)), testMatchingResultsErrors[2][0:3], decimal = 3) self.assertEqual(None, testMatchingResultsErrors[2][3])
def testMatchToRestFrame(self): """Test that Galaxies with no effects added into catalog mags are matched correctly.""" np.random.seed(42) galPhot = BandpassDict.loadTotalBandpassesFromFiles() imSimBand = Bandpass() imSimBand.imsimBandpass() testMatching = selectGalaxySED(galDir = self.testSpecDir) testSEDList = testMatching.loadBC03() testSEDNames = [] testMags = [] testMagNormList = [] magNormStep = 1 for testSED in testSEDList: getSEDMags = Sed() testSEDNames.append(testSED.name) getSEDMags.setSED(wavelen = testSED.wavelen, flambda = testSED.flambda) testMagNorm = np.round(np.random.uniform(20.0,22.0),magNormStep) testMagNormList.append(testMagNorm) fluxNorm = getSEDMags.calcFluxNorm(testMagNorm, imSimBand) getSEDMags.multiplyFluxNorm(fluxNorm) testMags.append(galPhot.magListForSed(getSEDMags)) #Also testing to make sure passing in non-default bandpasses works #Substitute in nan values to simulate incomplete data. testMags[0][1] = np.nan testMags[0][2] = np.nan testMags[0][4] = np.nan testMags[1][1] = np.nan testMatchingResults = testMatching.matchToRestFrame(testSEDList, testMags, bandpassDict = galPhot) self.assertEqual(None, testMatchingResults[0][0]) self.assertEqual(testSEDNames[1:], testMatchingResults[0][1:]) self.assertEqual(None, testMatchingResults[1][0]) np.testing.assert_almost_equal(testMagNormList[1:], testMatchingResults[1][1:], decimal = magNormStep) #Test Match Errors errMags = np.array((testMags[2], testMags[2], testMags[2], testMags[2])) 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.matchToRestFrame([errSED], errMags, bandpassDict = galPhot) np.testing.assert_almost_equal(np.array((0.0, 0.4, 2./3.)), testMatchingResultsErrors[2][0:3], decimal = 3) self.assertEqual(None, testMatchingResultsErrors[2][3])
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])
from lsst.sims.photUtils.selectGalaxySED import selectGalaxySED import os import numpy as np import lsst.utils from lsst.sims.photUtils.BandpassDict import BandpassDict #If you are using your own folder this should be selectGalaxySED(galDir = yourFolder) matchSED = selectGalaxySED() """Can restrict the loading below to a subset of the SEDs in a given folder if you pass a list as matchSED.loadBC03(subset = subsetList)""" sedList = matchSED.loadBC03() print sedList[1].wavelen print sedList[1].flambda print sedList[1].name #Import sample galaxy catalog organized as [RA, Dec, z, mag_g, mag_r, mag_i, mag_z, mag_y] ra, dec, z, mag_g, mag_r, mag_i, mag_z, mag_y = np.genfromtxt('sampleGalCat.dat', unpack=True) """Organize magnitudes into single array with each row corresponding to the set of magnitudes of one object""" catMagsObs = np.transpose([mag_g, mag_r, mag_i, mag_z, mag_y]) #Define bandpasses. This example shows loading in LSST grizy bandpasses. galBandpassDict = BandpassDict.loadTotalBandpassesFromFiles(['g', 'r', 'i', 'z', 'y']) #We'll need to set makeCopy to True since we are going to reuse this sedList below #matchObservedNames, matchObsMagNorm, matchObsErrors = matchSED.matchToObserved(sedList, catMagsObs, z, catRA=ra, catDec=dec, # bandpassDict = galBandpassDict, dzAcc=2,extCoeffs = (3.0999999,3.0999999,3.0999999,3.0999999,3.0999999,3.0999999)) #(4.145, 3.237, 2.273, 1.684, 1.323, 1.088)) matchObservedNames, matchObsMagNorm, matchObsErrors = matchSED.matchToObserved(sedList, catMagsObs, z, catRA=ra, catDec=dec, dzAcc = 2,
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])