Skip to content

Commit

Permalink
Require at least one key for SummaryConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
eivindjahren committed Feb 16, 2024
1 parent 84dbd67 commit 0831be9
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/ert/config/ensemble_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def make_field(field_list: List[str]) -> Field:
optional_keys.append(key)

summary_config = None
if ecl_base:
if ecl_base and optional_keys:
summary_config = SummaryConfig(
name="summary",
input_file=ecl_base,
Expand Down
12 changes: 11 additions & 1 deletion src/ert/config/observations.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,20 @@ def _handle_history_observation(
history_type: Optional[HistorySource],
time_len: int,
) -> Dict[str, ObsVector]:
if "summary" not in ensemble_config:
raise ObservationConfigError(
"The SUMMARY keyword is required to use HISTORY_OBSERVATION"
)
response_config = ensemble_config["summary"]
assert isinstance(response_config, SummaryConfig)

refcase = ensemble_config.refcase
if refcase is None:
raise ObservationConfigError("REFCASE is required for HISTORY_OBSERVATION")
if history_type is None:
raise ValueError("Need a history type in order to use history observations")
raise ObservationConfigError(
"Need a history type in order to use history observations"
)

if summary_key not in response_config.keys:
response_config.keys.append(summary_key)
Expand Down Expand Up @@ -274,6 +280,10 @@ def _handle_summary_observation(
time_map: List[datetime],
) -> Dict[str, ObsVector]:
summary_key = summary_dict["KEY"]
if "summary" not in ensemble_config:
raise ObservationConfigError(
"The SUMMARY keyword is required to use SUMMARY_OBSERVATION"
)
summary_config = ensemble_config["summary"]
assert isinstance(summary_config, SummaryConfig)
if summary_key not in summary_config.keys:
Expand Down
1 change: 1 addition & 0 deletions src/ert/config/parsing/config_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ def summary_keyword() -> SchemaItem:
kw=ConfigKeys.SUMMARY,
required_set=False,
argc_max=None,
argc_min=1,
required_children=[ConfigKeys.ECLBASE],
multi_occurrence=True,
)
Expand Down
2 changes: 2 additions & 0 deletions src/ert/config/summary_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class SummaryConfig(ResponseConfig):
def __post_init__(self) -> None:
if isinstance(self.refcase, list):
self.refcase = {datetime.fromisoformat(val) for val in self.refcase}
if not self.keys:
raise ValueError("SummaryConfig must be given at least one key")

def read_from_file(self, run_path: str, iens: int) -> xr.Dataset:
filename = self.input_file.replace("<IENS>", str(iens))
Expand Down
11 changes: 10 additions & 1 deletion tests/unit_tests/config/config_dict_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ class ErtConfigValues:
license_path: str
random_seed: int
setenv: List[Tuple[str, str]]
summary: Optional[List[str]]
observations: List[Observation]
refcase_smspec: Smspec
refcase_unsmry: Unsmry
Expand Down Expand Up @@ -298,6 +299,8 @@ def to_config_dict(self, config_file, cwd, all_defines=True):
}
if self.eclbase is not None:
result[ConfigKeys.ECLBASE] = self.eclbase
if self.summary is not None:
result[ConfigKeys.SUMMARY] = self.summary
if self.jobname is not None:
result[ConfigKeys.JOBNAME] = self.jobname
return result
Expand Down Expand Up @@ -387,7 +390,7 @@ def ert_config_values(draw, use_eclbase=booleans):
need_eclbase = any(
(isinstance(val, (HistoryObservation, SummaryObservation)) for val in obs)
)
use_eclbase = draw(use_eclbase) if not need_eclbase else st.just(True)
use_eclbase = draw(use_eclbase) or need_eclbase
dates = _observation_dates(obs, first_date)
time_diffs = [d - first_date for d in dates]
time_diff_floats = [diff.total_seconds() / (3600 * 24) for diff in time_diffs]
Expand Down Expand Up @@ -478,6 +481,7 @@ def ert_config_values(draw, use_eclbase=booleans):
refcase_unsmry=st.just(unsmry),
egrid=egrids,
datetimes=st.just(dates),
summary=st.just(["*"]) if use_eclbase else st.just(None),
)
)

Expand Down Expand Up @@ -656,6 +660,11 @@ def to_config_file(filename, config_values):
if keyword in tuple_value_keywords:
for tuple_key, tuple_value in keyword_value:
config.write(f"{keyword} {tuple_key} {tuple_value}\n")
elif keyword == ConfigKeys.SUMMARY:
config.write(keyword + " ")
for value in keyword_value:
config.write(value + " ")
config.write("\n")
elif keyword == ConfigKeys.SIMULATION_JOB:
for job_config in keyword_value:
job_name = job_config[0]
Expand Down
1 change: 1 addition & 0 deletions tests/unit_tests/config/test_observations.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def test_that_correct_key_observation_is_loaded(extra_config, expected):
ECLBASE my_case%d
REFCASE MY_REFCASE
OBS_CONFIG observations_config
SUMMARY *
"""
)
Path("observations_config").write_text(
Expand Down
21 changes: 0 additions & 21 deletions tests/unit_tests/config/test_summary_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,6 @@
from .summary_generator import summaries


def test_summary_config():
summary_config = SummaryConfig(name="ALT1", input_file="ECLBASE", keys=[])
assert summary_config.name == "ALT1"


def test_summary_config_equal():
summary_config = SummaryConfig(name="n", input_file="file", keys=[])

new_summary_config = SummaryConfig(name="n", input_file="file", keys=[])
assert summary_config == new_summary_config

new_summary_config = SummaryConfig(name="different", input_file="file", keys=[])
assert summary_config != new_summary_config

new_summary_config = SummaryConfig(name="n", input_file="different", keys=[])
assert summary_config != new_summary_config

new_summary_config = SummaryConfig(name="n", input_file="file", keys=["some_key"])
assert summary_config != new_summary_config


def test_bad_user_config_file_error_message(tmp_path):
(tmp_path / "test.ert").write_text("NUM_REALIZATIONS 10\n SUMMARY FOPR")
with pytest.raises(
Expand Down
1 change: 1 addition & 0 deletions tests/unit_tests/test_libres_facade.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ def test_get_observations(tmpdir):
ECLBASE ECLIPSE_CASE
REFCASE ECLIPSE_CASE
SUMMARY FOPR
OBS_CONFIG observations
"""
)
Expand Down

0 comments on commit 0831be9

Please sign in to comment.