Skip to content

Commit

Permalink
update unit tests following some changes in sn_tools
Browse files Browse the repository at this point in the history
  • Loading branch information
Philippe Gris committed Jun 12, 2020
1 parent 0b77449 commit 4f7e6e9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 23 deletions.
4 changes: 2 additions & 2 deletions sn_tools/sn_telescope.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ def mag_to_flux(self, mag, band, zp=None):
"""
if zp is None:
zp = self.zero_points(band)
return np.power(10., -0.4 * (mag-zp))
return np.power(10., -0.4 * (mag-zp.item()))

def zero_points(self, band):
"""
Expand All @@ -395,7 +395,7 @@ def zero_points(self, band):
array of zp
"""
return np.asarray([self.zp[b] for b in band])
return np.asarray([self.zp(b) for b in band])

def mag_to_flux_e_sec(self, mag, band, exptime, nexp):
"""
Expand Down
3 changes: 2 additions & 1 deletion sn_tools/sn_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -814,8 +814,9 @@ def prod_mag_to_flux(self):
for band in 'grizy':
mag_to_flux = np.array(m5, dtype=[('m5', 'f8')])
exptime = [30.] * len(m5)
nexp = range(1, len(m5))
b = [band] * len(m5)
f5 = self.telescope.mag_to_flux_e_sec(m5, b, exptime)
f5 = self.telescope.mag_to_flux_e_sec(m5, b, exptime, nexp)
mag_to_flux = rf.append_fields(mag_to_flux, ['band', 'flux_e'], [
b, f5[:, [1]]], dtypes=['U256', 'f8'])
if mag_to_flux_tot is None:
Expand Down
69 changes: 49 additions & 20 deletions tests/testSNtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def Observations(daymin=59000, cadence=3., season_length=140., band='r'):
return data


def getReference(x1, color):
def loadReference(x1, color):
# first step: get reference files
tel_par = {}
tel_par['name'] = 'LSST' # name of the telescope (internal)
Expand Down Expand Up @@ -178,7 +178,7 @@ def snSimuParam(x1, color):
def simuLCfast(x1, color, bands='r'):

# get reference LC
lc_ref = getReference(x1, color)
lc_ref = loadReference(x1, color)

# get telescope model
telescope = Telescope()
Expand Down Expand Up @@ -263,16 +263,21 @@ def testTelescope(self):
Tb = tel.Tb(bands)
zp = tel.zp(bands)
m5 = tel.m5(bands)
single_exposure_time = 30.
nexp = 1.
gamma = {}
print(m5, zp)
for key, val in m5.items():
gamma[key] = tel.gamma(val, key, 15.)
gamma[key] = tel.gamma(val, key, single_exposure_time, nexp)

mag_to_flux_e_sec = {}
mag_to_flux = {}

for key, val in m5.items():
mag_to_flux_e_sec[key] = tel.mag_to_flux_e_sec(val, key, 15.)
mag_to_flux[key] = tel.mag_to_flux(val, key, 15.)
mag_to_flux_e_sec[key] = tel.mag_to_flux_e_sec(
val, key, single_exposure_time, nexp)
mag_to_flux[key] = tel.mag_to_flux(
val, key)

# This is to print the reference data
"""
Expand All @@ -284,6 +289,7 @@ def testTelescope(self):
print('mag_to_flux_e_sec_ref = ', mag_to_flux_e_sec)
print('mag_to_flux_ref = ', mag_to_flux)
"""

sigmab_ref = {'u': 0.0508700300828208, 'g': 0.15101783445197073, 'r': 0.11436909314149583,
'i': 0.08338616181313455, 'z': 0.0556165878457169, 'y': 0.029824040498790286}
Tb_ref = {'u': 0.030591304228780723, 'g': 0.12434745927780146, 'r': 0.10309893990743581,
Expand All @@ -292,12 +298,12 @@ def testTelescope(self):
'i': 27.847688717586387, 'z': 27.435125355183057, 'y': 26.63811061635532}
m5_ref = {'u': 23.703378601363863, 'g': 24.808696323169624, 'r': 24.364793723029457,
'i': 23.93699490521644, 'z': 23.357218514891976, 'y': 22.445603830018765}
gamma_ref = {'u': 0.036281856881248506, 'g': 0.03746828818474719, 'r': 0.03797121537662722,
'i': 0.03818223331426463, 'z': 0.03844169333327794, 'y': 0.03859779035436962}
mag_to_flux_e_sec_ref = {'u': (116.93541502018535, 17.930096969761752), 'g': (171.734636650276, 26.33264428637565), 'r': (214.30693218328977, 32.86039626810443), 'i': (
239.18504619296752, 36.675040416255015), 'z': (279.009657072333, 42.78148075109106), 'y': (310.06961765705864, 47.544008040748984)}
mag_to_flux_ref = {'u': 0.00033010230695535505, 'g': 0.00011926732280013303, 'r': 0.00017950746372769034,
'i': 0.0002661963142302241, 'z': 0.00045405931961482683, 'y': 0.0010513769899871202}
gamma_ref = {'u': (0.03814092844062426, 17.930096969761752), 'g': (0.03873414409237359, 26.332644286375654), 'r': (0.038985607688313606, 32.86039626810443), 'i': (
0.039091116657132316, 36.675040416255015), 'z': (0.03922084666663897, 42.78148075109106), 'y': (0.03929889517718481, 47.544008040748984)}
mag_to_flux_e_sec_ref = {'u': (233.8708300403707, 17.930096969761752), 'g': (343.469273300552, 26.332644286375654), 'r': (428.61386436657955, 32.86039626810443), 'i': (
478.37009238593504, 36.675040416255015), 'z': (558.019314144666, 42.78148075109106), 'y': (620.1392353141173, 47.544008040748984)}
mag_to_flux_ref = {'u': 17.92625468, 'g': 26.32700139,
'r': 32.85335452, 'i': 36.66718122, 'z': 42.77231299, 'y': 47.5338197}

