Skip to content

Commit

Permalink
MAINT: Make layout, table_index properties
Browse files Browse the repository at this point in the history
  • Loading branch information
mferrera committed Jun 12, 2024
1 parent fa8fd62 commit eae2ea1
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 103 deletions.
9 changes: 9 additions & 0 deletions src/fmu/dataio/_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ class ExportFolder(str, Enum):
tables = "tables"


class Layout(str, Enum):
regular = "regular"
unset = "unset"
cornerpoint = "cornerpoint"
table = "table"
dictionary = "dictionary"
faultroom_triangulated = "faultroom_triangulated"


STANDARD_TABLE_INDEX_COLUMNS: Final[dict[str, list[str]]] = {
"volumes": ["ZONE", "REGION", "FACIES", "LICENCE"],
"rft": ["measured_depth", "well", "time"],
Expand Down
43 changes: 21 additions & 22 deletions src/fmu/dataio/providers/objectdata/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,18 @@
from fmu.dataio.providers._base import Provider

if TYPE_CHECKING:
from fmu.dataio._definitions import Layout
from fmu.dataio.dataio import ExportData
from fmu.dataio.datastructure.meta.content import BoundingBox2D, BoundingBox3D
from fmu.dataio.datastructure.meta.enums import FMUClassEnum
from fmu.dataio.datastructure.meta.specification import AnySpecification
from fmu.dataio.types import Inferrable, Layout
from fmu.dataio.types import Inferrable

logger: Final = null_logger(__name__)


@dataclass
class DerivedObjectDescriptor:
layout: Layout
table_index: list[str] | None


@dataclass
class DerivedNamedStratigraphy:
class NamedStratigraphy:
name: str
alias: list[str] = field(default_factory=list)

Expand Down Expand Up @@ -73,12 +68,6 @@ class ObjectDataProvider(Provider):
time1: datetime | None = field(default=None)

def __post_init__(self) -> None:
content_model = self._get_validated_content(self.dataio.content)
named_stratigraphy = self._get_named_stratigraphy()
obj_data = self.get_objectdata()

self.name = named_stratigraphy.name

if self.dataio.forcefolder:
if self.dataio.forcefolder.startswith("/"):
raise ValueError("Can't use absolute path as 'forcefolder'")
Expand All @@ -89,6 +78,10 @@ def __post_init__(self) -> None:
logger.info(msg)
warn(msg, UserWarning)

content_model = self._get_validated_content(self.dataio.content)
named_stratigraphy = self._get_named_stratigraphy()

self.name = named_stratigraphy.name
self._metadata["name"] = self.name
self._metadata["stratigraphic"] = named_stratigraphy.stratigraphic
self._metadata["offset"] = named_stratigraphy.offset
Expand All @@ -104,7 +97,7 @@ def __post_init__(self) -> None:

self._metadata["tagname"] = self.dataio.tagname
self._metadata["format"] = self.fmt
self._metadata["layout"] = obj_data.layout
self._metadata["layout"] = self.layout
self._metadata["unit"] = self.dataio.unit or ""
self._metadata["vertical_domain"] = list(self.dataio.vertical_domain.keys())[0]
self._metadata["depth_reference"] = list(self.dataio.vertical_domain.values())[
Expand All @@ -127,7 +120,7 @@ def __post_init__(self) -> None:
else None
)

self._metadata["table_index"] = obj_data.table_index
self._metadata["table_index"] = self.table_index
self._metadata["undef_is_zero"] = self.dataio.undef_is_zero

# timedata:
Expand Down Expand Up @@ -156,16 +149,22 @@ def extension(self) -> str:
def fmt(self) -> str:
raise NotImplementedError

@property
@abstractmethod
def get_bbox(self) -> BoundingBox2D | BoundingBox3D | None:
def layout(self) -> Layout:
raise NotImplementedError

@property
@abstractmethod
def get_spec(self) -> AnySpecification | None:
def table_index(self) -> list[str] | None:
raise NotImplementedError

@abstractmethod
def get_objectdata(self) -> DerivedObjectDescriptor:
def get_bbox(self) -> BoundingBox2D | BoundingBox3D | None:
raise NotImplementedError

@abstractmethod
def get_spec(self) -> AnySpecification | None:
raise NotImplementedError

def get_metadata(self) -> AnyContent | UnsetAnyContent:
Expand Down Expand Up @@ -200,7 +199,7 @@ def _get_validated_content(self, content: str | dict | None) -> AllowedContent:
{"content": ContentEnum(usecontent), "content_incl_specific": content}
)

