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

# This file is part of ctrl_mpexec. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (http://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 <http://www.gnu.org/licenses/>. 

 

"""Bunch of common classes and methods for use in unit tests. 

""" 

 

__all__ = ["AddTaskConfig", "AddTask", "AddTaskFactoryMock", "ButlerMock"] 

 

import itertools 

import logging 

import numpy 

import os 

from types import SimpleNamespace 

 

from lsst.daf.butler import (ButlerConfig, DatasetRef, DimensionUniverse, 

DatasetType, Registry, Run, DatasetOriginInfoDef) 

import lsst.pex.config as pexConfig 

import lsst.pipe.base as pipeBase 

from lsst.pipe.base import connectionTypes as cT 

 

_LOG = logging.getLogger(__name__) 

 

 

class AddTaskConnections(pipeBase.PipelineTaskConnections, 

dimensions=("instrument", "detector")): 

input = cT.Input(name="add_input", 

dimensions=["instrument", "detector"], 

storageClass="NumpyArray", 

doc="Input dataset type for this task") 

output = cT.Output(name="add_output", 

dimensions=["instrument", "detector"], 

storageClass="NumpyArray", 

doc="Output dataset type for this task") 

initout = cT.InitOutput(name="add_init_output", 

storageClass="NumpyArray", 

doc="Init Output dataset type for this task") 

 

 

class AddTaskConfig(pipeBase.PipelineTaskConfig, 

pipelineConnections=AddTaskConnections): 

addend = pexConfig.Field(doc="amount to add", dtype=int, default=3) 

 

 

# example task which overrides run() method 

class AddTask(pipeBase.PipelineTask): 

ConfigClass = AddTaskConfig 

_DefaultName = "add_task" 

 

initout = numpy.array([999]) 

"""InitOutputs for this task""" 

 

countExec = 0 

"""Number of times run() method was called for this class""" 

 

stopAt = -1 

"""Raises exception at this call to run()""" 

 

def run(self, input): 

if AddTask.stopAt == AddTask.countExec: 

raise RuntimeError("pretend something bad happened") 

AddTask.countExec += 1 

self.metadata.add("add", self.config.addend) 

output = [val + self.config.addend for val in input] 

_LOG.info("input = %s, output = %s", input, output) 

return pipeBase.Struct(output=output) 

 

 

class AddTaskFactoryMock(pipeBase.TaskFactory): 

def loadTaskClass(self, taskName): 

if taskName == "AddTask": 

return AddTask, "AddTask" 

 

def makeTask(self, taskClass, config, overrides, butler): 

if config is None: 

config = taskClass.ConfigClass() 

if overrides: 

overrides.applyTo(config) 

return taskClass(config=config, initInputs=None) 

 

 

class ButlerMock: 

"""Mock version of butler, only usable for testing 

 

Parameters 

---------- 

fullRegistry : `boolean`, optional 

If True then instantiate SQLite registry with default configuration. 

If False then registry is just a namespace with `dimensions` attribute 

containing DimensionUniverse from default configuration. 

""" 

def __init__(self, fullRegistry=False, collection="TestColl"): 

self.datasets = {} 

self.fullRegistry = fullRegistry 

if self.fullRegistry: 

testDir = os.path.dirname(__file__) 

configFile = os.path.join(testDir, "config/butler.yaml") 

butlerConfig = ButlerConfig(configFile) 

self.registry = Registry.fromConfig(butlerConfig, create=True) 

self.run = self.registry.makeRun(collection) 

else: 

self.registry = SimpleNamespace(dimensions=DimensionUniverse.fromConfig()) 

self.run = Run(collection=collection, environment=None, pipeline=None) 

 

def _standardizeArgs(self, datasetRefOrType, dataId=None, **kwds): 

"""Copied from real Butler 

""" 

if isinstance(datasetRefOrType, DatasetRef): 

if dataId is not None or kwds: 

raise ValueError("DatasetRef given, cannot use dataId as well") 

datasetType = datasetRefOrType.datasetType 

dataId = datasetRefOrType.dataId 

else: 

# Don't check whether DataId is provided, because Registry APIs 

# can usually construct a better error message when it wasn't. 

if isinstance(datasetRefOrType, DatasetType): 

datasetType = datasetRefOrType 

else: 

datasetType = self.registry.getDatasetType(datasetRefOrType) 

return datasetType, dataId 

 

@staticmethod 

def key(dataId): 

"""Make a dict key out of dataId. 

""" 

return frozenset(dataId.items()) 

 

def get(self, datasetRefOrType, dataId=None, parameters=None, **kwds): 

datasetType, dataId = self._standardizeArgs(datasetRefOrType, dataId, **kwds) 

_LOG.info("butler.get: datasetType=%s dataId=%s", datasetType.name, dataId) 

dsTypeName = datasetType.name 

key = self.key(dataId) 

dsdata = self.datasets.get(dsTypeName) 

if dsdata: 

return dsdata.get(key) 

return None 

 

