Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Implement a visualisation feature to NiTransforms #212

Open
wants to merge 58 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 50 commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
05b61b8
feat: Implement vis
May 29, 2024
b2cac3b
enh: Update plotting
May 29, 2024
7722545
enh: Reworked figure formatting and code structure
Jun 11, 2024
227e330
enh: code structure. Plotting now a class object.
Jun 11, 2024
59104c6
fix: remove straneous spaces
Jun 11, 2024
3865b65
fix: remove straneous spaces
Jun 12, 2024
76ca16a
enh: code structure
Jun 14, 2024
3c9be6d
N/A
Jun 14, 2024
304f362
enh: update progress on vis
Jun 17, 2024
62801e5
enh: Implemented function descriptions, reworked some lines.
Jun 19, 2024
b745a9f
feat: First implementation of draft dsm plots.
Jun 19, 2024
7f96037
feat: First draft of vis.py
Jun 20, 2024
ac31803
Feat: Addition of new, improved file vis_v2.py with new developments …
Jun 24, 2024
2ffa28a
enh: 1. Implemented 3d quiver; 2. Updated formatting; 3. Updated func…
Jun 24, 2024
6bb8afd
fix: Reworked file naming and versions
Jun 26, 2024
a3cef8d
fix: Relocate draft files
Jun 26, 2024
b426904
Feat: New file version, implemented divergence of deltas field. New (…
Jun 28, 2024
f13716a
Fix: ajust location of draft files
Jun 28, 2024
8601490
enh: finalised div map, removed normalised dsm plot. Prepared V5
Jul 1, 2024
f489927
Fix: fixed jacobian calculations, implemented jacobian-mapped scatter…
Jul 3, 2024
6b86390
Feat: Created v6 and reorganised drafts
Jul 3, 2024
d560077
Feat: Created v6 and reorganised drafts
Jul 3, 2024
9d2cab8
fix: debug grid, partial fix
Jul 5, 2024
68e72ef
Feat: Finalised grid, code functional. Required: clean up files; clea…
Jul 5, 2024
ead2351
Enh: formatting
Jul 8, 2024
dc87338
Enh: Removed draft files, rename final vis.py
Jul 8, 2024
f051b7d
fix: corrected descriptions
Jul 8, 2024
bde247b
enh: code structure
Jul 9, 2024
ef53ff8
remove example
Jul 9, 2024
1507e77
Enh: New file test_vis.py for implementation of tests. Added checks f…
Jul 10, 2024
6b14738
removed draft python files
Jul 10, 2024
af24af8
enh: Further additions to test_vis, output path for figures added to …
Jul 11, 2024
ed381b3
Fix: test 3d quiver fixed, fixed identical savefig filenames
Jul 11, 2024
30e1967
fix: accidentally running examples
Jul 11, 2024
6a1eb59
Fix: pacify flake8
Jul 15, 2024
2d54dc1
Fix: pacify flake8
Jul 15, 2024
85621c1
Fix: pacify flake8
Jul 15, 2024
164372c
Fix: pacify flake8
Jul 15, 2024
c047b3c
Enh: increase CodeCov coverage
Jul 15, 2024
0aa2d08
Enh: increase CodeCov coverage, pacify flake8
Jul 15, 2024
b66fee3
Fix Pacify flake8
Jul 15, 2024
94bab2a
Fix: RGB quiver plot failing if plotting transforms independent of on…
Jul 16, 2024
138b81e
Fix: pacify flake8
Jul 16, 2024
07f94f7
Fix: pacify flake8
Jul 16, 2024
011cf30
Fix: pacify flake8
Jul 16, 2024
67da55c
Feat: creation of notebook for vis module
Jul 16, 2024
a1fcdc7
feat: plot linear, planar and spherical coefficient. Other code tidying.
Jul 19, 2024
6bef93f
pacify flake8
Jul 19, 2024
2384469
fix: mistakenly broke syntax
Jul 19, 2024
c25a501
enh: added tests for codecov
Jul 19, 2024
f51e93f
Update nitransforms/tests/test_vis.py
jmarabotto Jul 23, 2024
d44da6c
Update nitransforms/tests/test_vis.py
jmarabotto Jul 23, 2024
91191ae
Update nitransforms/vis.py
jmarabotto Jul 23, 2024
0d8d997
Update nitransforms/vis.py
jmarabotto Jul 23, 2024
c262c55
Update nitransforms/vis.py
jmarabotto Jul 23, 2024
8863189
fix: pacify flake8, updated notebook
Jul 23, 2024
664f6f5
fix? read the docs
Jul 23, 2024
36dae68
Link to notebook
effigies Sep 16, 2024
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
3 changes: 2 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ jobs:
-v /tmp/data/nitransforms-tests:/data -e TEST_DATA_HOME=/data \
-e COVERAGE_FILE=/tmp/summaries/.pytest.coverage \
-v /tmp/fslicense/license.txt:/opt/freesurfer/license.txt:ro \
-v /tmp/tests:/tmp nitransforms:latest \
-v /tmp/tests:/tmp -e TEST_OUTPUT_DIR=/tmp/artifacts \
nitransforms:latest \
pytest --junit-xml=/tmp/summaries/pytest.xml \
--cov nitransforms --cov-report xml:/tmp/summaries/unittests.xml \
nitransforms/
Expand Down
524 changes: 524 additions & 0 deletions docs/notebooks/Visualizing transforms.ipynb
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The final cell seems to have failed:

image

Copy link
Contributor Author

@jmarabotto jmarabotto Jul 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've worked more on the notebook and fixed this since the last commit, it simply requires a 3x3 subplot (I carelessly copy pasted a 1x3 figure)

Edit: committed to fix, should work now 👍

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions nitransforms/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
_data = None
_brainmask = None
_testdir = Path(os.getenv("TEST_DATA_HOME", "~/.nitransforms/testdata")).expanduser()
_outdir = os.getenv("TEST_OUTPUT_DIR", None)

_datadir = Path(__file__).parent / "tests" / "data"


Expand Down Expand Up @@ -45,6 +47,12 @@ def testdata_path():
return _testdir


@pytest.fixture
def output_path():
"""Return an output folder."""
return Path(_outdir) if _outdir is not None else None


@pytest.fixture
def get_testdata():
"""Generate data in the requested orientation."""
Expand Down
2 changes: 1 addition & 1 deletion nitransforms/io/afni.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def from_image(cls, imgobj):
hdr = imgobj.header.copy()
shape = hdr.get_data_shape()

if len(shape) != 5 or shape[-2] != 1 or not shape[-1] in (2, 3):
if len(shape) != 5 or shape[-2] != 1 or shape[-1] not in (2, 3):
raise TransformFileError(
'Displacements field "%s" does not come from AFNI.'
% imgobj.file_map["image"].filename
Expand Down
2 changes: 1 addition & 1 deletion nitransforms/io/fsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def from_image(cls, imgobj):
hdr = imgobj.header.copy()
shape = hdr.get_data_shape()

if len(shape) != 4 or not shape[-1] in (2, 3):
if len(shape) != 4 or shape[-1] not in (2, 3):
raise TransformFileError(
'Displacements field "%s" does not come from FSL.' %
imgobj.file_map['image'].filename)
Expand Down
2 changes: 1 addition & 1 deletion nitransforms/io/itk.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def from_image(cls, imgobj):
hdr = imgobj.header.copy()
shape = hdr.get_data_shape()

if len(shape) != 5 or shape[-2] != 1 or not shape[-1] in (2, 3):
if len(shape) != 5 or shape[-2] != 1 or shape[-1] not in (2, 3):
raise TransformFileError(
'Displacements field "%s" does not come from ITK.'
% imgobj.file_map["image"].filename
Expand Down
171 changes: 171 additions & 0 deletions nitransforms/tests/test_vis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import numpy as np
import matplotlib.pyplot as plt
import pytest

import nibabel as nb
from nitransforms.nonlinear import DenseFieldTransform
from nitransforms.vis import PlotDenseField, format_axes


def test_read_path(data_path):
"Check that filepaths are a supported method for loading "
"and reading transforms with PlotDenseField"
jmarabotto marked this conversation as resolved.
Show resolved Hide resolved
PlotDenseField(transform=data_path / "ds-005_sub-01_from-OASIS_to-T1_warp_fsl.nii.gz")


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"
xfm = DenseFieldTransform(
field=np.zeros((10, 10, 10, 3)),
reference=nb.Nifti1Image(np.zeros((10, 10, 10, 3)), np.eye(4), None),
)
for idx in range(0,3):
if idx == 0:
i, j, k = 1, 0, 0
elif idx == 1:
i, j, k = 0, 1, 0
elif idx == 2:
i, j, k = 0, 0, 1

with pytest.raises(IndexError):
PlotDenseField(
transform=xfm._field,
reference=xfm._reference,
).test_slices(
xslice=xfm._field.shape[0] + i,
yslice=xfm._field.shape[1] + j,
zslice=xfm._field.shape[2] + k,
)


def test_show_transform(data_path, output_path):
PlotDenseField(
transform=data_path / "ds-005_sub-01_from-OASIS_to-T1_warp_fsl.nii.gz"
).show_transform(
xslice=45,
yslice=50,
zslice=55,
)
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,
show_grid=True,
show_brain=True,
)
if output_path is not None:
plt.savefig(output_path / "plot_distortion.svg", bbox_inches="tight")
else:
plt.show()


def test_empty_quiver():
fig, axes = plt.subplots(1, 3, figsize=(12, 4), tight_layout=True)
PlotDenseField(
transform=np.zeros((10, 10, 10, 3)),
reference=nb.Nifti1Image(np.zeros((10, 10, 10, 3)), np.eye(4), None),
).plot_quiverdsm(
axes=axes,
xslice=5,
yslice=5,
zslice=5,
)


def test_plot_quiverdsm(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_quiverdsm(
axes=axes,
xslice=50,
yslice=50,
zslice=50,
)

if output_path is not None:
plt.savefig(output_path / "plot_quiverdsm.svg", bbox_inches="tight")
else:
plt.show()


def test_3dquiver(data_path, output_path):
with pytest.raises(NotImplementedError):
fig = plt.figure()
axes = fig.add_subplot(projection='3d')
PlotDenseField(
transform=data_path / "ds-005_sub-01_from-OASIS_to-T1_warp_fsl.nii.gz",
).plot_quiverdsm(
axes=axes,
xslice=None,
yslice=None,
zslice=None,
three_D=True
)
format_axes(axes)

if output_path is not None:
plt.savefig(output_path / "plot_3dquiver.svg", bbox_inches="tight")
else:
plt.show()


def test_coeffs(data_path, output_path):
fig, axes = plt.subplots(3, 3, figsize=(10, 9))
PlotDenseField(
transform=data_path / "ds-005_sub-01_from-OASIS_to-T1_warp_fsl.nii.gz"
).plot_coeffs(
fig=fig,
axes=axes,
xslice=50,
yslice=50,
zslice=50,
)

if output_path is not None:
plt.savefig(output_path / "plot_coeffs.svg", bbox_inches="tight")
else:
plt.show()


def test_plot_jacobian(data_path, output_path):
fig, axes = plt.subplots(1, 3, figsize=(12, 5))
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 / "plot_jacobian.svg", bbox_inches="tight")
else:
plt.show()


if __name__ == "__main__":
pytest.main([__file__])
jmarabotto marked this conversation as resolved.
Show resolved Hide resolved
Loading
Loading