|
| 1 | +"""Load and process the PubMedQA dataset. |
| 2 | +
|
| 3 | +Dataset: HuggingFace `qiaojin/PubMedQA` dataset. |
| 4 | +Each example is normalized to the following fields: |
| 5 | +{ |
| 6 | + "question": "<formatted question with context>", # complete prompt with abstract |
| 7 | + "answer": "<A|B|C>", # A=yes, B=no, C=maybe |
| 8 | + "info": { ...original example fields... } # full source row for debugging |
| 9 | +} |
| 10 | +""" |
| 11 | + |
| 12 | +import json |
| 13 | +import os |
| 14 | +from typing import Any |
| 15 | + |
| 16 | +from datasets import load_dataset |
| 17 | + |
| 18 | + |
| 19 | +class PubMedQADataset: |
| 20 | + """Process the PubMedQA dataset.""" |
| 21 | + |
| 22 | + def __init__( |
| 23 | + self, |
| 24 | + num_train_examples: int = -1, |
| 25 | + num_test_examples: int = -1, |
| 26 | + ): |
| 27 | + """Initialize the PubMedQA dataset processor. |
| 28 | +
|
| 29 | + Args: |
| 30 | + num_train_examples: Number of training examples to use (-1 for all) |
| 31 | + num_test_examples: Number of test examples to use (-1 for all) |
| 32 | + """ |
| 33 | + self.num_train_examples = num_train_examples |
| 34 | + self.num_test_examples = num_test_examples |
| 35 | + self.rng_seed = 12345 |
| 36 | + self.dataset_path = "qiaojin/PubMedQA" |
| 37 | + |
| 38 | + # Load and process datasets on initialization |
| 39 | + self.train_ds, self.test_ds = self._load_and_process_datasets() |
| 40 | + |
| 41 | + def _load_and_process_datasets(self) -> tuple: |
| 42 | + """Load and process the PubMedQA datasets.""" |
| 43 | + # Load the raw datasets |
| 44 | + # pqa_artificial is the training set, pqa_labeled is the test set |
| 45 | + train_raw = load_dataset( |
| 46 | + self.dataset_path, name="pqa_artificial", split="train" |
| 47 | + ) |
| 48 | + test_raw = load_dataset(self.dataset_path, name="pqa_labeled", split="train") |
| 49 | + |
| 50 | + # Filter test set to only include human-annotated samples |
| 51 | + test_raw = self._filter_test_set(test_raw) |
| 52 | + |
| 53 | + # Limit number of examples if specified |
| 54 | + if self.num_train_examples != -1: |
| 55 | + train_raw = train_raw.select( |
| 56 | + range(min(self.num_train_examples, len(train_raw))) |
| 57 | + ) |
| 58 | + if self.num_test_examples != -1: |
| 59 | + test_raw = test_raw.select( |
| 60 | + range(min(self.num_test_examples, len(test_raw))) |
| 61 | + ) |
| 62 | + |
| 63 | + # Format datasets |
| 64 | + train_formatted = self._format_dataset(train_raw, "train") |
| 65 | + test_formatted = self._format_dataset(test_raw, "test") |
| 66 | + |
| 67 | + # Shuffle datasets |
| 68 | + train_formatted = train_formatted.shuffle(seed=self.rng_seed) |
| 69 | + test_formatted = test_formatted.shuffle(seed=self.rng_seed) |
| 70 | + |
| 71 | + return train_formatted, test_formatted |
| 72 | + |
| 73 | + def _filter_test_set(self, dataset: Any) -> Any: |
| 74 | + """Filter test set to only include human-annotated samples (500 from 1000).""" |
| 75 | + # Load the predefined test IDs |
| 76 | + here = os.path.dirname(__file__) |
| 77 | + file_path = os.path.join(here, "data", "test_ground_truth.json") |
| 78 | + |
| 79 | + try: |
| 80 | + with open(file_path) as f: |
| 81 | + test_ids = json.load(f) |
| 82 | + |
| 83 | + # Filter to only the 500 human-annotated samples |
| 84 | + return dataset.filter(lambda sample: str(sample["pubid"]) in test_ids) |
| 85 | + except FileNotFoundError: |
| 86 | + # If the file doesn't exist, return the full test set |
| 87 | + print(f"Warning: {file_path} not found. Using full test set.") |
| 88 | + return dataset |
| 89 | + |
| 90 | + def _format_dataset(self, dataset: Any, split: str) -> Any: |
| 91 | + """Format dataset with question, answer, and info fields.""" |
| 92 | + choices_map = {"yes": "A", "no": "B", "maybe": "C"} |
| 93 | + prompt_template = "Answer A for yes, B for no or C for maybe.\n\nContext: {context}\n\nQuestion: {question}\nAnswer:" |
| 94 | + |
| 95 | + def format_row(row: dict) -> dict: |
| 96 | + row = dict(row) |
| 97 | + |
| 98 | + # Extract question |
| 99 | + question_text = row.get("question", "") or "" |
| 100 | + |
| 101 | + # Extract and format context |
| 102 | + context_dict = row.get("context", {}) or {} |
| 103 | + labels = context_dict.get("labels", []) or [] |
| 104 | + contexts = context_dict.get("contexts", []) or [] |
| 105 | + |
| 106 | + # Format contexts with their labels |
| 107 | + formatted_contexts = [] |
| 108 | + for label, context in zip(labels, contexts): |
| 109 | + formatted_contexts.append(f"{label}. {context}") |
| 110 | + context_text = "\n".join(formatted_contexts) |
| 111 | + |
| 112 | + # Build complete prompt |
| 113 | + complete_prompt = prompt_template.format( |
| 114 | + context=context_text, question=question_text |
| 115 | + ) |
| 116 | + |
| 117 | + # Map final decision to letter (A/B/C) |
| 118 | + final_decision = (row.get("final_decision", "") or "").lower() |
| 119 | + answer = choices_map.get(final_decision, "") |
| 120 | + |
| 121 | + # Keep full original example under 'info' |
| 122 | + info = dict(row) |
| 123 | + |
| 124 | + return { |
| 125 | + "question": complete_prompt, |
| 126 | + "answer": answer, |
| 127 | + "info": info, |
| 128 | + } |
| 129 | + |
| 130 | + return dataset.map(format_row, load_from_cache_file=False) |
0 commit comments