""" Few-shot split utilities and support set selection. Provides: - Reproducible train/val/test split creation - Stratified few-shot subset selection (1-shot, 5-shot, 10-shot per class) """ import os import glob import random import numpy as np from pathlib import Path from typing import Dict, List, Tuple, Optional from collections import defaultdict from src.data.yolo_to_masks import parse_yolo_label def get_class_to_samples(labels_dir: str, images_dir: str) -> Dict[int, List[str]]: """ Map each class to the list of image stems containing that class. Returns: Dict mapping class_id → list of stems """ class_to_stems = defaultdict(list) label_files = sorted(glob.glob(os.path.join(labels_dir, '*.txt'))) for label_path in label_files: stem = Path(label_path).stem # Verify image exists img_exists = False for ext in ['.jpeg', '.jpg', '.png']: if os.path.exists(os.path.join(images_dir, stem + ext)): img_exists = True break if not img_exists: continue annotations = parse_yolo_label(label_path) classes_in_image = set(a[0] for a in annotations) for cls_id in classes_in_image: class_to_stems[cls_id].append(stem) return dict(class_to_stems) def select_few_shot_support(labels_dir: str, images_dir: str, n_shots: int, seed: int = 42, strategy: str = 'random') -> Tuple[List[str], List[str]]: """ Select a few-shot support set, stratified by class. Args: labels_dir: Path to labels images_dir: Path to images n_shots: Number of examples per class seed: Random seed strategy: 'random' or 'diverse' Returns: support_stems: List of stems selected as support query_stems: Remaining stems for evaluation """ rng = random.Random(seed) class_to_stems = get_class_to_samples(labels_dir, images_dir) support_stems = set() for cls_id, stems in sorted(class_to_stems.items()): available = [s for s in stems if s not in support_stems] if strategy == 'random': selected = rng.sample(available, min(n_shots, len(available))) elif strategy == 'diverse': # Select diverse examples (spread across the stem ID range) indices = np.linspace(0, len(available) - 1, min(n_shots, len(available))) selected = [available[int(i)] for i in indices] else: raise ValueError(f"Unknown strategy: {strategy}") support_stems.update(selected) # All stems all_stems = set() for stems in class_to_stems.values(): all_stems.update(stems) query_stems = sorted(all_stems - support_stems) support_stems = sorted(support_stems) return support_stems, query_stems def print_split_statistics(labels_dir: str, images_dir: str, stems: List[str], name: str): """Print class distribution for a set of stems.""" CLASS_NAMES = [ 'giant cell tumor', 'multiple osteochondromas', 'osteochondroma', 'osteofibroma', 'osteosarcoma', 'other bt', 'other mt', 'simple bone cyst', 'synovial osteochondroma' ] class_counts = defaultdict(int) for stem in stems: label_path = os.path.join(labels_dir, stem + '.txt') if os.path.exists(label_path): annotations = parse_yolo_label(label_path) for cls_id, _, _, _, _ in annotations: class_counts[cls_id] += 1 print(f"\n{name} ({len(stems)} images):") for cls_id in range(9): count = class_counts.get(cls_id, 0) print(f" {CLASS_NAMES[cls_id]:30s}: {count}") if __name__ == '__main__': root = '.' labels_dir = os.path.join(root, 'BTXRD dataset', 'labels', 'train') images_dir = os.path.join(root, 'BTXRD dataset', 'images', 'train') for n_shots in [1, 5, 10]: support, query = select_few_shot_support( labels_dir, images_dir, n_shots=n_shots ) print(f"\n{'='*60}") print(f"{n_shots}-shot selection") print(f"{'='*60}") print(f"Support set: {len(support)} images") print(f"Query set: {len(query)} images") print_split_statistics(labels_dir, images_dir, support, f"{n_shots}-shot support")