Datasets:

Languages:
English
License:
gabrielaltay commited on
Commit
7d02673
·
1 Parent(s): b010e9a

upload hubscripts/n2c2_2006_smokers_hub.py to hub from bigbio repo

Browse files
Files changed (1) hide show
  1. n2c2_2006_smokers.py +257 -0
n2c2_2006_smokers.py ADDED
@@ -0,0 +1,257 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2022 The HuggingFace Datasets Authors and the current dataset script contributor.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+
17
+ """
18
+ A dataset loader for the n2c2 2006 smoking status dataset.
19
+
20
+ https://portal.dbmi.hms.harvard.edu/projects/n2c2-nlp/
21
+
22
+ The dataset consists of two archive files,
23
+
24
+ * smokers_surrogate_train_all_version2.zip
25
+ * smokers_surrogate_test_all_groundtruth_version2.zip
26
+
27
+ The individual data files (inside the zip archives) come in just 1 type:
28
+
29
+ * xml (*.xml files): contains the id and text of the patient records,
30
+ and corresponding smoking status labels
31
+
32
+
33
+ The files comprising this dataset must be on the users local machine
34
+ in a single directory that is passed to `datasets.load_datset` via
35
+ the `data_dir` kwarg. This loader script will read the archive files
36
+ directly (i.e. the user should not uncompress, untar or unzip any of
37
+ the files). For example, if the following directory structure exists
38
+ on the users local machine,
39
+
40
+
41
+ n2c2_2006
42
+ ├── smokers_surrogate_train_all_version2.zip
43
+ ├── smokers_surrogate_test_all_groundtruth_version2.zip
44
+
45
+
46
+ Data Access
47
+
48
+ from https://www.i2b2.org/NLP/DataSets/Main.php
49
+
50
+ "As always, you must register AND submit a DUA for access. If you previously
51
+ accessed the data sets here on i2b2.org, you will need to set a new password
52
+ for your account on the Data Portal, but your original DUA will be retained."
53
+
54
+
55
+ """
56
+
57
+ import os
58
+ import xml.etree.ElementTree as et
59
+ import zipfile
60
+ from typing import Dict, List, Tuple
61
+
62
+ import datasets
63
+
64
+ from .bigbiohub import text_features
65
+ from .bigbiohub import BigBioConfig
66
+ from .bigbiohub import Tasks
67
+
68
+ _DATASETNAME = "n2c2_2006"
69
+ _DISPLAYNAME = "n2c2 2006 Smoking Status"
70
+
71
+ # https://academic.oup.com/jamia/article/15/1/14/779738
72
+ _LANGUAGES = ['English']
73
+ _PUBMED = False
74
+ _LOCAL = True
75
+ _CITATION = """\
76
+ @article{uzuner2008identifying,
77
+ author = {
78
+ Uzuner, Ozlem and
79
+ Goldstein, Ira and
80
+ Luo, Yuan and
81
+ Kohane, Isaac
82
+ },
83
+ title = {Identifying Patient Smoking Status from Medical Discharge Records},
84
+ journal = {Journal of the American Medical Informatics Association},
85
+ volume = {15},
86
+ number = {1},
87
+ pages = {14-24},
88
+ year = {2008},
89
+ month = {01},
90
+ url = {https://doi.org/10.1197/jamia.M2408},
91
+ doi = {10.1136/amiajnl-2011-000784},
92
+ eprint = {https://academic.oup.com/jamia/article-pdf/15/1/14/2339646/15-1-14.pdf}
93
+ }
94
+ """
95
+
96
+ _DESCRIPTION = """\
97
+ The data for the n2c2 2006 smoking challenge consisted of discharge summaries
98
+ from Partners HealthCare, which were then de-identified, tokenized, broken into
99
+ sentences, converted into XML format, and separated into training and test sets.
100
+
101
+ Two pulmonologists annotated each record with the smoking status of patients based
102
+ strictly on the explicitly stated smoking-related facts in the records. These
103
+ annotations constitute the textual judgments of the annotators. The annotators
104
+ were asked to classify patient records into five possible smoking status categories:
105
+ a past smoker, a current smoker, a smoker, a non-smoker and an unknown. A total of
106
+ 502 de-identified medical discharge records were used for the smoking challenge.
107
+ """
108
+
109
+ _HOMEPAGE = "https://portal.dbmi.hms.harvard.edu/projects/n2c2-nlp/"
110
+
111
+ _LICENSE = 'Data User Agreement'
112
+
113
+ _SUPPORTED_TASKS = [Tasks.TEXT_CLASSIFICATION]
114
+
115
+ _SOURCE_VERSION = "1.0.0"
116
+ _BIGBIO_VERSION = "1.0.0"
117
+
118
+ _CLASS_NAMES = ["current smoker", "non-smoker", "past smoker", "smoker", "unknown"]
119
+
120
+
121
+ def _read_zip(file_path):
122
+ _, filename = os.path.split(file_path)
123
+ zipped = zipfile.ZipFile(file_path, "r")
124
+ file = zipped.read(filename.split(".")[0] + ".xml")
125
+
126
+ root = et.fromstring(file)
127
+ ids = []
128
+ notes = []
129
+ labels = []
130
+ documents = root.findall("./RECORD")
131
+ for document in documents:
132
+ ids.append(document.attrib["ID"])
133
+ notes.append(document.findall("./TEXT")[0].text)
134
+ labels.append(document.findall("./SMOKING")[0].attrib["STATUS"].lower())
135
+ return [(id, note, label) for id, note, label in zip(ids, notes, labels)]
136
+
137
+
138
+ class N2C22006SmokingDataset(datasets.GeneratorBasedBuilder):
139
+ """n2c2 2006 smoking status identification task"""
140
+
141
+ SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
142
+ BIGBIO_VERSION = datasets.Version(_BIGBIO_VERSION)
143
+
144
+ BUILDER_CONFIGS = [
145
+ BigBioConfig(
146
+ name="n2c2_2006_smokers_source",
147
+ version=SOURCE_VERSION,
148
+ description="n2c2_2006_smokers source schema",
149
+ schema="source",
150
+ subset_id="n2c2_2006_smokers",
151
+ ),
152
+ BigBioConfig(
153
+ name="n2c2_2006_smokers_bigbio_text",
154
+ version=BIGBIO_VERSION,
155
+ description="n2c2_2006_smokers BigBio schema",
156
+ schema="bigbio_text",
157
+ subset_id="n2c2_2006_smokers",
158
+ ),
159
+ ]
160
+
161
+ DEFAULT_CONFIG_NAME = "n2c2_2006_smokers_source"
162
+
163
+ def _info(self) -> datasets.DatasetInfo:
164
+
165
+ if self.config.schema == "source":
166
+ features = datasets.Features(
167
+ {
168
+ "document_id": datasets.Value("string"),
169
+ "text": datasets.Value("string"),
170
+ "label": datasets.ClassLabel(names=_CLASS_NAMES),
171
+ }
172
+ )
173
+
174
+ elif self.config.schema == "bigbio_text":
175
+ features = text_features
176
+
177
+ return datasets.DatasetInfo(
178
+ description=_DESCRIPTION,
179
+ features=features,
180
+ homepage=_HOMEPAGE,
181
+ license=str(_LICENSE),
182
+ citation=_CITATION,
183
+ )
184
+
185
+ def _split_generators(
186
+ self, dl_manager: datasets.DownloadManager
187
+ ) -> List[datasets.SplitGenerator]:
188
+ """Returns SplitGenerators."""
189
+
190
+ if self.config.data_dir is None:
191
+ raise ValueError(
192
+ "This is a local dataset. Please pass the data_dir kwarg to load_dataset."
193
+ )
194
+ else:
195
+ data_dir = self.config.data_dir
196
+
197
+ return [
198
+ datasets.SplitGenerator(
199
+ name=datasets.Split.TRAIN,
200
+ gen_kwargs={
201
+ "data_dir": data_dir,
202
+ "split": "train",
203
+ },
204
+ ),
205
+ datasets.SplitGenerator(
206
+ name=datasets.Split.TEST,
207
+ gen_kwargs={
208
+ "data_dir": data_dir,
209
+ "split": "test",
210
+ },
211
+ ),
212
+ ]
213
+
214
+ def _generate_examples(self, data_dir, split: str) -> Tuple[int, Dict]:
215
+ """Yields examples as (key, example) tuples."""
216
+
217
+ if split == "train":
218
+ _id = 0
219
+ path = os.path.join(data_dir, "smokers_surrogate_train_all_version2.zip")
220
+ samples = _read_zip(path)
221
+ for sample in samples:
222
+ if self.config.schema == "source":
223
+ yield _id, {
224
+ "document_id": sample[0],
225
+ "text": sample[1],
226
+ "label": sample[-1],
227
+ }
228
+ elif self.config.schema == "bigbio_text":
229
+ yield _id, {
230
+ "id": sample[0],
231
+ "document_id": sample[0],
232
+ "text": sample[1],
233
+ "labels": [sample[-1]],
234
+ }
235
+ _id += 1
236
+
237
+ elif split == "test":
238
+ _id = 0
239
+ path = os.path.join(
240
+ data_dir, "smokers_surrogate_test_all_groundtruth_version2.zip"
241
+ )
242
+ samples = _read_zip(path)
243
+ for sample in samples:
244
+ if self.config.schema == "source":
245
+ yield _id, {
246
+ "document_id": sample[0],
247
+ "text": sample[1],
248
+ "label": sample[-1],
249
+ }
250
+ elif self.config.schema == "bigbio_text":
251
+ yield _id, {
252
+ "id": sample[0],
253
+ "document_id": sample[0],
254
+ "text": sample[1],
255
+ "labels": [sample[-1]],
256
+ }
257
+ _id += 1