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

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

# 

# This file is part of ap_verify. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

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

# See the COPYRIGHT file at the top-level directory of this distribution 

# for details of code ownership. 

# 

# 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 GNU General Public License 

# along with this program. If not, see <http://www.gnu.org/licenses/>. 

# 

 

import unittest 

 

import numpy as np 

import astropy.units as u 

from astropy.tests.helper import assert_quantity_allclose 

 

import lsst.utils.tests 

import lsst.afw.image as afwImage 

from lsst.ip.isr import FringeTask 

from lsst.verify import Measurement, Name, MetricComputationError 

from lsst.verify.gen2tasks.testUtils import MetricTaskTestCase 

 

from lsst.ap.verify.measurements.profiling import measureRuntime, TimingMetricTask 

 

 

def _createFringe(width, height, filterName): 

"""Create a fringe frame 

 

Parameters 

---------- 

width, height: `int` 

Size of image 

filterName: `str` 

name of the filterName to use 

 

Returns 

------- 

fringe: `lsst.afw.image.ExposureF` 

Fringe frame 

""" 

image = afwImage.ImageF(width, height) 

array = image.getArray() 

freq = np.pi / 10.0 

x, y = np.indices(array.shape) 

array[x, y] = np.sin(freq * x) + np.sin(freq * y) 

exp = afwImage.makeExposure(afwImage.makeMaskedImage(image)) 

exp.setFilter(afwImage.Filter(filterName)) 

return exp 

 

 

class MeasureRuntimeTestSuite(lsst.utils.tests.TestCase): 

 

def setUp(self): 

"""Run a dummy instance of `FringeTask` so that test cases can measure it. 

""" 

# Create dummy filter and fringe so that `FringeTask` has short but 

# significant run time. 

# Code adapted from lsst.ip.isr.test_fringes 

size = 128 

dummyFilter = 'FILTER' 

afwImage.utils.defineFilter(dummyFilter, lambdaEff=0) 

exp = _createFringe(size, size, dummyFilter) 

 

# Create and run `FringeTask` itself 

config = FringeTask.ConfigClass() 

config.filters = [dummyFilter] 

config.num = 1000 

config.small = 1 

config.large = size // 4 

config.pedestal = False 

self.task = FringeTask(name="fringe", config=config) 

 

# As an optimization, let test cases choose whether to run the dummy task 

def runTask(): 

self.task.run(exp, exp) 

self.runTask = runTask 

 

def tearDown(self): 

del self.task 

 

def testValid(self): 

"""Verify that timing information can be recovered. 

""" 

self.runTask() 

meas = measureRuntime(self.task.getFullMetadata(), taskName='fringe', metricName='ip_isr.IsrTime') 

self.assertIsInstance(meas, Measurement) 

self.assertEqual(meas.metric_name, Name(metric='ip_isr.IsrTime')) 

self.assertGreater(meas.quantity, 0.0 * u.second) 

# Task normally takes 0.2 s, so this should be a safe margin of error 

self.assertLess(meas.quantity, 10.0 * u.second) 

 

def testNoMetric(self): 

"""Verify that trying to measure a nonexistent metric fails. 

""" 

self.runTask() 

with self.assertRaises(TypeError): 

measureRuntime(self.task.getFullMetadata(), taskName='fringe', metricName='foo.bar.FooBarTime') 

 

def testNotRun(self): 

"""Verify that trying to measure a real but inapplicable metric returns None. 

""" 

meas = measureRuntime(self.task.getFullMetadata(), taskName='fringe', metricName='ip_isr.IsrTime') 

self.assertIsNone(meas) 

 

 

class TimingMetricTestSuite(MetricTaskTestCase): 

_SCIENCE_TASK_NAME = "fringe" 

 

@staticmethod 

def _taskFactory(): 

"""Implements MetricTaskTestCase.taskFactory. 

""" 

return TimingMetricTask(config=TimingMetricTestSuite._standardConfig()) 

MetricTaskTestCase.taskFactory = _taskFactory 

 

@staticmethod 

def _standardConfig(): 

config = TimingMetricTask.ConfigClass() 

config.metadata.name = TimingMetricTestSuite._SCIENCE_TASK_NAME + "_metadata" 

config.target = TimingMetricTestSuite._SCIENCE_TASK_NAME + ".run" 

config.metric = "ip_isr.IsrTime" 

return config 

 

def setUp(self): 

"""Create a dummy instance of `FringeTask` so that test cases can 

run and measure it. 

""" 

super().setUp() 

self.config = TimingMetricTestSuite._standardConfig() 

 

# Create dummy filter and fringe so that `FringeTask` has short but 

# significant run time. 

# Code adapted from lsst.ip.isr.test_fringes 

size = 128 

dummyFilter = "FILTER" 

