diff --git a/src/ert/config/design_matrix.py b/src/ert/config/design_matrix.py index 4a14ed0439c..da34bf37ca6 100644 --- a/src/ert/config/design_matrix.py +++ b/src/ert/config/design_matrix.py @@ -78,6 +78,40 @@ def from_config_list(cls, config_list: list[str]) -> DesignMatrix: default_sheet=default_sheet, ) + def merge_with_other(self, dm_other: DesignMatrix) -> DesignMatrix: + errors = [] + if self.active_realizations != dm_other.active_realizations: + errors.append( + ErrorInfo("Design Matrices don't have the same active realizations!") + ) + + common_keys = set(self.design_matrix_df.columns) & set( + dm_other.design_matrix_df.columns + ) + if common_keys: + errors.append( + ErrorInfo(f"Design Matrices do not have unique keys {common_keys}!") + ) + + try: + self.design_matrix_df = pd.concat( + [self.design_matrix_df, dm_other.design_matrix_df], axis=1 + ) + except ValueError as exc: + errors.append(ErrorInfo(f"Error when merging design matrices {exc}!")) + + pc_other = dm_other.parameter_configuration[DESIGN_MATRIX_GROUP] + pc_self = self.parameter_configuration[DESIGN_MATRIX_GROUP] + assert isinstance(pc_other, GenKwConfig) + assert isinstance(pc_self, GenKwConfig) + for tfd in pc_other.transform_function_definitions: + pc_self.transform_function_definitions.append(tfd) + + if errors: + raise ConfigValidationError.from_collected(errors) + + return self + def merge_with_existing_parameters( self, existing_parameters: list[ParameterConfig] ) -> tuple[list[ParameterConfig], ParameterConfig | None]: