From 5655414f9c8f4d869703bb6deda63cdd690d7183 Mon Sep 17 00:00:00 2001 From: larsevj Date: Thu, 19 Dec 2024 15:08:39 +0100 Subject: [PATCH] Validate derrf distribution parameters on startup --- src/ert/config/gen_kw_config.py | 26 ++++++++++++++ .../unit_tests/config/test_gen_kw_config.py | 36 +++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/src/ert/config/gen_kw_config.py b/src/ert/config/gen_kw_config.py index f371c5622d1..442eede74ff 100644 --- a/src/ert/config/gen_kw_config.py +++ b/src/ert/config/gen_kw_config.py @@ -224,6 +224,32 @@ def _check_valid_triangular_parameters(prior: PriorDict) -> None: ).set_context(self.name) ) + def _check_valid_derrf_parameters(prior: PriorDict) -> None: + key = prior["key"] + dist = prior["function"] + steps, min_, max_, _, width = prior["parameters"].values() + if not (steps >= 1 and steps.is_integer()): + errors.append( + ErrorInfo( + f"NBINS {steps} must be a positive integer larger than 1" + f" for {dist} distributed parameter {key}", + ).set_context(self.name) + ) + if not (min_ < max_): + errors.append( + ErrorInfo( + f"The minimum {min_} must be less than the maximum {max_}" + f" for {dist} distributed parameter {key}", + ).set_context(self.name) + ) + if not (width > 0): + errors.append( + ErrorInfo( + f"The width {width} must be greater than 0" + f" for {dist} distributed parameter {key}", + ).set_context(self.name) + ) + unique_keys = set() for prior in self.get_priors(): key = prior["key"] diff --git a/tests/ert/unit_tests/config/test_gen_kw_config.py b/tests/ert/unit_tests/config/test_gen_kw_config.py index b88d582a9e8..32aaa21c662 100644 --- a/tests/ert/unit_tests/config/test_gen_kw_config.py +++ b/tests/ert/unit_tests/config/test_gen_kw_config.py @@ -681,3 +681,39 @@ def test_validation_triangular_distribution( ErtConfig.from_file("config.ert") else: ErtConfig.from_file("config.ert") + + +@pytest.mark.parametrize( + "distribution, nbins, min, max, skew, width, error", + [ + ("DERRF", "10", "-1", "3", "-1", "2", None), + ], +) +def test_validation_derrf_distribution( + tmpdir, distribution, nbins, min, max, skew, width, error +): + with tmpdir.as_cwd(): + config = dedent( + """ + JOBNAME my_name%d + NUM_REALIZATIONS 1 + GEN_KW KW_NAME template.txt kw.txt prior.txt + """ + ) + with open("config.ert", "w", encoding="utf-8") as fh: + fh.writelines(config) + with open("template.txt", "w", encoding="utf-8") as fh: + fh.writelines("MY_KEYWORD ") + with open("prior.txt", "w", encoding="utf-8") as fh: + fh.writelines( + f"MY_KEYWORD {distribution} {nbins} {min} {max} {skew} {width}" + ) + + if error: + with pytest.raises( + ConfigValidationError, + match=error, + ): + ErtConfig.from_file("config.ert") + else: + ErtConfig.from_file("config.ert")