Skip to content

Commit

Permalink
ENH: Add ert simulation mode to object metadata
Browse files Browse the repository at this point in the history
The simulation mode should allow contextual inference about the type of
ensemble this data is derived from.
  • Loading branch information
mferrera committed Nov 5, 2024
1 parent de88a05 commit 8644c6f
Show file tree
Hide file tree
Showing 12 changed files with 268 additions and 45 deletions.
26 changes: 26 additions & 0 deletions schema/definitions/0.8.0/schema/fmu_results.json
Original file line number Diff line number Diff line change
Expand Up @@ -1004,11 +1004,37 @@
}
],
"default": null
},
"simulation_mode": {
"anyOf": [
{
"$ref": "#/$defs/ErtSimulationMode"
},
{
"type": "null"
}
],
"default": null
}
},
"title": "Ert",
"type": "object"
},
"ErtSimulationMode": {
"description": "The simulation mode ert was run in. These definitions come from\n`ert.mode_definitions`.",
"enum": [
"ensemble_experiment",
"ensemble_smoother",
"es_mda",
"evaluate_ensemble",
"iterative_ensemble_smoother",
"manual_update",
"test_run",
"workflow"
],
"title": "ErtSimulationMode",
"type": "string"
},
"Experiment": {
"description": "The ``fmu.ert.experiment`` block contains information about\nthe current ert experiment run.",
"properties": {
Expand Down
14 changes: 14 additions & 0 deletions src/fmu/dataio/_model/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ def _missing_(cls: Type[Content], value: object) -> None:
)


class ErtSimulationMode(str, Enum):
"""The simulation mode ert was run in. These definitions come from
`ert.mode_definitions`."""

ensemble_experiment = "ensemble_experiment"
ensemble_smoother = "ensemble_smoother"
es_mda = "es_mda"
evaluate_ensemble = "evaluate_ensemble"
iterative_ensemble_smoother = "iterative_ensemble_smoother"
manual_update = "manual_update"
test_run = "test_run"
workflow = "workflow"


class FMUClass(str, Enum):
"""The class of a data object by FMU convention or standards."""

Expand Down
4 changes: 4 additions & 0 deletions src/fmu/dataio/_model/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ class Ert(BaseModel):
"""Reference to the ert experiment.
See :class:`Experiment`."""

simulation_mode: Optional[enums.ErtSimulationMode] = Field(default=None)
"""Reference to the ert simulation mode.
See :class:`SimulationMode`."""


class Experiment(BaseModel):
"""The ``fmu.ert.experiment`` block contains information about
Expand Down
10 changes: 8 additions & 2 deletions src/fmu/dataio/providers/_fmu.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from fmu.dataio import _utils
from fmu.dataio._logging import null_logger
from fmu.dataio._model import fields, schema
from fmu.dataio._model.enums import FMUContext
from fmu.dataio._model.enums import ErtSimulationMode, FMUContext
from fmu.dataio.exceptions import InvalidMetadataError

from ._base import Provider
Expand Down Expand Up @@ -206,12 +206,18 @@ def _get_runpath_from_env() -> Path | None:

@staticmethod
def _get_ert_meta() -> fields.Ert:
try:
sim_mode = ErtSimulationMode(FmuEnv.SIMULATION_MODE.value)
except ValueError:
sim_mode = None

return fields.Ert(
experiment=fields.Experiment(
id=uuid.UUID(FmuEnv.EXPERIMENT_ID.value)
if FmuEnv.EXPERIMENT_ID.value
else None
)
),
simulation_mode=sim_mode,
)

def _validate_and_establish_casepath(self) -> Path | None:
Expand Down
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ def _current_function_name():
return inspect.currentframe().f_back.f_code.co_name


@pytest.fixture(scope="session")
def source_root(request) -> Path:
return request.config.rootpath


@pytest.fixture(scope="function", autouse=True)
def return_to_original_directory():
# store original folder, and restore after each function (before and after yield)
Expand Down
34 changes: 34 additions & 0 deletions tests/data/snakeoil/export-a-surface
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python

import sys
from pathlib import Path

import xtgeo

import fmu.dataio as dataio
from fmu.config import utilities as ut


def main() -> None:
snakeoil_path = Path(sys.argv[1])
CFG = ut.yaml_load(snakeoil_path / "fmuconfig/output/global_variables.yml")
surf = xtgeo.surface_from_file(
snakeoil_path / "ert/output/maps/props/poro_average.gri"
)
dataio.ExportData(
config=CFG,
name="all",
unit="fraction",
vertical_domain="depth",
domain_reference="msl",
content="property",
timedata=None,
is_prediction=True,
is_observation=False,
tagname="average_poro",
workflow="rms property model",
).export(surf)


if __name__ == "__main__":
main()
29 changes: 27 additions & 2 deletions tests/test_integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ def base_ert_config() -> str:


@pytest.fixture
def fmu_snakeoil_project(tmp_path, monkeypatch, base_ert_config, global_config2_path):
def fmu_snakeoil_project(
tmp_path, monkeypatch, base_ert_config, global_config2_path, source_root
):
"""Makes a skeleton FMU project structure into a tmp_path, copying global_config2
into it with a basic ert config that can be appended onto."""
monkeypatch.setenv("DATAIO_TMP_PATH", str(tmp_path))
Expand All @@ -42,7 +44,7 @@ def fmu_snakeoil_project(tmp_path, monkeypatch, base_ert_config, global_config2_
os.makedirs(tmp_path / f"{app}/bin")
os.makedirs(tmp_path / f"{app}/input")
os.makedirs(tmp_path / f"{app}/model")
os.makedirs(tmp_path / "rms/model/snakeoil.rms13.1.2")
os.makedirs(tmp_path / "rms/model/snakeoil.rms14.2.2")

os.makedirs(tmp_path / "fmuconfig/output")
shutil.copy(global_config2_path, tmp_path / "fmuconfig/output/")
Expand All @@ -65,6 +67,29 @@ def fmu_snakeoil_project(tmp_path, monkeypatch, base_ert_config, global_config2_
"../../share/preprocessed ", # inpath
encoding="utf-8",
)

# Add EXPORT_A_SURFACE forward model
os.makedirs(tmp_path / "ert/bin/scripts")
shutil.copy(
source_root / "tests/data/snakeoil/export-a-surface",
tmp_path / "ert/bin/scripts/export-a-surface",
)
os.makedirs(tmp_path / "ert/output/maps/props")
shutil.copy(
source_root
/ "examples/s/d/nn/xcase/realization-0/iter-0/rms"
/ "output/maps/props/poro_average.gri",
tmp_path / "ert/output/maps/props/poro_average.gri",
)
os.makedirs(tmp_path / "ert/bin/jobs")
pathlib.Path(tmp_path / "ert/bin/jobs/EXPORT_A_SURFACE").write_text(
"STDERR EXPORT_A_SURFACE.stderr\n"
"STDOUT EXPORT_A_SURFACE.stdout\n"
"EXECUTABLE ../scripts/export-a-surface\n"
"ARGLIST <SNAKEOIL_PATH>\n",
encoding="utf-8",
)

pathlib.Path(tmp_path / "ert/model/snakeoil.ert").write_text(
base_ert_config, encoding="utf-8"
)
Expand Down
35 changes: 35 additions & 0 deletions tests/test_integration/ert_config_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from __future__ import annotations

from pathlib import Path


def add_create_case_workflow(filepath: Path | str) -> None:
with open(filepath, "a", encoding="utf-8") as f:
f.writelines(
[
"LOAD_WORKFLOW ../bin/workflows/xhook_create_case_metadata\n"
"HOOK_WORKFLOW xhook_create_case_metadata PRE_SIMULATION\n"
]
)


def add_copy_preprocessed_workflow(filepath: Path | str) -> None:
with open(filepath, "a", encoding="utf-8") as f:
f.writelines(
[
"LOAD_WORKFLOW ../bin/workflows/xhook_copy_preprocessed_data\n"
"HOOK_WORKFLOW xhook_copy_preprocessed_data PRE_SIMULATION\n"
]
)


def add_export_a_surface_forward_model(
snakeoil_path: Path, filepath: Path | str
) -> None:
with open(filepath, "a", encoding="utf-8") as f:
f.writelines(
[
"INSTALL_JOB EXPORT_A_SURFACE ../bin/jobs/EXPORT_A_SURFACE\n"
f"FORWARD_MODEL EXPORT_A_SURFACE(<SNAKEOIL_PATH>={snakeoil_path})\n"
]
)
62 changes: 62 additions & 0 deletions tests/test_integration/test_simple_export_run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import getpass
from pathlib import Path
from typing import Any

import ert.__main__
import pytest
import yaml

from fmu.dataio._model import Root
from fmu.dataio._model.enums import ErtSimulationMode

from .ert_config_utils import (
add_create_case_workflow,
add_export_a_surface_forward_model,
)


@pytest.fixture
def snakeoil_export_surface(
fmu_snakeoil_project: Path, monkeypatch: Any, mocker: Any
) -> Path:
monkeypatch.chdir(fmu_snakeoil_project / "ert/model")
add_create_case_workflow("snakeoil.ert")
add_export_a_surface_forward_model(fmu_snakeoil_project, "snakeoil.ert")

mocker.patch(
"sys.argv", ["ert", "test_run", "snakeoil.ert", "--disable-monitoring"]
)
ert.__main__.main()
return fmu_snakeoil_project


def test_simple_export_case_metadata(snakeoil_export_surface: Path) -> None:
fmu_case_yml = (
snakeoil_export_surface / "scratch/user/snakeoil/share/metadata/fmu_case.yml"
)
assert fmu_case_yml.exists()

with open(fmu_case_yml, encoding="utf-8") as f:
fmu_case = yaml.safe_load(f)

assert fmu_case["fmu"]["case"]["name"] == "snakeoil"
assert fmu_case["fmu"]["case"]["user"]["id"] == "user"
assert fmu_case["source"] == "fmu"
assert len(fmu_case["tracklog"]) == 1
assert fmu_case["tracklog"][0]["user"]["id"] == getpass.getuser()


def test_simple_export_ert_environment_variables(snakeoil_export_surface: Path) -> None:
avg_poro_yml = Path(
snakeoil_export_surface
/ "scratch/user/snakeoil/realization-0/iter-0"
/ "share/results/maps/.all--average_poro.gri.yml"
)
assert avg_poro_yml.exists()

with open(avg_poro_yml, encoding="utf-8") as f:
avg_poro_metadata = yaml.safe_load(f)

avg_poro = Root.model_validate(avg_poro_metadata) # asserts valid
assert avg_poro.root.fmu.ert.simulation_mode == ErtSimulationMode.test_run
assert avg_poro.root.fmu.ert.experiment.id is not None
43 changes: 14 additions & 29 deletions tests/test_integration/test_wf_copy_preprocessed_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

import fmu.dataio as dataio

from .ert_config_utils import (
add_copy_preprocessed_workflow,
add_create_case_workflow,
)


def _export_preprocessed_data(config, regsurf):
"""Export preprocessed surfaces"""
Expand All @@ -23,26 +28,6 @@ def _export_preprocessed_data(config, regsurf):
).export(regsurf)


def _add_create_case_workflow(filepath):
with open(filepath, "a", encoding="utf-8") as f:
f.writelines(
[
"LOAD_WORKFLOW ../bin/workflows/xhook_create_case_metadata\n"
"HOOK_WORKFLOW xhook_create_case_metadata PRE_SIMULATION\n"
]
)


def _add_copy_preprocessed_workflow(filepath):
with open(filepath, "a", encoding="utf-8") as f:
f.writelines(
[
"LOAD_WORKFLOW ../bin/workflows/xhook_copy_preprocessed_data\n"
"HOOK_WORKFLOW xhook_copy_preprocessed_data PRE_SIMULATION\n"
]
)


def test_copy_preprocessed_runs_successfully(
fmu_snakeoil_project, monkeypatch, mocker, globalconfig2, regsurf
):
Expand All @@ -51,8 +36,8 @@ def test_copy_preprocessed_runs_successfully(
_export_preprocessed_data(globalconfig2, regsurf)

monkeypatch.chdir(fmu_snakeoil_project / "ert/model")
_add_create_case_workflow("snakeoil.ert")
_add_copy_preprocessed_workflow("snakeoil.ert")
add_create_case_workflow("snakeoil.ert")
add_copy_preprocessed_workflow("snakeoil.ert")

mocker.patch(
"sys.argv",
Expand Down Expand Up @@ -92,7 +77,7 @@ def test_copy_preprocessed_no_casemeta(
_export_preprocessed_data(globalconfig2, regsurf)

monkeypatch.chdir(fmu_snakeoil_project / "ert/model")
_add_copy_preprocessed_workflow("snakeoil.ert")
add_copy_preprocessed_workflow("snakeoil.ert")

mocker.patch(
"sys.argv",
Expand All @@ -114,8 +99,8 @@ def test_copy_preprocessed_no_preprocessed_files(
"""

monkeypatch.chdir(fmu_snakeoil_project / "ert/model")
_add_create_case_workflow("snakeoil.ert")
_add_copy_preprocessed_workflow("snakeoil.ert")
add_create_case_workflow("snakeoil.ert")
add_copy_preprocessed_workflow("snakeoil.ert")

mocker.patch(
"sys.argv",
Expand All @@ -142,7 +127,7 @@ def test_inpath_absolute_path_raises(fmu_snakeoil_project, monkeypatch, mocker,
)

monkeypatch.chdir(fmu_snakeoil_project / "ert/model")
_add_copy_preprocessed_workflow("snakeoil.ert")
add_copy_preprocessed_workflow("snakeoil.ert")

mocker.patch(
"sys.argv",
Expand All @@ -166,8 +151,8 @@ def test_copy_preprocessed_no_preprocessed_meta(
_export_preprocessed_data({"wrong": "config"}, regsurf)

monkeypatch.chdir(fmu_snakeoil_project / "ert/model")
_add_create_case_workflow("snakeoil.ert")
_add_copy_preprocessed_workflow("snakeoil.ert")
add_create_case_workflow("snakeoil.ert")
add_copy_preprocessed_workflow("snakeoil.ert")

mocker.patch(
"sys.argv",
Expand Down Expand Up @@ -200,7 +185,7 @@ def test_deprecation_warning_global_variables(
f.write(" '--global_variables_path' dummypath")

monkeypatch.chdir(fmu_snakeoil_project / "ert/model")
_add_copy_preprocessed_workflow("snakeoil.ert")
add_copy_preprocessed_workflow("snakeoil.ert")

mocker.patch(
"sys.argv",
Expand Down
Loading

0 comments on commit 8644c6f

Please sign in to comment.