Skip to content

Commit c21d5bd

Browse files
committed
ENH: Add monai MetaTensor support
Applies the .affine matrix.
1 parent 68ea8b4 commit c21d5bd

File tree

3 files changed

+158
-35
lines changed

3 files changed

+158
-35
lines changed

examples/integrations/MONAI/transform_visualization.ipynb

Lines changed: 140 additions & 35 deletions
Large diffs are not rendered by default.

itkwidgets/integrations/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import dask
77
from .itk import HAVE_ITK
88
from .pytorch import HAVE_TORCH
9+
from .monai import HAVE_MONAI
910
from .vtk import HAVE_VTK, vtk_image_to_ngff_image, vtk_polydata_to_vtkjs
1011
from .xarray import HAVE_XARRAY, HAVE_MULTISCALE_SPATIAL_IMAGE, xarray_data_array_to_numpy, xarray_data_set_to_numpy
1112
from ..render_types import RenderType
@@ -102,6 +103,15 @@ def _get_viewer_image(image, label=False):
102103
to_ngff_zarr(store, multiscales, chunk_store=chunk_store)
103104
return store
104105

106+
if HAVE_MONAI:
107+
from monai.data import MetaTensor, metatensor_to_itk_image
108+
if isinstance(image, MetaTensor):
109+
itk_image = metatensor_to_itk_image(image)
110+
ngff_image = itk_image_to_ngff_image(itk_image)
111+
multiscales = to_multiscales(ngff_image, method=method)
112+
to_ngff_zarr(store, multiscales, chunk_store=chunk_store)
113+
return store
114+
105115
if HAVE_TORCH:
106116
import torch
107117
if isinstance(image, torch.Tensor):

itkwidgets/integrations/monai.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import importlib_metadata
2+
3+
HAVE_MONAI = False
4+
try:
5+
importlib_metadata.metadata("monai")
6+
HAVE_MONAI = True
7+
except importlib_metadata.PackageNotFoundError:
8+
pass

0 commit comments

Comments
 (0)