From b5e4a53636e3f2e1975f6f50ea82b9b4503130c2 Mon Sep 17 00:00:00 2001 From: Mansa Krishna Date: Tue, 9 Apr 2024 16:35:08 -0400 Subject: [PATCH 1/3] added a plotting function + weight change in continuity.py --- PINNICLE/physics/continuity.py | 2 +- PINNICLE/utils/plotting.py | 79 ++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/PINNICLE/physics/continuity.py b/PINNICLE/physics/continuity.py index 56c0bcc..5178060 100644 --- a/PINNICLE/physics/continuity.py +++ b/PINNICLE/physics/continuity.py @@ -16,7 +16,7 @@ def set_default(self): self.output = ['u', 'v', 'a', 'H'] self.output_lb = [-1.0e4/self.yts, -1.0e4/self.yts, -5.0/self.yts, 10.0] self.output_ub = [ 1.0e4/self.yts, 1.0e4/self.yts, 5/self.yts, 2500.0] - self.data_weights = [1.0e-3*self.yts, 1.0e-3*self.yts, 1.0e4*self.yts, 1.0e-3] + self.data_weights = [1.0e-3*self.yts, 1.0e-3*self.yts, 1.0e4*self.yts, 1.0e-6] self.residuals = ["fMC"] self.pde_weights = [1.0e6] diff --git a/PINNICLE/utils/plotting.py b/PINNICLE/utils/plotting.py index 993d80b..ca2c9dc 100644 --- a/PINNICLE/utils/plotting.py +++ b/PINNICLE/utils/plotting.py @@ -1,6 +1,7 @@ import numpy as np import math import matplotlib.pyplot as plt +import matplotlib.colors as clr import matplotlib as mpl from matplotlib.colors import ListedColormap from scipy.interpolate import griddata @@ -214,3 +215,81 @@ def plot_data(X, Y, im_data, axs=None, vranges={}, **kwargs): plt.colorbar(im, ax=axs[i], shrink=0.8) return axs + +def plot_similarity(pinn, feature_name, savepath, sim='mae', cmap='jet', scale=1): + """ + plotting the feature prediction, compared to the reference solution + with error/difference. Default MAE. + """ + fig, axs = plt.subplots(1, 3, figsize=(15, 4)) + + # input and output + input_names = pinn.nn.parameters.input_variables + output_names = pinn.nn.parameters.output_variables + + # inputs + X_ref = pinn.model_data.data['ISSM'].X_dict + xref = X_ref[input_names[0]].flatten()[:,None] + for i in range(1, len(input_names)): + xref = np.hstack((xref, X_ref[input_names[i]].flatten()[:,None])) + meshx = np.squeeze(xref[:, 0]) + meshy = np.squeeze(xref[:, 1]) + + # predictions + pred = pinn.model.predict(xref) + + # reference solution + X_sol = pinn.model_data.data['ISSM'].data_dict + sol = X_sol[output_names[0]].flatten()[:,None] # initializing array + for i in range(1, len(output_names)): + sol = np.hstack((sol, X_sol[output_names[i]].flatten()[:,None])) + + # grab feature + fid = output_names.index(feature_name) + ref_sol = np.squeeze(sol[:, fid:fid+1]*scale) + pred_sol = np.squeeze(sol[:, fid:fid+1]*scale) + [cmin, cmax] = [np.min(np.append(ref_sol, pred_sol)), np.max(np.append(ref_sol, pred_sol))] + levels = np.linspace(cmin*0.9, cmax*1.1, 500) + + # reference plot + ax = axs[0].tricontourf(meshx, meshy, ref_sol, levels=levels, cmap=cmap) + cb = plt.colorbar(ax, ax=axs[0]) + cb.ax.tick_params(labelsize=14) + axs[0].set_title(feature_name+r'$_{ref}$', fontsize=14) + axs[0].axis('off') + + # prediction plot + ax = axs[1].tricontourf(meshx, meshy, pred_sol, levels=levels, cmap=cmap) + cb = plt.colorbar(ax, ax=axs[1]) + cb.ax.tick_params(labelsize=14) + axs[1].set_title(feature_name+r'$_{pred}$', fontsize=14) + axs[1].axis('off') + + # difference plot + diff = np.abs(ref_sol-pred_sol) + diff_val = np.round(np.mean(diff), 3) + title = "|"+feature_name+r"$_{ref} - $"+feature_name+r"$_{pred}$|, MAE = "+str(diff_val) + dmin, dmax = np.min(diff), np.max(diff) + levels = np.linspace(dmin*0.9, dmax*1.1, 500) + + if sim == 'mse' or sim == "MSE": + diff = (ref_sol-pred_sol)**2 + diff_val = np.round(np.mean(diff), 3) + title = r"$($"+feature_name+r"$_{ref} - $"+feature_name+r"$_{pred})^2$, MSE = "+str(diff_val) + dmin, dmax = np.min(diff), np.max(diff) + levels = np.linspace(dmin*0.9, dmax*1.1, 500) + + + ax = axs[2].tricontourf(meshx, meshy, diff, levels=levels, cmap='RdBu', norm=clr.CenteredNorm()) + cb = plt.colorbar(ax, ax=axs[2]) + cb.ax.tick_params(labelsize=14) + axs[2].set_title(title, fontsize=14) + axs[2].axis('off') + + plt.savefig(savepath) + + + + + + From ac10cdc1181aac9f3b8476e53bb64660af39616d Mon Sep 17 00:00:00 2001 From: Mansa Krishna Date: Tue, 9 Apr 2024 17:33:53 -0400 Subject: [PATCH 2/3] modified plotting_similarity function --- PINNICLE/utils/plotting.py | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/PINNICLE/utils/plotting.py b/PINNICLE/utils/plotting.py index ca2c9dc..b916a00 100644 --- a/PINNICLE/utils/plotting.py +++ b/PINNICLE/utils/plotting.py @@ -266,19 +266,35 @@ def plot_similarity(pinn, feature_name, savepath, sim='mae', cmap='jet', scale=1 axs[1].axis('off') # difference plot - diff = np.abs(ref_sol-pred_sol) - diff_val = np.round(np.mean(diff), 3) - title = "|"+feature_name+r"$_{ref} - $"+feature_name+r"$_{pred}$|, MAE = "+str(diff_val) - dmin, dmax = np.min(diff), np.max(diff) - levels = np.linspace(dmin*0.9, dmax*1.1, 500) - - if sim == 'mse' or sim == "MSE": + if sim == 'mae' or sim == 'MAE': + # mean absolute error + diff = np.abs(ref_sol-pred_sol) + diff_val = np.round(np.mean(diff), 3) + title = "|"+feature_name+r"$_{ref} - $"+feature_name+r"$_{pred}$|" + dmin, dmax = np.min(diff), np.max(diff) + levels = np.linspace(dmin*0.9, dmax*1.1, 500) + elif sim == 'mse' or sim == "MSE": + # mean squared error diff = (ref_sol-pred_sol)**2 diff_val = np.round(np.mean(diff), 3) - title = r"$($"+feature_name+r"$_{ref} - $"+feature_name+r"$_{pred})^2$, MSE = "+str(diff_val) + title = r"$($"+feature_name+r"$_{ref} - $"+feature_name+r"$_{pred})^2$" + dmin, dmax = np.min(diff), np.max(diff) + levels = np.linspace(dmin*0.9, dmax*1.1, 500) + elif sim = 'rmse' or sim == 'RMSE': + # root mean squared error + d_squared = (ref_sol - pred_sol)**2 + diff = np.sqrt(d_squared) + diff_val = np.round(np.sqrt(np.mean(d_squared)), 3) + title = r"$(($"+feature_name+r"$_{ref} - $"+feature_name+r"$_{pred})^2)^{1/2}$" + dmin, dmax = np.min(diff), np.max(diff) + levels = np.linspace(dmin*0.9, dmax*1.1, 500) + elif sim == 'simple' or sim == "SIMPLE": + # simple difference + diff = ref_sol - pred_sol + diff_val = np.round(np.mean(diff), 3) + title = feature_name+r"$_{ref} - $"+feature_name+r"$_{pred}$" dmin, dmax = np.min(diff), np.max(diff) levels = np.linspace(dmin*0.9, dmax*1.1, 500) - ax = axs[2].tricontourf(meshx, meshy, diff, levels=levels, cmap='RdBu', norm=clr.CenteredNorm()) cb = plt.colorbar(ax, ax=axs[2]) @@ -286,6 +302,7 @@ def plot_similarity(pinn, feature_name, savepath, sim='mae', cmap='jet', scale=1 axs[2].set_title(title, fontsize=14) axs[2].axis('off') + plt.text(0.705, 0, str(sim)+' = '+str(diff_val), fontsize=14, transform=plt.gcf().transFigure) plt.savefig(savepath) From 00ce0beb479880c1f3e540e45f3e2169bccb9409 Mon Sep 17 00:00:00 2001 From: Mansa Krishna Date: Wed, 10 Apr 2024 11:34:38 -0400 Subject: [PATCH 3/3] plotting function updated + tests --- PINNICLE/utils/__init__.py | 2 +- PINNICLE/utils/plotting.py | 108 +++++++++++++++---------------------- tests/test_pinn.py | 11 +++- 3 files changed, 54 insertions(+), 67 deletions(-) diff --git a/PINNICLE/utils/__init__.py b/PINNICLE/utils/__init__.py index 8b4af99..376776d 100644 --- a/PINNICLE/utils/__init__.py +++ b/PINNICLE/utils/__init__.py @@ -1,4 +1,4 @@ from .helper import * from .history import History from .data_misfit import get -from .plotting import plot_solutions, plot_dict_data, plot_data, plot_nn +from .plotting import plot_solutions, plot_dict_data, plot_data, plot_nn, plot_similarity diff --git a/PINNICLE/utils/plotting.py b/PINNICLE/utils/plotting.py index b916a00..28bba63 100644 --- a/PINNICLE/utils/plotting.py +++ b/PINNICLE/utils/plotting.py @@ -1,7 +1,7 @@ import numpy as np import math import matplotlib.pyplot as plt -import matplotlib.colors as clr +import matplotlib.colors as colors import matplotlib as mpl from matplotlib.colors import ListedColormap from scipy.interpolate import griddata @@ -216,14 +216,15 @@ def plot_data(X, Y, im_data, axs=None, vranges={}, **kwargs): return axs -def plot_similarity(pinn, feature_name, savepath, sim='mae', cmap='jet', scale=1): +def plot_similarity(pinn, feature_name, savepath, sim='MAE', cmap='jet', scale=1, cols=[0, 1, 2]): """ - plotting the feature prediction, compared to the reference solution - with error/difference. Default MAE. + plotting the similarity between reference and predicted + solutions, mae default """ - fig, axs = plt.subplots(1, 3, figsize=(15, 4)) + # initialize figure, default all 3 columns + fig, axs = plt.subplots(1, len(cols), figsize=(5*len(cols), 4)) - # input and output + # inputs and outputs of NN input_names = pinn.nn.parameters.input_variables output_names = pinn.nn.parameters.output_variables @@ -247,66 +248,43 @@ def plot_similarity(pinn, feature_name, savepath, sim='mae', cmap='jet', scale=1 # grab feature fid = output_names.index(feature_name) ref_sol = np.squeeze(sol[:, fid:fid+1]*scale) - pred_sol = np.squeeze(sol[:, fid:fid+1]*scale) + pred_sol = np.squeeze(pred[:, fid:fid+1]*scale) [cmin, cmax] = [np.min(np.append(ref_sol, pred_sol)), np.max(np.append(ref_sol, pred_sol))] levels = np.linspace(cmin*0.9, cmax*1.1, 500) - # reference plot - ax = axs[0].tricontourf(meshx, meshy, ref_sol, levels=levels, cmap=cmap) - cb = plt.colorbar(ax, ax=axs[0]) - cb.ax.tick_params(labelsize=14) - axs[0].set_title(feature_name+r'$_{ref}$', fontsize=14) - axs[0].axis('off') - - # prediction plot - ax = axs[1].tricontourf(meshx, meshy, pred_sol, levels=levels, cmap=cmap) - cb = plt.colorbar(ax, ax=axs[1]) - cb.ax.tick_params(labelsize=14) - axs[1].set_title(feature_name+r'$_{pred}$', fontsize=14) - axs[1].axis('off') - - # difference plot - if sim == 'mae' or sim == 'MAE': - # mean absolute error - diff = np.abs(ref_sol-pred_sol) - diff_val = np.round(np.mean(diff), 3) - title = "|"+feature_name+r"$_{ref} - $"+feature_name+r"$_{pred}$|" - dmin, dmax = np.min(diff), np.max(diff) - levels = np.linspace(dmin*0.9, dmax*1.1, 500) - elif sim == 'mse' or sim == "MSE": - # mean squared error - diff = (ref_sol-pred_sol)**2 - diff_val = np.round(np.mean(diff), 3) - title = r"$($"+feature_name+r"$_{ref} - $"+feature_name+r"$_{pred})^2$" - dmin, dmax = np.min(diff), np.max(diff) - levels = np.linspace(dmin*0.9, dmax*1.1, 500) - elif sim = 'rmse' or sim == 'RMSE': - # root mean squared error - d_squared = (ref_sol - pred_sol)**2 - diff = np.sqrt(d_squared) - diff_val = np.round(np.sqrt(np.mean(d_squared)), 3) - title = r"$(($"+feature_name+r"$_{ref} - $"+feature_name+r"$_{pred})^2)^{1/2}$" - dmin, dmax = np.min(diff), np.max(diff) - levels = np.linspace(dmin*0.9, dmax*1.1, 500) - elif sim == 'simple' or sim == "SIMPLE": - # simple difference - diff = ref_sol - pred_sol - diff_val = np.round(np.mean(diff), 3) - title = feature_name+r"$_{ref} - $"+feature_name+r"$_{pred}$" - dmin, dmax = np.min(diff), np.max(diff) - levels = np.linspace(dmin*0.9, dmax*1.1, 500) - - ax = axs[2].tricontourf(meshx, meshy, diff, levels=levels, cmap='RdBu', norm=clr.CenteredNorm()) - cb = plt.colorbar(ax, ax=axs[2]) - cb.ax.tick_params(labelsize=14) - axs[2].set_title(title, fontsize=14) - axs[2].axis('off') - - plt.text(0.705, 0, str(sim)+' = '+str(diff_val), fontsize=14, transform=plt.gcf().transFigure) + # plotting + # reference solution + c = 0 # column number initialize + if 0 in cols: + ax = axs[c].tricontourf(meshx, meshy, ref_sol, levels=levels, cmap=cmap) + cb = plt.colorbar(ax, ax=axs[c]) + cb.ax.tick_params(labelsize=14) + axs[c].set_title(feature_name+r"$_{ref}$", fontsize=14) + axs[c].axis('off') + c += 1 + + # predicted solution + if 1 in cols: + ax = axs[c].tricontourf(meshx, meshy, pred_sol, levels=levels, cmap=cmap) + cb = plt.colorbar(ax, ax=axs[c]) + cb.ax.tick_params(labelsize=14) + axs[c].set_title(feature_name+r"$_{pred}$", fontsize=14) + axs[c].axis('off') + c += 1 + + # difference / similarity + if 2 in cols: + if sim == 'MAE': + diff = np.abs(ref_sol-pred_sol) + diff_val = np.round(np.mean(diff), 2) + title = r"|"+feature_name+r"$_{ref} - $"+feature_name+r"$_{pred}$|, MAE="+str(diff_val) + dmin, dmax = np.min(diff), np.max(diff) + levels = np.linspace(dmin*0.9, dmax*1.1, 500) + ax = axs[c].tricontourf(meshx, meshy, np.squeeze(diff), levels=levels, cmap='RdBu', norm=colors.CenteredNorm()) + cb = plt.colorbar(ax, ax=axs[c]) + cb.ax.tick_params(labelsize=14) + axs[c].set_title(title, fontsize=14) + axs[c].axis('off') + + # save figure to path as defined plt.savefig(savepath) - - - - - - diff --git a/tests/test_pinn.py b/tests/test_pinn.py index c216b9d..57a8780 100644 --- a/tests/test_pinn.py +++ b/tests/test_pinn.py @@ -2,7 +2,7 @@ import PINNICLE as pinn import numpy as np import deepxde as dde -from PINNICLE.utils import data_misfit, plot_nn +from PINNICLE.utils import data_misfit, plot_nn, plot_similarity dde.config.set_default_float('float64') dde.config.disable_xla_jit() @@ -164,3 +164,12 @@ def test_plot(tmp_path): assert Y.shape == (10,10) assert len(im_data) == 5 assert im_data['u'].shape == (10,10) + +def test_similarity(tmp_path): + hp["save_path"] = str(tmp_path) + hp["is_save"] = True + issm["data_size"] = {"u":4000, "v":4000, "s":4000, "H":4000, "C":None} + hp["data"] = {"ISSM": issm} + experiment = pinn.PINN(params=hp) + experiment.compile() + assert plot_similarity(experiment, feature_name="u", savepath=tmp_path) is None