|
14 | 14 | import networkx as nx |
15 | 15 | from gufe import AlchemicalNetwork, Transformation, ChemicalSystem |
16 | 16 | from gufe.archival import AlchemicalArchive |
17 | | -from gufe.tokenization import GufeTokenizable, KeyedChain |
| 17 | +from gufe.tokenization import GufeTokenizable, KeyedChain, JSON_HANDLER |
18 | 18 | from gufe.protocols import ProtocolResult, ProtocolDAGResult |
19 | 19 | import zstandard as zstd |
20 | 20 |
|
@@ -1750,22 +1750,56 @@ def get_network_failures( |
1750 | 1750 | ) |
1751 | 1751 |
|
1752 | 1752 | def get_network_archives( |
1753 | | - self, networks: list[ScopedKey] |
| 1753 | + self, networks: list[ScopedKey], metadata: list[dict | None] = None |
1754 | 1754 | ) -> list[AlchemicalArchive | None]: |
1755 | 1755 | """Get the archives for the given ``AlchemicalNetwork`` objects. |
1756 | 1756 |
|
1757 | 1757 | Parameters |
1758 | 1758 | ---------- |
1759 | 1759 | networks |
1760 | 1760 | 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. |
1761 | 1770 |
|
1762 | 1771 | Returns |
1763 | 1772 | ------- |
1764 | 1773 | A list of ``AlchemicalArchive`` instances matching the order |
1765 | 1774 | of ``networks``. If a network was not found, ``None`` is |
1766 | 1775 | 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 | +
|
1767 | 1781 | """ |
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 |
1769 | 1803 |
|
1770 | 1804 | def get_network_archive(self, network: ScopedKey) -> AlchemicalArchive | None: |
1771 | 1805 | """Get the archive for a given ``AlchemicalNetwork``. |
|
0 commit comments