Skip to content

Commit

Permalink
Validate derrf distribution parameters on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
larsevj committed Dec 19, 2024
1 parent d9088eb commit 5655414
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/ert/config/gen_kw_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
36 changes: 36 additions & 0 deletions tests/ert/unit_tests/config/test_gen_kw_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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")

0 comments on commit 5655414

Please sign in to comment.