Skip to content

Commit

Permalink
MAINT: Add defaults to DerivedNamedStratigraphy
Browse files Browse the repository at this point in the history
Also fixed an annotation error.
  • Loading branch information
mferrera committed May 28, 2024
1 parent 156928d commit 9292639
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 31 deletions.
48 changes: 21 additions & 27 deletions src/fmu/dataio/providers/objectdata/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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/...
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions tests/test_units/test_objectdataprovider_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9292639

Please sign in to comment.