| from __future__ import annotations | |
| import random | |
| from typing import Dict, Iterable, List | |
| def cluster_naive_order( | |
| ligand_ids: Iterable[str], | |
| cluster_map: Dict[str, int], | |
| seed: int, | |
| ) -> List[str]: | |
| """ | |
| Static cluster baseline ordering: | |
| 1) one representative per cluster (cluster round-robin), | |
| 2) then remaining members cluster-by-cluster. | |
| """ | |
| ids = [str(x) for x in ligand_ids] | |
| by_cluster: Dict[int, List[str]] = {} | |
| for lid in ids: | |
| by_cluster.setdefault(int(cluster_map.get(lid, -1)), []).append(lid) | |
| rng = random.Random(int(seed)) | |
| cluster_ids = sorted(by_cluster.keys()) | |
| rng.shuffle(cluster_ids) | |
| for cid in cluster_ids: | |
| rng.shuffle(by_cluster[cid]) | |
| order: List[str] = [] | |
| for cid in cluster_ids: | |
| if by_cluster[cid]: | |
| order.append(by_cluster[cid][0]) | |
| for cid in cluster_ids: | |
| members = by_cluster[cid][1:] | |
| order.extend(members) | |
| seen = set() | |
| out: List[str] = [] | |
| for lid in order: | |
| if lid in seen: | |
| continue | |
| seen.add(lid) | |
| out.append(lid) | |
| return out | |