Skip to content

Commit 17b3f32

Browse files
ltetrelebrahimebrahim
authored andcommitted
Adding missing unit tests for solution analysis
Removed `calc_dist_from_focus`, the algorithm is now in `mask_focus` pre-commit hook
1 parent 576b54c commit 17b3f32

File tree

7 files changed

+186
-50
lines changed

7 files changed

+186
-50
lines changed

src/openlifu/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313
Pulse,
1414
Sequence,
1515
apod_methods,
16-
calc_dist_from_focus,
1716
delay_methods,
1817
focal_patterns,
1918
get_beamwidth,
20-
get_focus_matrix,
2119
mask_focus,
2220
offset_grid,
2321
)
@@ -64,9 +62,7 @@
6462
"focal_patterns",
6563
"delay_methods",
6664
"apod_methods",
67-
"calc_dist_from_focus",
6865
"get_beamwidth",
69-
"get_focus_matrix",
7066
"mask_focus",
7167
"offset_grid",
7268
"SimSetup",

src/openlifu/bf/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from .apod_methods import ApodizationMethod
2-
from .calc_dist_from_focus import calc_dist_from_focus
32
from .delay_methods import DelayMethod
43
from .focal_patterns import FocalPattern, SinglePoint, Wheel
54
from .get_beamwidth import get_beamwidth
@@ -17,7 +16,6 @@
1716
"Pulse",
1817
"Sequence",
1918
"offset_grid",
20-
"calc_dist_from_focus",
2119
"mask_focus",
2220
"get_beamwidth"
2321
]

src/openlifu/bf/calc_dist_from_focus.py

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/openlifu/bf/mask_focus.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import numpy as np
66
from xarray import Dataset
77

8-
from openlifu.bf import calc_dist_from_focus
8+
from openlifu.bf.offset_grid import offset_grid
99
from openlifu.geo import Point
1010
from openlifu.util.units import get_ndgrid_from_arr, rescale_coords
1111

