Download preprocess_data/fix_data_for_pair_alignment.py from latticetower/nested-tkf-repro-workspace: direct link, hf CLI and curl.
- Browser
- Download file 2.05 kB
-
https://huggingface.co/datasets/latticetower/nested-tkf-repro-workspace/resolve/main/preprocess_data/fix_data_for_pair_alignment.py
- Command line
-
hf download hf://datasets/latticetower/nested-tkf-repro-workspace/preprocess_data/fix_data_for_pair_alignment.py
-
curl -L -o fix_data_for_pair_alignment.py https://huggingface.co/datasets/latticetower/nested-tkf-repro-workspace/resolve/main/preprocess_data/fix_data_for_pair_alignment.py
2.05 kB
| #!/usr/bin/env python3 | |
| """ | |
| Bridges a naming/shape mismatch between preprocess_data/ output and the | |
| pair_alignment/ dataloader (FullLenDset), discovered while reproducing | |
| AnnabelLarge/protein_evolution_icml_2026: | |
| 1. metadata.tsv uses 'num_dels' but FullLenDset._load_metadata expects 'num_del'. | |
| 2. FullLenDset expects a *_pair-times.tsv (pairID, branch length) which | |
| preprocess_data/clean_data.py never writes; we derive it from the | |
| TREEDIST_anc-to-desc column already present in metadata.tsv. | |
| 3. FullLenDset expects *_AAcounts.npy to be a single (alphabet_size,) vector | |
| of amino-acid emission counts summed over the whole split (used to seed | |
| the F81 equilibrium distribution). clean_data.py instead writes a | |
| per-pair (N, 3, 3) array (a different, unrelated count), which crashes | |
| the += accumulation in FullLenDset. We recompute the correct (20,) vector | |
| directly from the aligned_mats.npy amino-acid tokens (indices 3..22). | |
| """ | |
| import argparse | |
| import numpy as np | |
| import pandas as pd | |
| def fix_split(data_dir: str, split: str) -> None: | |
| meta_path = f"{data_dir}/{split}_metadata.tsv" | |
| df = pd.read_csv(meta_path, sep="\t", index_col=0) | |
| df = df.rename(columns={"num_dels": "num_del"}) | |
| df.to_csv(meta_path, sep="\t") | |
| times_path = f"{data_dir}/{split}_pair-times.tsv" | |
| df[["pairID", "TREEDIST_anc-to-desc"]].to_csv( | |
| times_path, sep="\t", header=False, index=False | |
| ) | |
| aligned = np.load(f"{data_dir}/{split}_aligned_mats.npy") # (N, L, 4) | |
| tokens = np.concatenate([aligned[:, :, 0].ravel(), aligned[:, :, 1].ravel()]) | |
| counts = np.array([(tokens == (3 + i)).sum() for i in range(20)], dtype=np.uint32) | |
| np.save(f"{data_dir}/{split}_AAcounts.npy", counts) | |
| print(f"{split}: {len(df)} pairs, AAcounts sum={counts.sum()}") | |
| if __name__ == "__main__": | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("-data_dir", required=True) | |
| ap.add_argument("-splits", nargs="+", required=True) | |
| args = ap.parse_args() | |
| for s in args.splits: | |
| fix_split(args.data_dir, s) | |