for band in bands:
for val in [(sigmab, sigmab_ref), (Tb, Tb_ref), (zp, zp_ref), (m5, m5_ref), (gamma, gamma_ref), (mag_to_flux, mag_to_flux_ref), (mag_to_flux_e_sec, mag_to_flux_e_sec_ref)]:
Expand Down Expand Up @@ -768,7 +774,7 @@ def testMbCov(self):
def testGetReference(self):

x1, color = -2.0, 0.2
lc_ref = getReference(x1, color)
lc_ref = loadReference(x1, color)
bands_ref = ['g', 'r', 'i', 'z', 'y']
# check whether dict keys are ok

Expand Down Expand Up @@ -802,23 +808,46 @@ def testGamma(self):
if os.path.isfile(outName):
os.system('rm {}'.format(outName))

mag_range = np.arange(20., 25., 1.)
exptimes = np.array([15., 30.])
telescope = Telescope(airmass=1.2)

mag_range = np.arange(20., 25., 1)
nexps = range(1, 10, 1)
single_exposure_time = [30.]

Gamma(bands, telescope, outName,
mag_range=mag_range,
exptimes=exptimes)
single_exposure_time=single_exposure_time, nexps=nexps)
# check production
fFile = h5py.File(outName, 'r')
keys = list(fFile.keys())

data = Table()
for key in keys:
data = vstack([data, Table.read(fFile, path=key)])

gamma_ref = [0.039963862137247765, 0.03998193106862388, 0.03990922579288891, 0.039954612896444454, 0.0397719855008266,
0.039885992750413296, 0.03942725347333887, 0.039713626736669436, 0.038561325770985665, 0.03928066288549283]

assert(np.isclose(data['gamma'], np.array(gamma_ref)).all())
"""
for col in data.columns:
print('refDict[\'{}\']='.format(col), data[col].tolist())
"""
refDict = {}

refDict['band'] = ['r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r',
'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r']
refDict['mag'] = [20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 21.0, 21.0, 21.0, 21.0, 21.0, 21.0, 21.0, 21.0, 21.0, 22.0, 22.0, 22.0,
22.0, 22.0, 22.0, 22.0, 22.0, 22.0, 23.0, 23.0, 23.0, 23.0, 23.0, 23.0, 23.0, 23.0, 23.0, 24.0, 24.0, 24.0, 24.0, 24.0, 24.0, 24.0, 24.0, 24.0]
refDict['single_exptime'] = [30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0,
30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0]
refDict['nexp'] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2,
3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9]
refDict['gamma'] = [0.03998193106862388, 0.039990965534311945, 0.039993977022874626, 0.03999548276715597, 0.039996386213724776, 0.03999698851143731, 0.03999741872408913, 0.039997741383577985, 0.03999799234095821, 0.039954612896444454, 0.03997730644822223, 0.039984870965481485, 0.03998865322411111, 0.03999092257928889, 0.03999243548274074, 0.0399935161280635, 0.039994326612055556, 0.03999495698849383, 0.039885992750413296, 0.03994299637520665, 0.0399619975834711, 0.03997149818760332,
0.03997719855008266, 0.03998099879173555, 0.03998371325005904, 0.03998574909380166, 0.0399873325278237, 0.039713626736669436, 0.03985681336833472, 0.03990454224555648, 0.03992840668416736, 0.03994272534733389, 0.03995227112277824, 0.03995908953380992, 0.03996420334208368, 0.03996818074851883, 0.03928066288549283, 0.03964033144274642, 0.03976022096183095, 0.03982016572137321, 0.039856132577098566, 0.03988011048091547, 0.03989723755507041, 0.039910082860686605, 0.03992007365394365]
refDict['flux_e_sec'] = [1844.7872007190508, 1844.7872007190508, 1844.7872007190508, 1844.7872007190508, 1844.787200719051, 1844.7872007190508, 1844.787200719051, 1844.7872007190508, 1844.787200719051, 734.423012751567, 734.423012751567, 734.4230127515671, 734.423012751567, 734.423012751567, 734.4230127515671, 734.4230127515671, 734.423012751567, 734.4230127515672, 292.3790675959012, 292.3790675959012, 292.37906759590123, 292.3790675959012,
292.3790675959012, 292.37906759590123, 292.3790675959012, 292.3790675959012, 292.3790675959012, 116.39820332967382, 116.39820332967382, 116.39820332967382, 116.39820332967382, 116.39820332967382, 116.39820332967382, 116.39820332967383, 116.39820332967382, 116.39820332967383, 46.338959385087065, 46.338959385087065, 46.33895938508709, 46.338959385087065, 46.33895938508708, 46.33895938508709, 46.33895938508708, 46.338959385087065, 46.33895938508709]

for key, val in refDict.items():
if key not in ['band']:
assert(np.isclose(data[key].tolist(), val).all())
else:
assert(data[key].tolist() == val)


class TestSNcalcFast(unittest.TestCase):
Expand Down

0 comments on commit 4f7e6e9

Please sign in to comment.