Datasets:

Languages:
English
License:
File size: 8,422 Bytes
4ae00a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
249
250
251
252
# coding=utf-8
# Copyright 2022 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.
"""
NeuoTrialNER is an annotated dataset for named entities in clinical trial registry data in the domain of neurology/psychiatry. 
The corpus comprises 1093 clinical trial title and brief summaries from ClinicalTrials.gov. 
"""

import os
from typing import List, Tuple, Dict
import json

import datasets
from .bigbiohub import BigBioConfig
from .bigbiohub import Tasks
from .bigbiohub import kb_features

_LOCAL = False
_LANGUAGES = ['English']
_PUBMED = False
# TODO: Add BibTeX citation
_CITATION = """\
@article{,
  author    = {},
  title     = {},
  journal   = {},
  volume    = {},
  year      = {},
  url       = {},
  doi       = {},
  biburl    = {},
  bibsource = {}
}
"""

_DATASETNAME = "neurotrial_ner"
_DISPLAYNAME = "NeuroTrialNER"
_DESCRIPTION = """\
NeuoTrialNER is an annotated dataset for named entities in clinical trial registry data in the domain of neurology/psychiatry. 
The corpus comprises 1093 clinical trial title and brief summaries from ClinicalTrials.gov. 
It has been annotated by two to three annotators for key trial characteristics, i.e., condition (e.g., Alzheimer's disease), 
therapeutic intervention (e.g., aspirin), and control arms (e.g., placebo).
"""

_HOMEPAGE = "https://github.com/Ineichen-Group/NeuroTrialNER"

_LICENSE = 'CC0_1p0'

_URL = "https://raw.githubusercontent.com/Ineichen-Group/NeuroTrialNER/main/data/annotated_data/bigbio/"
_URLS = {
    "train": _URL + "train.json",
    "dev": _URL + "dev.json",
    "test": _URL + "test.json",
}

_SUPPORTED_TASKS = [Tasks.NAMED_ENTITY_RECOGNITION]

_SOURCE_VERSION = "1.0.0"
_BIGBIO_VERSION = "1.0.0"


class NeuroTrialNerDataset(datasets.GeneratorBasedBuilder):
    """
    1093 clinical trial official title and brief summary from ClinicalTrials.gov
    annotated for named entities.
    """
    SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
    BIGBIO_VERSION = datasets.Version(_BIGBIO_VERSION)

    BUILDER_CONFIGS = [
        BigBioConfig(
            name="neurotrial_ner_source",
            version=SOURCE_VERSION,
            description="neurotrial_ner source schema",
            schema="source",
            subset_id="neurotrial_ner",
        ),
        BigBioConfig(
            name="neurotrial_ner_bigbio_kb",
            version=BIGBIO_VERSION,
            description="neurotrial_ner BigBio schema",
            schema="bigbio_kb",
            subset_id="neurotrial_ner",
        ),
    ]

    DEFAULT_CONFIG_NAME = "neurotrial_ner_source"

    def _info(self) -> datasets.DatasetInfo:
        if self.config.schema == "source":
            features = datasets.Features(
                {
                    "nctid": datasets.Value("string"),
                    "text": datasets.Value("string"),
                    "tokens": datasets.Value("string"),
                    "token_bio_labels": datasets.Value("string"),
                    "entities": [
                        {
                            "start": datasets.Value("int32"),
                            "end": datasets.Value("int32"),
                            "text": datasets.Value("string"),
                            "type": datasets.Value("string"),
                        }
                    ],
                }
            )
        elif self.config.schema == "bigbio_kb":
            features = kb_features

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

    def _split_generators(self, dl_manager) -> List[datasets.SplitGenerator]:
        """Returns SplitGenerators."""
        urls_to_download = _URLS
        downloaded_files = dl_manager.download_and_extract(urls_to_download)

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "filepath": downloaded_files["train"],
                    "split": "train",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "filepath": downloaded_files["test"],
                    "split": "test",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "filepath": downloaded_files["dev"],
                    "split": "dev",
                },
            ),
        ]

    @staticmethod
    def get_source_example(uid, entry):
        nctid = entry.get("nctid", "").strip()
        text = entry.get("text", "").strip()
        tokens = entry.get("tokens", "").strip()
        token_bio_labels = entry.get("token_bio_labels", "").strip()
        entities = entry.get("entities", [])

        # Process the entities (which is a list of dictionaries)
        processed_entities = []
        for entity in entities:
            start = entity.get("start", 0)
            end = entity.get("end", 0)
            entity_text = entity.get("text", "").strip()
            entity_type = entity.get("type", "").strip()

            processed_entities.append({
                "start": start,
                "end": end,
                "text": entity_text,
                "type": entity_type,
            })

        doc = {
            "nctid": nctid,
            "text": text,
            "tokens": tokens,
            "token_bio_labels": token_bio_labels,
            "entities": processed_entities,
        }

        return uid, doc

    @staticmethod
    def get_bigbio_example(uid, entry):
        nctid = entry.get("nctid", "").strip()
        text = entry.get("text", "").strip()
        tokens = entry.get("tokens", "").strip()
        token_bio_labels = entry.get("token_bio_labels", "").strip()
        entities = entry.get("entities", [])

        # Generate passages to capture document structure (title and brief summary)
        passages = []
        passages.append({
            "id": str(uid) + "-passage-0",
            "type": "official_title_brief_summary",
            "text": [text],
            "offsets": [[0, len(text)]],
        })

        # Process entities to conform to the schema
        processed_entities = []
        ii = 0
        for i, entity in enumerate(entities):
            start = entity.get("start", 0)
            end = entity.get("end", 0)
            entity_text = entity.get("text", "").strip()
            entity_type = entity.get("type", "").strip()
            normalized = entity.get("normalized", [])

            processed_entities.append({
                "id": str(uid) + "-entity-" + str(ii),
                "offsets": [[start, end]],
                "text": [entity_text],
                "type": entity_type,
                "normalized": normalized,
            })
            ii += 1

        # Build the final document structure
        doc = {
            "id": uid,
            "document_id": nctid,
            "passages": passages,
            "entities": processed_entities,
            "events": [],
            "coreferences": [],
            "relations": [],
        }

        return uid, doc

    def _generate_examples(self, filepath, split: str) -> Tuple[int, Dict]:
        """Yields examples as (key, example) tuples."""
        with open(filepath, "r") as f:
            data = json.load(f)
            uid = 0
            # Iterate over each entry in the JSON file
            for entry in data:
                if self.config.schema == "source":
                    yield self.get_source_example(uid, entry)
                elif self.config.schema == "bigbio_kb":
                    yield self.get_bigbio_example(uid, entry)
                uid += 1