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

# This file is part of pipe_base. 

# 

# 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/>. 

from __future__ import annotations 

 

"""Module defining Pipeline class and related methods. 

""" 

 

__all__ = ["Pipeline", "TaskDef", "TaskDatasetTypes", "PipelineDatasetTypes"] 

 

# ------------------------------- 

# Imports of standard modules -- 

# ------------------------------- 

from dataclasses import dataclass 

from typing import FrozenSet, Mapping, Type 

from types import MappingProxyType 

 

# ----------------------------- 

# Imports for other modules -- 

# ----------------------------- 

from lsst.daf.butler import DatasetType, DimensionUniverse 

from .pipelineTask import PipelineTask 

from .config import PipelineTaskConfig 

 

# ---------------------------------- 

# Local non-exported definitions -- 

# ---------------------------------- 

 

# ------------------------ 

# Exported definitions -- 

# ------------------------ 

 

 

class TaskDef: 

"""TaskDef is a collection of information about task needed by Pipeline. 

 

The information includes task name, configuration object and optional 

task class. This class is just a collection of attributes and it exposes 

all of them so that attributes could potentially be modified in place 

(e.g. if configuration needs extra overrides). 

 

Attributes 

---------- 

taskName : `str` 

`PipelineTask` class name, currently it is not specified whether this 

is a fully-qualified name or partial name (e.g. ``module.TaskClass``). 

Framework should be prepared to handle all cases. 

config : `lsst.pex.config.Config` 

Instance of the configuration class corresponding to this task class, 

usually with all overrides applied. 

taskClass : `type` or ``None`` 

`PipelineTask` class object, can be ``None``. If ``None`` then 

framework will have to locate and load class. 

label : `str`, optional 

Task label, usually a short string unique in a pipeline. 

""" 

def __init__(self, taskName, config, taskClass=None, label=""): 

self.taskName = taskName 

self.config = config 

self.taskClass = taskClass 

self.label = label 

 

def __str__(self): 

rep = "TaskDef(" + self.taskName 

if self.label: 

rep += ", label=" + self.label 

rep += ")" 

return rep 

 

 

class Pipeline(list): 

"""Pipeline is a sequence of `TaskDef` objects. 

 

Pipeline is given as one of the inputs to a supervising framework 

which builds execution graph out of it. Pipeline contains a sequence 

of `TaskDef` instances. 

 

Main purpose of this class is to provide a mechanism to pass pipeline 

definition from users to supervising framework. That mechanism is 

implemented using simple serialization and de-serialization via 

`pickle`. Note that pipeline serialization is not guaranteed to be 

compatible between different versions or releases. 

 

In current implementation Pipeline is a list (it inherits from `list`) 

and one can use all list methods on pipeline. Content of the pipeline 

can be modified, it is up to the client to verify that modifications 

leave pipeline in a consistent state. One could modify container 

directly by adding or removing its elements. 

 

Parameters 

---------- 

pipeline : iterable of `TaskDef` instances, optional 

Initial sequence of tasks. 

""" 

def __init__(self, iterable=None): 

list.__init__(self, iterable or []) 

 

def labelIndex(self, label): 

"""Return task index given its label. 

 

Parameters 

---------- 

label : `str` 

Task label. 

 

Returns 

------- 

index : `int` 

Task index, or -1 if label is not found. 

""" 

for idx, taskDef in enumerate(self): 

if taskDef.label == label: 

return idx 

return -1 

 

def __str__(self): 

infos = [str(tdef) for tdef in self] 

return "Pipeline({})".format(", ".join(infos)) 

 

 

@dataclass(frozen=True) 

class TaskDatasetTypes: 

"""An immutable struct that extracts and classifies the dataset types used 

by a `PipelineTask` 

""" 

 

initInputs: FrozenSet[DatasetType] 

"""Dataset types that are needed as inputs in order to construct this Task. 

 

Task-level `initInputs` may be classified as either 

`~PipelineDatasetTypes.initInputs` or 

`~PipelineDatasetTypes.initIntermediates` at the Pipeline level. 

""" 

 

