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: validate inplace volumes with Pydantic #934

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 7 additions & 5 deletions src/fmu/dataio/export/rms/inplace_volumes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import fmu.dataio as dio
from fmu.dataio._logging import null_logger
from fmu.dataio._model.enums import Classification
from fmu.dataio._products.inplace_volumes import InplaceVolumesResult
from fmu.dataio.export import _enums
from fmu.dataio.export._decorators import experimental
from fmu.dataio.export._export_result import ExportResult, ExportResultItem
Expand Down Expand Up @@ -210,7 +211,7 @@ def _transform_and_add_fluid_column_to_table(
)

# add the fluid as column entry instead
fluid_table[_enums.InplaceVolumes.FLUID_COLUMN] = fluid
fluid_table[_enums.InplaceVolumes.FLUID_COLUMN.value] = fluid
Copy link
Collaborator

Choose a reason for hiding this comment

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

enums drive me crazy sometimes..

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Funny thing is that it worked just fine. Terrible thing is that it worked just fine 😅


tables.append(fluid_table)

Expand Down Expand Up @@ -244,12 +245,10 @@ def _validate_table(self) -> None:
_logger.debug("Validating the dataframe...")

has_oil = (
_enums.InplaceVolumes.Fluid.oil.value
in self._dataframe[_enums.InplaceVolumes.FLUID_COLUMN].values
"oil" in self._dataframe[_enums.InplaceVolumes.FLUID_COLUMN.value].values
)
has_gas = (
_enums.InplaceVolumes.Fluid.gas.value
in self._dataframe[_enums.InplaceVolumes.FLUID_COLUMN].values
"gas" in self._dataframe[_enums.InplaceVolumes.FLUID_COLUMN.value].values
)

# check that one of oil and gas fluids are present
Expand Down Expand Up @@ -285,6 +284,9 @@ def _validate_table(self) -> None:
"rerun the volumetric job before export."
)

df = self._dataframe.replace(np.nan, None).to_dict(orient="records")
InplaceVolumesResult.model_validate(df)

def _export_volume_table(self) -> ExportResult:
"""Do the actual volume table export using dataio setup."""

Expand Down
17 changes: 17 additions & 0 deletions tests/test_export_rms/test_export_rms_volumetrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pandas as pd
import pyarrow.parquet as pq
import pytest
from pydantic import ValidationError

import fmu.dataio as dataio
from fmu.dataio._logging import null_logger
Expand Down Expand Up @@ -425,6 +426,22 @@ def test_validate_table_has_gas_and_giip(exportvolumetrics, voltable_standard):
exportvolumetrics._validate_table()


def test_validate_table_against_pydantic_model_before_export(
exportvolumetrics, voltable_standard
):
"""Test that the validation fails if the volumes table does not conform to the
Pydantic model specifying the result."""

df = voltable_standard.copy()
exportvolumetrics._dataframe = df
exportvolumetrics._validate_table()

df["PORV"] = df["PORV"].replace(0.0, "a")
exportvolumetrics._dataframe = df
with pytest.raises(ValidationError, match="Input should be a valid number"):
exportvolumetrics._validate_table()


@inside_rms
def test_rms_volumetrics_export_config_missing(
mock_project_variable,
Expand Down
Loading