File size: 10,272 Bytes
e85b514
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4bcff3
e85b514
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4bcff3
e85b514
f4bcff3
e85b514
 
 
f4bcff3
 
 
e85b514
 
f4bcff3
 
 
e85b514
 
 
 
f4bcff3
 
 
e85b514
 
f4bcff3
 
 
e85b514
 
 
f4bcff3
 
e85b514
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4bcff3
 
 
 
 
 
 
e85b514
 
 
f4bcff3
 
e85b514
 
 
 
 
 
f4bcff3
e85b514
f4bcff3
 
 
e85b514
 
 
 
 
 
 
 
 
 
 
 
 
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# coding=utf-8
# Copyright 2020 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors.
#
# 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.

"""
We introduce BIOMRC, a large-scale cloze-style biomedical MRC dataset. Care was taken to reduce noise, compared to the
previous BIOREAD dataset of Pappas et al. (2018). Experiments show that simple heuristics do not perform well on the
new dataset and that two neural MRC models that had been tested on BIOREAD perform much better on BIOMRC, indicating
that the new dataset is indeed less noisy or at least that its task is more feasible. Non-expert human performance is
also higher on the new dataset compared to BIOREAD, and biomedical experts perform even better. We also introduce a new
BERT-based MRC model, the best version of which substantially outperforms all other methods tested, reaching or
surpassing the accuracy of biomedical experts in some experiments. We make the new dataset available in three different
sizes, also releasing our code, and providing a leaderboard.
"""

import itertools as it
import json

import datasets

from .bigbiohub import qa_features
from .bigbiohub import BigBioConfig
from .bigbiohub import Tasks

_LANGUAGES = ["English"]
_PUBMED = True
_LOCAL = False
_CITATION = """\
@inproceedings{pappas-etal-2020-biomrc,
    title = "{B}io{MRC}: A Dataset for Biomedical Machine Reading Comprehension",
    author = "Pappas, Dimitris  and
      Stavropoulos, Petros  and
      Androutsopoulos, Ion  and
      McDonald, Ryan",
    booktitle = "Proceedings of the 19th SIGBioMed Workshop on Biomedical Language Processing",
    month = jul,
    year = "2020",
    address = "Online",
    publisher = "Association for Computational Linguistics",
    url = "https://www.aclweb.org/anthology/2020.bionlp-1.15",
    pages = "140--149",
}
"""

_DATASETNAME = "biomrc"
_DISPLAYNAME = "BIOMRC"

_DESCRIPTION = """\
We introduce BIOMRC, a large-scale cloze-style biomedical MRC dataset. Care was taken to reduce noise, compared to the
previous BIOREAD dataset of Pappas et al. (2018). Experiments show that simple heuristics do not perform well on the
new dataset and that two neural MRC models that had been tested on BIOREAD perform much better on BIOMRC, indicating
that the new dataset is indeed less noisy or at least that its task is more feasible. Non-expert human performance is
also higher on the new dataset compared to BIOREAD, and biomedical experts perform even better. We also introduce a new
BERT-based MRC model, the best version of which substantially outperforms all other methods tested, reaching or
surpassing the accuracy of biomedical experts in some experiments. We make the new dataset available in three different
sizes, also releasing our code, and providing a leaderboard.
"""

_HOMEPAGE = "https://github.com/PetrosStav/BioMRC_code"

_LICENSE = "License information unavailable"

_BASE_URL = "https://huggingface.co/datasets/biomrc/resolve/main/data/"
_URLS = {
    "large": {
        "A": {
            "train": _BASE_URL + "biomrc_large/dataset_train.jsonl.gz",
            "val": _BASE_URL + "biomrc_large/dataset_val.jsonl.gz",
            "test": _BASE_URL + "biomrc_large/dataset_test.jsonl.gz",
        },
        "B": {
            "train": _BASE_URL + "biomrc_large/dataset_train_B.jsonl.gz",
            "val": _BASE_URL + "biomrc_large/dataset_val_B.jsonl.gz",
            "test": _BASE_URL + "biomrc_large/dataset_test_B.jsonl.gz",
        },
    },
    "small": {
        "A": {
            "train": _BASE_URL + "biomrc_small/dataset_train_small.jsonl.gz",
            "val": _BASE_URL + "biomrc_small/dataset_val_small.jsonl.gz",
            "test": _BASE_URL + "biomrc_small/dataset_test_small.jsonl.gz",
        },
        "B": {
            "train": _BASE_URL + "biomrc_small/dataset_train_small_B.jsonl.gz",
            "val": _BASE_URL + "biomrc_small/dataset_val_small_B.jsonl.gz",
            "test": _BASE_URL + "biomrc_small/dataset_test_small_B.jsonl.gz",
        },
    },
    "tiny": {
        "A": {"test": _BASE_URL + "biomrc_tiny/dataset_tiny.jsonl.gz"},
        "B": {"test": _BASE_URL + "biomrc_tiny/dataset_tiny_B.jsonl.gz"},
    },
}