initOutputs: FrozenSet[DatasetType] 

"""Dataset types that may be written after constructing this Task. 

 

Task-level `initOutputs` may be classified as either 

`~PipelineDatasetTypes.initOutputs` or 

`~PipelineDatasetTypes.initIntermediates` at the Pipeline level. 

""" 

 

inputs: FrozenSet[DatasetType] 

"""Dataset types that are regular inputs to this Task. 

 

If an input dataset needed for a Quantum cannot be found in the input 

collection(s) or produced by another Task in the Pipeline, that Quantum 

(and all dependent Quanta) will not be produced. 

 

Task-level `inputs` may be classified as either 

`~PipelineDatasetTypes.inputs` or `~PipelineDatasetTypes.intermediates` 

at the Pipeline level. 

""" 

 

prerequisites: FrozenSet[DatasetType] 

"""Dataset types that are prerequisite inputs to this Task. 

 

Prerequisite inputs must exist in the input collection(s) before the 

pipeline is run, but do not constrain the graph - if a prerequisite is 

missing for a Quantum, `PrerequisiteMissingError` is raised. 

 

Prerequisite inputs are not resolved until the second stage of 

QuantumGraph generation. 

""" 

 

outputs: FrozenSet[DatasetType] 

"""Dataset types that are produced by this Task. 

 

Task-level `outputs` may be classified as either 

`~PipelineDatasetTypes.outputs` or `~PipelineDatasetTypes.intermediates` 

at the Pipeline level. 

""" 

 

@classmethod 

def fromTask(cls, taskClass: Type[PipelineTask], config: PipelineTaskConfig, *, 

universe: DimensionUniverse) -> TaskDatasetTypes: 

"""Extract and classify the dataset types from a single `PipelineTask`. 

 

Parameters 

---------- 

taskClass: `type` 

A concrete `PipelineTask` subclass. 

config: `PipelineTaskConfig` 

Configuration for the concrete `PipelineTask`. 

universe: `DimensionUniverse` 

Set of all known dimensions, used to construct normalized 

`DatasetType` objects. 

 

Returns 

------- 

types: `TaskDatasetTypes` 

The dataset types used by this task. 

""" 

# TODO: there is both a bit too much repetition here and not quite 

# enough to make it worthwhile to refactor it (i.e. inputs and 

# prerequisites are special, so we can't use the same code for them 

# as we could for the others). But other work on PipelineTask 

# interfaces will eventually make this moot. 

allInputsByArgName = {k: descr.makeDatasetType(universe) 

for k, descr in taskClass.getInputDatasetTypes(config).items()} 

prerequisiteArgNames = taskClass.getPrerequisiteDatasetTypes(config) 

return cls( 

initInputs=frozenset(descr.makeDatasetType(universe) 

for descr in taskClass.getInitInputDatasetTypes(config).values()), 

initOutputs=frozenset(descr.makeDatasetType(universe) 

for descr in taskClass.getInitOutputDatasetTypes(config).values()), 

inputs=frozenset(v for k, v in allInputsByArgName.items() if k not in prerequisiteArgNames), 

prerequisites=frozenset(v for k, v in allInputsByArgName.items() if k in prerequisiteArgNames), 

outputs=frozenset(descr.makeDatasetType(universe) 

for descr in taskClass.getOutputDatasetTypes(config).values()), 

) 

 

 

@dataclass(frozen=True) 

class PipelineDatasetTypes: 

"""An immutable struct that classifies the dataset types used in a 

`Pipeline`. 

""" 

 

initInputs: FrozenSet[DatasetType] 

"""Dataset types that are needed as inputs in order to construct the Tasks 

in this Pipeline. 

 

This does not include dataset types that are produced when constructing 

other Tasks in the Pipeline (these are classified as `initIntermediates`). 

""" 

 

initOutputs: FrozenSet[DatasetType] 

"""Dataset types that may be written after constructing the Tasks in this 

Pipeline. 

 

This does not include dataset types that are also used as inputs when 

constructing other Tasks in the Pipeline (these are classified as 

`initIntermediates`). 

""" 

 

