from pathlib import Path import sys,numpy as np,torch import matplotlib;matplotlib.use("Agg");import matplotlib.pyplot as plt ROOT=Path(__file__).resolve().parents[1];sys.path.insert(0,str(ROOT)) from model.global_flood_lstm import * c=load_config(ROOT);d=np.load(ROOT/c["paths"]["predictions"]);p=d["prediction"].mean(0);t=d["target"];threshold=np.quantile(t,.8,axis=1,keepdims=True);obs=t>=threshold;hit=p>=threshold;tp=(hit&obs).sum(0);fp=(hit&~obs).sum(0);fn=(~hit&obs).sum(0);precision=tp/(tp+fp+1e-8);recall=tp/(tp+fn+1e-8);f1=2*precision*recall/(precision+recall+1e-8);rmse=np.sqrt(np.mean((p-t)**2,0));metrics={"rmse":rmse.tolist(),"precision":precision.tolist(),"recall":recall.tolist(),"f1":f1.tolist(),"kge":float(kge(torch.tensor(p),torch.tensor(t))),"threshold_proxy":"per-basin 80th percentile for synthetic data","synthetic":True};write_json(ROOT/c["paths"]["evaluation"],metrics);fig,ax=plt.subplots(1,2,figsize=(9,3.5));ax[0].plot(d["lead_days"],rmse,"o-");ax[0].set(xlabel="Lead (days)",ylabel="RMSE");ax[1].plot(d["lead_days"],f1,"s-");ax[1].set(xlabel="Lead (days)",ylabel="Extreme-event F1",ylim=(0,1));fig.tight_layout();path=ROOT/c["paths"]["figure"];path.parent.mkdir(parents=True,exist_ok=True);fig.savefig(path,dpi=150);print(path)