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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
PPS dataset is a list of triplets. Each entry is in format (patient_uid_1, patient_uid_2, similarity)
|
| 18 |
+
where similarity has three values:0, 1, 2, indicating corresponding similarity.
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
import json
|
| 22 |
+
import os
|
| 23 |
+
from typing import Dict, List, Tuple
|
| 24 |
+
|
| 25 |
+
import datasets
|
| 26 |
+
import pandas as pd
|
| 27 |
+
|
| 28 |
+
from .bigbiohub import pairs_features
|
| 29 |
+
from .bigbiohub import BigBioConfig
|
| 30 |
+
from .bigbiohub import Tasks
|
| 31 |
+
|
| 32 |
+
_LANGUAGES = ['English']
|
| 33 |
+
_PUBMED = True
|
| 34 |
+
_LOCAL = False
|
| 35 |
+
_CITATION = """\
|
| 36 |
+
@misc{zhao2022pmcpatients,
|
| 37 |
+
title={PMC-Patients: A Large-scale Dataset of Patient Notes and Relations Extracted from Case
|
| 38 |
+
Reports in PubMed Central},
|
| 39 |
+
author={Zhengyun Zhao and Qiao Jin and Sheng Yu},
|
| 40 |
+
year={2022},
|
| 41 |
+
eprint={2202.13876},
|
| 42 |
+
archivePrefix={arXiv},
|
| 43 |
+
primaryClass={cs.CL}
|
| 44 |
+
}"""
|
| 45 |
+
|
| 46 |
+
_DATASETNAME = "pmc_patients"
|
| 47 |
+
_DISPLAYNAME = "PMC-Patients"
|
| 48 |
+
|
| 49 |
+
_DESCRIPTION = """\
|
| 50 |
+
This dataset is used for calculating the similarity between two patient descriptions.
|
| 51 |
+
"""
|
| 52 |
+
|
| 53 |
+
_HOMEPAGE = "https://github.com/zhao-zy15/PMC-Patients"
|
| 54 |
+
|
| 55 |
+
_LICENSE = 'Creative Commons Attribution Non Commercial Share Alike 4.0 International'
|
| 56 |
+
|
| 57 |
+
_URLS = {
|
| 58 |
+
_DATASETNAME: "https://drive.google.com/u/0/uc?id=1vFCLy_CF8fxPDZvDtHPR6Dl6x9l0TyvW&export=download",
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
_SUPPORTED_TASKS = [Tasks.SEMANTIC_SIMILARITY]
|
| 62 |
+
|
| 63 |
+
_SOURCE_VERSION = "1.2.0"
|
| 64 |
+
|
| 65 |
+
_BIGBIO_VERSION = "1.0.0"
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
class PMCPatientsDataset(datasets.GeneratorBasedBuilder):
|
| 69 |
+
"""PPS dataset is a list of triplets.
|
| 70 |
+
Each entry is in format (patient_uid_1, patient_uid_2, similarity) and their
|
| 71 |
+
respective texts.
|
| 72 |
+
where similarity has three values:0, 1, 2, indicating corresponding similarity.
|
| 73 |
+
"""
|
| 74 |
+
|
| 75 |
+
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
|
| 76 |
+
BIGBIO_VERSION = datasets.Version(_BIGBIO_VERSION)
|
| 77 |
+
|
| 78 |
+
BUILDER_CONFIGS = [
|
| 79 |
+
BigBioConfig(
|
| 80 |
+
name="pmc_patients_source",
|
| 81 |
+
version=SOURCE_VERSION,
|
| 82 |
+
description="pmc_patients source schema",
|
| 83 |
+
schema="source",
|
| 84 |
+
subset_id="pmc_patients",
|
| 85 |
+
),
|
| 86 |
+
BigBioConfig(
|
| 87 |
+
name="pmc_patients_bigbio_pairs",
|
| 88 |
+
version=BIGBIO_VERSION,
|
| 89 |
+
description="pmc_patients BigBio schema",
|
| 90 |
+
schema="bigbio_pairs",
|
| 91 |
+
subset_id="pmc_patients",
|
| 92 |
+
),
|
| 93 |
+
]
|
| 94 |
+
|
| 95 |
+
DEFAULT_CONFIG_NAME = "pmc_patients_source"
|
| 96 |
+
|
| 97 |
+
def _info(self) -> datasets.DatasetInfo:
|
| 98 |
+
if self.config.schema == "source":
|
| 99 |
+
features = datasets.Features(
|
| 100 |
+
{
|
| 101 |
+
"id": datasets.Value("string"),
|
| 102 |
+
"id_text1": datasets.Value("string"),
|
| 103 |
+
"id_text2": datasets.Value("string"),
|
| 104 |
+
"label": datasets.Value("int8"),
|
| 105 |
+
}
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
elif self.config.schema == "bigbio_pairs":
|
| 109 |
+
features = pairs_features
|
| 110 |
+
|
| 111 |
+
return datasets.DatasetInfo(
|
| 112 |
+
description=_DESCRIPTION,
|
| 113 |
+
features=features,
|
| 114 |
+
homepage=_HOMEPAGE,
|
| 115 |
+
license=str(_LICENSE),
|
| 116 |
+
citation=_CITATION,
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
def _split_generators(self, dl_manager) -> List[datasets.SplitGenerator]:
|
| 120 |
+
"""Returns SplitGenerators."""
|
| 121 |
+
urls = _URLS[_DATASETNAME]
|
| 122 |
+
data_dir = dl_manager.download_and_extract(urls)
|
| 123 |
+
return [
|
| 124 |
+
datasets.SplitGenerator(
|
| 125 |
+
name=datasets.Split.TRAIN,
|
| 126 |
+
gen_kwargs={
|
| 127 |
+
"filepath": os.path.join(
|
| 128 |
+
data_dir,
|
| 129 |
+
"datasets/task_2_patient2patient_similarity/PPS_train.json",
|
| 130 |
+
),
|
| 131 |
+
"split": "train",
|
| 132 |
+
"data_dir": data_dir,
|
| 133 |
+
},
|
| 134 |
+
),
|
| 135 |
+
datasets.SplitGenerator(
|
| 136 |
+
name=datasets.Split.TEST,
|
| 137 |
+
gen_kwargs={
|
| 138 |
+
"filepath": os.path.join(
|
| 139 |
+
data_dir,
|
| 140 |
+
"datasets/task_2_patient2patient_similarity/PPS_test.json",
|
| 141 |
+
),
|
| 142 |
+
"split": "test",
|
| 143 |
+
"data_dir": data_dir,
|
| 144 |
+
},
|
| 145 |
+
),
|
| 146 |
+
datasets.SplitGenerator(
|
| 147 |
+
name=datasets.Split.VALIDATION,
|
| 148 |
+
gen_kwargs={
|
| 149 |
+
"filepath": os.path.join(
|
| 150 |
+
data_dir,
|
| 151 |
+
"datasets/task_2_patient2patient_similarity/PPS_dev.json",
|
| 152 |
+
),
|
| 153 |
+
"split": "dev",
|
| 154 |
+
"data_dir": data_dir,
|
| 155 |
+
},
|
| 156 |
+
),
|
| 157 |
+
]
|
| 158 |
+
|
| 159 |
+
def _generate_examples(
|
| 160 |
+
self, filepath, split: str, data_dir: str
|
| 161 |
+
) -> Tuple[int, Dict]:
|
| 162 |
+
"""Yields examples as (key, example) tuples."""
|
| 163 |
+
|
| 164 |
+
uid = 0
|
| 165 |
+
|
| 166 |
+
def lookup_text(patient_uid: str, df: pd.DataFrame) -> str:
|
| 167 |
+
try:
|
| 168 |
+
return df.loc[patient_uid]["patient"]
|
| 169 |
+
except KeyError:
|
| 170 |
+
return ""
|
| 171 |
+
|
| 172 |
+
with open(filepath, "r") as j:
|
| 173 |
+
ret_file = json.load(j)
|
| 174 |
+
|
| 175 |
+
if self.config.schema == "source":
|
| 176 |
+
|
| 177 |
+
for key, (id1, id2, label) in enumerate(ret_file):
|
| 178 |
+
feature_dict = {
|
| 179 |
+
"id": uid,
|
| 180 |
+
"id_text1": id1,
|
| 181 |
+
"id_text2": id2,
|
| 182 |
+
"label": label,
|
| 183 |
+
}
|
| 184 |
+
uid += 1
|
| 185 |
+
yield key, feature_dict
|
| 186 |
+
|
| 187 |
+
elif self.config.schema == "bigbio_pairs":
|
| 188 |
+
source_files = os.path.join(data_dir, f"datasets/PMC-Patients_{split}.json")
|
| 189 |
+
src_frame = pd.read_json(source_files, encoding="utf8").set_index(
|
| 190 |
+
"patient_uid"
|
| 191 |
+
)
|
| 192 |
+
for key, (id1, id2, label) in enumerate(ret_file):
|
| 193 |
+
text_1 = lookup_text(id1, src_frame)
|
| 194 |
+
text_2 = lookup_text(id2, src_frame)
|
| 195 |
+
# test/dev splits are faulty and may not contain the patient_uid
|
| 196 |
+
# if any of the lookup texts are empty skip the sample
|
| 197 |
+
if text_1 == "" or text_2 == "":
|
| 198 |
+
continue
|
| 199 |
+
feature_dict = {
|
| 200 |
+
"id": uid,
|
| 201 |
+
"document_id": "NULL",
|
| 202 |
+
"text_1": text_1,
|
| 203 |
+
"text_2": text_2,
|
| 204 |
+
"label": label,
|
| 205 |
+
}
|
| 206 |
+
uid += 1
|
| 207 |
+
yield key, feature_dict
|