lsst-pipe-base 30.0.0rc2__py3-none-any.whl → 30.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- lsst/pipe/base/_instrument.py +31 -20
- lsst/pipe/base/_quantumContext.py +3 -3
- lsst/pipe/base/_status.py +43 -10
- lsst/pipe/base/_task_metadata.py +2 -2
- lsst/pipe/base/all_dimensions_quantum_graph_builder.py +8 -3
- lsst/pipe/base/automatic_connection_constants.py +20 -1
- lsst/pipe/base/cli/cmd/__init__.py +18 -2
- lsst/pipe/base/cli/cmd/commands.py +149 -4
- lsst/pipe/base/connectionTypes.py +72 -160
- lsst/pipe/base/connections.py +6 -9
- lsst/pipe/base/execution_reports.py +0 -5
- lsst/pipe/base/graph/graph.py +11 -10
- lsst/pipe/base/graph/quantumNode.py +4 -4
- lsst/pipe/base/graph_walker.py +8 -10
- lsst/pipe/base/log_capture.py +40 -80
- lsst/pipe/base/log_on_close.py +76 -0
- lsst/pipe/base/mp_graph_executor.py +51 -15
- lsst/pipe/base/pipeline.py +5 -6
- lsst/pipe/base/pipelineIR.py +2 -8
- lsst/pipe/base/pipelineTask.py +5 -7
- lsst/pipe/base/pipeline_graph/_dataset_types.py +2 -2
- lsst/pipe/base/pipeline_graph/_edges.py +32 -22
- lsst/pipe/base/pipeline_graph/_mapping_views.py +4 -7
- lsst/pipe/base/pipeline_graph/_pipeline_graph.py +14 -7
- lsst/pipe/base/pipeline_graph/expressions.py +2 -2
- lsst/pipe/base/pipeline_graph/io.py +7 -10
- lsst/pipe/base/pipeline_graph/visualization/_dot.py +13 -12
- lsst/pipe/base/pipeline_graph/visualization/_layout.py +16 -18
- lsst/pipe/base/pipeline_graph/visualization/_merge.py +4 -7
- lsst/pipe/base/pipeline_graph/visualization/_printer.py +10 -10
- lsst/pipe/base/pipeline_graph/visualization/_status_annotator.py +7 -0
- lsst/pipe/base/prerequisite_helpers.py +2 -1
- lsst/pipe/base/quantum_graph/_common.py +19 -20
- lsst/pipe/base/quantum_graph/_multiblock.py +37 -31
- lsst/pipe/base/quantum_graph/_predicted.py +113 -15
- lsst/pipe/base/quantum_graph/_provenance.py +1136 -45
- lsst/pipe/base/quantum_graph/aggregator/__init__.py +0 -1
- lsst/pipe/base/quantum_graph/aggregator/_communicators.py +204 -289
- lsst/pipe/base/quantum_graph/aggregator/_config.py +87 -9
- lsst/pipe/base/quantum_graph/aggregator/_ingester.py +13 -12
- lsst/pipe/base/quantum_graph/aggregator/_scanner.py +49 -235
- lsst/pipe/base/quantum_graph/aggregator/_structs.py +6 -116
- lsst/pipe/base/quantum_graph/aggregator/_supervisor.py +29 -39
- lsst/pipe/base/quantum_graph/aggregator/_workers.py +303 -0
- lsst/pipe/base/quantum_graph/aggregator/_writer.py +34 -351
- lsst/pipe/base/quantum_graph/formatter.py +171 -0
- lsst/pipe/base/quantum_graph/ingest_graph.py +413 -0
- lsst/pipe/base/quantum_graph/visualization.py +5 -1
- lsst/pipe/base/quantum_graph_builder.py +33 -9
- lsst/pipe/base/quantum_graph_executor.py +116 -13
- lsst/pipe/base/quantum_graph_skeleton.py +31 -35
- lsst/pipe/base/quantum_provenance_graph.py +29 -12
- lsst/pipe/base/separable_pipeline_executor.py +19 -3
- lsst/pipe/base/single_quantum_executor.py +67 -42
- lsst/pipe/base/struct.py +4 -0
- lsst/pipe/base/testUtils.py +3 -3
- lsst/pipe/base/tests/mocks/_storage_class.py +2 -1
- lsst/pipe/base/version.py +1 -1
- {lsst_pipe_base-30.0.0rc2.dist-info → lsst_pipe_base-30.0.1.dist-info}/METADATA +3 -3
- lsst_pipe_base-30.0.1.dist-info/RECORD +129 -0
- {lsst_pipe_base-30.0.0rc2.dist-info → lsst_pipe_base-30.0.1.dist-info}/WHEEL +1 -1
- lsst_pipe_base-30.0.0rc2.dist-info/RECORD +0 -125
- {lsst_pipe_base-30.0.0rc2.dist-info → lsst_pipe_base-30.0.1.dist-info}/entry_points.txt +0 -0
- {lsst_pipe_base-30.0.0rc2.dist-info → lsst_pipe_base-30.0.1.dist-info}/licenses/COPYRIGHT +0 -0
- {lsst_pipe_base-30.0.0rc2.dist-info → lsst_pipe_base-30.0.1.dist-info}/licenses/LICENSE +0 -0
- {lsst_pipe_base-30.0.0rc2.dist-info → lsst_pipe_base-30.0.1.dist-info}/licenses/bsd_license.txt +0 -0
- {lsst_pipe_base-30.0.0rc2.dist-info → lsst_pipe_base-30.0.1.dist-info}/licenses/gpl-v3.0.txt +0 -0
- {lsst_pipe_base-30.0.0rc2.dist-info → lsst_pipe_base-30.0.1.dist-info}/top_level.txt +0 -0
- {lsst_pipe_base-30.0.0rc2.dist-info → lsst_pipe_base-30.0.1.dist-info}/zip-safe +0 -0
|
@@ -0,0 +1,413 @@
|
|
|
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 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/>.
|
|
27
|
+
|
|
28
|
+
"""A tool for ingesting provenance quantum graphs (written by the `aggregator`
|
|
29
|
+
module) and [re-]ingesting other datasets (metadata/logs/configs) backed by the
|
|
30
|
+
same file. This "finalizes" the RUN collection, prohibiting (at least
|
|
31
|
+
conceptually) further processing.
|
|
32
|
+
|
|
33
|
+
This always proceeds in three steps, so we can resume efficiently:
|
|
34
|
+
|
|
35
|
+
1. First we ask the butler to "forget" any metadata/log/config datasets that
|
|
36
|
+
exist in the output RUN collection, removing any record of them from the
|
|
37
|
+
butler database while preserving their files.
|
|
38
|
+
|
|
39
|
+
2. Next we ingest the ``run_provenance`` graph dataset itself.
|
|
40
|
+
|
|
41
|
+
3. Finally, in batches of quanta, we use a
|
|
42
|
+
`~lsst.daf.butler.QuantumBackedButler` to delete the original
|
|
43
|
+
metadata/log/config files and ingest new versions of those datasets into the
|
|
44
|
+
butler.
|
|
45
|
+
|
|
46
|
+
Thus, at any point, if the ``run_provenance`` dataset has not been ingested,
|
|
47
|
+
we know any metadata/log/config datasets that have been ingested are backed by
|
|
48
|
+
the original files.
|
|
49
|
+
|
|
50
|
+
Moreover, if the ``run_provenance`` dataset has been ingested, any existing
|
|
51
|
+
metadata/log/config datasets must be backed by the graph file, and the original
|
|
52
|
+
files for those datasets will have been deleted.
|
|
53
|
+
|
|
54
|
+
We also know that at all times the metadata/log/config *content* is safely
|
|
55
|
+
present in either the original files in the butler storage or in an
|
|
56
|
+
already-ingested ``run_provenance`` dataset.
|
|
57
|
+
"""
|
|
58
|
+
|
|
59
|
+
from __future__ import annotations
|
|
60
|
+
|
|
61
|
+
__all__ = ("ingest_graph",)
|
|
62
|
+
|
|
63
|
+
import dataclasses
|
|
64
|
+
import itertools
|
|
65
|
+
import os
|
|
66
|
+
import uuid
|
|
67
|
+
from collections.abc import Iterator
|
|
68
|
+
from contextlib import contextmanager
|
|
69
|
+
|
|
70
|
+
from lsst.daf.butler import (
|
|
71
|
+
Butler,
|
|
72
|
+
Config,
|
|
73
|
+
DataCoordinate,
|
|
74
|
+
DatasetRef,
|
|
75
|
+
DatasetType,
|
|
76
|
+
FileDataset,
|
|
77
|
+
QuantumBackedButler,
|
|
78
|
+
)
|
|
79
|
+
from lsst.daf.butler.registry.sql_registry import SqlRegistry
|
|
80
|
+
from lsst.resources import ResourcePath, ResourcePathExpression
|
|
81
|
+
from lsst.utils.logging import getLogger
|
|
82
|
+
|
|
83
|
+
from ..automatic_connection_constants import PROVENANCE_DATASET_TYPE_NAME, PROVENANCE_STORAGE_CLASS
|
|
84
|
+
from ._provenance import (
|
|
85
|
+
ProvenanceDatasetInfo,
|
|
86
|
+
ProvenanceInitQuantumInfo,
|
|
87
|
+
ProvenanceQuantumGraph,
|
|
88
|
+
ProvenanceQuantumGraphReader,
|
|
89
|
+
ProvenanceQuantumInfo,
|
|
90
|
+
)
|
|
91
|
+
from .formatter import ProvenanceFormatter
|
|
92
|
+
|
|
93
|
+
_LOG = getLogger(__name__)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def ingest_graph(
|
|
97
|
+
butler_config: str | Config,
|
|
98
|
+
uri: ResourcePathExpression | None = None,
|
|
99
|
+
*,
|
|
100
|
+
transfer: str | None = "move",
|
|
101
|
+
batch_size: int = 10000,
|
|
102
|
+
output_run: str | None = None,
|
|
103
|
+
) -> None:
|
|
104
|
+
"""Ingest a provenance graph into a butler repository.
|
|
105
|
+
|
|
106
|
+
Parameters
|
|
107
|
+
----------
|
|
108
|
+
butler_config : `str`
|
|
109
|
+
Path or alias for the butler repository, or a butler repository config
|
|
110
|
+
object.
|
|
111
|
+
uri : `lsst.resources.ResourcePathExpression` or `None`, optional
|
|
112
|
+
Location of the provenance quantum graph to ingest. `None` indicates
|
|
113
|
+
that the quantum graph has already been ingested, but other ingests
|
|
114
|
+
and/or deletions failed and need to be resumed.
|
|
115
|
+
transfer : `str` or `None`, optional
|
|
116
|
+
Transfer mode to use when ingesting graph. Matches those supported
|
|
117
|
+
by `lsst.resources.ResourcePath.transfer_from`.
|
|
118
|
+
batch_size : `int`, optional
|
|
119
|
+
Number of datasets to process in each transaction.
|
|
120
|
+
output_run : `str`, optional
|
|
121
|
+
Output `~lsst.daf.butler.CollectionType.RUN` collection name. Only
|
|
122
|
+
needs to be provided if ``uri`` is `None`. If it is provided the
|
|
123
|
+
output run in the graph is checked against it.
|
|
124
|
+
|
|
125
|
+
Notes
|
|
126
|
+
-----
|
|
127
|
+
After this operation, any further processing done in the
|
|
128
|
+
`~lsst.daf.butler.CollectionType.RUN` collection will not be included in
|
|
129
|
+
the provenance.
|
|
130
|
+
|
|
131
|
+
If this process is interrupted, it can pick up where it left off if run
|
|
132
|
+
again (at the cost of some duplicate work to figure out how much progress
|
|
133
|
+
it had made).
|
|
134
|
+
"""
|
|
135
|
+
with _GraphIngester.open(butler_config, uri, output_run) as helper:
|
|
136
|
+
helper.fetch_already_ingested_datasets()
|
|
137
|
+
if not helper.graph_already_ingested:
|
|
138
|
+
assert uri is not None
|
|
139
|
+
helper.forget_ingested_datasets(batch_size=batch_size)
|
|
140
|
+
helper.ingest_graph_dataset(uri, transfer=transfer)
|
|
141
|
+
helper.clean_and_reingest_datasets(batch_size=batch_size)
|
|
142
|
+
if helper.directories_to_delete:
|
|
143
|
+
_LOG.info(
|
|
144
|
+
"Deleting %d directories after checking that they are empty.",
|
|
145
|
+
len(helper.directories_to_delete),
|
|
146
|
+
)
|
|
147
|
+
n_deleted: int = 0
|
|
148
|
+
for top in sorted(helper.directories_to_delete):
|
|
149
|
+
nonempty: set[str] = set()
|
|
150
|
+
for root, dirnames, filenames in os.walk(top, topdown=False):
|
|
151
|
+
if filenames:
|
|
152
|
+
nonempty.add(root)
|
|
153
|
+
for dirname in dirnames:
|
|
154
|
+
dirpath = os.path.join(root, dirname)
|
|
155
|
+
if dirpath in nonempty:
|
|
156
|
+
nonempty.add(root)
|
|
157
|
+
else:
|
|
158
|
+
os.rmdir(dirpath)
|
|
159
|
+
if nonempty:
|
|
160
|
+
_LOG.warning(
|
|
161
|
+
"Directory %r was not deleted because it unexpectedly still had files in it.",
|
|
162
|
+
top,
|
|
163
|
+
)
|
|
164
|
+
else:
|
|
165
|
+
os.rmdir(root)
|
|
166
|
+
n_deleted += 1
|
|
167
|
+
_LOG.info("Deleted %d directories.", n_deleted)
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
@dataclasses.dataclass
|
|
171
|
+
class _GraphIngester:
|
|
172
|
+
butler_config: str | Config
|
|
173
|
+
butler: Butler
|
|
174
|
+
graph: ProvenanceQuantumGraph
|
|
175
|
+
graph_already_ingested: bool
|
|
176
|
+
n_datasets: int
|
|
177
|
+
datasets_already_ingested: set[uuid.UUID] = dataclasses.field(default_factory=set)
|
|
178
|
+
directories_to_delete: set[str] = dataclasses.field(default_factory=set)
|
|
179
|
+
|
|
180
|
+
@property
|
|
181
|
+
def output_run(self) -> str:
|
|
182
|
+
return self.graph.header.output_run
|
|
183
|
+
|
|
184
|
+
@classmethod
|
|
185
|
+
@contextmanager
|
|
186
|
+
def open(
|
|
187
|
+
cls,
|
|
188
|
+
butler_config: str | Config,
|
|
189
|
+
uri: ResourcePathExpression | None,
|
|
190
|
+
output_run: str | None,
|
|
191
|
+
) -> Iterator[_GraphIngester]:
|
|
192
|
+
with Butler.from_config(butler_config, collections=output_run, writeable=True) as butler:
|
|
193
|
+
butler.registry.registerDatasetType(
|
|
194
|
+
DatasetType(PROVENANCE_DATASET_TYPE_NAME, butler.dimensions.empty, PROVENANCE_STORAGE_CLASS)
|
|
195
|
+
)
|
|
196
|
+
graph, graph_already_ingested = cls.read_graph(butler, uri)
|
|
197
|
+
if output_run is not None and graph.header.output_run != output_run:
|
|
198
|
+
raise ValueError(
|
|
199
|
+
f"Given output run {output_run!r} does not match the graph "
|
|
200
|
+
f"header {graph.header.output_run!r}."
|
|
201
|
+
)
|
|
202
|
+
n_datasets = 2 * len(graph.quantum_only_xgraph) + len(graph.init_quanta)
|
|
203
|
+
yield cls(
|
|
204
|
+
butler_config=butler_config,
|
|
205
|
+
butler=butler,
|
|
206
|
+
graph=graph,
|
|
207
|
+
graph_already_ingested=graph_already_ingested,
|
|
208
|
+
n_datasets=n_datasets,
|
|
209
|
+
)
|
|
210
|
+
|
|
211
|
+
@staticmethod
|
|
212
|
+
def read_graph(
|
|
213
|
+
butler: Butler,
|
|
214
|
+
uri: ResourcePathExpression | None,
|
|
215
|
+
) -> tuple[ProvenanceQuantumGraph, bool]:
|
|
216
|
+
if uri is not None:
|
|
217
|
+
_LOG.info("Reading the pre-ingest provenance graph.")
|
|
218
|
+
with ProvenanceQuantumGraphReader.open(uri) as reader:
|
|
219
|
+
reader.read_quanta()
|
|
220
|
+
reader.read_init_quanta()
|
|
221
|
+
graph = reader.graph
|
|
222
|
+
already_ingested = (
|
|
223
|
+
butler.find_dataset(PROVENANCE_DATASET_TYPE_NAME, collections=[graph.header.output_run])
|
|
224
|
+
is not None
|
|
225
|
+
)
|
|
226
|
+
return graph, already_ingested
|
|
227
|
+
else:
|
|
228
|
+
_LOG.info("Reading the already-ingested provenance graph.")
|
|
229
|
+
parameters = {"datasets": [], "read_init_quanta": True}
|
|
230
|
+
return butler.get(PROVENANCE_DATASET_TYPE_NAME, parameters=parameters), True
|
|
231
|
+
|
|
232
|
+
def fetch_already_ingested_datasets(self) -> None:
|
|
233
|
+
_LOG.info("Querying for existing datasets in %r.", self.output_run)
|
|
234
|
+
self.datasets_already_ingested.update(self.butler.registry._fetch_run_dataset_ids(self.output_run))
|
|
235
|
+
|
|
236
|
+
def iter_datasets(self) -> Iterator[tuple[uuid.UUID, ProvenanceDatasetInfo]]:
|
|
237
|
+
xgraph = self.graph.bipartite_xgraph
|
|
238
|
+
for task_label, quanta_for_task in self.graph.quanta_by_task.items():
|
|
239
|
+
_LOG.verbose(
|
|
240
|
+
"Batching up metadata and log datasets from %d %s quanta.", len(quanta_for_task), task_label
|
|
241
|
+
)
|
|
242
|
+
for quantum_id in quanta_for_task.values():
|
|
243
|
+
quantum_info: ProvenanceQuantumInfo = xgraph.nodes[quantum_id]
|
|
244
|
+
metadata_id = quantum_info["metadata_id"]
|
|
245
|
+
yield metadata_id, xgraph.nodes[metadata_id]
|
|
246
|
+
log_id = quantum_info["log_id"]
|
|
247
|
+
yield log_id, xgraph.nodes[log_id]
|
|
248
|
+
_LOG.verbose("Batching up config datasets from %d tasks.", len(self.graph.init_quanta))
|
|
249
|
+
for task_label, quantum_id in self.graph.init_quanta.items():
|
|
250
|
+
init_quantum_info: ProvenanceInitQuantumInfo = xgraph.nodes[quantum_id]
|
|
251
|
+
config_id = init_quantum_info["config_id"]
|
|
252
|
+
yield config_id, xgraph.nodes[config_id]
|
|
253
|
+
|
|
254
|
+
def forget_ingested_datasets(self, batch_size: int) -> None:
|
|
255
|
+
_LOG.info(
|
|
256
|
+
"Dropping database records for metadata/log/config datasets backed by their original files."
|
|
257
|
+
)
|
|
258
|
+
to_forget: list[DatasetRef] = []
|
|
259
|
+
n_forgotten: int = 0
|
|
260
|
+
n_skipped: int = 0
|
|
261
|
+
for dataset_id, dataset_info in self.iter_datasets():
|
|
262
|
+
if dataset_info["produced"] and dataset_id in self.datasets_already_ingested:
|
|
263
|
+
to_forget.append(self._make_ref_from_info(dataset_id, dataset_info))
|
|
264
|
+
self.datasets_already_ingested.remove(dataset_id)
|
|
265
|
+
if len(to_forget) >= batch_size:
|
|
266
|
+
n_forgotten += self._run_forget(to_forget, n_forgotten + n_skipped)
|
|
267
|
+
else:
|
|
268
|
+
n_skipped += 1
|
|
269
|
+
n_forgotten += self._run_forget(to_forget, n_forgotten + n_skipped)
|
|
270
|
+
_LOG.info(
|
|
271
|
+
"Removed database records for %d metadata/log/config datasets, while %d were already absent.",
|
|
272
|
+
n_forgotten,
|
|
273
|
+
n_skipped,
|
|
274
|
+
)
|
|
275
|
+
|
|
276
|
+
def _run_forget(self, to_forget: list[DatasetRef], n_current: int) -> int:
|
|
277
|
+
if to_forget:
|
|
278
|
+
_LOG.verbose(
|
|
279
|
+
"Forgetting a %d-dataset batch; %d/%d forgotten so far or already absent.",
|
|
280
|
+
len(to_forget),
|
|
281
|
+
n_current,
|
|
282
|
+
self.n_datasets,
|
|
283
|
+
)
|
|
284
|
+
with self.butler.registry.transaction():
|
|
285
|
+
self.butler._datastore.forget(to_forget)
|
|
286
|
+
self.butler.registry.removeDatasets(to_forget)
|
|
287
|
+
n = len(to_forget)
|
|
288
|
+
to_forget.clear()
|
|
289
|
+
return n
|
|
290
|
+
|
|
291
|
+
def ingest_graph_dataset(self, uri: ResourcePathExpression, transfer: str | None) -> None:
|
|
292
|
+
_LOG.info("Ingesting the provenance quantum graph.")
|
|
293
|
+
dataset_type = DatasetType(
|
|
294
|
+
PROVENANCE_DATASET_TYPE_NAME, self.butler.dimensions.empty, PROVENANCE_STORAGE_CLASS
|
|
295
|
+
)
|
|
296
|
+
self.butler.registry.registerDatasetType(dataset_type)
|
|
297
|
+
ref = DatasetRef(dataset_type, DataCoordinate.make_empty(self.butler.dimensions), run=self.output_run)
|
|
298
|
+
uri = ResourcePath(uri)
|
|
299
|
+
self.butler.ingest(
|
|
300
|
+
# We use .abspath() since butler assumes paths are relative to the
|
|
301
|
+
# repo root, while users expects them to be relative to the CWD in
|
|
302
|
+
# this context.
|
|
303
|
+
FileDataset(refs=[ref], path=uri.abspath(), formatter=ProvenanceFormatter),
|
|
304
|
+
transfer=transfer,
|
|
305
|
+
)
|
|
306
|
+
|
|
307
|
+
def clean_and_reingest_datasets(self, batch_size: int) -> None:
|
|
308
|
+
_LOG.info(
|
|
309
|
+
"Deleting original metadata/log/config files and re-ingesting them with provenance graph backing."
|
|
310
|
+
)
|
|
311
|
+
direct_uri = self.butler.getURI(PROVENANCE_DATASET_TYPE_NAME, collections=[self.output_run])
|
|
312
|
+
qbb = self.make_qbb()
|
|
313
|
+
to_process: list[DatasetRef] = []
|
|
314
|
+
n_processed: int = 0
|
|
315
|
+
n_skipped: int = 0
|
|
316
|
+
n_not_produced: int = 0
|
|
317
|
+
for dataset_id, dataset_info in self.iter_datasets():
|
|
318
|
+
if not dataset_info["produced"]:
|
|
319
|
+
n_not_produced += 1
|
|
320
|
+
elif dataset_id not in self.datasets_already_ingested:
|
|
321
|
+
to_process.append(self._make_ref_from_info(dataset_id, dataset_info))
|
|
322
|
+
if len(to_process) >= batch_size:
|
|
323
|
+
n_processed += self._run_clean_and_ingest(
|
|
324
|
+
qbb, direct_uri, to_process, n_processed + n_skipped
|
|
325
|
+
)
|
|
326
|
+
else:
|
|
327
|
+
n_skipped += 1
|
|
328
|
+
n_processed += self._run_clean_and_ingest(qbb, direct_uri, to_process, n_processed + n_skipped)
|
|
329
|
+
_LOG.info(
|
|
330
|
+
"Deleted and re-ingested %d metadata/log/config datasets "
|
|
331
|
+
"(%d had already been processed, %d were not produced).",
|
|
332
|
+
n_processed,
|
|
333
|
+
n_skipped,
|
|
334
|
+
n_not_produced,
|
|
335
|
+
)
|
|
336
|
+
|
|
337
|
+
def _run_clean_and_ingest(
|
|
338
|
+
self, qbb: QuantumBackedButler, direct_uri: ResourcePath, to_process: list[DatasetRef], n_current: int
|
|
339
|
+
) -> int:
|
|
340
|
+
if not to_process:
|
|
341
|
+
return 0
|
|
342
|
+
_LOG.verbose(
|
|
343
|
+
"Deleting and re-ingesting a %d-dataset batch; %d/%d complete.",
|
|
344
|
+
len(to_process),
|
|
345
|
+
n_current,
|
|
346
|
+
self.n_datasets,
|
|
347
|
+
)
|
|
348
|
+
sql_registry: SqlRegistry = self.butler._registry # type: ignore[attr-defined]
|
|
349
|
+
expanded_refs = sql_registry.expand_refs(to_process)
|
|
350
|
+
# We need to pass predict=True to keep QBB/FileDatastore from wasting
|
|
351
|
+
# time doing existence checks, since ResourcePath.mremove will ignore
|
|
352
|
+
# nonexistent files anyway.
|
|
353
|
+
original_uris = list(
|
|
354
|
+
itertools.chain.from_iterable(
|
|
355
|
+
ref_uris.iter_all() for ref_uris in qbb.get_many_uris(expanded_refs, predict=True).values()
|
|
356
|
+
)
|
|
357
|
+
)
|
|
358
|
+
removal_status = ResourcePath.mremove(original_uris, do_raise=False)
|
|
359
|
+
for path, status in removal_status.items():
|
|
360
|
+
if not status.success and not isinstance(status.exception, FileNotFoundError):
|
|
361
|
+
assert status.exception is not None, "Exception should be set if success=False."
|
|
362
|
+
status.exception.add_note(f"Attempting to delete original file at {path}.")
|
|
363
|
+
raise status.exception
|
|
364
|
+
file_dataset = FileDataset(refs=expanded_refs, path=direct_uri, formatter=ProvenanceFormatter)
|
|
365
|
+
self.butler.ingest(file_dataset, transfer=None)
|
|
366
|
+
if len(original_uris) == len(expanded_refs):
|
|
367
|
+
for uri, ref in zip(original_uris, expanded_refs):
|
|
368
|
+
if uri.isLocal:
|
|
369
|
+
if (
|
|
370
|
+
parent_dir := self.find_dataset_type_directory(uri.ospath, ref.datasetType.name)
|
|
371
|
+
) is not None:
|
|
372
|
+
self.directories_to_delete.add(parent_dir)
|
|
373
|
+
elif any(uri.isLocal for uri in original_uris):
|
|
374
|
+
_LOG.warning(
|
|
375
|
+
"Not attempting to delete empty metadata/log/config directories because the number "
|
|
376
|
+
"of paths (%s) did not match the number of datasets (%s).",
|
|
377
|
+
len(original_uris),
|
|
378
|
+
len(expanded_refs),
|
|
379
|
+
)
|
|
380
|
+
n = len(to_process)
|
|
381
|
+
to_process.clear()
|
|
382
|
+
return n
|
|
383
|
+
|
|
384
|
+
@staticmethod
|
|
385
|
+
def _make_ref_from_info(dataset_id: uuid.UUID, dataset_info: ProvenanceDatasetInfo) -> DatasetRef:
|
|
386
|
+
return DatasetRef(
|
|
387
|
+
dataset_info["pipeline_node"].dataset_type,
|
|
388
|
+
dataset_info["data_id"],
|
|
389
|
+
run=dataset_info["run"],
|
|
390
|
+
id=dataset_id,
|
|
391
|
+
)
|
|
392
|
+
|
|
393
|
+
def make_qbb(self) -> QuantumBackedButler:
|
|
394
|
+
dataset_types = {d.name: d.dataset_type for d in self.graph.pipeline_graph.dataset_types.values()}
|
|
395
|
+
return QuantumBackedButler.from_predicted(
|
|
396
|
+
config=self.butler_config,
|
|
397
|
+
predicted_inputs=(),
|
|
398
|
+
predicted_outputs=(),
|
|
399
|
+
dimensions=self.butler.dimensions,
|
|
400
|
+
datastore_records={},
|
|
401
|
+
dataset_types=dataset_types,
|
|
402
|
+
)
|
|
403
|
+
|
|
404
|
+
def find_dataset_type_directory(self, ospath: str, dataset_type: str) -> str | None:
|
|
405
|
+
dir_components: list[str] = []
|
|
406
|
+
for component in os.path.dirname(ospath).split(os.path.sep):
|
|
407
|
+
dir_components.append(component)
|
|
408
|
+
# If the full dataset type name is in a single directory path
|
|
409
|
+
# component, we guess that directory can only have datasets of
|
|
410
|
+
# that type.
|
|
411
|
+
if dataset_type in component:
|
|
412
|
+
return os.path.sep.join(dir_components)
|
|
413
|
+
return None
|
|
@@ -37,12 +37,16 @@ from typing import IO, ClassVar, Generic, TypeVar
|
|
|
37
37
|
from ..pipeline_graph import NodeType
|
|
38
38
|
from ._common import BaseQuantumGraph, BipartiteEdgeInfo, DatasetInfo, QuantumInfo
|
|
39
39
|
|
|
40
|
+
# We use the old generic syntax in this module because for some reason the new
|
|
41
|
+
# one confused Sphinx (or one of its plugins), even though it seems fine with
|
|
42
|
+
# it in other places. We can try again when we're ready to remove types from
|
|
43
|
+
# the docstrings of annotated functions, in case that matters.
|
|
40
44
|
_G = TypeVar("_G", bound=BaseQuantumGraph, contravariant=True)
|
|
41
45
|
_Q = TypeVar("_Q", bound=QuantumInfo, contravariant=True)
|
|
42
46
|
_D = TypeVar("_D", bound=DatasetInfo, contravariant=True)
|
|
43
47
|
|
|
44
48
|
|
|
45
|
-
class QuantumGraphVisualizer(Generic[_G, _Q, _D]):
|
|
49
|
+
class QuantumGraphVisualizer(Generic[_G, _Q, _D]): # noqa: UP046
|
|
46
50
|
"""A base class for exporting quantum graphs to graph-visualization
|
|
47
51
|
languages.
|
|
48
52
|
|
|
@@ -380,8 +380,6 @@ class QuantumGraphBuilder(ABC):
|
|
|
380
380
|
|
|
381
381
|
Parameters
|
|
382
382
|
----------
|
|
383
|
-
metadata : `~collections.abc.Mapping`, optional
|
|
384
|
-
Flexible metadata to add to the quantum graph.
|
|
385
383
|
attach_datastore_records : `bool`, optional
|
|
386
384
|
Whether to include datastore records in the graph. Required for
|
|
387
385
|
`lsst.daf.butler.QuantumBackedButler` execution.
|
|
@@ -680,6 +678,26 @@ class QuantumGraphBuilder(ABC):
|
|
|
680
678
|
"Dropping task %s because no quanta remain%s.", task_node.label, message_parenthetical
|
|
681
679
|
)
|
|
682
680
|
skeleton.remove_task(task_node.label)
|
|
681
|
+
if len(no_work_quanta) > len(remaining_quanta):
|
|
682
|
+
only_overall_inputs = self._get_task_inputs_if_overall_only(task_node)
|
|
683
|
+
self.log.warning(
|
|
684
|
+
"More than half of %s quanta had no work to do given available inputs.\n"
|
|
685
|
+
"A query constraint on one of %s may yield a much faster build.",
|
|
686
|
+
task_node.label,
|
|
687
|
+
only_overall_inputs,
|
|
688
|
+
)
|
|
689
|
+
|
|
690
|
+
def _get_task_inputs_if_overall_only(self, task_node: TaskNode) -> list[str] | None:
|
|
691
|
+
"""If the given task consumes only overall-inputs, return their names.
|
|
692
|
+
Otherwise return `None`.
|
|
693
|
+
"""
|
|
694
|
+
result: list[str] = []
|
|
695
|
+
for read_edge in task_node.inputs.values():
|
|
696
|
+
if self._pipeline_graph.producer_of(read_edge.parent_dataset_type_name) is None:
|
|
697
|
+
result.append(read_edge.parent_dataset_type_name)
|
|
698
|
+
else:
|
|
699
|
+
return None
|
|
700
|
+
return result
|
|
683
701
|
|
|
684
702
|
def _skip_quantum_if_metadata_exists(
|
|
685
703
|
self, task_node: TaskNode, quantum_key: QuantumKey, skeleton: QuantumGraphSkeleton
|
|
@@ -887,11 +905,6 @@ class QuantumGraphBuilder(ABC):
|
|
|
887
905
|
Identifier for this quantum in the graph.
|
|
888
906
|
skeleton : `.quantum_graph_skeleton.QuantumGraphSkeleton`
|
|
889
907
|
Preliminary quantum graph, to be modified in-place.
|
|
890
|
-
skypix_bounds_builder : `~prerequisite_helpers.SkyPixBoundsBuilder`
|
|
891
|
-
An object that accumulates the appropriate spatial bounds for a
|
|
892
|
-
quantum.
|
|
893
|
-
timespan_builder : `~prerequisite_helpers.TimespanBuilder`
|
|
894
|
-
An object that accumulates the appropriate timespan for a quantum.
|
|
895
908
|
|
|
896
909
|
Returns
|
|
897
910
|
-------
|
|
@@ -1095,11 +1108,13 @@ class QuantumGraphBuilder(ABC):
|
|
|
1095
1108
|
to `lsst.daf.butler.DatastoreRecordData`, as used by
|
|
1096
1109
|
`lsst.daf.butler.Quantum`.
|
|
1097
1110
|
"""
|
|
1111
|
+
self.log.info("Fetching and attaching datastore records for all overall inputs.")
|
|
1098
1112
|
overall_inputs = skeleton.extract_overall_inputs()
|
|
1099
1113
|
exported_records = self.butler._datastore.export_records(overall_inputs.values())
|
|
1100
1114
|
for task_label in self._pipeline_graph.tasks:
|
|
1101
1115
|
if not skeleton.has_task(task_label):
|
|
1102
1116
|
continue
|
|
1117
|
+
self.log.verbose("Fetching and attaching datastore records for task %s.", task_label)
|
|
1103
1118
|
task_init_key = skeleton.get_task_init_node(task_label)
|
|
1104
1119
|
init_input_ids = {
|
|
1105
1120
|
ref.id
|
|
@@ -1142,7 +1157,7 @@ class QuantumGraphBuilder(ABC):
|
|
|
1142
1157
|
"outputs" attributes on all quantum nodes, as added by
|
|
1143
1158
|
`_resolve_task_quanta`, as well as a "datastore_records" attribute
|
|
1144
1159
|
as added by `_attach_datastore_records`.
|
|
1145
|
-
metadata :
|
|
1160
|
+
metadata : `~collections.abc.Mapping`
|
|
1146
1161
|
Flexible metadata to add to the graph.
|
|
1147
1162
|
|
|
1148
1163
|
Returns
|
|
@@ -1152,12 +1167,14 @@ class QuantumGraphBuilder(ABC):
|
|
|
1152
1167
|
"""
|
|
1153
1168
|
from .graph import QuantumGraph
|
|
1154
1169
|
|
|
1170
|
+
self.log.info("Transforming graph skeleton into a QuantumGraph instance.")
|
|
1155
1171
|
quanta: dict[TaskDef, set[Quantum]] = {}
|
|
1156
1172
|
init_inputs: dict[TaskDef, Iterable[DatasetRef]] = {}
|
|
1157
1173
|
init_outputs: dict[TaskDef, Iterable[DatasetRef]] = {}
|
|
1158
1174
|
for task_def in self._pipeline_graph._iter_task_defs():
|
|
1159
1175
|
if not skeleton.has_task(task_def.label):
|
|
1160
1176
|
continue
|
|
1177
|
+
self.log.verbose("Transforming graph skeleton nodes for task %s.", task_def.label)
|
|
1161
1178
|
task_node = self._pipeline_graph.tasks[task_def.label]
|
|
1162
1179
|
task_init_key = skeleton.get_task_init_node(task_def.label)
|
|
1163
1180
|
task_init_state = skeleton[task_init_key]
|
|
@@ -1198,7 +1215,8 @@ class QuantumGraphBuilder(ABC):
|
|
|
1198
1215
|
ref = skeleton.get_dataset_ref(dataset_key)
|
|
1199
1216
|
assert ref is not None, "Global init input refs should be resolved already."
|
|
1200
1217
|
global_init_outputs.append(ref)
|
|
1201
|
-
|
|
1218
|
+
self.log.verbose("Invoking QuantumGraph class constructor.")
|
|
1219
|
+
result = QuantumGraph(
|
|
1202
1220
|
quanta,
|
|
1203
1221
|
metadata=all_metadata,
|
|
1204
1222
|
universe=self.universe,
|
|
@@ -1207,6 +1225,8 @@ class QuantumGraphBuilder(ABC):
|
|
|
1207
1225
|
globalInitOutputs=global_init_outputs,
|
|
1208
1226
|
registryDatasetTypes=registry_dataset_types,
|
|
1209
1227
|
)
|
|
1228
|
+
self.log.info("Graph build complete.")
|
|
1229
|
+
return result
|
|
1210
1230
|
|
|
1211
1231
|
@final
|
|
1212
1232
|
@timeMethod
|
|
@@ -1243,6 +1263,7 @@ class QuantumGraphBuilder(ABC):
|
|
|
1243
1263
|
PredictedQuantumGraphComponents,
|
|
1244
1264
|
)
|
|
1245
1265
|
|
|
1266
|
+
self.log.info("Transforming graph skeleton into PredictedQuantumGraph components.")
|
|
1246
1267
|
components = PredictedQuantumGraphComponents(pipeline_graph=self._pipeline_graph)
|
|
1247
1268
|
components.header.inputs = list(self.input_collections)
|
|
1248
1269
|
components.header.output_run = self.output_run
|
|
@@ -1270,6 +1291,7 @@ class QuantumGraphBuilder(ABC):
|
|
|
1270
1291
|
for task_node in self._pipeline_graph.tasks.values():
|
|
1271
1292
|
if not skeleton.has_task(task_node.label):
|
|
1272
1293
|
continue
|
|
1294
|
+
self.log.verbose("Transforming graph skeleton nodes for task %s.", task_node.label)
|
|
1273
1295
|
task_init_key = TaskInitKey(task_node.label)
|
|
1274
1296
|
init_quantum_datasets = PredictedQuantumDatasetsModel.model_construct(
|
|
1275
1297
|
quantum_id=generate_uuidv7(),
|
|
@@ -1315,8 +1337,10 @@ class QuantumGraphBuilder(ABC):
|
|
|
1315
1337
|
},
|
|
1316
1338
|
)
|
|
1317
1339
|
components.quantum_datasets[quantum_datasets.quantum_id] = quantum_datasets
|
|
1340
|
+
self.log.verbose("Building the thin summary graph.")
|
|
1318
1341
|
components.set_thin_graph()
|
|
1319
1342
|
components.set_header_counts()
|
|
1343
|
+
self.log.info("Graph build complete.")
|
|
1320
1344
|
return components
|
|
1321
1345
|
|
|
1322
1346
|
@staticmethod
|