numpydantic/tests/fixtures/generation.py

65 lines
1.9 KiB
Python
Raw Normal View History

2024-10-04 01:51:34 +00:00
from pathlib import Path
from typing import Callable, Tuple, Union
import numpy as np
import pytest
2024-04-30 02:49:38 +00:00
import zarr
from numpydantic.interface.hdf5 import H5ArrayPath
2024-04-30 02:49:38 +00:00
from numpydantic.interface.zarr import ZarrArrayPath
from numpydantic.testing import ValidationCase
from numpydantic.testing.interfaces import HDF5Case, HDF5CompoundCase, VideoCase
2024-05-15 03:18:04 +00:00
@pytest.fixture(scope="function")
def hdf5_array(
2024-09-27 02:58:27 +00:00
request, tmp_output_dir_func
) -> Callable[[Tuple[int, ...], Union[np.dtype, type]], H5ArrayPath]:
def _hdf5_array(
shape: Tuple[int, ...] = (10, 10),
dtype: Union[np.dtype, type] = float,
compound: bool = False,
) -> H5ArrayPath:
if compound:
array: H5ArrayPath = HDF5CompoundCase.make_array(
shape, dtype, tmp_output_dir_func
)
return array
else:
return HDF5Case.make_array(shape, dtype, tmp_output_dir_func)
return _hdf5_array
2024-04-30 02:49:38 +00:00
@pytest.fixture(scope="function")
def zarr_nested_array(tmp_output_dir_func) -> ZarrArrayPath:
"""Zarr array within a nested array"""
file = tmp_output_dir_func / "nested.zarr"
path = "a/b/c"
root = zarr.open(str(file), mode="w")
2024-10-04 02:57:54 +00:00
_ = root.zeros(path, shape=(100, 100), chunks=(10, 10))
2024-04-30 02:49:38 +00:00
return ZarrArrayPath(file=file, path=path)
@pytest.fixture(scope="function")
def zarr_array(tmp_output_dir_func) -> Path:
file = tmp_output_dir_func / "array.zarr"
array = zarr.open(str(file), mode="w", shape=(100, 100), chunks=(10, 10))
array[:] = 0
return file
@pytest.fixture(scope="function")
2024-09-26 01:15:27 +00:00
def avi_video(tmp_output_dir_func) -> Callable[[Tuple[int, int], int, bool], Path]:
def _make_video(shape=(100, 50), frames=10, is_color=True) -> Path:
shape = (frames, *shape)
if is_color:
shape = (*shape, 3)
return VideoCase.array_from_case(
ValidationCase(shape=shape, dtype=np.uint8), tmp_output_dir_func
)
return _make_video