@@ -47,7 +47,13 @@ def mask_focus(
4747
focus.rescale(units)
4848

4949
# Calculate distances from the focus for each point in coords and compare with distance limit
50-
m = calc_dist_from_focus(data_arr_rescaled, focus, aspect_ratio)
50+
# The distance is calculated by first transforming the coordinate system so that the focus point is on
51+
# the z' axis, adjusting the x and y axes to be orthogonal to the z' axis, and then calculating the distance
52+
# e.g. d = sqrt(((x'-x0')/ax)^2 + ((y'-y0')/ay)^2 + ((z'-z0')/az)^2). This is useful for calculating how far
53+
# away from an oblong focal spot each point is.
54+
ogrid = offset_grid(data_arr_rescaled, focus)
55+
ogrid_aspect_corrected = ogrid*aspect_ratio
56+
m = np.sqrt(np.sum(ogrid_aspect_corrected**2, axis=-1))
5157

5258
# mask based on distance
5359
if mask_op is MaskOp.GREATER:

tests/test_mask_focus.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import numpy as np
2+
import pytest
3+
from xarray import DataArray, Dataset
4+
5+
from openlifu.bf import mask_focus
6+
from openlifu.geo import Point
7+
8+
9+
@pytest.fixture()
10+
def example_focus() -> DataArray:
11+
return Point(
12+
id="test_focus_point",
13+
radius=1.,
14+
position=np.array([0.5, 0.5, 0.5]),
15+
dims=("x", "y", "z"),
16+
units="mm"
17+
)
18+
19+
@pytest.fixture()
20+
def example_xarr() -> DataArray:
21+
rng = np.random.default_rng(147)
22+
return Dataset(
23+
{
24+
'p': DataArray(
25+
data=rng.random((3, 2, 3)),
26+
dims=["x", "y", "z"],
27+
attrs={'units': "Pa"}
28+
)
29+
},
30+
coords={
31+
'x': DataArray(dims=["x"], data=np.linspace(0, 1, 3), attrs={'units': "mm"}),
32+
'y': DataArray(dims=["y"], data=np.linspace(0, 1, 2), attrs={'units': "mm"}),
33+
'z': DataArray(dims=["z"], data=np.linspace(0, 1, 3), attrs={'units': "mm"})
34+
}
35+
)
36+
37+
def test_mask_focus(example_xarr: Dataset, example_focus: Point):
38+
"""Test that the distance grid from the focus point is correct."""
39+
expected = np.array(
40+
[[[ True, True, True],
41+
[ True, True, False]],
42+
43+
[[ True, True, True],
44+
[True, False, False]],
45+
46+
[[ True, True, False],
47+
[ False, False, False]]])
48+
mask = mask_focus(example_xarr, example_focus, distance=0.01)
49+
50+
np.testing.assert_equal(mask, expected)

tests/test_offset_grid.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import numpy as np
2+
import pytest
3+
from xarray import DataArray, Dataset
4+
5+
from openlifu.bf import offset_grid
6+
from openlifu.geo import Point
7+
8+
9+
@pytest.fixture()
10+
def example_focus() -> DataArray:
11+
return Point(
12+
id="test_focus_point",
13+
radius=1.,
14+
position=np.array([0.5, 0.5, 0.5]),
15+
dims=("x", "y", "z"),
16+
units="mm"
17+
)
18+
19+
@pytest.fixture()
20+
def example_xarr() -> DataArray:
21+
rng = np.random.default_rng(147)
22+
return Dataset(
23+
{
24+
'p': DataArray(
25+
data=rng.random((3, 2, 3)),
26+
dims=["x", "y", "z"],
27+
attrs={'units': "Pa"}
28+
)
29+
},
30+
coords={
31+
'x': DataArray(dims=["x"], data=np.linspace(0, 1, 3), attrs={'units': "mm"}),
32+
'y': DataArray(dims=["y"], data=np.linspace(0, 1, 2), attrs={'units': "mm"}),
33+
'z': DataArray(dims=["z"], data=np.linspace(0, 1, 3), attrs={'units': "mm"})
34+
}
35+
)
36+
37+
def test_offset_grid(example_xarr: Dataset, example_focus: Point):
38+
"""Test that the distance grid from the focus point is correct."""
39+
expected = np.array([
40+
[[[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
41+
[-3.53553391e-01, -2.04124145e-01, 2.88675135e-01],
42+
[-7.07106781e-01, -4.08248290e-01, 5.77350269e-01]],
43+
44+
[[ 0.00000000e+00, 8.16496581e-01, 5.77350269e-01],
45+
[-3.53553391e-01, 6.12372436e-01, 8.66025404e-01],
46+
[-7.07106781e-01, 4.08248290e-01, 1.15470054e+00]]],
47+
48+
49+
[[[ 3.53553391e-01, -2.04124145e-01, 2.88675135e-01],
50+
[ 0.00000000e+00, -4.08248290e-01, 5.77350269e-01],
51+
[-3.53553391e-01, -6.12372436e-01, 8.66025404e-01]],
52+
53+
[[ 3.53553391e-01, 6.12372436e-01, 8.66025404e-01],
54+
[ 0.00000000e+00, 4.08248290e-01, 1.15470054e+00],
55+
[-3.53553391e-01, 2.04124145e-01, 1.44337567e+00]]],
56+
57+
58+
[[[ 7.07106781e-01, -4.08248290e-01, 5.77350269e-01],
59+
[ 3.53553391e-01, -6.12372436e-01, 8.66025404e-01],
60+
[ 0.00000000e+00, -8.16496581e-01, 1.15470054e+00]],
61+
62+
[[ 7.07106781e-01, 4.08248290e-01, 1.15470054e+00],
63+
[ 3.53553391e-01, 2.04124145e-01, 1.44337567e+00],
64+
[ 0.00000000e+00, 0.00000000e+00, 1.73205081e+00]]]])
65+
offset = offset_grid(example_xarr, example_focus)
66+
67+
np.testing.assert_almost_equal(offset, expected)

tests/test_units.py

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,36 @@
11
import numpy as np
22
import pytest
3+
from xarray import DataArray, Dataset
34

4-
from openlifu.util.units import getsiscale
5+
from openlifu.util.units import (
6+
get_ndgrid_from_arr,
7+
getsiscale,
8+
rescale_coords,
9+
rescale_data_arr,
10+
)
11+
12+
13+
@pytest.fixture()
14+
def example_xarr() -> Dataset:
15+
rng = np.random.default_rng(147)
16+
return Dataset(
17+
{
18+
'p': DataArray(
19+
data=rng.random((3, 2)),
20+
dims=["x", "y"],
21+
attrs={'units': "Pa"}
22+
),
23+
'it': DataArray(
24+
data=rng.random((3, 2)),
25+
dims=["x", "y"],
26+
attrs={'units': "W/cm^2"}
27+
)
28+
},
29+
coords={
30+
'x': DataArray(dims=["x"], data=np.linspace(0, 1, 3), attrs={'units': "m"}),
31+
'y': DataArray(dims=["y"], data=np.linspace(0, 1, 2), attrs={'units': "m"})
32+
}
33+
)
534

635

736
def test_getsiscale():
@@ -20,3 +49,34 @@ def test_getsiscale():
2049
assert getsiscale('MHz', 'frequency') == 1e6
2150
assert getsiscale('GHz', 'frequency') == 1e9
2251
assert getsiscale('THz', 'frequency') == 1e12
52+
53+
54+
def test_rescale_data_arr(example_xarr: Dataset):
55+
"""Test that an xarray data can be correctly rescaled."""
56+
expected_p = 1e-6 * example_xarr['p'].data
57+
expected_it = 1e4 * example_xarr['it'].data
58+
rescaled_p = rescale_data_arr(example_xarr['p'], units="MPa")
59+
rescaled_it = rescale_data_arr(example_xarr['it'], units="W/m^2")
60+
61+
np.testing.assert_almost_equal(rescaled_p, expected_p)
62+
np.testing.assert_almost_equal(rescaled_it, expected_it)
63+
64+
65+
def test_rescale_coords(example_xarr: Dataset):
66+
"""Test that an xarray coords can be correctly rescaled."""
67+
expected_x = 1e3 * example_xarr['p'].coords['x'].data
68+
expected_y = 1e3 * example_xarr['p'].coords['y'].data
69+
rescaled = rescale_coords(example_xarr['p'], units="mm")
70+
71+
np.testing.assert_almost_equal(rescaled['x'].data, expected_x)
72+
np.testing.assert_almost_equal(rescaled['y'].data, expected_y)
73+
74+
75+
def test_get_ndgrid_from_arr(example_xarr: Dataset):
76+
"""Test that an ndgrid can be constructed from the coordinates of an xarray."""
77+
expected_X = np.array([[0., 0.], [0.5, 0.5], [1., 1.]])
78+
expected_Y = np.array([[0., 1.], [0., 1.], [0., 1.]])
79+
expected = np.stack([expected_X, expected_Y], axis=-1)
80+
ndgrid = get_ndgrid_from_arr(example_xarr)
81+
82+
np.testing.assert_equal(ndgrid, expected)

0 commit comments

Comments
 (0)