initIntermediates: FrozenSet[DatasetType] 

"""Dataset types that are both used when constructing one or more Tasks 

in the Pipeline and produced as a side-effect of constructing another 

Task in the Pipeline. 

""" 

 

inputs: FrozenSet[DatasetType] 

"""Dataset types that are regular inputs for the full pipeline. 

 

If an input dataset needed for a Quantum cannot be found in the input 

collection(s), that Quantum (and all dependent Quanta) will not be 

produced. 

""" 

 

prerequisites: FrozenSet[DatasetType] 

"""Dataset types that are prerequisite inputs for the full Pipeline. 

 

Prerequisite inputs must exist in the input collection(s) before the 

pipeline is run, but do not constrain the graph - if a prerequisite is 

missing for a Quantum, `PrerequisiteMissingError` is raised. 

 

Prerequisite inputs are not resolved until the second stage of 

QuantumGraph generation. 

""" 

 

intermediates: FrozenSet[DatasetType] 

"""Dataset types that are output by one Task in the Pipeline and consumed 

as inputs by one or more other Tasks in the Pipeline. 

""" 

 

outputs: FrozenSet[DatasetType] 

"""Dataset types that are output by a Task in the Pipeline and not consumed 

by any other Task in the Pipeline. 

""" 

 

byTask: Mapping[str, TaskDatasetTypes] 

"""Per-Task dataset types, keyed by label in the `Pipeline`. 

 

This is guaranteed to be zip-iterable with the `Pipeline` itself (assuming 

neither has been modified since the dataset types were extracted, of 

course). 

""" 

 

@classmethod 

def fromPipeline(cls, pipeline: Pipeline, *, universe: DimensionUniverse) -> PipelineDatasetTypes: 

"""Extract and classify the dataset types from all tasks in a 

`Pipeline`. 

 

Parameters 

---------- 

pipeline: `Pipeline` 

An ordered collection of tasks that can be run together. 

universe: `DimensionUniverse` 

Set of all known dimensions, used to construct normalized 

`DatasetType` objects. 

 

Returns 

------- 

types: `PipelineDatasetTypes` 

The dataset types used by this `Pipeline`. 

 

Raises 

------ 

ValueError 

Raised if Tasks are inconsistent about which datasets are marked 

prerequisite. This indicates that the Tasks cannot be run as part 

of the same `Pipeline`. 

""" 

allInputs = set() 

allOutputs = set() 

allInitInputs = set() 

allInitOutputs = set() 

prerequisites = set() 

byTask = dict() 

for taskDef in pipeline: 

thisTask = TaskDatasetTypes.fromTask(taskDef.taskClass, taskDef.config, universe=universe) 

allInitInputs.update(thisTask.initInputs) 

allInitOutputs.update(thisTask.initOutputs) 

allInputs.update(thisTask.inputs) 

prerequisites.update(thisTask.prerequisites) 

allOutputs.update(thisTask.outputs) 

byTask[taskDef.label] = thisTask 

if not prerequisites.isdisjoint(allInputs): 

raise ValueError("{} marked as both prerequisites and regular inputs".format( 

{dt.name for dt in allInputs & prerequisites} 

)) 

if not prerequisites.isdisjoint(allOutputs): 

raise ValueError("{} marked as both prerequisites and outputs".format( 

{dt.name for dt in allOutputs & prerequisites} 

)) 

return cls( 

initInputs=frozenset(allInitInputs - allInitOutputs), 

initIntermediates=frozenset(allInitInputs & allInitOutputs), 

initOutputs=frozenset(allInitOutputs - allInitInputs), 

inputs=frozenset(allInputs - allOutputs), 

intermediates=frozenset(allInputs & allOutputs), 

outputs=frozenset(allOutputs - allInputs), 

prerequisites=frozenset(prerequisites), 

byTask=MappingProxyType(byTask), # MappingProxyType -> frozen view of dict for immutability 

)