Skip to content

Commit b94cc53

Browse files
authored
Merge pull request #493 from oesteban/enh/notebook-components
ENH: Show registration reportlets inline within Jupyter notebooks
2 parents e43c6c1 + 4920bcb commit b94cc53

File tree

4 files changed

+689
-384
lines changed

4 files changed

+689
-384
lines changed

niworkflows/viz/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
# -*- coding: utf-8 -*-
21
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
32
# vi: set ft=python sts=4 ts=4 sw=4 et:
4-
from __future__ import absolute_import, division, print_function, unicode_literals
5-
63
from .plots import plot_carpet
74
from .utils import SVGNS
85

9-
__all__ = ['plot_carpet', 'SVGNS']
6+
__all__ = ["plot_carpet", "SVGNS"]

niworkflows/viz/notebook.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""Visualization component for Jupyter Notebooks."""
2+
from pathlib import Path
3+
import numpy as np
4+
import nibabel as nb
5+
from .utils import compose_view, plot_registration, cuts_from_bbox
6+
7+
8+
def display(
9+
fixed_image,
10+
moving_image,
11+
contour=None,
12+
cuts=None,
13+
fixed_label="F",
14+
moving_label="M",
15+
):
16+
"""Plot the flickering panels to show a registration process."""
17+
from IPython.display import SVG, display as _disp
18+
19+
if isinstance(fixed_image, (str, Path)):
20+
fixed_image = nb.load(str(fixed_image))
21+
if isinstance(moving_image, (str, Path)):
22+
moving_image = nb.load(str(moving_image))
23+
24+
if cuts is None:
25+
n_cuts = 7
26+
if contour is not None:
27+
if isinstance(contour, (str, Path)):
28+
contour = nb.load(str(contour))
29+
cuts = cuts_from_bbox(contour, cuts=n_cuts)
30+
else:
31+
hdr = fixed_image.header.copy()
32+
hdr.set_data_dtype("uint8")
33+
mask_nii = nb.Nifti1Image(
34+
np.ones(fixed_image.shape, dtype="uint8"), fixed_image.affine, hdr
35+
)
36+
cuts = cuts_from_bbox(mask_nii, cuts=n_cuts)
37+
38+
# Call composer
39+
_disp(
40+
SVG(
41+
compose_view(
42+
plot_registration(
43+
fixed_image,
44+
"fixed-image",
45+
estimate_brightness=True,
46+
cuts=cuts,
47+
label=fixed_label,
48+
contour=contour,
49+
compress=False,
50+
),
51+
plot_registration(
52+
moving_image,
53+
"moving-image",
54+
estimate_brightness=True,
55+
cuts=cuts,
56+
label=moving_label,
57+
contour=contour,
58+
compress=False,
59+
),
60+
)
61+
)
62+
)

0 commit comments

Comments
 (0)