File size: 13,959 Bytes
085fdaa c1857cb 085fdaa 1c76709 eadd412 085fdaa eadd412 c1857cb eadd412 1c76709 eadd412 1c76709 eadd412 c1857cb eadd412 57a5488 eadd412 57a5488 eadd412 085fdaa b97027a 085fdaa 1c76709 c1857cb 1c76709 085fdaa |
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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
import json
from io import BytesIO
from pathlib import Path
from typing import Any, Dict, List
import keras
import matplotlib.pyplot as plt
import numpy as np
import tyro
from keras import ops
from matplotlib.patches import PathPatch
from matplotlib.path import Path as pltPath
from PIL import Image
from skimage import measure
from zea import log
from zea.utils import save_to_gif
from zea.visualize import plot_image_grid
from utils import postprocess
def add_shape_from_mask(ax, mask, **kwargs):
"""add a shape to axis from mask array.
Args:
ax (plt.ax): matplotlib axis
mask (ndarray): numpy array with non-zero
shape defining the region of interest.
Kwargs:
edgecolor (str): color of the shape's edge
facecolor (str): color of the shape's face
linewidth (int): width of the shape's edge
Returns:
plt.ax: matplotlib axis with shape added
"""
# Pad mask to ensure edge contours are found
padded_mask = np.pad(mask, pad_width=1, mode="constant", constant_values=0)
contours = measure.find_contours(padded_mask, 0.5)
patches = []
for contour in contours:
# Remove padding offset
contour -= 1
path = pltPath(contour[:, ::-1])
patch = PathPatch(path, **kwargs)
patches.append(ax.add_patch(patch))
return patches
def matplotlib_figure_to_numpy(fig):
"""Convert matplotlib figure to numpy array.
Args:
fig (matplotlib.figure.Figure): figure to convert.
Returns:
np.ndarray: numpy array of figure.
"""
buf = BytesIO()
fig.savefig(buf, format="png", bbox_inches="tight")
buf.seek(0)
image = Image.open(buf).convert("RGB")
image = np.array(image)[..., :3]
buf.close()
return image
def plot_batch_with_named_masks(
images, masks_dict, mask_colors=None, titles=None, **kwargs
):
"""
Plot batch of images in rows, each column overlays a different mask from the dict.
Mask labels are shown as column titles. If mask name is 'per_pixel_omega', show it
directly with inferno colormap (no overlay).
Args:
images: np.ndarray, shape (batch, height, width, channels)
masks_dict: dict of {name: mask}, each mask shape (batch, height, width, channels)
mask_colors: dict of {name: color} or None (default colors used)
"""
mask_names = list(masks_dict.keys())
batch_size = images.shape[0]
default_colors = ["red", "green", "#33aaff", "yellow", "magenta", "cyan"]
mask_colors = mask_colors or {
name: default_colors[i % len(default_colors)]
for i, name in enumerate(mask_names)
}
# Prepare images for each column
columns = []
cmaps = []
for name in mask_names:
if name == "per_pixel_omega":
mask_np = np.array(masks_dict[name])
columns.append(np.squeeze(mask_np))
cmaps.append(["inferno"] * batch_size)
else:
columns.append(np.squeeze(images))
cmaps.append(["gray"] * batch_size)
# Stack columns: shape (num_columns, batch, ...)
all_images = np.stack(columns, axis=0) # (num_columns, batch, ...)
# Rearrange to (batch, num_columns, ...)
all_images = (
np.transpose(all_images, (1, 0, 2, 3, 4))
if all_images.ndim == 5
else np.transpose(all_images, (1, 0, 2, 3))
)
# Flatten to (batch * num_columns, ...)
all_images = all_images.reshape(batch_size * len(mask_names), *images.shape[1:])
# Flatten cmaps for plot_image_grid in the same order as images
flat_cmaps = []
for row in range(batch_size):
for col in range(len(mask_names)):
flat_cmaps.append(cmaps[col][row])
fig, _ = plot_image_grid(
all_images,
ncols=len(mask_names),
remove_axis=False,
cmap=flat_cmaps,
figsize=(8, 3.3),
**kwargs,
)
# Overlay masks for non-per_pixel_omega columns
for col_idx, name in enumerate(mask_names):
if name == "per_pixel_omega":
continue
mask_np = np.array(masks_dict[name])
axes = fig.axes[col_idx : batch_size * len(mask_names) : len(mask_names)]
for ax, mask_img in zip(axes, mask_np):
add_shape_from_mask(
ax, mask_img.squeeze(), color=mask_colors[name], alpha=0.3
)
# Add column titles
row_idx = 0
if titles is None:
titles = mask_names
for col_idx, name in enumerate(titles):
ax_idx = row_idx * len(mask_names) + col_idx
fig.axes[ax_idx].set_title(name, fontsize=9, color="white")
fig.axes[ax_idx].set_facecolor("black")
# Add colorbar for per_pixel_omega if present
if "per_pixel_omega" in mask_names:
col_idx = mask_names.index("per_pixel_omega")
axes = fig.axes[col_idx : batch_size * len(mask_names) : len(mask_names)]
# Get vertical bounds of the subplot column
top_ax = axes[0]
bottom_ax = axes[-1]
top_pos = top_ax.get_position()
bottom_pos = bottom_ax.get_position()
full_y0 = bottom_pos.y0
full_y1 = top_pos.y1
full_height = full_y1 - full_y0
# Manually shrink to 80% of full height and center vertically
scale = 0.8
height = full_height * scale
y0 = full_y0 + (full_height - height) / 2
x0 = top_pos.x1 + 0.015 # Horizontal position to the right
width = 0.015 # Thin bar
# Add colorbar axis
cax = fig.add_axes([x0, y0, width, height])
im = axes[0].get_images()[0] if axes[0].get_images() else None
cbar = fig.colorbar(im, cax=cax)
cbar.set_label(r"Guidance weighting $\mathbf{p}$")
cbar.ax.yaxis.set_major_locator(plt.MaxNLocator(nbins=6))
cbar.ax.yaxis.set_tick_params(labelsize=7)
cbar.ax.yaxis.label.set_size(8)
return fig
def plot_dehazed_results(
hazy_images,
pred_tissue_images,
pred_haze_images,
diffusion_model,
titles=("Hazy", "Dehazed", "Haze"),
):
"""Create and save visualization with optional mask overlays."""
# Create the processed image stack using the helper function
input_shape = diffusion_model.input_shape
stack_images = ops.stack(
[
hazy_images,
pred_tissue_images,
pred_haze_images,
]
)
stack_images = ops.reshape(stack_images, (-1, input_shape[0], input_shape[1]))
# Define labels based on what we're showing
fig, _ = plot_image_grid(
stack_images,
ncols=len(hazy_images),
remove_axis=False,
vmin=0,
vmax=255,
)
# Set labels and styling
for i, ax in enumerate(fig.axes):
if i % len(hazy_images) == 0:
label = titles[(i // len(hazy_images)) % len(titles)]
ax.set_ylabel(label, fontsize=12)
return fig
def plot_metrics(metrics, limits, out_path):
plt.style.use("seaborn-v0_8-darkgrid")
fig, axes = plt.subplots(1, len(metrics), figsize=(7.2, 2.7), dpi=200)
colors = ["#0057b7", "#ffb300", "#008744", "#d62d20"]
metric_labels = {
"CNR": r"CNR $\uparrow$",
"gCNR": r"gCNR $\uparrow$",
"KS_A": r"KS$_{septum}$ $\downarrow$",
"KS_B": r"KS$_{ventricle}$ $\uparrow$",
}
# For legend handles
legend_handles = []
import matplotlib.lines as mlines
min_style = {
"color": "crimson",
"linestyle": "--",
"lw": 1.2,
"marker": "o",
"markersize": 5,
}
max_style = {
"color": "crimson",
"linestyle": ":",
"lw": 1.2,
"marker": "s",
"markersize": 5,
}
for idx, (ax, (name, values)) in enumerate(zip(axes, metrics.items())):
ax.hist(
values,
bins=30,
color=colors[idx % len(colors)],
alpha=0.85,
edgecolor="black",
linewidth=0.7,
)
ax.set_xlabel(metric_labels.get(name, name), fontsize=11)
if idx == 0:
ax.set_ylabel("Count", fontsize=10)
# Draw limits and collect legend handles only once
if name in limits:
lims = limits[name]
if len(legend_handles) == 0:
# Only add legend handles for the first metric
min_handle = mlines.Line2D([], [], **min_style, label="min score")
max_handle = mlines.Line2D([], [], **max_style, label="max score")
legend_handles.extend([min_handle, max_handle])
if len(lims) > 0:
ax.axvline(lims[0], **min_style)
if len(lims) > 1:
ax.axvline(lims[1], **max_style)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.tick_params(axis="both", which="major", labelsize=9)
# Place legend above all subplots
fig.legend(
handles=legend_handles,
loc="upper center",
ncol=2,
fontsize=10,
frameon=False,
bbox_to_anchor=(0.5, 1.02),
)
return fig
def plot_optimization_history_from_json(
trials_data: List[Dict[str, Any]], output_path: Path, method: str
):
"""Plot optimization history directly from JSON data."""
# Extract completed trials with values
completed_trials = [
t for t in trials_data if t["state"] == "COMPLETE" and t["value"] is not None
]
if not completed_trials:
print("No completed trials found!")
return
# Sort by trial number
completed_trials.sort(key=lambda x: x["number"])
trial_numbers = [t["number"] for t in completed_trials]
values = [t["value"] for t in completed_trials]
# Apply seaborn styling
plt.style.use("seaborn-v0_8-darkgrid")
# Create the plot
fig, ax = plt.subplots(figsize=(5, 3), dpi=600)
# Plot all trial values with styling similar to plots.py
ax.scatter(
trial_numbers,
values,
c="#0057b7",
alpha=0.6,
s=30,
edgecolor="black",
linewidth=0.5,
)
# Plot best value so far
best_values = []
current_best = values[0]
for val in values:
if val > current_best: # Assuming maximization
current_best = val
best_values.append(current_best)
ax.plot(
trial_numbers,
best_values,
color="#d62d20",
linewidth=2.5,
label="Best Value",
marker="o",
markersize=4,
markevery=max(1, len(trial_numbers) // 20),
)
ax.set_xlabel("Trial", fontsize=11)
ax.set_ylabel("Objective Value", fontsize=11)
# ax.set_title("Optimization History", fontsize=12)
ax.legend(fontsize=10, frameon=False)
# Remove top and right spines like in plots.py
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.tick_params(axis="both", which="major", labelsize=9)
# Save plot
fig.savefig(
output_path / f"optimization_history_{method}.png", dpi=600, bbox_inches="tight"
)
fig.savefig(
output_path / f"optimization_history_{method}.pdf", dpi=600, bbox_inches="tight"
)
plt.close(fig)
def create_animation_frame(hazy_images, tissue_frame, haze_frame):
"""Create a single animation frame from the tracked progress."""
batch, height, width = ops.shape(hazy_images)
frame_stack = ops.stack(
[
hazy_images,
tissue_frame,
haze_frame,
]
)
frame_stack = ops.reshape(frame_stack, (-1, height, width))
fig_frame, _ = plot_image_grid(
frame_stack,
ncols=len(hazy_images),
remove_axis=False,
vmin=0,
vmax=255,
)
labels = ["Hazy", "Haze", "Tissue"]
for i, ax in enumerate(fig_frame.axes):
label = labels[i % len(labels)]
ax.set_ylabel(label, fontsize=12)
frame_array = matplotlib_figure_to_numpy(fig_frame)
plt.close(fig_frame)
return frame_array
def create_animation(hazy_images, diffusion_model, output_path, fps):
"""Create animation from tracked progress frames."""
if not (len(diffusion_model.track_progress) > 1):
log.warning(
"Animation requested but no intermediate frames were tracked. "
"Try reducing diffusion_steps or ensure progress tracking is enabled."
)
return
log.info(f"Creating animation with {len(diffusion_model.track_progress)} frames...")
animation_frames = []
progbar = keras.utils.Progbar(
len(diffusion_model.track_progress), unit_name="frame"
)
for tissue_frame in diffusion_model.track_progress:
haze_frame = hazy_images - tissue_frame - 1
tissue_frame = postprocess(tissue_frame, diffusion_model.input_range)
haze_frame = postprocess(haze_frame, diffusion_model.input_range)
_hazy_images = postprocess(hazy_images, diffusion_model.input_range)
frame_array = create_animation_frame(_hazy_images, tissue_frame, haze_frame)
animation_frames.append(frame_array)
progbar.add(1)
Path(output_path).parent.mkdir(parents=True, exist_ok=True)
animation_path = Path(output_path).with_suffix(".gif")
save_to_gif(animation_frames, animation_path, fps=fps)
def main(json_file: str, output_dir: str = "plots", method: str = "semantic_dps"):
json_path = Path(json_file)
if not json_path.exists():
raise FileNotFoundError(f"JSON file not found: {json_file}")
# Load trial data
with open(json_path, "r") as f:
trials_data = json.load(f)
print(f"Loaded {len(trials_data)} trials from {json_file}")
# Create output directory
output_path = Path(output_dir)
output_path.mkdir(parents=True, exist_ok=True)
print("Generating optimization history plot...")
plot_optimization_history_from_json(trials_data, output_path, method)
if __name__ == "__main__":
tyro.cli(main)
|