| |
| from __future__ import annotations |
|
|
| import argparse |
| import re |
| from pathlib import Path |
| from zipfile import ZIP_DEFLATED, ZipFile |
|
|
|
|
| IMAGE_ID_RE = re.compile(r"(?:_e)?_(\d+)\.[^.]+$") |
|
|
|
|
| def image_sort_key(path: Path) -> tuple[int, str]: |
| match = IMAGE_ID_RE.search(path.name) |
| if match: |
| return int(match.group(1)), path.name |
| return 10**12, path.name |
|
|
|
|
| def collect_first_n_images(img_dir: Path, limit: int) -> list[Path]: |
| images = [p for p in img_dir.iterdir() if p.is_file() and IMAGE_ID_RE.search(p.name)] |
| return sorted(images, key=image_sort_key)[:limit] |
|
|
|
|
| def build_zip(dataset_root: Path, output_zip: Path, image_limit: int) -> None: |
| with ZipFile(output_zip, "w", compression=ZIP_DEFLATED) as zf: |
| readme = dataset_root / "README.md" |
| if readme.exists(): |
| zf.write(readme, readme.relative_to(dataset_root)) |
|
|
| puzzle_type_info = dataset_root / "puzzle_type_info.csv" |
| if puzzle_type_info.exists(): |
| zf.write(puzzle_type_info, puzzle_type_info.relative_to(dataset_root)) |
|
|
| smart_data_dir = dataset_root / "SMART101-Data" |
| for puzzle_dir in sorted( |
| [p for p in smart_data_dir.iterdir() if p.is_dir()], |
| key=lambda p: int(p.name), |
| ): |
| csv_files = sorted(puzzle_dir.glob("*.csv")) |
| for csv_file in csv_files: |
| zf.write(csv_file, csv_file.relative_to(dataset_root)) |
|
|
| img_dir = puzzle_dir / "img" |
| if not img_dir.exists(): |
| continue |
|
|
| for image_path in collect_first_n_images(img_dir, image_limit): |
| zf.write(image_path, image_path.relative_to(dataset_root)) |
|
|
|
|
| def main() -> None: |
| parser = argparse.ArgumentParser( |
| description="Create a SMART101 zip archive with all CSVs and only the first N images per img directory." |
| ) |
| parser.add_argument( |
| "--dataset-root", |
| type=Path, |
| default=Path(__file__).resolve().parent, |
| help="Path containing README.md, puzzle_type_info.csv, and SMART101-Data/.", |
| ) |
| parser.add_argument( |
| "--output", |
| type=Path, |
| default=Path("SMART101-first20-images.zip"), |
| help="Output zip file path.", |
| ) |
| parser.add_argument( |
| "--image-limit", |
| type=int, |
| default=20, |
| help="Number of images to keep from each img directory.", |
| ) |
| args = parser.parse_args() |
|
|
| dataset_root = args.dataset_root.resolve() |
| output_zip = args.output.resolve() |
| output_zip.parent.mkdir(parents=True, exist_ok=True) |
|
|
| build_zip(dataset_root, output_zip, args.image_limit) |
| print(f"Created {output_zip}") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|