Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Add Ert experiment id to FMU metadata block #755

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
15 changes: 15 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,13 @@ 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=fields.Ert(
experiment=fields.Experiment(
id=uuid.UUID(FmuEnv.EXPERIMENT_ID.value)
if FmuEnv.EXPERIMENT_ID.value
else None
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could pack this into a method self._get_ert_meta() since it is used two places? Then it will be more aligned with the rest 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes absolutely, good point!

),
)

iter_uuid, real_uuid = self._get_iteration_and_real_uuid(
Expand All @@ -191,6 +199,13 @@ 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=fields.Ert(
experiment=fields.Experiment(
id=uuid.UUID(FmuEnv.EXPERIMENT_ID.value)
if FmuEnv.EXPERIMENT_ID.value
else None
)
),
)

@staticmethod
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
Loading