Coverage for python/lsst/daf/butler/core/dimensions/_coordinate.py : 27%

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# 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/>.
22#
23# Design notes for this module are in
24# doc/lsst.daf.butler/dev/dataCoordinate.py.
25#
27from __future__ import annotations
29__all__ = ("DataCoordinate", "DataId", "DataIdKey", "DataIdValue")
31from abc import abstractmethod
32import numbers
33from typing import (
34 AbstractSet,
35 Any,
36 Dict,
37 Iterator,
38 Mapping,
39 Optional,
40 Tuple,
41 TYPE_CHECKING,
42 Union,
43)
45from lsst.sphgeom import Region
46from ..named import NamedKeyMapping, NameLookupMapping, NamedValueAbstractSet
47from ..timespan import Timespan
48from ._elements import Dimension, DimensionElement
49from ._graph import DimensionGraph
50from ._records import DimensionRecord
52if TYPE_CHECKING: # Imports needed only for type annotations; may be circular. 52 ↛ 53line 52 didn't jump to line 53, because the condition on line 52 was never true
53 from ._universe import DimensionUniverse
55DataIdKey = Union[str, Dimension]
56"""Type annotation alias for the keys that can be used to index a
57DataCoordinate.
58"""
60DataIdValue = Union[str, int, None]
61"""Type annotation alias for the values that can be present in a
62DataCoordinate or other data ID.
63"""
66def _intersectRegions(*args: Region) -> Optional[Region]:
67 """Return the intersection of several regions.
69 For internal use by `ExpandedDataCoordinate` only.
71 If no regions are provided, returns `None`.
73 This is currently a placeholder; it actually returns `NotImplemented`
74 (it does *not* raise an exception) when multiple regions are given, which
75 propagates to `ExpandedDataCoordinate`. This reflects the fact that we
76 don't want to fail to construct an `ExpandedDataCoordinate` entirely when
77 we can't compute its region, and at present we don't have a high-level use
78 case for the regions of these particular data IDs.
79 """
80 if len(args) == 0:
81 return None
82 elif len(args) == 1:
83 return args[0]
84 else:
85 return NotImplemented
88class DataCoordinate(NamedKeyMapping[Dimension, DataIdValue]):
89 """An immutable data ID dictionary that guarantees that its key-value pairs
90 identify at least all required dimensions in a `DimensionGraph`.
92 `DataCoordinateSet` itself is an ABC, but provides `staticmethod` factory
93 functions for private concrete implementations that should be sufficient
94 for most purposes. `standardize` is the most flexible and safe of these;
95 the others (`makeEmpty`, `fromRequiredValues`, and `fromFullValues`) are
96 more specialized and perform little or no checking of inputs.
98 Notes
99 -----
100 Like any data ID class, `DataCoordinate` behaves like a dictionary, but
101 with some subtleties:
103 - Both `Dimension` instances and `str` names thereof may be used as keys
104 in lookup operations, but iteration (and `keys`) will yield `Dimension`
105 instances. The `names` property can be used to obtain the corresponding
106 `str` names.
108 - Lookups for implied dimensions (those in ``self.graph.implied``) are
109 supported if and only if `hasFull` returns `True`, and are never
110 included in iteration or `keys`. The `full` property may be used to
111 obtain a mapping whose keys do include implied dimensions.
113 - Equality comparison with other mappings is supported, but it always
114 considers only required dimensions (as well as requiring both operands
115 to identify the same dimensions). This is not quite consistent with the
116 way mappings usually work - normally differing keys imply unequal
117 mappings - but it makes sense in this context because data IDs with the
118 same values for required dimensions but different values for implied
119 dimensions represent a serious problem with the data that
120 `DataCoordinate` cannot generally recognize on its own, and a data ID
121 that knows implied dimension values should still be able to compare as
122 equal to one that does not. This is of course not the way comparisons
123 between simple `dict` data IDs work, and hence using a `DataCoordinate`
124 instance for at least one operand in any data ID comparison is strongly
125 recommended.
126 """
128 __slots__ = ()
130 @staticmethod
131 def standardize(
132 mapping: Optional[NameLookupMapping[Dimension, DataIdValue]] = None,
133 *,
134 graph: Optional[DimensionGraph] = None,
135 universe: Optional[DimensionUniverse] = None,
136 **kwargs: Any
137 ) -> DataCoordinate:
138 """Adapt an arbitrary mapping and/or additional arguments into a true
139 `DataCoordinate`, or augment an existing one.
141 Parameters
142 ----------
143 mapping : `~collections.abc.Mapping`, optional
144 An informal data ID that maps dimensions or dimension names to
145 their primary key values (may also be a true `DataCoordinate`).
146 graph : `DimensionGraph`
147 The dimensions to be identified by the new `DataCoordinate`.
148 If not provided, will be inferred from the keys of ``mapping``,
149 and ``universe`` must be provided unless ``mapping`` is already a
150 `DataCoordinate`.
151 universe : `DimensionUniverse`
152 All known dimensions and their relationships; used to expand
153 and validate dependencies when ``graph`` is not provided.
154 **kwargs
155 Additional keyword arguments are treated like additional key-value
156 pairs in ``mapping``.
158 Returns
159 -------
160 coordinate : `DataCoordinate`
161 A validated `DataCoordinate` instance.
163 Raises
164 ------
165 TypeError
166 Raised if the set of optional arguments provided is not supported.
167 KeyError
168 Raised if a key-value pair for a required dimension is missing.
169 """
170 d: Dict[str, DataIdValue] = {}
171 if isinstance(mapping, DataCoordinate):
172 if graph is None:
173 if not kwargs:
174 # Already standardized to exactly what we want.
175 return mapping
176 elif kwargs.keys().isdisjoint(graph.dimensions.names):
177 # User provided kwargs, but told us not to use them by
178 # passing in dimensions that are disjoint from those kwargs.
179 # This is not necessarily user error - it's a useful pattern
180 # to pass in all of the key-value pairs you have and let the
181 # code here pull out only what it needs.
182 return mapping.subset(graph)
183 assert universe is None or universe == mapping.universe
184 universe = mapping.universe
185 d.update((name, mapping[name]) for name in mapping.graph.required.names)
186 if mapping.hasFull():
187 d.update((name, mapping[name]) for name in mapping.graph.implied.names)
188 elif isinstance(mapping, NamedKeyMapping):
189 d.update(mapping.byName())
190 elif mapping is not None:
191 d.update(mapping)
192 d.update(kwargs)
193 if graph is None:
194 if universe is None:
195 raise TypeError("universe must be provided if graph is not.")
196 graph = DimensionGraph(universe, names=d.keys())
197 if not graph.dimensions:
198 return DataCoordinate.makeEmpty(graph.universe)
199 if d.keys() >= graph.dimensions.names:
200 values = tuple(d[name] for name in graph._dataCoordinateIndices.keys())
201 else:
202 try:
203 values = tuple(d[name] for name in graph.required.names)
204 except KeyError as err:
205 raise KeyError(f"No value in data ID ({mapping}) for required dimension {err}.") from err
206 # Some backends cannot handle numpy.int64 type which is a subclass of
207 # numbers.Integral; convert that to int.
208 values = tuple(int(val) if isinstance(val, numbers.Integral) # type: ignore
209 else val for val in values)
210 return _BasicTupleDataCoordinate(graph, values)
212 @staticmethod
213 def makeEmpty(universe: DimensionUniverse) -> DataCoordinate:
214 """Return an empty `DataCoordinate` that identifies the null set of
215 dimensions.
217 Parameters
218 ----------
219 universe : `DimensionUniverse`
220 Universe to which this null dimension set belongs.
222 Returns
223 -------
224 dataId : `DataCoordinate`
225 A data ID object that identifies no dimensions. `hasFull` and
226 `hasRecords` are guaranteed to return `True`, because both `full`
227 and `records` are just empty mappings.
228 """
229 return _ExpandedTupleDataCoordinate(universe.empty, (), {})
231 @staticmethod
232 def fromRequiredValues(graph: DimensionGraph, values: Tuple[DataIdValue, ...]) -> DataCoordinate:
233 """Construct a `DataCoordinate` from a tuple of dimension values that
234 identify only required dimensions.
236 This is a low-level interface with at most assertion-level checking of
237 inputs. Most callers should use `standardize` instead.
239 Parameters
240 ----------
241 graph : `DimensionGraph`
242 Dimensions this data ID will identify.
243 values : `tuple` [ `int` or `str` ]
244 Tuple of primary key values corresponding to ``graph.required``,
245 in that order.
247 Returns
248 -------
249 dataId : `DataCoordinate`
250 A data ID object that identifies the given dimensions.
251 ``dataId.hasFull()`` will return `True` if and only if
252 ``graph.implied`` is empty, and ``dataId.hasRecords()`` will never
253 return `True`.
254 """
255 assert len(graph.required) == len(values), \
256 f"Inconsistency between dimensions {graph.required} and required values {values}."
257 return _BasicTupleDataCoordinate(graph, values)
259 @staticmethod
260 def fromFullValues(graph: DimensionGraph, values: Tuple[DataIdValue, ...]) -> DataCoordinate:
261 """Construct a `DataCoordinate` from a tuple of dimension values that
262 identify all dimensions.
264 This is a low-level interface with at most assertion-level checking of
265 inputs. Most callers should use `standardize` instead.
267 Parameters
268 ----------
269 graph : `DimensionGraph`
270 Dimensions this data ID will identify.
271 values : `tuple` [ `int` or `str` ]
272 Tuple of primary key values corresponding to
273 ``itertools.chain(graph.required, graph.implied)``, in that order.
274 Note that this is _not_ the same order as ``graph.dimensions``,
275 though these contain the same elements.
277 Returns
278 -------
279 dataId : `DataCoordinate`
280 A data ID object that identifies the given dimensions.
281 ``dataId.hasFull()`` will return `True` if and only if
282 ``graph.implied`` is empty, and ``dataId.hasRecords()`` will never
283 return `True`.
284 """
285 assert len(graph.dimensions) == len(values), \
286 f"Inconsistency between dimensions {graph.dimensions} and full values {values}."
287 return _BasicTupleDataCoordinate(graph, values)
289 def __hash__(self) -> int:
290 return hash((self.graph,) + tuple(self[d.name] for d in self.graph.required))
292 def __eq__(self, other: Any) -> bool:
293 if not isinstance(other, DataCoordinate):
294 other = DataCoordinate.standardize(other, universe=self.universe)
295 return self.graph == other.graph and all(self[d.name] == other[d.name] for d in self.graph.required)
297 def __repr__(self) -> str:
298 # We can't make repr yield something that could be exec'd here without
299 # printing out the whole DimensionUniverse the graph is derived from.
300 # So we print something that mostly looks like a dict, but doesn't
301 # quote its keys: that's both more compact and something that can't
302 # be mistaken for an actual dict or something that could be exec'd.
303 terms = [f"{d}: {self[d]!r}" for d in self.graph.required.names]
304 if self.hasFull():
305 terms.append("...")
306 return "{{{}}}".format(', '.join(terms))
308 def __lt__(self, other: Any) -> bool:
309 # Allow DataCoordinate to be sorted
310 if not isinstance(other, type(self)):
311 return NotImplemented
312 # Form tuple of tuples for each DataCoordinate:
313 # Unlike repr() we only use required keys here to ensure that
314 # __eq__ can not be true simultaneously with __lt__ being true.
315 self_kv = tuple(self.items())
316 other_kv = tuple(other.items())
318 return self_kv < other_kv
320 def __iter__(self) -> Iterator[Dimension]:
321 return iter(self.keys())
323 def __len__(self) -> int:
324 return len(self.keys())
326 def keys(self) -> NamedValueAbstractSet[Dimension]:
327 return self.graph.required
329 @property
330 def names(self) -> AbstractSet[str]:
331 """The names of the required dimensions identified by this data ID, in
332 the same order as `keys` (`collections.abc.Set` [ `str` ]).
333 """
334 return self.keys().names
336 @abstractmethod
337 def subset(self, graph: DimensionGraph) -> DataCoordinate:
338 """Return a `DataCoordinate` whose graph is a subset of ``self.graph``.
340 Parameters
341 ----------
342 graph : `DimensionGraph`
343 The dimensions identified by the returned `DataCoordinate`.
345 Returns
346 -------
347 coordinate : `DataCoordinate`
348 A `DataCoordinate` instance that identifies only the given
349 dimensions. May be ``self`` if ``graph == self.graph``.
351 Raises
352 ------
353 KeyError
354 Raised if the primary key value for one or more required dimensions
355 is unknown. This may happen if ``graph.issubset(self.graph)`` is
356 `False`, or even if ``graph.issubset(self.graph)`` is `True`, if
357 ``self.hasFull()`` is `False` and
358 ``graph.required.issubset(self.graph.required)`` is `False`. As
359 an example of the latter case, consider trying to go from a data ID
360 with dimensions {instrument, physical_filter, band} to
361 just {instrument, band}; band is implied by
362 physical_filter and hence would have no value in the original data
363 ID if ``self.hasFull()`` is `False`.
365 Notes
366 -----
367 If `hasFull` and `hasRecords` return `True` on ``self``, they will
368 return `True` (respectively) on the returned `DataCoordinate` as well.
369 The converse does not hold.
370 """
371 raise NotImplementedError()
373 @abstractmethod
374 def expanded(self, records: NameLookupMapping[DimensionElement, Optional[DimensionRecord]]
375 ) -> DataCoordinate:
376 """Return a `DataCoordinate` that holds the given records and
377 guarantees that `hasRecords` returns `True`.
379 This is a low-level interface with at most assertion-level checking of
380 inputs. Most callers should use `Registry.expandDataId` instead.
382 Parameters
383 ----------
384 records : `Mapping` [ `str`, `DimensionRecord` or `None` ]
385 A `NamedKeyMapping` with `DimensionElement` keys or a regular
386 `Mapping` with `str` (`DimensionElement` name) keys and
387 `DimensionRecord` values. Keys must cover all elements in
388 ``self.graph.elements``. Values may be `None`, but only to reflect
389 actual NULL values in the database, not just records that have not
390 been fetched.
391 """
392 raise NotImplementedError()
394 @property
395 def universe(self) -> DimensionUniverse:
396 """The universe that defines all known dimensions compatible with
397 this coordinate (`DimensionUniverse`).
398 """
399 return self.graph.universe
401 @property
402 @abstractmethod
403 def graph(self) -> DimensionGraph:
404 """The dimensions identified by this data ID (`DimensionGraph`).
406 Note that values are only required to be present for dimensions in
407 ``self.graph.required``; all others may be retrieved (from a
408 `Registry`) given these.
409 """
410 raise NotImplementedError()
412 @abstractmethod
413 def hasFull(self) -> bool:
414 """Whether this data ID contains values for implied as well as
415 required dimensions.
417 Returns
418 -------
419 state : `bool`
420 If `True`, `__getitem__`, `get`, and `__contains__` (but not
421 `keys`!) will act as though the mapping includes key-value pairs
422 for implied dimensions, and the `full` property may be used. If
423 `False`, these operations only include key-value pairs for required
424 dimensions, and accessing `full` is an error. Always `True` if
425 there are no implied dimensions.
426 """
427 raise NotImplementedError()
429 @property
430 def full(self) -> NamedKeyMapping[Dimension, DataIdValue]:
431 """A mapping that includes key-value pairs for all dimensions in
432 ``self.graph``, including implied (`NamedKeyMapping`).
434 Accessing this attribute if `hasFull` returns `False` is a logic error
435 that may raise an exception of unspecified type either immediately or
436 when implied keys are accessed via the returned mapping, depending on
437 the implementation and whether assertions are enabled.
438 """
439 assert self.hasFull(), "full may only be accessed if hasRecords() returns True."
440 return _DataCoordinateFullView(self)
442 @abstractmethod
443 def hasRecords(self) -> bool:
444 """Whether this data ID contains records for all of the dimension
445 elements it identifies.
447 Returns
448 -------
449 state : `bool`
450 If `True`, the following attributes may be accessed:
452 - `records`
453 - `region`
454 - `timespan`
455 - `pack`
457 If `False`, accessing any of these is considered a logic error.
458 """
459 raise NotImplementedError()
461 @property
462 def records(self) -> NamedKeyMapping[DimensionElement, Optional[DimensionRecord]]:
463 """A mapping that contains `DimensionRecord` objects for all elements
464 identified by this data ID (`NamedKeyMapping`).
466 The values of this mapping may be `None` if and only if there is no
467 record for that element with these dimensions in the database (which
468 means some foreign key field must have a NULL value).
470 Accessing this attribute if `hasRecords` returns `False` is a logic
471 error that may raise an exception of unspecified type either
472 immediately or when the returned mapping is used, depending on the
473 implementation and whether assertions are enabled.
474 """
475 assert self.hasRecords(), "records may only be accessed if hasRecords() returns True."
476 return _DataCoordinateRecordsView(self)
478 @abstractmethod
479 def _record(self, name: str) -> Optional[DimensionRecord]:
480 """Protected implementation hook that backs the ``records`` attribute.
482 Parameters
483 ----------
484 name : `str`
485 The name of a `DimensionElement`, guaranteed to be in
486 ``self.graph.elements.names``.
488 Returns
489 -------
490 record : `DimensionRecord` or `None`
491 The dimension record for the given element identified by this
492 data ID, or `None` if there is no such record.
493 """
494 raise NotImplementedError()
496 @property
497 def region(self) -> Optional[Region]:
498 """The spatial region associated with this data ID
499 (`lsst.sphgeom.Region` or `None`).
501 This is `None` if and only if ``self.graph.spatial`` is empty.
503 Accessing this attribute if `hasRecords` returns `False` is a logic
504 error that may or may not raise an exception, depending on the
505 implementation and whether assertions are enabled.
506 """
507 assert self.hasRecords(), "region may only be accessed if hasRecords() returns True."
508 regions = []
509 for family in self.graph.spatial:
510 element = family.choose(self.graph.elements)
511 record = self._record(element.name)
512 if record is None or record.region is None:
513 return None
514 else:
515 regions.append(record.region)
516 return _intersectRegions(*regions)
518 @property
519 def timespan(self) -> Optional[Timespan]:
520 """The temporal interval associated with this data ID
521 (`Timespan` or `None`).
523 This is `None` if and only if ``self.graph.timespan`` is empty.
525 Accessing this attribute if `hasRecords` returns `False` is a logic
526 error that may or may not raise an exception, depending on the
527 implementation and whether assertions are enabled.
528 """
529 assert self.hasRecords(), "timespan may only be accessed if hasRecords() returns True."
530 timespans = []
531 for family in self.graph.temporal:
532 element = family.choose(self.graph.elements)
533 record = self._record(element.name)
534 # DimensionRecord subclasses for temporal elements always have
535 # .timespan, but they're dynamic so this can't be type-checked.
536 if record is None or record.timespan is None:
537 return None
538 else:
539 timespans.append(record.timespan)
540 return Timespan.intersection(*timespans)
542 def pack(self, name: str, *, returnMaxBits: bool = False) -> Union[Tuple[int, int], int]:
543 """Pack this data ID into an integer.
545 Parameters
546 ----------
547 name : `str`
548 Name of the `DimensionPacker` algorithm (as defined in the
549 dimension configuration).
550 returnMaxBits : `bool`, optional
551 If `True` (`False` is default), return the maximum number of
552 nonzero bits in the returned integer across all data IDs.
554 Returns
555 -------
556 packed : `int`
557 Integer ID. This ID is unique only across data IDs that have
558 the same values for the packer's "fixed" dimensions.
559 maxBits : `int`, optional
560 Maximum number of nonzero bits in ``packed``. Not returned unless
561 ``returnMaxBits`` is `True`.
563 Notes
564 -----
565 Accessing this attribute if `hasRecords` returns `False` is a logic
566 error that may or may not raise an exception, depending on the
567 implementation and whether assertions are enabled.
568 """
569 assert self.hasRecords(), "pack() may only be called if hasRecords() returns True."
570 return self.universe.makePacker(name, self).pack(self, returnMaxBits=returnMaxBits)
573DataId = Union[DataCoordinate, Mapping[str, Any]]
574"""A type-annotation alias for signatures that accept both informal data ID
575dictionaries and validated `DataCoordinate` instances.
576"""
579class _DataCoordinateFullView(NamedKeyMapping[Dimension, DataIdValue]):
580 """View class that provides the default implementation for
581 `DataCoordinate.full`.
583 Parameters
584 ----------
585 target : `DataCoordinate`
586 The `DataCoordinate` instance this object provides a view of.
587 """
588 def __init__(self, target: DataCoordinate):
589 self._target = target
591 __slots__ = ("_target",)
593 def __repr__(self) -> str:
594 terms = [f"{d}: {self[d]!r}" for d in self._target.graph.dimensions.names]
595 return "{{{}}}".format(', '.join(terms))
597 def __getitem__(self, key: DataIdKey) -> DataIdValue:
598 return self._target[key]
600 def __iter__(self) -> Iterator[Dimension]:
601 return iter(self.keys())
603 def __len__(self) -> int:
604 return len(self.keys())
606 def keys(self) -> NamedValueAbstractSet[Dimension]:
607 return self._target.graph.dimensions
609 @property
610 def names(self) -> AbstractSet[str]:
611 # Docstring inherited from `NamedKeyMapping`.
612 return self.keys().names
615class _DataCoordinateRecordsView(NamedKeyMapping[DimensionElement, Optional[DimensionRecord]]):
616 """View class that provides the default implementation for
617 `DataCoordinate.records`.
619 Parameters
620 ----------
621 target : `DataCoordinate`
622 The `DataCoordinate` instance this object provides a view of.
623 """
624 def __init__(self, target: DataCoordinate):
625 self._target = target
627 __slots__ = ("_target",)
629 def __repr__(self) -> str:
630 terms = [f"{d}: {self[d]!r}" for d in self._target.graph.elements.names]
631 return "{{{}}}".format(', '.join(terms))
633 def __str__(self) -> str:
634 return "\n".join(str(v) for v in self.values())
636 def __getitem__(self, key: Union[DimensionElement, str]) -> Optional[DimensionRecord]:
637 if isinstance(key, DimensionElement):
638 key = key.name
639 return self._target._record(key)
641 def __iter__(self) -> Iterator[DimensionElement]:
642 return iter(self.keys())
644 def __len__(self) -> int:
645 return len(self.keys())
647 def keys(self) -> NamedValueAbstractSet[DimensionElement]:
648 return self._target.graph.elements
650 @property
651 def names(self) -> AbstractSet[str]:
652 # Docstring inherited from `NamedKeyMapping`.
653 return self.keys().names
656class _BasicTupleDataCoordinate(DataCoordinate):
657 """Standard implementation of `DataCoordinate`, backed by a tuple of
658 values.
660 This class should only be accessed outside this module via the
661 `DataCoordinate` interface, and should only be constructed via the static
662 methods there.
664 Parameters
665 ----------
666 graph : `DimensionGraph`
667 The dimensions to be identified.
668 values : `tuple` [ `int` or `str` ]
669 Data ID values, ordered to match ``graph._dataCoordinateIndices``. May
670 include values for just required dimensions (which always come first)
671 or all dimensions.
672 """
673 def __init__(self, graph: DimensionGraph, values: Tuple[DataIdValue, ...]):
674 self._graph = graph
675 self._values = values
677 __slots__ = ("_graph", "_values")
679 @property
680 def graph(self) -> DimensionGraph:
681 # Docstring inherited from DataCoordinate.
682 return self._graph
684 def __getitem__(self, key: DataIdKey) -> DataIdValue:
685 # Docstring inherited from DataCoordinate.
686 if isinstance(key, Dimension):
687 key = key.name
688 index = self._graph._dataCoordinateIndices[key]
689 try:
690 return self._values[index]
691 except IndexError:
692 # Caller asked for an implied dimension, but this object only has
693 # values for the required ones.
694 raise KeyError(key)
696 def subset(self, graph: DimensionGraph) -> DataCoordinate:
697 # Docstring inherited from DataCoordinate.
698 if self._graph == graph:
699 return self
700 elif self.hasFull() or self._graph.required >= graph.dimensions:
701 return _BasicTupleDataCoordinate(
702 graph,
703 tuple(self[k] for k in graph._dataCoordinateIndices.keys()),
704 )
705 else:
706 return _BasicTupleDataCoordinate(graph, tuple(self[k] for k in graph.required.names))
708 def expanded(self, records: NameLookupMapping[DimensionElement, Optional[DimensionRecord]]
709 ) -> DataCoordinate:
710 # Docstring inherited from DataCoordinate
711 values = self._values
712 if not self.hasFull():
713 # Extract a complete values tuple from the attributes of the given
714 # records. It's possible for these to be inconsistent with
715 # self._values (which is a serious problem, of course), but we've
716 # documented this as a no-checking API.
717 values += tuple(getattr(records[d.name], d.primaryKey.name) for d in self._graph.implied)
718 return _ExpandedTupleDataCoordinate(self._graph, values, records)
720 def hasFull(self) -> bool:
721 # Docstring inherited from DataCoordinate.
722 return len(self._values) == len(self._graph._dataCoordinateIndices)
724 def hasRecords(self) -> bool:
725 # Docstring inherited from DataCoordinate.
726 return False
728 def _record(self, name: str) -> Optional[DimensionRecord]:
729 # Docstring inherited from DataCoordinate.
730 assert False
733class _ExpandedTupleDataCoordinate(_BasicTupleDataCoordinate):
734 """A `DataCoordinate` implementation that can hold `DimensionRecord`
735 objects.
737 This class should only be accessed outside this module via the
738 `DataCoordinate` interface, and should only be constructed via calls to
739 `DataCoordinate.expanded`.
741 Parameters
742 ----------
743 graph : `DimensionGraph`
744 The dimensions to be identified.
745 values : `tuple` [ `int` or `str` ]
746 Data ID values, ordered to match ``graph._dataCoordinateIndices``.
747 May include values for just required dimensions (which always come
748 first) or all dimensions.
749 records : `Mapping` [ `str`, `DimensionRecord` or `None` ]
750 A `NamedKeyMapping` with `DimensionElement` keys or a regular
751 `Mapping` with `str` (`DimensionElement` name) keys and
752 `DimensionRecord` values. Keys must cover all elements in
753 ``self.graph.elements``. Values may be `None`, but only to reflect
754 actual NULL values in the database, not just records that have not
755 been fetched.
756 """
757 def __init__(self, graph: DimensionGraph, values: Tuple[DataIdValue, ...],
758 records: NameLookupMapping[DimensionElement, Optional[DimensionRecord]]):
759 super().__init__(graph, values)
760 assert super().hasFull(), "This implementation requires full dimension records."
761 self._records = records
763 __slots__ = ("_records",)
765 def subset(self, graph: DimensionGraph) -> DataCoordinate:
766 # Docstring inherited from DataCoordinate.
767 if self._graph == graph:
768 return self
769 return _ExpandedTupleDataCoordinate(graph,
770 tuple(self[k] for k in graph._dataCoordinateIndices.keys()),
771 records=self._records)
773 def expanded(self, records: NameLookupMapping[DimensionElement, Optional[DimensionRecord]]
774 ) -> DataCoordinate:
775 # Docstring inherited from DataCoordinate.
776 return self
778 def hasFull(self) -> bool:
779 # Docstring inherited from DataCoordinate.
780 return True
782 def hasRecords(self) -> bool:
783 # Docstring inherited from DataCoordinate.
784 return True
786 def _record(self, name: str) -> Optional[DimensionRecord]:
787 # Docstring inherited from DataCoordinate.
788 return self._records[name]