Skip to content

Commit

Permalink
Add extra validation when reading dessign matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
xjules committed Nov 27, 2024
1 parent 59893c9 commit 6feef60
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 31 deletions.
31 changes: 17 additions & 14 deletions src/ert/config/design_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,23 +129,26 @@ 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.
param_names = (
pd.read_excel(
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(
self.xls_filename,
sheet_name=self.design_sheet,
nrows=1,
self.design_sheet,
header=None,
dtype="string",
skiprows=1,
)
.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,
)
except (ValueError, AttributeError) as exc:
raise ValueError(f"Error reading design matrix: {exc}") from exc
design_matrix_df.columns = param_names.to_list()

if "REAL" in design_matrix_df.columns:
Expand Down
40 changes: 23 additions & 17 deletions src/ert/gui/simulation/ensemble_experiment_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,31 @@ def __init__(

design_matrix = analysis_config.design_matrix
if design_matrix is not None:
design_matrix_err: str = ""
if design_matrix.design_matrix_df is None:
design_matrix.read_design_matrix()

if design_matrix.active_realizations:
self._active_realizations_field.setText(
ActiveRange(design_matrix.active_realizations).rangestring
try:
design_matrix.read_design_matrix()
except (ValueError, AttributeError) as exc:
design_matrix_err = str(exc)
if design_matrix_err:
layout.addRow("Design Matrix", QLabel(design_matrix_err))
else:
if design_matrix.active_realizations:
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)

button_layout = QHBoxLayout()
button_layout.addWidget(show_dm_param_button)
button_layout.addStretch() # Add stretch to push the button to the left

layout.addRow("Design Matrix", button_layout)
show_dm_param_button.clicked.connect(
lambda: self.on_show_dm_params_clicked(design_matrix)
)
show_dm_param_button = QPushButton("Show parameters")
show_dm_param_button.setObjectName("show-dm-parameters")
show_dm_param_button.setMinimumWidth(50)

button_layout = QHBoxLayout()
button_layout.addWidget(show_dm_param_button)
button_layout.addStretch() # Add stretch to push the button to the left

layout.addRow("Design Matrix", button_layout)
show_dm_param_button.clicked.connect(
lambda: self.on_show_dm_params_clicked(design_matrix)
)

self.setLayout(layout)

Expand Down

0 comments on commit 6feef60

Please sign in to comment.