File size: 4,065 Bytes
dd7e97a
 
 
 
 
 
 
 
2a307d8
 
258f73c
2a307d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ba814a
2a307d8
 
df3de85
2a307d8
 
 
2ba814a
 
591a7ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd7e97a
8b7f5c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
591a7ac
dd7e97a
 
2a307d8
 
 
dd7e97a
 
 
2ba814a
dd7e97a
 
 
2ba814a
dd7e97a
 
 
2ba814a
dd7e97a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import json
import os
import datasets

# Define the dataset
class M3Retrieve(datasets.GeneratorBasedBuilder):
    VERSION = datasets.Version("1.0.0")

    # Define available subfolders (datasets)
    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",
    ]

    # Define Builder Configs for each subfolder
    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={
    #             "queries": {
    #                 "_id": datasets.Value("string"),
    #                 "caption": datasets.Value("string"),
    #                 "image_path": datasets.Value("string"),
    #             },
    #             "corpus": {
    #                 "_id": datasets.Value("string"),
    #                 "text": datasets.Value("string"),
    #             },
    #             "qrels": {
    #                 "query-id": datasets.Value("string"),
    #                 "corpus-id": datasets.Value("string"),
    #                 "score": datasets.Value("float32"),
    #             },
    #         },
    #         supervised_keys=None,
    #     )

    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)}