Update dataset.py
Browse files- dataset.py +61 -0
dataset.py
CHANGED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
import datasets
|
| 4 |
+
|
| 5 |
+
# Define the dataset
|
| 6 |
+
class M3Retrieve(datasets.GeneratorBasedBuilder):
|
| 7 |
+
VERSION = datasets.Version("1.0.0")
|
| 8 |
+
|
| 9 |
+
def _info(self):
|
| 10 |
+
return datasets.DatasetInfo(
|
| 11 |
+
description="M3Retrieve: Benchmarking Multimodal Retrieval for Medicine",
|
| 12 |
+
features={
|
| 13 |
+
"queries": {
|
| 14 |
+
"_id": datasets.Value("string"),
|
| 15 |
+
"caption": datasets.Value("string"),
|
| 16 |
+
"image_path": datasets.Value("string"),
|
| 17 |
+
},
|
| 18 |
+
"corpus": {
|
| 19 |
+
"_id": datasets.Value("string"),
|
| 20 |
+
"text": datasets.Value("string"),
|
| 21 |
+
},
|
| 22 |
+
"qrels": {
|
| 23 |
+
"query-id": datasets.Value("string"),
|
| 24 |
+
"corpus-id": datasets.Value("string"),
|
| 25 |
+
"score": datasets.Value("float32"),
|
| 26 |
+
},
|
| 27 |
+
},
|
| 28 |
+
supervised_keys=None,
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
def _split_generators(self, dl_manager):
|
| 32 |
+
"""Returns SplitGenerators."""
|
| 33 |
+
data_dir = dl_manager.download_and_extract(self.config.data_dir)
|
| 34 |
+
|
| 35 |
+
return [
|
| 36 |
+
datasets.SplitGenerator(
|
| 37 |
+
name="queries",
|
| 38 |
+
gen_kwargs={"filepath": os.path.join(data_dir, "queries.jsonl"), "key": "queries"},
|
| 39 |
+
),
|
| 40 |
+
datasets.SplitGenerator(
|
| 41 |
+
name="corpus",
|
| 42 |
+
gen_kwargs={"filepath": os.path.join(data_dir, "corpus.jsonl"), "key": "corpus"},
|
| 43 |
+
),
|
| 44 |
+
datasets.SplitGenerator(
|
| 45 |
+
name="qrels",
|
| 46 |
+
gen_kwargs={"filepath": os.path.join(data_dir, "qrels/test.tsv"), "key": "qrels"},
|
| 47 |
+
),
|
| 48 |
+
]
|
| 49 |
+
|
| 50 |
+
def _generate_examples(self, filepath, key):
|
| 51 |
+
"""Yields examples as (key, example) tuples."""
|
| 52 |
+
if key in ["queries", "corpus"]:
|
| 53 |
+
with open(filepath, "r", encoding="utf-8") as f:
|
| 54 |
+
for i, line in enumerate(f):
|
| 55 |
+
data = json.loads(line)
|
| 56 |
+
yield i, data
|
| 57 |
+
elif key == "qrels":
|
| 58 |
+
with open(filepath, "r", encoding="utf-8") as f:
|
| 59 |
+
for i, line in enumerate(f):
|
| 60 |
+
query_id, corpus_id, score = line.strip().split("\t")
|
| 61 |
+
yield i, {"query-id": query_id, "corpus-id": corpus_id, "score": float(score)}
|