File size: 6,195 Bytes
e25ca88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8edc1c9
 
 
e25ca88
8edc1c9
e25ca88
8edc1c9
e25ca88
8edc1c9
e25ca88
 
 
8edc1c9
e25ca88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8edc1c9
e25ca88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8edc1c9
 
 
 
 
 
e25ca88
 
 
 
 
 
 
 
 
 
 
8fd26b4
e25ca88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fec07f5
e25ca88
 
 
 
 
 
 
 
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
MedQA Textbook (English) with focused emphasis on domain of Clinical Medicine and other subsets relevant for use with
Clinical Ontologies.

Textbooks:

Anatomy_Gray.txt                Clinical medicine
Biochemistry_Lippincott.txt     Basic biology
Cell_Biology_Alberts.txt        Basic biology
First_Aid_Step1.txt             Clinical medicine
First_Aid_Step2.txt             Clinical medicine
Gynecology_Novak.txt            Clinical medicine
Histology_Ross.txt              Basic biology
Immunology_Janeway.txt          Allergy and immunology      Clinical medicine
InternalMed_Harrison.txt        Clinical medicine
Neurology_Adams.txt             Neurology                   Clinical medicine
Obstentrics_Williams.txt        OBGYN                       Clinical medicine
Pathology_Robbins.txt           Basic biology
Pathoma_Husain.txt              Pathology                   Clinical medicine
Pediatrics_Nelson.txt           Pediatrics                  Clinical medicine
Pharmacology_Katzung.txt        Pharmacology
Physiology_Levy.txt             Basic biology
Psichiatry_DSM-5.txt            Psychiatry
Surgery_Schwartz.txt            Surgery                     Clinical medicine

"""

SUBJECT_SUBSETS = {
    "core_clinical":
        ["Anatomy_Gray", "First_Aid_Step1", "First_Aid_Step2", "Immunology_Janeway",
         "InternalMed_Harrison", "Neurology_Adams", "Obstentrics_Williams", "Pathoma_Husain", "Pediatrics_Nelson",
         "Surgery_Schwartz"],
    "basic_biology":
        ["Biochemistry_Lippincott", "Cell_Biology_Alberts", "Histology_Ross", "Pathology_Robbins", "Physiology_Levy"],
    "pharmacology":
        ["Pharmacology_Katzung"],
    "psychiatry":
        ["Psichiatry_DSM-5"]
}

from pathlib import Path
import json
import datasets

CHUNK_OVERLAP = 20
MAX_CHUNK_SIZE = 800

# TODO: Add description of the dataset here
# You can copy an official description
_DESCRIPTION = """\
MedQA Textbook (English) with emphasis on domain of Clinical Medicine and other subsets.
"""

# TODO: Add a link to an official homepage for the dataset here
_HOMEPAGE = "https://github.com/jind11/MedQA"

# TODO: Add the licence for the dataset here if you can find it
_LICENSE = """
MIT License

Copyright (c) 2022 Di Jin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

_URLS = ["textbooks_en_jsonl.zip"]

_CITATION = """\
@article{jin2021disease,
  title={What disease does this patient have? a large-scale open domain question answering dataset from medical exams},
  author={Jin, Di and Pan, Eileen and Oufattole, Nassim and Weng, Wei-Hung and Fang, Hanyi and Szolovits, Peter},
  journal={Applied Sciences},
  volume={11},
  number={14},
  pages={6421},
  year={2021},
  publisher={MDPI}
}
"""


def get_med_qa_textbooks(location, subset_name):
    for textbook_content in location.glob('*.jsonl'):
        textbook_name = textbook_content.name.split('.')[0]
        if textbook_name in SUBJECT_SUBSETS[subset_name]:
            with textbook_content.open("r") as fid:
                for line in fid:
                    yield json.loads(line)


class MedQACorpus(datasets.GeneratorBasedBuilder):
    """MedQA Textbook (English) with emphasis on domain of Clinical Medicine and other subsets."""

    VERSION = datasets.Version("0.0.1")

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(name="core_clinical", version=VERSION, description="Core clinical medicine"),
        datasets.BuilderConfig(name="basic_biology", version=VERSION, description="Basic biology"),
        datasets.BuilderConfig(name="pharmacology", version=VERSION, description="Pharmacology"),
        datasets.BuilderConfig(name="psychiatry", version=VERSION, description="Psychiatry")
    ]

    DEFAULT_CONFIG_NAME = "core_clinical"

    def _info(self):
        features = datasets.Features(
            {
                "text": datasets.Value("string"),
                "source": datasets.Value("string"),
            }
        )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        urls = _URLS[0]
        data_dir = Path(dl_manager.download_and_extract(urls))
        self.base_dir = data_dir / "textbooks" / "en"

        return [
            datasets.SplitGenerator(name=datasets.Split.TRAIN)
        ]

    def _generate_examples(self):
        for key, record in enumerate(get_med_qa_textbooks(self.base_dir, self.config.name)):
            yield key, record