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

278

279

280

281

282

283

284

285

286

287

288

289

# 

# 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, division 

 

__all__ = ['ThresholdSpecification'] 

 

import operator 

 

import astropy.units as u 

from astropy.tests.helper import quantity_allclose 

 

from ..jsonmixin import JsonSerializationMixin 

from ..datum import Datum 

from ..naming import Name 

from .base import Specification 

 

 

class ThresholdSpecification(Specification): 

"""A threshold-type specification, associated with a `Metric`, that 

defines a binary comparison against a measurement. 

 

Parameters 

---------- 

name : `str` 

Name of the specification for a metric. LPM-17, for example, 

uses ``'design'``, ``'minimum'`` and ``'stretch'`` terminology. 

quantity : `astropy.units.Quantity` 

The specification threshold level. 

operator_str : `str` 

The threshold's binary comparison operator. The operator is oriented 

so that ``measurement {{ operator }} threshold quantity`` is the 

specification test. Can be one of: ``'<'``, ``'<='``, ``'>'``, 

``'>='``, ``'=='``, or ``'!='``. 

metadata_query : `dict`, optional 

Dictionary of key-value terms that the measurement's metadata must 

have for this specification to apply. 

tags : sequence of `str`, optional 

Sequence of tags that group this specification with others. 

kwargs : `dict` 

Keyword arguments passed directly to the 

`lsst.validate.base.Specification` constructor. 

 

Raises 

------ 

TypeError 

If ``name`` is not compartible with `~lsst.verify.Name`, 

or `threshold` is not a `~astropy.units.Quantity`, or if the 

``operator_str`` cannot be converted into a Python binary comparison 

operator. 

""" 

 

threshold = None 

"""The specification threshold level (`astropy.units.Quantity`).""" 

 

def __init__(self, name, threshold, operator_str, **kwargs): 

Specification.__init__(self, name, **kwargs) 

 

self.threshold = threshold 

if not isinstance(self.threshold, u.Quantity): 

message = 'threshold {0!r} must be an astropy.units.Quantity' 

raise TypeError(message.format(self.threshold)) 

if not self.threshold.isscalar: 

raise TypeError('threshold must be scalar') 

 

try: 

self.operator_str = operator_str 

except ValueError: 

message = '{0!r} is not a known operator'.format(operator_str) 

raise TypeError(message) 

 

@property 

def type(self): 

return 'threshold' 

 

def __eq__(self, other): 

return (self.type == other.type) and \ 

(self.name == other.name) and \ 

quantity_allclose(self.threshold, other.threshold) and \ 

(self.operator_str == other.operator_str) 

 

def __ne__(self, other): 

return not self.__eq__(other) 

 

def __repr__(self): 

return "ThresholdSpecification({0!r}, {1!r}, {2!r})".format( 

self.name, 

self.threshold, 

self.operator_str) 

 

def __str__(self): 

return '{self.operator_str} {self.threshold}'.format(self=self) 

 

def _repr_latex_(self): 

"""Get a LaTeX-formatted string representation of the threshold 

specification test. 

 

Returns 

------- 

rep : `str` 

String representation. 

""" 

template = ('$x$ {self.operator_str} ' 

'{self.threshold.value} ' 

'{self.threshold.unit:latex_inline}') 

return template.format(self=self) 

 

@property 

def datum(self): 

"""Representation of this `ThresholdSpecification`\ 's threshold as 

a `Datum`. 

""" 

return Datum(self.threshold, label=str(self.name)) 

 

@classmethod 

def deserialize(cls, name=None, threshold=None, 

metric=None, package=None, **kwargs): 

