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

# 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__ = ["register", "registerMultiple"] 

 

from lsst.pex.config import Config, ConfigDictField, Registry 

from .metricTask import MetricTask 

 

 

def register(name): 

"""A class decorator that registers a `lsst.verify.compatibility.MetricTask` 

with a central repository. 

 

Parameters 

---------- 

name : `str` 

The name under which this decorator will register the 

`~lsst.verify.compatibility.MetricTask`. 

 

Raises 

------ 

RuntimeError 

Raised if another class has already been registered under ``name``. 

ValueError 

Raised if this decorator is applied to a class that is not a 

`lsst.verify.compatibility.MetricTask`. 

 

Notes 

----- 

This decorator must be used for any `~lsst.verify.compatibility.MetricTask` 

that is to be used with `lsst.verify.compatibility.MetricsControllerTask`. 

 

Examples 

-------- 

The decorator is applied at the class definition: 

 

>>> from lsst.verify.compatibility import register, MetricTask 

>>> @register("dummy") 

... class DummyMetricTask(MetricTask): 

... pass 

""" 

def wrapper(taskClass): 

61 ↛ 67line 61 didn't jump to line 67, because the condition on line 61 was never false if issubclass(taskClass, MetricTask): 

# TODO: if MetricRegistry is phased out, simply return taskClass 

# instead of removing the decorator 

MetricRegistry.registry.register(name, taskClass) 

return taskClass 

else: 

raise ValueError("%r is not a %r" % (taskClass, MetricTask)) 

return wrapper 

 

 

def registerMultiple(name): 

"""A class decorator that registers a `lsst.verify.compatibility.MetricTask` 

with a central repository. 

 

Unlike `register`, this decorator assumes the same 

`~lsst.verify.compatibility.MetricTask` class will be run by 

`lsst.verify.compatibility.MetricsControllerTask` multiple times with 

different configs. 

 

Parameters 

---------- 

name : `str` 

The name under which this decorator will register the 

`~lsst.verify.compatibility.MetricTask`. 

 

Raises 

------ 

RuntimeError 

Raised if another class has already been registered under ``name``. 

ValueError 

Raised if this decorator is applied to a class that is not a 

`lsst.verify.compatibility.MetricTask`. 

 

Notes 

----- 

This decorator must be used for any `~lsst.verify.compatibility.MetricTask` 

that will have multiple instances used with 

`lsst.verify.compatibility.MetricsControllerTask`. 

 

The registry entry produced by this decorator corresponds to an anonymous 

`~lsst.pex.config.Config` class with one field, ``configs``. ``configs`` 

is a `~lsst.pex.config.ConfigDictField` that may have any number of 

configs attached to it. The field will create multiple 

`~lsst.verify.compatibility.MetricTask` objects, one for each config 

provided. See :lsst-task:`~lsst.verify.compatibility.MetricsControllerTask` 

for an example of how to use ``configs``. 

 

Examples 

-------- 

The decorator is applied at the class definition: 

 

>>> from lsst.verify.compatibility import registerMultiple, MetricTask 

>>> @registerMultiple("reusable") 

... class ReusableMetricTask(MetricTask): 

... pass 

""" 

def wrapper(taskClass): 

118 ↛ 127line 118 didn't jump to line 127, because the condition on line 118 was never false if issubclass(taskClass, MetricTask): 

# TODO: if MetricRegistry is phased out, simply return taskClass 

# instead of removing the decorator 

MetricRegistry.registry.register( 

name, 

_MultiConfigFactory(taskClass), 

ConfigClass=_makeMultiConfig(taskClass.ConfigClass)) 

return taskClass 

else: 

raise ValueError("%r is not a %r" % (taskClass, MetricTask)) 

return wrapper 

 

 

def _makeMultiConfig(configClass): 

"""A factory function for creating a config for registerMultiple. 

 

Parameters 

---------- 

configClass : `lsst.verify.compatibility.MetricTask.ConfigClass`-type 

The type of task config to be stored inside the new config. Subclasses 

of ``configClass`` will **NOT** be supported (this is a limitation of 

`~lsst.pex.config.ConfigDictField`). 

 

Returns 

------- 

multiConfig : `lsst.pex.config.Config`-type 

A `~lsst.pex.config.Config` class containing the following fields: 

 

configs : `lsst.pex.config.ConfigDictField` 

A field that maps `str` to ``configClass``. The keys are arbitrary 

and can be chosen for user convenience. 

""" 

class MultiConfig(Config): 

configs = ConfigDictField( 

keytype=str, itemtype=configClass, optional=False, 

default={}, 

doc="A collection of multiple configs to create multiple items " 

"of the same type.") 

 

return MultiConfig 

 

 

class _MultiConfigFactory: 

"""A factory class for creating multiple `MetricTask` objects at once. 

 

Parameters 

---------- 

configurableClass : `lsst.verify.compatibility.MetricTask`-type 

The type of configurable created by `__call__`. 

""" 

def __init__(self, configurableClass): 

self._configurableClass = configurableClass 

 

def __call__(self, config, **kwargs): 

"""Create the configured task(s). 

 

Parameters 

---------- 

config : type from ``_makeMultiConfig(configurableClass.ConfigClass)`` 

A config containing multiple configs for ``configurableClass`. 

**kwargs 

Additional arguments to the ``configurableClass`` constructor. 

 

Returns 

------- 

tasks : iterable of ``configurableClass`` 

A sequence of ``configurableClass``, one for each config in 

``config``. The order in which the objects will be returned 

is undefined. 

""" 

return [self._configurableClass(subConfig, **kwargs) 

for subConfig in config.configs.values()] 

 

 

class MetricRegistry: 

"""Registry of all `lsst.verify.compatibility.MetricTask` subclasses known 

to `lsst.verify`'s client. 

 

Notes 

----- 

This class has a singleton-like architecture in case a custom subclass of 

`lsst.pex.config.Registry` is needed in the future. Code that refers to 

``MetricRegistry.registry`` should be agnostic to such changes. 

""" 

 

registry = Registry(MetricTask.ConfigClass) 

"""A unique registry of ``MetricTasks`` or collections of ``MetricTasks`` 

(`lsst.pex.config.Registry`). 

"""