"""Shared helpers for ViViD-5k artifact generation scripts.""" from pathlib import Path # Consistent style for all dataset-card figures (works on HF light + dark themes). STYLE = { "accent": "#6366F1", "secondary": "#94A3B8", "text": "#334155", "grid": "#E2E8F0", "dpi": 150, "figsize": (8, 4.5), } SPLIT_FILES = {"train": "train.txt", "val": "val.txt", "test": "test.txt"} def parse_bbox(raw): """Return (x, y, w, h). COCO bboxes here are flat [x,y,w,h] or nested [[x,y,w,h]].""" if isinstance(raw, (list, tuple)) and len(raw) == 1 and isinstance(raw[0], (list, tuple)): raw = raw[0] if not (isinstance(raw, (list, tuple)) and len(raw) == 4): raise ValueError(f"Unrecognized bbox format: {raw!r}") return tuple(float(v) for v in raw) def load_splits(data_dir): """Map file_name -> split. Raises if a file appears in more than one split.""" data_dir = Path(data_dir) splits = {} for split, fname in SPLIT_FILES.items(): for line in (data_dir / fname).read_text().splitlines(): name = line.strip() if not name: continue if name in splits: raise ValueError(f"{name} appears in both {splits[name]} and {split}") splits[name] = split return splits def apply_style(ax): """Apply the shared figure style to a matplotlib Axes.""" s = STYLE ax.spines[["top", "right"]].set_visible(False) for spine in ("left", "bottom"): ax.spines[spine].set_color(s["text"]) ax.tick_params(colors=s["text"], labelsize=9) ax.yaxis.grid(True, color=s["grid"], linewidth=0.8) ax.set_axisbelow(True) ax.xaxis.label.set_color(s["text"]) ax.yaxis.label.set_color(s["text"]) ax.title.set_color(s["text"])