Skip to content

Commit

Permalink
ENH: Don't persist metadata in ExportData
Browse files Browse the repository at this point in the history
`generate_metadata` stored generated metadata internally. We don't need
to do this, and probably don't want to do this, because an ExportData
instance can be used to generate metadata across many objects.
  • Loading branch information
mferrera committed Jun 13, 2024
1 parent 1838fa7 commit f9036e4
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
13 changes: 3 additions & 10 deletions src/fmu/dataio/dataio.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import os
import warnings
from copy import deepcopy
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, ClassVar, Dict, Final, List, Literal, Optional, Union
Expand All @@ -22,7 +21,6 @@
from ._metadata import generate_export_metadata
from ._utils import (
detect_inside_rms, # dataio_examples,
drop_nones,
export_file,
export_metadata_file,
prettyprint_dict,
Expand Down Expand Up @@ -377,7 +375,6 @@ class ExportData:
_usefmtflag: str = field(default="", init=False)

# storing resulting state variables for instance, non-public:
_metadata: dict = field(default_factory=dict, init=False)
_pwd: Path = field(default_factory=Path, init=False)
_config_is_valid: bool = field(default=True, init=False)
_fmurun: bool = field(default=False, init=False)
Expand Down Expand Up @@ -769,15 +766,11 @@ def generate_metadata(

self._update_fmt_flag()
fmudata = self._get_fmu_provider() if self._fmurun else None
self._metadata = drop_nones(
generate_export_metadata(
obj, self, fmudata, compute_md5=compute_md5
).model_dump(mode="json", exclude_none=True, by_alias=True)
)

logger.info("The metadata are now ready!")

return deepcopy(self._metadata)
return generate_export_metadata(
obj, self, fmudata, compute_md5=compute_md5
).model_dump(mode="json", exclude_none=True, by_alias=True)

def export(
self,
Expand Down
13 changes: 7 additions & 6 deletions tests/test_units/test_dataio.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,12 +535,13 @@ def test_exportdata_no_iter_folder(
edata = ExportData(config=rmsglobalconfig, content="depth")
assert edata._fmurun is True

edata.export(regsurf)

assert edata._metadata["fmu"]["realization"]["name"] == "realization-1"
assert edata._metadata["fmu"]["realization"]["id"] == 1
assert edata._metadata["fmu"]["iteration"]["name"] == "iter-0"
assert edata._metadata["fmu"]["iteration"]["id"] == 0
out = Path(edata.export(regsurf))
with open(out.parent / f".{out.name}.yml", encoding="utf-8") as f:
metadata = yaml.safe_load(f)
assert metadata["fmu"]["realization"]["name"] == "realization-1"
assert metadata["fmu"]["realization"]["id"] == 1
assert metadata["fmu"]["iteration"]["name"] == "iter-0"
assert metadata["fmu"]["iteration"]["id"] == 0


def test_fmucontext_case_casepath(fmurun_prehook, rmsglobalconfig, regsurf):
Expand Down
9 changes: 7 additions & 2 deletions tests/test_units/test_ert_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
import logging
import os
import sys
from pathlib import Path

import fmu.dataio.dataio as dataio
import pandas as pd
import pytest
import yaml
from fmu.dataio._utils import prettyprint_dict

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -115,8 +117,11 @@ def test_regsurf_export_file_fmurun(fmurun_w_casemetadata, rmsglobalconfig, regs
)
logger.info("Output is %s", output)

assert edata._metadata["access"]["ssdl"]["access_level"] == "restricted"
assert edata._metadata["data"]["unit"] == "forthnite"
out = Path(edata.export(regsurf))
with open(out.parent / f".{out.name}.yml", encoding="utf-8") as f:
metadata = yaml.safe_load(f)
assert metadata["access"]["ssdl"]["access_level"] == "restricted"
assert metadata["data"]["unit"] == "forthnite"


# ======================================================================================
Expand Down
8 changes: 7 additions & 1 deletion tests/test_units/test_objectdataprovider_class.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""Test the _ObjectData class from the _objectdata.py module"""

import os
from pathlib import Path

import fmu.dataio as dataio
import pytest
import yaml
from fmu.dataio._definitions import ConfigurationError, ValidFormats
from fmu.dataio.providers.objectdata._provider import (
objectdata_provider_factory,
Expand Down Expand Up @@ -141,4 +143,8 @@ def _run_case_fmu(fmurun_prehook, surfacepath):
edata, mysurf = _export_data_from_rms(rmssetup, rmsglobalconfig, regsurf)
set_ert_env_prehook(monkeypatch)
case_meta = _run_case_fmu(fmurun_prehook, mysurf)
assert edata._metadata["data"] == case_meta["data"]

out = Path(mysurf)
with open(out.parent / f".{out.name}.yml", encoding="utf-8") as f:
metadata = yaml.safe_load(f)
assert metadata["data"] == case_meta["data"]
8 changes: 6 additions & 2 deletions tests/test_units/test_rms_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import fmu.dataio.readers as readers
import pandas as pd
import pytest
import yaml
from fmu.dataio._definitions import FmuContext
from fmu.dataio._utils import prettyprint_dict
from fmu.dataio.dataio import ValidationError
Expand Down Expand Up @@ -224,8 +225,11 @@ def test_regsurf_export_file_fmurun(
)
logger.info("Output is %s", output)

assert edata._metadata["access"]["ssdl"]["access_level"] == "restricted"
assert edata._metadata["data"]["unit"] == "forthnite"
out = Path(edata.export(regsurf))
with open(out.parent / f".{out.name}.yml", encoding="utf-8") as f:
metadata = yaml.safe_load(f)
assert metadata["access"]["ssdl"]["access_level"] == "restricted"
assert metadata["data"]["unit"] == "forthnite"


# ======================================================================================
Expand Down

0 comments on commit f9036e4

Please sign in to comment.