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

Add default values using Pandas assign in design_matrix #8949

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
21 changes: 13 additions & 8 deletions src/ert/config/design_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,10 @@ def read_design_matrix(
error_msg = "\n".join(error_list)
raise ValueError(f"Design matrix is not valid, error:\n{error_msg}")

defaults = DesignMatrix._read_defaultssheet(
self.xls_filename, self.default_sheet
defaults_to_use = DesignMatrix._read_defaultssheet(
self.xls_filename, self.default_sheet, design_matrix_df.columns.to_list()
)
for k, v in defaults.items():
if k not in design_matrix_df.columns:
design_matrix_df[k] = v
design_matrix_df = design_matrix_df.assign(**defaults_to_use)

parameter_configuration: Dict[str, ParameterConfig] = {}
transform_function_definitions: List[TransformFunctionDefinition] = []
Expand Down Expand Up @@ -197,11 +195,14 @@ def _validate_design_matrix(design_matrix: pd.DataFrame) -> List[str]:

@staticmethod
def _read_defaultssheet(
xls_filename: Union[Path, str], defaults_sheetname: str
xls_filename: Union[Path, str],
defaults_sheetname: str,
existing_parameters: List[str],
) -> Dict[str, Union[str, float]]:
"""
Construct a dict of keys and values to be used as defaults from the
first two columns in a spreadsheet.
first two columns in a spreadsheet. Only returns the keys that are
different from the exisiting parameters.

Returns a dict of default values

Expand All @@ -227,7 +228,11 @@ def _read_defaultssheet(
if not default_df[0].is_unique:
raise ValueError("Default sheet contains duplicate parameter names")

return {row[0]: convert_to_numeric(row[1]) for _, row in default_df.iterrows()}
return {
row[0]: convert_to_numeric(row[1])
for _, row in default_df.iterrows()
if row[0] not in existing_parameters
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we write a test for this?

}


def convert_to_numeric(x: str) -> Union[str, float]:
Expand Down