|
| 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