Skip to content

Commit

Permalink
ENH: validate inplace volumes with Pydantic
Browse files Browse the repository at this point in the history
  • Loading branch information
mferrera committed Dec 20, 2024
1 parent 7e8d4e0 commit b3af680
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
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

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

0 comments on commit b3af680

Please sign in to comment.