Skip to content

Commit

Permalink
BUG: Fix tests
Browse files Browse the repository at this point in the history
Fix GP model testing: `GaussianProcessModel` is now hosted in the
`model.dipy` module.

Fix bvals array dimensionality and make bvecs be unit vectors in angle
computation test.

Fix the pairwise angle computation doctests.

Test the case where the second argument to the pairwaise angle
computation function is `None`. Simplify the test parameterization as
the former corresponds to providing the same value for both gtab
arguments. Adapt the test so that we avoid making computations if the
second argument is `None`.
  • Loading branch information
jhlegarreta committed Jul 4, 2024
1 parent e75b6e5 commit 0c32def
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 20 deletions.
24 changes: 12 additions & 12 deletions src/eddymotion/model/dipy.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,20 +380,20 @@ def compute_pairwise_angles(
Examples
--------
>>> compute_pairwise_angles(
... ((1.0, -1.0), (0.0, 0.0), (0.0, 0.0)),
... closest_polarity=False,
... )[0, 1] # doctest: +ELLIPSIS
>>> from dipy.core.gradients import gradient_table
>>> bvecs = np.asarray([(1.0, -1.0), (0.0, 0.0), (0.0, 0.0)])
>>> gtab = gradient_table([1000] * bvecs.shape[-1], bvecs)
>>> compute_pairwise_angles(gtab, closest_polarity=False)[0, 1] # doctest: +ELLIPSIS
3.1415...
>>> compute_pairwise_angles(
... ((1.0, -1.0), (0.0, 0.0), (0.0, 0.0)),
... ((1.0, -1.0), (0.0, 0.0), (0.0, 0.0)),
... closest_polarity=False,
... )[0, 1] # doctest: +ELLIPSIS
>>> bvecs1 = np.asarray([(1.0, -1.0), (0.0, 0.0), (0.0, 0.0)])
>>> bvecs2 = np.asarray([(1.0, -1.0), (0.0, 0.0), (0.0, 0.0)])
>>> gtab1 = gradient_table([1000] * bvecs1.shape[-1], bvecs1)
>>> gtab2 = gradient_table([1000] * bvecs2.shape[-1], bvecs2)
>>> compute_pairwise_angles(gtab1, gtab2, closest_polarity=False)[0, 1] # doctest: +ELLIPSIS
3.1415...
>>> compute_pairwise_angles(
... ((1.0, -1.0), (0.0, 0.0), (0.0, 0.0)),
... )[0, 1]
>>> bvecs = np.asarray([(1.0, -1.0), (0.0, 0.0), (0.0, 0.0)])
>>> gtab = gradient_table([1000] * bvecs.shape[-1], bvecs)
>>> compute_pairwise_angles(gtab)[0, 1]
0.0
References
Expand Down
47 changes: 40 additions & 7 deletions test/test_dipy.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,47 @@
[0, 0, 1, 0, 1, 1, 1],
]
),
None,
True,
np.array(
[
[0.0, np.pi / 2, np.pi / 2, np.pi / 4, np.pi / 4, np.pi / 2, np.pi / 4],
[np.pi / 2, 0.0, np.pi / 2, np.pi / 4, np.pi / 2, np.pi / 4, np.pi / 2],
[np.pi / 2, np.pi / 2, 0.0, np.pi / 2, np.pi / 4, np.pi / 4, np.pi / 4],
[np.pi / 4, np.pi / 4, np.pi / 2, 0.0, np.pi / 3, np.pi / 3, np.pi / 3],
[np.pi / 4, np.pi / 2, np.pi / 4, np.pi / 3, 0.0, np.pi / 3, np.pi / 2],
[np.pi / 2, np.pi / 4, np.pi / 4, np.pi / 3, np.pi / 3, 0.0, np.pi / 3],
[np.pi / 4, np.pi / 2, np.pi / 4, np.pi / 3, np.pi / 2, np.pi / 3, 0.0],
]
),
),
(
np.array(
[
[1, 0, 0, 1, 1, 0, -1],
[0, 1, 0, 1, 0, 1, 0],
[0, 0, 1, 0, 1, 1, 1],
]
),
True,
None,
False,
np.array(
[
[0.0, np.pi / 2, np.pi / 2, np.pi / 4, np.pi / 4, np.pi / 2, np.pi / 4],
[0.0, np.pi / 2, np.pi / 2, np.pi / 4, np.pi / 4, np.pi / 2, 3 * np.pi / 4],
[np.pi / 2, 0.0, np.pi / 2, np.pi / 4, np.pi / 2, np.pi / 4, np.pi / 2],
[np.pi / 2, np.pi / 2, 0.0, np.pi / 2, np.pi / 4, np.pi / 4, np.pi / 4],
[np.pi / 4, np.pi / 4, np.pi / 2, 0.0, np.pi / 3, np.pi / 3, np.pi / 3],
[np.pi / 4, np.pi / 4, np.pi / 2, 0.0, np.pi / 3, np.pi / 3, 2 * np.pi / 3],
[np.pi / 4, np.pi / 2, np.pi / 4, np.pi / 3, 0.0, np.pi / 3, np.pi / 2],
[np.pi / 2, np.pi / 4, np.pi / 4, np.pi / 3, np.pi / 3, 0.0, np.pi / 3],
[np.pi / 4, np.pi / 2, np.pi / 4, np.pi / 3, np.pi / 2, np.pi / 3, 0.0],
[
3 * np.pi / 4,
np.pi / 2,
np.pi / 4,
2 * np.pi / 3,
np.pi / 2,
np.pi / 3,
0.0,
],
]
),
),
Expand Down Expand Up @@ -125,11 +149,20 @@
],
)
def test_compute_pairwise_angles(bvecs1, bvecs2, closest_polarity, expected):
gtab1 = gradient_table([1000] * len(bvecs1), bvecs1)
gtab2 = gradient_table([1000] * len(bvecs2), bvecs2)
# DIPY requires the vectors to be normalized
_bvecs1 = bvecs1 / np.linalg.norm(bvecs1, axis=0)
gtab1 = gradient_table([1000] * _bvecs1.shape[-1], _bvecs1)

_bvecs2 = None
gtab2 = None
if bvecs2 is not None:
_bvecs2 = bvecs2 / np.linalg.norm(bvecs2, axis=0)
gtab2 = gradient_table([1000] * _bvecs2.shape[-1], _bvecs2)

obtained = compute_pairwise_angles(gtab1, gtab2, closest_polarity)

assert (bvecs1.shape[-1], bvecs2.shape[-1]) == obtained.shape
if _bvecs2 is not None:
assert (_bvecs1.shape[-1], _bvecs2.shape[-1]) == obtained.shape
assert obtained.shape == expected.shape
np.testing.assert_array_almost_equal(obtained, expected, decimal=2)

Expand Down
2 changes: 1 addition & 1 deletion test/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def test_average_model():
def test_gp_model():
gp = GaussianProcessModel(kernel="default")

assert isinstance(gp, model.GaussianProcessModel)
assert isinstance(gp, model.dipy.GaussianProcessModel)

X, y = make_friedman2(n_samples=500, noise=0, random_state=0)
gp.fit(X, y)
Expand Down

0 comments on commit 0c32def

Please sign in to comment.