-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathcreate_mosaic_banner.py
More file actions
291 lines (223 loc) · 9.87 KB
/
create_mosaic_banner.py
File metadata and controls
291 lines (223 loc) · 9.87 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
"""
Create a mosaic banner showcasing all py3plex visualization types.
This script creates an attractive mosaic banner combining the best examples
of py3plex visualizations for use in the README.
"""
import os
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from matplotlib.gridspec import GridSpec
import numpy as np
from PIL import Image
# Default location of example images used when no override is provided
DEFAULT_BASE_DIR = "/home/runner/work/py3plex/py3plex/example_images"
def load_and_resize_image(image_path, target_height=None, target_width=None):
"""Load and optionally resize an image."""
try:
img = Image.open(image_path)
if target_height or target_width:
# Calculate aspect ratio
aspect_ratio = img.width / img.height
if target_height and not target_width:
target_width = int(target_height * aspect_ratio)
elif target_width and not target_height:
target_height = int(target_width / aspect_ratio)
img = img.resize((target_width, target_height), Image.Resampling.LANCZOS)
return np.array(img)
except Exception as e:
print(f"Error loading {image_path}: {e}")
return None
def create_mosaic_banner(base_dir=DEFAULT_BASE_DIR):
"""Create a mosaic banner from existing example images."""
# Select the most representative and attractive visualizations
# These showcase different visualization types
selected_images = [
# Row 1: Multilayer visualizations
"multilayer.png",
"multilayer_edge_projection_spring.png",
"multilayer_flow.png",
# Row 2: Different layout styles
"multilayer_radial_with_inter.png",
"multilayer_supra_heatmap_inter.png",
"hairball.png",
# Row 3: Analysis and special visualizations
"communities.png",
"embedding.png",
"temporal.png",
]
# Create figure with a nice layout
fig = plt.figure(figsize=(20, 12), facecolor='white')
# Use GridSpec for flexible layout
gs = GridSpec(3, 3, figure=fig, wspace=0.05, hspace=0.05,
left=0.02, right=0.98, top=0.98, bottom=0.02)
# Load and display images
for idx, img_name in enumerate(selected_images):
if idx >= 9: # Safety check
break
img_path = os.path.join(base_dir, img_name)
if not os.path.exists(img_path):
print(f"Warning: {img_path} not found, skipping")
continue
row = idx // 3
col = idx % 3
ax = fig.add_subplot(gs[row, col])
try:
img = mpimg.imread(img_path)
ax.imshow(img)
ax.axis('off')
# Add subtle title/caption
viz_name = img_name.replace('_', ' ').replace('.png', '').title()
if viz_name.startswith('Multilayer'):
viz_name = viz_name.replace('Multilayer', 'Multilayer:')
# Add text label at the top
ax.text(0.5, 0.98, viz_name,
transform=ax.transAxes,
fontsize=10,
weight='bold',
ha='center',
va='top',
bbox=dict(boxstyle='round,pad=0.5',
facecolor='white',
edgecolor='none',
alpha=0.8))
except Exception as e:
print(f"Error processing {img_path}: {e}")
ax.text(0.5, 0.5, f'Image not available:\n{img_name}',
ha='center', va='center', fontsize=10)
ax.axis('off')
# Save the mosaic
output_path = os.path.join(base_dir, "py3plex_mosaic_banner.png")
plt.savefig(output_path, dpi=150, bbox_inches='tight',
facecolor='white', edgecolor='none')
print(f"\n Mosaic banner created: {output_path}")
plt.close()
return output_path
def create_compact_banner(base_dir=DEFAULT_BASE_DIR):
"""Create a more compact horizontal banner for README header."""
# Select 5-6 most visually striking images for a compact banner
selected_images = [
"multilayer.png",
"multilayer_flow.png",
"multilayer_edge_projection_spring.png",
"multilayer_radial_with_inter.png",
"multilayer_supra_heatmap_inter.png",
"hairball.png",
]
# Create horizontal banner
fig = plt.figure(figsize=(24, 4), facecolor='white')
gs = GridSpec(1, 6, figure=fig, wspace=0.03, hspace=0.03,
left=0.01, right=0.99, top=0.95, bottom=0.05)
for idx, img_name in enumerate(selected_images):
if idx >= 6:
break
img_path = os.path.join(base_dir, img_name)
if not os.path.exists(img_path):
print(f"Warning: {img_path} not found, skipping")
continue
ax = fig.add_subplot(gs[0, idx])
try:
img = mpimg.imread(img_path)
ax.imshow(img)
ax.axis('off')
except Exception as e:
print(f"Error processing {img_path}: {e}")
# Save the compact banner
output_path = os.path.join(base_dir, "py3plex_banner_horizontal.png")
plt.savefig(output_path, dpi=150, bbox_inches='tight',
facecolor='white', edgecolor='none')
print(f" Compact banner created: {output_path}")
plt.close()
return output_path
def create_showcase_collage(base_dir=DEFAULT_BASE_DIR):
"""Create a showcase collage with uniform tiles, no spacing, black borders, and descriptions."""
# Create a figure with white background
fig = plt.figure(figsize=(24, 9), facecolor='white')
# Create a grid with NO spacing between tiles
# 3 rows x 6 columns: diagonal takes 2x2 (4 tiles), others are 1x1
gs = GridSpec(3, 6, figure=fig, wspace=0, hspace=0,
left=0.02, right=0.98, top=0.96, bottom=0.02)
# Define layout: (row_start, row_end, col_start, col_end, image_name, description)
layout = [
# HERO: Diagonal projection (2x2 = 4 tiles, making it 2x bigger than 1x1 tiles)
(0, 2, 0, 2, "multilayer.png", "Diagonal Projection\nMultilayer Layout"),
# Row 1 - right side
(0, 1, 2, 3, "multilayer_edge_projection_spring.png", "Spring Layout\nEdge Projection"),
(0, 1, 3, 4, "multilayer_small_multiples_shared.png", "Small Multiples\nShared Layout"),
(0, 1, 4, 5, "communities.png", "Community\nDetection"),
(0, 1, 5, 6, "embedding.png", "Node\nEmbeddings"),
# Row 2 - right side
(1, 2, 2, 3, "multilayer_flow.png", "Flow Visualization\nAlluvial Style"),
(1, 2, 3, 4, "multilayer_supra_heatmap_inter.png", "Supra-Adjacency\nHeatmap"),
(1, 2, 4, 5, "hairball.png", "Complex Network\nVisualization"),
(1, 2, 5, 6, "temporal.png", "Temporal\nDynamics"),
# Row 3 - full width
(2, 3, 0, 1, "multilayer_ego_circular.png", "Ego Network\nCircular"),
(2, 3, 1, 2, "multilayer_radial_compact.png", "Radial Compact\nLayout"),
(2, 3, 2, 3, "networkx_wrapper.png", "NetworkX\nIntegration"),
(2, 3, 3, 4, "spreading.png", "Information\nSpreading"),
(2, 3, 4, 5, "part1.png", "Layer\nAnalysis"),
(2, 3, 5, 6, "part2.png", "Network\nStatistics"),
]
for row_start, row_end, col_start, col_end, img_name, description in layout:
img_path = os.path.join(base_dir, img_name)
if not os.path.exists(img_path):
print(f"Warning: {img_path} not found, skipping")
continue
ax = fig.add_subplot(gs[row_start:row_end, col_start:col_end])
try:
img = mpimg.imread(img_path)
ax.imshow(img, aspect='auto') # Use 'auto' to fill the tile uniformly
ax.axis('on') # Turn on axis to show borders
# Add BLACK borders to all tiles
for spine in ax.spines.values():
spine.set_edgecolor('black')
spine.set_linewidth(2)
# Remove tick marks
ax.set_xticks([])
ax.set_yticks([])
# Add description bubble (text box) at the top of each tile
ax.text(0.5, 0.98, description,
transform=ax.transAxes,
fontsize=8 if (row_end - row_start) == 1 else 10,
weight='bold',
ha='center',
va='top',
bbox=dict(boxstyle='round,pad=0.4',
facecolor='white',
edgecolor='black',
linewidth=1.5,
alpha=0.85))
except Exception as e:
print(f"Error processing {img_path}: {e}")
ax.axis('off')
# Add title
fig.text(0.5, 0.99, 'Py3plex: Multilayer Network Analysis & Visualization',
ha='center', va='top', fontsize=22, weight='bold',
color='#212529')
# Save the showcase
output_path = os.path.join(base_dir, "py3plex_showcase.png")
plt.savefig(output_path, dpi=150, bbox_inches='tight',
facecolor='white', edgecolor='none')
print(f" Showcase collage created: {output_path}")
plt.close()
return output_path
if __name__ == "__main__":
print("=" * 70)
print("CREATING PY3PLEX VISUALIZATION MOSAIC BANNERS")
print("=" * 70)
# Create all three styles
print("\n1. Creating main mosaic banner (3x3 grid)...")
mosaic_path = create_mosaic_banner()
print("\n2. Creating compact horizontal banner...")
banner_path = create_compact_banner()
print("\n3. Creating showcase collage...")
showcase_path = create_showcase_collage()
print("\n" + "=" * 70)
print("BANNER CREATION COMPLETE")
print("=" * 70)
print(f"\nGenerated files:")
print(f" - {mosaic_path}")
print(f" - {banner_path}")
print(f" - {showcase_path}")
print(f"\nThese banners showcase the diverse visualization capabilities of py3plex")
print(f"and can be used in README.md or documentation.")