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

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 return "{{{}}}".format(
304 ', '.join(f"{d}: {self.get(d, '?')}" for d in self.graph.dimensions.names)
305 )
307 def __lt__(self, other: Any) -> bool:
308 # Allow DataCoordinate to be sorted
309 if not isinstance(other, type(self)):
310 return NotImplemented
311 # Form tuple of tuples for each DataCoordinate:
312 # Unlike repr() we only use required keys here to ensure that
313 # __eq__ can not be true simultaneously with __lt__ being true.
314 self_kv = tuple(self.items())
315 other_kv = tuple(other.items())
317 return self_kv < other_kv
319 def __iter__(self) -> Iterator[Dimension]:
320 return iter(self.keys())
322 def __len__(self) -> int:
323 return len(self.keys())
325 def keys(self) -> NamedValueAbstractSet[Dimension]:
326 return self.graph.required
328 @property
329 def names(self) -> AbstractSet[str]:
330 """The names of the required dimensions identified by this data ID, in
331 the same order as `keys` (`collections.abc.Set` [ `str` ]).
332 """
333 return self.keys().names
335 @abstractmethod
336 def subset(self, graph: DimensionGraph) -> DataCoordinate:
337 """Return a `DataCoordinate` whose graph is a subset of ``self.graph``.
339 Parameters
340 ----------
341 graph : `DimensionGraph`
342 The dimensions identified by the returned `DataCoordinate`.
344 Returns
345 -------
346 coordinate : `DataCoordinate`
347 A `DataCoordinate` instance that identifies only the given
348 dimensions. May be ``self`` if ``graph == self.graph``.
350 Raises
351 ------
352 KeyError
353 Raised if the primary key value for one or more required dimensions
354 is unknown. This may happen if ``graph.issubset(self.graph)`` is
355 `False`, or even if ``graph.issubset(self.graph)`` is `True`, if
356 ``self.hasFull()`` is `False` and
357 ``graph.required.issubset(self.graph.required)`` is `False`. As
358 an example of the latter case, consider trying to go from a data ID
359 with dimensions {instrument, physical_filter, band} to
360 just {instrument, band}; band is implied by
361 physical_filter and hence would have no value in the original data
362 ID if ``self.hasFull()`` is `False`.
364 Notes
365 -----
366 If `hasFull` and `hasRecords` return `True` on ``self``, they will
367 return `True` (respectively) on the returned `DataCoordinate` as well.
368 The converse does not hold.
369 """
370 raise NotImplementedError()
372 @abstractmethod
373 def expanded(self, records: NameLookupMapping[DimensionElement, Optional[DimensionRecord]]
374 ) -> DataCoordinate:
375 """Return a `DataCoordinate` that holds the given records and
376 guarantees that `hasRecords` returns `True`.
378 This is a low-level interface with at most assertion-level checking of
379 inputs. Most callers should use `Registry.expandDataId` instead.
381 Parameters
382 ----------
383 records : `Mapping` [ `str`, `DimensionRecord` or `None` ]
384 A `NamedKeyMapping` with `DimensionElement` keys or a regular
385 `Mapping` with `str` (`DimensionElement` name) keys and
386 `DimensionRecord` values. Keys must cover all elements in
387 ``self.graph.elements``. Values may be `None`, but only to reflect
388 actual NULL values in the database, not just records that have not
389 been fetched.
390 """
391 raise NotImplementedError()
393 @property
394 def universe(self) -> DimensionUniverse:
395 """The universe that defines all known dimensions compatible with
396 this coordinate (`DimensionUniverse`).
397 """
398 return self.graph.universe
400 @property
401 @abstractmethod
402 def graph(self) -> DimensionGraph:
403 """The dimensions identified by this data ID (`DimensionGraph`).
405 Note that values are only required to be present for dimensions in
406 ``self.graph.required``; all others may be retrieved (from a
407 `Registry`) given these.
408 """
409 raise NotImplementedError()
411 @abstractmethod
412 def hasFull(self) -> bool:
413 """Whether this data ID contains values for implied as well as
414 required dimensions.
416 Returns
417 -------
418 state : `bool`
419 If `True`, `__getitem__`, `get`, and `__contains__` (but not
420 `keys`!) will act as though the mapping includes key-value pairs
421 for implied dimensions, and the `full` property may be used. If
422 `False`, these operations only include key-value pairs for required
423 dimensions, and accessing `full` is an error. Always `True` if
424 there are no implied dimensions.
425 """
426 raise NotImplementedError()
428 @property
429 def full(self) -> NamedKeyMapping[Dimension, DataIdValue]:
430 """A mapping that includes key-value pairs for all dimensions in
431 ``self.graph``, including implied (`NamedKeyMapping`).
433 Accessing this attribute if `hasFull` returns `False` is a logic error
434 that may raise an exception of unspecified type either immediately or
435 when implied keys are accessed via the returned mapping, depending on
436 the implementation and whether assertions are enabled.
437 """
438 assert self.hasFull(), "full may only be accessed if hasRecords() returns True."
439 return _DataCoordinateFullView(self)
441 @abstractmethod
442 def hasRecords(self) -> bool:
443 """Whether this data ID contains records for all of the dimension
444 elements it identifies.
446 Returns
447 -------
448 state : `bool`
449 If `True`, the following attributes may be accessed:
451 - `records`
452 - `region`
453 - `timespan`
454 - `pack`
456 If `False`, accessing any of these is considered a logic error.
457 """
458 raise NotImplementedError()
460 @property
461 def records(self) -> NamedKeyMapping[DimensionElement, Optional[DimensionRecord]]:
462 """A mapping that contains `DimensionRecord` objects for all elements
463 identified by this data ID (`NamedKeyMapping`).
465 The values of this mapping may be `None` if and only if there is no
466 record for that element with these dimensions in the database (which
467 means some foreign key field must have a NULL value).
469 Accessing this attribute if `hasRecords` returns `False` is a logic
470 error that may raise an exception of unspecified type either
471 immediately or when the returned mapping is used, depending on the
472 implementation and whether assertions are enabled.
473 """
474 assert self.hasRecords(), "records may only be accessed if hasRecords() returns True."
475 return _DataCoordinateRecordsView(self)
477 @abstractmethod
478 def _record(self, name: str) -> Optional[DimensionRecord]:
479 """Protected implementation hook that backs the ``records`` attribute.
481 Parameters
482 ----------
483 name : `str`
484 The name of a `DimensionElement`, guaranteed to be in
485 ``self.graph.elements.names``.
487 Returns
488 -------
489 record : `DimensionRecord` or `None`
490 The dimension record for the given element identified by this
491 data ID, or `None` if there is no such record.
492 """
493 raise NotImplementedError()
495 @property
496 def region(self) -> Optional[Region]:
497 """The spatial region associated with this data ID
498 (`lsst.sphgeom.Region` or `None`).
500 This is `None` if and only if ``self.graph.spatial`` is empty.
502 Accessing this attribute if `hasRecords` returns `False` is a logic
503 error that may or may not raise an exception, depending on the
504 implementation and whether assertions are enabled.
505 """
506 assert self.hasRecords(), "region may only be accessed if hasRecords() returns True."
507 regions = []
508 for family in self.graph.spatial:
509 element = family.choose(self.graph.elements)
510 record = self._record(element.name)
511 if record is None or record.region is None:
512 return None
513 else:
514 regions.append(record.region)
515 return _intersectRegions(*regions)
517 @property
518 def timespan(self) -> Optional[Timespan]:
519 """The temporal interval associated with this data ID
520 (`Timespan` or `None`).
522 This is `None` if and only if ``self.graph.timespan`` is empty.
524 Accessing this attribute if `hasRecords` returns `False` is a logic
525 error that may or may not raise an exception, depending on the
526 implementation and whether assertions are enabled.
527 """
528 assert self.hasRecords(), "timespan may only be accessed if hasRecords() returns True."
529 timespans = []
530 for family in self.graph.temporal:
531 element = family.choose(self.graph.elements)
532 record = self._record(element.name)
533 # DimensionRecord subclasses for temporal elements always have
534 # .timespan, but they're dynamic so this can't be type-checked.
535 if record is None or record.timespan is None:
536 return None
537 else:
538 timespans.append(record.timespan)
539 return Timespan.intersection(*timespans)
541 def pack(self, name: str, *, returnMaxBits: bool = False) -> Union[Tuple[int, int], int]:
542 """Pack this data ID into an integer.
544 Parameters
545 ----------
546 name : `str`
547 Name of the `DimensionPacker` algorithm (as defined in the
548 dimension configuration).
549 returnMaxBits : `bool`, optional
550 If `True` (`False` is default), return the maximum number of
551 nonzero bits in the returned integer across all data IDs.
553 Returns
554 -------
555 packed : `int`
556 Integer ID. This ID is unique only across data IDs that have
557 the same values for the packer's "fixed" dimensions.
558 maxBits : `int`, optional
559 Maximum number of nonzero bits in ``packed``. Not returned unless
560 ``returnMaxBits`` is `True`.
562 Notes
563 -----
564 Accessing this attribute if `hasRecords` returns `False` is a logic
565 error that may or may not raise an exception, depending on the
566 implementation and whether assertions are enabled.
567 """
568 assert self.hasRecords(), "pack() may only be called if hasRecords() returns True."
569 return self.universe.makePacker(name, self).pack(self, returnMaxBits=returnMaxBits)
572DataId = Union[DataCoordinate, Mapping[str, Any]]
573"""A type-annotation alias for signatures that accept both informal data ID
574dictionaries and validated `DataCoordinate` instances.
575"""
578class _DataCoordinateFullView(NamedKeyMapping[Dimension, DataIdValue]):
579 """View class that provides the default implementation for
580 `DataCoordinate.full`.
582 Parameters
583 ----------
584 target : `DataCoordinate`
585 The `DataCoordinate` instance this object provides a view of.
586 """
587 def __init__(self, target: DataCoordinate):
588 self._target = target
590 __slots__ = ("_target",)
592 def __getitem__(self, key: DataIdKey) -> DataIdValue:
593 return self._target[key]
595 def __iter__(self) -> Iterator[Dimension]:
596 return iter(self.keys())
598 def __len__(self) -> int:
599 return len(self.keys())
601 def keys(self) -> NamedValueAbstractSet[Dimension]:
602 return self._target.graph.dimensions
604 @property
605 def names(self) -> AbstractSet[str]:
606 # Docstring inherited from `NamedKeyMapping`.
607 return self.keys().names
610class _DataCoordinateRecordsView(NamedKeyMapping[DimensionElement, Optional[DimensionRecord]]):
611 """View class that provides the default implementation for
612 `DataCoordinate.records`.
614 Parameters
615 ----------
616 target : `DataCoordinate`
617 The `DataCoordinate` instance this object provides a view of.
618 """
619 def __init__(self, target: DataCoordinate):
620 self._target = target
622 __slots__ = ("_target",)
624 def __getitem__(self, key: Union[DimensionElement, str]) -> Optional[DimensionRecord]:
625 if isinstance(key, DimensionElement):
626 key = key.name
627 return self._target._record(key)
629 def __iter__(self) -> Iterator[DimensionElement]:
630 return iter(self.keys())
632 def __len__(self) -> int:
633 return len(self.keys())
635 def keys(self) -> NamedValueAbstractSet[DimensionElement]:
636 return self._target.graph.elements
638 @property
639 def names(self) -> AbstractSet[str]:
640 # Docstring inherited from `NamedKeyMapping`.
641 return self.keys().names
644class _BasicTupleDataCoordinate(DataCoordinate):
645 """Standard implementation of `DataCoordinate`, backed by a tuple of
646 values.
648 This class should only be accessed outside this module via the
649 `DataCoordinate` interface, and should only be constructed via the static
650 methods there.
652 Parameters
653 ----------
654 graph : `DimensionGraph`
655 The dimensions to be identified.
656 values : `tuple` [ `int` or `str` ]
657 Data ID values, ordered to match ``graph._dataCoordinateIndices``. May
658 include values for just required dimensions (which always come first)
659 or all dimensions.
660 """
661 def __init__(self, graph: DimensionGraph, values: Tuple[DataIdValue, ...]):
662 self._graph = graph
663 self._values = values
665 __slots__ = ("_graph", "_values")
667 @property
668 def graph(self) -> DimensionGraph:
669 # Docstring inherited from DataCoordinate.
670 return self._graph
672 def __getitem__(self, key: DataIdKey) -> DataIdValue:
673 # Docstring inherited from DataCoordinate.
674 if isinstance(key, Dimension):
675 key = key.name
676 index = self._graph._dataCoordinateIndices[key]
677 try:
678 return self._values[index]
679 except IndexError:
680 # Caller asked for an implied dimension, but this object only has
681 # values for the required ones.
682 raise KeyError(key)
684 def subset(self, graph: DimensionGraph) -> DataCoordinate:
685 # Docstring inherited from DataCoordinate.
686 if self._graph == graph:
687 return self
688 elif self.hasFull() or self._graph.required >= graph.dimensions:
689 return _BasicTupleDataCoordinate(
690 graph,
691 tuple(self[k] for k in graph._dataCoordinateIndices.keys()),
692 )
693 else:
694 return _BasicTupleDataCoordinate(graph, tuple(self[k] for k in graph.required.names))
696 def expanded(self, records: NameLookupMapping[DimensionElement, Optional[DimensionRecord]]
697 ) -> DataCoordinate:
698 # Docstring inherited from DataCoordinate
699 values = self._values
700 if not self.hasFull():
701 # Extract a complete values tuple from the attributes of the given
702 # records. It's possible for these to be inconsistent with
703 # self._values (which is a serious problem, of course), but we've
704 # documented this as a no-checking API.
705 values += tuple(getattr(records[d.name], d.primaryKey.name) for d in self._graph.implied)
706 return _ExpandedTupleDataCoordinate(self._graph, values, records)
708 def hasFull(self) -> bool:
709 # Docstring inherited from DataCoordinate.
710 return len(self._values) == len(self._graph._dataCoordinateIndices)
712 def hasRecords(self) -> bool:
713 # Docstring inherited from DataCoordinate.
714 return False
716 def _record(self, name: str) -> Optional[DimensionRecord]:
717 # Docstring inherited from DataCoordinate.
718 assert False
721class _ExpandedTupleDataCoordinate(_BasicTupleDataCoordinate):
722 """A `DataCoordinate` implementation that can hold `DimensionRecord`
723 objects.
725 This class should only be accessed outside this module via the
726 `DataCoordinate` interface, and should only be constructed via calls to
727 `DataCoordinate.expanded`.
729 Parameters
730 ----------
731 graph : `DimensionGraph`
732 The dimensions to be identified.
733 values : `tuple` [ `int` or `str` ]
734 Data ID values, ordered to match ``graph._dataCoordinateIndices``.
735 May include values for just required dimensions (which always come
736 first) or all dimensions.
737 records : `Mapping` [ `str`, `DimensionRecord` or `None` ]
738 A `NamedKeyMapping` with `DimensionElement` keys or a regular
739 `Mapping` with `str` (`DimensionElement` name) keys and
740 `DimensionRecord` values. Keys must cover all elements in
741 ``self.graph.elements``. Values may be `None`, but only to reflect
742 actual NULL values in the database, not just records that have not
743 been fetched.
744 """
745 def __init__(self, graph: DimensionGraph, values: Tuple[DataIdValue, ...],
746 records: NameLookupMapping[DimensionElement, Optional[DimensionRecord]]):
747 super().__init__(graph, values)
748 assert super().hasFull(), "This implementation requires full dimension records."
749 self._records = records
751 __slots__ = ("_records",)
753 def subset(self, graph: DimensionGraph) -> DataCoordinate:
754 # Docstring inherited from DataCoordinate.
755 if self._graph == graph:
756 return self
757 return _ExpandedTupleDataCoordinate(graph,
758 tuple(self[k] for k in graph._dataCoordinateIndices.keys()),
759 records=self._records)
761 def expanded(self, records: NameLookupMapping[DimensionElement, Optional[DimensionRecord]]
762 ) -> DataCoordinate:
763 # Docstring inherited from DataCoordinate.
764 return self
766 def hasFull(self) -> bool:
767 # Docstring inherited from DataCoordinate.
768 return True
770 def hasRecords(self) -> bool:
771 # Docstring inherited from DataCoordinate.
772 return True
774 def _record(self, name: str) -> Optional[DimensionRecord]:
775 # Docstring inherited from DataCoordinate.
776 return self._records[name]