From 9292639199e326ecc4981ebc76256874d5fe4331 Mon Sep 17 00:00:00 2001 From: mferrera Date: Tue, 28 May 2024 10:43:00 +0200 Subject: [PATCH] MAINT: Add defaults to DerivedNamedStratigraphy Also fixed an annotation error. --- src/fmu/dataio/providers/objectdata/_base.py | 48 ++++++++----------- .../test_objectdataprovider_class.py | 8 ++-- 2 files changed, 25 insertions(+), 31 deletions(-) diff --git a/src/fmu/dataio/providers/objectdata/_base.py b/src/fmu/dataio/providers/objectdata/_base.py index 326142d05..23c450c37 100644 --- a/src/fmu/dataio/providers/objectdata/_base.py +++ b/src/fmu/dataio/providers/objectdata/_base.py @@ -47,14 +47,14 @@ class DerivedObjectDescriptor: @dataclass class DerivedNamedStratigraphy: name: str - alias: list[str] + alias: list[str] = field(default_factory=list) - stratigraphic: bool - stratigraphic_alias: list[str] + stratigraphic: bool = field(default=False) + stratigraphic_alias: list[str] = field(default_factory=list) - offset: int - base: str | None - top: str | None + offset: float = field(default=0.0) + base: str | None = field(default=None) + top: str | None = field(default=None) def derive_name( @@ -191,7 +191,7 @@ def __post_init__(self) -> None: if self.metadata: return - namedstratigraphy = self._derive_name_stratigraphy() + namedstratigraphy = self._derive_named_stratigraphy() objres = self.get_objectdata() content_model = get_validated_content(self.dataio.content) @@ -251,7 +251,7 @@ def __post_init__(self) -> None: self.fmt = objres.fmt logger.info("Derive all metadata for data object... DONE") - def _derive_name_stratigraphy(self) -> DerivedNamedStratigraphy: + def _derive_named_stratigraphy(self) -> DerivedNamedStratigraphy: """Derive the name and stratigraphy for the object; may have several sources. If not in input settings it is tried to be inferred from the xtgeo/pandas/... @@ -263,28 +263,22 @@ def _derive_name_stratigraphy(self) -> DerivedNamedStratigraphy: name = derive_name(self.dataio, self.obj) # next check if usename has a "truename" and/or aliases from the config - strat = self.dataio.config.get("stratigraphy", {}) - no_stratigraphy_or_name = strat is None or name not in strat + stratigraphy = self.dataio.config.get("stratigraphy", {}) + if name not in stratigraphy: + return DerivedNamedStratigraphy(name=name) + + named_stratigraphy = stratigraphy.get(name) rv = DerivedNamedStratigraphy( - name=name if no_stratigraphy_or_name else strat[name].get("name", name), - alias=[] if no_stratigraphy_or_name else strat[name].get("alias", []), - stratigraphic=( - False - if no_stratigraphy_or_name - else strat[name].get("stratigraphic", False) - ), - stratigraphic_alias=( - [] - if no_stratigraphy_or_name - else strat[name].get("stratigraphic_alias") - ), - offset=0.0 if no_stratigraphy_or_name else strat[name].get("offset", 0.0), - top=None if no_stratigraphy_or_name else strat[name].get("top"), - base=None if no_stratigraphy_or_name else strat[name].get("base"), + name=named_stratigraphy.get("name", name), + alias=named_stratigraphy.get("alias", []), + stratigraphic=named_stratigraphy.get("stratigraphic", False), + stratigraphic_alias=named_stratigraphy.get("stratigraphic_alias", []), + offset=named_stratigraphy.get("offset", 0.0), + top=named_stratigraphy.get("top"), + base=named_stratigraphy.get("base"), ) - - if not no_stratigraphy_or_name and rv.name != "name": + if rv.name != "name": rv.alias.append(name) return rv diff --git a/tests/test_units/test_objectdataprovider_class.py b/tests/test_units/test_objectdataprovider_class.py index 16063f2bf..be1788e26 100644 --- a/tests/test_units/test_objectdataprovider_class.py +++ b/tests/test_units/test_objectdataprovider_class.py @@ -46,24 +46,24 @@ def test_get_timedata_from_existing(given: dict, expected: tuple): # -------------------------------------------------------------------------------------- -def test_objectdata_regularsurface_derive_name_stratigraphy(regsurf, edataobj1): +def test_objectdata_regularsurface_derive_named_stratigraphy(regsurf, edataobj1): """Get name and some stratigaphic keys for a valid RegularSurface object .""" # mimic the stripped parts of configuations for testing here objdata = objectdata_provider_factory(regsurf, edataobj1) - res = objdata._derive_name_stratigraphy() + res = objdata._derive_named_stratigraphy() assert res.name == "Whatever Top" assert "TopWhatever" in res.alias assert res.stratigraphic is True -def test_objectdata_regularsurface_derive_name_stratigraphy_differ(regsurf, edataobj2): +def test_objectdata_regularsurface_derive_named_stratigraphy_differ(regsurf, edataobj2): """Get name and some stratigaphic keys for a valid RegularSurface object .""" # mimic the stripped parts of configuations for testing here objdata = objectdata_provider_factory(regsurf, edataobj2) - res = objdata._derive_name_stratigraphy() + res = objdata._derive_named_stratigraphy() assert res.name == "VOLANTIS GP. Top" assert "TopVolantis" in res.alias