Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ classifiers = [
]
dependencies = [
"array-api-compat>=1.11.0",
"beartype>=0.21.0",
"einops>=0.8.0",
"jaxtyping>=0.3.4",
]
Expand Down Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion src/mach/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion src/mach/io/must.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]:
Comment on lines +71 to +74
"""Generate element positions for a linear array.

Args:
Expand Down
10 changes: 7 additions & 3 deletions src/mach/wavefront.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"],
Expand Down Expand Up @@ -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"],
Expand Down Expand Up @@ -372,12 +375,13 @@ def spherical(
)


@jaxtyped(typechecker=beartype)
Comment thread
charlesbmi marked this conversation as resolved.
Resolved
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"]:
Expand Down
17 changes: 9 additions & 8 deletions tests/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
38 changes: 28 additions & 10 deletions tests/test_wavefront.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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,
)
6 changes: 2 additions & 4 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading