-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_ingest.py
More file actions
221 lines (183 loc) · 6.91 KB
/
run_ingest.py
File metadata and controls
221 lines (183 loc) · 6.91 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env python3
"""Run full SpatialPathDB ingestion pipeline with hardcoded patient list.
Ingests data into all 7 database configurations:
Mono, Mono-T, Mono-C, SO, SO-C, SPDB, SPDB-Z
"""
import sys
import os
import time
import json
import gc
sys.path.insert(0, os.path.dirname(__file__))
from spdb import config, schema
from spdb.ingest import download_patient, transform_patient, _copy_chunk_numpy
SELECTED_PATIENTS = [
"bcr_patient_barcode=TCGA-2F-A9KO",
"bcr_patient_barcode=TCGA-4Z-AA7R",
"bcr_patient_barcode=TCGA-5N-A9KI",
"bcr_patient_barcode=TCGA-BT-A0YX",
"bcr_patient_barcode=TCGA-BT-A42B",
"bcr_patient_barcode=TCGA-CF-A27C",
"bcr_patient_barcode=TCGA-CF-A5UA",
"bcr_patient_barcode=TCGA-CU-A0YO",
"bcr_patient_barcode=TCGA-E7-A4IJ",
"bcr_patient_barcode=TCGA-E7-A7PW",
"bcr_patient_barcode=TCGA-FD-A3B6",
"bcr_patient_barcode=TCGA-FD-A43N",
"bcr_patient_barcode=TCGA-FD-A5BT",
"bcr_patient_barcode=TCGA-FD-A62P",
"bcr_patient_barcode=TCGA-FD-A6TK",
"bcr_patient_barcode=TCGA-G2-A2EJ",
"bcr_patient_barcode=TCGA-G2-AA3B",
"bcr_patient_barcode=TCGA-GC-A3RD",
"bcr_patient_barcode=TCGA-GD-A3OP",
"bcr_patient_barcode=TCGA-GU-A764",
"bcr_patient_barcode=TCGA-GV-A3QK",
"bcr_patient_barcode=TCGA-K4-A3WU",
"bcr_patient_barcode=TCGA-K4-A6MB",
"bcr_patient_barcode=TCGA-UY-A8OB",
"bcr_patient_barcode=TCGA-XF-A8HH",
"bcr_patient_barcode=TCGA-XF-A9ST",
"bcr_patient_barcode=TCGA-XF-A9T4",
"bcr_patient_barcode=TCGA-XF-AAMJ",
"bcr_patient_barcode=TCGA-XF-AAMZ",
]
TABLES = config.ALL_TABLES
def main():
P = config.HILBERT_ORDER
T = config.BUCKET_TARGET
print(f"=== SpatialPathDB Ingestion: {len(SELECTED_PATIENTS)} slides, "
f"p={P}, T={T}, {len(TABLES)} configs ===")
# download + transform
all_metas = {}
object_counts = {}
slide_ids = []
total_objects = 0
parquet_paths = {}
for i, patient_dir in enumerate(SELECTED_PATIENTS):
print(f"\n[{i+1}/{len(SELECTED_PATIENTS)}] {patient_dir}")
try:
t0 = time.time()
path = download_patient(patient_dir)
dl = time.time() - t0
t0 = time.time()
df, meta = transform_patient(path, p=P, bucket_target=T)
tx = time.time() - t0
sid = meta["slide_id"]
slide_ids.append(sid)
all_metas[sid] = meta
object_counts[sid] = meta["num_objects"]
total_objects += meta["num_objects"]
parquet_paths[sid] = path
del df
gc.collect()
print(f" {sid}: {meta['num_objects']:,} objects "
f"({meta['image_width']:.0f}x{meta['image_height']:.0f}px) "
f"[dl={dl:.1f}s tx={tx:.1f}s]")
except Exception as e:
print(f" SKIP: {e}")
import traceback
traceback.print_exc()
print(f"\n{'='*60}")
print(f"Total: {total_objects:,} objects across {len(slide_ids)} slides")
print(f"{'='*60}")
# setup schemas
conn = schema.get_connection()
conn.autocommit = True
schema.drop_all(conn)
conn.autocommit = False
print("\nCreating schemas...")
# Unpartitioned tables
schema.create_monolithic(conn)
schema.create_monolithic(conn, config.TABLE_MONO_TUNED)
schema.create_monolithic_clustered(conn)
# Partitioned tables
schema.create_slide_only(conn)
schema.create_slide_only_clustered(conn)
schema.create_spdb(conn)
schema.create_spdb(conn, config.TABLE_SPDB_ZORDER)
for sid in slide_ids:
n = object_counts[sid]
num_buckets = max(1, n // T)
schema.add_slide_partition_so(conn, sid)
schema.add_slide_partition_soc(conn, sid)
schema.add_slide_hilbert_partitions(conn, sid, num_buckets)
schema.add_slide_hilbert_partitions(
conn, sid, num_buckets,
table_name=config.TABLE_SPDB_ZORDER,
key_col="zorder_key",
)
print(f" Partitions created for {len(slide_ids)} slides.")
# ingest slides
print(f"\nIngesting into {len(TABLES)} tables...")
t_ingest_total = time.time()
for idx, sid in enumerate(slide_ids):
t0 = time.time()
df, _ = transform_patient(parquet_paths[sid], p=P, bucket_target=T)
for tbl in TABLES:
_copy_chunk_numpy(conn, tbl, df)
elapsed = time.time() - t0
n = len(df)
rate = n * len(TABLES) / elapsed
del df
gc.collect()
print(f" [{idx+1}/{len(slide_ids)}] {sid}: {n:,} rows x {len(TABLES)} tables "
f"in {elapsed:.1f}s ({rate:.0f} rows/sec)")
total_elapsed = time.time() - t_ingest_total
total_rows = total_objects * len(TABLES)
print(f"\n Ingestion: {total_rows:,} total rows in {total_elapsed:.1f}s "
f"({total_rows/total_elapsed:.0f} rows/sec)")
# build indexes
print("\nBuilding indexes...")
t0 = time.time()
schema.index_monolithic(conn)
schema.index_monolithic(conn, config.TABLE_MONO_TUNED)
print(" Mono + Mono-T indexes done.")
print(" Clustering Mono-C (this reorders the full table by Hilbert via GiST)...")
schema.index_monolithic_clustered(conn)
print(" Mono-C CLUSTER + BRIN done.")
schema.index_slide_only(conn, slide_ids)
print(" SO indexes done.")
print(" Clustering SO-C partitions...")
schema.index_slide_only_clustered(conn, slide_ids)
print(" SO-C CLUSTER + BRIN done.")
for sid in slide_ids:
n = object_counts[sid]
num_buckets = max(1, n // T)
schema.index_spdb(conn, [sid], num_buckets)
schema.index_spdb(conn, [sid], num_buckets,
table_name=config.TABLE_SPDB_ZORDER,
key_col="zorder_key")
print(f" SPDB + SPDB-Z indexes done.")
print(f" Total index time: {time.time()-t0:.1f}s")
print("\nANALYZE...")
schema.analyze_all(conn)
# Verify
print("\nVerification:")
with conn.cursor() as cur:
for tbl in TABLES:
try:
cur.execute(f"SELECT COUNT(*) FROM {tbl}")
cnt = cur.fetchone()[0]
print(f" {tbl}: {cnt:,} rows")
except Exception as e:
print(f" {tbl}: ERROR ({e})")
conn.rollback()
# Save metadata
os.makedirs(config.RESULTS_DIR, exist_ok=True)
meta_path = os.path.join(config.RESULTS_DIR, "ingest_metadata.json")
with open(meta_path, "w") as f:
json.dump({
"slide_ids": slide_ids,
"object_counts": object_counts,
"total_objects": total_objects,
"metas": {k: {kk: str(vv) for kk, vv in v.items()} for k, v in all_metas.items()},
"hilbert_order": P,
"bucket_target": T,
"tables": TABLES,
}, f, indent=2)
print(f"\nMetadata: {meta_path}")
conn.close()
print("\n=== Ingestion Complete ===")
if __name__ == "__main__":
main()