Skip to content

Commit 0d364f6

Browse files
committed
Validate DESIGN_MATRIX when running DESIGN2PARAMS
This commit makes us run validation for DESIGN_MATRIX when parsing configs using DESIGN2PARAMS, and log a warning if it would have failed.
1 parent 6d86949 commit 0d364f6

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

src/ert/config/design_matrix.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def __post_init__(self) -> None:
3333
self.active_realizations,
3434
self.design_matrix_df,
3535
self.parameter_configuration,
36-
) = self.read_design_matrix()
36+
) = self.read_and_validate_design_matrix()
3737
except (ValueError, AttributeError) as exc:
3838
raise ConfigValidationError.with_context(
3939
f"Error reading design matrix {self.xls_filename}: {exc}",
@@ -157,7 +157,7 @@ def merge_with_existing_parameters(
157157
new_param_config += [parameter_group]
158158
return new_param_config, design_parameter_group
159159

160-
def read_design_matrix(
160+
def read_and_validate_design_matrix(
161161
self,
162162
) -> tuple[list[bool], pd.DataFrame, GenKwConfig]:
163163
# Read the parameter names (first row) as strings to prevent pandas from modifying them.

src/ert/config/ert_config.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from pydantic import field_validator
2424
from pydantic.dataclasses import dataclass, rebuild_dataclass
2525

26+
from ert.config.design_matrix import DesignMatrix
2627
from ert.config.parsing.context_values import ContextBoolEncoder
2728
from ert.plugins import ErtPluginManager
2829
from ert.plugins.workflow_config import ErtScriptWorkflow
@@ -459,7 +460,7 @@ def create_list_of_forward_model_steps_to_run(
459460
env_pr_fm_step: dict[str, dict[str, Any]],
460461
) -> list[ForwardModelStep]:
461462
errors = []
462-
fm_steps = []
463+
fm_steps: list[ForwardModelStep] = []
463464

464465
env_vars = {}
465466
for key, val in config_dict.get("SETENV", []):
@@ -511,6 +512,12 @@ def create_list_of_forward_model_steps_to_run(
511512
fm_steps.append(fm_step)
512513

513514
for fm_step in fm_steps:
515+
if fm_step.name == "DESIGN2PARAMS":
516+
xls_filename = fm_step.private_args.get("<xls_filename>")
517+
designsheet = fm_step.private_args.get("<designsheet>")
518+
defaultsheet = fm_step.private_args.get("<defaultssheet>")
519+
validate_ert_design_matrix(xls_filename, designsheet, defaultsheet)
520+
514521
if fm_step.name in preinstalled_forward_model_steps:
515522
try:
516523
substituted_json = create_forward_model_json(
@@ -1253,5 +1260,14 @@ def _forward_model_step_from_config_file(
12531260
)
12541261

12551262

1263+
def validate_ert_design_matrix(xlsfilename, designsheetname, defaultssheetname):
1264+
try:
1265+
DesignMatrix(xlsfilename, designsheetname, defaultssheetname)
1266+
except ConfigValidationError as err:
1267+
logger.warning(
1268+
f"DESIGN_MATRIX validation of DESIGN2PARAMS would have failed with: {err!s}"
1269+
)
1270+
1271+
12561272
# Due to circular dependency in type annotations between ErtConfig -> WorkflowJob -> ErtScript -> ErtConfig
12571273
rebuild_dataclass(ErtConfig)

tests/ert/unit_tests/config/test_ert_config.py

+22
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from textwrap import dedent
1111
from unittest.mock import MagicMock
1212

13+
import pandas as pd
1314
import pytest
1415
from hypothesis import HealthCheck, assume, given, settings
1516
from hypothesis import strategies as st
@@ -29,6 +30,7 @@
2930
from ert.config.parsing.queue_system import QueueSystem
3031
from ert.plugins import ErtPluginManager
3132
from ert.shared import ert_share_path
33+
from tests.ert.ui_tests.cli.analysis.test_design_matrix import _create_design_matrix
3234

3335
from .config_dict_generator import config_generators
3436

@@ -1991,3 +1993,23 @@ def run(self, *args):
19911993

19921994
assert ert_config.substitutions["<FOO>"] == "ertconfig_foo"
19931995
assert ert_config.substitutions["<FOO2>"] == "ertconfig_foo2"
1996+
1997+
1998+
def test_design2params_also_validates_design_matrix(tmp_path, caplog):
1999+
design_matrix_file = tmp_path / "my_design_matrix.xlsx"
2000+
_create_design_matrix(
2001+
design_matrix_file,
2002+
pd.DataFrame(
2003+
{
2004+
"REAL": [1],
2005+
"a": [1],
2006+
"category": ["cat1"],
2007+
}
2008+
),
2009+
pd.DataFrame([["b", 1], ["c", 2]]),
2010+
)
2011+
2012+
ErtConfig.from_file_contents(
2013+
f"NUM_REALIZATIONS 1\nFORWARD_MODEL DESIGN2PARAMS(<xls_filename>={design_matrix_file}, <designsheet>=DesignSheet01,<defaultsheet>=DefaultSheet)"
2014+
)
2015+
assert "DESIGN_MATRIX validation of DESIGN2PARAMS" in caplog.text

0 commit comments

Comments
 (0)