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 testSignalToNoise(self): """ Test that calcSNR_m5 and calcSNR_sed give similar results """ defaults = LSSTdefaults() photParams = PhotometricParameters() totalDict, hardwareDict = BandpassDict.loadBandpassesFromFiles() skySED = Sed() skySED.readSED_flambda( os.path.join(lsst.utils.getPackageDir('throughputs'), 'baseline', 'darksky.dat')) m5 = [] for filt in totalDict: m5.append( calcM5(skySED, totalDict[filt], hardwareDict[filt], photParams, seeing=defaults.seeing(filt))) sedDir = lsst.utils.getPackageDir('sims_sed_library') sedDir = os.path.join(sedDir, 'starSED', 'kurucz') fileNameList = os.listdir(sedDir) numpy.random.seed(42) offset = numpy.random.random_sample(len(fileNameList)) * 2.0 for ix, name in enumerate(fileNameList): if ix > 100: break spectrum = Sed() spectrum.readSED_flambda(os.path.join(sedDir, name)) ff = spectrum.calcFluxNorm(m5[2] - offset[ix], totalDict.values()[2]) spectrum.multiplyFluxNorm(ff) magList = [] controlList = [] magList = [] for filt in totalDict: controlList.append( calcSNR_sed(spectrum, totalDict[filt], skySED, hardwareDict[filt], photParams, defaults.seeing(filt))) magList.append(spectrum.calcMag(totalDict[filt])) testList, gammaList = calcSNR_m5(numpy.array(magList), numpy.array(totalDict.values()), numpy.array(m5), photParams) for tt, cc in zip(controlList, testList): msg = '%e != %e ' % (tt, cc) self.assertTrue(numpy.abs(tt / cc - 1.0) < 0.001, msg=msg)
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 testSignalToNoise(self): """ Test that calcSNR_m5 and calcSNR_sed give similar results """ defaults = LSSTdefaults() photParams = PhotometricParameters() totalDict, hardwareDict = BandpassDict.loadBandpassesFromFiles() skySED = Sed() skySED.readSED_flambda(os.path.join(lsst.utils.getPackageDir("throughputs"), "baseline", "darksky.dat")) m5 = [] for filt in totalDict: m5.append(calcM5(skySED, totalDict[filt], hardwareDict[filt], photParams, seeing=defaults.seeing(filt))) sedDir = lsst.utils.getPackageDir("sims_sed_library") sedDir = os.path.join(sedDir, "starSED", "kurucz") fileNameList = os.listdir(sedDir) numpy.random.seed(42) offset = numpy.random.random_sample(len(fileNameList)) * 2.0 for ix, name in enumerate(fileNameList): if ix > 100: break spectrum = Sed() spectrum.readSED_flambda(os.path.join(sedDir, name)) ff = spectrum.calcFluxNorm(m5[2] - offset[ix], totalDict.values()[2]) spectrum.multiplyFluxNorm(ff) magList = [] controlList = [] magList = [] for filt in totalDict: controlList.append( calcSNR_sed( spectrum, totalDict[filt], skySED, hardwareDict[filt], photParams, defaults.seeing(filt) ) ) magList.append(spectrum.calcMag(totalDict[filt])) testList, gammaList = calcSNR_m5( numpy.array(magList), numpy.array(totalDict.values()), numpy.array(m5), photParams ) for tt, cc in zip(controlList, testList): msg = "%e != %e " % (tt, cc) self.assertTrue(numpy.abs(tt / cc - 1.0) < 0.001, msg=msg)
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)