Skip to content

Commit

Permalink
ENH: Add monai MetaTensor support
Browse files Browse the repository at this point in the history
Applies the .affine matrix.
  • Loading branch information
thewtex committed Jan 30, 2024
1 parent 68ea8b4 commit 0b974ff
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 35 deletions.
173 changes: 138 additions & 35 deletions examples/integrations/MONAI/transform_visualization.ipynb

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions itkwidgets/integrations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import dask
from .itk import HAVE_ITK
from .pytorch import HAVE_TORCH
from .monai import HAVE_MONAI
from .vtk import HAVE_VTK, vtk_image_to_ngff_image, vtk_polydata_to_vtkjs
from .xarray import HAVE_XARRAY, HAVE_MULTISCALE_SPATIAL_IMAGE, xarray_data_array_to_numpy, xarray_data_set_to_numpy
from ..render_types import RenderType
Expand Down Expand Up @@ -102,6 +103,15 @@ def _get_viewer_image(image, label=False):
to_ngff_zarr(store, multiscales, chunk_store=chunk_store)
return store

if HAVE_MONAI:
from monai.data import MetaTensor, metatensor_to_itk_image
if isinstance(image, MetaTensor):
itk_image = metatensor_to_itk_image(image)
ngff_image = itk_image_to_ngff_image(itk_image)
multiscales = to_multiscales(ngff_image, method=method)
to_ngff_zarr(store, multiscales, chunk_store=chunk_store)
return store

if HAVE_TORCH:
import torch
if isinstance(image, torch.Tensor):
Expand Down
8 changes: 8 additions & 0 deletions itkwidgets/integrations/monai.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import importlib_metadata

HAVE_MONAI = False
try:
importlib_metadata.metadata("monai")
HAVE_MONAI = True
except importlib_metadata.PackageNotFoundError:
pass

0 comments on commit 0b974ff

Please sign in to comment.