Skip to content

Commit

Permalink
Remove SEBA from everest
Browse files Browse the repository at this point in the history
  • Loading branch information
yngve-sk committed Jan 10, 2025
1 parent 4df609d commit 85daa7f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 62 deletions.
54 changes: 4 additions & 50 deletions src/ert/run_models/everest_run_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import shutil
from collections import defaultdict
from collections.abc import Callable
from dataclasses import dataclass
from enum import IntEnum
from pathlib import Path
from types import TracebackType
Expand All @@ -22,7 +21,6 @@
from ropt.evaluator import EvaluatorContext, EvaluatorResult
from ropt.plan import BasicOptimizer
from ropt.plan import Event as OptimizerEvent
from seba_sqlite import SqliteStorage, sqlite_storage
from typing_extensions import TypedDict

from _ert.events import EESnapshot, EESnapshotUpdate, Event
Expand All @@ -31,7 +29,7 @@
from ert.runpaths import Runpaths
from ert.storage import open_storage
from everest.config import EverestConfig
from everest.everest_storage import EverestStorage
from everest.everest_storage import EverestStorage, OptimalResult
from everest.optimizer.everest2ropt import everest2ropt
from everest.simulator.everest_to_ert import everest_to_ert_config
from everest.strings import EVEREST
Expand Down Expand Up @@ -70,24 +68,6 @@ class OptimizerCallback(Protocol):
def __call__(self) -> str | None: ...


@dataclass
class OptimalEverestResult:
batch: int
controls: list[Any]
total_objective: float

@staticmethod
def from_seba_optimal_result(
o: sqlite_storage.OptimalResult | None = None,
) -> OptimalEverestResult | None:
if o is None:
return None

return OptimalEverestResult(
batch=o.batch, controls=o.controls, total_objective=o.total_objective
)


class EverestExitCode(IntEnum):
COMPLETED = 1
TOO_FEW_REALIZATIONS = 2
Expand Down Expand Up @@ -120,7 +100,7 @@ def __init__(
self._sim_callback = simulation_callback
self._opt_callback = optimization_callback
self._fm_errors: dict[int, dict[str, Any]] = {}
self._result: OptimalEverestResult | None = None
self._result: OptimalResult | None = None
self._exit_code: EverestExitCode | None = None
self._simulator_cache = (
SimulatorCache()
Expand Down Expand Up @@ -173,7 +153,7 @@ def exit_code(self) -> EverestExitCode | None:
return self._exit_code

@property
def result(self) -> OptimalEverestResult | None:
def result(self) -> OptimalResult | None:
return self._result

def __repr__(self) -> str:
Expand Down Expand Up @@ -204,37 +184,11 @@ def run_experiment(
/ "OPT_DEFAULT.out",
)

# The SqliteStorage object is used to store optimization results from
# Seba in an sqlite database. It reacts directly to events emitted by
# Seba and is not called by Everest directly. The stored results are
# accessed by Everest via separate SebaSnapshot objects.
# This mechanism is outdated and not supported by the ropt package. It
# is retained for now via the seba_sqlite package.
seba_storage = SqliteStorage( # type: ignore
optimizer, self._everest_config.optimization_output_dir
)

# Run the optimization:
optimizer_exit_code = optimizer.run().exit_code

# Extract the best result from the storage.
self._result = OptimalEverestResult.from_seba_optimal_result(
seba_storage.get_optimal_result() # type: ignore
)
optimal_result_from_everstorage = self.ever_storage.get_optimal_result()

# TEMP sanity check
if optimal_result_from_everstorage is None:
assert self._result == None
else:
assert optimal_result_from_everstorage.__dict__ == self._result.__dict__

# Seems ROPT batches are 1-indexed now,
# whereas seba has its own 0-indexed counter.
if self._result is None:
assert optimal_result_from_everstorage is None
else:
assert self._result.__dict__ == optimal_result_from_everstorage.__dict__
self._result = self.ever_storage.get_optimal_result()

if self._exit_code is None:
match optimizer_exit_code:
Expand Down
12 changes: 0 additions & 12 deletions src/everest/everest_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from ropt.enums import EventType
from ropt.plan import BasicOptimizer, Event
from ropt.results import FunctionResults, GradientResults, convert_to_maximize
from seba_sqlite import sqlite_storage

logger = logging.getLogger(__name__)

Expand All @@ -32,17 +31,6 @@ class OptimalResult:
controls: list[Any]
total_objective: float

@staticmethod
def from_seba_optimal_result(
o: sqlite_storage.OptimalResult | None = None,
) -> OptimalResult | None:
if o is None:
return None

return OptimalResult(
batch=o.batch + 1, controls=o.controls, total_objective=o.total_objective
)


def try_read_df(path: Path) -> polars.DataFrame | None:
return polars.read_parquet(path) if path.exists() else None
Expand Down

0 comments on commit 85daa7f

Please sign in to comment.