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

271

272

273

274

275

276

277

# This file is part of verify. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (https://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 <https://www.gnu.org/licenses/>. 

 

__all__ = ["MetadataMetricTask", "MetadataMetricConfig", 

"SingleMetadataMetricConnections"] 

 

import abc 

 

from lsst.pipe.base import Struct, connectionTypes 

from lsst.verify.tasks import MetricTask, MetricConfig, MetricConnections, \ 

MetricComputationError 

 

 

class SingleMetadataMetricConnections( 

MetricConnections, 

dimensions={"instrument", "exposure", "detector"}, 

defaultTemplates={"labelName": "", "package": None, "metric": None}): 

"""An abstract connections class defining a metadata input. 

 

Notes 

----- 

``SingleMetadataMetricConnections`` defines the following dataset 

templates: 

 

``package`` 

Name of the metric's namespace. By 

:ref:`verify_metrics <verify-metrics-package>` convention, this is 

the name of the package the metric is most closely 

associated with. 

``metric`` 

Name of the metric, excluding any namespace. 

``labelName`` 

Pipeline label of the `~lsst.pipe.base.PipelineTask` or name of 

the `~lsst.pipe.base.CmdLineTask` whose metadata are being read. 

""" 

metadata = connectionTypes.Input( 

name="{labelName}_metadata", 

doc="The target top-level task's metadata. The name must be set to " 

"the metadata's butler type, such as 'processCcd_metadata'.", 

storageClass="PropertySet", 

dimensions={"Instrument", "Exposure", "Detector"}, 

multiple=False, 

) 

 

 

class MetadataMetricConfig( 

MetricConfig, 

pipelineConnections=SingleMetadataMetricConnections): 

"""A base class for metadata metric task configs. 

 

Notes 

----- 

`MetadataMetricTask` classes that have CCD-level granularity can use 

this class as-is. Support for metrics of a different granularity 

may be added later. 

""" 

pass 

 

 

class _AbstractMetadataMetricTask(MetricTask): 

"""A base class for tasks that compute metrics from metadata values. 

 

This class contains code that is agnostic to whether the input is one 

metadata object or many. 

 

Parameters 

---------- 

*args 

**kwargs 

Constructor parameters are the same as for 

`lsst.pipe.base.PipelineTask`. 

 

Notes 

----- 

This class should be customized by overriding `getInputMetadataKeys` 

and `makeMeasurement`. You should not need to override `run`. 

 

This class makes no assumptions about how to handle missing data; 

`makeMeasurement` may be called with `None` values, and is responsible 

for deciding how to deal with them. 

""" 

# Design note: getInputMetadataKeys and makeMeasurement are overrideable 

# methods rather than subtask(s) to keep the configs for 

# `MetricsControllerTask` as simple as possible. This was judged more 

# important than ensuring that no implementation details of MetricTask 

# can leak into application-specific code. 

 

@classmethod 

@abc.abstractmethod 

def getInputMetadataKeys(cls, config): 

"""Return the metadata keys read by this task. 

 

Parameters 

---------- 

config : ``cls.ConfigClass`` 

Configuration for this task. 

 

Returns 

------- 

keys : `dict` [`str`, `str`] 

The keys are the (arbitrary) names of values needed by 

`makeMeasurement`, the values are the metadata keys to be looked 

up. Metadata keys are assumed to include task prefixes in the 

format of `lsst.pipe.base.Task.getFullMetadata()`. This method may 

return a substring of the desired (full) key, but multiple matches 

for any key will cause an error. 

""" 

 

@staticmethod 

def _searchKeys(metadata, keyFragment): 

"""Search the metadata for all keys matching a substring. 

 

Parameters 

---------- 

metadata : `lsst.daf.base.PropertySet` 

A metadata object with task-qualified keys as returned by 

`lsst.pipe.base.Task.getFullMetadata()`. 

keyFragment : `str` 

A substring for a full metadata key. 

 

Returns 

------- 

keys : `set` of `str` 

All keys in ``metadata`` that have ``keyFragment`` as a substring. 

""" 

keys = metadata.paramNames(topLevelOnly=False) 

return {key for key in keys if keyFragment in key} 

 

