Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-47328: Add QuantumBackedButler.retrieveArtifacts #1111

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changes/DM-47328.api.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added ``QuantumBackedButler.retrieve_artifacts`` method to allow dataset artifacts to be retrieved from a graph.
47 changes: 47 additions & 0 deletions python/lsst/daf/butler/_quantum_backed.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,53 @@ def retrieve_artifacts_zip(
"""
return retrieve_and_zip(refs, destination, self._datastore.retrieveArtifacts, overwrite)

def retrieve_artifacts(
self,
refs: Iterable[DatasetRef],
destination: ResourcePathExpression,
transfer: str = "auto",
preserve_path: bool = True,
overwrite: bool = False,
) -> list[ResourcePath]:
"""Retrieve the artifacts associated with the supplied refs.

Parameters
----------
refs : iterable of `DatasetRef`
The datasets for which artifacts are to be retrieved.
A single ref can result in multiple artifacts. The refs must
be resolved.
destination : `lsst.resources.ResourcePath` or `str`
Location to write the artifacts.
transfer : `str`, optional
Method to use to transfer the artifacts. Must be one of the options
supported by `~lsst.resources.ResourcePath.transfer_from()`.
"move" is not allowed.
preserve_path : `bool`, optional
If `True` the full path of the artifact within the datastore
is preserved. If `False` the final file component of the path
is used.
timj marked this conversation as resolved.
Show resolved Hide resolved
overwrite : `bool`, optional
If `True` allow transfers to overwrite existing files at the
destination.

Returns
-------
targets : `list` of `lsst.resources.ResourcePath`
URIs of file artifacts in destination location. Order is not
preserved.
timj marked this conversation as resolved.
Show resolved Hide resolved
"""
outdir = ResourcePath(destination)
artifact_map = self._datastore.retrieveArtifacts(
refs,
outdir,
transfer=transfer,
preserve_path=preserve_path,
overwrite=overwrite,
write_index=True,
)
return list(artifact_map)

def extract_provenance_data(self) -> QuantumProvenanceData:
"""Extract provenance information and datastore records from this
butler.
Expand Down
9 changes: 9 additions & 0 deletions tests/test_quantumBackedButler.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,15 @@ def test_getput(self) -> None:
self.assertEqual(set(zip_refs), set(self.output_refs))
self.assertEqual(len(index.artifact_map), 4) # Count number of artifacts in Zip.

# Retrieve them to a directory.
with tempfile.TemporaryDirectory() as tmpdir:
retrieved = qbb.retrieve_artifacts(self.output_refs, destination=tmpdir, preserve_path=False)
self.assertEqual(len(retrieved), 4)
self.assertTrue(retrieved[0].exists())
with open(os.path.join(tmpdir, ZipIndex.index_name)) as zf:
index = ZipIndex.model_validate_json(zf.read())
self.assertEqual(len(index.artifact_map), len(retrieved))

def test_getDeferred(self) -> None:
"""Test for getDeferred method"""
quantum = self.make_quantum()
Expand Down
Loading