mirror of
https://github.com/p2p-ld/numpydantic.git
synced 2024-11-14 10:44:28 +00:00
add ability to dump proxy classes to arrays, tests for doing so and json dumping
This commit is contained in:
parent
99a6571578
commit
2cb09076fd
4 changed files with 44 additions and 3 deletions
|
@ -134,10 +134,17 @@ class H5Proxy:
|
||||||
else:
|
else:
|
||||||
return obj.dtype[self.field]
|
return obj.dtype[self.field]
|
||||||
|
|
||||||
|
def __array__(self) -> np.ndarray:
|
||||||
|
"""To a numpy array"""
|
||||||
|
with h5py.File(self.file, "r") as h5f:
|
||||||
|
obj = h5f.get(self.path)
|
||||||
|
return obj[:]
|
||||||
|
|
||||||
def __getattr__(self, item: str):
|
def __getattr__(self, item: str):
|
||||||
with h5py.File(self.file, "r") as h5f:
|
with h5py.File(self.file, "r") as h5f:
|
||||||
obj = h5f.get(self.path)
|
obj = h5f.get(self.path)
|
||||||
return getattr(obj, item)
|
val = getattr(obj, item)
|
||||||
|
return val
|
||||||
|
|
||||||
def __getitem__(
|
def __getitem__(
|
||||||
self, item: Union[int, slice, Tuple[Union[int, slice], ...]]
|
self, item: Union[int, slice, Tuple[Union[int, slice], ...]]
|
||||||
|
|
|
@ -137,6 +137,10 @@ class VideoProxy:
|
||||||
slice_ = slice(0, slice_.stop, slice_.step)
|
slice_ = slice(0, slice_.stop, slice_.step)
|
||||||
return slice_
|
return slice_
|
||||||
|
|
||||||
|
def __array__(self) -> np.ndarray:
|
||||||
|
"""Whole video as a numpy array"""
|
||||||
|
return self[:]
|
||||||
|
|
||||||
def __getitem__(self, item: Union[int, slice, tuple]) -> np.ndarray:
|
def __getitem__(self, item: Union[int, slice, tuple]) -> np.ndarray:
|
||||||
if isinstance(item, int):
|
if isinstance(item, int):
|
||||||
# want a single frame
|
# want a single frame
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from typing import Tuple, Callable
|
from typing import Callable, Tuple, Type
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import dask.array as da
|
import dask.array as da
|
||||||
import zarr
|
import zarr
|
||||||
|
@ -32,7 +32,7 @@ from numpydantic import interface, NDArray
|
||||||
"video",
|
"video",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def interface_type(request) -> Tuple[NDArray, interface.Interface]:
|
def interface_type(request) -> Tuple[NDArray, Type[interface.Interface]]:
|
||||||
"""
|
"""
|
||||||
Test cases for each interface's ``check`` method - each input should match the
|
Test cases for each interface's ``check`` method - each input should match the
|
||||||
provided interface and that interface only
|
provided interface and that interface only
|
||||||
|
|
|
@ -2,6 +2,10 @@
|
||||||
Tests that should be applied to all interfaces
|
Tests that should be applied to all interfaces
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from typing import Callable
|
||||||
|
import numpy as np
|
||||||
|
from numpydantic.interface import Interface
|
||||||
|
|
||||||
|
|
||||||
def test_interface_revalidate(all_interfaces):
|
def test_interface_revalidate(all_interfaces):
|
||||||
"""
|
"""
|
||||||
|
@ -10,3 +14,29 @@ def test_interface_revalidate(all_interfaces):
|
||||||
See: https://github.com/p2p-ld/numpydantic/pull/14
|
See: https://github.com/p2p-ld/numpydantic/pull/14
|
||||||
"""
|
"""
|
||||||
_ = type(all_interfaces)(array=all_interfaces.array)
|
_ = type(all_interfaces)(array=all_interfaces.array)
|
||||||
|
|
||||||
|
|
||||||
|
def test_interface_rematch(interface_type):
|
||||||
|
"""
|
||||||
|
All interfaces should match the results of the object they return after validation
|
||||||
|
"""
|
||||||
|
array, interface = interface_type
|
||||||
|
if isinstance(array, Callable):
|
||||||
|
array = array()
|
||||||
|
|
||||||
|
assert Interface.match(interface().validate(array)) is interface
|
||||||
|
|
||||||
|
|
||||||
|
def test_interface_to_numpy_array(all_interfaces):
|
||||||
|
"""
|
||||||
|
All interfaces should be able to have the output of their validation stage
|
||||||
|
coerced to a numpy array with np.array()
|
||||||
|
"""
|
||||||
|
_ = np.array(all_interfaces.array)
|
||||||
|
|
||||||
|
|
||||||
|
def test_interface_dump_json(all_interfaces):
|
||||||
|
"""
|
||||||
|
All interfaces should be able to dump to json
|
||||||
|
"""
|
||||||
|
all_interfaces.model_dump_json()
|
||||||
|
|
Loading…
Reference in a new issue