Datasets:
pmc
/

Languages:
English
License:
open_access / open_access.py
albertvillanova's picture
Fix default config name (#14)
b093fd4
raw
history blame
6.75 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.
"""PMC Open Access Subset."""
import datetime
import pandas as pd
import datasets
from datasets.tasks import LanguageModeling
# TODO: Add BibTeX citation
# Find for instance the citation on arxiv or on the dataset repo/website
_CITATION = """\
@InProceedings{huggingface:dataset,
title = {A great new dataset},
author={huggingface, Inc.
},
year={2020}
}
"""
_DESCRIPTION = """\
The PMC Open Access Subset includes more than 3.4 million journal articles and preprints that are made available under
license terms that allow reuse.
Not all articles in PMC are available for text mining and other reuse, many have copyright protection, however articles
in the PMC Open Access Subset are made available under Creative Commons or similar licenses that generally allow more
liberal redistribution and reuse than a traditional copyrighted work.
The PMC Open Access Subset is one part of the PMC Article Datasets
"""
_HOMEPAGE = "https://www.ncbi.nlm.nih.gov/pmc/tools/openftlist/"
# TODO: Add the licence for the dataset here if you can find it
_LICENSE = ""
_URL = "https://ftp.ncbi.nlm.nih.gov/pub/pmc/oa_bulk/{subset}/txt/"
_SUBSETS = {
"commercial": "oa_comm",
"non_commercial": "oa_noncomm",
"other": "oa_other",
}
_BASELINE_DATE = "2023-02-09"
_BASELINE_MAX_RANGE = 10
_BASELINE_RANGES = {
"commercial": range(_BASELINE_MAX_RANGE),
"non_commercial": range(1, _BASELINE_MAX_RANGE), # non-commercial PMC000xxxxxx baseline does not exist
"other": range(_BASELINE_MAX_RANGE),
}
class OpenAccessConfig(datasets.BuilderConfig):
"""BuilderConfig for the PMC Open Access Subset."""
def __init__(self, date=_BASELINE_DATE, subsets=None, **kwargs):
"""BuilderConfig for the PMC Open Access Subset.
Args:
date (`str`, default _BASELINE_DATE) : Up to date, in ISO format. Pass 'latest' for latest date.
subsets (`str` or `list[str]`): List of subsets to load. Possible values are 'all' or any combination
of {'commercial', 'non_commercial', 'other'}.
**kwargs: Keyword arguments forwarded to `BuilderConfig`.
"""
date = datetime.date.today().isoformat() if date=="latest" else date
subsets = [subsets] if isinstance(subsets, str) else subsets
subsets_name = "+".join(subsets)
name = f"{date}.{subsets_name}"
super().__init__(name=name, **kwargs)
self.subsets = subsets if subsets_name != "all" else list(_SUBSETS.keys())
self.date = date
class OpenAccess(datasets.GeneratorBasedBuilder):
"""PMC Open Access Subset."""
VERSION = datasets.Version("1.0.0")
BUILDER_CONFIG_CLASS = OpenAccessConfig
BUILDER_CONFIGS = [OpenAccessConfig(subsets="all")] + [OpenAccessConfig(subsets=subset) for subset in _SUBSETS]
DEFAULT_CONFIG_NAME = f"{_BASELINE_DATE}.all"
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"text": datasets.Value("string"),
"pmid": datasets.Value("string"),
"accession_id": datasets.Value("string"),
"license": datasets.Value("string"),
"last_updated": datasets.Value("string"),
"retracted": datasets.Value("string"),
"citation": datasets.Value("string"),
}
),
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
task_templates=[LanguageModeling(text_column="text")],
)
def _split_generators(self, dl_manager):
paths = []
for subset in self.config.subsets:
url = _URL.format(subset=_SUBSETS[subset])
basename = f"{_SUBSETS[subset]}_txt."
# Baselines
baselines = [f"PMC00{i}xxxxxx.baseline.{_BASELINE_DATE}" for i in _BASELINE_RANGES[subset]]
baseline_urls = [
(f"{url}{basename}{baseline}.filelist.csv", f"{url}{basename}{baseline}.tar.gz")
for baseline in baselines
]
# Incremental
date_delta = datetime.date.fromisoformat(self.config.date) - datetime.date.fromisoformat(_BASELINE_DATE)
incremental_dates = [
(datetime.date.fromisoformat(_BASELINE_DATE) + datetime.timedelta(days=i + 1)).isoformat()
for i in range(date_delta.days)
]
incrementals = [f"incr.{date}" for date in incremental_dates]
incremental_urls = [
(f"{url}{basename}{incremental}.filelist.csv", f"{url}{basename}{incremental}.tar.gz")
for incremental in incrementals
]
paths += dl_manager.download(baseline_urls + incremental_urls)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"paths": [(file_list, dl_manager.iter_archive(archive)) for file_list, archive in paths],
},
),
]
def _generate_examples(self, paths):
key = 0
for file_list, archive in paths:
file_list_data = pd.read_csv(file_list, index_col="Article File").to_dict(orient="index")
for path, file in archive:
data = file_list_data.pop(path)
content = file.read()
try:
text = content.decode("utf-8").strip()
except UnicodeDecodeError as e:
text = content.decode("latin-1").strip()
data = {
"text": text,
"pmid": data["PMID"],
"accession_id": data["AccessionID"],
"license": data["License"],
"last_updated": data["LastUpdated (YYYY-MM-DD HH:MM:SS)"],
"retracted": data["Retracted"],
"citation": data["Article Citation"],
}
yield key, data
key += 1