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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

# 

# LSST Data Management System 

# 

# This product includes software developed by the 

# LSST Project (http://www.lsst.org/). 

# 

# See COPYRIGHT file at the top of the source tree. 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the LSST License Statement and 

# the GNU General Public License along with this program. If not, 

# see <https://www.lsstcorp.org/LegalNotices/>. 

# 

from __future__ import print_function 

 

import astropy.units as u 

import unittest 

 

from lsst.verify import (Job, Metric, ThresholdSpecification, Measurement, 

MeasurementSet, MetricSet, SpecificationSet, Datum, 

Blob) 

 

 

class JobTestCase(unittest.TestCase): 

"""Test Job classes.""" 

 

def setUp(self): 

# Mock metrics 

self.metric_photrms = Metric('test.PhotRms', 'Photometric RMS', 'mmag') 

self.metric_photmed = Metric('test.PhotMedian', 

'Median magntidue', 'mag') 

self.metric_set = MetricSet([self.metric_photrms, self.metric_photmed]) 

 

# Mock specifications 

self.spec_photrms_design = ThresholdSpecification( 

'test.PhotRms.design', 20. * u.mmag, '<' 

) 

self.spec_set = SpecificationSet([self.spec_photrms_design]) 

 

# Mock measurements 

self.meas_photrms = Measurement( 

self.metric_photrms, 15 * u.mmag, 

notes={'note': 'value'}) 

self.meas_photrms.extras['n_stars'] = Datum( 

250, 

label='N stars', 

description='Number of stars included in RMS estimate') 

self.measurement_set = MeasurementSet([self.meas_photrms]) 

 

# Metrics for Job 2 

self.metric_test_2 = Metric('test2.SourceCount', 'Source Count', '') 

self.blob_test_2 = Blob( 

'test2_blob', 

sn=Datum(50 * u.dimensionless_unscaled, label='S/N')) 

self.metric_set_2 = MetricSet([self.metric_test_2]) 

 

# Specifications for Job 2 

self.spec_test_2 = ThresholdSpecification( 

'test2.SourceCount.design', 100 * u.dimensionless_unscaled, '>=') 

self.spec_set_2 = SpecificationSet([self.spec_test_2]) 

 

# Measurements for Job 2 

self.meas_test_2_SourceCount = Measurement( 

self.metric_test_2, 200 * u.dimensionless_unscaled) 

self.meas_test_2_SourceCount.link_blob(self.blob_test_2) 

self.measurement_set_2 = MeasurementSet([self.meas_test_2_SourceCount]) 

 

def test_job(self): 

"""Create a Job from object sets.""" 

job = Job(metrics=self.metric_set, specs=self.spec_set, 

measurements=self.measurement_set) 

 

# Test object access via properties 

self.assertIn('test.PhotRms.design', job.specs) 

self.assertIn('test.PhotRms', job.metrics) 

self.assertIn('test.PhotRms', job.measurements) 

 

# Test metadata access 

self.assertIn('test.PhotRms.note', job.meta) 

self.assertEqual(job.meta['test.PhotRms.note'], 'value') 

# measurement metadata is always prefixed 

self.assertNotIn('note', job.meta) 

 

job.meta['job-level-key'] = 'yes' 

self.assertEqual(job.meta['job-level-key'], 'yes') 

self.assertIn('job-level-key', job.meta) 

 

self.assertEqual(len(job.meta), 2) 

 

job.meta.update({'test.PhotRms.note2': 'foo', 

'dataset': 'ci_hsc'}) 

# note2 should be in measurement notes 

self.assertEqual( 

job.measurements['test.PhotRms'].notes['note2'], 

'foo') 

self.assertEqual(job.meta['dataset'], 'ci_hsc') 

# Delete measurement and job-level metadata 

del job.meta['test.PhotRms.note2'] 

self.assertNotIn('test.PhotRms.note2', job.meta) 

self.assertNotIn('note2', job.measurements['test.PhotRms'].notes) 

del job.meta['dataset'] 

self.assertNotIn('dataset', job.meta) 

 

self.assertEqual( 

set(job.meta.keys()), 

set(['job-level-key', 'test.PhotRms.note']) 

) 

self.assertEqual( 

set([key for key in job.meta]), 

set(['job-level-key', 'test.PhotRms.note']) 

) 

keys = set() 

for key, value in job.meta.items(): 

keys.add(key) 

self.assertEqual(keys, set(['job-level-key', 'test.PhotRms.note'])) 

 

# Add a new measurement 

m = Measurement('test.PhotMedian', 28.5 * u.mag, 

notes={'aperture_corr': True}) 

job.measurements.insert(m) 

self.assertIn('test.PhotMedian', job.measurements) 

self.assertEqual(job.meta['test.PhotMedian.aperture_corr'], True) 

 

# Test serialization 

json_doc = job.json 

 

self.assertIn('measurements', json_doc) 

self.assertEqual(len(json_doc['measurements']), len(job.measurements)) 

 

self.assertIn('blobs', json_doc) 

 

self.assertIn('metrics', json_doc) 

self.assertEqual(len(json_doc['metrics']), len(job.metrics)) 

 

self.assertIn('specs', json_doc) 

self.assertEqual(len(json_doc['specs']), len(job.specs)) 

 

self.assertIn('meta', json_doc) 

self.assertEqual(len(json_doc['meta']), len(job.meta)) 

 

new_job = Job.deserialize(**json_doc) 

self.assertEqual(job, new_job) 

 

# check job-to-measurement metadata deserialization 

self.assertEqual( 

new_job.measurements['test.PhotRms'].notes['note'], 

'value') 

self.assertEqual( 

new_job.meta['test.PhotRms.note'], 

'value') 

self.assertEqual( 

new_job.meta['job-level-key'], 

'yes') 

 

def test_job_iadd(self): 

job_1 = Job(metrics=self.metric_set, specs=self.spec_set, 

measurements=self.measurement_set) 

job_2 = Job(metrics=self.metric_set_2, specs=self.spec_set_2, 

measurements=self.measurement_set_2) 

 

job_1 += job_2 

 

self.assertIn(self.metric_photrms.name, job_1.metrics) 

self.assertIn(self.metric_test_2.name, job_1.metrics) 

self.assertIn('test.PhotRms.design', job_1.specs) 

self.assertIn('test2.SourceCount.design', job_1.specs) 

self.assertIn('test.PhotRms', job_1.measurements) 

self.assertIn('test2.SourceCount', job_1.measurements) 

self.assertIn('test.PhotRms', job_1.measurements['test.PhotRms'].blobs) 

self.assertIn( 

'test2_blob', 

job_1.measurements['test2.SourceCount'].blobs) 

 

def test_metric_package_reload(self): 

# Create a Job without Metric definitions 

meas = Measurement('validate_drp.PA1', 15 * u.mmag) 

measurement_set = MeasurementSet([meas]) 

 

job = Job(measurements=measurement_set) 

job.reload_metrics_package('verify_metrics') 

 

# Should now have metrics and specs 

self.assertTrue(len(job.specs) > 0) 

self.assertTrue(len(job.metrics) > 0) 

self.assertIsInstance( 

job.measurements['validate_drp.PA1'].metric, 

 

Metric) 

 

 

200 ↛ 201line 200 didn't jump to line 201, because the condition on line 200 was never trueif __name__ == "__main__": 

unittest.main()