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 

11import os 

12import warnings 

13 

14m5_ref = dict( 

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

16 

17 

18class TestSNmetrics(unittest.TestCase): 

19 

20 def testSNCadenceMetric(self): 

21 """Test the SN cadence metric """ 

22 

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) 

37 

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'] 

46 

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 

60 

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) 

65 

66 # And the result should be... 

67 result_ref = 0.3743514 

68 

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") 

73 

74 def testSNSNRMetric(self): 

75 """Test the SN SNR metric """ 

76 

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')] 

87 

88 names_ref = ['SNCosmo'] 

89 coadd = False 

90 

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

92 

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'] 

101 

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))) 

109 

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 

118 

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) 

123 

124 result = metric.run(data, slicePoint) 

125 

126 # And the result should be... 

127 result_ref = 0.4830508474576271 

128 

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") 

133 

134 

135def setup_module(module): 

136 lsst.utils.tests.init() 

137 

138 

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()