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

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

# 

# LSST Data Management System 

# Copyright 2008, 2009, 2010 LSST Corporation. 

# 

# This product includes software developed by the 

# LSST Project (http://www.lsst.org/). 

# 

# 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 <http://www.lsstcorp.org/LegalNotices/>. 

# 

 

__all__ = ("Registry", "makeRegistry", "RegistryField", "registerConfig", "registerConfigurable") 

 

import collections.abc 

import copy 

 

from .config import Config, FieldValidationError, _typeStr 

from .configChoiceField import ConfigInstanceDict, ConfigChoiceField 

 

 

class ConfigurableWrapper: 

"""A wrapper for configurables. 

 

Used for configurables that don't contain a ``ConfigClass`` attribute, 

or contain one that is being overridden. 

""" 

 

def __init__(self, target, ConfigClass): 

self.ConfigClass = ConfigClass 

self._target = target 

 

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

return self._target(*args, **kwargs) 

 

 

class Registry(collections.abc.Mapping): 

"""A base class for global registries, which map names to configurables. 

 

A registry acts like a read-only dictionary with an additional `register` 

method to add targets. Targets in the registry are configurables (see 

*Notes*). 

 

Parameters 

---------- 

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

The base class for config classes in the registry. 

 

Notes 

----- 

A configurable is a callable with call signature ``(config, *args)`` 

Configurables typically create an algorithm or are themselves the 

algorithm. Often configurables are `lsst.pipe.base.Task` subclasses, but 

this is not required. 

 

A ``Registry`` has these requirements: 

 

- All configurables added to a particular registry have the same call 

signature. 

- All configurables in a registry typically share something important 

in common. For example, all configurables in ``psfMatchingRegistry`` 

return a PSF matching class that has a ``psfMatch`` method with a 

particular call signature. 

 

Examples 

-------- 

This examples creates a configurable class ``Foo`` and adds it to a 

registry. First, creating the configurable: 

 

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

>>> class FooConfig(Config): 

... val = Field(dtype=int, default=3, doc="parameter for Foo") 

... 

>>> class Foo: 

... ConfigClass = FooConfig 

... def __init__(self, config): 

... self.config = config 

... def addVal(self, num): 

... return self.config.val + num 

... 

 

Next, create a ``Registry`` instance called ``registry`` and register the 

``Foo`` configurable under the ``"foo"`` key: 

 

>>> registry = Registry() 

>>> registry.register("foo", Foo) 

>>> print(list(registry.keys())) 

["foo"] 

 

Now ``Foo`` is conveniently accessible from the registry itself. 

 

Finally, use the registry to get the configurable class and create an 

instance of it: 

 

>>> FooConfigurable = registry["foo"] 

>>> foo = FooConfigurable(FooConfigurable.ConfigClass()) 

>>> foo.addVal(5) 

8 

""" 

 

def __init__(self, configBaseType=Config): 

if not issubclass(configBaseType, Config): 

raise TypeError("configBaseType=%s must be a subclass of Config" % _typeStr(configBaseType,)) 

self._configBaseType = configBaseType 

self._dict = {} 

 

def register(self, name, target, ConfigClass=None): 

"""Add a new configurable target to the registry. 

 

Parameters 

---------- 

name : `str` 

Name that the ``target`` is registered under. The target can 

be accessed later with `dict`-like patterns using ``name`` as 

the key. 

target : obj 

A configurable type, usually a subclass of `lsst.pipe.base.Task`. 

ConfigClass : `lsst.pex.config.Config`-type, optional 

A subclass of `lsst.pex.config.Config` used to configure the 

configurable. If `None` then the configurable's ``ConfigClass`` 

attribute is used. 

 

Raises 

------ 

RuntimeError 

Raised if an item with ``name`` is already in the registry. 

AttributeError 

Raised if ``ConfigClass`` is `None` and ``target`` does not have 

a ``ConfigClass`` attribute. 

 

Notes 

----- 

If ``ConfigClass`` is provided then the ``target`` configurable is 

wrapped in a new object that forwards function calls to it. Otherwise 

the original ``target`` is stored. 

""" 

if name in self._dict: 

raise RuntimeError("An item with name %r already exists" % name) 

if ConfigClass is None: 

wrapper = target 

else: 

wrapper = ConfigurableWrapper(target, ConfigClass) 

if not issubclass(wrapper.ConfigClass, self._configBaseType): 

raise TypeError("ConfigClass=%s is not a subclass of %r" % 

(_typeStr(wrapper.ConfigClass), _typeStr(self._configBaseType))) 

self._dict[name] = wrapper 

 

def __getitem__(self, key): 

return self._dict[key] 

 

def __len__(self): 

return len(self._dict) 

 

def __iter__(self): 

return iter(self._dict) 

 

def __contains__(self, key): 

return key in self._dict 

 

def makeField(self, doc, default=None, optional=False, multi=False): 

"""Create a `RegistryField` configuration field from this registry. 

 

Parameters 

---------- 

doc : `str` 

A description of the field. 

default : object, optional 

The default target for the field. 

optional : `bool`, optional 

When `False`, `lsst.pex.config.Config.validate` fails if the 

field's value is `None`. 

multi : `bool`, optional 

A flag to allow multiple selections in the `RegistryField` if 

`True`. 

 

Returns 

------- 

field : `lsst.pex.config.RegistryField` 

`~lsst.pex.config.RegistryField` Configuration field. 

""" 

return RegistryField(doc, self, default, optional, multi) 

 

 

class RegistryAdaptor(collections.abc.Mapping): 

