Coverage for python / lsst / daf / butler / registry / expand_data_ids.py: 23%
21 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-30 08:41 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-30 08:41 +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 software is dual licensed under the GNU General Public License and also
10# under a 3-clause BSD license. Recipients may choose which of these licenses
11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
12# respectively. If you choose the GPL option then the following text applies
13# (but note that there is still no warranty even if you opt for BSD instead):
14#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <http://www.gnu.org/licenses/>.
28from __future__ import annotations
30from collections import defaultdict
31from collections.abc import Iterable
33from ..dimensions import (
34 DataCoordinate,
35 DimensionDataAttacher,
36 DimensionGroup,
37 DimensionUniverse,
38)
39from ..dimensions.record_cache import DimensionRecordCache
40from ..queries import QueryFactoryFunction
43def expand_data_ids(
44 data_ids: Iterable[DataCoordinate],
45 universe: DimensionUniverse,
46 query_func: QueryFactoryFunction,
47 cache: DimensionRecordCache | None,
48) -> list[DataCoordinate]:
49 """Expand the given data IDs to look up implied dimension values and attach
50 dimension records.
52 Parameters
53 ----------
54 data_ids : `~collections.abc.Iterable` [ `DataCoordinate` ]
55 Data coordinates to be expanded.
56 universe : `DimensionUniverse`
57 Dimension universe associated with the given ``data_ids`` values.
58 query_func : QueryFactoryFunction
59 Function used to set up a Butler query context for looking up required
60 information from the database.
61 cache : `DimensionRecordCache` | None
62 Cache containing already-known dimension records. May be `None` if a
63 cache is not available.
65 Returns
66 -------
67 expanded : `list` [ `DataCoordinate` ]
68 List of `DataCoordinate` instances in the same order as the input
69 values. It is guaranteed that each `DataCoordinate` has
70 ``hasRecords()=True`` and ``hasFull()=True``.
71 """
72 output = list(data_ids)
74 grouped_by_dimensions: defaultdict[DimensionGroup, list[int]] = defaultdict(list)
75 for i, data_id in enumerate(data_ids):
76 if not data_id.hasRecords():
77 grouped_by_dimensions[data_id.dimensions].append(i)
79 if not grouped_by_dimensions:
80 # All given DataCoordinate values are already expanded.
81 return output
83 attacher = DimensionDataAttacher(
84 cache=cache,
85 dimensions=DimensionGroup.union(*grouped_by_dimensions.keys(), universe=universe),
86 )
87 for dimensions, indexes in grouped_by_dimensions.items():
88 with query_func() as query:
89 expanded = attacher.attach(dimensions, (output[index] for index in indexes), query)
90 for index, data_id in zip(indexes, expanded):
91 output[index] = data_id
93 return output