Hide keyboard shortcuts

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 

11from lsst.sims.maf.metrics.snSLMetric import SNSLMetric 

12import os 

13import warnings 

14 

15m5_ref = dict( 

16 zip('ugrizy', [23.60, 24.83, 24.38, 23.92, 23.35, 22.44])) 

17 

18 

19def fakeData(band, season=1): 

20 

21 # Define fake data 

22 names = ['observationStartMJD', 'fieldRA', 'fieldDec', 

23 'fiveSigmaDepth', 'visitExposureTime', 

24 'numExposures', 'visitTime', 'season', 

25 'seeingFwhmEff', 'seeingFwhmGeom', 

26 'airmass', 'sky', 'moonPhase', 'pixRA', 'pixDec'] 

27 

28 types = ['f8']*len(names) 

29 names += ['night'] 

30 types += ['i2'] 

31 names += ['healpixID'] 

32 types += ['i2'] 

33 names += ['filter'] 

34 types += ['O'] 

35 

36 dayobs = [59948.31957176, 59959.2821412, 59970.26134259, 

37 59973.25978009, 59976.26383102, 59988.20670139, 59991.18412037, 

38 60004.1853588, 60032.08975694, 60045.11981481, 60047.98747685, 

39 60060.02083333, 60071.986875, 60075.96452546] 

40 day0 = np.min(dayobs) 

41 npts = len(dayobs) 

42 data = np.zeros(npts, dtype=list(zip(names, types))) 

43 data['observationStartMJD'] = dayobs 

44 data['night'] = np.floor(data['observationStartMJD']-day0+1) 

45 data['fiveSigmaDepth'] = m5_ref[band] 

46 data['visitExposureTime'] = 15. 

47 data['numExposures'] = 2 

48 data['visitTime'] = 2.*15. 

49 data['season'] = season 

50 data['filter'] = band 

51 data['seeingFwhmEff'] = 0. 

52 data['seeingFwhmGeom'] = 0. 

53 data['airmass'] = 1.2 

54 data['sky'] = 20.0 

55 data['moonPhase'] = 0.5 

56 data['pixRA'] = 0.0 

57 data['pixDec'] = 0.0 

58 data['healpixID'] = 1 

59 

60 return data 

61 

62 

63class TestSNmetrics(unittest.TestCase): 

64 

65 def testSNCadenceMetric(self): 

66 """Test the SN cadence metric """ 

67 

68 # Load up the files from sims_maf_contrib if possible 

69 sims_maf_contrib_dir = os.getenv("SIMS_MAF_CONTRIB_DIR") 

70 if sims_maf_contrib_dir is not None: 

71 # Load required SN info to run the metric 

72 band = 'r' 

73 SNR = dict(zip('griz', [30., 40., 30., 20.])) # SNR for WFD 

74 mag_range = [21., 25.5] # WFD mag range 

75 dt_range = [0.5, 30.] # WFD dt range 

76 Li_files = [os.path.join( 

77 sims_maf_contrib_dir, 'data', 'Li_SNCosmo_-2.0_0.2.npy')] 

78 mag_to_flux_files = [os.path.join( 

79 sims_maf_contrib_dir, 'data', 'Mag_to_Flux_SNCosmo.npy')] 

80 lim_sn = Lims(Li_files, mag_to_flux_files, band, SNR[band], 

81 mag_range=mag_range, dt_range=dt_range) 

82 

83 # Define fake data 

84 names = ['observationStartMJD', 'fieldRA', 'fieldDec', 

85 'fiveSigmaDepth', 'visitExposureTime', 'numExposures', 'visitTime'] 

86 types = ['f8']*len(names) 

87 names += ['night'] 

88 types += ['i2'] 

89 names += ['filter'] 

90 types += ['O'] 

91 

92 day0 = 59000 

93 daylast = day0+250 

94 cadence = 5 

95 dayobs = np.arange(day0, daylast, cadence) 

96 npts = len(dayobs) 

97 data = np.zeros(npts, dtype=list(zip(names, types))) 

98 data['observationStartMJD'] = dayobs 

99 data['night'] = np.floor(data['observationStartMJD']-day0) 

100 data['fiveSigmaDepth'] = m5_ref[band] 

101 data['visitExposureTime'] = 15. 

102 data['numExposures'] = 2 

103 data['visitTime'] = 2.*15. 

104 data['filter'] = band 

