SpaceDG-Bench / prepare_data.py
xlzhou126's picture
Duplicate from SpaceDG/SpaceDG-Bench
f5782e3
Raw
History Blame
4.81 kB
from pathlib import Path
import ast
import shutil
import sys
import time
import pyarrow.csv as csv
import pyarrow.dataset as ds
import pyarrow.parquet as pq
parquet_path = "spacedg_bench.parquet"
tsv_path = "spacedg_bench.tsv"
out_dir = Path("tmp_images")
out_dir.mkdir(parents=True, exist_ok=True)
target_root = Path(
"images/spacedg_bench"
)
shards = sorted(Path("data").glob("spacedg_bench-*-of-*.parquet"))
if shards:
dataset = ds.dataset([str(p) for p in shards], format="parquet")
else:
if not Path(parquet_path).exists():
raise FileNotFoundError(
f"Missing parquet input. Expected shards under 'data/' or '{parquet_path}'."
)
dataset = ds.dataset(parquet_path, format="parquet")
written = 0
PRINT_EVERY = 50 # samples
seen = 0
start_t = time.time()
last_t = start_t
scanner = dataset.scanner(columns=["id", "images"], batch_size=16)
for batch in scanner.to_batches():
ids = batch.column(0)
imgs_col = batch.column(1) # list<binary> OR list<struct<bytes,path>>
for i in range(batch.num_rows):
sid = int(ids[i].as_py())
imgs = imgs_col[i]
if imgs is None:
continue
# imgs is a ListScalar; convert just this row to Python
img_list = imgs.as_py() # list[bytes] OR list[dict]
for j, item in enumerate(img_list):
out_path = out_dir / f"{sid}_{j}.jpg"
out_path.parent.mkdir(parents=True, exist_ok=True)
if not out_path.exists():
if isinstance(item, (bytes, bytearray, memoryview)):
out_path.write_bytes(bytes(item))
elif isinstance(item, dict):
b = item.get("bytes")
p = item.get("path")
if b:
out_path.write_bytes(b)
elif p:
out_path.write_bytes(Path(p).read_bytes())
else:
raise ValueError(f"Invalid image item for id={sid}: {item}")
else:
raise ValueError(f"Unknown image item type for id={sid}: {type(item)}")
written += 1
seen += 1
if seen % PRINT_EVERY == 0:
now = time.time()
dt = now - last_t
total_dt = now - start_t
rate = (PRINT_EVERY / dt) if dt > 0 else 0.0
print(
f"[progress] samples={seen}"
+ f" images_written={written} rate={rate:.1f} samples/s elapsed={total_dt:.1f}s",
file=sys.stderr,
flush=True,
)
last_t = now
print("[OK] extracted images:", written, "to:", out_dir.resolve())
# 2) Re-organize extracted images into LMUData/images/spacedg_bench/<relpath>
if not Path(tsv_path).exists():
raise FileNotFoundError(f"Missing TSV file: {tsv_path}")
read_opts = csv.ReadOptions(autogenerate_column_names=False)
parse_opts = csv.ParseOptions(delimiter="\t", quote_char='"', newlines_in_values=True)
convert_opts = csv.ConvertOptions(strings_can_be_null=True)
qa_table = csv.read_csv(tsv_path, read_options=read_opts, parse_options=parse_opts, convert_options=convert_opts)
need_cols = ["index", "image_path"]
missing = [c for c in need_cols if c not in qa_table.column_names]
if missing:
raise ValueError(f"TSV missing required columns: {missing}")
idxs = qa_table.column("index").combine_chunks().to_pylist()
img_paths = qa_table.column("image_path").combine_chunks().to_pylist()
moved = 0
skipped_existing = 0
missing_src = 0
target_root.mkdir(parents=True, exist_ok=True)
for sid, s in zip(idxs, img_paths):
if sid is None or s is None:
continue
sid = int(sid)
paths = ast.literal_eval(s) if isinstance(s, str) else []
if not isinstance(paths, list) or len(paths) not in (1, 2):
raise ValueError(f"Unexpected image_path for index={sid}: {s}")
for j, relpath in enumerate(paths):
src = out_dir / f"{sid}_{j}.jpg"
dst = target_root / relpath
dst.parent.mkdir(parents=True, exist_ok=True)
if dst.exists():
skipped_existing += 1
continue
if not src.exists():
# If the source does not exist (e.g., already moved by a previous duplicate),
# just count it and continue.
missing_src += 1
continue
shutil.move(str(src), str(dst))
moved += 1
print(
f"[OK] organized images under: {target_root}\n"
f" moved={moved} skipped_existing={skipped_existing} missing_src={missing_src}",
flush=True,
)
# 3) Remove the temporary extraction folder if empty (or remove entirely as requested)
if out_dir.exists():
shutil.rmtree(out_dir)
print(f"[OK] removed temporary folder: {out_dir}", flush=True)