From 04dd0ca716b16cfa1af0acf6e65f902d5e87c364 Mon Sep 17 00:00:00 2001 From: kohlhaasrebecca <rebecca.kohlhaas@outlook.com> Date: Wed, 23 Oct 2024 09:29:58 +0200 Subject: [PATCH] Update PostProcessing tests --- .../post_processing/post_processing.py | 49 ++++++++++--------- tests/test_PostProcessing.py | 3 +- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/bayesvalidrox/post_processing/post_processing.py b/src/bayesvalidrox/post_processing/post_processing.py index 659e3cbaf..91692669b 100644 --- a/src/bayesvalidrox/post_processing/post_processing.py +++ b/src/bayesvalidrox/post_processing/post_processing.py @@ -17,11 +17,12 @@ from matplotlib.patches import Patch plt.style.use(os.path.join(os.path.split(__file__)[0], '../', 'bayesvalidrox.mplstyle')) + class PostProcessing: """ This class provides many helper functions to post-process the trained meta-model. - + Parameters ---------- engine : obj @@ -31,15 +32,15 @@ class PostProcessing: The default is 'calib'. out_dir : string Output directory in which the images are placed. The default is ''. - + """ - def __init__(self, engine, name='calib', out_dir = ''): + def __init__(self, engine, name='calib', out_dir=''): self.engine = engine self.name = name - - self.out_dir = f'{out_dir}/Outputs_PostProcessing_{self.name}/' - + + self.out_dir = f'./{out_dir}/Outputs_PostProcessing_{self.name}/' + # Open a pdf for the plots if not os.path.exists(self.out_dir): os.makedirs(self.out_dir) @@ -58,7 +59,7 @@ class PostProcessing: self.pce_out_std = None # ------------------------------------------------------------------------- - def plot_moments(self, xlabel:str='Time [s]', plot_type:str=None): + def plot_moments(self, xlabel: str = 'Time [s]', plot_type: str = None): """ Plots the moments in a pdf format in the directory `Outputs_PostProcessing`. @@ -190,7 +191,8 @@ class PostProcessing: self.model_out_dict = model_out_dict else: self.model_out_dict = self._eval_model(samples, key_str='valid') - self.pce_out_mean, self.pce_out_std = self.engine.eval_metamodel(samples) + self.pce_out_mean, self.pce_out_std = self.engine.eval_metamodel( + samples) try: key = self.engine.out_names[1] @@ -203,9 +205,10 @@ class PostProcessing: self._plot_validation(samples) else: self._plot_validation_multi(x_values=x_values, x_axis=x_axis) - + # Zip the subdirectories - self.engine.Model.zip_subdirs(f'{self.engine.Model.name}valid', f'{self.engine.Model.name}valid_') + self.engine.Model.zip_subdirs( + f'{self.engine.Model.name}valid', f'{self.engine.Model.name}valid_') # Zip the subdirectories self.engine.Model.zip_subdirs( @@ -271,9 +274,8 @@ class PostProcessing: self.engine.MetaModel.rmse = self.rmse self.engine.MetaModel.valid_error = self.valid_error - # ------------------------------------------------------------------------- - def plot_seq_design_diagnostics(self, ref_BME_KLD=None)->None: + def plot_seq_design_diagnostics(self, ref_BME_KLD=None) -> None: """ Plots the Bayesian Model Evidence (BME) and Kullback-Leibler divergence (KLD) for the sequential design. @@ -520,7 +522,7 @@ class PostProcessing: # ------------------------------------------------------------------------- - def sobol_indices(self, xlabel:str='Time [s]', plot_type:str=None): + def sobol_indices(self, xlabel: str = 'Time [s]', plot_type: str = None): """ Provides Sobol indices as a sensitivity measure to infer the importance of the input parameters. See Eq. 27 in [1] for more details. For the @@ -548,7 +550,7 @@ class PostProcessing: ------ AttributeError MetaModel in given Engine needs to be of type 'pce' or 'apce'. - + Returns ------- sobol_cell: dict @@ -840,7 +842,7 @@ class PostProcessing: return self.total_sobol # ------------------------------------------------------------------------- - def check_reg_quality(self, n_samples:int=1000, samples=None, outputs:dict=None)->None: + def check_reg_quality(self, n_samples: int = 1000, samples=None, outputs: dict = None) -> None: """ Checks the quality of the metamodel for single output models based on: https://towardsdatascience.com/how-do-you-check-the-quality-of-your-regression-model-in-python-fa61759ff685 @@ -960,11 +962,11 @@ class PostProcessing: plt.close() # ------------------------------------------------------------------------- - def plot_metamodel_3d(self, n_samples = 10): + def plot_metamodel_3d(self, n_samples=10): """ Visualize the results of a PCE MetaModel as a 3D surface over two input parameters. - + Parameters ---------- n_samples : int @@ -991,25 +993,24 @@ class PostProcessing: title = 'MetaModel' else: title = 'Model' - X,Y = np.meshgrid(samples[:,0],samples[:,1]) + X, Y = np.meshgrid(samples[:, 0], samples[:, 1]) for name in self.engine.out_names: for t in range(mean[name].shape[1]): fig = plt.figure() ax = plt.axes(projection='3d') - ax.plot_surface(X, Y, np.atleast_2d(mean[name][:,t]), rstride=1, cstride=1, + ax.plot_surface(X, Y, np.atleast_2d(mean[name][:, t]), rstride=1, cstride=1, cmap='viridis', edgecolor='none') ax.set_title(title) ax.set_xlabel('$x_1$') ax.set_ylabel('$x_2$') ax.set_zlabel('$f(x_1,x_2)$') - + plt.grid() - + # save the figure to file fig.savefig(f'./{self.out_dir}/3DPlot_{title}_{name}{t}.pdf', - bbox_inches='tight') + bbox_inches='tight') plt.close(fig) - # ------------------------------------------------------------------------- @@ -1139,7 +1140,7 @@ class PostProcessing: List of x values. The default is []. x_axis : str, optional Label of the x axis. The default is "x [m]". - + Raises ------ AttributeError: This evaluation only support PCE-type models! diff --git a/tests/test_PostProcessing.py b/tests/test_PostProcessing.py index 64cc61533..a57e8c085 100644 --- a/tests/test_PostProcessing.py +++ b/tests/test_PostProcessing.py @@ -158,7 +158,8 @@ def test_check_reg_quality_pce(pce_engine) -> None: post = PostProcessing(engine) post.check_reg_quality(samples=engine.ExpDesign.X, outputs=engine.ExpDesign.Y) -#%% plot_metamodel_3d +#%% eplot_metamodel_3d + def test_plot_metamodel_3d_nopce(basic_engine) -> None: """ 3d eval of non-PCE metamodel -- GitLab