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

Raise when duplicate GEN_KW keys #6109

Merged
merged 1 commit into from
Sep 19, 2023
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
10 changes: 10 additions & 0 deletions src/ert/config/gen_kw_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from dataclasses import dataclass
from hashlib import sha256
from pathlib import Path
from typing import TYPE_CHECKING, Callable, Dict, List, Optional, TypedDict, overload

Check failure on line 11 in src/ert/config/gen_kw_config.py

View workflow job for this annotation

GitHub Actions / annotate-python-linting

line too long (85 > 79 characters)

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -120,7 +120,7 @@
if init_file and "%" not in init_file:
errors.append(
ConfigValidationError.with_context(
"Loading GEN_KW from files requires %d in file format", gen_kw

Check failure on line 123 in src/ert/config/gen_kw_config.py

View workflow job for this annotation

GitHub Actions / annotate-python-linting

line too long (82 > 79 characters)
)
)
if errors:
Expand All @@ -145,7 +145,7 @@
def _validate(self) -> None:
errors = []

def _check_non_negative_parameter(param: str, prior: PriorDict) -> None:

Check failure on line 148 in src/ert/config/gen_kw_config.py

View workflow job for this annotation

GitHub Actions / annotate-python-linting

line too long (80 > 79 characters)
key = prior["key"]
dist = prior["function"]
param_val = prior["parameters"][param]
Expand All @@ -157,7 +157,17 @@
).set_context(self.name)
)

unique_keys = set()
for prior in self.get_priors():
key = prior["key"]
if key in unique_keys:
errors.append(
ErrorInfo(
f"Duplicate GEN_KW keys {key!r} found, keys must be unique."

Check failure on line 166 in src/ert/config/gen_kw_config.py

View workflow job for this annotation

GitHub Actions / annotate-python-linting

line too long (84 > 79 characters)
).set_context(self.name)
)
unique_keys.add(key)

if prior["function"] == "LOGNORMAL":
_check_non_negative_parameter("MEAN", prior)
_check_non_negative_parameter("STD", prior)
Expand All @@ -172,7 +182,7 @@
if self.forward_init_file:
return self.read_from_runpath(Path(), real_nr)

logging.info(f"Sampling parameter {self.name} for realization {real_nr}")

Check failure on line 185 in src/ert/config/gen_kw_config.py

View workflow job for this annotation

GitHub Actions / annotate-python-linting

line too long (81 > 79 characters)
keys = [e.name for e in self.transfer_functions]
parameter_value = self._sample_value(
self.name,
Expand All @@ -185,7 +195,7 @@
return xr.Dataset(
{
"values": ("names", parameter_value),
"transformed_values": ("names", self.transform(parameter_value)),

Check failure on line 198 in src/ert/config/gen_kw_config.py

View workflow job for this annotation

GitHub Actions / annotate-python-linting

line too long (81 > 79 characters)
"names": keys,
}
)
Expand All @@ -197,7 +207,7 @@
) -> xr.Dataset:
keys = [e.name for e in self.transfer_functions]
if not self.forward_init_file:
raise ValueError("loading gen_kw values requires forward_init_file")

Check failure on line 210 in src/ert/config/gen_kw_config.py

View workflow job for this annotation

GitHub Actions / annotate-python-linting

line too long (80 > 79 characters)

parameter_value = self._values_from_file(
real_nr,
Expand All @@ -208,7 +218,7 @@
return xr.Dataset(
{
"values": ("names", parameter_value),
"transformed_values": ("names", self.transform(parameter_value)),

Check failure on line 221 in src/ert/config/gen_kw_config.py

View workflow job for this annotation

GitHub Actions / annotate-python-linting

line too long (81 > 79 characters)
"names": keys,
}
)
Expand All @@ -216,11 +226,11 @@
def write_to_runpath(
self, run_path: Path, real_nr: int, ensemble: EnsembleReader
) -> Dict[str, Dict[str, float]]:
array = ensemble.load_parameters(self.name, real_nr, var="transformed_values")

Check failure on line 229 in src/ert/config/gen_kw_config.py

View workflow job for this annotation

GitHub Actions / annotate-python-linting

line too long (86 > 79 characters)
if not array.size == len(self.transfer_functions):
raise ValueError(
f"The configuration of GEN_KW parameter {self.name}"
f" is of size {len(self.transfer_functions)}, expected {array.size}"

Check failure on line 233 in src/ert/config/gen_kw_config.py

View workflow job for this annotation

GitHub Actions / annotate-python-linting

line too long (84 > 79 characters)
)

data = dict(zip(array["names"].values.tolist(), array.values.tolist()))
Expand Down
20 changes: 20 additions & 0 deletions tests/unit_tests/config/test_gen_kw_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ def test_gen_kw_config():
assert len(conf.transfer_functions) == 3


@pytest.mark.usefixtures("use_tmpdir")
def test_gen_kw_config_duplicate_keys_raises():
with pytest.raises(
ConfigValidationError,
match="Duplicate GEN_KW keys 'KEY2' found, keys must be unique.",
):
GenKwConfig(
name="KEY",
forward_init=False,
template_file="",
transfer_function_definitions=[
"KEY1 UNIFORM 0 1",
"KEY2 UNIFORM 0 1",
"KEY2 UNIFORM 0 1",
"KEY3 UNIFORM 0 1",
],
output_file="kw.txt",
)


@pytest.mark.usefixtures("use_tmpdir")
def test_gen_kw_config_get_priors():
parameter_file = "parameters.txt"
Expand Down
Loading