Skip to content

Commit

Permalink
Add test for merge_with_other
Browse files Browse the repository at this point in the history
  • Loading branch information
xjules committed Dec 19, 2024
1 parent 6c7240e commit 2d94476
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/ert/config/analysis_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def from_dict(cls, config_dict: ConfigDict) -> AnalysisConfig:
if design_matrices:
design_matrix = design_matrices[0]
for dm_other in design_matrices[1:]:
design_matrix = design_matrix.merge_with_other(dm_other)
design_matrix.merge_with_other(dm_other)
config = cls(
max_runtime=config_dict.get(ConfigKeys.MAX_RUNTIME),
minimum_required_realizations=min_realization,
Expand Down
4 changes: 1 addition & 3 deletions src/ert/config/design_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def from_config_list(cls, config_list: list[str]) -> DesignMatrix:
default_sheet=default_sheet,
)

def merge_with_other(self, dm_other: DesignMatrix) -> DesignMatrix:
def merge_with_other(self, dm_other: DesignMatrix) -> None:
errors = []
if self.active_realizations != dm_other.active_realizations:
errors.append(
Expand Down Expand Up @@ -110,8 +110,6 @@ def merge_with_other(self, dm_other: DesignMatrix) -> DesignMatrix:
if errors:
raise ConfigValidationError.from_collected(errors)

return self

def merge_with_existing_parameters(
self, existing_parameters: list[ParameterConfig]
) -> tuple[list[ParameterConfig], ParameterConfig | None]:
Expand Down
73 changes: 73 additions & 0 deletions tests/ert/unit_tests/sensitivity_analysis/test_design_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,79 @@
from ert.config.gen_kw_config import GenKwConfig, TransformFunctionDefinition


def _create_design_matrix(xls_path, design_matrix_df, default_sheet_df) -> DesignMatrix:
with pd.ExcelWriter(xls_path) as xl_write:
design_matrix_df.to_excel(xl_write, index=False, sheet_name="DesignSheet01")
default_sheet_df.to_excel(
xl_write, index=False, sheet_name="DefaultValues", header=False
)
return DesignMatrix(xls_path, "DesignSheet01", "DefaultValues")


@pytest.mark.parametrize(
"design_sheet_pd, default_sheet_pd, error_msg",
[
pytest.param(
pd.DataFrame(
{
"REAL": [0, 1, 2],
"c": [1, 2, 3],
"d": [0, 2, 0],
}
),
pd.DataFrame([["e", 1]]),
"",
id="ok_merge",
),
pytest.param(
pd.DataFrame(
{
"REAL": [0, 1, 2],
"a": [1, 2, 3],
}
),
pd.DataFrame([["e", 1]]),
"Design Matrices do not have unique keys",
id="not_unique_keys",
),
pytest.param(
pd.DataFrame(
{
"REAL": [0, 1],
"d": [1, 2],
}
),
pd.DataFrame([["e", 1]]),
"Design Matrices don't have the same active realizations!",
id="not_same_acitve_realizations",
),
],
)
def test_merge_multiple_occurrences(
tmp_path, design_sheet_pd, default_sheet_pd, error_msg
):
design_matrix_1 = _create_design_matrix(
tmp_path / "design_matrix_1.xlsx",
pd.DataFrame(
{
"REAL": [0, 1, 2],
"a": [1, 2, 3],
"b": [0, 2, 0],
},
),
pd.DataFrame([["a", 1], ["b", 4]]),
)

design_matrix_2 = _create_design_matrix(
tmp_path / "design_matrix_2.xlsx", design_sheet_pd, default_sheet_pd
)
if error_msg:
with pytest.raises(ValueError, match=error_msg):
design_matrix_1.merge_with_other(design_matrix_2)
else:
design_matrix_1.merge_with_other(design_matrix_2)


@pytest.mark.parametrize(
"parameters, error_msg",
[
Expand Down

0 comments on commit 2d94476

Please sign in to comment.