-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathutils.py
More file actions
166 lines (128 loc) · 4.95 KB
/
Copy pathutils.py
File metadata and controls
166 lines (128 loc) · 4.95 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
"""Shared utilities for training, evaluation, and experiment I/O."""
from __future__ import annotations
from pathlib import Path
from typing import Iterable, Optional, Tuple
import matplotlib.pyplot as plt
import numpy as np
import torch
def set_random_seed(seed: int) -> None:
"""Set NumPy and PyTorch seeds for reproducibility."""
np.random.seed(seed)
torch.manual_seed(seed)
def ensure_directory(path: Path) -> None:
"""Create a directory tree if it does not already exist."""
path.mkdir(parents=True, exist_ok=True)
def save_checkpoint(
model: torch.nn.Module,
optimizer: torch.optim.Optimizer,
scheduler: torch.optim.lr_scheduler._LRScheduler,
save_path: Path,
) -> None:
"""Persist model and optimizer state."""
ensure_directory(save_path.parent)
torch.save(
{
"model_state_dict": model.state_dict(),
"optimizer_state_dict": optimizer.state_dict(),
"scheduler_state_dict": scheduler.state_dict(),
},
save_path,
)
def load_checkpoint(
model: torch.nn.Module,
save_path: Path,
optimizer: Optional[torch.optim.Optimizer] = None,
scheduler: Optional[torch.optim.lr_scheduler._LRScheduler] = None,
map_location: Optional[torch.device] = None,
) -> Tuple[torch.nn.Module, Optional[torch.optim.Optimizer], Optional[torch.optim.lr_scheduler._LRScheduler]]:
"""Load model and optional optimizer state from a checkpoint."""
checkpoint = torch.load(save_path, map_location=map_location)
model.load_state_dict(checkpoint["model_state_dict"])
if optimizer is not None and "optimizer_state_dict" in checkpoint:
optimizer.load_state_dict(checkpoint["optimizer_state_dict"])
if scheduler is not None and "scheduler_state_dict" in checkpoint:
scheduler.load_state_dict(checkpoint["scheduler_state_dict"])
return model, optimizer, scheduler
def count_parameters(model: torch.nn.Module) -> int:
"""Count trainable parameters."""
return sum(parameter.numel() for parameter in model.parameters() if parameter.requires_grad)
def frobenius_norm(array: np.ndarray) -> float:
"""Compute the Frobenius norm of a NumPy array."""
return float(np.sqrt(np.sum(array**2)))
def plot_training_loss(train_loss: Iterable[float], save_path: Path) -> None:
"""Save the training-loss curve."""
ensure_directory(save_path.parent)
plt.figure()
plt.plot(list(train_loss), label="train loss")
plt.yscale("log")
plt.legend()
plt.savefig(save_path, dpi=300)
plt.close("all")
def plot_time_trace(
predicted: np.ndarray,
truth: np.ndarray,
t_pred: np.ndarray,
t_true: np.ndarray,
save_path: Path,
x_index: int = 32,
y_index: int = 32,
) -> None:
"""Plot a time trace for a fixed grid location."""
ensure_directory(save_path.parent)
plt.figure()
plt.plot(t_pred, predicted[:, y_index, x_index], label=f"x={x_index}, y={y_index}, CRL")
plt.plot(t_true, truth[:, y_index, x_index], "--", label=f"x={x_index}, y={y_index}, Ref.")
plt.xlabel("t")
plt.ylabel("u")
plt.xlim(0, 2)
plt.legend()
plt.savefig(save_path)
plt.close("all")
def post_process_comparison(
output: torch.Tensor,
truth: np.ndarray,
axis_limits: Tuple[float, float, float, float],
uv_limits: Tuple[float, float, float, float],
step_index: int,
save_path: Path,
) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
"""Render prediction/reference comparisons for a single time step."""
xmin, xmax, ymin, ymax = axis_limits
u_min, u_max, v_min, v_max = uv_limits
resolution = truth.shape[-1] - 2
x = np.linspace(xmin, xmax, resolution + 1)[:-1]
x_star, y_star = np.meshgrid(x, x)
u_true = truth[step_index, 0, 1:-1, 1:-1]
u_pred = output[step_index, 0, 1:-1, 1:-1].detach().cpu().numpy()
v_true = truth[step_index, 1, 1:-1, 1:-1]
v_pred = output[step_index, 1, 1:-1, 1:-1].detach().cpu().numpy()
ensure_directory(save_path.parent)
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(7, 7))
fig.subplots_adjust(hspace=0.3, wspace=0.3)
plots = (
(axes[0, 0], u_pred, "u-RCNN", u_min, u_max),
(axes[0, 1], u_true, "u-Ref.", u_min, u_max),
(axes[1, 0], v_pred, "v-RCNN", v_min, v_max),
(axes[1, 1], v_true, "v-Ref.", v_min, v_max),
)
for axis, values, title, vmin, vmax in plots:
color_mesh = axis.scatter(
x_star,
y_star,
c=values,
alpha=0.9,
edgecolors="none",
cmap="RdYlBu",
marker="s",
s=4,
vmin=vmin,
vmax=vmax,
)
axis.axis("square")
axis.set_xlim([xmin, xmax])
axis.set_ylim([ymin, ymax])
axis.set_title(title)
fig.colorbar(color_mesh, ax=axis)
fig.savefig(save_path)
plt.close("all")
return u_true, u_pred, v_true, v_pred