Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn when active realizations differ from num realizations #10340

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions src/ert/gui/simulation/experiment_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
QWidget,
)

from ert.config import capture_validation
from ert.gui.ertnotifier import ErtNotifier
from ert.run_models import BaseRunModel, StatusEvents, create_model

Expand Down Expand Up @@ -219,12 +220,26 @@ def run_experiment(self) -> None:
QApplication.setOverrideCursor(Qt.CursorShape.WaitCursor)
event_queue: SimpleQueue[StatusEvents] = SimpleQueue()
try:
model = create_model(
self.config,
self._notifier.storage,
args,
event_queue,
)
with capture_validation() as validation_messages:
model = create_model(
self.config,
self._notifier.storage,
args,
event_queue,
)
if validation_messages.warnings:
QApplication.restoreOverrideCursor()
warn_box = QMessageBox(self)
warn_box.setIcon(QMessageBox.Icon.Warning)
warn_box.setWindowTitle("WARNING:")
warn_box.setObjectName("Warning_box_run_experiment")
warn_box.setInformativeText(
"- " + "\n- ".join(str(e) for e in validation_messages.warnings)
)
warn_box.setStandardButtons(QMessageBox.StandardButton.Ok)
warn_box.setWindowModality(Qt.WindowModality.ApplicationModal)
warn_box.exec()
QApplication.setOverrideCursor(Qt.CursorShape.WaitCursor)

except ValueError as e:
QMessageBox.warning(
Expand Down
10 changes: 10 additions & 0 deletions src/ert/run_models/model_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def _setup_single_test_run(

def validate_minimum_realizations(config: ErtConfig, args: Namespace) -> None:
min_realizations_count = config.analysis_config.minimum_required_realizations
num_realization = config.model_config.num_realizations
active_realizations = _get_active_realizations_list(args, config)
active_realizations_count = int(np.sum(active_realizations))
if active_realizations_count < min_realizations_count:
Expand All @@ -102,6 +103,15 @@ def validate_minimum_realizations(config: ErtConfig, args: Namespace) -> None:
f"({active_realizations_count}) as it is lower than the MIN_REALIZATIONS "
f"({min_realizations_count}) that was specified in the config file."
)
if (
active_realizations_count != num_realization
and config.analysis_config.design_matrix is not None
and config.analysis_config.design_matrix.active_realizations is not None
):
ConfigWarning.warn(
f"The ensemble size is set to {active_realizations_count} instead of "
f"{num_realization} (NUM_REALIZATIONS) due to the 'REAL' entries in the DESIGN_MATRIX."
)


def _setup_ensemble_experiment(
Expand Down
79 changes: 79 additions & 0 deletions tests/ert/unit_tests/gui/simulation/test_run_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
)
from ert.gui.ertnotifier import ErtNotifier
from ert.gui.main import GUILogHandler, _setup_main_window
from ert.gui.main_window import ErtMainWindow
from ert.gui.simulation.ensemble_experiment_panel import EnsembleExperimentPanel
from ert.gui.simulation.experiment_panel import ExperimentPanel
from ert.gui.simulation.run_dialog import RunDialog
Expand Down Expand Up @@ -809,3 +810,81 @@ def qt_bot_click_tab_index(tab_index: int) -> None:

qt_bot_click_tab_index(1)
assert "Realization id 1 in iteration 1" in fm_step_label.text()


def handle_warning_box(
gui: ErtMainWindow,
qtbot: QtBot,
expected_warning: str,
):
wb = gui.findChildren(QMessageBox, "Warning_box_run_experiment")
wb = wb[-1] if wb else None

if wb is not None:
assert wb
assert isinstance(wb, QMessageBox)
message = wb.informativeText()

ok_button = wb.button(QMessageBox.StandardButton.Ok)
assert ok_button is not None

qtbot.mouseClick(ok_button, Qt.MouseButton.LeftButton)

assert message == expected_warning


@pytest.mark.integration_test
@pytest.mark.usefixtures("use_tmpdir")
def test_warning_when_active_realizations_differ_from_num_realizations(
qtbot: QtBot, storage
):
xls_filename = "design_matrix.xlsx"
design_matrix_df = pd.DataFrame(
{
"REAL": [0, 5, 7],
"a": [0, 1, 2],
}
)
default_sheet_df = pd.DataFrame([["b", 1], ["c", 2]])
with pd.ExcelWriter(xls_filename) as xl_write:
design_matrix_df.to_excel(xl_write, index=False, sheet_name="DesignSheet")
default_sheet_df.to_excel(
xl_write, index=False, sheet_name="DefaultSheet", header=False
)

config_file = "minimal_config.ert"
with open(config_file, "w", encoding="utf-8") as f:
f.write("NUM_REALIZATIONS 1")
f.write(
f"\nDESIGN_MATRIX {xls_filename} DESIGN_SHEET:DesignSheet DEFAULT_SHEET:DefaultSheet"
)

args_mock = Mock()
args_mock.config = config_file

ert_config = ErtConfig.from_file(config_file)
gui = _setup_main_window(ert_config, args_mock, GUILogHandler(), storage)
experiment_panel = gui.findChild(ExperimentPanel)
assert experiment_panel

simulation_mode_combo = experiment_panel.findChild(QComboBox)
assert simulation_mode_combo

simulation_mode_combo.setCurrentText(EnsembleExperiment.name())
run_experiment = experiment_panel.findChild(QToolButton, name="run_experiment")
assert run_experiment

QTimer.singleShot(
1000,
lambda: handle_warning_box(
gui,
qtbot,
expected_warning="- The ensemble size is set to 3 instead of 1 (NUM_REALIZATIONS) due to the 'REAL' entries in the DESIGN_MATRIX.",
),
)
qtbot.mouseClick(run_experiment, Qt.MouseButton.LeftButton)

qtbot.waitUntil(lambda: gui.findChild(RunDialog) is not None, timeout=5000)
run_dialog = gui.findChild(RunDialog)

qtbot.waitUntil(lambda: run_dialog.is_simulation_done() == True, timeout=10000)