diff --git a/afids_utils/tests/test_transforms.py b/afids_utils/tests/test_transforms.py index c45d0e76..11e107f4 100644 --- a/afids_utils/tests/test_transforms.py +++ b/afids_utils/tests/test_transforms.py @@ -1,16 +1,17 @@ from __future__ import annotations import numpy as np +import pytest from hypothesis import HealthCheck, given, settings from numpy.typing import NDArray from afids_utils.afids import AfidPosition, AfidVoxel from afids_utils.tests.strategies import ( affine_xfms, - world_coords, voxel_coords, + world_coords, ) -from afids_utils.transforms import world_to_voxel, voxel_to_world +from afids_utils.transforms import voxel_to_world, world_to_voxel class TestAfidWorld2Voxel: @@ -30,6 +31,17 @@ def test_world_to_voxel_xfm( assert isinstance(voxel_coord.j, np.int64) assert isinstance(voxel_coord.k, np.int64) + @given(voxel_coord=voxel_coords(), nii_affine=affine_xfms()) + @settings( + deadline=400, + suppress_health_check=[HealthCheck.function_scoped_fixture], + ) + def test_invalid_world_type( + self, voxel_coord: AfidVoxel, nii_affine: NDArray[np.float_] + ): + with pytest.raises(TypeError, match="Not an AfidPosition.*"): + world_to_voxel(voxel_coord, nii_affine) + class TestAfidVoxel2World: @given(voxel_coord=voxel_coords(), nii_affine=affine_xfms()) @@ -47,3 +59,14 @@ def test_voxel_to_world_xfm( assert isinstance(world_coord.x, np.float64) assert isinstance(world_coord.y, np.float64) assert isinstance(world_coord.z, np.float64) + + @given(world_coord=world_coords(), nii_affine=affine_xfms()) + @settings( + deadline=400, + suppress_health_check=[HealthCheck.function_scoped_fixture], + ) + def test_invalid_voxel_type( + self, world_coord: AfidPosition, nii_affine: NDArray[np.float_] + ): + with pytest.raises(TypeError, match="Not an AfidVoxel.*"): + voxel_to_world(world_coord, nii_affine) diff --git a/afids_utils/transforms.py b/afids_utils/transforms.py index e0994b80..e3d7b575 100644 --- a/afids_utils/transforms.py +++ b/afids_utils/transforms.py @@ -29,6 +29,8 @@ def world_to_voxel( AfidVoxel Object containing transformed integer voxel coordinates (i, j, k) """ + if not isinstance(afid_world, AfidPosition): + raise TypeError("Not an AfidPosition object") # Put into numpy array for easier computation world_pos = np.asarray([afid_world.x, afid_world.y, afid_world.z]) @@ -69,6 +71,8 @@ def voxel_to_world( Object containing approximate floating-point spatial coordinates (x, y, z) """ + if not isinstance(afid_voxel, AfidVoxel): + raise TypeError("Not an AfidVoxel object") # Put into numpy array for easier computation voxel_pos = np.asarray([afid_voxel.i, afid_voxel.j, afid_voxel.k])