From d86a33367b4d6e91bfdb91693675710ef78956a5 Mon Sep 17 00:00:00 2001 From: xjules Date: Thu, 28 Nov 2024 13:18:30 +0100 Subject: [PATCH] Set active realizations on startup --- src/ert/config/design_matrix.py | 31 ++++++++++------------- src/ert/run_models/ensemble_experiment.py | 2 -- src/ert/run_models/model_factory.py | 17 +++++++++---- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/ert/config/design_matrix.py b/src/ert/config/design_matrix.py index 52bc354382e..7a17f8f3919 100644 --- a/src/ert/config/design_matrix.py +++ b/src/ert/config/design_matrix.py @@ -141,26 +141,23 @@ def read_design_matrix( # Read the parameter names (first row) as strings to prevent pandas from modifying them. # This ensures that duplicate or empty column names are preserved exactly as they appear in the Excel sheet. # By doing this, we can properly validate variable names, including detecting duplicates or missing names. - try: - param_names = ( - pd.read_excel( - self.xls_filename, - sheet_name=self.design_sheet, - nrows=1, - header=None, - dtype="string", - ) - .iloc[0] - .apply(lambda x: x.strip() if isinstance(x, str) else x) - ) - design_matrix_df = DesignMatrix._read_excel( + param_names = ( + pd.read_excel( self.xls_filename, - self.design_sheet, + sheet_name=self.design_sheet, + nrows=1, header=None, - skiprows=1, + dtype="string", ) - except (ValueError, AttributeError) as exc: - raise ValueError(f"Error reading design matrix: {exc}") from exc + .iloc[0] + .apply(lambda x: x.strip() if isinstance(x, str) else x) + ) + design_matrix_df = DesignMatrix._read_excel( + self.xls_filename, + self.design_sheet, + header=None, + skiprows=1, + ) design_matrix_df.columns = param_names.to_list() if "REAL" in design_matrix_df.columns: diff --git a/src/ert/run_models/ensemble_experiment.py b/src/ert/run_models/ensemble_experiment.py index 1b51125bc24..2372da969eb 100644 --- a/src/ert/run_models/ensemble_experiment.py +++ b/src/ert/run_models/ensemble_experiment.py @@ -77,8 +77,6 @@ def run_experiment( except ConfigValidationError as exc: raise ErtRunError(str(exc)) from exc - assert design_matrix.active_realizations is not None - self.active_realizations = design_matrix.active_realizations if not restart: self.experiment = self._storage.create_experiment( name=self.experiment_name, diff --git a/src/ert/run_models/model_factory.py b/src/ert/run_models/model_factory.py index 68cb4a75d56..f0688309201 100644 --- a/src/ert/run_models/model_factory.py +++ b/src/ert/run_models/model_factory.py @@ -117,13 +117,20 @@ def _setup_ensemble_experiment( args: Namespace, status_queue: SimpleQueue[StatusEvents], ) -> EnsembleExperiment: - active_realizations = _realizations(args, config.model_config.num_realizations) + active_realizations = _realizations( + args, config.model_config.num_realizations + ).tolist() + if ( + config.analysis_config.design_matrix is not None + and config.analysis_config.design_matrix.active_realizations is not None + ): + active_realizations = config.analysis_config.design_matrix.active_realizations experiment_name = args.experiment_name assert experiment_name is not None return EnsembleExperiment( random_seed=config.random_seed, - active_realizations=active_realizations.tolist(), + active_realizations=active_realizations, ensemble_name=args.current_ensemble, minimum_required_realizations=config.analysis_config.minimum_required_realizations, experiment_name=experiment_name, @@ -271,9 +278,9 @@ def _setup_iterative_ensemble_smoother( random_seed=config.random_seed, active_realizations=active_realizations.tolist(), target_ensemble=_iterative_ensemble_format(args), - number_of_iterations=int(args.num_iterations) - if args.num_iterations is not None - else 4, + number_of_iterations=( + int(args.num_iterations) if args.num_iterations is not None else 4 + ), minimum_required_realizations=config.analysis_config.minimum_required_realizations, num_retries_per_iter=4, experiment_name=experiment_name,