-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_pubs_complete.py
More file actions
295 lines (253 loc) · 11.4 KB
/
update_pubs_complete.py
File metadata and controls
295 lines (253 loc) · 11.4 KB
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
#!/usr/bin/env python3
"""
Complete script to update publications:
1. Download image icons for specified papers
2. Update code links for ALL papers from JSON
"""
import json
import re
import os
from pathlib import Path
from urllib.parse import urlparse
from urllib.request import urlopen, Request
from urllib.error import URLError, HTTPError
# Papers that need image icons downloaded
PAPERS_TO_UPDATE_IMAGES = [
"Mammo-CLIP: A Vision Language Foundation Model to Enhance Data Efficiency and Robustness in Mammography",
"Anatomy-specific Progression Classification in Chest Radiographs via Weakly Supervised Learning",
"Beyond Distribution Shift: Spurious Features Through the Lens of Training Dynamics",
"Automated Detection of Premalignant Oral Lesions on Whole Slide Images Using CNN",
"Anatomy-Guided Weakly-Supervised Abnormality Localization in Chest X-rays",
"Adversarial Consistency for Single Domain Generalization in Medical Image Segmentation",
"Hierarchical Amortized Training for Memory-efficient High-Resolution 3D GAN",
"Maximum Spatial Perturbation Consistency for Unpaired Image-to-Image Translation",
"Knowledge Distillation via Constrained Variational Inference"
]
def download_image(url, save_path):
"""Download an image from URL to save_path."""
try:
# Fix URL if it has double 'h' (like in Anatomy-Guided)
if url.startswith('hhttps'):
url = url[1:]
# Create directory if it doesn't exist
os.makedirs(os.path.dirname(save_path), exist_ok=True)
# Determine file extension from URL
parsed_url = urlparse(url)
path = parsed_url.path
if path.endswith(('.png', '.jpg', '.jpeg', '.gif')):
ext = os.path.splitext(path)[1]
else:
ext = '.png'
# Update save_path with correct extension
if not save_path.endswith(ext):
save_path = os.path.splitext(save_path)[0] + ext
# Download with urllib
req = Request(url, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'})
with urlopen(req, timeout=30) as response:
content_type = response.headers.get('content-type', '')
# Update extension based on content type if needed
if 'image/jpeg' in content_type and not save_path.endswith('.jpg'):
save_path = os.path.splitext(save_path)[0] + '.jpg'
with open(save_path, 'wb') as f:
f.write(response.read())
print(f"✓ Downloaded: {os.path.basename(save_path)}")
return save_path
except (URLError, HTTPError, Exception) as e:
print(f"✗ Failed to download {url[:60]}...: {e}")
return None
def sanitize_filename(title):
"""Create a safe filename from paper title."""
# Remove special characters and limit length
safe = re.sub(r'[^\w\s-]', '', title)
safe = re.sub(r'[-\s]+', '_', safe)
return safe[:80] # Limit length
def find_paper_in_html(html_content, title):
"""Find the article block for a paper by title."""
# Escape special regex characters in title, but allow flexible matching
# The title in HTML might have slight variations
escaped_title = re.escape(title)
# Match the article block containing this title (case insensitive)
pattern = rf'(<article class="item-row">.*?<h5[^>]*>{escaped_title}</h5>.*?</article>)'
match = re.search(pattern, html_content, re.DOTALL | re.IGNORECASE)
return match
def update_image_in_html(html_content, title, image_path):
"""Update the image src for a paper."""
# Find the article block
match = find_paper_in_html(html_content, title)
if not match:
print(f"⚠ Could not find paper: {title[:60]}...")
return html_content
article_block = match.group(1)
# Update the img src attribute
updated_block = re.sub(
r'(<img src=")[^"]+(" alt="[^"]*" onerror="[^"]*")',
rf'\1{image_path}\2',
article_block
)
# Replace in full HTML
html_content = html_content[:match.start()] + updated_block + html_content[match.end():]
print(f"✓ Updated image for: {title[:60]}...")
return html_content
def update_code_link_in_html(html_content, title, code_link):
"""Update or add code link for a paper."""
if not code_link:
return html_content
# Find the article block
match = find_paper_in_html(html_content, title)
if not match:
print(f"⚠ Could not find paper for code link: {title[:60]}...")
return html_content
article_block = match.group(1)
# Check if code link already exists
code_link_pattern = r'<a href="[^"]*"><i class="bi bi-github"></i> Code</a>'
code_link_html = f'<a href="{code_link}"><i class="bi bi-github"></i> Code</a>'
if re.search(code_link_pattern, article_block):
# Update existing code link
updated_block = re.sub(
r'(<a href=")[^"]*("<i class="bi bi-github"></i> Code</a>)',
rf'\1{code_link}\2',
article_block
)
else:
# Add code link to meta-links div
if '<div class="meta-links mb-2">' in article_block:
# Add code link to existing meta-links
updated_block = re.sub(
r'(<div class="meta-links mb-2">)(.*?)(</div>)',
rf'\1\2 {code_link_html}\3',
article_block
)
else:
# Create meta-links div if it doesn't exist
# Try to find venue div and add after it
if '<div class="mb-1"><span class="fw-semibold">Venue:</span>' in article_block:
updated_block = re.sub(
r'(<div class="mb-1"><span class="fw-semibold">Venue:</span>[^<]*</div>)',
rf'\1\n <div class="meta-links mb-2">{code_link_html}</div>',
article_block
)
else:
# Add before closing div
updated_block = re.sub(
r'(</div>\s*</div>\s*</article>)',
rf' <div class="meta-links mb-2">{code_link_html}</div>\n </div>\n </article>',
article_block
)
# Replace in full HTML
html_content = html_content[:match.start()] + updated_block + html_content[match.end():]
print(f"✓ Updated code link for: {title[:60]}...")
return html_content
def main():
base_dir = Path(__file__).parent
index_html_path = base_dir / "index.html"
images_dir = base_dir / "images" / "publications"
images_dir.mkdir(parents=True, exist_ok=True)
# Try to load JSON from various possible files
json_files = [
base_dir / "publications_complete.json",
base_dir / "publications_full.json",
base_dir / "publications_data.json",
base_dir / "publications_new.json",
base_dir / "publications_user_data.json"
]
full_json_data = None
for json_file in json_files:
if json_file.exists():
try:
with open(json_file, 'r', encoding='utf-8') as f:
full_json_data = json.load(f)
print(f"✓ Loaded JSON from {json_file.name}")
break
except Exception as e:
print(f"⚠ Error loading {json_file.name}: {e}")
continue
if not full_json_data:
print("✗ No valid JSON file found. Please ensure publications_full.json exists.")
return
# Read HTML
print(f"\nReading {index_html_path}...")
with open(index_html_path, 'r', encoding='utf-8') as f:
html_content = f.read()
# Step 1: Download images for specified papers
print("\n" + "=" * 60)
print("Step 1: Downloading images for specified papers...")
print("=" * 60)
# Create a mapping of title -> paper data
title_to_paper = {}
for year, papers in full_json_data.items():
for paper in papers:
title = paper.get("title", "")
if title:
title_to_paper[title] = paper
# Download images and update HTML
downloaded_count = 0
for title in PAPERS_TO_UPDATE_IMAGES:
if title in title_to_paper:
paper = title_to_paper[title]
img_url = paper.get("image_icon_link", "")
if not img_url:
print(f"⚠ No image URL for: {title[:60]}...")
continue
# Create filename based on existing naming convention
# Use year and a sanitized version of title
year = None
for y, papers in full_json_data.items():
if paper in papers:
year = y
break
# Find the index in that year
if year:
idx = full_json_data[year].index(paper) + 1
# Use similar naming to existing files: YEAR_NN_Title.png
safe_title = sanitize_filename(title)
if img_url.endswith('.jpg') or img_url.endswith('.jpeg'):
img_filename = f"{year}_{idx:02d}_{safe_title}.jpg"
else:
img_filename = f"{year}_{idx:02d}_{safe_title}.png"
else:
safe_title = sanitize_filename(title)
if img_url.endswith('.jpg') or img_url.endswith('.jpeg'):
img_filename = f"{safe_title}.jpg"
else:
img_filename = f"{safe_title}.png"
img_path = images_dir / img_filename
relative_path = f"images/publications/{img_filename}"
# Download if not exists
if not img_path.exists():
downloaded_path = download_image(img_url, str(img_path))
if downloaded_path:
downloaded_count += 1
# Update path to use downloaded filename
img_path = Path(downloaded_path)
relative_path = f"images/publications/{img_path.name}"
else:
print(f"✓ Image already exists: {img_filename}")
# Update HTML
html_content = update_image_in_html(html_content, title, relative_path)
else:
print(f"⚠ Paper not found in JSON: {title[:60]}...")
print(f"\nDownloaded {downloaded_count} new images")
# Step 2: Update code links for ALL papers
print("\n" + "=" * 60)
print("Step 2: Updating code links for all papers...")
print("=" * 60)
updated_code_count = 0
for year, papers in full_json_data.items():
for paper in papers:
title = paper.get("title", "")
code_link = paper.get("code_link")
if title and code_link:
old_content = html_content
html_content = update_code_link_in_html(html_content, title, code_link)
if html_content != old_content:
updated_code_count += 1
print(f"\nUpdated {updated_code_count} code links")
# Write updated HTML
print(f"\nWriting updated HTML to {index_html_path}...")
with open(index_html_path, 'w', encoding='utf-8') as f:
f.write(html_content)
print("\n" + "=" * 60)
print("✓ Successfully updated index.html")
print("=" * 60)
if __name__ == "__main__":
main()