diff --git a/pyproject.toml b/pyproject.toml index f0e79f7..8ab0e68 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,6 +30,7 @@ classifiers = [ ] dependencies = [ "array-api-compat>=1.11.0", + "beartype>=0.21.0", "einops>=0.8.0", "jaxtyping>=0.3.4", ] @@ -70,7 +71,6 @@ dev = [ test = [ "array-api-extra>=0.10.0", "array-api-strict>=2.0.0", - "beartype>=0.21.0", "h5py>=3.12.1", "matplotlib>=3.9.4", "numpy>=2.0.2", diff --git a/src/mach/geometry.py b/src/mach/geometry.py index df6b9ca..23b1107 100644 --- a/src/mach/geometry.py +++ b/src/mach/geometry.py @@ -23,11 +23,13 @@ import numbers import types -from jaxtyping import Real +from beartype import beartype +from jaxtyping import Real, jaxtyped from mach._array_api import Array, _ArrayNamespace, array_namespace +@jaxtyped(typechecker=beartype) def ultrasound_angles_to_cartesian( azimuth_rad: Real[Array, " *angles"] | float | int, elevation_rad: Real[Array, " *angles"] | float | int, @@ -86,6 +88,7 @@ def ultrasound_angles_to_cartesian( return result +@jaxtyped(typechecker=beartype) def spherical_to_cartesian( theta_rad: Real[Array, " *angles"] | float | int, phi_rad: Real[Array, " *angles"] | float | int, diff --git a/src/mach/io/must.py b/src/mach/io/must.py index ef64a94..3ccdfb3 100644 --- a/src/mach/io/must.py +++ b/src/mach/io/must.py @@ -68,7 +68,10 @@ def extract_pymust_params(mat_data: dict) -> dict: return param -def linear_probe_positions(n_elements: int, pitch: float) -> Float[np.ndarray, "{n_elements} xyz=3"]: +def linear_probe_positions( + n_elements: int, + pitch: float | int, +) -> Float[np.ndarray, "{n_elements} xyz=3"]: """Generate element positions for a linear array. Args: diff --git a/src/mach/wavefront.py b/src/mach/wavefront.py index da9ac92..ca7755c 100644 --- a/src/mach/wavefront.py +++ b/src/mach/wavefront.py @@ -2,7 +2,8 @@ from typing import NamedTuple -from jaxtyping import Float, Real +from beartype import beartype +from jaxtyping import Real, jaxtyped from mach._array_api import Array, array_namespace, vector_norm @@ -21,6 +22,7 @@ class _ApertureGeometry(NamedTuple): elevation_half_width_m: Array +@jaxtyped(typechecker=beartype) def plane( origin_m: Real[Array, "xyz=3"], points_m: Real[Array, "*points xyz=3"], @@ -298,6 +300,7 @@ def _unified_spherical_bounded( ) +@jaxtyped(typechecker=beartype) def spherical( origin_m: Real[Array, "xyz=3"], points_m: Real[Array, "*points xyz=3"], @@ -372,12 +375,13 @@ def spherical( ) +@jaxtyped(typechecker=beartype) def earliest_arrival( element_positions_m: Real[Array, "elements xyz=3"], element_delays_s: Real[Array, " elements"], - points_m: Float[Array, "*points xyz=3"], + points_m: Real[Array, "*points xyz=3"], *, - sound_speed_m_s: float, + sound_speed_m_s: float | int, element_chunk_size: int = _DEFAULT_ELEMENT_CHUNK, point_chunk_size: int = _DEFAULT_POINT_CHUNK, ) -> Real[Array, "*points"]: diff --git a/tests/test_geometry.py b/tests/test_geometry.py index 76ba217..55be01d 100644 --- a/tests/test_geometry.py +++ b/tests/test_geometry.py @@ -4,15 +4,9 @@ import array_api_strict as xp import pytest -from beartype import beartype -from jaxtyping import jaxtyped +from jaxtyping import TypeCheckError -from mach.geometry import spherical_to_cartesian as spherical_unchecked -from mach.geometry import ultrasound_angles_to_cartesian as az_el_unchecked - -# type-check functions during unit tests -ultrasound_angles_to_cartesian = jaxtyped(typechecker=beartype)(az_el_unchecked) -spherical_to_cartesian = jaxtyped(typechecker=beartype)(spherical_unchecked) +from mach.geometry import spherical_to_cartesian, ultrasound_angles_to_cartesian @pytest.mark.no_cuda @@ -166,6 +160,13 @@ def test_mixed_scalar_array_inputs(self): assert float(result[0, 2]) == pytest.approx(1.0) # First point z-coordinate assert float(result[1, 0]) == pytest.approx(1.0) # Second point x-coordinate + def test_rejects_mismatched_angle_shapes(self): + with pytest.raises(TypeCheckError): + ultrasound_angles_to_cartesian( + xp.asarray([0.0, 0.1]), + xp.asarray([0.0, 0.1, 0.2]), + ) + @pytest.mark.no_cuda class TestSphericalToCartesian: diff --git a/tests/test_wavefront.py b/tests/test_wavefront.py index 1a8c231..8913b86 100644 --- a/tests/test_wavefront.py +++ b/tests/test_wavefront.py @@ -5,17 +5,9 @@ import array_api_strict as xp import pytest from array_api_extra.testing import assert_close -from beartype import beartype -from jaxtyping import jaxtyped +from jaxtyping import TypeCheckError -from mach.wavefront import earliest_arrival as earliest_arrival_unchecked -from mach.wavefront import plane as plane_unchecked -from mach.wavefront import spherical as spherical_unchecked - -# type-check functions during unit tests -earliest_arrival = jaxtyped(typechecker=beartype)(earliest_arrival_unchecked) -plane = jaxtyped(typechecker=beartype)(plane_unchecked) -spherical = jaxtyped(typechecker=beartype)(spherical_unchecked) +from mach.wavefront import earliest_arrival, plane, spherical @pytest.mark.no_cuda @@ -97,6 +89,14 @@ def test_non_unit_direction_raises_error(self): with pytest.raises(ValueError, match="direction must be a unit vector"): plane(origin, points, direction) + def test_rejects_non_xyz_origin(self): + with pytest.raises(TypeCheckError): + plane( + xp.asarray([0.0, 0.0]), + xp.asarray([[0.0, 0.0, 1.0]]), + xp.asarray([0.0, 0.0, 1.0]), + ) + def test_different_origins(self): """Test with non-zero origin.""" origin = xp.asarray([2.0, 3.0, 1.0]) @@ -482,3 +482,21 @@ def test_rejects_nonpositive_sound_speed(self): xp.asarray([[0.0, 0.0, 1.0]]), sound_speed_m_s=0.0, ) + + def test_rejects_non_xyz_element_positions(self): + with pytest.raises(TypeCheckError): + earliest_arrival( + xp.asarray([[0.0, 0.0]]), + xp.asarray([0.0]), + xp.asarray([[0.0, 0.0, 1.0]]), + sound_speed_m_s=1540.0, + ) + + def test_rejects_mismatched_element_counts(self): + with pytest.raises(TypeCheckError): + earliest_arrival( + xp.asarray([[0.0, 0.0, 0.0], [1.0, 0.0, 0.0]]), + xp.asarray([0.0]), + xp.asarray([[0.0, 0.0, 1.0]]), + sound_speed_m_s=1540.0, + ) diff --git a/uv.lock b/uv.lock index 11d62e0..6e5d32c 100644 --- a/uv.lock +++ b/uv.lock @@ -1623,6 +1623,7 @@ source = { editable = "." } default-groups = "all" dependencies = [ { name = "array-api-compat" }, + { name = "beartype" }, { name = "einops" }, { name = "jaxtyping" }, ] @@ -1703,7 +1704,6 @@ docs = [ profile = [ { name = "array-api-extra" }, { name = "array-api-strict" }, - { name = "beartype" }, { name = "h5py" }, { name = "matplotlib" }, { name = "numpy" }, @@ -1720,7 +1720,6 @@ profile = [ test = [ { name = "array-api-extra" }, { name = "array-api-strict" }, - { name = "beartype" }, { name = "h5py" }, { name = "matplotlib" }, { name = "numpy" }, @@ -1737,6 +1736,7 @@ test = [ [package.metadata] requires-dist = [ { name = "array-api-compat", specifier = ">=1.11.0" }, + { name = "beartype", specifier = ">=0.21.0" }, { name = "colorcet", marker = "extra == 'vis'", specifier = ">=3.1.0" }, { name = "cupy-cuda12x", marker = "extra == 'all'", specifier = ">=14.0.0" }, { name = "einops", specifier = ">=0.8.0" }, @@ -1798,7 +1798,6 @@ docs = [ profile = [ { name = "array-api-extra", specifier = ">=0.10.0" }, { name = "array-api-strict", specifier = ">=2.0.0" }, - { name = "beartype", specifier = ">=0.21.0" }, { name = "h5py", specifier = ">=3.12.1" }, { name = "matplotlib", specifier = ">=3.9.4" }, { name = "numpy", specifier = ">=2.0.2" }, @@ -1815,7 +1814,6 @@ profile = [ test = [ { name = "array-api-extra", specifier = ">=0.10.0" }, { name = "array-api-strict", specifier = ">=2.0.0" }, - { name = "beartype", specifier = ">=0.21.0" }, { name = "h5py", specifier = ">=3.12.1" }, { name = "matplotlib", specifier = ">=3.9.4" }, { name = "numpy", specifier = ">=2.0.2" },