From 8466cb4b9e9b025213e10e5c338c592616dfbf27 Mon Sep 17 00:00:00 2001 From: faridm69 <faridmohammadi69@gmail.com> Date: Wed, 3 Jun 2020 12:10:20 +0200 Subject: [PATCH] [surrogate] solved the issue with polynomial coeffs calulations for aPCE. --- BayesValidRox/BayesInference/BayesInference.py | 13 ++++--------- BayesValidRox/surrogate_models/ExpDesigns.py | 13 +++++++------ BayesValidRox/surrogate_models/Exploration.py | 4 ++-- BayesValidRox/surrogate_models/surrogate_models.py | 10 +++++----- 4 files changed, 18 insertions(+), 22 deletions(-) diff --git a/BayesValidRox/BayesInference/BayesInference.py b/BayesValidRox/BayesInference/BayesInference.py index 3276c5f9d..f886a0a0e 100644 --- a/BayesValidRox/BayesInference/BayesInference.py +++ b/BayesValidRox/BayesInference/BayesInference.py @@ -679,9 +679,8 @@ class BayesInference: plt.close() - - # -------- Find MAP and run PCEModel and origModel for it -------- + # -------- Find MAP and run PCEModel and origModel -------- # Compute the MAP if self.MAP =='Mean': MAP_theta = self.Posterior_df.to_numpy().mean(axis=0).reshape((1,NofPa)) @@ -698,13 +697,9 @@ class BayesInference: MAP_origModel = self.eval_Model(Samples=MAP_theta) self.MAPorigModel = MAP_origModel - # -------- Posteior parameters-------- + # -------- Posteior parameters -------- if self.PlotPostDist == True: -# fig, ax = plt.subplots() -# with sns.axes_style("whitegrid"): -# g = sns.pairplot(self.Posterior_df, diag_kind = 'kde') -# g.fig.set_size_inches(24,16) -# g.savefig('./'+OutputDir+'/Poisterior_Distribution.svg') + import corner parNames = self.PCEModel.ExpDesign.parNames figPosterior = corner.corner(self.Posterior_df.to_numpy(), labels=parNames, @@ -730,7 +725,7 @@ class BayesInference: figPosterior.set_size_inches((24,16)) figPosterior.savefig('./'+OutputDir+'/Poisterior_Distribution.svg', - bbox_inches='tight') + bbox_inches='tight') # -------- Posteior perdictives -------- if self.PlotPostPred == True: diff --git a/BayesValidRox/surrogate_models/ExpDesigns.py b/BayesValidRox/surrogate_models/ExpDesigns.py index 15c2e8a82..53a21c9c7 100644 --- a/BayesValidRox/surrogate_models/ExpDesigns.py +++ b/BayesValidRox/surrogate_models/ExpDesigns.py @@ -8,11 +8,9 @@ Created on Sat Aug 24 14:41:02 2019 import numpy as np import math -import os -import sys import itertools -from scipy import stats import chaospy +from tqdm import tqdm from .aPoly_Construction import aPoly_Construction @@ -41,7 +39,7 @@ class ExpDesigns: self.Y = None - def GetSample(self, NrSamples, MaxPceDegree=2, SamplingMethod = 'random'): + def GetSample(self, NrSamples, SamplingMethod='random', MaxPceDegree=None): """ @@ -187,8 +185,11 @@ class ExpDesigns: for parIdx in range(NofPa): self.Input_distributions[parIdx,:] = np.array(Inputs.Marginals[parIdx].InputValues) - # Create orthogonal polynomial coefficients - aPoly_Construction(self.Input_distributions[parIdx,:], MaxPceDegree, parIdx) + + # Create orthogonal polynomial coefficients if necessary + if MaxPceDegree is not None: + for parIdx in tqdm(range(NofPa), ascii=True, desc ="Computing orth. polynomial coeffs"): + aPoly_Construction(self.Input_distributions[parIdx,:], MaxPceDegree, parIdx) else: # Generate random samples based on parameter distributions self.Input_distributions = chaospy.generate_samples(self.MCSize, domain=self.JDist).T diff --git a/BayesValidRox/surrogate_models/Exploration.py b/BayesValidRox/surrogate_models/Exploration.py index e7cc6deb6..f8d977701 100644 --- a/BayesValidRox/surrogate_models/Exploration.py +++ b/BayesValidRox/surrogate_models/Exploration.py @@ -188,7 +188,7 @@ class Exploration: # ----- Compute the number of random points ----- if allCandidates is None: # Generate MC Samples - allCandidates = PCEModel.ExpDesign.GetSample(self.numNewSamples, SamplingMethod=ExplorationMethod) + allCandidates = PCEModel.ExpDesign.GetSample(self.numNewSamples, 'random') self.allCandidates = allCandidates # initialization @@ -294,7 +294,7 @@ class Exploration: # points[:,i] = self.scaleColumns(points[:,i],Bounds[i][0],Bounds[i][1]) ExpDesign = ExpDesigns(PCEModel.Inputs) - points = ExpDesign.GetSample(nPoints,PCEModel.MaxPceDegree, 'random') + points = ExpDesign.GetSample(nPoints, 'random') self.allCandidates = points diff --git a/BayesValidRox/surrogate_models/surrogate_models.py b/BayesValidRox/surrogate_models/surrogate_models.py index 6c06063ae..8ff76de96 100644 --- a/BayesValidRox/surrogate_models/surrogate_models.py +++ b/BayesValidRox/surrogate_models/surrogate_models.py @@ -170,7 +170,7 @@ class aPCE: def Exe_ExpDesign(self, Model): # Get the sample for X (GetSample) - self.ExpDesign.X = self.ExpDesign.GetSample(self.ExpDesign.NrSamples, self.MaxPceDegree, self.ExpDesign.SamplingMethod) + self.ExpDesign.X = self.ExpDesign.GetSample(self.ExpDesign.NrSamples, self.ExpDesign.SamplingMethod, self.MaxPceDegree) self.OptimalCollocationPointsBase = self.ExpDesign.X self.BoundTuples = self.ExpDesign.BoundTuples @@ -1275,7 +1275,7 @@ class aPCE: # Enriching Monte Carlo samples if need be if ESS != 0: - X_MC = self.ExpDesign.GetSample(MCsize,self.MaxPceDegree, 'random'); + X_MC = self.ExpDesign.GetSample(MCsize, 'random'); # Evaluate the PCEModel at the given samples Y_PC_MC, std_PC_MC = PCE_SparseBayes_can.eval_PCEmodel(X_MC) @@ -1547,7 +1547,7 @@ class aPCE: OutputNames = list(OutputDictY.keys())[1:] # Generate random samples - allCandidates = self.ExpDesign.GetSample(NCandidate,self.MaxPceDegree, + allCandidates = self.ExpDesign.GetSample(NCandidate, SamplingMethod='random') # Construct error model based on LCerror @@ -1646,7 +1646,7 @@ class aPCE: # Create a sample pool for rejection sampling MCsize = 15000 - X_MC = self.ExpDesign.GetSample(MCsize,self.MaxPceDegree, 'random'); + X_MC = self.ExpDesign.GetSample(MCsize, 'random'); # Multiprocessing pool = multiprocessing.Pool(multiprocessing.cpu_count()) @@ -2251,7 +2251,7 @@ class aPCE: # Generate samples for Monte Carlo simulation if len(validLikelihoods)==0: ExpDesign = ExpDesigns(self.Inputs) - X_MC = ExpDesign.GetSample(MCsize,self.MaxPceDegree, SamplingMethod); + X_MC = ExpDesign.GetSample(MCsize, SamplingMethod); else: X_MC = self.validSamples MCsize = X_MC.shape[0] -- GitLab