-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathswift.py
293 lines (245 loc) · 9.63 KB
/
swift.py
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# coding=utf-8
# Copyright 2023 Wojciech Kusa
#
# 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.
import os
from typing import List, Tuple, Dict
import datasets
import pandas as pd
from csmed.loader.bigbiohub import BigBioConfig
from csmed.loader.bigbiohub import Tasks
from csmed.loader.bigbiohub import text_features
from csmed.utils import (
get_from_pubmed,
is_prepared,
save_checksum,
mark_all_files_prepared,
)
_LANGUAGES = ["English"]
_PUBMED = True
_LOCAL = False
_CITATION = """\
@article{Howard2016,
author = {Howard, Brian E. and Phillips, Jason and Miller, Kyle and Tandon, Arpit and Mav, Deepak and Shah, Mihir R. and Holmgren, Stephanie and Pelch, Katherine E. and Walker, Vickie and Rooney, Andrew A. and Macleod, Malcolm and Shah, Ruchir R. and Thayer, Kristina},
doi = {10.1186/s13643-016-0263-z},
issn = {20464053},
journal = {Systematic Reviews},
keywords = {Literature prioritization, SWIFT-Review, Scoping reports, Software, Systematic review},
month = {5},
number = {1},
pages = {1--16},
pmid = {27216467},
publisher = {BioMed Central Ltd.},
title = {{SWIFT-Review: A text-mining workbench for systematic review}},
url = {https://link.springer.com/articles/10.1186/s13643-016-0263-z https://link.springer.com/article/10.1186/s13643-016-0263-z},
volume = {5},
year = {2016},
bdsk-url-1 = {https://link.springer.com/articles/10.1186/s13643-016-0263-z%20https://link.springer.com/article/10.1186/s13643-016-0263-z},
bdsk-url-2 = {https://doi.org/10.1186/s13643-016-0263-z}}
"""
_DATASETNAME = "swift"
_DISPLAYNAME = "swift"
_DESCRIPTION = """\
Four datasets (Additional file 1) were generated by the National Toxicology Program (NTP)
Office of Health Assessment and Translation (OHAT), one dataset (Additional file 2) was provided by
the Edinburgh CAMARADES group (www.camarades.info).
"""
_HOMEPAGE = "https://systematicreviewsjournal.biomedcentral.com/articles/10.1186/s13643-016-0263-z#Sec30"
_LICENSE = "CC0 1.0 Universal (CC0 1.0) Public Domain Dedication"
_URLS = {
_DATASETNAME: {
"ohat": "https://static-content.springer.com/esm/art%3A10.1186%2Fs13643-016-0263-z/MediaObjects/13643_2016_263_MOESM1_ESM.xlsx",
"camrades": "https://static-content.springer.com/esm/art%3A10.1186%2Fs13643-016-0263-z/MediaObjects/13643_2016_263_MOESM2_ESM.xlsx",
}
}
_SUPPORTED_TASKS = [Tasks.TEXT_CLASSIFICATION]
_SOURCE_VERSION = "1.0.0"
_BIGBIO_VERSION = "1.0.0"
_CLASS_NAMES = ["included", "excluded"]
def prepare_fluoride_dataset(df: pd.DataFrame) -> pd.DataFrame:
"""
:param df: input dataframe containing Fluoride dataset
"""
labels_column: str = "Label"
df["Title"] = df["Title"].fillna("")
df["Abstract"] = df["Abstract"].fillna("")
df[labels_column] = 1
df.loc[df["Included"] == "EXCLUDED", labels_column] = 0
return df
def prepare_neuropathic_pain_dataset(df: pd.DataFrame) -> pd.DataFrame:
"""
:param df: input dataframe containing NeuropathicPain dataset
"""
labels_column: str = "Label"
df["Title"] = df["Title"].fillna("")
df["Abstract"] = df["Abstract"].fillna("")
df["tmp_label"] = df["Label"]
df[labels_column] = 1
df.loc[df["tmp_label"] == "Excluded", labels_column] = 0
return df
reviews_version = {
"Neuropain": prepare_neuropathic_pain_dataset,
"Fluoride": prepare_fluoride_dataset,
"BPA": get_from_pubmed,
"Transgenerational": get_from_pubmed,
"PFOS-PFOA": get_from_pubmed,
}
file_to_review_mapping = {
"ohat": [
"BPA",
"Transgenerational",
"PFOS-PFOA",
"Fluoride",
],
"camrades": ["Neuropain"],
}
REVIEWS = [x.split(".")[0] for x in reviews_version.keys()]
def prepare_dataset(
input_files: dict[str, str],
output_folder: str,
) -> None:
if is_prepared(output_folder):
return
print("PubMed data is being downloaded. This may take a while for the first time.")
for file_name, input_file in input_files.items():
for review in file_to_review_mapping[file_name]:
df = pd.read_excel(input_file, sheet_name=review)
print(f"Processing {review}, {len(df)=}")
df = reviews_version[review](df)
df.to_csv(f"{output_folder}/{review}.csv", index=False)
save_checksum(
file=f"{output_folder}/{review}.csv", dataset_directory=output_folder
)
mark_all_files_prepared(output_folder)
class SwiftDataset(datasets.GeneratorBasedBuilder):
"""Four datasets (Additional file 1) were generated by the National Toxicology Program (NTP)
Office of Health Assessment and Translation (OHAT), one dataset (Additional file 2) was provided by
the Edinburgh CAMARADES group (www.camarades.info)."""
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
BIGBIO_VERSION = datasets.Version(_BIGBIO_VERSION)
BUILDER_CONFIGS = []
reviews = REVIEWS
for dataset_version in reviews:
BUILDER_CONFIGS.append(
BigBioConfig(
name=f"swift_{dataset_version}_source",
version=SOURCE_VERSION,
description=f"swift {dataset_version} source schema",
schema="source",
subset_id=f"swift_{dataset_version}",
)
)
BUILDER_CONFIGS.append(
BigBioConfig(
name=f"swift_{dataset_version}_bigbio_text",
version=BIGBIO_VERSION,
description=f"swift {dataset_version} BigBio schema",
schema="bigbio_text",
subset_id=f"swift_{dataset_version}",
)
)
# Add an "all" config that combines all the reviews -- only for source schema
BUILDER_CONFIGS.append(
BigBioConfig(
name="swift_all_source",
version=SOURCE_VERSION,
description=f"swift all source schema",
schema="source",
subset_id=f"swift_all",
)
)
DEFAULT_CONFIG_NAME = "swift_all_source"
def _info(self) -> datasets.DatasetInfo:
"""Returns the dataset metadata."""
if self.config.schema == "source":
features = datasets.Features(
{
"review_name": datasets.Value("string"),
"pmid": datasets.Value("string"),
"title": datasets.Value("string"),
"abstract": datasets.Value("string"),
"label": datasets.ClassLabel(names=_CLASS_NAMES),
}
)
elif self.config.schema == "bigbio_text":
features = text_features
else:
raise ValueError(f"Unsupported schema {self.config.schema}")
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 = _URLS[_DATASETNAME]
data_dir = dl_manager.download(urls)
pubmed_output_dir = "/".join(self.cache_dir.split("/")[:-3])
prepare_dataset(input_files=data_dir, output_folder=pubmed_output_dir)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"split": "train",
},
),
]
def _generate_examples(self, split: str) -> Tuple[int, Dict]:
"""Yields examples as (key, example) tuples."""
data_dir = "/".join(self.cache_dir.split("/")[:-3])
review = "_".join(self.config.subset_id.split("_")[1:])
uid = 0
if review == "all":
df = pd.DataFrame()
for r in REVIEWS:
review_df = pd.read_csv(os.path.join(data_dir, f"{r}.csv"))
review_df["Review"] = r
df = pd.concat([df, review_df])
else:
df = pd.read_csv(os.path.join(data_dir, f"{review}.csv"))
df["Review"] = review
for key, example in df.iterrows():
review_name = example["Review"]
title = example["Title"]
abstract = example["Abstract"]
label = example["Label"]
try:
pmid = str(example["PMID"])
except (ValueError, KeyError):
pmid = "NA" # some reviews don't have PMIDs
uid += 1
text = f"{title}\n\n{abstract}"
if self.config.schema == "source":
data = {
"review_name": review_name,
"pmid": pmid,
"title": title,
"abstract": abstract,
"label": label,
}
yield str(uid), data
elif self.config.schema == "bigbio_text":
data = {
"id": str(uid),
"document_id": pmid,
"text": text,
"labels": [label],
}
yield str(uid), data
if __name__ == "__main__":
x = datasets.load_dataset(__file__, name="swift_BPA_bigbio_text")
print(x)
y = datasets.load_dataset(__file__, name="swift_all_source")
print(y)