Update code and bump baseline date
Browse files- pmc_open_access_figure.py +27 -15
pmc_open_access_figure.py
CHANGED
|
@@ -34,6 +34,9 @@ import tarfile
|
|
| 34 |
import datasets
|
| 35 |
from datasets.tasks import LanguageModeling
|
| 36 |
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# TODO: Add BibTeX citation
|
| 39 |
# Find for instance the citation on arxiv or on the dataset repo/website
|
|
@@ -71,7 +74,7 @@ _SUBSETS = {
|
|
| 71 |
"non_commercial": "oa_noncomm",
|
| 72 |
"other": "oa_other",
|
| 73 |
}
|
| 74 |
-
_BASELINE_DATE = "2022-
|
| 75 |
|
| 76 |
begin_doc_rgx = re.compile("""<!DOCTYPE.*""")
|
| 77 |
def clean_raw(xml_text):
|
|
@@ -89,6 +92,13 @@ def clean_raw(xml_text):
|
|
| 89 |
|
| 90 |
return xml_text
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
def extract_captions(article_tree):
|
| 93 |
ref_el_l = article_tree.xpath(".//fig")
|
| 94 |
figure_captions = []
|
|
@@ -134,7 +144,8 @@ class OpenAccessFigure(datasets.GeneratorBasedBuilder):
|
|
| 134 |
{
|
| 135 |
"accession_id": datasets.Value("string"),
|
| 136 |
"pmid": datasets.Value("string"),
|
| 137 |
-
"figure_idx": datasets.Value("
|
|
|
|
| 138 |
"figure": datasets.Image(),
|
| 139 |
"caption": datasets.Value("string"),
|
| 140 |
"license": datasets.Value("string"),
|
|
@@ -156,7 +167,7 @@ class OpenAccessFigure(datasets.GeneratorBasedBuilder):
|
|
| 156 |
for subset in self.config.subsets:
|
| 157 |
url = _URL.format(subset=_SUBSETS[subset])
|
| 158 |
basename = f"{_SUBSETS[subset]}_xml."
|
| 159 |
-
baseline_file_list_urls = [f"{url}{basename}PMC00{i}xxxxxx.baseline.{_BASELINE_DATE}.filelist.csv" for i in range(
|
| 160 |
baseline_file_list_l.extend(dl_manager.download(baseline_file_list_urls))
|
| 161 |
|
| 162 |
#date_delta = datetime.date.today() - datetime.date.fromisoformat(_BASELINE_DATE)
|
|
@@ -176,7 +187,7 @@ class OpenAccessFigure(datasets.GeneratorBasedBuilder):
|
|
| 176 |
figure_archives = []
|
| 177 |
df_l = []
|
| 178 |
set_article = set()
|
| 179 |
-
for l, baseline_file_list in enumerate(baseline_file_list_l): # incremental_file_list_l[::-1] +
|
| 180 |
try:
|
| 181 |
file_list = pd.read_csv(baseline_file_list, index_col="AccessionID")
|
| 182 |
except FileNotFoundError: # File not found can happen here in stream mode
|
|
@@ -186,14 +197,14 @@ class OpenAccessFigure(datasets.GeneratorBasedBuilder):
|
|
| 186 |
#mask = (~file_list.File.isin(set_article)) & (file_list.File!="")
|
| 187 |
#file_list = file_list[mask]
|
| 188 |
figure_url_l = list(_URL_ROOT + file_list.File) #[f"{_URL_ROOT}{figure_path}" for figure_path in file_list.File]
|
| 189 |
-
|
| 190 |
-
try
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
except FileNotFoundError:
|
| 196 |
-
|
| 197 |
|
| 198 |
package_df = pd.concat(df_l).reset_index()
|
| 199 |
figure_archives = list(chain(*figure_archives))
|
|
@@ -243,7 +254,7 @@ class OpenAccessFigure(datasets.GeneratorBasedBuilder):
|
|
| 243 |
try:
|
| 244 |
for path, file in figure_archive:
|
| 245 |
bn, ext = os.path.splitext(os.path.basename(path))
|
| 246 |
-
if ext in
|
| 247 |
f_d[ext][bn] = Image.open(file, mode="r")
|
| 248 |
|
| 249 |
if ext in [".nxml", ".xml"]:
|
|
@@ -257,7 +268,7 @@ class OpenAccessFigure(datasets.GeneratorBasedBuilder):
|
|
| 257 |
figure_captions, graphic_names = extract_captions(article_tree)
|
| 258 |
|
| 259 |
image_d = {}
|
| 260 |
-
for ext in [".tif", ".jpg", ".png", ".gif"]:
|
| 261 |
for bn, image in f_d[ext].items():
|
| 262 |
if bn not in image_d.keys():
|
| 263 |
image_d[bn] = image
|
|
@@ -269,7 +280,8 @@ class OpenAccessFigure(datasets.GeneratorBasedBuilder):
|
|
| 269 |
"caption":caption,
|
| 270 |
"pmid": data["PMID"],
|
| 271 |
"accession_id": data['AccessionID'],
|
| 272 |
-
"figure_idx":
|
|
|
|
| 273 |
"license": data["License"],
|
| 274 |
"last_updated": data["LastUpdated (YYYY-MM-DD HH:MM:SS)"],
|
| 275 |
"retracted": data["Retracted"],
|
|
|
|
| 34 |
import datasets
|
| 35 |
from datasets.tasks import LanguageModeling
|
| 36 |
|
| 37 |
+
from PIL import ImageFile # Important for error: UserWarning: Corrupt EXIF data. Expecting to read 4 bytes but only got 0
|
| 38 |
+
import mimetypes
|
| 39 |
+
ImageFile.LOAD_TRUNCATED_IMAGES = True
|
| 40 |
|
| 41 |
# TODO: Add BibTeX citation
|
| 42 |
# Find for instance the citation on arxiv or on the dataset repo/website
|
|
|
|
| 74 |
"non_commercial": "oa_noncomm",
|
| 75 |
"other": "oa_other",
|
| 76 |
}
|
| 77 |
+
_BASELINE_DATE = "2022-12-18"
|
| 78 |
|
| 79 |
begin_doc_rgx = re.compile("""<!DOCTYPE.*""")
|
| 80 |
def clean_raw(xml_text):
|
|
|
|
| 92 |
|
| 93 |
return xml_text
|
| 94 |
|
| 95 |
+
def get_extensions_for_type(general_type):
|
| 96 |
+
for ext in mimetypes.types_map:
|
| 97 |
+
if mimetypes.types_map[ext].split('/')[0] == general_type:
|
| 98 |
+
yield ext
|
| 99 |
+
|
| 100 |
+
IMAGE_EXT = list(get_extensions_for_type('image'))
|
| 101 |
+
|
| 102 |
def extract_captions(article_tree):
|
| 103 |
ref_el_l = article_tree.xpath(".//fig")
|
| 104 |
figure_captions = []
|
|
|
|
| 144 |
{
|
| 145 |
"accession_id": datasets.Value("string"),
|
| 146 |
"pmid": datasets.Value("string"),
|
| 147 |
+
"figure_idx": datasets.Value("int16"),
|
| 148 |
+
"figure_fn": datasets.Value("string"),
|
| 149 |
"figure": datasets.Image(),
|
| 150 |
"caption": datasets.Value("string"),
|
| 151 |
"license": datasets.Value("string"),
|
|
|
|
| 167 |
for subset in self.config.subsets:
|
| 168 |
url = _URL.format(subset=_SUBSETS[subset])
|
| 169 |
basename = f"{_SUBSETS[subset]}_xml."
|
| 170 |
+
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)]
|
| 171 |
baseline_file_list_l.extend(dl_manager.download(baseline_file_list_urls))
|
| 172 |
|
| 173 |
#date_delta = datetime.date.today() - datetime.date.fromisoformat(_BASELINE_DATE)
|
|
|
|
| 187 |
figure_archives = []
|
| 188 |
df_l = []
|
| 189 |
set_article = set()
|
| 190 |
+
for l, baseline_file_list in enumerate(baseline_file_list_l): # incremental_file_list_l[::-1] +
|
| 191 |
try:
|
| 192 |
file_list = pd.read_csv(baseline_file_list, index_col="AccessionID")
|
| 193 |
except FileNotFoundError: # File not found can happen here in stream mode
|
|
|
|
| 197 |
#mask = (~file_list.File.isin(set_article)) & (file_list.File!="")
|
| 198 |
#file_list = file_list[mask]
|
| 199 |
figure_url_l = list(_URL_ROOT + file_list.File) #[f"{_URL_ROOT}{figure_path}" for figure_path in file_list.File]
|
| 200 |
+
|
| 201 |
+
#try
|
| 202 |
+
figure_archives.append(dl_manager.download(figure_url_l))
|
| 203 |
+
#if l < len(incremental_file_list_l): # Only adding the incrementals to the list, the rest don't have overlap in pmid
|
| 204 |
+
# set_article.union(file_list.File[slc_])
|
| 205 |
+
df_l.append(file_list)
|
| 206 |
+
#except FileNotFoundError:
|
| 207 |
+
# continue
|
| 208 |
|
| 209 |
package_df = pd.concat(df_l).reset_index()
|
| 210 |
figure_archives = list(chain(*figure_archives))
|
|
|
|
| 254 |
try:
|
| 255 |
for path, file in figure_archive:
|
| 256 |
bn, ext = os.path.splitext(os.path.basename(path))
|
| 257 |
+
if ext in IMAGE_EXT:
|
| 258 |
f_d[ext][bn] = Image.open(file, mode="r")
|
| 259 |
|
| 260 |
if ext in [".nxml", ".xml"]:
|
|
|
|
| 268 |
figure_captions, graphic_names = extract_captions(article_tree)
|
| 269 |
|
| 270 |
image_d = {}
|
| 271 |
+
for ext in [".tif", ".jpg", ".png", ".gif"]: #Focussing the dataset only on these four types
|
| 272 |
for bn, image in f_d[ext].items():
|
| 273 |
if bn not in image_d.keys():
|
| 274 |
image_d[bn] = image
|
|
|
|
| 280 |
"caption":caption,
|
| 281 |
"pmid": data["PMID"],
|
| 282 |
"accession_id": data['AccessionID'],
|
| 283 |
+
"figure_idx": j+1,
|
| 284 |
+
"figure_fn": graph_name,
|
| 285 |
"license": data["License"],
|
| 286 |
"last_updated": data["LastUpdated (YYYY-MM-DD HH:MM:SS)"],
|
| 287 |
"retracted": data["Retracted"],
|