Skip to content

Commit e44caaa

Browse files
committed
Allow user to specify metadata
1 parent 0060e91 commit e44caaa

1 file changed

Lines changed: 37 additions & 3 deletions

File tree

alchemiscale/interface/client.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import networkx as nx
1515
from gufe import AlchemicalNetwork, Transformation, ChemicalSystem
1616
from gufe.archival import AlchemicalArchive
17-
from gufe.tokenization import GufeTokenizable, KeyedChain
17+
from gufe.tokenization import GufeTokenizable, KeyedChain, JSON_HANDLER
1818
from gufe.protocols import ProtocolResult, ProtocolDAGResult
1919
import zstandard as zstd
2020

@@ -1750,22 +1750,56 @@ def get_network_failures(
17501750
)
17511751

17521752
def get_network_archives(
1753-
self, networks: list[ScopedKey]
1753+
self, networks: list[ScopedKey], metadata: list[dict | None] = None
17541754
) -> list[AlchemicalArchive | None]:
17551755
"""Get the archives for the given ``AlchemicalNetwork`` objects.
17561756
17571757
Parameters
17581758
----------
17591759
networks
17601760
A list of ``AlchemicalNetwork`` ``ScopedKey`` values. The list must contain unique values.
1761+
metadata
1762+
Metadata to attach to the ``AlchemicalArchive``
1763+
objects. This must be a list of dictionaries that are
1764+
compatible with ``GufeTokenizable`` serialization, in the
1765+
order of the provided ``AlchemicalNetwork`` ``ScopedKey``
1766+
values. A ``None`` entry in the list will attach no
1767+
metadata to the corresponding ``AlchemicalArchive``. A
1768+
``None`` in place of the list is interpretted as a list of
1769+
``None``, which is the default.
17611770
17621771
Returns
17631772
-------
17641773
A list of ``AlchemicalArchive`` instances matching the order
17651774
of ``networks``. If a network was not found, ``None`` is
17661775
returned in its place.
1776+
1777+
Raises
1778+
------
1779+
A ``ValueError`` is raised if the provided metadata is not serializable or the lenghts of the metadata and networks lists are not the same.
1780+
17671781
"""
1768-
raise NotImplementedError
1782+
1783+
metadata = metadata or [None] * len(networks)
1784+
1785+
if len(metadata) != len(networks):
1786+
raise ValueError("metadata and networks list must be the same length")
1787+
1788+
for network, meta in zip(networks, metadata):
1789+
if meta:
1790+
try:
1791+
_ = json.dumps(meta, cls=JSON_HANDLER)
1792+
except:
1793+
raise ValueError(f"Unable to serialize metadata for {network}")
1794+
1795+
# TODO make proper request
1796+
raw_archives = get_archives(networks)
1797+
1798+
archives = []
1799+
for archive, meta in zip(raw_archives, metadata):
1800+
archives.append(archive.copy_with_replacements(metadata=meta) if metadata else archive)
1801+
1802+
return archives
17691803

17701804
def get_network_archive(self, network: ScopedKey) -> AlchemicalArchive | None:
17711805
"""Get the archive for a given ``AlchemicalNetwork``.

0 commit comments

Comments
 (0)