Skip to content

Commit

Permalink
Move and refactor NotInStorage -> ExperimentValidation
Browse files Browse the repository at this point in the history
  • Loading branch information
larsevj committed Sep 16, 2024
1 parent e1cda91 commit 9dcd437
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 37 deletions.
8 changes: 3 additions & 5 deletions src/ert/gui/ertwidgets/create_experiment_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from ert.gui.ertnotifier import ErtNotifier
from ert.gui.ertwidgets import StringBox, TextModel
from ert.validation.range_string_argument import NotInStorage
from ert.validation.proper_name_argument import ExperimentValidation


class CreateExperimentDialog(QDialog):
Expand All @@ -36,11 +36,9 @@ def __init__(

experiment_label = QLabel("Experiment name:")
self._experiment_edit = StringBox(
TextModel(""), placeholder_text="My experiment", minimum_width=200
)
self._experiment_edit.setValidator(
NotInStorage(notifier.storage, "experiments")
TextModel(""), placeholder_text="My_experiment", minimum_width=200
)
self._experiment_edit.setValidator(ExperimentValidation(notifier.storage))

ensemble_label = QLabel("Ensemble name:")
self._ensemble_edit = QLineEdit()
Expand Down
4 changes: 2 additions & 2 deletions src/ert/gui/simulation/ensemble_experiment_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from ert.mode_definitions import ENSEMBLE_EXPERIMENT_MODE
from ert.run_models import EnsembleExperiment
from ert.validation import RangeStringArgument
from ert.validation.range_string_argument import NotInStorage
from ert.validation.proper_name_argument import ExperimentValidation

from .experiment_config_panel import ExperimentConfigPanel

Expand Down Expand Up @@ -48,7 +48,7 @@ def __init__(self, ensemble_size: int, run_path: str, notifier: ErtNotifier):
self._experiment_name_field.setMinimumWidth(250)
layout.addRow("Experiment name:", self._experiment_name_field)
self._experiment_name_field.setValidator(
NotInStorage(self.notifier.storage, "experiments")
ExperimentValidation(self.notifier.storage)
)

self._ensemble_name_field = StringBox(
Expand Down
4 changes: 2 additions & 2 deletions src/ert/gui/simulation/ensemble_smoother_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from ert.mode_definitions import ENSEMBLE_SMOOTHER_MODE
from ert.run_models import EnsembleSmoother
from ert.validation import ProperNameFormatArgument, RangeStringArgument
from ert.validation.range_string_argument import NotInStorage
from ert.validation.proper_name_argument import ExperimentValidation

from .experiment_config_panel import ExperimentConfigPanel

Expand Down Expand Up @@ -57,7 +57,7 @@ def __init__(
self._experiment_name_field.setMinimumWidth(250)
layout.addRow("Experiment name:", self._experiment_name_field)
self._experiment_name_field.setValidator(
NotInStorage(self.notifier.storage, "experiments")
ExperimentValidation(self.notifier.storage)
)

runpath_label = CopyableLabel(text=run_path)
Expand Down
4 changes: 2 additions & 2 deletions src/ert/gui/simulation/iterated_ensemble_smoother_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from ert.mode_definitions import ITERATIVE_ENSEMBLE_SMOOTHER_MODE
from ert.run_models import IteratedEnsembleSmoother
from ert.validation import ProperNameFormatArgument, RangeStringArgument
from ert.validation.range_string_argument import NotInStorage
from ert.validation.proper_name_argument import ExperimentValidation

from .experiment_config_panel import ExperimentConfigPanel

Expand Down Expand Up @@ -57,7 +57,7 @@ def __init__(
self._experiment_name_field.setMinimumWidth(250)
layout.addRow("Experiment name:", self._experiment_name_field)
self._experiment_name_field.setValidator(
NotInStorage(self.notifier.storage, "experiments")
ExperimentValidation(self.notifier.storage)
)

runpath_label = CopyableLabel(text=run_path)
Expand Down
4 changes: 2 additions & 2 deletions src/ert/gui/simulation/multiple_data_assimilation_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
ProperNameFormatArgument,
RangeStringArgument,
)
from ert.validation.range_string_argument import NotInStorage
from ert.validation.proper_name_argument import ExperimentValidation

from .experiment_config_panel import ExperimentConfigPanel

Expand Down Expand Up @@ -68,7 +68,7 @@ def __init__(
self._experiment_name_field.setMinimumWidth(250)
layout.addRow("Experiment name:", self._experiment_name_field)
self._experiment_name_field.setValidator(
NotInStorage(self.notifier.storage, "experiments")
ExperimentValidation(self.notifier.storage)
)
runpath_label = CopyableLabel(text=run_path)
layout.addRow("Runpath:", runpath_label)
Expand Down
30 changes: 30 additions & 0 deletions src/ert/validation/proper_name_argument.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
from __future__ import annotations

import re
from typing import TYPE_CHECKING

from .argument_definition import ArgumentDefinition
from .validation_status import ValidationStatus

if TYPE_CHECKING:
from ..storage import Storage


class ProperNameArgument(ArgumentDefinition):
NOT_A_VALID_NAME = (
Expand Down Expand Up @@ -36,3 +42,27 @@ def validate(self, token: str) -> ValidationStatus:
validation_status.setValue(token)

return validation_status


class ExperimentValidation(ProperNameArgument):
def __init__(self, storage: Storage) -> None:
self.storage = storage
super().__init__()

def validate(self, token: str) -> ValidationStatus:
validation_status = super().validate(token)

if not validation_status:
return validation_status

existing = [exp.name for exp in self.storage.experiments]

if token in existing:
validation_status.setFailed()
validation_status.addToMessage(
f"Experiment name must be unique, not one of: {existing}"
)
elif not validation_status.failed():
validation_status.setValue(token)

return validation_status
25 changes: 1 addition & 24 deletions src/ert/validation/range_string_argument.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Optional
from typing import Optional

from .active_range import ActiveRange
from .argument_definition import ArgumentDefinition
from .validation_status import ValidationStatus

if TYPE_CHECKING:
from ..storage import Storage


class RangeStringArgument(ArgumentDefinition):
NOT_A_VALID_RANGE_STRING = (
Expand Down Expand Up @@ -46,23 +43,3 @@ def validate(self, token: str) -> ValidationStatus:
validation_status.setValue(token)

return validation_status


class NotInStorage(ArgumentDefinition):
def __init__(self, storage: Storage, prop: str) -> None:
self.storage = storage
self.prop = prop

def validate(self, token: str) -> ValidationStatus:
validation_status = ValidationStatus()

existing = [exp.name for exp in getattr(self.storage, self.prop)]
if token in existing:
validation_status.setFailed()
validation_status.addToMessage(
f"{self.prop.capitalize()} name must be unique, not one of: {existing}"
)

validation_status.setValue(token)

return validation_status

0 comments on commit 9dcd437

Please sign in to comment.