afwImage.utils.defineFilter(dummyFilter, lambdaEff=0) 

exp = _createFringe(size, size, dummyFilter) 

 

# Create and run `FringeTask` itself 

config = FringeTask.ConfigClass() 

config.filters = [dummyFilter] 

config.num = 1000 

config.small = 1 

config.large = size // 4 

config.pedestal = False 

self.scienceTask = FringeTask(name=TimingMetricTestSuite._SCIENCE_TASK_NAME, config=config) 

 

# As an optimization, let test cases choose whether to run the dummy task 

def runTask(): 

self.scienceTask.run(exp, exp) 

self.runTask = runTask 

 

def tearDown(self): 

del self.scienceTask 

 

def testValid(self): 

self.runTask() 

result = self.task.run([self.scienceTask.getFullMetadata()]) 

meas = result.measurement 

 

self.assertIsInstance(meas, Measurement) 

self.assertEqual(meas.metric_name, Name(metric=self.config.metric)) 

self.assertGreater(meas.quantity, 0.0 * u.second) 

# Task normally takes 0.2 s, so this should be a safe margin of error 

self.assertLess(meas.quantity, 10.0 * u.second) 

 

def testNoMetric(self): 

self.runTask() 

self.config.metric = "foo.bar.FooBarTime" 

task = TimingMetricTask(config=self.config) 

with self.assertRaises(TypeError): 

task.run([self.scienceTask.getFullMetadata()]) 

 

def testMissingData(self): 

result = self.task.run([None]) 

meas = result.measurement 

self.assertIsNone(meas) 

 

def testNoDataExpected(self): 

result = self.task.run([]) 

meas = result.measurement 

self.assertIsNone(meas) 

 

def testRunDifferentMethod(self): 

self.runTask() 

self.config.target = TimingMetricTestSuite._SCIENCE_TASK_NAME + ".runDataRef" 

task = TimingMetricTask(config=self.config) 

result = task.run([self.scienceTask.getFullMetadata()]) 

meas = result.measurement 

self.assertIsNone(meas) 

 

def testNonsenseKeys(self): 

self.runTask() 

metadata = self.scienceTask.getFullMetadata() 

startKeys = [key for key in metadata.paramNames(topLevelOnly=False) if "StartCpuTime" in key] 

for key in startKeys: 

metadata.remove(key) 

 

task = TimingMetricTask(config=self.config) 

with self.assertRaises(MetricComputationError): 

task.run([metadata]) 

 

def testBadlyTypedKeys(self): 

self.runTask() 

metadata = self.scienceTask.getFullMetadata() 

endKeys = [key for key in metadata.paramNames(topLevelOnly=False) if "EndCpuTime" in key] 

for key in endKeys: 

metadata.set(key, str(metadata.getAsDouble(key))) 

 

task = TimingMetricTask(config=self.config) 

with self.assertRaises(MetricComputationError): 

task.run([metadata]) 

 

def testGetInputDatasetTypes(self): 

types = TimingMetricTask.getInputDatasetTypes(self.config) 

# dict.keys() is a collections.abc.Set, which has a narrower interface than __builtins__.set... WTF??? 

self.assertSetEqual(set(types.keys()), {"metadata"}) 

self.assertEqual(types["metadata"], TimingMetricTestSuite._SCIENCE_TASK_NAME + "_metadata") 

 

def testFineGrainedMetric(self): 

self.runTask() 

metadata = self.scienceTask.getFullMetadata() 

inputData = {"metadata": [metadata]} 

inputDataIds = {"metadata": [{"visit": 42, "ccd": 1}]} 

outputDataId = {"measurement": {"visit": 42, "ccd": 1}} 

measDirect = self.task.run([metadata]).measurement 

measIndirect = self.task.adaptArgsAndRun(inputData, inputDataIds, outputDataId).measurement 

 

assert_quantity_allclose(measIndirect.quantity, measDirect.quantity) 

 

def testCoarseGrainedMetric(self): 

self.runTask() 

metadata = self.scienceTask.getFullMetadata() 

nCcds = 3 

inputData = {"metadata": [metadata] * nCcds} 

inputDataIds = {"metadata": [{"visit": 42, "ccd": x} for x in range(nCcds)]} 

outputDataId = {"measurement": {"visit": 42}} 

measDirect = self.task.run([metadata]).measurement 

measMany = self.task.adaptArgsAndRun(inputData, inputDataIds, outputDataId).measurement 

 

assert_quantity_allclose(measMany.quantity, nCcds * measDirect.quantity) 

 

def testGetOutputMetricName(self): 

self.assertEqual(TimingMetricTask.getOutputMetricName(self.config), Name(self.config.metric)) 

 

 

class MemoryTester(lsst.utils.tests.MemoryTestCase): 

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()