File size: 3,448 Bytes
4d99a96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import argparse
import json
from pathlib import Path
from typing import Any

import numpy as np
import yaml

try:
    import matplotlib

    matplotlib.use("Agg")
    import matplotlib.pyplot as plt
except ImportError as exc:  # pragma: no cover
    raise SystemExit(f"matplotlib is required for visualization: {exc}") from exc

PROJECT_ROOT = Path(__file__).resolve().parents[1]


def load_config(path: str | Path) -> dict[str, Any]:
    with open(path, "r", encoding="utf-8") as handle:
        return yaml.safe_load(handle)


def resolve_path(path: str | Path) -> Path:
    path = Path(path)
    return path if path.is_absolute() else PROJECT_ROOT / path


def compute_numpy_metrics(pred: np.ndarray, target: np.ndarray) -> dict[str, float]:
    diff = pred - target
    mse = float(np.mean(diff**2))
    mae = float(np.mean(np.abs(diff)))
    rel = np.linalg.norm(diff.reshape(diff.shape[0], -1), axis=1) / np.maximum(
        np.linalg.norm(target.reshape(target.shape[0], -1), axis=1), 1.0e-12
    )
    return {"mse": mse, "relative_l2": float(np.mean(rel)), "mae": mae}


def plot_sample(pred: np.ndarray, target: np.ndarray, output_path: Path, sample_id: int) -> None:
    channels = pred.shape[0]
    fig, axes = plt.subplots(channels, 3, figsize=(10, 3 * channels), squeeze=False)
    for ch in range(channels):
        fields = [target[ch], pred[ch], np.abs(pred[ch] - target[ch])]
        titles = [f"target ch{ch}", f"prediction ch{ch}", f"abs error ch{ch}"]
        for col, (field, title) in enumerate(zip(fields, titles)):
            ax = axes[ch, col]
            image = ax.imshow(field, origin="lower", cmap="viridis")
            ax.set_title(title)
            ax.set_xticks([])
            ax.set_yticks([])
            fig.colorbar(image, ax=ax, fraction=0.046, pad=0.04)
    fig.suptitle(f"FlowBench FNO sample {sample_id}")
    fig.tight_layout()
    output_path.parent.mkdir(parents=True, exist_ok=True)
    fig.savefig(output_path, dpi=160)
    plt.close(fig)


def main() -> None:
    parser = argparse.ArgumentParser(description="Summarize and visualize FlowBench FNO predictions.")
    parser.add_argument("--config", default=str(PROJECT_ROOT / "config/config.yaml"))
    parser.add_argument("--predictions", default=None)
    parser.add_argument("--output-dir", default=None)
    parser.add_argument("--max-samples", type=int, default=None)
    args = parser.parse_args()

    config = load_config(args.config)
    vis_cfg = config["visualization"]
    predictions_path = resolve_path(args.predictions or vis_cfg["predictions_path"])
    output_dir = resolve_path(args.output_dir or vis_cfg["output_dir"])
    max_samples = args.max_samples or int(vis_cfg.get("max_samples", 4))

    archive = np.load(predictions_path)
    pred = archive["predictions"]
    target = archive["targets"]
    metrics = compute_numpy_metrics(pred, target)
    output_dir.mkdir(parents=True, exist_ok=True)
    for idx in range(min(max_samples, pred.shape[0])):
        plot_sample(pred[idx], target[idx], output_dir / f"sample_{idx:03d}.png", idx)
    summary_path = output_dir.parent / "result_summary.json"
    with open(summary_path, "w", encoding="utf-8") as handle:
        json.dump({"metrics": metrics, "figures_dir": str(output_dir)}, handle, indent=2)
    print(json.dumps({"metrics": metrics, "figures_dir": str(output_dir)}, indent=2), flush=True)


if __name__ == "__main__":
    main()