Skip to content

Commit

Permalink
Move experiment creation
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindeide committed Jan 30, 2024
1 parent 0c4a70c commit ccbdc3d
Show file tree
Hide file tree
Showing 16 changed files with 54 additions and 104 deletions.
9 changes: 0 additions & 9 deletions src/ert/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,11 @@ def run_cli(args: Namespace, _: Any = None) -> None:
execute_workflow(ert, storage, args.name)
return

experiment = storage.create_experiment(
parameters=ert_config.ensemble_config.parameter_configuration,
responses=ert_config.ensemble_config.response_configuration,
observations=ert_config.observations,
)

try:
model = create_model(
ert_config,
storage,
args,
experiment.id,
)
except ValueError as e:
raise ErtCliError(e) from e
Expand All @@ -103,8 +96,6 @@ def run_cli(args: Namespace, _: Any = None) -> None:

evaluator_server_config = EvaluatorServerConfig(custom_port_range=args.port_range)

experiment.write_simulation_arguments(model.simulation_arguments)

if model.check_if_runpath_exists():
print(
"Warning: ERT is running in an existing runpath.\n"
Expand Down
28 changes: 7 additions & 21 deletions src/ert/cli/model_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import logging
from typing import TYPE_CHECKING
from uuid import UUID

import numpy as np

Expand Down Expand Up @@ -54,7 +53,6 @@ def create_model(
config: ErtConfig,
storage: StorageAccessor,
args: Namespace,
experiment_id: UUID,
) -> BaseRunModel:
logger = logging.getLogger(__name__)
logger.info(
Expand All @@ -75,28 +73,24 @@ def create_model(
)

if args.mode == TEST_RUN_MODE:
return _setup_single_test_run(config, storage, args, experiment_id)
return _setup_single_test_run(config, storage, args)
elif args.mode == ENSEMBLE_EXPERIMENT_MODE:
return _setup_ensemble_experiment(config, storage, args, experiment_id)
return _setup_ensemble_experiment(config, storage, args)
elif args.mode == ENSEMBLE_SMOOTHER_MODE:
return _setup_ensemble_smoother(
config, storage, args, experiment_id, update_settings
)
return _setup_ensemble_smoother(config, storage, args, update_settings)
elif args.mode == ES_MDA_MODE:
return _setup_multiple_data_assimilation(
config, storage, args, experiment_id, update_settings
)
return _setup_multiple_data_assimilation(config, storage, args, update_settings)
elif args.mode == ITERATIVE_ENSEMBLE_SMOOTHER_MODE:
return _setup_iterative_ensemble_smoother(
config, storage, args, experiment_id, update_settings
config, storage, args, update_settings
)

else:
raise NotImplementedError(f"Run type not supported {args.mode}")


def _setup_single_test_run(
config: ErtConfig, storage: StorageAccessor, args: Namespace, experiment_id: UUID
config: ErtConfig, storage: StorageAccessor, args: Namespace
) -> SingleTestRun:
return SingleTestRun(
SingleTestRunArguments(
Expand All @@ -108,12 +102,11 @@ def _setup_single_test_run(
),
config,
storage,
experiment_id,
)


def _setup_ensemble_experiment(
config: ErtConfig, storage: StorageAccessor, args: Namespace, experiment_id: UUID
config: ErtConfig, storage: StorageAccessor, args: Namespace
) -> EnsembleExperiment:
min_realizations_count = config.analysis_config.minimum_required_realizations
active_realizations = _realizations(args, config.model_config.num_realizations)
Expand All @@ -139,15 +132,13 @@ def _setup_ensemble_experiment(
config,
storage,
config.queue_config,
experiment_id,
)


def _setup_ensemble_smoother(
config: ErtConfig,
storage: StorageAccessor,
args: Namespace,
experiment_id: UUID,
update_settings: UpdateSettings,
) -> EnsembleSmoother:
return EnsembleSmoother(
Expand All @@ -165,7 +156,6 @@ def _setup_ensemble_smoother(
config,
storage,
config.queue_config,
experiment_id,
es_settings=config.analysis_config.es_module,
update_settings=update_settings,
)
Expand All @@ -175,7 +165,6 @@ def _setup_multiple_data_assimilation(
config: ErtConfig,
storage: StorageAccessor,
args: Namespace,
experiment_id: UUID,
update_settings: UpdateSettings,
) -> MultipleDataAssimilation:
# Because the configuration of the CLI is different from the gui, we
Expand Down Expand Up @@ -203,7 +192,6 @@ def _setup_multiple_data_assimilation(
config,
storage,
config.queue_config,
experiment_id,
prior_ensemble,
es_settings=config.analysis_config.es_module,
update_settings=update_settings,
Expand All @@ -214,7 +202,6 @@ def _setup_iterative_ensemble_smoother(
config: ErtConfig,
storage: StorageAccessor,
args: Namespace,
id_: UUID,
update_settings: UpdateSettings,
) -> IteratedEnsembleSmoother:
return IteratedEnsembleSmoother(
Expand All @@ -234,7 +221,6 @@ def _setup_iterative_ensemble_smoother(
config,
storage,
config.queue_config,
id_,
config.analysis_config.ies_module,
update_settings=update_settings,
)
Expand Down
9 changes: 0 additions & 9 deletions src/ert/gui/simulation/simulation_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,20 +182,11 @@ def runSimulation(self):
QApplication.setOverrideCursor(Qt.CursorShape.WaitCursor)
config = self.facade.config
try:
experiment = self._notifier.storage.create_experiment(
parameters=config.ensemble_config.parameter_configuration,
responses=config.ensemble_config.response_configuration,
observations=config.observations,
name=args.experiment_name,
)

model = create_model(
config,
self._notifier.storage,
args,
experiment.id,
)
experiment.write_simulation_arguments(model.simulation_arguments)

except ValueError as e:
QMessageBox.warning(
Expand Down
7 changes: 0 additions & 7 deletions src/ert/run_models/base_run_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ def __init__(
config: ErtConfig,
storage: StorageAccessor,
queue_config: QueueConfig,
experiment_id: uuid.UUID,
phase_count: int = 1,
):
"""
Expand Down Expand Up @@ -120,7 +119,6 @@ def __init__(
self.facade = LibresFacade(self.ert)
self._storage = storage
self._simulation_arguments = simulation_arguments
self._experiment_id = experiment_id
self.reset()
# mapping from iteration number to ensemble id
self._iter_map: Dict[int, str] = {}
Expand Down Expand Up @@ -224,7 +222,6 @@ def _set_default_env_context(self) -> None:
"""
simulation_mode = MODULE_MODE.get(type(self).__name__, "")
self.set_env_key("_ERT_SIMULATION_MODE", simulation_mode)
self.set_env_key("_ERT_EXPERIMENT_ID", str(self._experiment_id))

def _clean_env_context(self) -> None:
"""
Expand Down Expand Up @@ -402,10 +399,6 @@ def _build_ensemble(
builder.add_realization(real)
return builder.set_id(str(uuid.uuid1()).split("-", maxsplit=1)[0]).build()

@property
def id(self) -> uuid.UUID:
return self._experiment_id

@property
def paths(self) -> List[str]:
run_paths = []
Expand Down
12 changes: 8 additions & 4 deletions src/ert/run_models/ensemble_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from pathlib import Path
from typing import TYPE_CHECKING, Union
from uuid import UUID

import numpy as np

Expand Down Expand Up @@ -33,14 +32,12 @@ def __init__(
config: ErtConfig,
storage: StorageAccessor,
queue_config: QueueConfig,
id_: UUID,
):
super().__init__(
simulation_arguments,
config,
storage,
queue_config,
id_,
)

def runSimulations__(
Expand All @@ -53,13 +50,20 @@ def runSimulations__(
try:
ensemble = self._storage.get_ensemble_by_name(current_case)
assert isinstance(ensemble, EnsembleAccessor)
experiment = ensemble.experiment
except KeyError:
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,
)
ensemble = self._storage.create_ensemble(
self._experiment_id,
experiment,
name=current_case,
ensemble_size=self._simulation_arguments.ensemble_size,
iteration=self._simulation_arguments.iter_num,
)
self.set_env_key("_ERT_EXPERIMENT_ID", str(experiment.id))
self.set_env_key("_ERT_ENSEMBLE_ID", str(ensemble.id))

prior_context = RunContext(
Expand Down
14 changes: 8 additions & 6 deletions src/ert/run_models/ensemble_smoother.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import functools
import logging
from typing import TYPE_CHECKING
from uuid import UUID

import numpy as np

Expand Down Expand Up @@ -34,7 +33,6 @@ def __init__(
config: ErtConfig,
storage: StorageAccessor,
queue_config: QueueConfig,
experiment_id: UUID,
es_settings: ESSettings,
update_settings: UpdateSettings,
):
Expand All @@ -43,7 +41,6 @@ def __init__(
config,
storage,
queue_config,
experiment_id,
phase_count=2,
)
self.es_settings = es_settings
Expand All @@ -67,10 +64,15 @@ def run_experiment(
log_msg = "Running ES"
logger.info(log_msg)
self.setPhaseName(log_msg, indeterminate=True)

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,
)
self.set_env_key("_ERT_EXPERIMENT_ID", str(experiment.id))
prior_name = self._simulation_arguments.current_case
prior = self._storage.create_ensemble(
self._experiment_id,
experiment,
ensemble_size=self._simulation_arguments.ensemble_size,
name=prior_name,
)
Expand Down Expand Up @@ -108,7 +110,7 @@ def run_experiment(
target_case_format = self._simulation_arguments.target_case
posterior_context = RunContext(
sim_fs=self._storage.create_ensemble(
self._experiment_id,
experiment,
ensemble_size=prior.ensemble_size,
iteration=1,
name=target_case_format,
Expand Down
14 changes: 9 additions & 5 deletions src/ert/run_models/iterated_ensemble_smoother.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import functools
import logging
from typing import TYPE_CHECKING
from uuid import UUID

import numpy as np
from iterative_ensemble_smoother import steplength_exponential
Expand Down Expand Up @@ -39,7 +38,6 @@ def __init__(
config: ErtConfig,
storage: StorageAccessor,
queue_config: QueueConfig,
experiment_id: UUID,
analysis_config: IESSettings,
update_settings: UpdateSettings,
):
Expand All @@ -48,7 +46,6 @@ def __init__(
config,
storage,
queue_config,
experiment_id,
phase_count=2,
)
self.support_restart = False
Expand Down Expand Up @@ -132,12 +129,19 @@ def run_experiment(
self.setPhaseName(log_msg, indeterminate=True)

target_case_format = self._simulation_arguments.target_case
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,
)
prior = self._storage.create_ensemble(
self._experiment_id,
experiment=experiment,
ensemble_size=self._simulation_arguments.ensemble_size,
name=target_case_format % 0,
)
self.set_env_key("_ERT_ENSEMBLE_ID", str(prior.id))
self.set_env_key("_ERT_EXPERIMENT_ID", str(experiment.id))

initial_mask = np.array(
self._simulation_arguments.active_realizations, dtype=bool
)
Expand Down Expand Up @@ -167,7 +171,7 @@ def run_experiment(
)

posterior = self._storage.create_ensemble(
self._experiment_id,
experiment,
name=target_case_format % current_iter, # noqa
ensemble_size=prior_context.sim_fs.ensemble_size,
iteration=current_iter,
Expand Down
Loading

0 comments on commit ccbdc3d

Please sign in to comment.