File size: 2,749 Bytes
b36e4c5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | import os
import sys
import json
import numpy as np
import pandas as pd
# make src/ available
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "src")))
from data.split import (
stratified_group_splits,
disjoint_aptamer_splits,
disjoint_molecule_splits,
)
# ======================================================
# CONFIG
# ======================================================
DATASET_PATH = os.path.join(os.path.dirname(__file__), "AptaBench_dataset_v3.csv")
N_SPLITS = 5
RANDOM_STATE = 42
FRAC_TOLERANCE = 0.02 # acceptable abs deviation from expected test fraction
MAX_TRIES = 200 # retry different seeds to meet ratio
# ======================================================
# LOAD DATA
# ======================================================
df = pd.read_csv(DATASET_PATH)
# ======================================================
# MAKE OUTPUT DIR
# ======================================================
outdir = os.path.join(os.path.dirname(__file__), "splits")
os.makedirs(outdir, exist_ok=True)
# ======================================================
# HELPER TO SAVE SPLITS
# ======================================================
def save_splits(name, splits):
outpath = os.path.join(outdir, f"{name}.json")
data = []
for i, (tr, va) in enumerate(splits):
data.append({
"fold": i,
"train_idx": tr.tolist(),
"val_idx": va.tolist(),
})
with open(outpath, "w") as f:
json.dump(data, f, indent=2)
print(f"Saved {name} splits -> {outpath}")
# ======================================================
# STRATIFIED
# ======================================================
splits = stratified_group_splits(
df,
n_splits=N_SPLITS,
random_state=RANDOM_STATE,
frac_tolerance=FRAC_TOLERANCE,
max_tries=MAX_TRIES,
)
save_splits("stratified", splits)
# ======================================================
# DISJOINT APTAMER
# ======================================================
splits = disjoint_aptamer_splits(
df,
n_splits=N_SPLITS,
random_state=RANDOM_STATE,
frac_tolerance=FRAC_TOLERANCE,
max_tries=MAX_TRIES,
)
save_splits("disjoint_aptamer", splits)
# ======================================================
# DISJOINT MOLECULE
# ======================================================
splits = disjoint_molecule_splits(
df,
n_splits=N_SPLITS,
random_state=RANDOM_STATE,
scaffold_missing="separate",
frac_tolerance=FRAC_TOLERANCE,
max_tries=MAX_TRIES,
)
save_splits("disjoint_molecule", splits)
print("All splits successfully saved in dataset/splits/")
|