Skip to content

Commit

Permalink
Merge pull request #734 from thewtex/monai-updates
Browse files Browse the repository at this point in the history
Monai updates
  • Loading branch information
thewtex authored Feb 2, 2024
2 parents 0e43a71 + c21d5bd commit f4f1c4f
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 102 deletions.
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencies:
- imjoy-elfinder
- imjoy-jupyter-extension
- imjoy-jupyterlab-extension
- monai-weekly[nibabel, matplotlib, tqdm]
- monai[nibabel, matplotlib, tqdm]
- imageio
- pyvista
- dask[diagnostics]
Expand Down
2 changes: 1 addition & 1 deletion examples/EnvironmentCheck.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
" \"imjoy-jupyter-extension\",\n",
" \"imjoy-jupyterlab-extension\",\n",
" \"itk\",\n",
" \"monai-weekly[nibabel, matplotlib, tqdm]\",\n",
" \"monai[nibabel, matplotlib, tqdm]\",\n",
" \"imageio\",\n",
" \"pyvista\",\n",
" \"dask[diagnostics]\",\n",
Expand Down
254 changes: 169 additions & 85 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
12 changes: 7 additions & 5 deletions itkwidgets/integrations/itk.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import itkwasm

from packaging import version
import importlib_metadata
HAVE_ITK = False
try:
import itk
if not hasattr(itk, '__version__') or version.parse(itk.__version__) < version.parse('5.3.0'):
raise RuntimeError('itk 5.3 or newer is required. `pip install itk>=5.3.0`')
itk_version = importlib_metadata.version('itk')
if version.parse(itk_version) < version.parse('5.3.0'):
raise RuntimeError('itk 5.3 or newer is required. `pip install itk>=5.3.0`')
HAVE_ITK = True
except ImportError:
except importlib_metadata.PackageNotFoundError:
pass


if HAVE_ITK:
def itk_group_spatial_object_to_wasm_point_set(point_set):
import itk
point_set_dict = itk.dict_from_pointset(point_set)
wasm_point_set = itkwasm.PointSet(**point_set_dict)
return wasm_point_set
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
6 changes: 4 additions & 2 deletions itkwidgets/integrations/pytorch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import importlib_metadata

HAVE_TORCH = False
try:
import torch
importlib_metadata.metadata("torch")
HAVE_TORCH = True
except ImportError:
except importlib_metadata.PackageNotFoundError:
pass
11 changes: 7 additions & 4 deletions itkwidgets/integrations/vtk.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import importlib_metadata

HAVE_VTK = False
try:
import vtk
importlib_metadata.metadata("vtk")
HAVE_VTK = True
from vtk.util.numpy_support import vtk_to_numpy
except ImportError:
except importlib_metadata.PackageNotFoundError:
pass

from ngff_zarr import ngff_image, to_ngff_image
from ngff_zarr import to_ngff_image


def vtk_image_to_ngff_image(image):
from vtk.util.numpy_support import vtk_to_numpy
array = vtk_to_numpy(image.GetPointData().GetScalars())
dimensions = list(image.GetDimensions())
array.shape = dimensions[::-1]
Expand All @@ -25,5 +27,6 @@ def vtk_image_to_ngff_image(image):
return ngff_image

def vtk_polydata_to_vtkjs(point_set):
from vtk.util.numpy_support import vtk_to_numpy
array = vtk_to_numpy(point_set.GetPoints().GetData())
return array
10 changes: 6 additions & 4 deletions itkwidgets/integrations/xarray.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import importlib_metadata

HAVE_XARRAY = False
try:
import xarray
importlib_metadata.metadata("xarray")
HAVE_XARRAY = True
except ImportError:
except importlib_metadata.PackageNotFoundError:
pass

HAVE_MULTISCALE_SPATIAL_IMAGE = False
try:
import multiscale_spatial_image
importlib_metadata.metadata("multiscale-spatial-image")
HAVE_MULTISCALE_SPATIAL_IMAGE = True
except ImportError:
except importlib_metadata.PackageNotFoundError:
pass

def xarray_data_array_to_numpy(data_array):
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dependencies = [
"itkwasm >= 1.0b.78",
"imjoy-rpc >= 0.5.42",
"imjoy-utils >= 0.1.2",
"importlib_metadata",
"ngff-zarr[dask-image] >= 0.4.3",
"numcodecs",
"zarr",
Expand Down

0 comments on commit f4f1c4f

Please sign in to comment.