"""Private class that makes a `Registry` behave like the thing a 

`~lsst.pex.config.ConfigChoiceField` expects. 

 

Parameters 

---------- 

registry : `Registry` 

`Registry` instance. 

""" 

 

def __init__(self, registry): 

self.registry = registry 

 

def __getitem__(self, k): 

return self.registry[k].ConfigClass 

 

def __iter__(self): 

return iter(self.registry) 

 

def __len__(self): 

return len(self.registry) 

 

def __contains__(self, k): 

return k in self.registry 

 

 

class RegistryInstanceDict(ConfigInstanceDict): 

"""Dictionary of instantiated configs, used to populate a `RegistryField`. 

 

Parameters 

---------- 

config : `lsst.pex.config.Config` 

Configuration instance. 

field : `RegistryField` 

Configuration field. 

""" 

 

def __init__(self, config, field): 

ConfigInstanceDict.__init__(self, config, field) 

self.registry = field.registry 

 

def _getTarget(self): 

if self._field.multi: 

raise FieldValidationError(self._field, self._config, 

"Multi-selection field has no attribute 'target'") 

return self._field.typemap.registry[self._selection] 

 

target = property(_getTarget) 

 

def _getTargets(self): 

if not self._field.multi: 

raise FieldValidationError(self._field, self._config, 

"Single-selection field has no attribute 'targets'") 

return [self._field.typemap.registry[c] for c in self._selection] 

 

targets = property(_getTargets) 

 

def apply(self, *args, **kw): 

"""Call the active target(s) with the active config as a keyword arg 

 

If this is a multi-selection field, return a list obtained by calling 

each active target with its corresponding active config. 

 

Additional arguments will be passed on to the configurable target(s) 

""" 

if self.active is None: 

msg = "No selection has been made. Options: %s" % \ 

(" ".join(list(self._field.typemap.registry.keys()))) 

raise FieldValidationError(self._field, self._config, msg) 

if self._field.multi: 

retvals = [] 

for c in self._selection: 

retvals.append(self._field.typemap.registry[c](*args, config=self[c], **kw)) 

return retvals 

else: 

return self._field.typemap.registry[self.name](*args, config=self[self.name], **kw) 

 

def __setattr__(self, attr, value): 

if attr == "registry": 

object.__setattr__(self, attr, value) 

else: 

ConfigInstanceDict.__setattr__(self, attr, value) 

 

 

class RegistryField(ConfigChoiceField): 

"""A configuration field whose options are defined in a `Registry`. 

 

Parameters 

---------- 

doc : `str` 

A description of the field. 

registry : `Registry` 

The registry that contains this field. 

default : `str`, optional 

The default target key. 

optional : `bool`, optional 

When `False`, `lsst.pex.config.Config.validate` fails if the field's 

value is `None`. 

multi : `bool`, optional 

If `True`, the field allows multiple selections. The default is 

`False`. 

 

See also 

-------- 

ChoiceField 

ConfigChoiceField 

ConfigDictField 

ConfigField 

ConfigurableField 

DictField 

Field 

ListField 

RangeField 

""" 

 

instanceDictClass = RegistryInstanceDict 

"""Class used to hold configurable instances in the field. 

""" 

 

def __init__(self, doc, registry, default=None, optional=False, multi=False): 

types = RegistryAdaptor(registry) 

self.registry = registry 

ConfigChoiceField.__init__(self, doc, types, default, optional, multi) 

 

def __deepcopy__(self, memo): 

"""Customize deep-copying, want a reference to the original registry. 

 

WARNING: this must be overridden by subclasses if they change the 

constructor signature! 

""" 

other = type(self)(doc=self.doc, registry=self.registry, 

default=copy.deepcopy(self.default), 

optional=self.optional, multi=self.multi) 

other.source = self.source 

return other 

 

 

def makeRegistry(doc, configBaseType=Config): 

"""Create a `Registry`. 

 

Parameters 

---------- 

doc : `str` 

Docstring for the created `Registry` (this is set as the ``__doc__`` 

attribute of the `Registry` instance. 

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

Base type of config classes in the `Registry` 

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

 

Returns 

------- 

registry : `Registry` 

Registry with ``__doc__`` and `~Registry.configBaseType` attributes 

set. 

""" 

cls = type("Registry", (Registry,), {"__doc__": doc}) 

return cls(configBaseType=configBaseType) 

 

 

def registerConfigurable(name, registry, ConfigClass=None): 

"""A decorator that adds a class as a configurable in a `Registry` 

instance. 

 

Parameters 

---------- 

name : `str` 

Name of the target (the decorated class) in the ``registry``. 

registry : `Registry` 

The `Registry` instance that the decorated class is added to. 

ConfigClass : `lsst.pex.config.Config`-type, optional 

Config class associated with the configurable. If `None`, the class's 

``ConfigClass`` attribute is used instead. 

 

See also 

-------- 

registerConfig 

 

Notes 

----- 

Internally, this decorator runs `Registry.register`. 

""" 

def decorate(cls): 

registry.register(name, target=cls, ConfigClass=ConfigClass) 

return cls 

return decorate 

 

 

def registerConfig(name, registry, target): 

"""Decorator that adds a class as a ``ConfigClass`` in a `Registry` and 

associates it with the given configurable. 

 

Parameters 

---------- 

name : `str` 

Name of the ``target`` in the ``registry``. 

registry : `Registry` 

The registry containing the ``target``. 

target : obj 

A configurable type, such as a subclass of `lsst.pipe.base.Task`. 

 

See also 

-------- 

registerConfigurable 

 

Notes 

----- 

Internally, this decorator runs `Registry.register`. 

""" 

def decorate(cls): 

registry.register(name, target=target, ConfigClass=cls) 

return cls 

return decorate