File size: 12,806 Bytes
bee03a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1ab669
bee03a2
 
 
f7838cd
 
 
bee03a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21f36f5
bee03a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7838cd
 
 
 
 
 
 
bee03a2
 
 
 
 
4f3b68b
 
 
 
bee03a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7838cd
 
bee03a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7838cd
bee03a2
 
1088738
 
 
 
 
 
 
 
 
 
 
bee03a2
 
 
 
 
 
f7838cd
bee03a2
 
 
 
 
 
1088738
 
5a9abed
f7838cd
 
 
 
 
 
 
 
34dc2c6
bee03a2
5a9abed
bee03a2
 
 
 
 
1088738
bee03a2
 
 
 
 
 
1088738
bee03a2
 
 
 
 
 
1088738
bee03a2
 
 
 
1088738
 
 
 
 
 
 
 
 
 
 
 
bee03a2
 
 
 
 
 
 
f8eeb59
 
 
 
 
 
 
 
 
 
bee03a2
f8eeb59
b9fb443
 
 
 
 
 
f8eeb59
 
b9fb443
f8eeb59
 
 
 
 
 
 
 
 
 
 
f7838cd
 
f8eeb59
 
 
 
47569c1
f8eeb59
 
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
# 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.
#
# This dataset script is based on pmc/open_access.py loading script.

"""PMC Open Access Subset of figures with captions"""
from huggingface_hub import hf_hub_url
import datetime
import pandas as pd
import numpy as np
from itertools import compress, chain
from collections import defaultdict
import os
import re
from lxml import etree
import unicodedata
import html
import json
from PIL import Image
import tarfile

import datasets

from PIL import ImageFile # Important for error: UserWarning: Corrupt EXIF data.  Expecting to read 4 bytes but only got 0
import mimetypes
ImageFile.LOAD_TRUNCATED_IMAGES = True

# TODO: Add BibTeX citation
# Find for instance the citation on arxiv or on the dataset repo/website
_CITATION = ""

_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

This version focus on associating the graphics of figures with their captions
"""

_HOMEPAGE = "https://www.ncbi.nlm.nih.gov/pmc/tools/openftlist/"

# TODO: Add the licence for the dataset here if you can find it
_LICENSE = """
https://www.ncbi.nlm.nih.gov/pmc/about/copyright/

Within the PMC Open Access Subset, there are three groupings:

