Coverage for python/lsst/pipe/base/pipeline.py: 20%
420 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-10-30 02:02 -0700
« prev ^ index » next coverage.py v6.5.0, created at 2022-10-30 02:02 -0700
1# This file is part of pipe_base.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
21from __future__ import annotations
23"""Module defining Pipeline class and related methods.
24"""
26__all__ = ["Pipeline", "TaskDef", "TaskDatasetTypes", "PipelineDatasetTypes", "LabelSpecifier"]
28import copy
29import logging
30import os
31import re
32import urllib.parse
33import warnings
35# -------------------------------
36# Imports of standard modules --
37# -------------------------------
38from dataclasses import dataclass
39from types import MappingProxyType
40from typing import (
41 TYPE_CHECKING,
42 AbstractSet,
43 Callable,
44 ClassVar,
45 Dict,
46 Generator,
47 Iterable,
48 Iterator,
49 Mapping,
50 Optional,
51 Set,
52 Tuple,
53 Type,
54 Union,
55 cast,
56)
58# -----------------------------
59# Imports for other modules --
60from lsst.daf.butler import DatasetType, NamedValueSet, Registry, SkyPixDimension
61from lsst.resources import ResourcePath, ResourcePathExpression
62from lsst.utils import doImportType
63from lsst.utils.introspection import get_full_type_name
65from . import pipelineIR, pipeTools
66from ._task_metadata import TaskMetadata
67from .config import PipelineTaskConfig
68from .configOverrides import ConfigOverrides
69from .connections import iterConnections
70from .pipelineTask import PipelineTask
71from .task import _TASK_METADATA_TYPE
73if TYPE_CHECKING: # Imports needed only for type annotations; may be circular. 73 ↛ 74line 73 didn't jump to line 74, because the condition on line 73 was never true
74 from lsst.obs.base import Instrument
75 from lsst.pex.config import Config
77# ----------------------------------
78# Local non-exported definitions --
79# ----------------------------------
81_LOG = logging.getLogger(__name__)
83# ------------------------
84# Exported definitions --
85# ------------------------
88@dataclass
89class LabelSpecifier:
90 """A structure to specify a subset of labels to load
92 This structure may contain a set of labels to be used in subsetting a
93 pipeline, or a beginning and end point. Beginning or end may be empty,
94 in which case the range will be a half open interval. Unlike python
95 iteration bounds, end bounds are *INCLUDED*. Note that range based
96 selection is not well defined for pipelines that are not linear in nature,
97 and correct behavior is not guaranteed, or may vary from run to run.
98 """
100 labels: Optional[Set[str]] = None
101 begin: Optional[str] = None
102 end: Optional[str] = None
104 def __post_init__(self) -> None:
105 if self.labels is not None and (self.begin or self.end):
106 raise ValueError(
107 "This struct can only be initialized with a labels set or a begin (and/or) end specifier"
108 )
111class TaskDef:
112 """TaskDef is a collection of information about task needed by Pipeline.
114 The information includes task name, configuration object and optional
115 task class. This class is just a collection of attributes and it exposes
116 all of them so that attributes could potentially be modified in place
117 (e.g. if configuration needs extra overrides).
119 Attributes
120 ----------
121 taskName : `str`, optional
122 The fully-qualified `PipelineTask` class name. If not provided,
123 ``taskClass`` must be.
124 config : `lsst.pipe.base.config.PipelineTaskConfig`, optional
125 Instance of the configuration class corresponding to this task class,
126 usually with all overrides applied. This config will be frozen. If
127 not provided, ``taskClass`` must be provided and
128 ``taskClass.ConfigClass()`` will be used.
129 taskClass : `type`, optional
130 `PipelineTask` class object; if provided and ``taskName`` is as well,
131 the caller guarantees that they are consistent. If not provided,
132 ``taskName`` is used to import the type.
133 label : `str`, optional
134 Task label, usually a short string unique in a pipeline. If not
135 provided, ``taskClass`` must be, and ``taskClass._DefaultName`` will
136 be used.
137 """
139 def __init__(
140 self,
141 taskName: Optional[str] = None,
142 config: Optional[PipelineTaskConfig] = None,
143 taskClass: Optional[Type[PipelineTask]] = None,
144 label: Optional[str] = None,
145 ):
146 if taskName is None:
147 if taskClass is None:
148 raise ValueError("At least one of `taskName` and `taskClass` must be provided.")
149 taskName = get_full_type_name(taskClass)
150 elif taskClass is None:
151 taskClass = doImportType(taskName)
152 if config is None:
153 if taskClass is None:
154 raise ValueError("`taskClass` must be provided if `config` is not.")
155 config = taskClass.ConfigClass()
156 if label is None:
157 if taskClass is None:
158 raise ValueError("`taskClass` must be provided if `label` is not.")
159 label = taskClass._DefaultName
160 self.taskName = taskName
161 try:
162 config.validate()
163 except Exception:
164 _LOG.error("Configuration validation failed for task %s (%s)", label, taskName)
165 raise
166 config.freeze()
167 self.config = config
168 self.taskClass = taskClass
169 self.label = label
170 self.connections = config.connections.ConnectionsClass(config=config)
172 @property
173 def configDatasetName(self) -> str:
174 """Name of a dataset type for configuration of this task (`str`)"""
175 return self.label + "_config"
177 @property
178 def metadataDatasetName(self) -> Optional[str]:
179 """Name of a dataset type for metadata of this task, `None` if
180 metadata is not to be saved (`str`)
181 """
182 if self.config.saveMetadata:
183 return self.makeMetadataDatasetName(self.label)
184 else:
185 return None
187 @classmethod
188 def makeMetadataDatasetName(cls, label: str) -> str:
189 """Construct the name of the dataset type for metadata for a task.
191 Parameters
192 ----------
193 label : `str`
194 Label for the task within its pipeline.
196 Returns
197 -------
198 name : `str`
199 Name of the task's metadata dataset type.
200 """
201 return f"{label}_metadata"
203 @property
204 def logOutputDatasetName(self) -> Optional[str]:
205 """Name of a dataset type for log output from this task, `None` if
206 logs are not to be saved (`str`)
207 """
208 if cast(PipelineTaskConfig, self.config).saveLogOutput:
209 return self.label + "_log"
210 else:
211 return None
213 def __str__(self) -> str:
214 rep = "TaskDef(" + self.taskName
215 if self.label:
216 rep += ", label=" + self.label
217 rep += ")"
218 return rep
220 def __eq__(self, other: object) -> bool:
221 if not isinstance(other, TaskDef):
222 return False
223 # This does not consider equality of configs when determining equality
224 # as config equality is a difficult thing to define. Should be updated
225 # after DM-27847
226 return self.taskClass == other.taskClass and self.label == other.label
228 def __hash__(self) -> int:
229 return hash((self.taskClass, self.label))
231 @classmethod
232 def _unreduce(cls, taskName: str, config: PipelineTaskConfig, label: str) -> TaskDef:
233 """Custom callable for unpickling.
235 All arguments are forwarded directly to the constructor; this
236 trampoline is only needed because ``__reduce__`` callables can't be
237 called with keyword arguments.
238 """
239 return cls(taskName=taskName, config=config, label=label)
241 def __reduce__(self) -> Tuple[Callable[[str, PipelineTaskConfig, str], TaskDef], Tuple[str, Config, str]]:
242 return (self._unreduce, (self.taskName, self.config, self.label))
245class Pipeline:
246 """A `Pipeline` is a representation of a series of tasks to run, and the
247 configuration for those tasks.
249 Parameters
250 ----------
251 description : `str`
252 A description of that this pipeline does.
253 """
255 def __init__(self, description: str):
256 pipeline_dict = {"description": description, "tasks": {}}
257 self._pipelineIR = pipelineIR.PipelineIR(pipeline_dict)
259 @classmethod
260 def fromFile(cls, filename: str) -> Pipeline:
261 """Load a pipeline defined in a pipeline yaml file.
263 Parameters
264 ----------
265 filename: `str`
266 A path that points to a pipeline defined in yaml format. This
267 filename may also supply additional labels to be used in
268 subsetting the loaded Pipeline. These labels are separated from
269 the path by a \\#, and may be specified as a comma separated
270 list, or a range denoted as beginning..end. Beginning or end may
271 be empty, in which case the range will be a half open interval.
272 Unlike python iteration bounds, end bounds are *INCLUDED*. Note
273 that range based selection is not well defined for pipelines that
274 are not linear in nature, and correct behavior is not guaranteed,
275 or may vary from run to run.
277 Returns
278 -------
279 pipeline: `Pipeline`
280 The pipeline loaded from specified location with appropriate (if
281 any) subsetting
283 Notes
284 -----
285 This method attempts to prune any contracts that contain labels which
286 are not in the declared subset of labels. This pruning is done using a
287 string based matching due to the nature of contracts and may prune more
288 than it should.
289 """
290 return cls.from_uri(filename)
292 @classmethod
293 def from_uri(cls, uri: ResourcePathExpression) -> Pipeline:
294 """Load a pipeline defined in a pipeline yaml file at a location
295 specified by a URI.
297 Parameters
298 ----------
299 uri: convertible to `ResourcePath`
300 If a string is supplied this should be a URI path that points to a
301 pipeline defined in yaml format, either as a direct path to the
302 yaml file, or as a directory containing a "pipeline.yaml" file (the
303 form used by `write_to_uri` with ``expand=True``). This uri may
304 also supply additional labels to be used in subsetting the loaded
305 Pipeline. These labels are separated from the path by a \\#, and
306 may be specified as a comma separated list, or a range denoted as
307 beginning..end. Beginning or end may be empty, in which case the
308 range will be a half open interval. Unlike python iteration bounds,
309 end bounds are *INCLUDED*. Note that range based selection is not
310 well defined for pipelines that are not linear in nature, and
311 correct behavior is not guaranteed, or may vary from run to run.
312 The same specifiers can be used with a `ResourcePath` object, by
313 being the sole contents in the fragments attribute.
315 Returns
316 -------
317 pipeline: `Pipeline`
318 The pipeline loaded from specified location with appropriate (if
319 any) subsetting
321 Notes
322 -----
323 This method attempts to prune any contracts that contain labels which
324 are not in the declared subset of labels. This pruning is done using a
325 string based matching due to the nature of contracts and may prune more
326 than it should.
327 """
328 # Split up the uri and any labels that were supplied
329 uri, label_specifier = cls._parse_file_specifier(uri)
330 pipeline: Pipeline = cls.fromIR(pipelineIR.PipelineIR.from_uri(uri))
332 # If there are labels supplied, only keep those
333 if label_specifier is not None:
334 pipeline = pipeline.subsetFromLabels(label_specifier)
335 return pipeline
337 def subsetFromLabels(self, labelSpecifier: LabelSpecifier) -> Pipeline:
338 """Subset a pipeline to contain only labels specified in labelSpecifier
340 Parameters
341 ----------
342 labelSpecifier : `labelSpecifier`
343 Object containing labels that describes how to subset a pipeline.
345 Returns
346 -------
347 pipeline : `Pipeline`
348 A new pipeline object that is a subset of the old pipeline
350 Raises
351 ------
352 ValueError
353 Raised if there is an issue with specified labels
355 Notes
356 -----
357 This method attempts to prune any contracts that contain labels which
358 are not in the declared subset of labels. This pruning is done using a
359 string based matching due to the nature of contracts and may prune more
360 than it should.
361 """
362 # Labels supplied as a set
363 if labelSpecifier.labels:
364 labelSet = labelSpecifier.labels
365 # Labels supplied as a range, first create a list of all the labels
366 # in the pipeline sorted according to task dependency. Then only
367 # keep labels that lie between the supplied bounds
368 else:
369 # Create a copy of the pipeline to use when assessing the label
370 # ordering. Use a dict for fast searching while preserving order.
371 # Remove contracts so they do not fail in the expansion step. This
372 # is needed because a user may only configure the tasks they intend
373 # to run, which may cause some contracts to fail if they will later
374 # be dropped
375 pipeline = copy.deepcopy(self)
376 pipeline._pipelineIR.contracts = []
377 labels = {taskdef.label: True for taskdef in pipeline.toExpandedPipeline()}
379 # Verify the bounds are in the labels
380 if labelSpecifier.begin is not None:
381 if labelSpecifier.begin not in labels:
382 raise ValueError(
383 f"Beginning of range subset, {labelSpecifier.begin}, not found in "
384 "pipeline definition"
385 )
386 if labelSpecifier.end is not None:
387 if labelSpecifier.end not in labels:
388 raise ValueError(
389 f"End of range subset, {labelSpecifier.end}, not found in pipeline definition"
390 )
392 labelSet = set()
393 for label in labels:
394 if labelSpecifier.begin is not None:
395 if label != labelSpecifier.begin:
396 continue
397 else:
398 labelSpecifier.begin = None
399 labelSet.add(label)
400 if labelSpecifier.end is not None and label == labelSpecifier.end:
401 break
402 return Pipeline.fromIR(self._pipelineIR.subset_from_labels(labelSet))
404 @staticmethod
405 def _parse_file_specifier(uri: ResourcePathExpression) -> Tuple[ResourcePath, Optional[LabelSpecifier]]:
406 """Split appart a uri and any possible label subsets"""
407 if isinstance(uri, str):
408 # This is to support legacy pipelines during transition
409 uri, num_replace = re.subn("[:](?!\\/\\/)", "#", uri)
410 if num_replace:
411 warnings.warn(
412 f"The pipeline file {uri} seems to use the legacy : to separate "
413 "labels, this is deprecated and will be removed after June 2021, please use "
414 "# instead.",
415 category=FutureWarning,
416 )
417 if uri.count("#") > 1:
418 raise ValueError("Only one set of labels is allowed when specifying a pipeline to load")
419 # Everything else can be converted directly to ResourcePath.
420 uri = ResourcePath(uri)
421 label_subset = uri.fragment or None
423 specifier: Optional[LabelSpecifier]
424 if label_subset is not None:
425 label_subset = urllib.parse.unquote(label_subset)
426 args: Dict[str, Union[Set[str], str, None]]
427 # labels supplied as a list
428 if "," in label_subset:
429 if ".." in label_subset:
430 raise ValueError(
431 "Can only specify a list of labels or a rangewhen loading a Pipline not both"
432 )
433 args = {"labels": set(label_subset.split(","))}
434 # labels supplied as a range
435 elif ".." in label_subset:
436 # Try to de-structure the labelSubset, this will fail if more
437 # than one range is specified
438 begin, end, *rest = label_subset.split("..")
439 if rest:
440 raise ValueError("Only one range can be specified when loading a pipeline")
441 args = {"begin": begin if begin else None, "end": end if end else None}
442 # Assume anything else is a single label
443 else:
444 args = {"labels": {label_subset}}
446 # MyPy doesn't like how cavalier kwarg construction is with types.
447 specifier = LabelSpecifier(**args) # type: ignore
448 else:
449 specifier = None
451 return uri, specifier
453 @classmethod
454 def fromString(cls, pipeline_string: str) -> Pipeline:
455 """Create a pipeline from string formatted as a pipeline document.
457 Parameters
458 ----------
459 pipeline_string : `str`
460 A string that is formatted according like a pipeline document
462 Returns
463 -------
464 pipeline: `Pipeline`
465 """
466 pipeline = cls.fromIR(pipelineIR.PipelineIR.from_string(pipeline_string))
467 return pipeline
469 @classmethod
470 def fromIR(cls, deserialized_pipeline: pipelineIR.PipelineIR) -> Pipeline:
471 """Create a pipeline from an already created `PipelineIR` object.
473 Parameters
474 ----------
475 deserialized_pipeline: `PipelineIR`
476 An already created pipeline intermediate representation object
478 Returns
479 -------
480 pipeline: `Pipeline`
481 """
482 pipeline = cls.__new__(cls)
483 pipeline._pipelineIR = deserialized_pipeline
484 return pipeline
486 @classmethod
487 def fromPipeline(cls, pipeline: Pipeline) -> Pipeline:
488 """Create a new pipeline by copying an already existing `Pipeline`.
490 Parameters
491 ----------
492 pipeline: `Pipeline`
493 An already created pipeline intermediate representation object
495 Returns
496 -------
497 pipeline: `Pipeline`
498 """
499 return cls.fromIR(copy.deepcopy(pipeline._pipelineIR))
501 def __str__(self) -> str:
502 return str(self._pipelineIR)
504 def addInstrument(self, instrument: Union[Instrument, str]) -> None:
505 """Add an instrument to the pipeline, or replace an instrument that is
506 already defined.
508 Parameters
509 ----------
510 instrument : `~lsst.daf.butler.instrument.Instrument` or `str`
511 Either a derived class object of a `lsst.daf.butler.instrument` or
512 a string corresponding to a fully qualified
513 `lsst.daf.butler.instrument` name.
514 """
515 if isinstance(instrument, str):
516 pass
517 else:
518 # TODO: assume that this is a subclass of Instrument, no type
519 # checking
520 instrument = get_full_type_name(instrument)
521 self._pipelineIR.instrument = instrument
523 def getInstrument(self) -> Optional[str]:
524 """Get the instrument from the pipeline.
526 Returns
527 -------
528 instrument : `str`, or None
529 The fully qualified name of a `lsst.obs.base.Instrument` subclass,
530 name, or None if the pipeline does not have an instrument.
531 """
532 return self._pipelineIR.instrument
534 def addTask(self, task: Union[Type[PipelineTask], str], label: str) -> None:
535 """Add a new task to the pipeline, or replace a task that is already
536 associated with the supplied label.
538 Parameters
539 ----------
540 task: `PipelineTask` or `str`
541 Either a derived class object of a `PipelineTask` or a string
542 corresponding to a fully qualified `PipelineTask` name.
543 label: `str`
544 A label that is used to identify the `PipelineTask` being added
545 """
546 if isinstance(task, str):
547 taskName = task
548 elif issubclass(task, PipelineTask):
549 taskName = get_full_type_name(task)
550 else:
551 raise ValueError(
552 "task must be either a child class of PipelineTask or a string containing"
553 " a fully qualified name to one"
554 )
555 if not label:
556 # in some cases (with command line-generated pipeline) tasks can
557 # be defined without label which is not acceptable, use task
558 # _DefaultName in that case
559 if isinstance(task, str):
560 task_class = doImportType(task)
561 label = task_class._DefaultName
562 self._pipelineIR.tasks[label] = pipelineIR.TaskIR(label, taskName)
564 def removeTask(self, label: str) -> None:
565 """Remove a task from the pipeline.
567 Parameters
568 ----------
569 label : `str`
570 The label used to identify the task that is to be removed
572 Raises
573 ------
574 KeyError
575 If no task with that label exists in the pipeline
577 """
578 self._pipelineIR.tasks.pop(label)
580 def addConfigOverride(self, label: str, key: str, value: object) -> None:
581 """Apply single config override.
583 Parameters
584 ----------
585 label : `str`
586 Label of the task.
587 key: `str`
588 Fully-qualified field name.
589 value : object
590 Value to be given to a field.
591 """
592 self._addConfigImpl(label, pipelineIR.ConfigIR(rest={key: value}))
594 def addConfigFile(self, label: str, filename: str) -> None:
595 """Add overrides from a specified file.
597 Parameters
598 ----------
599 label : `str`
600 The label used to identify the task associated with config to
601 modify
602 filename : `str`
603 Path to the override file.
604 """
605 self._addConfigImpl(label, pipelineIR.ConfigIR(file=[filename]))
607 def addConfigPython(self, label: str, pythonString: str) -> None:
608 """Add Overrides by running a snippet of python code against a config.
610 Parameters
611 ----------
612 label : `str`
613 The label used to identity the task associated with config to
614 modify.
615 pythonString: `str`
616 A string which is valid python code to be executed. This is done
617 with config as the only local accessible value.
618 """
619 self._addConfigImpl(label, pipelineIR.ConfigIR(python=pythonString))
621 def _addConfigImpl(self, label: str, newConfig: pipelineIR.ConfigIR) -> None:
622 if label == "parameters":
623 if newConfig.rest.keys() - self._pipelineIR.parameters.mapping.keys():
624 raise ValueError("Cannot override parameters that are not defined in pipeline")
625 self._pipelineIR.parameters.mapping.update(newConfig.rest)
626 if newConfig.file:
627 raise ValueError("Setting parameters section with config file is not supported")
628 if newConfig.python:
629 raise ValueError("Setting parameters section using python block in unsupported")
630 return
631 if label not in self._pipelineIR.tasks:
632 raise LookupError(f"There are no tasks labeled '{label}' in the pipeline")
633 self._pipelineIR.tasks[label].add_or_update_config(newConfig)
635 def write_to_uri(self, uri: ResourcePathExpression) -> None:
636 """Write the pipeline to a file or directory.
638 Parameters
639 ----------
640 uri : convertible to `ResourcePath`
641 URI to write to; may have any scheme with `ResourcePath` write
642 support or no scheme for a local file/directory. Should have a
643 ``.yaml``.
644 """
645 self._pipelineIR.write_to_uri(uri)
647 def toExpandedPipeline(self) -> Generator[TaskDef, None, None]:
648 """Returns a generator of TaskDefs which can be used to create quantum
649 graphs.
651 Returns
652 -------
653 generator : generator of `TaskDef`
654 The generator returned will be the sorted iterator of tasks which
655 are to be used in constructing a quantum graph.
657 Raises
658 ------
659 NotImplementedError
660 If a dataId is supplied in a config block. This is in place for
661 future use
662 """
663 taskDefs = []
664 for label in self._pipelineIR.tasks:
665 taskDefs.append(self._buildTaskDef(label))
667 # lets evaluate the contracts
668 if self._pipelineIR.contracts is not None:
669 label_to_config = {x.label: x.config for x in taskDefs}
670 for contract in self._pipelineIR.contracts:
671 # execute this in its own line so it can raise a good error
672 # message if there was problems with the eval
673 success = eval(contract.contract, None, label_to_config)
674 if not success:
675 extra_info = f": {contract.msg}" if contract.msg is not None else ""
676 raise pipelineIR.ContractError(
677 f"Contract(s) '{contract.contract}' were not satisfied{extra_info}"
678 )
680 taskDefs = sorted(taskDefs, key=lambda x: x.label)
681 yield from pipeTools.orderPipeline(taskDefs)
683 def _buildTaskDef(self, label: str) -> TaskDef:
684 if (taskIR := self._pipelineIR.tasks.get(label)) is None:
685 raise NameError(f"Label {label} does not appear in this pipeline")
686 taskClass: Type[PipelineTask] = doImportType(taskIR.klass)
687 taskName = get_full_type_name(taskClass)
688 config = taskClass.ConfigClass()
689 overrides = ConfigOverrides()
690 if self._pipelineIR.instrument is not None:
691 overrides.addInstrumentOverride(self._pipelineIR.instrument, taskClass._DefaultName)
692 if taskIR.config is not None:
693 for configIR in (configIr.formatted(self._pipelineIR.parameters) for configIr in taskIR.config):
694 if configIR.dataId is not None:
695 raise NotImplementedError(
696 "Specializing a config on a partial data id is not yet "
697 "supported in Pipeline definition"
698 )
699 # only apply override if it applies to everything
700 if configIR.dataId is None:
701 if configIR.file:
702 for configFile in configIR.file:
703 overrides.addFileOverride(os.path.expandvars(configFile))
704 if configIR.python is not None:
705 overrides.addPythonOverride(configIR.python)
706 for key, value in configIR.rest.items():
707 overrides.addValueOverride(key, value)
708 overrides.applyTo(config)
709 return TaskDef(taskName=taskName, config=config, taskClass=taskClass, label=label)
711 def __iter__(self) -> Generator[TaskDef, None, None]:
712 return self.toExpandedPipeline()
714 def __getitem__(self, item: str) -> TaskDef:
715 return self._buildTaskDef(item)
717 def __len__(self) -> int:
718 return len(self._pipelineIR.tasks)
720 def __eq__(self, other: object) -> bool:
721 if not isinstance(other, Pipeline):
722 return False
723 elif self._pipelineIR == other._pipelineIR:
724 # Shortcut: if the IR is the same, the expanded pipeline must be
725 # the same as well. But the converse is not true.
726 return True
727 else:
728 self_expanded = {td.label: (td.taskClass,) for td in self}
729 other_expanded = {td.label: (td.taskClass,) for td in other}
730 if self_expanded != other_expanded:
731 return False
732 # After DM-27847, we should compare configuration here, or better,
733 # delegated to TaskDef.__eq__ after making that compare configurations.
734 raise NotImplementedError(
735 "Pipelines cannot be compared because config instances cannot be compared; see DM-27847."
736 )
739@dataclass(frozen=True)
740class TaskDatasetTypes:
741 """An immutable struct that extracts and classifies the dataset types used
742 by a `PipelineTask`
743 """
745 initInputs: NamedValueSet[DatasetType]
746 """Dataset types that are needed as inputs in order to construct this Task.
748 Task-level `initInputs` may be classified as either
749 `~PipelineDatasetTypes.initInputs` or
750 `~PipelineDatasetTypes.initIntermediates` at the Pipeline level.
751 """
753 initOutputs: NamedValueSet[DatasetType]
754 """Dataset types that may be written after constructing this Task.
756 Task-level `initOutputs` may be classified as either
757 `~PipelineDatasetTypes.initOutputs` or
758 `~PipelineDatasetTypes.initIntermediates` at the Pipeline level.
759 """
761 inputs: NamedValueSet[DatasetType]
762 """Dataset types that are regular inputs to this Task.
764 If an input dataset needed for a Quantum cannot be found in the input
765 collection(s) or produced by another Task in the Pipeline, that Quantum
766 (and all dependent Quanta) will not be produced.
768 Task-level `inputs` may be classified as either
769 `~PipelineDatasetTypes.inputs` or `~PipelineDatasetTypes.intermediates`
770 at the Pipeline level.
771 """
773 prerequisites: NamedValueSet[DatasetType]
774 """Dataset types that are prerequisite inputs to this Task.
776 Prerequisite inputs must exist in the input collection(s) before the
777 pipeline is run, but do not constrain the graph - if a prerequisite is
778 missing for a Quantum, `PrerequisiteMissingError` is raised.
780 Prerequisite inputs are not resolved until the second stage of
781 QuantumGraph generation.
782 """
784 outputs: NamedValueSet[DatasetType]
785 """Dataset types that are produced by this Task.
787 Task-level `outputs` may be classified as either
788 `~PipelineDatasetTypes.outputs` or `~PipelineDatasetTypes.intermediates`
789 at the Pipeline level.
790 """
792 @classmethod
793 def fromTaskDef(
794 cls,
795 taskDef: TaskDef,
796 *,
797 registry: Registry,
798 include_configs: bool = True,
799 storage_class_mapping: Optional[Mapping[str, str]] = None,
800 ) -> TaskDatasetTypes:
801 """Extract and classify the dataset types from a single `PipelineTask`.
803 Parameters
804 ----------
805 taskDef: `TaskDef`
806 An instance of a `TaskDef` class for a particular `PipelineTask`.
807 registry: `Registry`
808 Registry used to construct normalized `DatasetType` objects and
809 retrieve those that are incomplete.
810 include_configs : `bool`, optional
811 If `True` (default) include config dataset types as
812 ``initOutputs``.
813 storage_class_mapping : `Mapping` of `str` to `StorageClass`, optional
814 If a taskdef contains a component dataset type that is unknown
815 to the registry, its parent StorageClass will be looked up in this
816 mapping if it is supplied. If the mapping does not contain the
817 composite dataset type, or the mapping is not supplied an exception
818 will be raised.
820 Returns
821 -------
822 types: `TaskDatasetTypes`
823 The dataset types used by this task.
825 Raises
826 ------
827 ValueError
828 Raised if dataset type connection definition differs from
829 registry definition.
830 LookupError
831 Raised if component parent StorageClass could not be determined
832 and storage_class_mapping does not contain the composite type, or
833 is set to None.
834 """
836 def makeDatasetTypesSet(
837 connectionType: str,
838 is_input: bool,
839 freeze: bool = True,
840 ) -> NamedValueSet[DatasetType]:
841 """Constructs a set of true `DatasetType` objects
843 Parameters
844 ----------
845 connectionType : `str`
846 Name of the connection type to produce a set for, corresponds
847 to an attribute of type `list` on the connection class instance
848 is_input : `bool`
849 These are input dataset types, else they are output dataset
850 types.
851 freeze : `bool`, optional
852 If `True`, call `NamedValueSet.freeze` on the object returned.
854 Returns
855 -------
856 datasetTypes : `NamedValueSet`
857 A set of all datasetTypes which correspond to the input
858 connection type specified in the connection class of this
859 `PipelineTask`
861 Raises
862 ------
863 ValueError
864 Raised if dataset type connection definition differs from
865 registry definition.
866 LookupError
867 Raised if component parent StorageClass could not be determined
868 and storage_class_mapping does not contain the composite type,
869 or is set to None.
871 Notes
872 -----
873 This function is a closure over the variables ``registry`` and
874 ``taskDef``, and ``storage_class_mapping``.
875 """
876 datasetTypes = NamedValueSet[DatasetType]()
877 for c in iterConnections(taskDef.connections, connectionType):
878 dimensions = set(getattr(c, "dimensions", set()))
879 if "skypix" in dimensions:
880 try:
881 datasetType = registry.getDatasetType(c.name)
882 except LookupError as err:
883 raise LookupError(
884 f"DatasetType '{c.name}' referenced by "
885 f"{type(taskDef.connections).__name__} uses 'skypix' as a dimension "
886 f"placeholder, but does not already exist in the registry. "
887 f"Note that reference catalog names are now used as the dataset "
888 f"type name instead of 'ref_cat'."
889 ) from err
890 rest1 = set(registry.dimensions.extract(dimensions - set(["skypix"])).names)
891 rest2 = set(
892 dim.name for dim in datasetType.dimensions if not isinstance(dim, SkyPixDimension)
893 )
894 if rest1 != rest2:
895 raise ValueError(
896 f"Non-skypix dimensions for dataset type {c.name} declared in "
897 f"connections ({rest1}) are inconsistent with those in "
898 f"registry's version of this dataset ({rest2})."
899 )
900 else:
901 # Component dataset types are not explicitly in the
902 # registry. This complicates consistency checks with
903 # registry and requires we work out the composite storage
904 # class.
905 registryDatasetType = None
906 try:
907 registryDatasetType = registry.getDatasetType(c.name)
908 except KeyError:
909 compositeName, componentName = DatasetType.splitDatasetTypeName(c.name)
910 if componentName:
911 if storage_class_mapping is None or compositeName not in storage_class_mapping:
912 raise LookupError(
913 "Component parent class cannot be determined, and "
914 "composite name was not in storage class mapping, or no "
915 "storage_class_mapping was supplied"
916 )
917 else:
918 parentStorageClass = storage_class_mapping[compositeName]
919 else:
920 parentStorageClass = None
921 datasetType = c.makeDatasetType(
922 registry.dimensions, parentStorageClass=parentStorageClass
923 )
924 registryDatasetType = datasetType
925 else:
926 datasetType = c.makeDatasetType(
927 registry.dimensions, parentStorageClass=registryDatasetType.parentStorageClass
928 )
930 if registryDatasetType and datasetType != registryDatasetType:
931 # The dataset types differ but first check to see if
932 # they are compatible before raising.
933 if is_input:
934 # This DatasetType must be compatible on get.
935 is_compatible = datasetType.is_compatible_with(registryDatasetType)
936 else:
937 # Has to be able to be converted to expect type
938 # on put.
939 is_compatible = registryDatasetType.is_compatible_with(datasetType)
940 if is_compatible:
941 # For inputs we want the pipeline to use the
942 # pipeline definition, for outputs it should use
943 # the registry definition.
944 if not is_input:
945 datasetType = registryDatasetType
946 _LOG.debug(
947 "Dataset types differ (task %s != registry %s) but are compatible"
948 " for %s in %s.",
949 datasetType,
950 registryDatasetType,
951 "input" if is_input else "output",
952 taskDef.label,
953 )
954 else:
955 try:
956 # Explicitly check for storage class just to
957 # make more specific message.
958 _ = datasetType.storageClass
959 except KeyError:
960 raise ValueError(
961 "Storage class does not exist for supplied dataset type "
962 f"{datasetType} for {taskDef.label}."
963 ) from None
964 raise ValueError(
965 f"Supplied dataset type ({datasetType}) inconsistent with "
966 f"registry definition ({registryDatasetType}) "
967 f"for {taskDef.label}."
968 )
969 datasetTypes.add(datasetType)
970 if freeze:
971 datasetTypes.freeze()
972 return datasetTypes
974 # optionally add initOutput dataset for config
975 initOutputs = makeDatasetTypesSet("initOutputs", is_input=False, freeze=False)
976 if include_configs:
977 initOutputs.add(
978 DatasetType(
979 taskDef.configDatasetName,
980 registry.dimensions.empty,
981 storageClass="Config",
982 )
983 )
984 initOutputs.freeze()
986 # optionally add output dataset for metadata
987 outputs = makeDatasetTypesSet("outputs", is_input=False, freeze=False)
988 if taskDef.metadataDatasetName is not None:
989 # Metadata is supposed to be of the TaskMetadata type, its
990 # dimensions correspond to a task quantum.
991 dimensions = registry.dimensions.extract(taskDef.connections.dimensions)
993 # Allow the storage class definition to be read from the existing
994 # dataset type definition if present.
995 try:
996 current = registry.getDatasetType(taskDef.metadataDatasetName)
997 except KeyError:
998 # No previous definition so use the default.
999 storageClass = "TaskMetadata" if _TASK_METADATA_TYPE is TaskMetadata else "PropertySet"
1000 else:
1001 storageClass = current.storageClass.name
1003 outputs.update({DatasetType(taskDef.metadataDatasetName, dimensions, storageClass)})
1004 if taskDef.logOutputDatasetName is not None:
1005 # Log output dimensions correspond to a task quantum.
1006 dimensions = registry.dimensions.extract(taskDef.connections.dimensions)
1007 outputs.update({DatasetType(taskDef.logOutputDatasetName, dimensions, "ButlerLogRecords")})
1009 outputs.freeze()
1011 return cls(
1012 initInputs=makeDatasetTypesSet("initInputs", is_input=True),
1013 initOutputs=initOutputs,
1014 inputs=makeDatasetTypesSet("inputs", is_input=True),
1015 prerequisites=makeDatasetTypesSet("prerequisiteInputs", is_input=True),
1016 outputs=outputs,
1017 )
1020@dataclass(frozen=True)
1021class PipelineDatasetTypes:
1022 """An immutable struct that classifies the dataset types used in a
1023 `Pipeline`.
1024 """
1026 packagesDatasetName: ClassVar[str] = "packages"
1027 """Name of a dataset type used to save package versions.
1028 """
1030 initInputs: NamedValueSet[DatasetType]
1031 """Dataset types that are needed as inputs in order to construct the Tasks
1032 in this Pipeline.
1034 This does not include dataset types that are produced when constructing
1035 other Tasks in the Pipeline (these are classified as `initIntermediates`).
1036 """
1038 initOutputs: NamedValueSet[DatasetType]
1039 """Dataset types that may be written after constructing the Tasks in this
1040 Pipeline.
1042 This does not include dataset types that are also used as inputs when
1043 constructing other Tasks in the Pipeline (these are classified as
1044 `initIntermediates`).
1045 """
1047 initIntermediates: NamedValueSet[DatasetType]
1048 """Dataset types that are both used when constructing one or more Tasks
1049 in the Pipeline and produced as a side-effect of constructing another
1050 Task in the Pipeline.
1051 """
1053 inputs: NamedValueSet[DatasetType]
1054 """Dataset types that are regular inputs for the full pipeline.
1056 If an input dataset needed for a Quantum cannot be found in the input
1057 collection(s), that Quantum (and all dependent Quanta) will not be
1058 produced.
1059 """
1061 prerequisites: NamedValueSet[DatasetType]
1062 """Dataset types that are prerequisite inputs for the full Pipeline.
1064 Prerequisite inputs must exist in the input collection(s) before the
1065 pipeline is run, but do not constrain the graph - if a prerequisite is
1066 missing for a Quantum, `PrerequisiteMissingError` is raised.
1068 Prerequisite inputs are not resolved until the second stage of
1069 QuantumGraph generation.
1070 """
1072 intermediates: NamedValueSet[DatasetType]
1073 """Dataset types that are output by one Task in the Pipeline and consumed
1074 as inputs by one or more other Tasks in the Pipeline.
1075 """
1077 outputs: NamedValueSet[DatasetType]
1078 """Dataset types that are output by a Task in the Pipeline and not consumed
1079 by any other Task in the Pipeline.
1080 """
1082 byTask: Mapping[str, TaskDatasetTypes]
1083 """Per-Task dataset types, keyed by label in the `Pipeline`.
1085 This is guaranteed to be zip-iterable with the `Pipeline` itself (assuming
1086 neither has been modified since the dataset types were extracted, of
1087 course).
1088 """
1090 @classmethod
1091 def fromPipeline(
1092 cls,
1093 pipeline: Union[Pipeline, Iterable[TaskDef]],
1094 *,
1095 registry: Registry,
1096 include_configs: bool = True,
1097 include_packages: bool = True,
1098 ) -> PipelineDatasetTypes:
1099 """Extract and classify the dataset types from all tasks in a
1100 `Pipeline`.
1102 Parameters
1103 ----------
1104 pipeline: `Pipeline` or `Iterable` [ `TaskDef` ]
1105 A collection of tasks that can be run together.
1106 registry: `Registry`
1107 Registry used to construct normalized `DatasetType` objects and
1108 retrieve those that are incomplete.
1109 include_configs : `bool`, optional
1110 If `True` (default) include config dataset types as
1111 ``initOutputs``.
1112 include_packages : `bool`, optional
1113 If `True` (default) include the dataset type for software package
1114 versions in ``initOutputs``.
1116 Returns
1117 -------
1118 types: `PipelineDatasetTypes`
1119 The dataset types used by this `Pipeline`.
1121 Raises
1122 ------
1123 ValueError
1124 Raised if Tasks are inconsistent about which datasets are marked
1125 prerequisite. This indicates that the Tasks cannot be run as part
1126 of the same `Pipeline`.
1127 """
1128 allInputs = NamedValueSet[DatasetType]()
1129 allOutputs = NamedValueSet[DatasetType]()
1130 allInitInputs = NamedValueSet[DatasetType]()
1131 allInitOutputs = NamedValueSet[DatasetType]()
1132 prerequisites = NamedValueSet[DatasetType]()
1133 byTask = dict()
1134 if include_packages:
1135 allInitOutputs.add(
1136 DatasetType(
1137 cls.packagesDatasetName,
1138 registry.dimensions.empty,
1139 storageClass="Packages",
1140 )
1141 )
1142 # create a list of TaskDefs in case the input is a generator
1143 pipeline = list(pipeline)
1145 # collect all the output dataset types
1146 typeStorageclassMap: Dict[str, str] = {}
1147 for taskDef in pipeline:
1148 for outConnection in iterConnections(taskDef.connections, "outputs"):
1149 typeStorageclassMap[outConnection.name] = outConnection.storageClass
1151 for taskDef in pipeline:
1152 thisTask = TaskDatasetTypes.fromTaskDef(
1153 taskDef,
1154 registry=registry,
1155 include_configs=include_configs,
1156 storage_class_mapping=typeStorageclassMap,
1157 )
1158 allInitInputs.update(thisTask.initInputs)
1159 allInitOutputs.update(thisTask.initOutputs)
1160 allInputs.update(thisTask.inputs)
1161 prerequisites.update(thisTask.prerequisites)
1162 allOutputs.update(thisTask.outputs)
1163 byTask[taskDef.label] = thisTask
1164 if not prerequisites.isdisjoint(allInputs):
1165 raise ValueError(
1166 "{} marked as both prerequisites and regular inputs".format(
1167 {dt.name for dt in allInputs & prerequisites}
1168 )
1169 )
1170 if not prerequisites.isdisjoint(allOutputs):
1171 raise ValueError(
1172 "{} marked as both prerequisites and outputs".format(
1173 {dt.name for dt in allOutputs & prerequisites}
1174 )
1175 )
1176 # Make sure that components which are marked as inputs get treated as
1177 # intermediates if there is an output which produces the composite
1178 # containing the component
1179 intermediateComponents = NamedValueSet[DatasetType]()
1180 intermediateComposites = NamedValueSet[DatasetType]()
1181 outputNameMapping = {dsType.name: dsType for dsType in allOutputs}
1182 for dsType in allInputs:
1183 # get the name of a possible component
1184 name, component = dsType.nameAndComponent()
1185 # if there is a component name, that means this is a component
1186 # DatasetType, if there is an output which produces the parent of
1187 # this component, treat this input as an intermediate
1188 if component is not None:
1189 # This needs to be in this if block, because someone might have
1190 # a composite that is a pure input from existing data
1191 if name in outputNameMapping:
1192 intermediateComponents.add(dsType)
1193 intermediateComposites.add(outputNameMapping[name])
1195 def checkConsistency(a: NamedValueSet, b: NamedValueSet) -> None:
1196 common = a.names & b.names
1197 for name in common:
1198 # Any compatibility is allowed. This function does not know
1199 # if a dataset type is to be used for input or output.
1200 if not (a[name].is_compatible_with(b[name]) or b[name].is_compatible_with(a[name])):
1201 raise ValueError(f"Conflicting definitions for dataset type: {a[name]} != {b[name]}.")
1203 checkConsistency(allInitInputs, allInitOutputs)
1204 checkConsistency(allInputs, allOutputs)
1205 checkConsistency(allInputs, intermediateComposites)
1206 checkConsistency(allOutputs, intermediateComposites)
1208 def frozen(s: AbstractSet[DatasetType]) -> NamedValueSet[DatasetType]:
1209 assert isinstance(s, NamedValueSet)
1210 s.freeze()
1211 return s
1213 return cls(
1214 initInputs=frozen(allInitInputs - allInitOutputs),
1215 initIntermediates=frozen(allInitInputs & allInitOutputs),
1216 initOutputs=frozen(allInitOutputs - allInitInputs),
1217 inputs=frozen(allInputs - allOutputs - intermediateComponents),
1218 # If there are storage class differences in inputs and outputs
1219 # the intermediates have to choose priority. Here choose that
1220 # inputs to tasks much match the requested storage class by
1221 # applying the inputs over the top of the outputs.
1222 intermediates=frozen(allOutputs & allInputs | intermediateComponents),
1223 outputs=frozen(allOutputs - allInputs - intermediateComposites),
1224 prerequisites=frozen(prerequisites),
1225 byTask=MappingProxyType(byTask), # MappingProxyType -> frozen view of dict for immutability
1226 )
1228 @classmethod
1229 def initOutputNames(
1230 cls,
1231 pipeline: Union[Pipeline, Iterable[TaskDef]],
1232 *,
1233 include_configs: bool = True,
1234 include_packages: bool = True,
1235 ) -> Iterator[str]:
1236 """Return the names of dataset types ot task initOutputs, Configs,
1237 and package versions for a pipeline.
1239 Parameters
1240 ----------
1241 pipeline: `Pipeline` or `Iterable` [ `TaskDef` ]
1242 A `Pipeline` instance or collection of `TaskDef` instances.
1243 include_configs : `bool`, optional
1244 If `True` (default) include config dataset types.
1245 include_packages : `bool`, optional
1246 If `True` (default) include the dataset type for package versions.
1248 Yields
1249 ------
1250 datasetTypeName : `str`
1251 Name of the dataset type.
1252 """
1253 if include_packages:
1254 # Package versions dataset type
1255 yield cls.packagesDatasetName
1257 if isinstance(pipeline, Pipeline):
1258 pipeline = pipeline.toExpandedPipeline()
1260 for taskDef in pipeline:
1262 # all task InitOutputs
1263 for name in taskDef.connections.initOutputs:
1264 attribute = getattr(taskDef.connections, name)
1265 yield attribute.name
1267 # config dataset name
1268 if include_configs:
1269 yield taskDef.configDatasetName