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

Add dtype compatibility check #776

Merged
merged 8 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions docs/api_reference/python-api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ SPDX-License-Identifier: MPL-2.0
.. autoclass:: power_grid_model.PowerGridModel
:show-inheritance:
.. autofunction:: power_grid_model.initialize_array
.. autoclass:: power_grid_model.power_grid_meta_data
nitbharambe marked this conversation as resolved.
Show resolved Hide resolved
```

## enum
Expand Down
27 changes: 26 additions & 1 deletion src/power_grid_model/core/power_grid_dataset.py
nitbharambe marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@
Power grid model raw dataset handler
"""

import warnings
from typing import Any, Mapping, Optional

from power_grid_model._utils import get_dataset_type, is_columnar, is_nan_or_equivalent, process_data_filter
from power_grid_model._utils import (
_extract_data_from_component_data,
get_dataset_type,
is_columnar,
is_nan_or_equivalent,
process_data_filter,
)
from power_grid_model.core.buffer_handling import (
BufferProperties,
CAttributeBuffer,
Expand Down Expand Up @@ -231,6 +238,7 @@ def _add_component_data(self, component: ComponentType, data: ComponentData, all
return

self._validate_properties(data, self._schema[component])
self._validate_dtypes_compatibility(data, self._schema[component])
c_buffer = get_buffer_view(data, self._schema[component], self._is_batch, self._batch_size)
self._buffer_views.append(c_buffer)
self._register_buffer(component, c_buffer)
Expand Down Expand Up @@ -266,6 +274,23 @@ def _validate_properties(self, data: ComponentData, schema: ComponentMetaData):
if properties.batch_size != self._batch_size:
raise ValueError(f"Dataset must have a consistent batch size across all components. {VALIDATOR_MSG}")

def _validate_dtypes_compatibility(self, data: ComponentData, schema: ComponentMetaData):
sub_data = _extract_data_from_component_data(data)
if is_columnar(data):
for attr, array in sub_data.items():
if (
schema.dtype.names is None
or attr not in schema.dtype.names
or (schema.dtype[attr].shape == (3,) and array.shape[-1] != 3)
):
raise TypeError("Given data has a different schema than supported.")
if array.dtype.base != schema.dtype[attr].base:
warnings.warn(
TonyXiang8787 marked this conversation as resolved.
Show resolved Hide resolved
f"Data type for attribute {attr} does not match schema. {VALIDATOR_MSG}", DeprecationWarning
)
elif sub_data.dtype != schema.dtype:
warnings.warn("Data type does not match schema. {VALIDATOR_MSG}", DeprecationWarning)

def __del__(self):
pgc.destroy_dataset_mutable(self._mutable_dataset)

Expand Down
36 changes: 36 additions & 0 deletions tests/unit/test_data_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
#
# SPDX-License-Identifier: MPL-2.0

import warnings

import numpy as np
import pytest

from power_grid_model._utils import is_columnar
from power_grid_model.core.data_handling import create_output_data
from power_grid_model.core.dataset_definitions import ComponentType as CT, DatasetType as DT
from power_grid_model.core.power_grid_core import VoidPtr
from power_grid_model.core.power_grid_dataset import CMutableDataset
from power_grid_model.core.power_grid_meta import initialize_array


Expand Down Expand Up @@ -69,3 +73,35 @@ def test_create_output_data(output_component_types, is_batch, expected):
else:
assert actual[comp].keys() == expected[comp].keys()
assert all(actual[comp][attr].dtype == expected[comp][attr].dtype for attr in expected[comp])


def test_dtype_compatibility_check_normal():
nodes = initialize_array(DT.sym_output, CT.node, (1, 2))
nodes_ptr = nodes.ctypes.data_as(VoidPtr)

data = {CT.node: nodes}
mutable_dataset = CMutableDataset(data, DT.sym_output)
buffer_views = mutable_dataset.get_buffer_views()

assert buffer_views[0].data.value == nodes_ptr.value


def test_dtype_compatibility_check_compatible():
nodes = initialize_array(DT.sym_output, CT.node, 4)
nodes = nodes[::2]
nodes_ptr = nodes.ctypes.data_as(VoidPtr)

data = {CT.node: nodes}
with warnings.catch_warnings():
warnings.simplefilter("error")
mutable_dataset = CMutableDataset(data, DT.sym_output)
buffer_views = mutable_dataset.get_buffer_views()

assert buffer_views[0].data.value != nodes_ptr.value


def test_dtype_compatibility_check__error():
nodes = initialize_array(DT.sym_output, CT.node, (1, 2))
data = {CT.node: nodes.astype(nodes.dtype.newbyteorder("S"))}
with pytest.warns(DeprecationWarning):
CMutableDataset(data, DT.sym_output)
Loading