Skip to content

Commit 8fc6417

Browse files
committed
Add test for merge_with_other
1 parent 2a66b8c commit 8fc6417

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed

src/ert/config/analysis_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def from_dict(cls, config_dict: ConfigDict) -> AnalysisConfig:
194194
if design_matrices:
195195
design_matrix = design_matrices[0]
196196
for dm_other in design_matrices[1:]:
197-
design_matrix = design_matrix.merge_with_other(dm_other)
197+
design_matrix.merge_with_other(dm_other)
198198
config = cls(
199199
minimum_required_realizations=min_realization,
200200
update_log_path=config_dict.get(ConfigKeys.UPDATE_LOG_PATH, "update_log"),

src/ert/config/design_matrix.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def from_config_list(cls, config_list: list[str]) -> DesignMatrix:
7878
default_sheet=default_sheet,
7979
)
8080

81-
def merge_with_other(self, dm_other: DesignMatrix) -> DesignMatrix:
81+
def merge_with_other(self, dm_other: DesignMatrix) -> None:
8282
errors = []
8383
if self.active_realizations != dm_other.active_realizations:
8484
errors.append(
@@ -110,8 +110,6 @@ def merge_with_other(self, dm_other: DesignMatrix) -> DesignMatrix:
110110
if errors:
111111
raise ConfigValidationError.from_collected(errors)
112112

113-
return self
114-
115113
def merge_with_existing_parameters(
116114
self, existing_parameters: list[ParameterConfig]
117115
) -> tuple[list[ParameterConfig], ParameterConfig | None]:

tests/ert/unit_tests/sensitivity_analysis/test_design_matrix.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,79 @@
66
from ert.config.gen_kw_config import GenKwConfig, TransformFunctionDefinition
77

88

9+
def _create_design_matrix(xls_path, design_matrix_df, default_sheet_df) -> DesignMatrix:
10+
with pd.ExcelWriter(xls_path) as xl_write:
11+
design_matrix_df.to_excel(xl_write, index=False, sheet_name="DesignSheet01")
12+
default_sheet_df.to_excel(
13+
xl_write, index=False, sheet_name="DefaultValues", header=False
14+
)
15+
return DesignMatrix(xls_path, "DesignSheet01", "DefaultValues")
16+
17+
18+
@pytest.mark.parametrize(
19+
"design_sheet_pd, default_sheet_pd, error_msg",
20+
[
21+
pytest.param(
22+
pd.DataFrame(
23+
{
24+
"REAL": [0, 1, 2],
25+
"c": [1, 2, 3],
26+
"d": [0, 2, 0],
27+
}
28+
),
29+
pd.DataFrame([["e", 1]]),
30+
"",
31+
id="ok_merge",
32+
),
33+
pytest.param(
34+
pd.DataFrame(
35+
{
36+
"REAL": [0, 1, 2],
37+
"a": [1, 2, 3],
38+
}
39+
),
40+
pd.DataFrame([["e", 1]]),
41+
"Design Matrices do not have unique keys",
42+
id="not_unique_keys",
43+
),
44+
pytest.param(
45+
pd.DataFrame(
46+
{
47+
"REAL": [0, 1],
48+
"d": [1, 2],
49+
}
50+
),
51+
pd.DataFrame([["e", 1]]),
52+
"Design Matrices don't have the same active realizations!",
53+
id="not_same_acitve_realizations",
54+
),
55+
],
56+
)
57+
def test_merge_multiple_occurrences(
58+
tmp_path, design_sheet_pd, default_sheet_pd, error_msg
59+
):
60+
design_matrix_1 = _create_design_matrix(
61+
tmp_path / "design_matrix_1.xlsx",
62+
pd.DataFrame(
63+
{
64+
"REAL": [0, 1, 2],
65+
"a": [1, 2, 3],
66+
"b": [0, 2, 0],
67+
},
68+
),
69+
pd.DataFrame([["a", 1], ["b", 4]]),
70+
)
71+
72+
design_matrix_2 = _create_design_matrix(
73+
tmp_path / "design_matrix_2.xlsx", design_sheet_pd, default_sheet_pd
74+
)
75+
if error_msg:
76+
with pytest.raises(ValueError, match=error_msg):
77+
design_matrix_1.merge_with_other(design_matrix_2)
78+
else:
79+
design_matrix_1.merge_with_other(design_matrix_2)
80+
81+
982
@pytest.mark.parametrize(
1083
"parameters, error_msg",
1184
[

0 commit comments

Comments
 (0)