_SUPPORTED_TASKS = [Tasks.QUESTION_ANSWERING]

_SOURCE_VERSION = "1.0.0"

_BIGBIO_VERSION = "1.0.0"


class BiomrcDataset(datasets.GeneratorBasedBuilder):
    """BioMRC: A Dataset for Biomedical Machine Reading Comprehension"""

    SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
    BIGBIO_VERSION = datasets.Version(_BIGBIO_VERSION)

    BUILDER_CONFIGS = []

    for biomrc_setting in ["A", "B"]:
        for biomrc_version in ["large", "small", "tiny"]:
            subset_id = f"biomrc_{biomrc_version}_{biomrc_setting}"
            BUILDER_CONFIGS.append(
                BigBioConfig(
                    name=f"{subset_id}_source",
                    version=SOURCE_VERSION,
                    description=f"BioMRC Version {biomrc_version} Setting {biomrc_setting} source schema",
                    schema="source",
                    subset_id=subset_id,
                )
            )
            BUILDER_CONFIGS.append(
                BigBioConfig(
                    name=f"{subset_id}_bigbio_qa",
                    version=BIGBIO_VERSION,
                    description=f"BioMRC Version {biomrc_version} Setting {biomrc_setting} BigBio schema",
                    schema="bigbio_qa",
                    subset_id=subset_id,
                )
            )

    DEFAULT_CONFIG_NAME = "biomrc_large_B_source"

    def _info(self):
        if self.config.schema == "source":
            features = datasets.Features(
                {
                    "abstract": datasets.Value("string"),
                    "title": datasets.Value("string"),
                    "entities_list": datasets.features.Sequence(
                        {
                            "pseudoidentifier": datasets.Value("string"),
                            "identifier": datasets.Value("string"),
                            "synonyms": datasets.Value("string"),
                        }
                    ),
                    "answer": {
                        "pseudoidentifier": datasets.Value("string"),
                        "identifier": datasets.Value("string"),
                        "synonyms": datasets.Value("string"),
                    },
                }
            )
        elif self.config.schema == "bigbio_qa":
            features = qa_features
        else:
            raise NotImplementedError()

        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=str(_LICENSE),
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""

        _, version, setting = self.config.subset_id.split("_")
        downloaded_files = dl_manager.download_and_extract(_URLS[version][setting])

        if version == "tiny":
            return [
                datasets.SplitGenerator(
                    name=datasets.Split.TRAIN,
                    gen_kwargs={"filepath": downloaded_files["test"]},
                ),
            ]
        else:
            return [
                datasets.SplitGenerator(
                    name=datasets.Split.TRAIN,
                    gen_kwargs={"filepath": downloaded_files["train"]},
                ),
                datasets.SplitGenerator(
                    name=datasets.Split.VALIDATION,
                    gen_kwargs={"filepath": downloaded_files["val"]},
                ),
                datasets.SplitGenerator(
                    name=datasets.Split.TEST,
                    gen_kwargs={"filepath": downloaded_files["test"]},
                ),
            ]

    def _generate_examples(self, filepath):
        """Yields examples as (key, example) tuples."""

        if self.config.schema == "source":
            with open(filepath, encoding="utf-8") as fp:
                for _id, line in enumerate(fp):
                    example = json.loads(line)
                    example["entities_list"] = [
                        self._parse_dict_from_entity(entity) for entity in example["entities_list"]
                    ]
                    example["answer"] = self._parse_dict_from_entity(example["answer"])
                    yield _id, example
        elif self.config.schema == "bigbio_qa":
            with open(filepath, encoding="utf-8") as fp:
                uid = it.count(0)
                for _id, line in enumerate(fp):
                    example = json.loads(line)
                    # remove info such as code, label, synonyms from answer and choices
                    # f.e. @entity1 :: ('9606', 'Species') :: ['patients', 'patient']"
                    example = {
                        "id": next(uid),
                        "question_id": next(uid),
                        "document_id": next(uid),
                        "question": example["title"],
                        "type": "multiple_choice",
                        "choices": [x.split(" :: ")[0] for x in example["entities_list"]],
                        "context": example["abstract"],
                        "answer": [example["answer"].split(" :: ")[0]],
                    }
                    yield _id, example

    def _parse_dict_from_entity(self, entity):
        if "::" in entity:
            pseudoidentifier, identifier, synonyms = entity.split(" :: ")
            return {
                "pseudoidentifier": pseudoidentifier,
                "identifier": identifier,
                "synonyms": synonyms,
            }
        else:
            return {"pseudoidentifier": entity, "identifier": "", "synonyms": ""}