Download scripts/result.py from OneScience-Group/FlowBench-FNO-LDC2D-Harmonics: direct link, hf CLI and curl.
- Browser
- Download file 3.45 kB
-
https://huggingface.co/OneScience-Group/FlowBench-FNO-LDC2D-Harmonics/resolve/main/scripts/result.py
- Command line
-
hf download hf://OneScience-Group/FlowBench-FNO-LDC2D-Harmonics/scripts/result.py
-
curl -L -o result.py https://huggingface.co/OneScience-Group/FlowBench-FNO-LDC2D-Harmonics/resolve/main/scripts/result.py
3.45 kB
| 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() | |