diff --git a/src/ert/config/analysis_config.py b/src/ert/config/analysis_config.py index 870c3676edb..9dd37fb59fc 100644 --- a/src/ert/config/analysis_config.py +++ b/src/ert/config/analysis_config.py @@ -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, diff --git a/src/ert/config/design_matrix.py b/src/ert/config/design_matrix.py index da34bf37ca6..4d38933c2a6 100644 --- a/src/ert/config/design_matrix.py +++ b/src/ert/config/design_matrix.py @@ -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( @@ -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]: diff --git a/tests/ert/unit_tests/sensitivity_analysis/test_design_matrix.py b/tests/ert/unit_tests/sensitivity_analysis/test_design_matrix.py index 3ed8c7309e2..b4992196ac7 100644 --- a/tests/ert/unit_tests/sensitivity_analysis/test_design_matrix.py +++ b/tests/ert/unit_tests/sensitivity_analysis/test_design_matrix.py @@ -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", [