|
10 | 10 | from textwrap import dedent
|
11 | 11 | from unittest.mock import MagicMock
|
12 | 12 |
|
| 13 | +import pandas as pd |
13 | 14 | import pytest
|
14 | 15 | from hypothesis import HealthCheck, assume, given, settings
|
15 | 16 | from hypothesis import strategies as st
|
16 | 17 | from pydantic import RootModel, TypeAdapter
|
17 | 18 |
|
18 | 19 | from ert.config import ConfigValidationError, ErtConfig, HookRuntime
|
19 | 20 | from ert.config.ert_config import _split_string_into_sections, create_forward_model_json
|
| 21 | +from ert.config.forward_model_step import ForwardModelStep |
20 | 22 | from ert.config.parsing import ConfigKeys, ConfigWarning
|
21 | 23 | from ert.config.parsing.context_values import (
|
22 | 24 | ContextBool,
|
|
29 | 31 | from ert.config.parsing.queue_system import QueueSystem
|
30 | 32 | from ert.plugins import ErtPluginManager
|
31 | 33 | from ert.shared import ert_share_path
|
| 34 | +from tests.ert.ui_tests.cli.analysis.test_design_matrix import _create_design_matrix |
32 | 35 |
|
33 | 36 | from .config_dict_generator import config_generators
|
34 | 37 |
|
@@ -1991,3 +1994,75 @@ def run(self, *args):
|
1991 | 1994 |
|
1992 | 1995 | assert ert_config.substitutions["<FOO>"] == "ertconfig_foo"
|
1993 | 1996 | assert ert_config.substitutions["<FOO2>"] == "ertconfig_foo2"
|
| 1997 | + |
| 1998 | + |
| 1999 | +def test_design2params_also_validates_design_matrix(tmp_path, caplog, monkeypatch): |
| 2000 | + design_matrix_file = tmp_path / "my_design_matrix.xlsx" |
| 2001 | + _create_design_matrix( |
| 2002 | + design_matrix_file, |
| 2003 | + pd.DataFrame( |
| 2004 | + { |
| 2005 | + "REAL": ["not_a_valid_real"], |
| 2006 | + "a": [1], |
| 2007 | + "category": ["cat1"], |
| 2008 | + } |
| 2009 | + ), |
| 2010 | + pd.DataFrame([["b", 1], ["c", 2]]), |
| 2011 | + ) |
| 2012 | + mock_design2params = ForwardModelStep( |
| 2013 | + "DESIGN2PARAMS", |
| 2014 | + "/usr/bin/env", |
| 2015 | + arglist=["<IENS>", "<xls_filename>", "<designsheet>", "<defaultssheet>"], |
| 2016 | + ) |
| 2017 | + monkeypatch.setattr( |
| 2018 | + ErtConfig, |
| 2019 | + "PREINSTALLED_FORWARD_MODEL_STEPS", |
| 2020 | + {"DESIGN2PARAMS": mock_design2params}, |
| 2021 | + ) |
| 2022 | + ErtConfig.from_file_contents( |
| 2023 | + f"NUM_REALIZATIONS 1\nFORWARD_MODEL DESIGN2PARAMS(<xls_filename>={design_matrix_file}, <designsheet>=DesignSheet01,<defaultssheet>=DefaultSheet)" |
| 2024 | + ) |
| 2025 | + assert "DESIGN_MATRIX validation of DESIGN2PARAMS" in caplog.text |
| 2026 | + |
| 2027 | + |
| 2028 | +def test_multiple_design2params_also_validates_design_matrix_merging( |
| 2029 | + tmp_path, caplog, monkeypatch |
| 2030 | +): |
| 2031 | + design_matrix_file = tmp_path / "my_design_matrix.xlsx" |
| 2032 | + design_matrix_file2 = tmp_path / "my_design_matrix2.xlsx" |
| 2033 | + _create_design_matrix( |
| 2034 | + design_matrix_file, |
| 2035 | + pd.DataFrame( |
| 2036 | + { |
| 2037 | + "REAL": [0, 1], |
| 2038 | + "letters": ["x", "y"], |
| 2039 | + } |
| 2040 | + ), |
| 2041 | + pd.DataFrame([["a", 1], ["c", 2]]), |
| 2042 | + ) |
| 2043 | + _create_design_matrix( |
| 2044 | + design_matrix_file2, |
| 2045 | + pd.DataFrame({"REAL": [1, 2], "numbers": [99, 98]}), |
| 2046 | + pd.DataFrame(), |
| 2047 | + ) |
| 2048 | + mock_design2params = ForwardModelStep( |
| 2049 | + "DESIGN2PARAMS", |
| 2050 | + "/usr/bin/env", |
| 2051 | + arglist=["<IENS>", "<xls_filename>", "<designsheet>", "<defaultssheet>"], |
| 2052 | + ) |
| 2053 | + monkeypatch.setattr( |
| 2054 | + ErtConfig, |
| 2055 | + "PREINSTALLED_FORWARD_MODEL_STEPS", |
| 2056 | + {"DESIGN2PARAMS": mock_design2params}, |
| 2057 | + ) |
| 2058 | + ErtConfig.from_file_contents( |
| 2059 | + f"""\ |
| 2060 | + NUM_REALIZATIONS 1 |
| 2061 | + FORWARD_MODEL DESIGN2PARAMS(<xls_filename>={design_matrix_file}, <designsheet>=DesignSheet01,<defaultssheet>=DefaultSheet) |
| 2062 | + FORWARD_MODEL DESIGN2PARAMS(<xls_filename>={design_matrix_file2}, <designsheet>=DesignSheet01,<defaultssheet>=DefaultSheet) |
| 2063 | + """ |
| 2064 | + ) |
| 2065 | + assert ( |
| 2066 | + "Design matrix merging would have failed due to: Design Matrices don't have the same active realizations" |
| 2067 | + in caplog.text |
| 2068 | + ) |
0 commit comments