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

# 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"] 

 

import abc 

 

from lsst.pipe.base import Struct, PipelineTaskConnections, connectionTypes 

from lsst.verify.gen2tasks import MetricTask 

from lsst.verify.tasks import MetricComputationError 

 

 

class MetadataMetricConnections( 

PipelineTaskConnections, 

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

defaultTemplates={"taskName": ""}): 

metadata = connectionTypes.Input( 

name="{taskName}_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=True, 

) 

 

 

class MetadataMetricConfig(MetricTask.ConfigClass, 

pipelineConnections=MetadataMetricConnections): 

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

 

Notes 

----- 

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

this class as-is. Classes representing metrics of a different granularity 

should use `setDefaults` to override ``metadata.dimensions``. 

""" 

pass 

 

 

class MetadataMetricTask(MetricTask): 

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

 

Parameters 

---------- 

*args 

**kwargs 

Constructor parameters are the same as for 

`lsst.pipe.base.PipelineTask`. 

 

Notes 

----- 

This class should be customized by overriding `getInputMetadataKeys`, 

`makeMeasurement`, and `getOutputMetricName`. 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 

 

@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. 

""" 

 

@abc.abstractmethod 

def makeMeasurement(self, values): 

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

 

Parameters 

---------- 

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

A list where each element corresponds to a metadata object passed 

to `run`. Each `dict` 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 `adaptArgsAndRun` for expected behavior. 

 

Notes 

----- 

As with all `lsst.verify.gen2tasks.MetricTask` subclasses, this method 

should assume a many-to-one relationship between input data and the 

resulting metric, i.e., it should not assume that the output metric 

and the input data have the same granularity. In the common case that 

they do, ``values`` will contain only one element. 

""" 

 

@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 

 

def run(self, metadata): 

"""Compute a measurement from science task metadata. 

 

Parameters 

---------- 

metadata : iterable of `lsst.daf.base.PropertySet` 

A collection of metadata objects, one for each unit of science 

processing to be incorporated into this metric. Its elements 

may be `None` to represent missing data. 

 

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 element of ``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) 

 

values = [] 

for singleMetadata in metadata: 

if singleMetadata is not None: 

data = self._extractMetadata(singleMetadata, metadataKeys) 

values.append(data) 

else: 

values.append({dataName: None for dataName in metadataKeys}) 

 

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