""" ECBS5200 — Shared data loading utilities. Single import for all course notebooks and scripts: from utils.data_utils import load_course_data, LABEL_LIST, NUM_LABELS Handles dataset loading, label merging, class filtering, canonical train/val/test splitting, and slice assignment. Never solve these problems twice. """ import json import re from pathlib import Path from datasets import load_dataset # --------------------------------------------------------------------------- # Paths (relative to repo root) # --------------------------------------------------------------------------- _DATA_DIR = Path(__file__).resolve().parent.parent / "data" _MERGE_MAP_PATH = _DATA_DIR / "label_merge_mapping.json" _TRAIN_INDICES_PATH = _DATA_DIR / "train_indices.json" _VAL_INDICES_PATH = _DATA_DIR / "val_indices.json" _LABEL_LIST_PATH = _DATA_DIR / "label_list.json" # --------------------------------------------------------------------------- # Constants (loaded once at import time) # --------------------------------------------------------------------------- with open(_LABEL_LIST_PATH) as f: LABEL_LIST: list[str] = json.load(f) NUM_LABELS: int = len(LABEL_LIST) # 113 LABEL_TO_ID: dict[str, int] = {label: i for i, label in enumerate(LABEL_LIST)} ID_TO_LABEL: dict[int, str] = {i: label for i, label in enumerate(LABEL_LIST)} with open(_MERGE_MAP_PATH) as f: _MERGE_MAP: dict[str, str] = { k: v for k, v in json.load(f).items() if not k.startswith("_") } # --------------------------------------------------------------------------- # Dataset name # --------------------------------------------------------------------------- DATASET_NAME = "determined-ai/consumer_complaints_medium" TEXT_COLUMN = "Consumer Complaint" LABEL_COLUMN = "Issue" MAX_LENGTH = 128 def _apply_merge(label: str) -> str: """Map a raw Issue label to its canonical merged form.""" return _MERGE_MAP.get(label, label) # --------------------------------------------------------------------------- # Slice assignment # --------------------------------------------------------------------------- _VALID_CLASSES = set(LABEL_LIST) # Rare = bottom quartile by class frequency (computed lazily) _rare_classes: set[str] | None = None def _get_rare_classes(labels: list[str]) -> set[str]: """Classes whose total count puts them in the bottom quartile.""" from collections import Counter counts = Counter(labels) sorted_counts = sorted(counts.values()) q25 = sorted_counts[len(sorted_counts) // 4] return {label for label, c in counts.items() if c <= q25} def assign_slices(text: str, label: str, rare_classes: set[str]) -> dict[str, str]: """ Assign slice tags to a single example. Returns a dict of slice names to slice values. Used for per-slice evaluation in Weeks 4-5. Slices: frequency: "rare" | "common" length: "short" | "medium" | "long" (word-count terciles: <35, 35-65, >65) redaction: "redacted" | "clean" (contains XXXX) numeric: "numeric" | "non_numeric" (contains dollar amounts or numbers with 4+ digits) """ word_count = len(text.split()) return { "frequency": "rare" if label in rare_classes else "common", "length": ( "short" if word_count < 35 else "long" if word_count > 65 else "medium" ), "redaction": "redacted" if "XXXX" in text else "clean", "numeric": ( "numeric" if re.search(r"\$\d|(?