Datasets:
EMBO
/

Languages:
English
ArXiv:
DOI:
License:
SourceData / SourceData.py
drAbreu's picture
Update SourceData.py
b114e70
raw
history blame
11 kB
# coding=utf-8
# Copyright 2020 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.
# template from : https://github.com/huggingface/datasets/blob/master/templates/new_dataset_script.py
from __future__ import absolute_import, division, print_function
import json
import datasets
_BASE_URL = "https://huggingface.co/datasets/EMBO/SourceData/resolve/main/"
class SourceData(datasets.GeneratorBasedBuilder):
"""SourceDataNLP provides datasets to train NLP tasks in cell and molecular biology."""
_NER_LABEL_NAMES = [
"O",
"B-SMALL_MOLECULE",
"I-SMALL_MOLECULE",
"B-GENEPROD",
"I-GENEPROD",
"B-SUBCELLULAR",
"I-SUBCELLULAR",
"B-CELL_TYPE",
"I-CELL_TYPE",
"B-TISSUE",
"I-TISSUE",
"B-ORGANISM",
"I-ORGANISM",
"B-EXP_ASSAY",
"I-EXP_ASSAY",
"B-DISEASE",
"I-DISEASE",
"B-CELL_LINE",
"I-CELL_LINE"
]
_SEMANTIC_ROLES = ["O", "B-CONTROLLED_VAR", "I-CONTROLLED_VAR", "B-MEASURED_VAR", "I-MEASURED_VAR"]
_PANEL_START_NAMES = ["O", "B-PANEL_START", "I-PANEL_START"]
_ROLES_MULTI = ["O", "GENEPROD", "SMALL_MOLECULE"]
_CITATION = """\
@Unpublished{
huggingface: dataset,
title = {SourceData NLP},
authors={Thomas Lemberger & Jorge Abreu-Vicente, EMBO},
year={2023}
}
"""
_DESCRIPTION = """\
This dataset is based on the SourceData database and is intented to facilitate training of NLP tasks in the cell and molecualr biology domain.
"""
_HOMEPAGE = "https://huggingface.co/datasets/EMBO/SourceData"
_LICENSE = "CC-BY 4.0"
DEFAULT_CONFIG_NAME = "NER"
def _info(self):
self._URLS = {
"NER": f"{_BASE_URL}token_classification/v_{self.config.version}/ner/",
"PANELIZATION": f"{_BASE_URL}token_classification/v_{self.config.version}/panelization/",
"ROLES_GP": f"{_BASE_URL}token_classification/v_{self.config.version}/roles_gene/",
"ROLES_SM": f"{_BASE_URL}token_classification/v_{self.config.version}/roles_small_mol/",
"ROLES_MULTI": f"{_BASE_URL}token_classification/v_{self.config.version}/roles_multi/",
}
self.BUILDER_CONFIGS = [
datasets.BuilderConfig(name="NER", version=self.config.version, description="Dataset for named-entity recognition."),
datasets.BuilderConfig(name="PANELIZATION", version=self.config.version, description="Dataset to separate figure captions into panels."),
datasets.BuilderConfig(name="ROLES_GP", version=self.config.version, description="Dataset for semantic roles of gene products."),
datasets.BuilderConfig(name="ROLES_SM", version=self.config.version, description="Dataset for semantic roles of small molecules."),
datasets.BuilderConfig(name="ROLES_MULTI", version=self.config.version, description="Dataset to train roles. ROLES_GP and ROLES_SM at once."),
]
if self.config.name == "NER":
features = datasets.Features(
{
"words": datasets.Sequence(feature=datasets.Value("string")),
"labels": datasets.Sequence(
feature=datasets.ClassLabel(num_classes=len(self._NER_LABEL_NAMES),
names=self._NER_LABEL_NAMES)
),
# "is_category": datasets.Sequence(feature=datasets.Value("int8")),
"tag_mask": datasets.Sequence(feature=datasets.Value("int8")),
"text": datasets.Value("string"),
}
)
elif self.config.name == "ROLES_GP":
features = datasets.Features(
{
"words": datasets.Sequence(feature=datasets.Value("string")),
"labels": datasets.Sequence(
feature=datasets.ClassLabel(
num_classes=len(self._SEMANTIC_ROLES),
names=self._SEMANTIC_ROLES
)
),
# "is_category": datasets.Sequence(feature=datasets.Value("int8")),
"tag_mask": datasets.Sequence(feature=datasets.Value("int8")),
"text": datasets.Value("string"),
}
)
elif self.config.name == "ROLES_SM":
features = datasets.Features(
{
"words": datasets.Sequence(feature=datasets.Value("string")),
"labels": datasets.Sequence(
feature=datasets.ClassLabel(
num_classes=len(self._SEMANTIC_ROLES),
names=self._SEMANTIC_ROLES
)
),
# "is_category": datasets.Sequence(feature=datasets.Value("int8")),
"tag_mask": datasets.Sequence(feature=datasets.Value("int8")),
"text": datasets.Value("string"),
}
)
elif self.config.name == "ROLES_MULTI":
features = datasets.Features(
{
"words": datasets.Sequence(feature=datasets.Value("string")),
"labels": datasets.Sequence(
feature=datasets.ClassLabel(
num_classes=len(self._SEMANTIC_ROLES),
names=self._SEMANTIC_ROLES
)
),
"is_category": datasets.Sequence(
feature=datasets.ClassLabel(
num_classes=len(self._ROLES_MULTI),
names=self._ROLES_MULTI
)
),
"tag_mask": datasets.Sequence(feature=datasets.Value("int8")),
"text": datasets.Value("string"),
}
)
elif self.config.name == "PANELIZATION":
features = datasets.Features(
{
"words": datasets.Sequence(feature=datasets.Value("string")),
"labels": datasets.Sequence(
feature=datasets.ClassLabel(num_classes=len(self._PANEL_START_NAMES),
names=self._PANEL_START_NAMES)
),
"tag_mask": datasets.Sequence(feature=datasets.Value("int8")),
}
)
return datasets.DatasetInfo(
description=self._DESCRIPTION,
features=features,
supervised_keys=("words", "label_ids"),
homepage=self._HOMEPAGE,
license=self._LICENSE,
citation=self._CITATION,
)
def _split_generators(self, dl_manager: datasets.DownloadManager):
"""Returns SplitGenerators.
Uses local files if a data_dir is specified. Otherwise downloads the files from their official url."""
try:
urls = [
self._URLS[self.config.name]+"train.jsonl",
self._URLS[self.config.name]+"test.jsonl",
self._URLS[self.config.name]+"validation.jsonl"
]
data_files = dl_manager.download(urls)
except:
raise ValueError(f"unkonwn config name: {self.config.name}")
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"filepath": data_files[0]},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={
"filepath": data_files[1]},
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs={
"filepath": data_files[2]},
),
]
def _generate_examples(self, filepath):
"""Yields examples. This method will receive as arguments the `gen_kwargs` defined in the previous `_split_generators` method.
It is in charge of opening the given file and yielding (key, example) tuples from the dataset
The key is not important, it's more here for legacy reason (legacy from tfds)"""
with open(filepath, encoding="utf-8") as f:
# logger.info("⏳ Generating examples from = %s", filepath)
for id_, row in enumerate(f):
data = json.loads(row)
if self.config.name == "NER":
yield id_, {
"words": data["words"],
"labels": data["labels"],
"tag_mask": data["is_category"],
"text": data["text"]
}
elif self.config.name == "ROLES_GP":
yield id_, {
"words": data["words"],
"labels": data["labels"],
"tag_mask": data["is_category"],
"text": data["text"]
}
elif self.config.name == "ROLES_MULTI":
labels = data["labels"]
tag_mask = [1 if t!=0 else 0 for t in labels]
yield id_, {
"words": data["words"],
"labels": data["labels"],
"tag_mask": tag_mask,
"category": data["is_category"],
"text": data["text"]
}
elif self.config.name == "ROLES_SM":
yield id_, {
"words": data["words"],
"labels": data["labels"],
"tag_mask": data["is_category"],
"text": data["text"]
}
elif self.config.name == "PANELIZATION":
labels = data["labels"]
tag_mask = [1 if t == "B-PANEL_START" else 0 for t in labels]
yield id_, {
"words": data["words"],
"labels": data["labels"],
"tag_mask": tag_mask,
}