@staticmethod 

def _extractMetadata(metadata, metadataKeys): 

"""Read multiple keys from a metadata object. 

 

Parameters 

---------- 

metadata : `lsst.daf.base.PropertySet` 

A metadata object, assumed not `None`. 

metadataKeys : `dict` [`str`, `str`] 

Keys are arbitrary labels, values are metadata keys (or their 

substrings) in the format of 

`lsst.pipe.base.Task.getFullMetadata()`. 

 

Returns 

------- 

metadataValues : `dict` [`str`, any] 

Keys are the same as for ``metadataKeys``, values are the value of 

each metadata key, or `None` if no matching key was found. 

 

Raises 

------ 

lsst.verify.tasks.MetricComputationError 

Raised if any metadata key string has more than one match 

in ``metadata``. 

""" 

data = {} 

for dataName, keyFragment in metadataKeys.items(): 

matchingKeys = MetadataMetricTask._searchKeys( 

metadata, keyFragment) 

if len(matchingKeys) == 1: 

key, = matchingKeys 

data[dataName] = metadata.getScalar(key) 

elif not matchingKeys: 

data[dataName] = None 

else: 

error = "String %s matches multiple metadata keys: %s" \ 

% (keyFragment, matchingKeys) 

raise MetricComputationError(error) 

return data 

 

 

class MetadataMetricTask(_AbstractMetadataMetricTask): 

"""A base class for tasks that compute metrics from single metadata objects. 

 

Parameters 

---------- 

*args 

**kwargs 

Constructor parameters are the same as for 

`lsst.pipe.base.PipelineTask`. 

 

Notes 

----- 

This class should be customized by overriding `getInputMetadataKeys` 

and `makeMeasurement`. You should not need to override `run`. 

 

This class makes no assumptions about how to handle missing data; 

`makeMeasurement` may be called with `None` values, and is responsible 

for deciding how to deal with them. 

""" 

# Design note: getInputMetadataKeys and makeMeasurement are overrideable 

# methods rather than subtask(s) to keep the configs for 

# `MetricsControllerTask` as simple as possible. This was judged more 

# important than ensuring that no implementation details of MetricTask 

# can leak into application-specific code. 

 

ConfigClass = MetadataMetricConfig 

 

@abc.abstractmethod 

def makeMeasurement(self, values): 

"""Compute the metric given the values of the metadata. 

 

Parameters 

---------- 

values : `dict` [`str`, any] 

A `dict` representation of the metadata passed to `run`. It has the 

same keys as returned by `getInputMetadataKeys`, and maps them to 

the values extracted from the metadata. Any value may be `None` to 

represent missing data. 

 

Returns 

------- 

measurement : `lsst.verify.Measurement` or `None` 

The measurement corresponding to the input data. 

 

Raises 

------ 

lsst.verify.tasks.MetricComputationError 

Raised if an algorithmic or system error prevents calculation of 

the metric. See `run` for expected behavior. 

""" 

 

def run(self, metadata): 

"""Compute a measurement from science task metadata. 

 

Parameters 

---------- 

metadata : `lsst.daf.base.PropertySet` or `None` 

A metadata object for the unit of science processing to use for 

this metric, or a collection of such objects if this task combines 

many units of processing into a single metric. 

 

Returns 

------- 

result : `lsst.pipe.base.Struct` 

A `~lsst.pipe.base.Struct` containing the following component: 

 

- ``measurement``: the value of the metric 

(`lsst.verify.Measurement` or `None`) 

 

Raises 

------ 

lsst.verify.tasks.MetricComputationError 

Raised if the strings returned by `getInputMetadataKeys` match 

more than one key in any metadata object. 

 

Notes 

----- 

This implementation calls `getInputMetadataKeys`, then searches for 

matching keys in each metadata. It then passes the values of these 

keys (or `None` if no match) to `makeMeasurement`, and returns its 

result to the caller. 

""" 

metadataKeys = self.getInputMetadataKeys(self.config) 

 

if metadata is not None: 

data = self._extractMetadata(metadata, metadataKeys) 

else: 

data = {dataName: None for dataName in metadataKeys} 

 

return Struct(measurement=self.makeMeasurement(data))