Skip to content

Commit

Permalink
add assertion for correct type; quality fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kaitj committed Aug 30, 2023
1 parent b4b4926 commit f29da5d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
27 changes: 25 additions & 2 deletions afids_utils/tests/test_transforms.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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())
Expand All @@ -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)
4 changes: 4 additions & 0 deletions afids_utils/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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])
Expand Down

0 comments on commit f29da5d

Please sign in to comment.