def put(self, obj, datasetRefOrType, dataId=None, producer=None, **kwds): 

datasetType, dataId = self._standardizeArgs(datasetRefOrType, dataId, **kwds) 

_LOG.info("butler.put: datasetType=%s dataId=%s obj=%r", datasetType.name, dataId, obj) 

dsTypeName = datasetType.name 

key = self.key(dataId) 

dsdata = self.datasets.setdefault(dsTypeName, {}) 

dsdata[key] = obj 

if self.fullRegistry: 

ref = self.registry.addDataset(datasetType, dataId, run=self.run, producer=producer, 

recursive=False, **kwds) 

else: 

# we should return DatasetRef with reasonable ID, ID is supposed to be unique 

refId = sum(len(val) for val in self.datasets.values()) 

ref = DatasetRef(datasetType, dataId, id=refId) 

return ref 

 

def remove(self, datasetRefOrType, dataId=None, *, delete=True, remember=True, **kwds): 

datasetType, dataId = self._standardizeArgs(datasetRefOrType, dataId, **kwds) 

_LOG.info("butler.remove: datasetType=%s dataId=%s", datasetType.name, dataId) 

dsTypeName = datasetType.name 

key = self.key(dataId) 

dsdata = self.datasets.get(dsTypeName) 

del dsdata[key] 

ref = self.registry.find(self.run.collection, datasetType, dataId, **kwds) 

if remember: 

self.registry.disassociate(self.run.collection, [ref]) 

else: 

self.registry.removeDataset(ref) 

 

 

def registerDatasetTypes(registry, pipeline): 

"""Register all dataset types used by tasks in a registry. 

 

Copied and modified from `PreExecInit.initializeDatasetTypes`. 

 

Parameters 

---------- 

registry : `~lsst.daf.butler.Registry` 

Registry instance. 

pipeline : `~lsst.pipe.base.Pipeline` 

Iterable of TaskDef instances. 

""" 

for taskDef in pipeline: 

datasetTypes = pipeBase.TaskDatasetTypes.fromConnections(taskDef.connections, 

universe=registry.dimensions) 

for datasetType in itertools.chain(datasetTypes.initInputs, datasetTypes.initOutputs, 

datasetTypes.inputs, datasetTypes.outputs, 

datasetTypes.prerequisites): 

_LOG.info("Registering %s with registry", datasetType) 

# this is a no-op if it already exists and is consistent, 

# and it raises if it is inconsistent. 

registry.registerDatasetType(datasetType) 

 

 

def makeSimpleQGraph(nQuanta=5, pipeline=None, butler=None, skipExisting=False, clobberExisting=False): 

"""Make simple QuantumGraph for tests. 

 

Makes simple one-task pipeline with AddTask, sets up in-memory 

registry and butler, fills them with minimal data, and generates 

QuantumGraph with all of that. 

 

Parameters 

---------- 

nQuanta : `int` 

Number of quanta in a graph. 

pipeline : `~lsst.pipe.base.Pipeline` 

If `None` then one-task pipeline is made with `AddTask` and 

default `AddTaskConfig`. 

butler : `~lsst.daf.butler.Butler`, optional 

Data butler instance, this should be an instance returned from a 

previous call to this method. 

skipExisting : `bool`, optional 

If `True` (default), a Quantum is not created if all its outputs 

already exist. 

clobberExisting : `bool`, optional 

If `True`, overwrite any outputs that already exist. Cannot be 

`True` if ``skipExisting`` is. 

 

Returns 

------- 

butler : `~lsst.daf.butler.Butler` 

Butler instance 

qgraph : `~lsst.pipe.base.QuantumGraph` 

Quantum graph instance 

""" 

 

if pipeline is None: 

taskDef = pipeBase.TaskDef("AddTask", AddTaskConfig(), taskClass=AddTask, label="task1") 

pipeline = pipeBase.Pipeline([taskDef]) 

 

if butler is None: 

 

butler = ButlerMock(fullRegistry=True) 

 

# Add dataset types to registry 

registerDatasetTypes(butler.registry, pipeline) 

 

# Small set of DataIds included in QGraph 

dataIds = [dict(instrument="INSTR", detector=detector) for detector in range(nQuanta)] 

 

# Add all needed dimensions to registry 

butler.registry.addDimensionEntry("instrument", dict(instrument="INSTR")) 

butler.registry.addDimensionEntryList("detector", dataIds) 

 

# Add inputs to butler 

for i, dataId in enumerate(dataIds): 

data = numpy.array([i, 10*i]) 

butler.put(data, "add_input", dataId) 

 

# Make the graph, task factory is not needed here 

builder = pipeBase.GraphBuilder(taskFactory=None, registry=butler.registry, 

skipExisting=skipExisting, clobberExisting=clobberExisting) 

originInfo = DatasetOriginInfoDef([butler.run.collection], butler.run.collection) 

qgraph = builder.makeGraph(pipeline, originInfo, "") 

 

return butler, qgraph