Skip to content

Commit

Permalink
Remove ErtConfig and LibresFacade
Browse files Browse the repository at this point in the history
  • Loading branch information
frode-aarstad committed Nov 18, 2024
1 parent c4a2062 commit a02a486
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
43 changes: 20 additions & 23 deletions src/everest/bin/everload_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@
from functools import partial

from ert import LibresFacade
from ert.config import ErtConfig
from ert.storage import open_storage
from everest import MetaDataColumnNames as MDCN
from everest.config import EverestConfig
from everest.config.export_config import ExportConfig
from everest.export import export_data
from everest.simulator.everest_to_ert import _everest_to_ert_config_dict
from everest.strings import SIMULATION_DIR, STORAGE_DIR
from everest.util import version_info


def everload_entry(args=None):
def everload_entry(args=None) -> None:
parser = _build_args_parser()
options = parser.parse_args(args)
if options.debug:
Expand Down Expand Up @@ -70,7 +69,7 @@ def everload_entry(args=None):
reload_data(config, backup_path=backup_path)


def _build_args_parser():
def _build_args_parser() -> argparse.ArgumentParser:
"""Build arg parser"""
arg_parser = argparse.ArgumentParser(
description="Load Eclipse data from an existing simulation folder",
Expand Down Expand Up @@ -117,7 +116,7 @@ def batch(batch_str, parser=arg_parser):
return arg_parser


def user_confirms(simulation_path, storage_path=None, backup_path=None):
def user_confirms(simulation_path, storage_path=None, backup_path=None) -> bool:
print("\n*************************************************************")
print("*** This operation can take several minutes or even hours ***")
print("*************************************************************\n")
Expand All @@ -139,7 +138,7 @@ def user_confirms(simulation_path, storage_path=None, backup_path=None):
return True


def reload_data(ever_config: EverestConfig, backup_path=None):
def reload_data(ever_config: EverestConfig, backup_path=None) -> None:
"""Load data from a completed optimization into ert storage
If @batch_ids are given, only the specified batches are internalized
Expand All @@ -155,12 +154,6 @@ def reload_data(ever_config: EverestConfig, backup_path=None):
ever_config.install_data = None
ever_config.install_templates = None

# prepare the ErtConfig object
ert_config_dict = _everest_to_ert_config_dict(
ever_config, site_config=ErtConfig.read_site_config()
)
ert_config = ErtConfig.with_plugins().from_dict(config_dict=ert_config_dict)

# load information about batches from previous run
df = export_data(
export_config=ever_config.export,
Expand All @@ -176,25 +169,29 @@ def reload_data(ever_config: EverestConfig, backup_path=None):
else:
shutil.rmtree(ever_config.storage_dir)

ensemble_path = os.path.join(ever_config.output_dir, STORAGE_DIR)
run_path_format = os.path.join(
ever_config.simulation_dir,
"<CASE_NAME>",
"geo_realization_<GEO_ID>",
SIMULATION_DIR,
)

# internalize one batch at a time
for batch_id, group in groups:
_internalize_batch(ert_config, batch_id, group)
_internalize_batch(ensemble_path, run_path_format, batch_id, group)


def _internalize_batch(ert_config, batch_id, batch_data):
facade = LibresFacade(ert_config)
def _internalize_batch(
ensemble_path: str, run_path_format: str, batch_id, batch_data
) -> None:
case_name = "batch_{}".format(batch_id)
batch_size = batch_data.shape[0]
with open_storage(facade.enspath, "w") as storage:
with open_storage(ensemble_path, "w") as storage:
experiment = storage.get_experiment_by_name(f"experiment_{case_name}")
ensemble = experiment.get_ensemble_by_name(case_name)
# Everest artificially inflates the ensemble size as it is not possible to
# add after the fact, therefore a batch is much smaller than the overall
# ensemble size
realizations = [True] * batch_size + [False] * (
facade.get_ensemble_size() - batch_size
)
facade.load_from_forward_model(ensemble, realizations)
active_realizations = list(range(batch_size))
LibresFacade.load_from_run_path(run_path_format, ensemble, active_realizations)


if __name__ == "__main__":
Expand Down
20 changes: 11 additions & 9 deletions tests/everest/unit/everest/bin/test_everload.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
create_cached_mocked_test_case,
)

from ert.config import ErtConfig
from everest import MetaDataColumnNames as MDCN
from everest.bin.everload_script import everload_entry
from everest.config import EverestConfig
Expand Down Expand Up @@ -43,10 +42,13 @@ def get_config(cache_dir):
return config


def assertInternalizeCalls(batch_ids, mocked_internalize):
def assert_internalize_calls(batch_ids, mocked_internalize):
for i, b_id in enumerate(batch_ids):
config, bid, data = mocked_internalize.call_args_list[i].args
assert isinstance(config, ErtConfig)
ensemble_path, run_path_format, bid, data = mocked_internalize.call_args_list[
i
].args
assert isinstance(ensemble_path, str)
assert isinstance(run_path_format, str)
assert isinstance(data, pd.DataFrame)
assert bid == b_id

Expand All @@ -73,7 +75,7 @@ def test_everload_entry_run(
export_ecl=False,
)
batch_ids = set(df[MDCN.BATCH])
assertInternalizeCalls(batch_ids, mocked_internalize)
assert_internalize_calls(batch_ids, mocked_internalize)
assertBackup(config)


Expand Down Expand Up @@ -125,7 +127,7 @@ def test_everload_entry_batches(

everload_entry([CONFIG_FILE, "-s", "-b"] + [str(b) for b in batch_ids])

assertInternalizeCalls(batch_ids, mocked_internalize)
assert_internalize_calls(batch_ids, mocked_internalize)
assertBackup(config)


Expand Down Expand Up @@ -162,7 +164,7 @@ def test_everload_entry_overwrite(
export_ecl=False,
)
batch_ids = set(df[MDCN.BATCH])
assertInternalizeCalls(batch_ids, mocked_internalize)
assert_internalize_calls(batch_ids, mocked_internalize)

# Note that, as we are mocking the entire ert related part, the
# internalization does not take place, so no new storage dir is created
Expand Down Expand Up @@ -204,7 +206,7 @@ def test_everload_entry_not_silent(
export_ecl=False,
)
batch_ids = set(df[MDCN.BATCH])
assertInternalizeCalls(batch_ids, mocked_internalize)
assert_internalize_calls(batch_ids, mocked_internalize)

df = export_data(
export_config=config.export,
Expand All @@ -213,5 +215,5 @@ def test_everload_entry_not_silent(
export_ecl=False,
)
batch_ids = set(df[MDCN.BATCH])
assertInternalizeCalls(batch_ids, mocked_internalize)
assert_internalize_calls(batch_ids, mocked_internalize)
assertBackup(config)

0 comments on commit a02a486

Please sign in to comment.