Add cluster-balanced PyTorch sampler to dataset card
Browse files
README.md
CHANGED
|
@@ -51,6 +51,114 @@ This artifact-only rebuild does not rerun Foldseek or MMseqs2. It enforces and
|
|
| 51 |
verifies the published cluster labels plus optional external sequence
|
| 52 |
verification edges when supplied.
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
## Build Metadata
|
| 55 |
|
| 56 |
```json
|
|
|
|
| 51 |
verifies the published cluster labels plus optional external sequence
|
| 52 |
verification edges when supplied.
|
| 53 |
|
| 54 |
+
## Cluster-Balanced PyTorch Sampling
|
| 55 |
+
|
| 56 |
+
The training split can be loaded with `streaming=False` and sampled as one
|
| 57 |
+
random row per sequence-cluster group per epoch. Chain mode groups by
|
| 58 |
+
`sequence_cluster_30`. Complex mode defaults to one sample per member sequence
|
| 59 |
+
cluster from `sequence_clusters_30`; set `complex_grouping="cluster_set"` to
|
| 60 |
+
sample one row per unique complex cluster signature instead.
|
| 61 |
+
|
| 62 |
+
```python
|
| 63 |
+
import collections
|
| 64 |
+
import random
|
| 65 |
+
from typing import Any, Mapping, Optional
|
| 66 |
+
|
| 67 |
+
from datasets import load_dataset
|
| 68 |
+
from torch.utils.data import DataLoader, Dataset
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
DEFAULT_REPO_ID = "Synthyra/PDB-Chain-Complex-Benchmark-Rigor"
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
class PDBClusteredDataset(Dataset):
|
| 75 |
+
"""One randomly selected row per sequence-cluster group per epoch."""
|
| 76 |
+
|
| 77 |
+
def __init__(
|
| 78 |
+
self,
|
| 79 |
+
repo_id: str = DEFAULT_REPO_ID,
|
| 80 |
+
mode: str = "chains",
|
| 81 |
+
split: str = "train",
|
| 82 |
+
seed: int = 0,
|
| 83 |
+
epoch: int = 0,
|
| 84 |
+
cluster_key: Optional[str] = None,
|
| 85 |
+
complex_grouping: str = "member_cluster",
|
| 86 |
+
load_kwargs: Optional[Mapping[str, Any]] = None,
|
| 87 |
+
) -> None:
|
| 88 |
+
mode_to_config = {
|
| 89 |
+
"single": "chains",
|
| 90 |
+
"chains": "chains",
|
| 91 |
+
"complex": "complexes",
|
| 92 |
+
"complexes": "complexes",
|
| 93 |
+
}
|
| 94 |
+
self.config = mode_to_config[mode]
|
| 95 |
+
self.seed = int(seed)
|
| 96 |
+
self.cluster_key = cluster_key or (
|
| 97 |
+
"sequence_cluster_30" if self.config == "chains" else "sequence_clusters_30"
|
| 98 |
+
)
|
| 99 |
+
self.complex_grouping = complex_grouping
|
| 100 |
+
self.dataset = load_dataset(
|
| 101 |
+
repo_id,
|
| 102 |
+
self.config,
|
| 103 |
+
split=split,
|
| 104 |
+
streaming=False,
|
| 105 |
+
**dict(load_kwargs or {}),
|
| 106 |
+
)
|
| 107 |
+
self.groups = self._build_groups()
|
| 108 |
+
self.group_keys = sorted(self.groups)
|
| 109 |
+
self.set_epoch(epoch)
|
| 110 |
+
|
| 111 |
+
def _group_keys_for_value(self, value: Any) -> tuple[tuple[str, ...], ...]:
|
| 112 |
+
if self.config == "chains":
|
| 113 |
+
return ((str(value),),)
|
| 114 |
+
|
| 115 |
+
clusters = tuple(sorted({str(cluster) for cluster in value if str(cluster)}))
|
| 116 |
+
if not clusters:
|
| 117 |
+
raise ValueError("complex row has no sequence cluster labels")
|
| 118 |
+
if self.complex_grouping == "cluster_set":
|
| 119 |
+
return (clusters,)
|
| 120 |
+
if self.complex_grouping == "member_cluster":
|
| 121 |
+
return tuple((cluster,) for cluster in clusters)
|
| 122 |
+
raise ValueError("complex_grouping must be 'member_cluster' or 'cluster_set'")
|
| 123 |
+
|
| 124 |
+
def _build_groups(self) -> dict[tuple[str, ...], list[int]]:
|
| 125 |
+
groups = collections.defaultdict(list)
|
| 126 |
+
for row_idx, value in enumerate(self.dataset[self.cluster_key]):
|
| 127 |
+
for key in self._group_keys_for_value(value):
|
| 128 |
+
groups[key].append(row_idx)
|
| 129 |
+
if not groups:
|
| 130 |
+
raise ValueError("dataset split contains no rows")
|
| 131 |
+
return dict(groups)
|
| 132 |
+
|
| 133 |
+
def set_epoch(self, epoch: int) -> None:
|
| 134 |
+
self.epoch = int(epoch)
|
| 135 |
+
rng = random.Random(self.seed + self.epoch)
|
| 136 |
+
self.indices = [rng.choice(self.groups[key]) for key in self.group_keys]
|
| 137 |
+
|
| 138 |
+
def __len__(self) -> int:
|
| 139 |
+
return len(self.indices)
|
| 140 |
+
|
| 141 |
+
def __getitem__(self, index: int) -> Mapping[str, Any]:
|
| 142 |
+
return self.dataset[self.indices[index]]
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
def identity_collate(batch: list[Mapping[str, Any]]) -> list[Mapping[str, Any]]:
|
| 146 |
+
return batch
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
chains = PDBClusteredDataset(mode="single", split="train", seed=42)
|
| 150 |
+
complexes = PDBClusteredDataset(mode="complex", split="train", seed=42)
|
| 151 |
+
|
| 152 |
+
chains_loader = DataLoader(chains, batch_size=8, shuffle=False, collate_fn=identity_collate)
|
| 153 |
+
complex_loader = DataLoader(complexes, batch_size=2, shuffle=False, collate_fn=identity_collate)
|
| 154 |
+
|
| 155 |
+
for epoch in range(3):
|
| 156 |
+
chains.set_epoch(epoch)
|
| 157 |
+
complexes.set_epoch(epoch)
|
| 158 |
+
first_chain_batch = next(iter(chains_loader))
|
| 159 |
+
first_complex_batch = next(iter(complex_loader))
|
| 160 |
+
```
|
| 161 |
+
|
| 162 |
## Build Metadata
|
| 163 |
|
| 164 |
```json
|