def test_rectifiedSED(self): """ Check for an extreme case that the SN seds are being rectified. This is done by setting up an extreme case where there will be negative seds, and checking that this is indeed the case, and checking that they are not negative if rectified. """ snobj = SNObject(ra=30., dec=-60., source='salt2') snobj.set(z=0.96, t0=self.mjdobs, x1=-3., x0=1.8e-6) snobj.rectifySED = False times = np.arange(self.mjdobs - 50., self.mjdobs + 150., 1.) badTimes = [] for time in times: sed = snobj.SNObjectSED(time=time, bandpass=self.lsstBandPass['r']) if any(sed.flambda < 0.): badTimes.append(time) # Check that there are negative SEDs assert(len(badTimes) > 0) snobj.rectifySED = True for time in badTimes: sed = snobj.SNObjectSED(time=time, bandpass=self.lsstBandPass['r']) self.assertGreaterEqual(sed.calcADU(bandpass=self.lsstBandPass['r'], photParams=self.rectify_photParams), 0.) self.assertFalse(any(sed.flambda < 0.))
def load_SNsed(self): """ returns a list of SN seds in `lsst.sims.photUtils.Sed` observed within the spatio-temporal range specified by obs_metadata """ c, x1, x0, t0, _z, ra, dec = self.column_by_name('c'),\ self.column_by_name('x1'),\ self.column_by_name('x0'),\ self.column_by_name('t0'),\ self.column_by_name('redshift'),\ self.column_by_name('raJ2000'),\ self.column_by_name('decJ2000') SNobject = SNObject() raDeg = np.degrees(ra) decDeg = np.degrees(dec) sedlist = [] for i in range(self.numobjs): SNobject.set(z=_z[i], c=c[i], x1=x1[i], t0=t0[i], x0=x0[i]) SNobject.setCoords(ra=raDeg[i], dec=decDeg[i]) SNobject.mwEBVfromMaps() sed = SNobject.SNObjectSED(time=self.mjdobs, bandpass=self.lsstBandpassDict, applyExitinction=True) sedlist.append(sed) return sedlist
def get_snbrightness(self): """ getters for brightness related parameters of sn """ if self._sn_object_cache is None or len( self._sn_object_cache) > 1000000: self._sn_object_cache = {} c, x1, x0, t0, _z, ra, dec = self.column_by_name('c'),\ self.column_by_name('x1'),\ self.column_by_name('x0'),\ self.column_by_name('t0'),\ self.column_by_name('redshift'),\ self.column_by_name('raJ2000'),\ self.column_by_name('decJ2000') raDeg = np.degrees(ra) decDeg = np.degrees(dec) ebv = self.column_by_name('EBV') id_list = self.column_by_name('snid') bandname = self.obs_metadata.bandpass if isinstance(bandname, list): raise ValueError('bandname expected to be string, but is list\n') bandpass = self.lsstBandpassDict[bandname] # Initialize return array so that it contains the values you would get # if you passed through a t0=self.badvalues supernova vals = np.array([[0.0] * len(t0), [np.inf] * len(t0), [np.nan] * len(t0), [np.inf] * len(t0), [0.0] * len(t0)]).transpose() for i in np.where( np.logical_and( np.isfinite(t0), np.abs(self.mjdobs - t0) < self.maxTimeSNVisible))[0]: if id_list[i] in self._sn_object_cache: SNobject = self._sn_object_cache[id_list[i]] else: SNobject = SNObject() SNobject.set(z=_z[i], c=c[i], x1=x1[i], t0=t0[i], x0=x0[i]) SNobject.setCoords(ra=raDeg[i], dec=decDeg[i]) SNobject.set_MWebv(ebv[i]) self._sn_object_cache[id_list[i]] = SNobject if self.mjdobs <= SNobject.maxtime( ) and self.mjdobs >= SNobject.mintime(): # Calculate fluxes fluxinMaggies = SNobject.catsimBandFlux( time=self.mjdobs, bandpassobject=bandpass) mag = SNobject.catsimBandMag(time=self.mjdobs, fluxinMaggies=fluxinMaggies, bandpassobject=bandpass) vals[i, 0] = fluxinMaggies vals[i, 1] = mag flux_err = SNobject.catsimBandFluxError( time=self.mjdobs, bandpassobject=bandpass, m5=self.obs_metadata.m5[bandname], photParams=self.photometricparameters, fluxinMaggies=fluxinMaggies, magnitude=mag) mag_err = SNobject.catsimBandMagError( time=self.mjdobs, bandpassobject=bandpass, m5=self.obs_metadata.m5[bandname], photParams=self.photometricparameters, magnitude=mag) sed = SNobject.SNObjectSED(time=self.mjdobs, bandpass=self.lsstBandpassDict, applyExtinction=True) adu = sed.calcADU(bandpass, photParams=self.photometricparameters) vals[i, 2] = flux_err vals[i, 3] = mag_err vals[i, 4] = adu return (vals[:, 0], vals[:, 1], vals[:, 2], vals[:, 3], vals[:, 4])
class SNObject_tests(unittest.TestCase): @classmethod def tearDownClass(cls): sims_clean_up() def setUp(self): """ Setup tests SN_blank: A SNObject with no MW extinction """ mydir = get_config_dir() print('===============================') print('===============================') print (mydir) print('===============================') print('===============================') # A range of wavelengths in Ang self.wave = np.arange(3000., 12000., 50.) # Equivalent wavelenths in nm self.wavenm = self.wave / 10. # Time to be used as Peak self.mjdobs = 571190 # Check that we can set up a SED # with no extinction self.SN_blank = SNObject() self.SN_blank.setCoords(ra=30., dec=-60.) self.SN_blank.set(z=0.96, t0=571181, x1=2.66, c=0.353, x0=1.796e-6) self.SN_blank.set_MWebv(0.) self.SN_extincted = SNObject(ra=30., dec=-60.) self.SN_extincted.set(z=0.96, t0=571181, x1=2.66, c=0.353, x0=1.796112e-06) self.SNCosmoModel = self.SN_extincted.equivalentSNCosmoModel() self.rectify_photParams = PhotometricParameters() self.lsstBandPass = BandpassDict.loadTotalBandpassesFromFiles() self.SNCosmoBP = sncosmo.Bandpass(wave=self.lsstBandPass['r'].wavelen, trans=self.lsstBandPass['r'].sb, wave_unit=astropy.units.Unit('nm'), name='lsst_r') def tearDown(self): del self.SNCosmoBP del self.SN_blank del self.SN_extincted def test_SNstatenotEmpty(self): """ Check that the state of SNObject, stored in self.SNstate has valid entries for all keys and does not contain keys with None type Values. """ myDict = self.SN_extincted.SNstate for key in myDict: assert myDict[key] is not None def test_attributeDefaults(self): """ Check the defaults and the setter properties for rectifySED and modelOutSideRange """ snobj = SNObject(ra=30., dec=-60., source='salt2') self.assertEqual(snobj.rectifySED, True) self.assertEqual(snobj.modelOutSideTemporalRange, 'zero') snobj.rectifySED = False self.assertFalse(snobj.rectifySED, False) self.assertEqual(snobj.modelOutSideTemporalRange, 'zero') def test_raisingerror_forunimplementedmodelOutSideRange(self): """ check that correct error is raised if the user tries to assign an un-implemented model value to `sims.catUtils.supernovae.SNObject.modelOutSideTemporalRange` """ snobj = SNObject(ra=30., dec=-60., source='salt2') assert snobj.modelOutSideTemporalRange == 'zero' with self.assertRaises(ValueError) as context: snobj.modelOutSideTemporalRange = 'False' self.assertEqual('Model not implemented, defaulting to zero method\n', context.exception.args[0]) def test_rectifiedSED(self): """ Check for an extreme case that the SN seds are being rectified. This is done by setting up an extreme case where there will be negative seds, and checking that this is indeed the case, and checking that they are not negative if rectified. """ snobj = SNObject(ra=30., dec=-60., source='salt2') snobj.set(z=0.96, t0=self.mjdobs, x1=-3., x0=1.8e-6) snobj.rectifySED = False times = np.arange(self.mjdobs - 50., self.mjdobs + 150., 1.) badTimes = [] for time in times: sed = snobj.SNObjectSED(time=time, bandpass=self.lsstBandPass['r']) if any(sed.flambda < 0.): badTimes.append(time) # Check that there are negative SEDs assert(len(badTimes) > 0) snobj.rectifySED = True for time in badTimes: sed = snobj.SNObjectSED(time=time, bandpass=self.lsstBandPass['r']) self.assertGreaterEqual(sed.calcADU(bandpass=self.lsstBandPass['r'], photParams=self.rectify_photParams), 0.) self.assertFalse(any(sed.flambda < 0.)) def test_ComparebandFluxes2photUtils(self): """ The SNObject.catsimBandFlux computation uses the sims.photUtils.sed band flux computation under the hood. This test makes sure that these definitions are in sync """ snobject_r = self.SN_extincted.catsimBandFlux( bandpassobject=self.lsstBandPass['r'], time=self.mjdobs) # `sims.photUtils.Sed` sed = self.SN_extincted.SNObjectSED(time=self.mjdobs, bandpass=self.lsstBandPass['r']) sedflux = sed.calcFlux(bandpass=self.lsstBandPass['r']) np.testing.assert_allclose(snobject_r, sedflux / 3631.0) def test_CompareBandFluxes2SNCosmo(self): """ Compare the r band flux at a particular time computed in SNObject and SNCosmo for MW-extincted SEDs. While the underlying sed is obtained from SNCosmo the integration with the bandpass is an independent calculation in SNCosmo and catsim """ times = self.mjdobs catsim_r = self.SN_extincted.catsimBandFlux( bandpassobject=self.lsstBandPass['r'], time=times) sncosmo_r = self.SNCosmoModel.bandflux(band=self.SNCosmoBP, time=times, zpsys='ab', zp=0.) np.testing.assert_allclose(sncosmo_r, catsim_r) def test_CompareBandMags2SNCosmo(self): """ Compare the r band flux at a particular time computed in SNObject and SNCosmo for MW-extincted SEDs. Should work whenever the flux comparison above works. """ times = self.mjdobs catsim_r = self.SN_extincted.catsimBandMag( bandpassobject=self.lsstBandPass['r'], time=times) sncosmo_r = self.SNCosmoModel.bandmag(band=self.SNCosmoBP, time=times, magsys='ab') np.testing.assert_allclose(sncosmo_r, catsim_r) def test_CompareExtinctedSED2SNCosmo(self): """ Compare the extincted SEDS in SNCosmo and SNObject. Slightly more non-trivial than comparing unextincted SEDS, as the extinction in SNObject uses different code from SNCosmo. However, this is still using the same values of MWEBV, rather than reading it off a map. """ SNObjectSED = self.SN_extincted.SNObjectSED(time=self.mjdobs, wavelen=self.wavenm) SNCosmoSED = self.SNCosmoModel.flux(time=self.mjdobs, wave=self.wave) \ * 10. np.testing.assert_allclose(SNObjectSED.flambda, SNCosmoSED, rtol=1.0e-7) def test_CompareUnextinctedSED2SNCosmo(self): """ Compares the unextincted flux Densities in SNCosmo and SNObject. This is mereley a sanity check as SNObject uses SNCosmo under the hood. """ SNCosmoFluxDensity = self.SN_blank.flux(wave=self.wave, time=self.mjdobs) * 10. unextincted_sed = self.SN_blank.SNObjectSED(time=self.mjdobs, wavelen=self.wavenm) SNObjectFluxDensity = unextincted_sed.flambda np.testing.assert_allclose(SNCosmoFluxDensity, SNObjectFluxDensity, rtol=1.0e-7) def test_redshift(self): """ test that the redshift method works as expected by checking that if we redshift a SN from its original redshift orig_z to new_z where new_z is smaller (larger) than orig_z: - 1. x0 increases (decreases) - 2. source peak absolute magnitude in BesselB band stays the same """ from astropy.cosmology import FlatLambdaCDM cosmo = FlatLambdaCDM(H0=70., Om0=0.3) orig_z = self.SN_extincted.get('z') orig_x0 = self.SN_extincted.get('x0') peakabsMag = self.SN_extincted.source_peakabsmag('BessellB', 'AB', cosmo=cosmo) lowz = orig_z * 0.5 highz = orig_z * 2.0 # Test Case for lower redshift self.SN_extincted.redshift(z=lowz, cosmo=cosmo) low_x0 = self.SN_extincted.get('x0') lowPeakAbsMag = self.SN_extincted.source_peakabsmag('BessellB', 'AB', cosmo=cosmo) # Test 1. self.assertGreater(low_x0, orig_x0) # Test 2. self.assertAlmostEqual(peakabsMag, lowPeakAbsMag, places=14) # Test Case for higher redshift self.SN_extincted.redshift(z=highz, cosmo=cosmo) high_x0 = self.SN_extincted.get('x0') HiPeakAbsMag = self.SN_extincted.source_peakabsmag('BessellB', 'AB', cosmo=cosmo) # Test 1. self.assertLess(high_x0, orig_x0) # Test 2. self.assertAlmostEqual(peakabsMag, HiPeakAbsMag, places=14) def test_bandFluxErrorWorks(self): """ test that bandflux errors work even if the flux is negative """ times = self.mjdobs e = self.SN_extincted.catsimBandFluxError(times, self.lsstBandPass['r'], m5=24.5, fluxinMaggies=-1.0) assert isinstance(e, np.float) print(e) assert not(np.isinf(e) or np.isnan(e))
class SNSynthPhotFactory: """ Factory class to return the SyntheticPhotometry objects for a SN Ia as a function of time. This uses the 'salt2-extended' model in sncosmo as implemented in sims_catUtils. """ lsst_bp_dict = BandpassDict.loadTotalBandpassesFromFiles() def __init__(self, z=0, t0=0, x0=1, x1=0, c=0, snra=0, sndec=0): """ Parameters ---------- z: float [0] Redshift of the SNIa object to pass to sncosmo. t0: float [0] Time in mjd of phase=0, corresponding to the B-band maximum. x0: float [1] Normalization factor for the lightcurves. x1: float [0] Empirical parameter controlling the stretch in time of the light curves c: float [0] Empirical parameter controlling the colors. snra: float [0] RA in degrees of the SNIa. sndec: float [0] Dec in degrees of the SNIa. """ self.sn_obj = SNObject(snra, sndec) self.sn_obj.set(z=z, t0=t0, x0=x0, x1=x1, c=c, hostebv=0, hostr_v=3.1, mwebv=0, mwr_v=3.1) self.bp_dict = self.lsst_bp_dict def set_bp_dict(self, bp_dict): """ Set the bandpass dictionary. Parameters ---------- bp_dict: lsst.sims.photUtils.BandpassDict Dictionary containing the bandpasses. If None, the standard LSST total bandpasses will be used. """ self.bp_dict = bp_dict def create(self, mjd): """ Return a SyntheticPhotometry object for the specified time in MJD. Parameters ---------- mjd: float Observation time in MJD. Returns ------- desc.sims_truthcatalog.SyntheticPhotometry """ # Create the Sed object. Milky Way extinction will be # below, so set applyExtinction=False. sed = self.sn_obj.SNObjectSED(mjd, bandpass=self.bp_dict, applyExtinction=False) # The redshift was applied to the model SED computed by # sncosmo in the __init__ function, so set redshift=0 here. synth_phot = SyntheticPhotometry.create_from_sed(sed, redshift=0) synth_phot.add_MW_dust(*np.degrees(self.sn_obj.skycoord).flatten()) return synth_phot def __getattr__(self, attr): # Pass any attribute access requests to the SNObject instance. return getattr(self.sn_obj, attr)