|
| 1 | +"""Load and process the MedQA dataset. |
| 2 | +
|
| 3 | +Dataset: HuggingFace `GBaker/MedQA-USMLE-4-options` dataset. |
| 4 | +Each example is normalized to the following fields: |
| 5 | +{ |
| 6 | + "question": "<question + formatted options>", # string used as the user prompt |
| 7 | + "answer": "<A|B|C|D>", # top-level gold letter |
| 8 | + "info": { ...original example fields... } # full source row for debugging |
| 9 | +} |
| 10 | +""" |
| 11 | + |
| 12 | +from typing import Any |
| 13 | + |
| 14 | +from datasets import load_dataset |
| 15 | + |
| 16 | + |
| 17 | +class MedQADataset: |
| 18 | + """Process the MedQA dataset.""" |
| 19 | + |
| 20 | + def __init__( |
| 21 | + self, |
| 22 | + num_train_examples: int = -1, |
| 23 | + num_test_examples: int = -1, |
| 24 | + ): |
| 25 | + """Initialize the MedQA dataset processor. |
| 26 | +
|
| 27 | + Args: |
| 28 | + num_train_examples: Number of training examples to use (-1 for all) |
| 29 | + num_test_examples: Number of test examples to use (-1 for all) |
| 30 | + """ |
| 31 | + self.num_train_examples = num_train_examples |
| 32 | + self.num_test_examples = num_test_examples |
| 33 | + self.rng_seed = 12345 |
| 34 | + |
| 35 | + # Load and process datasets on initialization |
| 36 | + self.train_ds, self.test_ds = self._load_and_process_datasets() |
| 37 | + |
| 38 | + def _load_and_process_datasets(self) -> tuple: |
| 39 | + """Load and process the MedQA datasets.""" |
| 40 | + # Load the raw datasets |
| 41 | + ds = load_dataset("GBaker/MedQA-USMLE-4-options") |
| 42 | + train_raw = ds["train"] |
| 43 | + test_raw = ds["test"] |
| 44 | + |
| 45 | + # Limit number of examples if specified |
| 46 | + if self.num_train_examples != -1: |
| 47 | + train_raw = train_raw.select( |
| 48 | + range(min(self.num_train_examples, len(train_raw))) |
| 49 | + ) |
| 50 | + if self.num_test_examples != -1: |
| 51 | + test_raw = test_raw.select( |
| 52 | + range(min(self.num_test_examples, len(test_raw))) |
| 53 | + ) |
| 54 | + |
| 55 | + # Format datasets for verifiers |
| 56 | + train_formatted = self._format_for_verifiers(train_raw, "train") |
| 57 | + test_formatted = self._format_for_verifiers(test_raw, "test") |
| 58 | + |
| 59 | + # Shuffle datasets |
| 60 | + train_formatted = train_formatted.shuffle(seed=self.rng_seed) |
| 61 | + test_formatted = test_formatted.shuffle(seed=self.rng_seed) |
| 62 | + |
| 63 | + return train_formatted, test_formatted |
| 64 | + |
| 65 | + def _format_for_verifiers(self, dataset: Any, split: str) -> Any: |
| 66 | + """Format dataset for verifiers with question, answer, and info fields.""" |
| 67 | + valid = {"A", "B", "C", "D"} |
| 68 | + |
| 69 | + def format_row(row: dict) -> dict: |
| 70 | + row = dict(row) |
| 71 | + |
| 72 | + # Build the user-visible question string (question + options) |
| 73 | + q = row.get("question", "") or "" |
| 74 | + opts = row.get("options", {}) or {} |
| 75 | + |
| 76 | + question_str = f"Question: {q}\n" |
| 77 | + for k, v in opts.items(): |
| 78 | + # Skip null or empty values |
| 79 | + if v is not None and v != "": |
| 80 | + question_str += f"\n{k}. {v}" |
| 81 | + |
| 82 | + # Lift the answer top-level, normalize to a single letter |
| 83 | + ans = (row.get("answer_idx") or "").strip().upper() |
| 84 | + if ans not in valid: |
| 85 | + # Final guard: set to empty if unexpected |
| 86 | + ans = "" |
| 87 | + |
| 88 | + # Keep full original example under 'info' |
| 89 | + info = dict(row) |
| 90 | + |
| 91 | + return { |
| 92 | + "question": question_str, |
| 93 | + "answer": ans, |
| 94 | + "info": info, |
| 95 | + } |
| 96 | + |
| 97 | + return dataset.map(format_row) |
0 commit comments