From 9574ae1f5f0d1c990d838e31e044950cff243c12 Mon Sep 17 00:00:00 2001 From: kohlhaasrebecca <rebecca.kohlhaas@outlook.com> Date: Mon, 26 Aug 2024 10:39:31 +0200 Subject: [PATCH] [tests] Use PCE instead of MetaModel in tests that need a fitted surrogate --- .coverage.DESKTOP-ATMEKSV.10624.XjrvRHAx | 0 .coverage.DESKTOP-ATMEKSV.22780.XWOfrNvx | 0 .coverage.DESKTOP-ATMEKSV.25100.XbtpWDFx | 0 .coverage.DESKTOP-ATMEKSV.34604.XhfDgthx | 0 .coverage.DESKTOP-ATMEKSV.37188.XIOzbGrx | 0 src/bayesvalidrox/surrogate_models/engine.py | 2 + tests/test_BayesInference.py | 19 ++++---- tests/test_SequentialDesign.py | 49 ++++++++++---------- tests/test_engine.py | 13 +++--- 9 files changed, 44 insertions(+), 39 deletions(-) delete mode 100644 .coverage.DESKTOP-ATMEKSV.10624.XjrvRHAx delete mode 100644 .coverage.DESKTOP-ATMEKSV.22780.XWOfrNvx delete mode 100644 .coverage.DESKTOP-ATMEKSV.25100.XbtpWDFx delete mode 100644 .coverage.DESKTOP-ATMEKSV.34604.XhfDgthx delete mode 100644 .coverage.DESKTOP-ATMEKSV.37188.XIOzbGrx diff --git a/.coverage.DESKTOP-ATMEKSV.10624.XjrvRHAx b/.coverage.DESKTOP-ATMEKSV.10624.XjrvRHAx deleted file mode 100644 index e69de29bb..000000000 diff --git a/.coverage.DESKTOP-ATMEKSV.22780.XWOfrNvx b/.coverage.DESKTOP-ATMEKSV.22780.XWOfrNvx deleted file mode 100644 index e69de29bb..000000000 diff --git a/.coverage.DESKTOP-ATMEKSV.25100.XbtpWDFx b/.coverage.DESKTOP-ATMEKSV.25100.XbtpWDFx deleted file mode 100644 index e69de29bb..000000000 diff --git a/.coverage.DESKTOP-ATMEKSV.34604.XhfDgthx b/.coverage.DESKTOP-ATMEKSV.34604.XhfDgthx deleted file mode 100644 index e69de29bb..000000000 diff --git a/.coverage.DESKTOP-ATMEKSV.37188.XIOzbGrx b/.coverage.DESKTOP-ATMEKSV.37188.XIOzbGrx deleted file mode 100644 index e69de29bb..000000000 diff --git a/src/bayesvalidrox/surrogate_models/engine.py b/src/bayesvalidrox/surrogate_models/engine.py index 8720ed0f8..0c4f15263 100644 --- a/src/bayesvalidrox/surrogate_models/engine.py +++ b/src/bayesvalidrox/surrogate_models/engine.py @@ -655,6 +655,8 @@ class Engine: likelihoods = 1.0 # Loop over the outputs + print(self.out_names) + print(y_hat_pce) for idx, out in enumerate(self.out_names): # (Meta)Model Output diff --git a/tests/test_BayesInference.py b/tests/test_BayesInference.py index 9048776c8..65a5e9cca 100644 --- a/tests/test_BayesInference.py +++ b/tests/test_BayesInference.py @@ -34,6 +34,7 @@ sys.path.append("../src/") from bayesvalidrox.surrogate_models.inputs import Input from bayesvalidrox.surrogate_models.exp_designs import ExpDesigns from bayesvalidrox.surrogate_models.surrogate_models import MetaModel +from bayesvalidrox.surrogate_models.polynomial_chaos import PCE from bayesvalidrox.pylink.pylink import PyLinkForwardModel as PL from bayesvalidrox.surrogate_models.engine import Engine from bayesvalidrox.bayes_inference.discrepancy import Discrepancy @@ -110,7 +111,7 @@ def test_create_inference() -> None: expdes.Y = {'Z': [[0.4], [0.5], [0.45]]} expdes.x_values = np.array([0]) # Error in plots if this is not available - mm = MetaModel(inp) + mm = PCE(inp) mm.fit(expdes.X, expdes.Y) expdes.generate_ED(expdes.n_init_samples, max_pce_deg=np.max(mm._pce_deg)) @@ -258,7 +259,7 @@ def test_corr_factor_BME() -> None: expdes.X = np.array([[0], [1], [0.5]]) expdes.Y = {'Z': [[0.4], [0.5], [0.45]]} - mm = MetaModel(inp) + mm = PCE(inp) mm.fit(expdes.X, expdes.Y) mod = PL() engine = Engine(mm, mod, expdes) @@ -285,7 +286,7 @@ def test_corr_factor_BME_selectedindices() -> None: expdes.X = np.array([[0], [1], [0.5]]) expdes.Y = {'Z': [[0.4], [0.5], [0.45]]} - mm = MetaModel(inp) + mm = PCE(inp) mm.fit(expdes.X, expdes.Y) mod = PL() engine = Engine(mm, mod, expdes) @@ -570,7 +571,7 @@ def test_perform_bootstrap() -> None: expdes.Y = {'Z': [[0.4], [0.5], [0.45]]} expdes.x_values = np.array([0]) # Error in plots if this is not available - mm = MetaModel(inp) + mm = PCE(inp) mm.n_params = 1 mm.fit(expdes.X, expdes.Y) expdes.generate_ED(expdes.n_init_samples, max_pce_deg=np.max(1)) @@ -613,7 +614,7 @@ def test_perform_bootstrap_bayesloocv() -> None: expdes.Y = {'Z': [[0.4], [0.5], [0.45]]} expdes.x_values = np.array([0]) # Error in plots if this is not available - mm = MetaModel(inp) + mm = PCE(inp) mm.n_params = 1 mm.fit(expdes.X, expdes.Y) expdes.generate_ED(expdes.n_init_samples, max_pce_deg=np.max(1)) @@ -659,7 +660,7 @@ def create_error_model_prior() -> None: expdes.X = np.array([[0], [1], [0.5]]) expdes.Y = {'Z': [[0.4], [0.5], [0.45]]} - mm = MetaModel(inp) + mm = PCE(inp) mm.n_params = 1 mm.fit(expdes.X, expdes.Y) expdes.generate_ED(expdes.n_init_samples, max_pce_deg=np.max(1)) @@ -701,7 +702,7 @@ def create_error_model_posterior() -> None: expdes.X = np.array([[0], [1], [0.5]]) expdes.Y = {'Z': [[0.4], [0.5], [0.45]]} - mm = MetaModel(inp) + mm = PCE(inp) mm.n_params = 1 mm.fit(expdes.X, expdes.Y) expdes.generate_ED(expdes.n_init_samples, max_pce_deg=np.max(1)) @@ -750,7 +751,7 @@ def test_posterior_predictive() -> None: expdes.Y = {'Z': [[0.4], [0.5], [0.45]]} expdes.x_values = np.array([0]) # Error in plots if this is not available - mm = MetaModel(inp) + mm = PCE(inp) mm.n_params = 1 mm.fit(expdes.X, expdes.Y) expdes.generate_ED(expdes.n_init_samples, max_pce_deg=np.max(1)) @@ -1053,7 +1054,7 @@ if __name__ == '__main__': expdes.Y = {'Z': [[0.4], [0.5], [0.45]]} expdes.x_values = np.array([0]) # Error in plots if this is not - mm = MetaModel(inp) + mm = PCE(inp) mm.n_params = 1 mm.fit(expdes.X, expdes.Y) expdes.generate_ED(expdes.n_init_samples, max_pce_deg=np.max(1)) diff --git a/tests/test_SequentialDesign.py b/tests/test_SequentialDesign.py index f922a77d7..bcffc5d05 100644 --- a/tests/test_SequentialDesign.py +++ b/tests/test_SequentialDesign.py @@ -39,6 +39,7 @@ from bayesvalidrox.surrogate_models.inputs import Input from bayesvalidrox.surrogate_models.exp_designs import ExpDesigns from bayesvalidrox.surrogate_models.sequential_design import SequentialDesign, hellinger_distance, subdomain, logpdf from bayesvalidrox.surrogate_models.surrogate_models import MetaModel +from bayesvalidrox.surrogate_models.polynomial_chaos import PCE from bayesvalidrox.pylink.pylink import PyLinkForwardModel as PL from bayesvalidrox.surrogate_models.engine import Engine from bayesvalidrox.bayes_inference.discrepancy import Discrepancy @@ -137,7 +138,7 @@ def test_tradeoff_weights_adaptiveit1() -> None: expdes.n_max_samples = 4 expdes.X = np.array([[0], [1], [0.5]]) expdes.Y = {'Z': [[0.4], [0.5], [0.45]]} - mm = MetaModel(inp) + mm = PCE(inp) mm.fit(expdes.X, expdes.Y) mod = PL() @@ -168,7 +169,7 @@ def test_choose_next_sample() -> None: expdes.exploit_method = 'Space-filling' expdes.util_func = 'Space-filling' - mm = MetaModel(inp) + mm = PCE(inp) mm.fit(expdes.X, expdes.Y) expdes.generate_ED(expdes.n_init_samples, max_pce_deg=np.max(mm._pce_deg)) mod = PL() @@ -198,7 +199,7 @@ def test_choose_next_sample_da_spaceparallel() -> None: expdes.exploit_method = 'Space-filling' expdes.util_func = 'Space-filling' - mm = MetaModel(inp) + mm = PCE(inp) mm.fit(expdes.X, expdes.Y) expdes.generate_ED(expdes.n_init_samples, max_pce_deg=np.max(mm._pce_deg)) mod = PL() @@ -229,7 +230,7 @@ def test_choose_next_sample_da_spacenoparallel() -> None: expdes.exploit_method = 'Space-filling' expdes.util_func = 'Space-filling' - mm = MetaModel(inp) + mm = PCE(inp) mm.fit(expdes.X, expdes.Y) expdes.generate_ED(expdes.n_init_samples, max_pce_deg=np.max(mm._pce_deg)) mod = PL() @@ -260,7 +261,7 @@ def test_choose_next_sample_loo_space() -> None: expdes.exploit_method = 'Space-filling' expdes.util_func = 'Space-filling' - mm = MetaModel(inp) + mm = PCE(inp) mm.fit(expdes.X, expdes.Y) expdes.generate_ED(expdes.n_init_samples, max_pce_deg=np.max(mm._pce_deg)) mod = PL() @@ -290,7 +291,7 @@ def test_choose_next_sample_vor_space() -> None: expdes.exploit_method = 'Space-filling' expdes.util_func = 'Space-filling' - mm = MetaModel(inp) + mm = PCE(inp) mm.fit(expdes.X, expdes.Y) expdes.generate_ED(expdes.n_init_samples, max_pce_deg=np.max(mm._pce_deg)) mod = PL() @@ -325,7 +326,7 @@ def test_choose_next_sample_latin_space() -> None: expdes.exploit_method = 'Space-filling' expdes.util_func = 'Space-filling' - mm = MetaModel(inp) + mm = PCE(inp) mm.fit(expdes.X, expdes.Y) expdes.generate_ED(expdes.n_init_samples, max_pce_deg=np.max(mm._pce_deg)) mod = PL() @@ -355,7 +356,7 @@ def test_choose_next_sample_latin_alphD() -> None: expdes.exploit_method = 'alphabetic' expdes.util_func = 'D-Opt' - mm = MetaModel(inp) + mm = PCE(inp) mm.fit(expdes.X, expdes.Y) expdes.generate_ED(expdes.n_init_samples, max_pce_deg=np.max(mm._pce_deg)) mod = PL() @@ -385,7 +386,7 @@ def test_choose_next_sample_latin_alphK() -> None: expdes.exploit_method = 'alphabetic' expdes.util_func = 'K-Opt' - mm = MetaModel(inp) + mm = PCE(inp) mm.fit(expdes.X, expdes.Y) expdes.generate_ED(expdes.n_init_samples, max_pce_deg=np.max(mm._pce_deg)) mod = PL() @@ -415,7 +416,7 @@ def test_choose_next_sample_latin_alphA() -> None: expdes.exploit_method = 'alphabetic' expdes.util_func = 'A-Opt' - mm = MetaModel(inp) + mm = PCE(inp) mm.fit(expdes.X, expdes.Y) expdes.generate_ED(expdes.n_init_samples, max_pce_deg=np.max(mm._pce_deg)) mod = PL() @@ -446,7 +447,7 @@ def test_choose_next_sample_latin_VarALM() -> None: expdes.exploit_method = 'VarOptDesign' expdes.util_func = 'ALM' - mm = MetaModel(inp) + mm = PCE(inp) mm.fit(expdes.X, expdes.Y) expdes.generate_ED(expdes.n_init_samples, max_pce_deg=np.max(mm._pce_deg)) mod = PL() @@ -477,7 +478,7 @@ def test_choose_next_sample_latin_VarEIGF() -> None: expdes.exploit_method = 'VarOptDesign' expdes.util_func = 'EIGF' - mm = MetaModel(inp) + mm = PCE(inp) mm.fit(expdes.X, expdes.Y) expdes.generate_ED(expdes.n_init_samples, max_pce_deg=np.max(mm._pce_deg)) mod = PL() @@ -510,7 +511,7 @@ if 0: expdes.exploit_method = 'VarOptDesign' expdes.util_func = 'LOOCV' - mm = MetaModel(inp) + mm = PCE(inp) mm.fit(expdes.X, expdes.Y) expdes.generate_ED(expdes.n_init_samples, max_pce_deg=np.max(mm._pce_deg)) mod = PL() @@ -541,7 +542,7 @@ def test_choose_next_sample_latin_BODMI() -> None: expdes.explore_method = 'latin-hypercube' expdes.exploit_method = 'BayesOptDesign' expdes.util_func = 'MI' - mm = MetaModel(inp) + mm = PCE(inp) mm.fit(expdes.X, expdes.Y) expdes.generate_ED(expdes.n_init_samples, max_pce_deg=np.max(mm._pce_deg)) mod = PL() @@ -574,7 +575,7 @@ def test_choose_next_sample_vor_BODMI() -> None: expdes.explore_method = 'voronoi' expdes.exploit_method = 'BayesOptDesign' expdes.util_func = 'MI' - mm = MetaModel(inp) + mm = PCE(inp) mm.fit(expdes.X, expdes.Y) expdes.generate_ED(expdes.n_init_samples, max_pce_deg=np.max(mm._pce_deg)) mod = PL() @@ -612,7 +613,7 @@ def test_choose_next_sample_latin_BODALC() -> None: expdes.explore_method = 'latin-hypercube' expdes.exploit_method = 'BayesOptDesign' expdes.util_func = 'ALC' - mm = MetaModel(inp) + mm = PCE(inp) mm.fit(expdes.X, expdes.Y) expdes.generate_ED(expdes.n_init_samples, max_pce_deg=np.max(mm._pce_deg)) mod = PL() @@ -645,7 +646,7 @@ def test_choose_next_sample_latin_BODDKL() -> None: expdes.explore_method = 'latin-hypercube' expdes.exploit_method = 'BayesOptDesign' expdes.util_func = 'DKL' - mm = MetaModel(inp) + mm = PCE(inp) mm.fit(expdes.X, expdes.Y) expdes.generate_ED(expdes.n_init_samples, max_pce_deg=np.max(mm._pce_deg)) mod = PL() @@ -678,7 +679,7 @@ def test_choose_next_sample_latin_BODDPP() -> None: expdes.explore_method = 'latin-hypercube' expdes.exploit_method = 'BayesOptDesign' expdes.util_func = 'DPP' - mm = MetaModel(inp) + mm = PCE(inp) mm.fit(expdes.X, expdes.Y) expdes.generate_ED(expdes.n_init_samples, max_pce_deg=np.max(mm._pce_deg)) mod = PL() @@ -711,7 +712,7 @@ def test_choose_next_sample_latin_BODAPP() -> None: expdes.explore_method = 'latin-hypercube' expdes.exploit_method = 'BayesOptDesign' expdes.util_func = 'APP' - mm = MetaModel(inp) + mm = PCE(inp) mm.fit(expdes.X, expdes.Y) expdes.generate_ED(expdes.n_init_samples, max_pce_deg=np.max(mm._pce_deg)) mod = PL() @@ -744,7 +745,7 @@ def test_choose_next_sample_latin_BODMI_() -> None: expdes.explore_method = 'latin-hypercube' expdes.exploit_method = 'BayesOptDesign' expdes.util_func = 'MI' - mm = MetaModel(inp) + mm = PCE(inp) mm.fit(expdes.X, expdes.Y) expdes.generate_ED(expdes.n_init_samples, max_pce_deg=np.max(mm._pce_deg)) mod = PL() @@ -777,7 +778,7 @@ if 0: expdes.explore_method = 'latin-hypercube' expdes.exploit_method = 'BayesActDesign' expdes.util_func = 'BME' - mm = MetaModel(inp) + mm = PCE(inp) mm.fit(expdes.X, expdes.Y) expdes.generate_ED(expdes.n_init_samples, max_pce_deg=np.max(mm._pce_deg)) mod = PL() @@ -811,7 +812,7 @@ if 0: expdes.explore_method = 'latin-hypercube' expdes.exploit_method = 'BayesActDesign' expdes.util_func = 'DKL' - mm = MetaModel(inp) + mm = PCE(inp) mm.fit(expdes.X, expdes.Y) expdes.generate_ED(expdes.n_init_samples, max_pce_deg=np.max(mm._pce_deg)) mod = PL() @@ -845,7 +846,7 @@ if 0: expdes.explore_method = 'latin-hypercube' expdes.exploit_method = 'BayesActDesign' expdes.util_func = 'infEntropy' - mm = MetaModel(inp) + mm = PCE(inp) mm.fit(expdes.X, expdes.Y) expdes.generate_ED(expdes.n_init_samples, max_pce_deg=np.max(mm._pce_deg)) mod = PL() @@ -926,7 +927,7 @@ if __name__ == '__main__': expdes.Y = {'Z': [[0.4], [0.5], [0.45]]} expdes.x_values = np.array([0]) # Error in plots if this is not - mm = MetaModel(inp) + mm = PCE(inp) mm.n_params = 1 mm.fit(expdes.X, expdes.Y) expdes.generate_ED(expdes.n_init_samples, max_pce_deg=np.max(1)) diff --git a/tests/test_engine.py b/tests/test_engine.py index 02014ca92..cbb0e8a3f 100644 --- a/tests/test_engine.py +++ b/tests/test_engine.py @@ -39,6 +39,7 @@ sys.path.append("../src/") from bayesvalidrox.surrogate_models.inputs import Input from bayesvalidrox.surrogate_models.exp_designs import ExpDesigns from bayesvalidrox.surrogate_models.surrogate_models import MetaModel +from bayesvalidrox.surrogate_models.polynomial_chaos import PCE from bayesvalidrox.pylink.pylink import PyLinkForwardModel as PL from bayesvalidrox.surrogate_models.engine import Engine @@ -92,7 +93,7 @@ def test__error_Mean_Std_nomc() -> None: inp.add_marginals() inp.Marginals[0].dist_type = 'normal' inp.Marginals[0].parameters = [0, 1] - mm = MetaModel(inp) + mm = PCE(inp) mm.fit([[0.0], [1.0]], {'Z': [[0.5], [0.5]]}) expdes = ExpDesigns(inp) mod = PL() @@ -125,7 +126,7 @@ def test__validError() -> None: inp.add_marginals() inp.Marginals[0].dist_type = 'normal' inp.Marginals[0].parameters = [0, 1] - mm = MetaModel(inp) + mm = PCE(inp) mm.fit([[0.0], [1.0]], {'Z': [[0.5], [0.5]]}) expdes = ExpDesigns(inp) mod = PL() @@ -148,7 +149,7 @@ def test__BME_Calculator() -> None: inp.add_marginals() inp.Marginals[0].dist_type = 'normal' inp.Marginals[0].parameters = [0, 1] - mm = MetaModel(inp) + mm = PCE(inp) mm.fit([[0.0], [0.5], [1.0]], {'Z': [[0.5], [0.4], [0.5]]}) expdes = ExpDesigns(inp) expdes.generate_ED(2, max_pce_deg=1) @@ -171,7 +172,7 @@ def test__BME_Calculator_rmse() -> None: inp.add_marginals() inp.Marginals[0].dist_type = 'normal' inp.Marginals[0].parameters = [0, 1] - mm = MetaModel(inp) + mm = PCE(inp) mm.fit([[0.0], [0.5], [1.0]], {'Z': [[0.5], [0.4], [0.5]]}) expdes = ExpDesigns(inp) expdes.generate_ED(2, max_pce_deg=1) @@ -194,7 +195,7 @@ def test__BME_Calculator_lik() -> None: inp.add_marginals() inp.Marginals[0].dist_type = 'normal' inp.Marginals[0].parameters = [0, 1] - mm = MetaModel(inp) + mm = PCE(inp) mm.fit([[0.0], [0.5], [1.0]], {'Z': [[0.5], [0.4], [0.5]]}) expdes = ExpDesigns(inp) expdes.generate_ED(2, max_pce_deg=1) @@ -222,7 +223,7 @@ def test__BME_Calculator_2d() -> None: inp.add_marginals() inp.Marginals[1].dist_type = 'normal' inp.Marginals[1].parameters = [0, 1] - mm = MetaModel(inp) + mm = PCE(inp) mm.fit([[0.0, 0.0], [0.5, 0.1], [1.0, 0.9]], {'Z': [[0.5], [0.4], [0.5]]}) expdes = ExpDesigns(inp) expdes.generate_ED(2, max_pce_deg=1) -- GitLab