-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enh: Further additions to test_vis, output path for figures added to …
…conftest
- Loading branch information
Julien Marabotto
authored and
Julien Marabotto
committed
Jul 11, 2024
1 parent
6b14738
commit af24af8
Showing
7 changed files
with
144 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,120 @@ | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
import pytest, unittest | ||
from pathlib import Path | ||
import pytest | ||
|
||
import nibabel as nb | ||
from nitransforms.nonlinear import DenseFieldTransform | ||
from nitransforms.vis import PlotDenseField | ||
|
||
test_dir = Path("tests/data/") | ||
test_file = Path("ds-005_sub-01_from-OASIS_to-T1_warp_fsl.nii.gz") | ||
|
||
xfm = Path(test_dir/test_file) | ||
def test_read_path(data_path): | ||
"""Check that filepaths are a supported method for loading and reading transforms with PlotDenseField""" | ||
PlotDenseField(transform = data_path / "ds-005_sub-01_from-OASIS_to-T1_warp_fsl.nii.gz") | ||
|
||
def test_slice_values(xfm, xslice, yslice, zslice, is_deltas=True): | ||
|
||
def test_slice_values(): | ||
"""Check that ValueError is issued if negative slices are provided""" | ||
with pytest.raises(ValueError): | ||
PlotDenseField( | ||
transform = np.zeros((10, 10, 10, 3)), | ||
reference=nb.Nifti1Image(np.zeros((10, 10, 10, 3)), np.eye(4), None), | ||
).test_slices( | ||
xslice=-1, | ||
yslice=-1, | ||
zslice=-1, | ||
) | ||
|
||
"""Check that IndexError is issued if provided slices are beyond range of transform dimensions""" | ||
with pytest.raises(IndexError): | ||
xfm = DenseFieldTransform( | ||
field=np.zeros((10, 10, 10, 3)), | ||
reference=nb.Nifti1Image(np.zeros((10, 10, 10, 3)), np.eye(4), None), | ||
) | ||
PlotDenseField( | ||
transform=xfm._field, | ||
reference=xfm._reference, | ||
).test_slices( | ||
xslice=xfm._field.shape[0]+1, | ||
yslice=xfm._field.shape[1]+1, | ||
zslice=xfm._field.shape[2]+1, | ||
) | ||
|
||
|
||
def test_show_transform(data_path, output_path): | ||
PlotDenseField( | ||
path_to_file=Path(xfm), | ||
is_deltas=is_deltas, | ||
).test_slices( | ||
xslice=xslice, | ||
yslice=yslice, | ||
zslice=zslice, | ||
transform = data_path / "ds-005_sub-01_from-OASIS_to-T1_warp_fsl.nii.gz" | ||
).show_transform( | ||
xslice=50, | ||
yslice=50, | ||
zslice=50, | ||
) | ||
if output_path is not None: | ||
plt.savefig(output_path / "show_transform.svg", bbox_inches="tight") | ||
else: | ||
plt.show() | ||
|
||
|
||
def test_plot_distortion(data_path, output_path): | ||
fig, axes = plt.subplots(1, 3, figsize=(12, 4)) | ||
PlotDenseField( | ||
transform = data_path / "ds-005_sub-01_from-OASIS_to-T1_warp_fsl.nii.gz" | ||
).plot_distortion( | ||
axes=axes, | ||
xslice=50, | ||
yslice=50, | ||
zslice=50, | ||
) | ||
if output_path is not None: | ||
plt.savefig(output_path / "show_transform.svg", bbox_inches="tight") | ||
else: | ||
plt.show() | ||
|
||
|
||
def test_show_transform(xfm, xslice=50, yslice=50, zslice=50, is_deltas=True): | ||
def test_plot_quiverdsm(data_path, output_path): | ||
fig, axes = plt.subplots(1, 3, figsize=(12, 4)) | ||
PlotDenseField( | ||
path_to_file=Path(xfm), | ||
is_deltas=is_deltas, | ||
transform = data_path / "ds-005_sub-01_from-OASIS_to-T1_warp_fsl.nii.gz" | ||
).plot_quiverdsm( | ||
axes=axes, | ||
xslice=xslice, | ||
yslice=yslice, | ||
zslice=zslice, | ||
xslice=50, | ||
yslice=50, | ||
zslice=50, | ||
) | ||
plt.show() | ||
if output_path is not None: | ||
plt.savefig(output_path / "show_transform.svg", bbox_inches="tight") | ||
else: | ||
plt.show() | ||
|
||
|
||
def test_3dquiver(data_path, output_path): | ||
with pytest.raises(NotImplementedError): | ||
fig = plt.figure() | ||
axes = plt.subplots(projection='3d') | ||
PlotDenseField( | ||
transform = data_path / "ds-005_sub-01_from-OASIS_to-T1_warp_fsl.nii.gz" | ||
).plot_quiverdsm(axes=axes, three_D=True) | ||
|
||
if output_path is not None: | ||
plt.savefig(output_path / "show_transform.svg", bbox_inches="tight") | ||
else: | ||
plt.show() | ||
|
||
|
||
def test_plot_jacobian(data_path, output_path): | ||
fig, axes = plt.subplots(1, 3, figsize=(12, 4)) | ||
PlotDenseField( | ||
transform = data_path / "ds-005_sub-01_from-OASIS_to-T1_warp_fsl.nii.gz" | ||
).plot_jacobian( | ||
axes=axes, | ||
xslice=50, | ||
yslice=50, | ||
zslice=50, | ||
) | ||
if output_path is not None: | ||
plt.savefig(output_path / "show_transform.svg", bbox_inches="tight") | ||
else: | ||
plt.show() | ||
|
||
|
||
test_slice_values(xfm, 50, -50, 50) #should raise ValueError | ||
test_slice_values(xfm, 500, 50, 50) #should raise IndexError | ||
test_show_transform(Path("tests/data/ds-005_sub-01_from-OASIS_to-T1_warp_fsl.nii.gz")) | ||
if __name__ == "__main__": | ||
pytest.main([__file__]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters