-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: dataset loading and standardize naming
- Loading branch information
Leonardo Schettini
committed
Dec 17, 2024
1 parent
bb15f35
commit 792fb4f
Showing
4 changed files
with
153 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import json | ||
import os | ||
from typing import Dict, List | ||
|
||
from datasets import DatasetDict, load_dataset | ||
|
||
from helm.benchmark.scenarios.scenario import ( | ||
CORRECT_TAG, | ||
TEST_SPLIT, | ||
TRAIN_SPLIT, | ||
Input, | ||
Instance, | ||
Output, | ||
Reference, | ||
Scenario, | ||
) | ||
from helm.common.general import ensure_directory_exists | ||
|
||
|
||
class MedCalcBenchScenario(Scenario): | ||
""" | ||
MedCalcBench scenario: Processes a medical calculation dataset with explanations. | ||
Each record in the dataset has: | ||
- Row Number | ||
- Calculator ID | ||
- Calculator Name | ||
- Category | ||
- Output Type | ||
- Note ID | ||
- Note Type | ||
- Question | ||
- Ground Truth Explanation | ||
- Patient Note | ||
- Relevant Entities | ||
- Lower Limit | ||
- Upper Limit | ||
- Ground Truth Answer | ||
The output is formatted as: | ||
"The answer is <calculated value>. Steps: <explanation>" | ||
""" | ||
|
||
HUGGING_FACE_DATASET_PATH: str = "ncbi/MedCalc-Bench-v1.0" | ||
|
||
# TODO: Add a base url | ||
DATASET_DOWNLOAD_BASE_URL: str = "" | ||
|
||
name = "medcalcbench" | ||
description = "Medical calculation questions with step-by-step explanations." | ||
tags = ["reasoning", "medicine", "calculation"] | ||
|
||
def get_instances(self, output_path: str) -> List[Instance]: | ||
data_path: str = os.path.join(output_path, "data") | ||
ensure_directory_exists(data_path) | ||
dataset: DatasetDict = load_dataset(self.HUGGING_FACE_DATASET_PATH) | ||
|
||
splits = {TRAIN_SPLIT: "train", TEST_SPLIT: "test"} | ||
instances: List[Instance] = [] | ||
for ( | ||
helm_split_name, | ||
dataset_split_name, | ||
) in splits.items(): # Iterate over the splits | ||
split_data = dataset[dataset_split_name] | ||
|
||
for example in split_data: | ||
question = example["Question"] | ||
patient_note = example["Patient Note"] | ||
|
||
input_text = ( | ||
f"Patient Note:\n\n{patient_note}\n\nQuestion:\n\n{question}" | ||
) | ||
|
||
# Format the final answer with explanation | ||
instances.append( | ||
Instance( | ||
input=Input(text=input_text), | ||
references=[ | ||
Reference( | ||
Output(text=example["Ground Truth Answer"]), | ||
tags=[CORRECT_TAG], | ||
) | ||
], | ||
split=helm_split_name, | ||
extra_data={ | ||
"id": example["Row Number"], | ||
"relevant_entities": example["Relevant Entities"], | ||
"lower_limit": example["Lower Limit"], | ||
"upper_limit": example["Upper Limit"], | ||
"calculator_id": example["Calculator ID"], | ||
"ground_truth": example["Ground Truth Answer"], | ||
}, | ||
) | ||
) | ||
|
||
return instances |