|
| 1 | +"""Load and process the Medbullets dataset. |
| 2 | +
|
| 3 | +Dataset: HuggingFace `mkieffer/Medbullets` dataset. |
| 4 | +Each example is normalized to the fields expected by `vf.Verifiers`: |
| 5 | +{ |
| 6 | + "question": "<stem + formatted options>", # string used as the user prompt |
| 7 | + "answer": "<A|B|C|D|E>", # top-level gold letter |
| 8 | + "info": { ...original example fields... } # full source row for debugging |
| 9 | +} |
| 10 | +
|
| 11 | +- num_options=4 : loads splits `op4_train` / `op4_eval` and drops option "E" |
| 12 | +- num_options=5 : loads splits `op5_train` / `op5_eval` |
| 13 | +""" |
| 14 | + |
| 15 | +from typing import Any |
| 16 | + |
| 17 | +from datasets import load_dataset |
| 18 | + |
| 19 | + |
| 20 | +class MedBulletsDataset: |
| 21 | + """Process the MedBullets dataset.""" |
| 22 | + |
| 23 | + def __init__( |
| 24 | + self, |
| 25 | + num_train_examples: int = -1, |
| 26 | + num_eval_examples: int = -1, |
| 27 | + num_options: int = 4, |
| 28 | + ): |
| 29 | + """Initialize the MedBullets dataset processor. |
| 30 | +
|
| 31 | + Args: |
| 32 | + num_train_examples: Number of training examples to use (-1 for all) |
| 33 | + num_eval_examples: Number of evaluation examples to use (-1 for all) |
| 34 | + num_options: Number of options per question (4 or 5) |
| 35 | + """ |
| 36 | + if num_options not in [4, 5]: |
| 37 | + raise ValueError("'num_options' must be 4 or 5") |
| 38 | + |
| 39 | + self.num_train_examples = num_train_examples |
| 40 | + self.num_eval_examples = num_eval_examples |
| 41 | + self.num_options = num_options |
| 42 | + self.rng_seed = 12345 |
| 43 | + |
| 44 | + # Load and process datasets on initialization |
| 45 | + self.train_ds, self.eval_ds = self._load_and_process_datasets() |
| 46 | + |
| 47 | + def _load_and_process_datasets(self) -> tuple: |
| 48 | + """Load and process the MedBullets datasets.""" |
| 49 | + # Load the raw datasets based on number of options |
| 50 | + if self.num_options == 4: |
| 51 | + train_raw, eval_raw = load_dataset( |
| 52 | + "mkieffer/Medbullets", split=["op4_train", "op4_eval"] |
| 53 | + ) |
| 54 | + # Remove option E from 4-option datasets |
| 55 | + train_raw = self._remove_option_e(train_raw) |
| 56 | + eval_raw = self._remove_option_e(eval_raw) |
| 57 | + else: # num_options == 5 |
| 58 | + train_raw, eval_raw = load_dataset( |
| 59 | + "mkieffer/Medbullets", split=["op5_train", "op5_eval"] |
| 60 | + ) |
| 61 | + |
| 62 | + # Limit number of examples if specified |
| 63 | + if self.num_train_examples != -1: |
| 64 | + train_raw = train_raw.select( |
| 65 | + range(min(self.num_train_examples, len(train_raw))) |
| 66 | + ) |
| 67 | + if self.num_eval_examples != -1: |
| 68 | + eval_raw = eval_raw.select( |
| 69 | + range(min(self.num_eval_examples, len(eval_raw))) |
| 70 | + ) |
| 71 | + |
| 72 | + # Format datasets for verifiers |
| 73 | + train_formatted = self._format_for_verifiers(train_raw, "train") |
| 74 | + eval_formatted = self._format_for_verifiers(eval_raw, "eval") |
| 75 | + |
| 76 | + # Shuffle datasets |
| 77 | + train_formatted = train_formatted.shuffle(seed=self.rng_seed) |
| 78 | + eval_formatted = eval_formatted.shuffle(seed=self.rng_seed) |
| 79 | + |
| 80 | + return train_formatted, eval_formatted |
| 81 | + |
| 82 | + def _remove_option_e(self, dataset: Any) -> Any: |
| 83 | + """Remove option E from the dataset.""" |
| 84 | + |
| 85 | + def remove_e(ex: dict) -> dict: |
| 86 | + ex = dict(ex) |
| 87 | + ex["options"] = {k: v for k, v in ex["options"].items() if k != "E"} |
| 88 | + return ex |
| 89 | + |
| 90 | + return dataset.map(remove_e) |
| 91 | + |
| 92 | + def _format_for_verifiers(self, dataset: Any, split: str) -> Any: |
| 93 | + """Format dataset for verifiers with question, answer, and info fields.""" |
| 94 | + valid = {"A", "B", "C", "D", "E"} |
| 95 | + |
| 96 | + def format_row(row: dict) -> dict: |
| 97 | + row = dict(row) |
| 98 | + |
| 99 | + # Build the user-visible question string (stem + options) |
| 100 | + q = row.get("question", "") or "" |
| 101 | + opts = row.get("options", {}) or {} |
| 102 | + |
| 103 | + question_str = f"Question: {q}\n" |
| 104 | + for k, v in opts.items(): |
| 105 | + # Skip null values of v (for the combined dataset where E |
| 106 | + # opt for 4op is null) |
| 107 | + if v is not None and v != "": |
| 108 | + question_str += f"\n{k}: {v}" |
| 109 | + |
| 110 | + # Lift the answer top-level, normalize to a single letter |
| 111 | + ans = (row.get("answer") or "").strip().upper() |
| 112 | + if ans not in valid: |
| 113 | + # If op4 split sometimes stores 'E' or empty, coerce safely |
| 114 | + if ans == "" and "answer_letter" in row: |
| 115 | + ans = str(row["answer_letter"]).strip().upper() |
| 116 | + if ans not in valid: |
| 117 | + # Final guard: set to empty if unexpected |
| 118 | + ans = "" |
| 119 | + |
| 120 | + # Keep full original example under 'info' |
| 121 | + info = dict(row) |
| 122 | + |
| 123 | + return { |
| 124 | + "question": question_str, |
| 125 | + "answer": ans, |
| 126 | + "info": info, |
| 127 | + } |
| 128 | + |
| 129 | + return dataset.map(format_row) |
0 commit comments