Skip to content

Commit

Permalink
Update relative weights if restart toggled
Browse files Browse the repository at this point in the history
Store relative weights in metadata.json file
  • Loading branch information
andreas-el committed Aug 23, 2024
1 parent ef64736 commit 618c8ff
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 29 deletions.
48 changes: 23 additions & 25 deletions src/ert/gui/ertwidgets/stringbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(
self._validation = ValidationSupport(self)
self._validator: Optional[ArgumentDefinition] = None
self._model = model
self._disable_validation = False
self._enable_validation = True

if placeholder_text:
self.setPlaceholderText(placeholder_text)
Expand All @@ -55,25 +55,26 @@ def __init__(
self.modelChanged()

def validateString(self) -> None:
if self._disable_validation:
return
string_to_validate = str(self.text())
if not string_to_validate and self.placeholderText():
string_to_validate = self.placeholderText()
if self._validator is not None:
status = self._validator.validate(string_to_validate)

palette = QPalette()
if not status:
palette.setColor(self.backgroundRole(), ValidationSupport.ERROR_COLOR)
self.setPalette(palette)
self._validation.setValidationMessage(
str(status), ValidationSupport.EXCLAMATION
)
else:
palette.setColor(self.backgroundRole(), self._valid_color)
self.setPalette(palette)
self._validation.setValidationMessage("")
if self._enable_validation:
string_to_validate = str(self.text())
if not string_to_validate and self.placeholderText():
string_to_validate = self.placeholderText()
if self._validator is not None:
status = self._validator.validate(string_to_validate)

palette = QPalette()
if not status:
palette.setColor(
self.backgroundRole(), ValidationSupport.ERROR_COLOR
)
self.setPalette(palette)
self._validation.setValidationMessage(
str(status), ValidationSupport.EXCLAMATION
)
else:
palette.setColor(self.backgroundRole(), self._valid_color)
self.setPalette(palette)
self._validation.setValidationMessage("")

def emitChange(self, q_string: Any) -> None:
self.textChanged.emit(str(q_string))
Expand Down Expand Up @@ -113,8 +114,5 @@ def isValid(self) -> bool:
def get_text(self) -> str:
return self.text() if self.text() else self.placeholderText()

def enable_validation(self) -> None:
self._disable_validation = False

def disable_validation(self) -> None:
self._disable_validation = True
def enable_validation(self, enabled: bool) -> None:
self._enable_validation = enabled
20 changes: 16 additions & 4 deletions src/ert/gui/simulation/multiple_data_assimilation_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,23 +149,35 @@ def update_experiment_name(self) -> None:
self._ensemble_selector.selected_ensemble.experiment.name
)

self._relative_iteration_weights_box.setText(
self._ensemble_selector.selected_ensemble.relative_weights
or MultipleDataAssimilation.default_weights
)

@Slot(bool)
def update_experiment_edit(self, checked: bool) -> None:
if checked:
self._experiment_name_field.disable_validation()
self._experiment_name_field.setText(
self._ensemble_selector.selected_ensemble.experiment.name
)
self._experiment_name_field.setEnabled(False)
else:
self._experiment_name_field.enable_validation()
self._experiment_name_field.clear()
self._experiment_name_field.setEnabled(True)

self._experiment_name_field.enable_validation(not checked)
self._experiment_name_field.setEnabled(not checked)
self._relative_iteration_weights_box.setEnabled(not checked)

def restart_run_toggled(self) -> None:
self._restart_box.setEnabled(bool(self._ensemble_selector._ensemble_list()))
self._ensemble_selector.setEnabled(self._restart_box.isChecked())

self._relative_iteration_weights_box.setText(
self._ensemble_selector.selected_ensemble.relative_weights
or MultipleDataAssimilation.default_weights
if self._restart_box.isChecked()
else MultipleDataAssimilation.default_weights
)

def _createInputForWeights(self, layout: QFormLayout) -> None:
relative_iteration_weights_model = ValueModel(self.weights)
self._relative_iteration_weights_box = StringBox(
Expand Down
3 changes: 3 additions & 0 deletions src/ert/run_models/multiple_data_assimilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def __init__(
update_settings: UpdateSettings,
status_queue: SimpleQueue[StatusEvents],
):
self._relative_weights = weights
self.weights = self.parse_weights(weights)

self.target_ensemble_format = target_ensemble
Expand Down Expand Up @@ -98,10 +99,12 @@ def run_experiment(
f"Prior ensemble with ID: {id} does not exists"
) from err
else:
sim_args = {"weights": self._relative_weights}
experiment = self._storage.create_experiment(
parameters=self.ert_config.ensemble_config.parameter_configuration,
observations=self.ert_config.observations,
responses=self.ert_config.ensemble_config.response_configuration,
simulation_arguments=sim_args,
name=self.experiment_name,
)

Expand Down
4 changes: 4 additions & 0 deletions src/ert/storage/local_ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ def parent(self) -> Optional[UUID]:
def experiment(self) -> LocalExperiment:
return self._storage.get_experiment(self.experiment_id)

@property
def relative_weights(self) -> str:
return self._storage.get_experiment(self.experiment_id).relative_weights

def get_realization_mask_without_failure(self) -> npt.NDArray[np.bool_]:
"""
Mask array indicating realizations without any failure.
Expand Down
4 changes: 4 additions & 0 deletions src/ert/storage/local_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ def metadata(self) -> Dict[str, Any]:
with open(path, encoding="utf-8", mode="r") as f:
return json.load(f)

@property
def relative_weights(self) -> str:
return self.metadata.get("weights", "")

@property
def name(self) -> str:
return self._index.name
Expand Down

0 comments on commit 618c8ff

Please sign in to comment.