import argparse import json import time from pathlib import Path import numpy as np from peakrecon import data, methods, metrics, sampling METHODS = ("interp", "gp_rbf", "gp_matern15", "gp_matern25", "nsgp_homo", "nsgp_hetero", "mlp", "ffmlp", "dip") STOCHASTIC = ("mlp", "ffmlp", "dip") def reconstruct(method, fd, frame, idx, seed, start): xn = data.normalised_coords(fd) y = fd["true"][frame][idx] / fd["cap"] if method == "interp": p = methods.linear_interp(xn, idx, y) elif method.startswith("gp_"): p = methods.stationary_gp(xn, idx, y, seed, kernel=method[3:]) elif method.startswith("nsgp_"): p = methods.nonstationary_gp(xn, idx, y, fd["x"], fd["y"], hetero=method.endswith("hetero")) elif method == "mlp": p = methods.mlp(xn, idx, y, 100 * seed + start) elif method == "ffmlp": p = methods.fourier_feature_mlp(xn, idx, y, 100 * seed + start) elif method == "dip": init = start if seed == 0 else 1000 * seed + start p, _ = methods.deep_image_prior(fd["x"], fd["y"], idx, y, seed=init, val_seed=seed) return np.clip(np.asarray(p, float) * fd["cap"], 0.0, None) def main(): ap = argparse.ArgumentParser() ap.add_argument("--case", default="data/fields/plain_medium.npz") ap.add_argument("--field", choices=data.FIELDS, default="dkappa") ap.add_argument("--method", choices=METHODS, default="gp_matern15") ap.add_argument("--seeds", default="0") ap.add_argument("--starts", type=int, default=3) ap.add_argument("--out", default="results") a = ap.parse_args() fd = data.load(a.case, a.field) out = Path(a.out) out.mkdir(parents=True, exist_ok=True) rows = [] for seed in [int(s) for s in a.seeds.split(",")]: idx = sampling.sample(fd["x"], fd["y"], seed=seed) for start in range(a.starts if a.method in STOCHASTIC else 1): for fr, t in enumerate(fd["times"]): t0 = time.time() p = reconstruct(a.method, fd, fr, idx, seed, start) truth = fd["true"][fr] r = dict(case=Path(a.case).stem, field=a.field, method=a.method, seed=seed, start=start, time=round(float(t), 2), **metrics.amplitude(truth, p), **metrics.SpatialFidelity(fd["x"], fd["y"], truth)(p), seconds=round(time.time() - t0, 1)) rows.append(r) print(json.dumps(r)) np.save(out / f"{r['case']}_{a.field}_{a.method}_s{seed}_st{start}_t{t:.2f}.npy", p.astype(np.float32)) (out / f"{Path(a.case).stem}_{a.field}_{a.method}.json").write_text(json.dumps(rows, indent=1)) if __name__ == "__main__": main()