Skip to content

Commit

Permalink
Make DesignMatrix attributes not Optional
Browse files Browse the repository at this point in the history
  • Loading branch information
xjules committed Nov 29, 2024
1 parent ab7b616 commit 58bbfd7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 41 deletions.
28 changes: 11 additions & 17 deletions src/ert/config/design_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ class DesignMatrix:
default_sheet: str

def __post_init__(self) -> None:
self.num_realizations: Optional[int] = None
self.active_realizations: Optional[List[bool]] = None
self.design_matrix_df: Optional[pd.DataFrame] = None
self.parameter_configuration: Optional[Dict[str, ParameterConfig]] = None

try:
self.read_design_matrix()
(
self.active_realizations,
self.design_matrix_df,
self.parameter_configuration,
) = self.read_design_matrix()
except (ValueError, AttributeError) as exc:
raise ConfigValidationError.with_context(
f"Error reading design matrix {self.xls_filename}: {exc}",
Expand Down Expand Up @@ -98,11 +97,6 @@ def merge_with_existing_parameters(
tuple[List[ParameterConfig], ParameterConfig]: List of existing parameters and the dedicated design matrix group
"""

if self.parameter_configuration is None or not isinstance(
self.parameter_configuration[DESIGN_MATRIX_GROUP], GenKwConfig
):
return existing_parameters, None

new_param_config: List[ParameterConfig] = []

design_parameter_group = self.parameter_configuration[DESIGN_MATRIX_GROUP]
Expand Down Expand Up @@ -141,7 +135,7 @@ def merge_with_existing_parameters(

def read_design_matrix(
self,
) -> None:
) -> tuple[List[bool], pd.DataFrame, Dict[str, ParameterConfig]]:
# 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.
Expand Down Expand Up @@ -205,11 +199,11 @@ def read_design_matrix(
[[DESIGN_MATRIX_GROUP], design_matrix_df.columns]
)
reals = design_matrix_df.index.tolist()
self.num_realizations = len(reals)
self.active_realizations = [x in reals for x in range(max(reals) + 1)]

self.design_matrix_df = design_matrix_df
self.parameter_configuration = parameter_configuration
return (
[x in reals for x in range(max(reals) + 1)],
design_matrix_df,
parameter_configuration,
)

@staticmethod
def _read_excel(
Expand Down
29 changes: 11 additions & 18 deletions src/ert/gui/simulation/ensemble_experiment_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,9 @@ def __init__(

design_matrix = analysis_config.design_matrix
if design_matrix is not None:
if design_matrix.active_realizations:
self._active_realizations_field.setText(
ActiveRange(design_matrix.active_realizations).rangestring
)
self._active_realizations_field.setText(
ActiveRange(design_matrix.active_realizations).rangestring
)
show_dm_param_button = QPushButton("Show parameters")
show_dm_param_button.setObjectName("show-dm-parameters")
show_dm_param_button.setMinimumWidth(50)
Expand Down Expand Up @@ -117,20 +116,14 @@ def __init__(
self.notifier.ertChanged.connect(self._update_experiment_name_placeholder)

def on_show_dm_params_clicked(self, design_matrix: DesignMatrix) -> None:
assert design_matrix is not None

if (
design_matrix.design_matrix_df is not None
and not design_matrix.design_matrix_df.empty
):
viewer = DesignMatrixPanel(
design_matrix.design_matrix_df,
design_matrix.xls_filename.name,
)
viewer.setMinimumHeight(500)
viewer.setMinimumWidth(1000)
viewer.adjustSize()
viewer.exec_()
viewer = DesignMatrixPanel(
design_matrix.design_matrix_df,
design_matrix.xls_filename.name,
)
viewer.setMinimumHeight(500)
viewer.setMinimumWidth(1000)
viewer.adjustSize()
viewer.exec_()

@Slot(ExperimentConfigPanel)
def experimentTypeChanged(self, w: ExperimentConfigPanel) -> None:
Expand Down
6 changes: 1 addition & 5 deletions src/ert/run_models/ensemble_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,7 @@ def run_experiment(
random_seed=self.random_seed,
)

if (
design_matrix_group is not None
and design_matrix is not None
and design_matrix.design_matrix_df is not None
):
if design_matrix_group is not None and design_matrix is not None:
save_design_matrix_to_ensemble(
design_matrix.design_matrix_df,
self.ensemble,
Expand Down
1 change: 0 additions & 1 deletion tests/ert/unit_tests/test_libres_facade.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,6 @@ def test_save_parameters_to_storage_from_design_dataframe(
xl_write, index=False, sheet_name="DefaultValues", header=False
)
design_matrix = DesignMatrix(design_path, "DesignSheet01", "DefaultValues")
design_matrix.read_design_matrix()
with open_storage(tmp_path / "storage", mode="w") as storage:
experiment_id = storage.create_experiment(
parameters=[design_matrix.parameter_configuration[DESIGN_MATRIX_GROUP]]
Expand Down

0 comments on commit 58bbfd7

Please sign in to comment.