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 19, 2024
1 parent 704f5cb commit 8ed7dfd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/fmu/dataio/export/rms/inplace_volumes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,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 @@ -171,7 +172,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 @@ -203,8 +204,12 @@ def _validate_table(self) -> None:
"""
_logger.debug("Validating the dataframe...")

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

# check that one of oil and gas fluids are present
if not (has_oil or has_gas):
Expand Down Expand Up @@ -233,6 +238,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 @@ -7,6 +7,7 @@
import numpy as np
import pandas as pd
import pytest
from pydantic import ValidationError

import fmu.dataio as dataio
from fmu.dataio._logging import null_logger
Expand Down Expand Up @@ -262,6 +263,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 8ed7dfd

Please sign in to comment.