gabrielaltay commited on
Commit
7b2f556
1 Parent(s): be18ceb

upload hubscripts/ehr_rel_hub.py to hub from bigbio repo

Browse files
Files changed (1) hide show
  1. ehr_rel.py +222 -0
ehr_rel.py ADDED
@@ -0,0 +1,222 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ EHR-Rel is a novel open-source1 biomedical concept relatedness dataset consisting of 3630 concept pairs, six times more
18
+ than the largest existing dataset. Instead of manually selecting and pairing concepts as done in previous work,
19
+ the dataset is sampled from EHRs to ensure concepts are relevant for the EHR concept retrieval task.
20
+ A detailed analysis of the concepts in the dataset reveals a far larger coverage compared to existing datasets.
21
+ """
22
+
23
+ import csv
24
+ from pathlib import Path
25
+ from typing import Dict, Iterator, List, Tuple
26
+
27
+ import datasets
28
+
29
+ from .bigbiohub import pairs_features
30
+ from .bigbiohub import BigBioConfig
31
+ from .bigbiohub import Tasks
32
+
33
+ _LANGUAGES = ['English']
34
+ _PUBMED = False
35
+ _LOCAL = False
36
+ _CITATION = """\
37
+ @inproceedings{schulz-etal-2020-biomedical,
38
+ title = {Biomedical Concept Relatedness {--} A large {EHR}-based benchmark},
39
+ author = {Schulz, Claudia and
40
+ Levy-Kramer, Josh and
41
+ Van Assel, Camille and
42
+ Kepes, Miklos and
43
+ Hammerla, Nils},
44
+ booktitle = {Proceedings of the 28th International Conference on Computational Linguistics},
45
+ month = {dec},
46
+ year = {2020},
47
+ address = {Barcelona, Spain (Online)},
48
+ publisher = {International Committee on Computational Linguistics},
49
+ url = {https://aclanthology.org/2020.coling-main.577},
50
+ doi = {10.18653/v1/2020.coling-main.577},
51
+ pages = {6565--6575},
52
+ }
53
+ """
54
+
55
+ _DATASETNAME = "ehr_rel"
56
+ _DISPLAYNAME = "EHR-Rel"
57
+
58
+ _DESCRIPTION = """\
59
+ EHR-Rel is a novel open-source1 biomedical concept relatedness dataset consisting of 3630 concept pairs, six times more
60
+ than the largest existing dataset. Instead of manually selecting and pairing concepts as done in previous work,
61
+ the dataset is sampled from EHRs to ensure concepts are relevant for the EHR concept retrieval task.
62
+ A detailed analysis of the concepts in the dataset reveals a far larger coverage compared to existing datasets.
63
+ """
64
+
65
+ _HOMEPAGE = "https://github.com/babylonhealth/EHR-Rel"
66
+
67
+ _LICENSE = 'Apache License 2.0'
68
+
69
+ _URLS = {
70
+ _DATASETNAME: "https://github.com/babylonhealth/EHR-Rel/archive/refs/heads/master.zip",
71
+ }
72
+
73
+ _SUPPORTED_TASKS = [Tasks.SEMANTIC_SIMILARITY]
74
+
75
+
76
+ _SOURCE_VERSION = "1.0.0"
77
+
78
+ _BIGBIO_VERSION = "1.0.0"
79
+
80
+
81
+ class EHRRelDataset(datasets.GeneratorBasedBuilder):
82
+ """Dataset for EHR-Rel Corpus"""
83
+
84
+ SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
85
+ BIGBIO_VERSION = datasets.Version(_BIGBIO_VERSION)
86
+
87
+ BUILDER_CONFIGS = [
88
+ BigBioConfig(
89
+ name="ehr_rel_source",
90
+ version=SOURCE_VERSION,
91
+ description="EHR-Rel combined source schema",
92
+ schema="source",
93
+ subset_id="ehr_rel",
94
+ ),
95
+ BigBioConfig(
96
+ name="ehr_rel_a_source",
97
+ version=SOURCE_VERSION,
98
+ description="EHR-Rel-A source schema",
99
+ schema="source",
100
+ subset_id="ehr_rel_a",
101
+ ),
102
+ BigBioConfig(
103
+ name="ehr_rel_b_source",
104
+ version=SOURCE_VERSION,
105
+ description="EHR-Rel-B source schema",
106
+ schema="source",
107
+ subset_id="ehr_rel_b",
108
+ ),
109
+ BigBioConfig(
110
+ name="ehr_rel_bigbio_pairs",
111
+ version=BIGBIO_VERSION,
112
+ description="EHR-Rel BigBio schema",
113
+ schema="bigbio_pairs",
114
+ subset_id="ehr_rel",
115
+ ),
116
+ ]
117
+
118
+ DEFAULT_CONFIG_NAME = "ehr_rel_bigbio_pairs"
119
+
120
+ def _info(self) -> datasets.DatasetInfo:
121
+
122
+ if self.config.schema == "source":
123
+ features = datasets.Features(
124
+ {
125
+ "document_id": datasets.Value("string"),
126
+ "snomed_id_1": datasets.Value("string"),
127
+ "snomed_label_1": datasets.Value("string"),
128
+ "snomed_id_2": datasets.Value("string"),
129
+ "snomed_label_2": datasets.Value("string"),
130
+ "rater_A": datasets.Value("string"),
131
+ "rater_B": datasets.Value("string"),
132
+ "rater_C": datasets.Value("string"),
133
+ "rater_D": datasets.Value("string"),
134
+ "rater_E": datasets.Value("string"),
135
+ "mean_rating": datasets.Value("string"),
136
+ "CUI_1": datasets.Value("string"),
137
+ "CUI_2": datasets.Value("string"),
138
+ }
139
+ )
140
+
141
+ elif self.config.schema == "bigbio_pairs":
142
+ features = pairs_features
143
+
144
+ return datasets.DatasetInfo(
145
+ description=_DESCRIPTION,
146
+ features=features,
147
+ homepage=_HOMEPAGE,
148
+ license=str(_LICENSE),
149
+ citation=_CITATION,
150
+ )
151
+
152
+ def _split_generators(self, dl_manager) -> List[datasets.SplitGenerator]:
153
+ """Returns SplitGenerators."""
154
+ urls = _URLS[_DATASETNAME]
155
+ data_dir = Path(dl_manager.download_and_extract(urls))
156
+ data_dir = data_dir.joinpath("EHR-Rel-master")
157
+ return [
158
+ datasets.SplitGenerator(
159
+ name=datasets.Split.TRAIN,
160
+ gen_kwargs={"data_dir": data_dir},
161
+ ),
162
+ ]
163
+
164
+ def _generate_examples(self, data_dir: Path) -> Iterator[Tuple[str, Dict]]:
165
+
166
+ if self.config.subset_id == "ehr_rel_a":
167
+ files = ["EHR-RelA.tsv"]
168
+ elif self.config.subset_id == "ehr_rel_b":
169
+ files = ["EHR-RelB.tsv"]
170
+ else:
171
+ files = ["EHR-RelA.tsv", "EHR-RelB.tsv"]
172
+
173
+ uid = -1 # want first instance to be 0
174
+
175
+ for filename in files:
176
+ file = data_dir.joinpath(filename)
177
+ document_id = str(file.stem)
178
+ with open(file, encoding="utf-8") as csv_file:
179
+ csv_reader = csv.reader(csv_file, quotechar='"', delimiter="\t")
180
+ next(csv_reader, None) # remove column headers
181
+ for id_, row in enumerate(csv_reader):
182
+ uid += 1
183
+ (
184
+ snomed_id_1,
185
+ snomed_label_1,
186
+ snomed_id_2,
187
+ snomed_label_2,
188
+ rater_A,
189
+ rater_B,
190
+ rater_C,
191
+ rater_D,
192
+ rater_E,
193
+ mean_rating,
194
+ CUI_1,
195
+ CUI_2,
196
+ ) = row
197
+
198
+ if self.config.schema == "source":
199
+ yield uid, {
200
+ "document_id": document_id,
201
+ "snomed_id_1": snomed_id_1,
202
+ "snomed_label_1": snomed_label_1,
203
+ "snomed_id_2": snomed_id_1,
204
+ "snomed_label_2": snomed_label_2,
205
+ "rater_A": rater_A,
206
+ "rater_B": rater_B,
207
+ "rater_C": rater_C,
208
+ "rater_D": rater_D,
209
+ "rater_E": rater_E,
210
+ "mean_rating": mean_rating,
211
+ "CUI_1": CUI_1,
212
+ "CUI_2": CUI_2,
213
+ }
214
+
215
+ elif self.config.schema == "bigbio_pairs":
216
+ yield uid, {
217
+ "id": uid,
218
+ "document_id": document_id,
219
+ "text_1": snomed_label_1,
220
+ "text_2": snomed_label_2,
221
+ "label": mean_rating,
222
+ }