Skip to content

Commit

Permalink
ENH: Add Ert experiment id to FMU metadata block (#755)
Browse files Browse the repository at this point in the history
  • Loading branch information
slangeveld authored Aug 26, 2024
1 parent 2e58bdf commit fc19b07
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 1 deletion.
49 changes: 49 additions & 0 deletions schema/definitions/0.8.0/schema/fmu_results.json
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,44 @@
"title": "DomainReference",
"type": "string"
},
"Ert": {
"description": "The ``fmu.ert`` block contains information about the current ert run.",
"properties": {
"experiment": {
"anyOf": [
{
"$ref": "#/$defs/Experiment"
},
{
"type": "null"
}
],
"default": null
}
},
"title": "Ert",
"type": "object"
},
"Experiment": {
"description": "The ``fmu.ert.experiment`` block contains information about\nthe current ert experiment run.",
"properties": {
"id": {
"anyOf": [
{
"format": "uuid",
"type": "string"
},
{
"type": "null"
}
],
"default": null,
"title": "Id"
}
},
"title": "Experiment",
"type": "object"
},
"FMU": {
"dependencies": {
"aggregation": {
Expand Down Expand Up @@ -987,6 +1025,17 @@
"context": {
"$ref": "#/$defs/Context"
},
"ert": {
"anyOf": [
{
"$ref": "#/$defs/Ert"
},
{
"type": "null"
}
],
"default": null
},
"iteration": {
"anyOf": [
{
Expand Down
20 changes: 20 additions & 0 deletions src/fmu/dataio/_model/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,22 @@ class Case(BaseModel):
"""A free-text description of this case."""


class Ert(BaseModel):
"""The ``fmu.ert`` block contains information about the current ert run."""

experiment: Optional[Experiment] = Field(default=None)
"""Reference to the ert experiment.
See :class:`Experiment`."""


class Experiment(BaseModel):
"""The ``fmu.ert.experiment`` block contains information about
the current ert experiment run."""

id: Optional[UUID] = Field(default=None)
"""The unique identifier of this ert experiment run."""


class Iteration(BaseModel):
"""
The ``fmu.iteration`` block contains information about the iteration this data
Expand Down Expand Up @@ -575,6 +591,10 @@ class FMU(FMUBase):
"""The ``fmu.realization`` block contains information about the realization this
data object belongs to. See :class:`Realization`."""

ert: Optional[Ert] = Field(default=None)
"""The ``fmu.ert`` block contains information about the current ert run
See :class:`Ert`."""

@model_validator(mode="before")
@classmethod
def _dependencies_aggregation_realization(cls, values: Dict) -> Dict:
Expand Down
13 changes: 13 additions & 0 deletions src/fmu/dataio/providers/_fmu.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from __future__ import annotations

import os
import uuid
from dataclasses import dataclass, field
from enum import Enum, auto
from pathlib import Path
Expand Down Expand Up @@ -179,6 +180,7 @@ def get_metadata(self) -> internal.FMUClassMetaData:
context=self._get_fmucontext_meta(),
model=self._get_fmumodel_meta() if self.model else case_meta.fmu.model,
workflow=self._get_workflow_meta() if self.workflow else None,
ert=self._get_ert_meta(),
)

iter_uuid, real_uuid = self._get_iteration_and_real_uuid(
Expand All @@ -191,13 +193,24 @@ def get_metadata(self) -> internal.FMUClassMetaData:
workflow=self._get_workflow_meta() if self.workflow else None,
iteration=self._get_iteration_meta(iter_uuid),
realization=self._get_realization_meta(real_uuid),
ert=self._get_ert_meta(),
)

@staticmethod
def _get_runpath_from_env() -> Path | None:
"""get runpath as an absolute path if detected from the enviroment"""
return Path(runpath).resolve() if (runpath := FmuEnv.RUNPATH.value) else None

@staticmethod
def _get_ert_meta() -> fields.Ert:
return fields.Ert(
experiment=fields.Experiment(
id=uuid.UUID(FmuEnv.EXPERIMENT_ID.value)
if FmuEnv.EXPERIMENT_ID.value
else None
)
)

def _validate_and_establish_casepath(self) -> Path | None:
"""If casepath is not given, then try update _casepath (if in realization).
Expand Down
30 changes: 30 additions & 0 deletions tests/test_units/test_dataio.py
Original file line number Diff line number Diff line change
Expand Up @@ -1000,3 +1000,33 @@ def test_append_to_alias_list(globalconfig2, regsurf):
# also check that the name input was added to the alias list
assert name not in strat["alias"]
assert name in meta["data"]["alias"]


def test_ert_experiment_id_present_in_generated_metadata(
fmurun_w_casemetadata, monkeypatch, globalconfig1, regsurf
):
"""Test that the ert experiment id has been set correctly
in the generated metadata"""

monkeypatch.chdir(fmurun_w_casemetadata)

edata = ExportData(config=globalconfig1, content="depth")
meta = edata.generate_metadata(regsurf)
expected_id = "6a8e1e0f-9315-46bb-9648-8de87151f4c7"
assert meta["fmu"]["ert"]["experiment"]["id"] == expected_id


def test_ert_experiment_id_present_in_exported_metadata(
fmurun_w_casemetadata, monkeypatch, globalconfig1, regsurf
):
"""Test that the ert experiment id has been set correctly
in the exported metadata"""

monkeypatch.chdir(fmurun_w_casemetadata)

edata = ExportData(config=globalconfig1, content="depth")
out = Path(edata.export(regsurf))
with open(out.parent / f".{out.name}.yml", encoding="utf-8") as f:
export_meta = yaml.safe_load(f)
expected_id = "6a8e1e0f-9315-46bb-9648-8de87151f4c7"
assert export_meta["fmu"]["ert"]["experiment"]["id"] == expected_id
2 changes: 1 addition & 1 deletion tests/test_units/test_ert_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_regsurf_generate_metadata(fmurun_w_casemetadata, rmsglobalconfig, regsu


def test_incl_jobs_warning(rmsglobalconfig):
"""Check that usning the deprecated class variable include_ertjobs gives warning."""
"""Check that using the deprecated class variable include_ertjobs gives warning."""

dataio.ExportData.include_ertjobs = True

Expand Down

0 comments on commit fc19b07

Please sign in to comment.