|
import json |
|
import os |
|
import datasets |
|
|
|
|
|
class M3Retrieve(datasets.GeneratorBasedBuilder): |
|
VERSION = datasets.Version("1.0.0") |
|
|
|
|
|
SUBFOLDERS = [ |
|
"Anatomy and Physiology", |
|
"Cardiology", |
|
"Dermatology", |
|
"Endocrinology_and_Diabetes", |
|
"Gastroenterology", |
|
"Hematology", |
|
"Microbiology_and_Cell_Biology", |
|
"Miscellaneous", |
|
"Neurology_and_Neuroscience", |
|
"Ophthalmology_and_Sensory_Systems", |
|
"Orthopedics_and_Musculoskeletal", |
|
"Pharmacology", |
|
"Psychiatry_and_Mental_Health", |
|
"Pubmed", |
|
"Radiology_and_Imaging", |
|
"Reproductive_System", |
|
"Respiratory_and_Pulmonology", |
|
"Surgical_Specialties", |
|
] |
|
|
|
|
|
BUILDER_CONFIGS = [ |
|
datasets.BuilderConfig( |
|
name=subfolder, |
|
version=datasets.Version("1.0.0"), |
|
description=f"Dataset for {subfolder.replace('_', ' ')}" |
|
) |
|
for subfolder in SUBFOLDERS |
|
] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _info(self): |
|
return datasets.DatasetInfo( |
|
description="M3Retrieve: Benchmarking Multimodal Retrieval for Medicine", |
|
features=datasets.Features( |
|
{ |
|
"_id": datasets.Value("string"), |
|
"caption": datasets.Value("string"), |
|
"image_path": datasets.Value("string"), |
|
"text": datasets.Value("string"), |
|
"query-id": datasets.Value("string"), |
|
"corpus-id": datasets.Value("string"), |
|
"score": datasets.Value("float32"), |
|
} |
|
), |
|
supervised_keys=None, |
|
) |
|
|
|
|
|
def _split_generators(self, dl_manager): |
|
"""Returns SplitGenerators for the selected subfolder""" |
|
data_dir = os.path.join(dl_manager.download_and_extract(self.config.data_dir), self.config.name) |
|
|
|
return [ |
|
datasets.SplitGenerator( |
|
name="queries", |
|
gen_kwargs={"filepath": os.path.join(data_dir, "queries.jsonl"), "key": "queries"}, |
|
), |
|
datasets.SplitGenerator( |
|
name="corpus", |
|
gen_kwargs={"filepath": os.path.join(data_dir, "corpus.jsonl"), "key": "corpus"}, |
|
), |
|
datasets.SplitGenerator( |
|
name="qrels", |
|
gen_kwargs={"filepath": os.path.join(data_dir, "qrels/test.tsv"), "key": "qrels"}, |
|
), |
|
] |
|
|
|
def _generate_examples(self, filepath, key): |
|
"""Yields examples as (key, example) tuples.""" |
|
if key in ["queries", "corpus"]: |
|
with open(filepath, "r", encoding="utf-8") as f: |
|
for i, line in enumerate(f): |
|
data = json.loads(line) |
|
yield i, data |
|
elif key == "qrels": |
|
with open(filepath, "r", encoding="utf-8") as f: |
|
for i, line in enumerate(f): |
|
query_id, corpus_id, score = line.strip().split("\t") |
|
yield i, {"query-id": query_id, "corpus-id": corpus_id, "score": float(score)} |
|
|