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

# 

# 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__ = ["Specification"] 

 

import abc 

from future.utils import with_metaclass 

from past.builtins import basestring 

 

from ..jsonmixin import JsonSerializationMixin 

from ..metaquery import MetadataQuery 

from ..naming import Name 

 

 

class Specification(with_metaclass(abc.ABCMeta, JsonSerializationMixin)): 

"""Specification base class. 

 

Specification classes must implement: 

 

- `type` 

- `_serialize_type` 

- `check` 

 

Subclasses should also call ``Specification.__init__`` to initialize 

the specifications ``name`` attribute (a `~lsst.verify.Name` 

instance). 

""" 

 

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

# interal object behind self.tags 

self._tags = set() 

 

# name attibute must be a Name instance representing a specification 

if not isinstance(name, Name): 

self._name = Name(spec=name) 

else: 

self._name = name 

if not self._name.is_spec: 

message = 'name {0!r} does not represent a specification' 

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

 

if 'metadata_query' in kwargs: 

self._metadata_query = MetadataQuery(kwargs['metadata_query']) 

else: 

self._metadata_query = MetadataQuery() 

 

if 'tags' in kwargs: 

self.tags = kwargs['tags'] 

 

@property 

def name(self): 

"""Specification name (`lsst.verify.Name`).""" 

return self._name 

 

@property 

def metric_name(self): 

"""Name of the metric this specification corresponds to 

(`lsst.verify.Name`).""" 

return Name(package=self.name.package, metric=self.name.metric) 

 

@property 

def tags(self): 

"""Tag labels (`set` of `str`).""" 

return self._tags 

 

@tags.setter 

def tags(self, t): 

# Ensure that tags is always a set. 

90 ↛ 91line 90 didn't jump to line 91, because the condition on line 90 was never true if isinstance(t, basestring): 

t = [t] 

self._tags = set(t) 

 

@abc.abstractproperty 

def type(self): 

"""Specification type (`str`).""" 

pass 

 

@abc.abstractmethod 

def _serialize_type(self): 

"""Serialize type-specific specification data to a JSON-serializable 

`dict`. 

 

This method is used by the `json` property as the value associated 

with the key named for `type`. 

""" 

pass 

 

@property 

def json(self): 

"""`dict` that can be serialized as semantic JSON, compatible with 

the SQUASH metric service. 

""" 

return JsonSerializationMixin.jsonify_dict( 

{ 

'name': str(self.name), 

'type': self.type, 

self.type: self._serialize_type(), 

'metadata_query': self._metadata_query, 

'tags': self.tags 

} 

) 

 

@abc.abstractmethod 

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 the specification. 

 

Returns 

------- 

passed : `bool` 

`True` if the measurement meets the specification, 

`False` otherwise. 

""" 

pass 

 

def query_metadata(self, metadata, arg_driven=False): 

"""Query a Job's metadata to determine if this specification applies. 

 

Parameters 

---------- 

metadata : `lsst.verify.Metadata` or `dict`-type 

Metadata mapping. Typically this is the `lsst.verify.Job.meta` 

attribute. 

arg_driven : `bool`, optional 

If `False` (default), ``metadata`` matches the ``MetadataQuery`` 

if ``metadata`` has all the terms defined in ``MetadataQuery``, 

and those terms match. If ``metadata`` has more terms than 

``MetadataQuery``, it can still match. This behavior is 

appropriate for finding if a specification applies to a Job 

given metadata. 

 

If `True`, the orientation of the matching is reversed. Now 

``metadata`` matches the ``MetadataQuery`` if ``MetadataQuery`` 

has all the terms defined in ``metadata`` and those terms match. 

If ``MetadataQuery`` has more terms than ``metadata``, it can 

still match. This behavior is appropriate for discovering 

specifications. 

 

Returns 

------- 

matched : `bool` 

`True` if this specification matches, `False` otherwise. 

 

See also 

-------- 

lsst.verify.MetadataQuery 

""" 

return self._metadata_query(metadata, arg_driven=arg_driven)