Commercial Use Allowed - CC0, CC BY, CC BY-SA, CC BY-ND licenses
Non-Commercial Use Only - CC BY-NC, CC BY-NC-SA, CC BY-NC-ND licenses; and
Other - no machine-readable Creative Commons license, no license, or a custom license.
"""

_URL_ROOT = "https://ftp.ncbi.nlm.nih.gov/pub/pmc/"
_URL      = _URL_ROOT+"oa_bulk/{subset}/xml/"

_SUBSETS = {
    "commercial": "oa_comm",
    "non_commercial": "oa_noncomm",
    "other": "oa_other",
}
_BASELINE_DATE = "2023-12-18"

begin_doc_rgx = re.compile("""<!DOCTYPE.*""")
def clean_raw(xml_text):
    """
    Fixes the formating of xml of files and returns it.
    Some have bad formating but they can be fixed/improved
    """
    #Some XML can't be parsed because they are not starting with the DOCTYPE declaration
    # Could be disabled if we handle the parsing error (TBD, how many files would be trashed)

    begin_doc = begin_doc_rgx.search(xml_text)
    if begin_doc is None:
        return xml_text
    xml_text  = xml_text[begin_doc.start():]

    return xml_text

def get_extensions_for_type(general_type):
    for ext in mimetypes.types_map:
        if mimetypes.types_map[ext].split('/')[0] == general_type:
            yield ext

IMAGE_EXT = list(get_extensions_for_type('image'))

def extract_captions(article_tree):
    ref_el_l = article_tree.xpath(".//fig")
    figure_captions = []
    graphic_names = []
    for el in ref_el_l:
        graphic_l = el.xpath(".//graphic")
        if len(graphic_l) == 0:
            continue
        graphic_el = graphic_l[0]
        graphic_names.append(graphic_el.get("{http://www.w3.org/1999/xlink}href"))
        text = " ".join(el.itertext())
        text = unicodedata.normalize("NFKD", html.unescape(text))
        figure_captions.append(text)
    return figure_captions, graphic_names

class OpenAccessFigureConfig(datasets.BuilderConfig):
    """BuilderConfig for the PMC Open Access Subset."""

    def __init__(self, subsets=None, **kwargs):
        """BuilderConfig for the PMC Open Access Subset.
        Args:
            subsets (:obj:`List[str]`): List of subsets/groups to load.
            **kwargs: Keyword arguments forwarded to super.
        """
        subsets = [subsets] if isinstance(subsets, str) else subsets
        super().__init__(
            name="+".join(subsets), **kwargs,
        )
        self.subsets = subsets if self.name != "all" else list(_SUBSETS.keys())

class OpenAccessFigure(datasets.GeneratorBasedBuilder):
    """PMC Open Access Subset for figure and captions"""

    VERSION = datasets.Version("1.0.0")
    BUILDER_CONFIG_CLASS = OpenAccessFigureConfig
    BUILDER_CONFIGS = [OpenAccessFigureConfig(subsets="all")] + [OpenAccessFigureConfig(subsets=subset) for subset in _SUBSETS]
    DEFAULT_CONFIG_NAME = "all"

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "accession_id": datasets.Value("string"),
                    "pmid":         datasets.Value("string"),
                    "figure_idx":    datasets.Value("int16"),
					"figure_fn":    datasets.Value("string"),
                    "figure":  datasets.Image(),
                    "caption": datasets.Value("string"),
                    "license": datasets.Value("string"),
                    "retracted": datasets.Value("string"),
                    "last_updated": datasets.Value("string"),
                    "citation": datasets.Value("string"),
                }
            ),
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):

        baseline_package_list = dl_manager.download(f"{_URL_ROOT}oa_file_list.csv")
        baseline_file_list_l, incremental_file_list_l = [], []
        for subset in self.config.subsets:
            url = _URL.format(subset=_SUBSETS[subset])
            basename = f"{_SUBSETS[subset]}_xml."
            baseline_file_list_urls = [f"{url}{basename}PMC00{i}xxxxxx.baseline.{_BASELINE_DATE}.filelist.csv" for i in range(10) if (subset!="non_commercial" or i>0)]
            baseline_file_list_l.extend(dl_manager.download(baseline_file_list_urls))

            #date_delta = datetime.date.today() - 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)
            #                    ]
            #incremental_urls = [f"{url}{basename}incr.{date}.filelist.csv" for date in incremental_dates]
            #for url in incremental_urls:
            #    try:
            #        incremental_file_list_l.append(dl_manager.download(url))
            #    except FileNotFoundError:  # Some increment don't exist
            #        continue

        oa_package_list = pd.read_csv(baseline_package_list, index_col="Accession ID")
        oa_package_list = oa_package_list[["File"]]
        figure_archives = []
        df_l = []
        set_article = set()
        for l, baseline_file_list in enumerate(baseline_file_list_l): # incremental_file_list_l[::-1] +
            try:
                file_list = pd.read_csv(baseline_file_list, index_col="AccessionID")
            except FileNotFoundError:  # File not found can happen here in stream mode
                continue
            file_list = file_list.join(oa_package_list).reset_index().set_index("Article File")
            file_list.File = file_list.File.fillna('')
            #mask = (~file_list.File.isin(set_article)) & (file_list.File!="")
            #file_list = file_list[mask]
            figure_url_l = list(_URL_ROOT + file_list.File) #[f"{_URL_ROOT}{figure_path}" for figure_path in file_list.File]

            #try
            figure_archives.append(dl_manager.download(figure_url_l))
            #if l < len(incremental_file_list_l): # Only adding the incrementals to the list, the rest don't have overlap in pmid
            #    set_article.union(file_list.File[slc_])
            df_l.append(file_list)
            #except FileNotFoundError:
            #    continue
                
        package_df = pd.concat(df_l).reset_index()
        figure_archives = list(chain(*figure_archives))

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "figure_archive_lists": self.archive_generator(dl_manager, figure_archives, "train"),
                    "package_df": package_df[np.arange(len(package_df))%10 < 8],
                    },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "figure_archive_lists": self.archive_generator(dl_manager, figure_archives, "test"),
                    "package_df": package_df[np.arange(len(package_df))%10 == 8],
                    },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "figure_archive_lists": self.archive_generator(dl_manager, figure_archives, "validation"),
                    "package_df": package_df[np.arange(len(package_df))%10 == 9],
                    },
            ),
        ]
        
    def archive_generator(self, dl_manager, figure_archives, name):
        if name == "train":
            for k, archive in enumerate(figure_archives):
                if k%10 < 8:
                    yield dl_manager.iter_archive(archive)
        elif name == "test":
            for k, archive in enumerate(figure_archives[8::10]):
                yield dl_manager.iter_archive(archive)
        elif name == "validation":
            for k, archive in enumerate(figure_archives[9::10]):
                yield dl_manager.iter_archive(archive)

    def _generate_examples(self, figure_archive_lists, package_df):
        #Loading the file listing folders of individual PMC Article package (with medias and graphics)
        for i, figure_archive in enumerate(figure_archive_lists):
            data = package_df.iloc[i]
            f_d = defaultdict(lambda: {})
            file_xml = None
            try:
                for path, file in figure_archive:
                    bn, ext = os.path.splitext(os.path.basename(path))
                    if ext in [".nxml", ".xml"]:
                        content = file.read()
                        try:
                            text = content.decode("utf-8").strip()
                        except UnicodeDecodeError as e:
                            text = content.decode("latin-1").strip()
                        text = clean_raw(text)
                        article_tree = etree.ElementTree(etree.fromstring(text))
                        figure_captions, graphic_names = extract_captions(article_tree)
                        break
                        
                for path, file in figure_archive:
                    bn, ext = os.path.splitext(os.path.basename(path))
                    if ext in IMAGE_EXT and bn in graphic_names:
                        f_d[ext][bn] = Image.open(file, mode="r")
    
                image_d = {}
                for ext in [".tif", ".jpg", ".png", ".gif"]:
                    for bn, image in f_d[ext].items():
                        if bn not in image_d.keys():
                            image_d[bn] = image
    
                for j, (caption, graph_name) in enumerate(zip(figure_captions, graphic_names)):
                    if graph_name in image_d.keys():
                        yield (f"{data['AccessionID']}_{str(j+1)}",
                               {"figure": image_d[graph_name],
                                "caption":caption,
                                "pmid": data["PMID"],
                                "accession_id": data['AccessionID'],
                                "figure_idx": j+1,
								"figure_fn": graph_name,
                                "license": data["License"],
                                "last_updated": data["LastUpdated (YYYY-MM-DD HH:MM:SS)"],
                                "retracted": data["Retracted"],
                                "citation": data["Article Citation"]})
            except: #  (etree.XMLSyntaxError, tarfile.ReadError) In some files, xml is broken, and tarfile readerror may happen
                continue