|
| 1 | +"""Load and process the MetaMedQA dataset. |
| 2 | +
|
| 3 | +Dataset: HuggingFace `maximegmd/MetaMedQA` dataset. |
| 4 | +Each example is normalized to the fields expected by `vf.Verifiers`: |
| 5 | +{ |
| 6 | + "question": "<formatted question + 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 | + |
| 12 | +from typing import Any |
| 13 | + |
| 14 | +from datasets import load_dataset |
| 15 | + |
| 16 | + |
| 17 | +class MetaMedQADataset: |
| 18 | + """Process the MetaMedQA dataset.""" |
| 19 | + |
| 20 | + def __init__( |
| 21 | + self, |
| 22 | + split: str = "test", |
| 23 | + num_examples: int = -1, |
| 24 | + ): |
| 25 | + """Initialize the MetaMedQA dataset processor. |
| 26 | +
|
| 27 | + Args: |
| 28 | + split: Dataset split to use (train, validation, test) |
| 29 | + num_examples: Number of examples to use (-1 for all) |
| 30 | + """ |
| 31 | + self.split = split |
| 32 | + self.num_examples = num_examples |
| 33 | + self.rng_seed = 12345 |
| 34 | + |
| 35 | + # Load and process datasets on initialization |
| 36 | + self.dataset = self._load_and_process_dataset() |
| 37 | + |
| 38 | + def _load_and_process_dataset(self) -> Any: |
| 39 | + """Load and process the MetaMedQA dataset.""" |
| 40 | + # Load the raw dataset |
| 41 | + raw_ds = load_dataset("maximegmd/MetaMedQA", split=self.split) |
| 42 | + |
| 43 | + # Limit number of examples if specified |
| 44 | + if self.num_examples != -1: |
| 45 | + raw_ds = raw_ds.select(range(min(self.num_examples, len(raw_ds)))) |
| 46 | + |
| 47 | + # Format dataset for verifiers |
| 48 | + formatted_ds = self._format_for_verifiers(raw_ds) |
| 49 | + |
| 50 | + # Shuffle dataset |
| 51 | + return formatted_ds.shuffle(seed=self.rng_seed) |
| 52 | + |
| 53 | + def _build_prompt(self, question: str, options: dict) -> str: |
| 54 | + """Build prompt with question and options.""" |
| 55 | + opts = "\n".join(f"{k}. {v}" for k, v in options.items()) |
| 56 | + letters = ", ".join(sorted(options.keys())) |
| 57 | + return ( |
| 58 | + "You are a clinician. Choose exactly ONE option letter.\n\n" |
| 59 | + f"Question:\n{question}\n\n" |
| 60 | + f"Options:\n{opts}\n\n" |
| 61 | + f"Answer with ONLY the letter ({letters})." |
| 62 | + ) |
| 63 | + |
| 64 | + def _format_for_verifiers(self, dataset: Any) -> Any: |
| 65 | + """Format dataset for verifiers with question, answer, and info fields.""" |
| 66 | + valid = {"A", "B", "C", "D", "E"} |
| 67 | + |
| 68 | + def format_row(row: dict) -> dict: |
| 69 | + row = dict(row) |
| 70 | + |
| 71 | + q: str = row["question"] |
| 72 | + options: dict = row["options"] |
| 73 | + gold_text: str = row["answer"] |
| 74 | + |
| 75 | + # Find the gold letter by matching the answer text with options |
| 76 | + gold_letter = None |
| 77 | + for k, v in options.items(): |
| 78 | + if (v or "").strip().lower() == (gold_text or "").strip().lower(): |
| 79 | + gold_letter = k |
| 80 | + break |
| 81 | + |
| 82 | + # If we can't find a matching letter, return None to filter out |
| 83 | + if gold_letter is None or gold_letter not in valid: |
| 84 | + # Default to first option if no match found |
| 85 | + gold_letter = next(iter(options.keys())) |
| 86 | + |
| 87 | + # Build the user-visible question string (question + options) |
| 88 | + question_str = self._build_prompt(q, options) |
| 89 | + |
| 90 | + # Keep full original example under 'info' |
| 91 | + info = dict(row) |
| 92 | + |
| 93 | + return { |
| 94 | + "question": question_str, |
| 95 | + "answer": gold_letter, |
| 96 | + "info": info, |
| 97 | + } |
| 98 | + |
| 99 | + return dataset.map(format_row) |
0 commit comments