Coverage for python/lsst/daf/butler/core/datasets/type.py: 21%
217 statements
« prev ^ index » next coverage.py v6.4.1, created at 2022-06-09 09:43 +0000
« prev ^ index » next coverage.py v6.4.1, created at 2022-06-09 09:43 +0000
1# This file is part of daf_butler.
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/>.
22from __future__ import annotations
24__all__ = ["DatasetType", "SerializedDatasetType"]
26import re
27from copy import deepcopy
28from types import MappingProxyType
29from typing import TYPE_CHECKING, Any, Callable, Dict, Iterable, List, Mapping, Optional, Tuple, Type, Union
31from pydantic import BaseModel, StrictBool, StrictStr
33from ..configSupport import LookupKey
34from ..dimensions import DimensionGraph, SerializedDimensionGraph
35from ..json import from_json_pydantic, to_json_pydantic
36from ..storageClass import StorageClass, StorageClassFactory
38if TYPE_CHECKING: 38 ↛ 39line 38 didn't jump to line 39, because the condition on line 38 was never true
39 from ...registry import Registry
40 from ..dimensions import Dimension, DimensionUniverse
43def _safeMakeMappingProxyType(data: Optional[Mapping]) -> Mapping:
44 if data is None:
45 data = {}
46 return MappingProxyType(data)
49class SerializedDatasetType(BaseModel):
50 """Simplified model of a `DatasetType` suitable for serialization."""
52 name: StrictStr
53 storageClass: Optional[StrictStr] = None
54 dimensions: Optional[SerializedDimensionGraph] = None
55 parentStorageClass: Optional[StrictStr] = None
56 isCalibration: StrictBool = False
58 @classmethod
59 def direct(
60 cls,
61 *,
62 name: str,
63 storageClass: Optional[str] = None,
64 dimensions: Optional[Dict] = None,
65 parentStorageClass: Optional[str] = None,
66 isCalibration: bool = False,
67 ) -> SerializedDatasetType:
68 """Construct a `SerializedDatasetType` directly without validators.
70 This differs from PyDantics construct method in that the arguments are
71 explicitly what the model requires, and it will recurse through
72 members, constructing them from their corresponding `direct` methods.
74 This method should only be called when the inputs are trusted.
75 """
76 node = SerializedDatasetType.__new__(cls)
77 setter = object.__setattr__
78 setter(node, "name", name)
79 setter(node, "storageClass", storageClass)
80 setter(
81 node,
82 "dimensions",
83 dimensions if dimensions is None else SerializedDimensionGraph.direct(**dimensions),
84 )
85 setter(node, "parentStorageClass", parentStorageClass)
86 setter(node, "isCalibration", isCalibration)
87 setter(
88 node,
89 "__fields_set__",
90 {"name", "storageClass", "dimensions", "parentStorageClass", "isCalibration"},
91 )
92 return node
95class DatasetType:
96 r"""A named category of Datasets.
98 Defines how they are organized, related, and stored.
100 A concrete, final class whose instances represent `DatasetType`\ s.
101 `DatasetType` instances may be constructed without a `Registry`,
102 but they must be registered
103 via `Registry.registerDatasetType()` before corresponding Datasets
104 may be added.
105 `DatasetType` instances are immutable.
107 Parameters
108 ----------
109 name : `str`
110 A string name for the Dataset; must correspond to the same
111 `DatasetType` across all Registries. Names must start with an
112 upper or lowercase letter, and may contain only letters, numbers,
113 and underscores. Component dataset types should contain a single
114 period separating the base dataset type name from the component name
115 (and may be recursive).
116 dimensions : `DimensionGraph` or iterable of `Dimension` or `str`
117 Dimensions used to label and relate instances of this `DatasetType`.
118 If not a `DimensionGraph`, ``universe`` must be provided as well.
119 storageClass : `StorageClass` or `str`
120 Instance of a `StorageClass` or name of `StorageClass` that defines
121 how this `DatasetType` is persisted.
122 parentStorageClass : `StorageClass` or `str`, optional
123 Instance of a `StorageClass` or name of `StorageClass` that defines
124 how the composite parent is persisted. Must be `None` if this
125 is not a component.
126 universe : `DimensionUniverse`, optional
127 Set of all known dimensions, used to normalize ``dimensions`` if it
128 is not already a `DimensionGraph`.
129 isCalibration : `bool`, optional
130 If `True`, this dataset type may be included in
131 `~CollectionType.CALIBRATION` collections.
133 See Also
134 --------
135 :ref:`daf_butler_organizing_datasets`
136 """
138 __slots__ = (
139 "_name",
140 "_dimensions",
141 "_storageClass",
142 "_storageClassName",
143 "_parentStorageClass",
144 "_parentStorageClassName",
145 "_isCalibration",
146 )
148 _serializedType = SerializedDatasetType
150 VALID_NAME_REGEX = re.compile("^[a-zA-Z_][a-zA-Z0-9_]*(\\.[a-zA-Z_][a-zA-Z0-9_]*)*$")
152 @staticmethod
153 def nameWithComponent(datasetTypeName: str, componentName: str) -> str:
154 """Form a valid DatasetTypeName from a parent and component.
156 No validation is performed.
158 Parameters
159 ----------
160 datasetTypeName : `str`
161 Base type name.
162 componentName : `str`
163 Name of component.
165 Returns
166 -------
167 compTypeName : `str`
168 Name to use for component DatasetType.
169 """
170 return "{}.{}".format(datasetTypeName, componentName)
172 def __init__(
173 self,
174 name: str,
175 dimensions: Union[DimensionGraph, Iterable[Union[Dimension, str]]],
176 storageClass: Union[StorageClass, str],
177 parentStorageClass: Optional[Union[StorageClass, str]] = None,
178 *,
179 universe: Optional[DimensionUniverse] = None,
180 isCalibration: bool = False,
181 ):
182 if self.VALID_NAME_REGEX.match(name) is None:
183 raise ValueError(f"DatasetType name '{name}' is invalid.")
184 self._name = name
185 if not isinstance(dimensions, DimensionGraph):
186 if universe is None:
187 raise ValueError(
188 "If dimensions is not a normalized DimensionGraph, a universe must be provided."
189 )
190 dimensions = universe.extract(dimensions)
191 self._dimensions = dimensions
192 if name in self._dimensions.universe.getGovernorDimensions().names:
193 raise ValueError(f"Governor dimension name {name} cannot be used as a dataset type name.")
194 if not isinstance(storageClass, (StorageClass, str)):
195 raise ValueError(f"StorageClass argument must be StorageClass or str. Got {storageClass}")
196 self._storageClass: Optional[StorageClass]
197 if isinstance(storageClass, StorageClass):
198 self._storageClass = storageClass
199 self._storageClassName = storageClass.name
200 else:
201 self._storageClass = None
202 self._storageClassName = storageClass
204 self._parentStorageClass: Optional[StorageClass] = None
205 self._parentStorageClassName: Optional[str] = None
206 if parentStorageClass is not None:
207 if not isinstance(storageClass, (StorageClass, str)):
208 raise ValueError(
209 f"Parent StorageClass argument must be StorageClass or str. Got {parentStorageClass}"
210 )
212 # Only allowed for a component dataset type
213 _, componentName = self.splitDatasetTypeName(self._name)
214 if componentName is None:
215 raise ValueError(
216 f"Can not specify a parent storage class if this is not a component ({self._name})"
217 )
218 if isinstance(parentStorageClass, StorageClass):
219 self._parentStorageClass = parentStorageClass
220 self._parentStorageClassName = parentStorageClass.name
221 else:
222 self._parentStorageClassName = parentStorageClass
224 # Ensure that parent storage class is specified when we have
225 # a component and is not specified when we don't
226 _, componentName = self.splitDatasetTypeName(self._name)
227 if parentStorageClass is None and componentName is not None:
228 raise ValueError(
229 f"Component dataset type '{self._name}' constructed without parent storage class"
230 )
231 if parentStorageClass is not None and componentName is None:
232 raise ValueError(f"Parent storage class specified by {self._name} is not a composite")
233 self._isCalibration = isCalibration
235 def __repr__(self) -> str:
236 extra = ""
237 if self._parentStorageClassName:
238 extra = f", parentStorageClass={self._parentStorageClassName}"
239 if self._isCalibration:
240 extra += ", isCalibration=True"
241 return f"DatasetType({self.name!r}, {self.dimensions}, {self._storageClassName}{extra})"
243 def _equal_ignoring_storage_class(self, other: Any) -> bool:
244 """Check everything is equal except the storage class.
246 Parameters
247 ----------
248 other : Any
249 Object to check against this one.
251 Returns
252 -------
253 mostly : `bool`
254 Returns `True` if everything except the storage class is equal.
255 """
256 if not isinstance(other, type(self)):
257 return False
258 if self._name != other._name:
259 return False
260 if self._dimensions != other._dimensions:
261 return False
262 if self._isCalibration != other._isCalibration:
263 return False
264 if self._parentStorageClass is not None and other._parentStorageClass is not None:
265 return self._parentStorageClass == other._parentStorageClass
266 else:
267 return self._parentStorageClassName == other._parentStorageClassName
269 def __eq__(self, other: Any) -> bool:
270 mostly_equal = self._equal_ignoring_storage_class(other)
271 if not mostly_equal:
272 return False
274 # Be careful not to force a storage class to import the corresponding
275 # python code.
276 if self._storageClass is not None and other._storageClass is not None:
277 if self._storageClass != other._storageClass:
278 return False
279 else:
280 if self._storageClassName != other._storageClassName:
281 return False
282 return True
284 def is_compatible_with(self, other: DatasetType) -> bool:
285 """Determine if the given `DatasetType` is compatible with this one.
287 Compatibility requires a matching name and dimensions and a storage
288 class for this dataset type that can convert the python type associated
289 with the other storage class to this python type.
291 Parameters
292 ----------
293 other : `DatasetType`
294 Dataset type to check.
296 Returns
297 -------
298 is_compatible : `bool`
299 Returns `True` if the other dataset type is either the same as this
300 or the storage class associated with the other can be converted to
301 this.
302 """
303 mostly_equal = self._equal_ignoring_storage_class(other)
304 if not mostly_equal:
305 return False
307 # If the storage class names match then they are compatible.
308 if self._storageClassName == other._storageClassName:
309 return True
311 # Now required to check the full storage class.
312 self_sc = self.storageClass
313 other_sc = other.storageClass
315 return self_sc.can_convert(other_sc)
317 def __hash__(self) -> int:
318 """Hash DatasetType instance.
320 This only uses StorageClass name which is it consistent with the
321 implementation of StorageClass hash method.
322 """
323 return hash((self._name, self._dimensions, self._storageClassName, self._parentStorageClassName))
325 def __lt__(self, other: Any) -> bool:
326 """Sort using the dataset type name."""
327 if not isinstance(other, type(self)):
328 return NotImplemented
329 return self.name < other.name
331 @property
332 def name(self) -> str:
333 """Return a string name for the Dataset.
335 Must correspond to the same `DatasetType` across all Registries.
336 """
337 return self._name
339 @property
340 def dimensions(self) -> DimensionGraph:
341 r"""Return the `Dimension`\ s fir this dataset type.
343 The dimensions label and relate instances of this
344 `DatasetType` (`DimensionGraph`).
345 """
346 return self._dimensions
348 @property
349 def storageClass(self) -> StorageClass:
350 """Return `StorageClass` instance associated with this dataset type.
352 The `StorageClass` defines how this `DatasetType`
353 is persisted. Note that if DatasetType was constructed with a name
354 of a StorageClass then Butler has to be initialized before using
355 this property.
356 """
357 if self._storageClass is None:
358 self._storageClass = StorageClassFactory().getStorageClass(self._storageClassName)
359 return self._storageClass
361 @property
362 def parentStorageClass(self) -> Optional[StorageClass]:
363 """Return the storage class of the composite containing this component.
365 Note that if DatasetType was constructed with a name of a
366 StorageClass then Butler has to be initialized before using this
367 property. Can be `None` if this is not a component of a composite.
368 Must be defined if this is a component.
369 """
370 if self._parentStorageClass is None and self._parentStorageClassName is None:
371 return None
372 if self._parentStorageClass is None and self._parentStorageClassName is not None:
373 self._parentStorageClass = StorageClassFactory().getStorageClass(self._parentStorageClassName)
374 return self._parentStorageClass
376 def isCalibration(self) -> bool:
377 """Return if datasets of this type can be in calibration collections.
379 Returns
380 -------
381 flag : `bool`
382 `True` if datasets of this type may be included in calibration
383 collections.
384 """
385 return self._isCalibration
387 @staticmethod
388 def splitDatasetTypeName(datasetTypeName: str) -> Tuple[str, Optional[str]]:
389 """Return the root name and the component from a composite name.
391 Parameters
392 ----------
393 datasetTypeName : `str`
394 The name of the dataset type, can include a component using
395 a "."-separator.
397 Returns
398 -------
399 rootName : `str`
400 Root name without any components.
401 componentName : `str`
402 The component if it has been specified, else `None`.
404 Notes
405 -----
406 If the dataset type name is ``a.b.c`` this method will return a
407 root name of ``a`` and a component name of ``b.c``.
408 """
409 comp = None
410 root = datasetTypeName
411 if "." in root:
412 # If there is doubt, the component is after the first "."
413 root, comp = root.split(".", maxsplit=1)
414 return root, comp
416 def nameAndComponent(self) -> Tuple[str, Optional[str]]:
417 """Return the root name of this dataset type and any component.
419 Returns
420 -------
421 rootName : `str`
422 Root name for this `DatasetType` without any components.
423 componentName : `str`
424 The component if it has been specified, else `None`.
425 """
426 return self.splitDatasetTypeName(self.name)
428 def component(self) -> Optional[str]:
429 """Return the component name (if defined).
431 Returns
432 -------
433 comp : `str`
434 Name of component part of DatasetType name. `None` if this
435 `DatasetType` is not associated with a component.
436 """
437 _, comp = self.nameAndComponent()
438 return comp
440 def componentTypeName(self, component: str) -> str:
441 """Derive a component dataset type from a composite.
443 Parameters
444 ----------
445 component : `str`
446 Name of component
448 Returns
449 -------
450 derived : `str`
451 Compound name of this `DatasetType` and the component.
453 Raises
454 ------
455 KeyError
456 Requested component is not supported by this `DatasetType`.
457 """
458 if component in self.storageClass.allComponents():
459 return self.nameWithComponent(self.name, component)
460 raise KeyError(f"Requested component ({component}) not understood by this DatasetType ({self})")
462 def makeCompositeDatasetType(self) -> DatasetType:
463 """Return a composite dataset type from the component.
465 Returns
466 -------
467 composite : `DatasetType`
468 The composite dataset type.
470 Raises
471 ------
472 RuntimeError
473 Raised if this dataset type is not a component dataset type.
474 """
475 if not self.isComponent():
476 raise RuntimeError(f"DatasetType {self.name} must be a component to form the composite")
477 composite_name, _ = self.nameAndComponent()
478 if self.parentStorageClass is None:
479 raise ValueError(
480 f"Parent storage class is not set. Unable to create composite type from {self.name}"
481 )
482 return DatasetType(
483 composite_name,
484 dimensions=self.dimensions,
485 storageClass=self.parentStorageClass,
486 isCalibration=self.isCalibration(),
487 )
489 def makeComponentDatasetType(self, component: str) -> DatasetType:
490 """Return a component dataset type from a composite.
492 Assumes the same dimensions as the parent.
494 Parameters
495 ----------
496 component : `str`
497 Name of component
499 Returns
500 -------
501 datasetType : `DatasetType`
502 A new DatasetType instance.
503 """
504 # The component could be a read/write or read component
505 return DatasetType(
506 self.componentTypeName(component),
507 dimensions=self.dimensions,
508 storageClass=self.storageClass.allComponents()[component],
509 parentStorageClass=self.storageClass,
510 isCalibration=self.isCalibration(),
511 )
513 def makeAllComponentDatasetTypes(self) -> List[DatasetType]:
514 """Return all component dataset types for this composite.
516 Returns
517 -------
518 all : `list` of `DatasetType`
519 All the component dataset types. If this is not a composite
520 then returns an empty list.
521 """
522 return [
523 self.makeComponentDatasetType(componentName)
524 for componentName in self.storageClass.allComponents()
525 ]
527 def isComponent(self) -> bool:
528 """Return whether this `DatasetType` refers to a component.
530 Returns
531 -------
532 isComponent : `bool`
533 `True` if this `DatasetType` is a component, `False` otherwise.
534 """
535 if self.component():
536 return True
537 return False
539 def isComposite(self) -> bool:
540 """Return whether this `DatasetType` is a composite.
542 Returns
543 -------
544 isComposite : `bool`
545 `True` if this `DatasetType` is a composite type, `False`
546 otherwise.
547 """
548 return self.storageClass.isComposite()
550 def _lookupNames(self) -> Tuple[LookupKey, ...]:
551 """Return name keys to use for lookups in configurations.
553 The names are returned in order of priority.
555 Returns
556 -------
557 names : `tuple` of `LookupKey`
558 Tuple of the `DatasetType` name and the `StorageClass` name.
559 If the name includes a component the name with the component
560 is first, then the name without the component and finally
561 the storage class name and the storage class name of the
562 composite.
563 """
564 rootName, componentName = self.nameAndComponent()
565 lookups: Tuple[LookupKey, ...] = (LookupKey(name=self.name),)
566 if componentName is not None:
567 lookups = lookups + (LookupKey(name=rootName),)
569 if self.dimensions:
570 # Dimensions are a lower priority than dataset type name
571 lookups = lookups + (LookupKey(dimensions=self.dimensions),)
573 storageClasses = self.storageClass._lookupNames()
574 if componentName is not None and self.parentStorageClass is not None:
575 storageClasses += self.parentStorageClass._lookupNames()
577 return lookups + storageClasses
579 def to_simple(self, minimal: bool = False) -> SerializedDatasetType:
580 """Convert this class to a simple python type.
582 This makes it suitable for serialization.
584 Parameters
585 ----------
586 minimal : `bool`, optional
587 Use minimal serialization. Requires Registry to convert
588 back to a full type.
590 Returns
591 -------
592 simple : `SerializedDatasetType`
593 The object converted to a class suitable for serialization.
594 """
595 as_dict: Dict[str, Any]
596 if minimal:
597 # Only needs the name.
598 as_dict = {"name": self.name}
599 else:
600 # Convert to a dict form
601 as_dict = {
602 "name": self.name,
603 "storageClass": self._storageClassName,
604 "isCalibration": self._isCalibration,
605 "dimensions": self.dimensions.to_simple(),
606 }
608 if self._parentStorageClassName is not None:
609 as_dict["parentStorageClass"] = self._parentStorageClassName
610 return SerializedDatasetType(**as_dict)
612 @classmethod
613 def from_simple(
614 cls,
615 simple: SerializedDatasetType,
616 universe: Optional[DimensionUniverse] = None,
617 registry: Optional[Registry] = None,
618 ) -> DatasetType:
619 """Construct a new object from the simplified form.
621 This is usually data returned from the `to_simple` method.
623 Parameters
624 ----------
625 simple : `SerializedDatasetType`
626 The value returned by `to_simple()`.
627 universe : `DimensionUniverse`
628 The special graph of all known dimensions of which this graph will
629 be a subset. Can be `None` if a registry is provided.
630 registry : `lsst.daf.butler.Registry`, optional
631 Registry to use to convert simple name of a DatasetType to
632 a full `DatasetType`. Can be `None` if a full description of
633 the type is provided along with a universe.
635 Returns
636 -------
637 datasetType : `DatasetType`
638 Newly-constructed object.
639 """
640 if simple.storageClass is None:
641 # Treat this as minimalist representation
642 if registry is None:
643 raise ValueError(
644 f"Unable to convert a DatasetType name '{simple}' to DatasetType without a Registry"
645 )
646 return registry.getDatasetType(simple.name)
648 if universe is None and registry is None:
649 raise ValueError("One of universe or registry must be provided.")
651 if universe is None and registry is not None:
652 # registry should not be none by now but test helps mypy
653 universe = registry.dimensions
655 if universe is None:
656 # this is for mypy
657 raise ValueError("Unable to determine a usable universe")
659 if simple.dimensions is None:
660 # mypy hint
661 raise ValueError(f"Dimensions must be specified in {simple}")
663 return cls(
664 name=simple.name,
665 dimensions=DimensionGraph.from_simple(simple.dimensions, universe=universe),
666 storageClass=simple.storageClass,
667 isCalibration=simple.isCalibration,
668 parentStorageClass=simple.parentStorageClass,
669 universe=universe,
670 )
672 to_json = to_json_pydantic
673 from_json = classmethod(from_json_pydantic)
675 def __reduce__(
676 self,
677 ) -> Tuple[
678 Callable, Tuple[Type[DatasetType], Tuple[str, DimensionGraph, str, Optional[str]], Dict[str, bool]]
679 ]:
680 """Support pickling.
682 StorageClass instances can not normally be pickled, so we pickle
683 StorageClass name instead of instance.
684 """
685 return _unpickle_via_factory, (
686 self.__class__,
687 (self.name, self.dimensions, self._storageClassName, self._parentStorageClassName),
688 {"isCalibration": self._isCalibration},
689 )
691 def __deepcopy__(self, memo: Any) -> DatasetType:
692 """Support for deep copy method.
694 Normally ``deepcopy`` will use pickle mechanism to make copies.
695 We want to avoid that to support (possibly degenerate) use case when
696 DatasetType is constructed with StorageClass instance which is not
697 registered with StorageClassFactory (this happens in unit tests).
698 Instead we re-implement ``__deepcopy__`` method.
699 """
700 return DatasetType(
701 name=deepcopy(self.name, memo),
702 dimensions=deepcopy(self.dimensions, memo),
703 storageClass=deepcopy(self._storageClass or self._storageClassName, memo),
704 parentStorageClass=deepcopy(self._parentStorageClass or self._parentStorageClassName, memo),
705 isCalibration=deepcopy(self._isCalibration, memo),
706 )
709def _unpickle_via_factory(factory: Callable, args: Any, kwargs: Any) -> DatasetType:
710 """Unpickle something by calling a factory.
712 Allows subclasses to unpickle using `__reduce__` with keyword
713 arguments as well as positional arguments.
714 """
715 return factory(*args, **kwargs)