Coverage for tests/testSNMetrics.py : 19%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from builtins import zip
2import matplotlib
3matplotlib.use("Agg")
4import numpy as np
5import unittest
6#import lsst.sims.maf.metrics as metrics
7import lsst.utils.tests
8from lsst.sims.maf.utils.snUtils import Lims, ReferenceData
9from lsst.sims.maf.metrics.snCadenceMetric import SNCadenceMetric
10from lsst.sims.maf.metrics.snSNRMetric import SNSNRMetric
11import os
12import warnings
14m5_ref = dict(
15 zip('ugrizy', [23.60, 24.83, 24.38, 23.92, 23.35, 22.44]))
18class TestSNmetrics(unittest.TestCase):
20 def testSNCadenceMetric(self):
21 """Test the SN cadence metric """
23 # Load up the files from sims_maf_contrib if possible
24 sims_maf_contrib_dir = os.getenv("SIMS_MAF_CONTRIB_DIR")
25 if sims_maf_contrib_dir is not None:
26 # Load required SN info to run the metric
27 band = 'r'
28 SNR = dict(zip('griz', [30., 40., 30., 20.])) # SNR for WFD
29 mag_range = [21., 25.5] # WFD mag range
30 dt_range = [0.5, 30.] # WFD dt range
31 Li_files = [os.path.join(
32 sims_maf_contrib_dir, 'data', 'Li_SNCosmo_-2.0_0.2.npy')]
33 mag_to_flux_files = [os.path.join(
34 sims_maf_contrib_dir, 'data', 'Mag_to_Flux_SNCosmo.npy')]
35 lim_sn = Lims(Li_files, mag_to_flux_files, band, SNR[band],
36 mag_range=mag_range, dt_range=dt_range)
38 # Define fake data
39 names = ['observationStartMJD', 'fieldRA', 'fieldDec',
40 'fiveSigmaDepth', 'visitExposureTime', 'numExposures', 'visitTime']
41 types = ['f8']*len(names)
42 names += ['night']
43 types += ['i2']
44 names += ['filter']
45 types += ['O']
47 day0 = 59000
48 daylast = day0+250
49 cadence = 5
50 dayobs = np.arange(day0, daylast, cadence)
51 npts = len(dayobs)
52 data = np.zeros(npts, dtype=list(zip(names, types)))
53 data['observationStartMJD'] = dayobs
54 data['night'] = np.floor(data['observationStartMJD']-day0)
55 data['fiveSigmaDepth'] = m5_ref[band]
56 data['visitExposureTime'] = 15.
57 data['numExposures'] = 2
58 data['visitTime'] = 2.*15.
59 data['filter'] = band
61 # Run the metric with these fake data
62 slicePoint = [0]
63 metric = SNCadenceMetric(lim_sn=lim_sn, coadd=False)
64 result = metric.run(data, slicePoint)
66 # And the result should be...
67 result_ref = 0.3743514
69 assert(np.abs(result-result_ref) < 1.e-5)
70 else:
71 warnings.warn(
72 "skipping SN test because no SIMS_MAF_CONTRIB_DIR set")
74 def testSNSNRMetric(self):
75 """Test the SN SNR metric """
77 sims_maf_contrib_dir = os.getenv("SIMS_MAF_CONTRIB_DIR")
78 if sims_maf_contrib_dir is not None:
79 # Load required SN info to run the metric
80 band = 'r'
81 z = 0.3
82 season = 1.
83 Li_files = [os.path.join(
84 sims_maf_contrib_dir, 'data', 'Li_SNCosmo_-2.0_0.2.npy')]
85 mag_to_flux_files = [os.path.join(
86 sims_maf_contrib_dir, 'data', 'Mag_to_Flux_SNCosmo.npy')]
88 names_ref = ['SNCosmo']
89 coadd = False
91 lim_sn = ReferenceData(Li_files, mag_to_flux_files, band, z)
93 # Define fake data
94 names = ['observationStartMJD', 'fieldRA', 'fieldDec',
95 'fiveSigmaDepth', 'visitExposureTime', 'numExposures', 'visitTime', 'season']
96 types = ['f8']*len(names)
97 names += ['night']
98 types += ['i2']
99 names += ['filter']
100 types += ['O']
102 dayobs = [59948.31957176, 59959.2821412, 59970.26134259,
103 59973.25978009, 59976.26383102, 59988.20670139, 59991.18412037,
104 60004.1853588, 60032.08975694, 60045.11981481, 60047.98747685,
105 60060.02083333, 60071.986875, 60075.96452546]
106 day0 = np.min(dayobs)
107 npts = len(dayobs)
108 data = np.zeros(npts, dtype=list(zip(names, types)))
110 data['observationStartMJD'] = dayobs
111 data['night'] = np.floor(data['observationStartMJD']-day0)
112 data['fiveSigmaDepth'] = m5_ref[band]
113 data['visitExposureTime'] = 15.
114 data['numExposures'] = 2
115 data['visitTime'] = 2.*15.
116 data['season'] = season
117 data['filter'] = band
119 # Run the metric with these fake data
120 slicePoint = [0]
121 metric = SNSNRMetric(
122 lim_sn=lim_sn, coadd=coadd, names_ref=names_ref, season=season, z=z)
124 result = metric.run(data, slicePoint)
126 # And the result should be...
127 result_ref = 0.4830508474576271
129 assert(np.abs(result-result_ref) < 1.e-5)
130 else:
131 warnings.warn(
132 "skipping SN test because no SIMS_MAF_CONTRIB_DIR set")
135def setup_module(module):
136 lsst.utils.tests.init()
139if __name__ == "__main__": 139 ↛ 140line 139 didn't jump to line 140, because the condition on line 139 was never true
140 lsst.utils.tests.init()
141 unittest.main()