File size: 14,767 Bytes
bd45da9 bd5ce5f bd45da9 bd5ce5f bd45da9 |
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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# 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.
import os
import re
from typing import Dict, List, Tuple
import datasets
from .bigbiohub import BigBioConfig, Tasks, text_features
_LOCAL = False
_LANGUAGES = ["English"]
_PUBMED = False
_CITATION = """\
@inproceedings{,
author = {Dannenfelser, Ruth and Zhong, Jeffrey and Zhang, Ran and Yao, Vicky},
title = {Into the Single Cell Multiverse: an End-to-End Dataset for Procedural Knowledge Extraction in Biomedical Texts},
publisher = {Advances in Neural Information Processing Systems},
volume = {36},
year = {2024},
url = {https://proceedings.neurips.cc/paper_files/paper/2023/file/23e3d86c9a19d0caf2ec997e73dfcfbd-Paper-Datasets_and_Benchmarks.pdf},
}
"""
_DATASETNAME = "flambe"
_DISPLAYNAME = "Flambe"
_DESCRIPTION = """\
FlaMBe is a dataset aimed at procedural knowledge extraction from biomedical texts,
particularly focusing on single cell research methodologies described in academic papers. It includes
annotations from 55 full-text articles and 1,195 abstracts, covering nearly 710,000 tokens, and is
distinguished by its comprehensive named entity recognition (NER) and disambiguation (NED) for
tissue/cell types, software tools, and computational methods. This dataset, to our knowledge, is
the largest of its kind for tissue/cell types, links entities to identifiers in relevant knowledge
bases and annotates nearly 400 workflow relations between tool-context pairs.
"""
_HOMEPAGE = "https://github.com/ylaboratory/flambe"
_LICENSE = "CC_BY_4p0"
_URLS = {
_DATASETNAME: "https://zenodo.org/records/10050681/files/data.zip?download",
"ned": {
"tissue_test": "https://zenodo.org/records/11218662/files/tissue_ned_test.csv?download",
"tissue_train": "https://zenodo.org/records/11218662/files/tissue_ned_train.csv?download",
"tissue_val": "https://zenodo.org/records/11218662/files/tissue_ned_val.csv?download",
"tool_test": "https://zenodo.org/records/11218662/files/tool_ned_test.csv?download",
"tool_train": "https://zenodo.org/records/11218662/files/tool_ned_train.csv?download",
"tool_val": "https://zenodo.org/records/11218662/files/tool_ned_val.csv?download",
},
}
_SUPPORTED_TASKS = [
Tasks.NAMED_ENTITY_RECOGNITION,
Tasks.NAMED_ENTITY_DISAMBIGUATION,
]
_SOURCE_VERSION = "1.0.0"
_BIGBIO_VERSION = "1.0.0"
class FlambeDataset(datasets.GeneratorBasedBuilder):
"""TODO: Short description of my dataset."""
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
BIGBIO_VERSION = datasets.Version(_BIGBIO_VERSION)
BUILDER_CONFIGS = [
BigBioConfig(
name="flambe_ner_fulltext_tools_source",
version=SOURCE_VERSION,
description="NER dataset for tools from full papers",
schema="source",
subset_id="flambe_ner_fulltext_tools_source",
),
BigBioConfig(
name="flambe_ner_fulltext_tissues_source",
version=SOURCE_VERSION,
description="NER dataset for tissues from full papers",
schema="source",
subset_id="flambe_ner_fulltext_tissues_source",
),
BigBioConfig(
name="flambe_ner_abstract_tissues_source",
version=SOURCE_VERSION,
description="NER dataset for tissues from abstracts",
schema="source",
subset_id="flambe_ner_abstract_tissues_source",
),
BigBioConfig(
name="flambe_ned_tissues",
version=SOURCE_VERSION,
description="NED dataset for tissues from full papers",
schema="source_ned_tissue",
subset_id="flambe_ned_tissues",
),
BigBioConfig(
name="flambe_ned_tools",
version=SOURCE_VERSION,
description="NED dataset for tools from full papers",
schema="source_ned_tool",
subset_id="flambe_ned_tools",
),
BigBioConfig(
name="flambe_fulltext_tools_bigbio_text",
version=BIGBIO_VERSION,
description="Flambe Tissues BigBio schema",
schema="bigbio_text",
subset_id="flambe_tool_bigbio",
),
BigBioConfig(
name="flambe_fulltext_tissues_bigbio_text",
version=BIGBIO_VERSION,
description="Flambe Tool BigBio schema",
schema="bigbio_text",
subset_id="flambe_tissue_bigbio",
),
BigBioConfig(
name="flambe_abstract_tissues_bigbio_text",
version=BIGBIO_VERSION,
description="Flambe Tool BigBio schema",
schema="bigbio_text",
subset_id="flambe_tissue_bigbio",
),
]
DEFAULT_CONFIG_NAME = "flambe_ner_fulltext_tools_source"
def _info(self) -> datasets.DatasetInfo:
if self.config.schema == "source":
features = datasets.Features(
{
"id": datasets.Value("string"),
"tokens": datasets.Sequence(datasets.Value("string")),
"tags": datasets.Sequence(datasets.Value("string")),
}
)
elif self.config.schema == "source_ned_tissue":
features = datasets.Features(
{
"orginal_text": datasets.Value("string"),
"mapped_NCIT": datasets.Value("string"),
"NCIT_name": datasets.Value("string"),
}
)
elif self.config.schema == "source_ned_tool":
features = datasets.Features(
{
"orginal_text": datasets.Value("string"),
"standardized_name": datasets.Value("string"),
"url": datasets.Value("string"),
}
)
elif self.config.schema == "bigbio_text":
features = text_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."""
# TODO: KEEP if your dataset is PUBLIC; remove if not
# TODO: KEEP if your dataset is PUBLIC; remove if not
urls = _URLS[_DATASETNAME]
data_dir = dl_manager.download_and_extract(urls)
path = {
"flambe_ner_fulltext_tools_source": {
"train": os.path.join(data_dir, "data/tags/fulltext_iob/fulltext_tools_train.iob"),
"test": os.path.join(data_dir, "data/tags/fulltext_iob/fulltext_tools_test.iob"),
"dev": os.path.join(data_dir, "data/tags/fulltext_iob/fulltext_tools_validation.iob"),
},
"flambe_ner_fulltext_tissues_source": {
"train": os.path.join(data_dir, "data/tags/fulltext_iob/fulltext_tissues_train.iob"),
"test": os.path.join(data_dir, "data/tags/fulltext_iob/fulltext_tissues_test.iob"),
"dev": os.path.join(data_dir, "data/tags/fulltext_iob/fulltext_tissues_validation.iob"),
},
"flambe_ner_abstract_tissues_source": {
"train": os.path.join(data_dir, "data/tags/abstract_iob/abstract_tissues_train.iob"),
"test": os.path.join(data_dir, "data/tags/abstract_iob/abstract_tissues_test.iob"),
"dev": os.path.join(data_dir, "data/tags/abstract_iob/abstract_tissues_validation.iob"),
},
"flambe_ned_tissues": {
"train": dl_manager.download_and_extract(_URLS["ned"]["tissue_train"]),
"test": dl_manager.download_and_extract(_URLS["ned"]["tissue_test"]),
"dev": dl_manager.download_and_extract(_URLS["ned"]["tissue_val"]),
},
"flambe_ned_tools": {
"train": dl_manager.download_and_extract(_URLS["ned"]["tool_train"]),
"test": dl_manager.download_and_extract(_URLS["ned"]["tool_test"]),
"dev": dl_manager.download_and_extract(_URLS["ned"]["tool_val"]),
},
"flambe_fulltext_tools_bigbio_text": {
"train": os.path.join(data_dir, "data/tags/fulltext_iob/fulltext_tools_train.iob"),
"test": os.path.join(data_dir, "data/tags/fulltext_iob/fulltext_tools_test.iob"),
"dev": os.path.join(data_dir, "data/tags/fulltext_iob/fulltext_tools_validation.iob"),
},
"flambe_fulltext_tissues_bigbio_text": {
"train": os.path.join(data_dir, "data/tags/fulltext_iob/fulltext_tissues_train.iob"),
"test": os.path.join(data_dir, "data/tags/fulltext_iob/fulltext_tissues_test.iob"),
"dev": os.path.join(data_dir, "data/tags/fulltext_iob/fulltext_tissues_validation.iob"),
},
"flambe_abstract_tissues_bigbio_text": {
"train": os.path.join(data_dir, "data/tags/abstract_iob/abstract_tissues_train.iob"),
"test": os.path.join(data_dir, "data/tags/abstract_iob/abstract_tissues_test.iob"),
"dev": os.path.join(data_dir, "data/tags/abstract_iob/abstract_tissues_validation.iob"),
},
}
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"filepath": path[self.config.name]["train"],
"split": "train",
},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={
"filepath": path[self.config.name]["test"],
"split": "test",
},
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs={
"filepath": path[self.config.name]["dev"],
"split": "dev",
},
),
]
def _generate_examples(self, filepath, split: str) -> Tuple[int, Dict]:
"""Yields examples as (key, example) tuples."""
if self.config.schema == "source":
with open(filepath, "r") as f:
id_value = None
tokens = []
tags = []
key = 0
for line in f:
line = line.strip()
if line:
parts = line.split()
if parts[1] == "begin":
if id_value is not None:
yield key, {"id": id_value, "tokens": tokens, "tags": tags}
key += 1
tokens = []
tags = []
id_value = parts[0]
elif parts[1] == "end":
yield key, {"id": id_value, "tokens": tokens, "tags": tags}
key += 1
id_value = None
tokens = []
tags = []
else:
tokens.append(parts[0])
tags.append(parts[1])
if id_value is not None:
yield key, {"id": id_value, "tokens": tokens, "tags": tags}
key += 1
elif self.config.schema == "bigbio_text":
with open(filepath, "r") as f:
id_value = None
tokens = []
tags = []
key = 0
for line in f:
line = line.strip()
if line:
parts = line.split()
if parts[1] == "begin":
if id_value is not None:
yield key, {
"id": key,
"document_id": id_value,
"text": " ".join(tokens),
"labels": tags,
}
key += 1
tokens = []
tags = []
id_value = parts[0]
elif parts[1] == "end":
yield key, {
"id": key,
"document_id": id_value,
"text": " ".join(tokens),
"labels": tags,
}
key += 1
id_value = None
tokens = []
tags = []
else:
tokens.append(parts[0])
tags.append(parts[1])
if id_value is not None:
yield key, {
"id": key,
"document_id": id_value,
"text": " ".join(tokens),
"labels": tags,
}
key += 1
elif self.config.schema == "source_ned_tissue":
key = 0
for line in open(filepath):
csv_row = line.strip("\n").split(",")
if csv_row is not None:
yield key, {"orginal_text": csv_row[0], "mapped_NCIT": csv_row[1], "NCIT_name": csv_row[2]}
key += 1
elif self.config.schema == "source_ned_tool":
key = 0
for line in open(filepath):
csv_row = line.strip("\n").split(",")
if csv_row is not None:
yield key, {"orginal_text": csv_row[0], "standardized_name": csv_row[1], "url": csv_row[2]}
key += 1
if __name__ == "__main__":
datasets.load_dataset(__file__)
|