leebecca commited on
Commit
6bb897c
·
verified ·
1 Parent(s): a47cca3

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. src/split_dataset.py +123 -0
src/split_dataset.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import shutil
3
+ import os
4
+ import glob
5
+
6
+ # ============================================================
7
+ # CONFIGURATION - Edit these paths as needed
8
+ # ============================================================
9
+ PDB_ROOT = "pdb"
10
+ OUTPUT_ROOT = "pdb_2021aug02"
11
+
12
+ TRAIN_CSV = "train.csv"
13
+ VALIDATION_CSV = "validation.csv"
14
+ TEST_CSV = "test.csv"
15
+
16
+ SPLITS = {
17
+ "training": TRAIN_CSV,
18
+ "validation": VALIDATION_CSV,
19
+ "test": TEST_CSV
20
+ }
21
+
22
+ # ============================================================
23
+ # HELPER FUNCTIONS
24
+ # ============================================================
25
+
26
+ def get_subfolder(name):
27
+ """
28
+ Extract subfolder from filename.
29
+ E.g., '1nb4_A' -> 'nb' (characters at index 1 and 2)
30
+ """
31
+ # Strip extension if present
32
+ base = os.path.splitext(name)[0]
33
+ return base[1:3].lower() # e.g., '1nb4_A' -> 'nb'
34
+
35
+
36
+
37
+ def read_csv_entries(csv_file):
38
+ """
39
+ Read comma-separated entries from a single-line CSV.
40
+ E.g., 5naf_A,5naf_B,5naf_C,...
41
+ """
42
+ entries = []
43
+ with open(csv_file, 'r') as f:
44
+ for line in f:
45
+ entries.extend(line.strip().split(','))
46
+ return [e.strip() for e in entries if e.strip()]
47
+
48
+ def find_pt_file(pdb_root, name):
49
+ """
50
+ Locate the .pt file given the entry name.
51
+ E.g., '1nb4_A' -> pdb_root/nb/1nb4_A.pt
52
+ """
53
+ subfolder = get_subfolder(name)
54
+ pt_path = os.path.join(pdb_root, subfolder, f"{name}.pt")
55
+ if os.path.exists(pt_path):
56
+ return pt_path, subfolder
57
+ return None, subfolder
58
+
59
+
60
+ def copy_file(src, dst_dir, filename):
61
+ """
62
+ Copy file to destination directory, creating dirs if needed.
63
+ """
64
+ os.makedirs(dst_dir, exist_ok=True)
65
+ dst = os.path.join(dst_dir, filename)
66
+ shutil.copy2(src, dst)
67
+ return dst
68
+
69
+
70
+ # ============================================================
71
+ # MAIN
72
+ # ============================================================
73
+
74
+ def main():
75
+ stats = {}
76
+
77
+ for split_name, csv_file in SPLITS.items():
78
+ print(f"\n{'='*50}")
79
+ print(f"Processing: {split_name.upper()} <- {csv_file}")
80
+ print(f"{'='*50}")
81
+
82
+ if not os.path.exists(csv_file):
83
+ print(f" [WARNING] CSV file not found: {csv_file}")
84
+ continue
85
+
86
+ entries = read_csv_entries(csv_file)
87
+ print(f" Total entries found in CSV: {len(entries)}")
88
+
89
+ copied = 0
90
+ missing = []
91
+
92
+ for name in entries:
93
+ src_path, subfolder = find_pt_file(PDB_ROOT, name)
94
+
95
+ if src_path:
96
+ # Destination: e.g., OUTPUT_ROOT/validation/nb/1nb4_A.pt
97
+ dst_dir = os.path.join(OUTPUT_ROOT, split_name, subfolder)
98
+ copy_file(src_path, dst_dir, f"{name}.pt")
99
+ copied += 1
100
+ else:
101
+ missing.append(name)
102
+
103
+ print(f"Copied : {copied}")
104
+ print(f"Missing : {len(missing)}")
105
+
106
+ if missing:
107
+ missing_log = os.path.join(OUTPUT_ROOT, f"{split_name}_missing.txt")
108
+ with open(missing_log, "w") as f:
109
+ f.write("\n".join(missing))
110
+ print(f"Missing entries logged to: {missing_log}")
111
+
112
+ stats[split_name] = {"copied": copied, "missing": len(missing)}
113
+
114
+ # ---- Summary ----
115
+ print(f"\n{'='*50}")
116
+ print("SUMMARY")
117
+ print(f"{'='*50}")
118
+ for split, s in stats.items():
119
+ print(f" {split:<12}: {s['copied']} copied, {s['missing']} missing")
120
+
121
+
122
+ if __name__ == "__main__":
123
+ main()