"""Deserialize from keys in a specification YAML document or a 

JSON serialization into a `ThresholdSpecification` instance. 

 

Parameters 

---------- 

name : `str` or `lsst.validate.base.Name` 

Specification name, either as a string or 

`~lsst.validate.base.Name`. 

threshold : `dict` 

A `dict` with fields: 

 

- ``'value'``: threshold value (`float` or `int`). 

- ``'unit'``: threshold unit, as an `astropy.units.Unit`- 

compatible `str`. 

- ``'operator'``: a binary comparison operator, described in 

the class parameters documentation (`str`). 

metric : `str` or `lsst.validate.base.Name`, optional 

Name of the fully-qualified name of the metric the specification 

corresponds to. This parameter is optional if ``name`` is already 

fully-qualified. 

package : `str` or `lsst.validate.base.Name`, optional 

Name of the package the specification corresponds to. This 

parameter is optional if ``name`` or ``metric`` are already 

fully-qualified. 

kwargs : `dict` 

Keyword arguments passed directly to the 

`lsst.validate.base.Specification` constructor. 

 

Returns 

------- 

specification : `ThresholdSpecification` 

A specification instance. 

""" 

_name = Name(metric=metric, spec=name) 

operator_str = threshold['operator'] 

_threshold = u.Quantity(threshold['value'], 

u.Unit(threshold['unit'])) 

return cls(_name, _threshold, operator_str, **kwargs) 

 

def _serialize_type(self): 

"""Serialize attributes of this specification type to a `dict` that is 

JSON-serializable. 

""" 

return JsonSerializationMixin.jsonify_dict( 

{ 

'value': self.threshold.value, 

'unit': self.threshold.unit.to_string(), 

'operator': self.operator_str 

} 

) 

 

@property 

def operator_str(self): 

"""Threshold comparision operator ('str'). 

 

A measurement *passes* the specification if:: 

 

measurement {{ operator }} threshold == True 

 

The operator string is a standard Python binary comparison token, such 

as: ``'<'``, ``'>'``, ``'<='``, ``'>='``, ``'=='`` or ``'!='``. 

""" 

return self._operator_str 

 

@operator_str.setter 

def operator_str(self, v): 

# Cache the operator function as a means of validating the input too 

self._operator = ThresholdSpecification.convert_operator_str(v) 

self._operator_str = v 

 

@property 

def operator(self): 

"""Binary comparision operator that tests success of a measurement 

fulfilling a specification of this metric. 

 

Measured value is on left side of comparison and specification level 

is on right side. 

""" 

return self._operator 

 

@staticmethod 

def convert_operator_str(op_str): 

"""Convert a string representing a binary comparison operator to 

the operator function itself. 

 

Operators are oriented so that the measurement is on the left-hand 

side, and specification threshold on the right hand side. 

 

The following operators are permitted: 

 

========== ============= 

``op_str`` Function 

========== ============= 

``>=`` `operator.ge` 

``>`` `operator.gt` 

``<`` `operator.lt` 

``<=`` `operator.le` 

``==`` `operator.eq` 

``!=`` `operator.ne` 

========== ============= 

 

Parameters 

---------- 

op_str : `str` 

A string representing a binary operator. 

 

Returns 

------- 

op_func : obj 

An operator function from the `operator` standard library 

module. 

 

Raises 

------ 

ValueError 

Raised if ``op_str`` is not a supported binary comparison operator. 

""" 

operators = {'>=': operator.ge, 

'>': operator.gt, 

'<': operator.lt, 

'<=': operator.le, 

'==': operator.eq, 

'!=': operator.ne} 

try: 

return operators[op_str] 

except KeyError: 

message = '{0!r} is not a supported threshold operator'.format( 

op_str) 

raise ValueError(message) 

 

def check(self, measurement): 

"""Check if a measurement passes this specification. 

 

Parameters 

---------- 

measurement : `astropy.units.Quantity` 

The measurement value. The measurement `~astropy.units.Quantity` 

must have units *compatible* with `threshold`. 

 

Returns 

------- 

passed : `bool` 

`True` if the measurement meets the specification, 

`False` otherwise. 

 

Raises 

------ 

astropy.units.UnitError 

Raised if the measurement cannot be compared to the threshold. 

For example, if the measurement is not an `astropy.units.Quantity` 

or if the units are not compatible. 

""" 

return self.operator(measurement, self.threshold)