Skip to content

Commit f4f1c4f

Browse files
authored
Merge pull request #734 from thewtex/monai-updates
Monai updates
2 parents 0e43a71 + c21d5bd commit f4f1c4f

File tree

10 files changed

+214
-102
lines changed

10 files changed

+214
-102
lines changed

environment.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ dependencies:
1212
- imjoy-elfinder
1313
- imjoy-jupyter-extension
1414
- imjoy-jupyterlab-extension
15-
- monai-weekly[nibabel, matplotlib, tqdm]
15+
- monai[nibabel, matplotlib, tqdm]
1616
- imageio
1717
- pyvista
1818
- dask[diagnostics]

examples/EnvironmentCheck.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
" \"imjoy-jupyter-extension\",\n",
112112
" \"imjoy-jupyterlab-extension\",\n",
113113
" \"itk\",\n",
114-
" \"monai-weekly[nibabel, matplotlib, tqdm]\",\n",
114+
" \"monai[nibabel, matplotlib, tqdm]\",\n",
115115
" \"imageio\",\n",
116116
" \"pyvista\",\n",
117117
" \"dask[diagnostics]\",\n",

examples/integrations/MONAI/transform_visualization.ipynb

Lines changed: 169 additions & 85 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/itk.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import itkwasm
22

33
from packaging import version
4+
import importlib_metadata
45
HAVE_ITK = False
56
try:
6-
import itk
7-
if not hasattr(itk, '__version__') or version.parse(itk.__version__) < version.parse('5.3.0'):
8-
raise RuntimeError('itk 5.3 or newer is required. `pip install itk>=5.3.0`')
7+
itk_version = importlib_metadata.version('itk')
8+
if version.parse(itk_version) < version.parse('5.3.0'):
9+
raise RuntimeError('itk 5.3 or newer is required. `pip install itk>=5.3.0`')
910
HAVE_ITK = True
10-
except ImportError:
11+
except importlib_metadata.PackageNotFoundError:
1112
pass
12-
13+
1314

1415
if HAVE_ITK:
1516
def itk_group_spatial_object_to_wasm_point_set(point_set):
17+
import itk
1618
point_set_dict = itk.dict_from_pointset(point_set)
1719
wasm_point_set = itkwasm.PointSet(**point_set_dict)
1820
return wasm_point_set

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

itkwidgets/integrations/pytorch.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import importlib_metadata
2+
13
HAVE_TORCH = False
24
try:
3-
import torch
5+
importlib_metadata.metadata("torch")
46
HAVE_TORCH = True
5-
except ImportError:
7+
except importlib_metadata.PackageNotFoundError:
68
pass

itkwidgets/integrations/vtk.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
import importlib_metadata
2+
13
HAVE_VTK = False
24
try:
3-
import vtk
5+
importlib_metadata.metadata("vtk")
46
HAVE_VTK = True
5-
from vtk.util.numpy_support import vtk_to_numpy
6-
except ImportError:
7+
except importlib_metadata.PackageNotFoundError:
78
pass
89

9-
from ngff_zarr import ngff_image, to_ngff_image
10+
from ngff_zarr import to_ngff_image
1011

1112

1213
def vtk_image_to_ngff_image(image):
14+
from vtk.util.numpy_support import vtk_to_numpy
1315
array = vtk_to_numpy(image.GetPointData().GetScalars())
1416
dimensions = list(image.GetDimensions())
1517
array.shape = dimensions[::-1]
@@ -25,5 +27,6 @@ def vtk_image_to_ngff_image(image):
2527
return ngff_image
2628

2729
def vtk_polydata_to_vtkjs(point_set):
30+
from vtk.util.numpy_support import vtk_to_numpy
2831
array = vtk_to_numpy(point_set.GetPoints().GetData())
2932
return array

itkwidgets/integrations/xarray.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
import importlib_metadata
2+
13
HAVE_XARRAY = False
24
try:
3-
import xarray
5+
importlib_metadata.metadata("xarray")
46
HAVE_XARRAY = True
5-
except ImportError:
7+
except importlib_metadata.PackageNotFoundError:
68
pass
79

810
HAVE_MULTISCALE_SPATIAL_IMAGE = False
911
try:
10-
import multiscale_spatial_image
12+
importlib_metadata.metadata("multiscale-spatial-image")
1113
HAVE_MULTISCALE_SPATIAL_IMAGE = True
12-
except ImportError:
14+
except importlib_metadata.PackageNotFoundError:
1315
pass
1416

1517
def xarray_data_array_to_numpy(data_array):

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ dependencies = [
3939
"itkwasm >= 1.0b.78",
4040
"imjoy-rpc >= 0.5.42",
4141
"imjoy-utils >= 0.1.2",
42+
"importlib_metadata",
4243
"ngff-zarr[dask-image] >= 0.4.3",
4344
"numcodecs",
4445
"zarr",

0 commit comments

Comments
 (0)