105 

106 # Run the metric with these fake data 

107 slicePoint = {'nside': 64} 

108 metric = SNCadenceMetric(lim_sn=lim_sn, coadd=False) 

109 result = metric.run(data, slicePoint) 

110 

111 # And the result should be... 

112 result_ref = 0.3743514 

113 

114 assert(np.abs(result-result_ref) < 1.e-5) 

115 else: 

116 warnings.warn( 

117 "skipping SN test because no SIMS_MAF_CONTRIB_DIR set") 

118 

119 def testSNSNRMetric(self): 

120 """Test the SN SNR metric """ 

121 

122 sims_maf_contrib_dir = os.getenv("SIMS_MAF_CONTRIB_DIR") 

123 if sims_maf_contrib_dir is not None: 

124 # Load required SN info to run the metric 

125 band = 'r' 

126 z = 0.3 

127 season = 1. 

128 Li_files = [os.path.join( 

129 sims_maf_contrib_dir, 'data', 'Li_SNCosmo_-2.0_0.2.npy')] 

130 mag_to_flux_files = [os.path.join( 

131 sims_maf_contrib_dir, 'data', 'Mag_to_Flux_SNCosmo.npy')] 

132 

133 names_ref = ['SNCosmo'] 

134 coadd = False 

135 

136 lim_sn = ReferenceData(Li_files, mag_to_flux_files, band, z) 

137 

138 # Define fake data 

139 names = ['observationStartMJD', 'fieldRA', 'fieldDec', 

140 'fiveSigmaDepth', 'visitExposureTime', 'numExposures', 'visitTime', 'season'] 

141 types = ['f8']*len(names) 

142 names += ['night'] 

143 types += ['i2'] 

144 names += ['filter'] 

145 types += ['O'] 

146 

147 dayobs = [59948.31957176, 59959.2821412, 59970.26134259, 

148 59973.25978009, 59976.26383102, 59988.20670139, 59991.18412037, 

149 60004.1853588, 60032.08975694, 60045.11981481, 60047.98747685, 

150 60060.02083333, 60071.986875, 60075.96452546] 

151 day0 = np.min(dayobs) 

152 npts = len(dayobs) 

153 data = np.zeros(npts, dtype=list(zip(names, types))) 

154 

155 data['observationStartMJD'] = dayobs 

156 data['night'] = np.floor(data['observationStartMJD']-day0) 

157 data['fiveSigmaDepth'] = m5_ref[band] 

158 data['visitExposureTime'] = 15. 

159 data['numExposures'] = 2 

160 data['visitTime'] = 2.*15. 

161 data['season'] = season 

162 data['filter'] = band 

163 

164 # Run the metric with these fake data 

165 slicePoint = {'nside': 64} 

166 metric = SNSNRMetric( 

167 lim_sn=lim_sn, coadd=coadd, names_ref=names_ref, season=season, z=z) 

168 

169 result = metric.run(data, slicePoint) 

170 

171 # And the result should be... 

172 result_ref = 0.4830508474576271 

173 

174 assert(np.abs(result-result_ref) < 1.e-5) 

175 else: 

176 warnings.warn( 

177 "skipping SN test because no SIMS_MAF_CONTRIB_DIR set") 

178 

179 def testSNSLMetric(self): 

180 """Test the SN SNR metric """ 

181 

182 # load some fake data 

183 data = None 

184 bands = 'griz' 

185 cadence = dict(zip(bands, [2, 1, 2, 1])) 

186 for band in bands: 

187 for i in range(cadence[band]): 

188 fakes = fakeData(band) 

189 if data is None: 

190 data = fakes 

191 else: 

192 data = np.concatenate((data, fakes)) 

193 

194 # metric instance 

195 night_collapse = True 

196 

197 metric = SNSLMetric(night_collapse=night_collapse) 

198 

199 # run the metric 

200 nSL = metric.run(data, slicePoint={'nside': 64}) 

201 

202 # and the result should be 

203 

204 nSL_ref = 0.00012650940 

205 

206 assert(np.abs(nSL-nSL_ref) < 1.e-8) 

207 

208 

209def setup_module(module): 

210 lsst.utils.tests.init() 

211 

212 

213if __name__ == "__main__": 213 ↛ 214line 213 didn't jump to line 214, because the condition on line 213 was never true

214 lsst.utils.tests.init() 

215 unittest.main()