Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate triangular dist parameters on startup #9596

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/ert/config/gen_kw_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,25 @@ def _check_non_negative_parameter(param: str, prior: PriorDict) -> None:
).set_context(self.name)
)

def _check_valid_triangular_parameters(prior: PriorDict) -> None:
key = prior["key"]
dist = prior["function"]
xmin, xmode, xmax = prior["parameters"].values()
if not (xmin < xmax):
errors.append(
ErrorInfo(
f"Minimum {xmin} must be strictly less than the maxiumum {xmax}"
f" for {dist} distributed parameter {key}",
).set_context(self.name)
)
if not (xmin <= xmode <= xmax):
errors.append(
ErrorInfo(
f"The mode {xmode} must be between the minimum {xmin} and maximum {xmax}"
f" for {dist} distributed parameter {key}",
).set_context(self.name)
)

unique_keys = set()
for prior in self.get_priors():
key = prior["key"]
Expand All @@ -219,6 +238,8 @@ def _check_non_negative_parameter(param: str, prior: PriorDict) -> None:
if prior["function"] == "LOGNORMAL":
_check_non_negative_parameter("MEAN", prior)
_check_non_negative_parameter("STD", prior)
elif prior["function"] == "TRIANGULAR":
_check_valid_triangular_parameters(prior)
elif prior["function"] in {"NORMAL", "TRUNCATED_NORMAL"}:
_check_non_negative_parameter("STD", prior)
if errors:
Expand Down
56 changes: 56 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 @@ -625,3 +625,59 @@ def test_suggestion_on_empty_parameter_file(tmp_path):
],
}
)


@pytest.mark.parametrize(
"distribution, min, mode, max, error",
[
("TRIANGULAR", "0", "2", "3", None),
(
"TRIANGULAR",
"3.0",
"3.0",
"3.0",
"Minimum 3.0 must be strictly less than the maxiumum 3.0",
),
("TRIANGULAR", "-1", "0", "1", None),
(
"TRIANGULAR",
"3.0",
"6.0",
"5.5",
"The mode 6.0 must be between the minimum 3.0 and maximum 5.5",
),
(
"TRIANGULAR",
"3.0",
"-6.0",
"5.5",
"The mode -6.0 must be between the minimum 3.0 and maximum 5.5",
),
],
)
def test_validation_triangular_distribution(
tmpdir, distribution, min, mode, max, 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} {min} {mode} {max}")

if error:
with pytest.raises(
ConfigValidationError,
match=error,
):
ErtConfig.from_file("config.ert")
else:
ErtConfig.from_file("config.ert")
Loading