def _get_named_stratigraphy(self) -> DerivedNamedStratigraphy:
def _get_named_stratigraphy(self) -> NamedStratigraphy:
"""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 @@ -219,10 +218,10 @@ def _get_named_stratigraphy(self) -> DerivedNamedStratigraphy:
stratigraphy = self.dataio.config.get("stratigraphy", {})

if name not in stratigraphy:
return DerivedNamedStratigraphy(name=name)
return NamedStratigraphy(name=name)

named_stratigraphy = stratigraphy.get(name)
rv = DerivedNamedStratigraphy(
rv = NamedStratigraphy(
name=named_stratigraphy.get("name", name),
alias=named_stratigraphy.get("alias", []),
stratigraphic=named_stratigraphy.get("stratigraphic", False),
Expand Down
18 changes: 9 additions & 9 deletions src/fmu/dataio/providers/objectdata/_faultroom.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
from dataclasses import dataclass
from typing import Final

from fmu.dataio._definitions import ExportFolder, ValidFormats
from fmu.dataio._definitions import ExportFolder, Layout, ValidFormats
from fmu.dataio._logging import null_logger
from fmu.dataio.datastructure.meta.content import BoundingBox3D
from fmu.dataio.datastructure.meta.enums import FMUClassEnum
from fmu.dataio.datastructure.meta.specification import FaultRoomSurfaceSpecification
from fmu.dataio.readers import FaultRoomSurface

from ._base import (
DerivedObjectDescriptor,
ObjectDataProvider,
)

Expand All @@ -38,6 +37,14 @@ def extension(self) -> str:
def fmt(self) -> str:
return self.dataio.dict_fformat

@property
def layout(self) -> Layout:
return Layout.faultroom_triangulated

@property
def table_index(self) -> None:
"""Return the table index."""

def get_bbox(self) -> BoundingBox3D:
"""Derive data.bbox for FaultRoomSurface."""
logger.info("Get bbox for FaultRoomSurface")
Expand All @@ -61,10 +68,3 @@ def get_spec(self) -> FaultRoomSurfaceSpecification:
properties=self.obj.properties,
name=self.obj.name,
)

def get_objectdata(self) -> DerivedObjectDescriptor:
"""Derive object data for FaultRoomSurface"""
return DerivedObjectDescriptor(
layout="faultroom_triangulated",
table_index=None,
)
18 changes: 9 additions & 9 deletions src/fmu/dataio/providers/objectdata/_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,12 @@
import pandas as pd
import xtgeo

from fmu.dataio._definitions import ExportFolder, ValidFormats
from fmu.dataio._definitions import ExportFolder, Layout, ValidFormats
from fmu.dataio._logging import null_logger
from fmu.dataio.datastructure.meta.enums import FMUClassEnum
from fmu.dataio.readers import FaultRoomSurface

from ._base import (
DerivedObjectDescriptor,
ObjectDataProvider,
)
from ._faultroom import FaultRoomSurfaceProvider
Expand Down Expand Up @@ -180,15 +179,16 @@ def extension(self) -> str:
def fmt(self) -> str:
return self.dataio.dict_fformat

@property
def layout(self) -> Layout:
return Layout.dictionary

@property
def table_index(self) -> None:
"""Return the table index."""

def get_bbox(self) -> None:
"""Derive data.bbox for dict."""

def get_spec(self) -> None:
"""Derive data.spec for dict."""

def get_objectdata(self) -> DerivedObjectDescriptor:
"""Derive object data for dict."""
return DerivedObjectDescriptor(
layout="dictionary",
table_index=None,
)
36 changes: 19 additions & 17 deletions src/fmu/dataio/providers/objectdata/_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
from fmu.dataio._definitions import (
STANDARD_TABLE_INDEX_COLUMNS,
ExportFolder,
Layout,
ValidFormats,
)
from fmu.dataio._logging import null_logger
from fmu.dataio.datastructure.meta.enums import FMUClassEnum
from fmu.dataio.datastructure.meta.specification import TableSpecification

from ._base import (
DerivedObjectDescriptor,
ObjectDataProvider,
)

Expand Down Expand Up @@ -80,6 +80,15 @@ def extension(self) -> str:
def fmt(self) -> str:
return self.dataio.table_fformat

@property
def layout(self) -> Layout:
return Layout.table

@property
def table_index(self) -> list[str]:
"""Return the table index."""
return _derive_index(self.dataio.table_index, list(self.obj.columns))

def get_bbox(self) -> None:
"""Derive data.bbox for pd.DataFrame."""

Expand All @@ -91,14 +100,6 @@ def get_spec(self) -> TableSpecification:
size=int(self.obj.size),
)

def get_objectdata(self) -> DerivedObjectDescriptor:
"""Derive object data for pd.DataFrame."""
table_index = _derive_index(self.dataio.table_index, list(self.obj.columns))
return DerivedObjectDescriptor(
layout="table",
table_index=table_index,
)


@dataclass
class ArrowTableDataProvider(ObjectDataProvider):
Expand All @@ -120,6 +121,15 @@ def extension(self) -> str:
def fmt(self) -> str:
return self.dataio.arrow_fformat

@property
def layout(self) -> Layout:
return Layout.table

@property
def table_index(self) -> list[str]:
"""Return the table index."""
return _derive_index(self.dataio.table_index, list(self.obj.column_names))

def get_bbox(self) -> None:
"""Derive data.bbox for pyarrow.Table."""

Expand All @@ -130,11 +140,3 @@ def get_spec(self) -> TableSpecification:
columns=list(self.obj.column_names),
size=self.obj.num_columns * self.obj.num_rows,
)

def get_objectdata(self) -> DerivedObjectDescriptor:
"""Derive object data from pyarrow.Table."""
table_index = _derive_index(self.dataio.table_index, self.obj.column_names)
return DerivedObjectDescriptor(
layout="table",
table_index=table_index,
)
Loading

0 comments on commit eae2ea1

Please sign in to comment.