from pathlib import Path import sys,numpy as np import matplotlib;matplotlib.use("Agg");import matplotlib.pyplot as plt ROOT=Path(__file__).resolve().parents[1];sys.path.insert(0,str(ROOT)) from model.clm5_emulator import load_config,write_json c=load_config(ROOT);d=np.load(ROOT/c["paths"]["predictions"]);p=d["predicted_pc"];t=d["target_pc"];r2=[] for v in range(2):r2.append([float(np.corrcoef(p[:,v,k],t[:,v,k])[0,1]**2) for k in range(3)]) rmse=np.sqrt(np.mean((d["predicted_fields"]-d["target_fields"])**2,axis=(0,2,3)));write_json(ROOT/c["paths"]["evaluation"],{"pc_r2":r2,"spatial_rmse":{"GPP":float(rmse[0]),"LHF":float(rmse[1])},"estimated_parameters":d["best_parameters"].tolist(),"calibration_cost":float(d["cost"]),"synthetic":True});fig,ax=plt.subplots(1,2,figsize=(9,3.5));ax[0].scatter(t[:,0,0],p[:,0,0]);ax[0].set(xlabel="CLM PC1",ylabel="Emulated PC1",title="GPP emulator");im=ax[1].imshow((d["predicted_fields"][0,1]-d["target_fields"][0,1]),cmap="coolwarm");ax[1].set_title("LHF spatial error");fig.colorbar(im,ax=ax[1]);fig.tight_layout();path=ROOT/c["paths"]["figure"];path.parent.mkdir(parents=True,exist_ok=True);fig.savefig(path,dpi=150);print(path)