Skip to content

Commit

Permalink
Install forward models as ert plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
verveerpj committed Nov 4, 2024
1 parent 974266c commit 2f44d01
Show file tree
Hide file tree
Showing 20 changed files with 66 additions and 61 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ docs = [
[project.entry-points."everest"]
everest-models = "everest_models.everest_hooks"

[project.entry-points."ert"]
everest_models_forward_models = "everest_models.forward_models"

[project.scripts]
fm_add_templates = "everest_models.jobs.fm_add_templates.cli:main_entry_point"
fm_drill_date_planner = "everest_models.jobs.fm_drill_date_planner.cli:main_entry_point"
Expand Down
24 changes: 5 additions & 19 deletions src/everest_models/everest_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import logging
import pathlib
import sys
from importlib import import_module, resources
from typing import Any, Dict, List, Sequence, Type

Expand All @@ -24,34 +23,21 @@

logger = logging.getLogger(__name__)

FORWARD_MODEL_DIR = "forward_models"
PACKAGE = "everest_models"
JOBS = f"{PACKAGE}.jobs"
JOBS = "everest_models.jobs"


def _get_jobs():
return (job for job in resources.contents(JOBS) if job.startswith("fm_"))


@hookimpl
def get_forward_models() -> List[Dict[str, str]]:
"""Accumulate all maintained forward model jobs by name and path.
def get_forward_models() -> List[str]:
"""Return a list of forward model names.
Returns:
(List[Dict[str, str]]): list of forward models and corrolated path
- {name: forward_model, path: /path/to/forward_model}
- ...
List[str]: list of forward models
"""
if sys.version_info.minor >= 9:
jobs = resources.files(PACKAGE) / FORWARD_MODEL_DIR # type: ignore
else:
with resources.path(PACKAGE, FORWARD_MODEL_DIR) as fd:
jobs = fd

return [
{"name": (job_name := job.lstrip("fm_")), "path": str(jobs / job_name)}
for job in _get_jobs()
]
return [job.lstrip("fm_") for job in _get_jobs()]


@hookimpl
Expand Down
40 changes: 40 additions & 0 deletions src/everest_models/forward_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from importlib import resources
from importlib.util import find_spec
from typing import Final, Type

_HAVE_ERT: Final = find_spec("ert") is not None

if _HAVE_ERT: # The everest-models package should remain installable without ERT.
import ert
from ert import ForwardModelStepDocumentation, ForwardModelStepPlugin

def build_forward_model_step_plugin(
executable_name: str,
) -> Type[ForwardModelStepPlugin]:
forward_model_name = executable_name.lstrip("fm_")
class_name = "".join(
x.capitalize() for x in forward_model_name.lower().split("_")
)
return type(
class_name,
(ForwardModelStepPlugin,),
{
"__init__": lambda x: ForwardModelStepPlugin.__init__(
x, name=forward_model_name, command=[executable_name]
),
"documentation": lambda: ForwardModelStepDocumentation(
category="everest.everest_models",
source_package="everest_models",
source_function_name=class_name,
description=f"The {forward_model_name} forward model.",
),
},
)

@ert.plugin(name="everest_models")
def installable_forward_model_steps():
return [
build_forward_model_step_plugin(job)
for job in resources.contents("everest_models.jobs")
if job.startswith("fm_")
]
1 change: 0 additions & 1 deletion src/everest_models/forward_models/add_templates

This file was deleted.

1 change: 0 additions & 1 deletion src/everest_models/forward_models/compute_economics

This file was deleted.

1 change: 0 additions & 1 deletion src/everest_models/forward_models/drill_date_planner

This file was deleted.

1 change: 0 additions & 1 deletion src/everest_models/forward_models/drill_planner

This file was deleted.

1 change: 0 additions & 1 deletion src/everest_models/forward_models/extract_summary_data

This file was deleted.

1 change: 0 additions & 1 deletion src/everest_models/forward_models/interpret_well_drill

This file was deleted.

1 change: 0 additions & 1 deletion src/everest_models/forward_models/npv

This file was deleted.

1 change: 0 additions & 1 deletion src/everest_models/forward_models/rf

This file was deleted.

1 change: 0 additions & 1 deletion src/everest_models/forward_models/schmerge

This file was deleted.

1 change: 0 additions & 1 deletion src/everest_models/forward_models/select_wells

This file was deleted.

4 changes: 0 additions & 4 deletions src/everest_models/forward_models/stea

This file was deleted.

3 changes: 0 additions & 3 deletions src/everest_models/forward_models/strip_dates

This file was deleted.

1 change: 0 additions & 1 deletion src/everest_models/forward_models/well_constraints

This file was deleted.

1 change: 0 additions & 1 deletion src/everest_models/forward_models/well_filter

This file was deleted.

1 change: 0 additions & 1 deletion src/everest_models/forward_models/well_swapping

This file was deleted.

1 change: 0 additions & 1 deletion src/everest_models/forward_models/well_trajectory

This file was deleted.

39 changes: 18 additions & 21 deletions tests/integration/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,25 @@ def test_hooks_registered(plugin_manager):

def test_get_forward_models_hook(plugin_manager):
jobs = {
"stea": f"{FORWARD_MODEL_DIR}/stea",
"drill_planner": f"{FORWARD_MODEL_DIR}/drill_planner",
"compute_economics": f"{FORWARD_MODEL_DIR}/compute_economics",
"schmerge": f"{FORWARD_MODEL_DIR}/schmerge",
"extract_summary_data": f"{FORWARD_MODEL_DIR}/extract_summary_data",
"drill_date_planner": f"{FORWARD_MODEL_DIR}/drill_date_planner",
"strip_dates": f"{FORWARD_MODEL_DIR}/strip_dates",
"select_wells": f"{FORWARD_MODEL_DIR}/select_wells",
"npv": f"{FORWARD_MODEL_DIR}/npv",
"well_constraints": f"{FORWARD_MODEL_DIR}/well_constraints",
"add_templates": f"{FORWARD_MODEL_DIR}/add_templates",
"rf": f"{FORWARD_MODEL_DIR}/rf",
"well_filter": f"{FORWARD_MODEL_DIR}/well_filter",
"interpret_well_drill": f"{FORWARD_MODEL_DIR}/interpret_well_drill",
"well_trajectory": f"{FORWARD_MODEL_DIR}/well_trajectory",
"well_swapping": f"{FORWARD_MODEL_DIR}/well_swapping",
"stea",
"drill_planner",
"compute_economics",
"schmerge",
"extract_summary_data",
"drill_date_planner",
"strip_dates",
"select_wells",
"npv",
"well_constraints",
"add_templates",
"rf",
"well_filter",
"interpret_well_drill",
"well_trajectory",
"well_swapping",
}
assert all(
jobs[job["name"]] in job["path"]
for job in itertools.chain.from_iterable(
plugin_manager.hook.get_forward_models()
)
assert jobs.issubset(
set(itertools.chain.from_iterable(plugin_manager.hook.get_forward_models()))
)


Expand Down

0 comments on commit 2f44d01

Please sign in to comment.