|
9 | 9 | from .xarray import HAVE_XARRAY, xarray_data_array_to_numpy, xarray_data_set_to_numpy
|
10 | 10 | from ..render_types import RenderType
|
11 | 11 |
|
12 |
| -_image_count = 1 |
13 | 12 |
|
14 |
| -async def _set_viewer_image(itk_viewer, image, name=None, is_label=False): |
15 |
| - global _image_count |
16 |
| - if isinstance(image, itkwasm.Image): |
17 |
| - if not name: |
18 |
| - name = image.name |
19 |
| - if not name: |
20 |
| - name = f"image {_image_count}" |
21 |
| - _image_count += 1 |
22 |
| - if is_label: |
23 |
| - await itk_viewer.setLabelImage(image) |
24 |
| - else: |
25 |
| - await itk_viewer.setImage(image, name) |
26 |
| - elif isinstance(image, np.ndarray): |
27 |
| - if not name: |
28 |
| - name = f"image {_image_count}" |
29 |
| - _image_count += 1 |
30 |
| - if is_label: |
31 |
| - await itk_viewer.setLabelImage(image) |
32 |
| - else: |
33 |
| - await itk_viewer.setImage(image, name) |
34 |
| - elif isinstance(image, zarr.Group): |
35 |
| - if not name: |
36 |
| - name = f"image {_image_count}" |
37 |
| - _image_count += 1 |
38 |
| - if is_label: |
39 |
| - await itk_viewer.setLabelImage(image) |
40 |
| - else: |
41 |
| - await itk_viewer.setImage(image, name) |
42 |
| - elif HAVE_ITK: |
| 13 | +async def _get_viewer_image(image): |
| 14 | + if HAVE_ITK: |
43 | 15 | import itk
|
44 | 16 | if isinstance(image, itk.Image):
|
45 |
| - wasm_image = itk_image_to_wasm_image(image) |
46 |
| - name = image.GetObjectName() |
47 |
| - if not name: |
48 |
| - name = f"image {_image_count}" |
49 |
| - _image_count += 1 |
50 |
| - if is_label: |
51 |
| - await itk_viewer.setLabelImage(wasm_image) |
52 |
| - else: |
53 |
| - await itk_viewer.setImage(wasm_image, name) |
| 17 | + return itk_image_to_wasm_image(image) |
54 | 18 | if HAVE_VTK:
|
55 | 19 | import vtk
|
56 | 20 | if isinstance(image, vtk.vtkImageData):
|
57 |
| - ndarray = vtk_image_to_ndarray(image) |
58 |
| - if not name: |
59 |
| - name = f"image {_image_count}" |
60 |
| - _image_count += 1 |
61 |
| - if is_label: |
62 |
| - await itk_viewer.setLabelImage(ndarray) |
63 |
| - else: |
64 |
| - await itk_viewer.setImage(ndarray, name) |
| 21 | + return vtk_image_to_ndarray(image) |
65 | 22 | if HAVE_DASK:
|
66 | 23 | import dask
|
67 | 24 | if isinstance(image, dask.array.core.Array):
|
68 |
| - ndarray = dask_array_to_ndarray(image) |
69 |
| - name = image.name |
70 |
| - if not name: |
71 |
| - name = f"image {_image_count}" |
72 |
| - _image_count += 1 |
73 |
| - if is_label: |
74 |
| - await itk_viewer.setLabelImage(ndarray) |
75 |
| - else: |
76 |
| - await itk_viewer.setImage(ndarray, name) |
| 25 | + return dask_array_to_ndarray(image) |
77 | 26 | if HAVE_TORCH:
|
78 | 27 | import torch
|
79 | 28 | if isinstance(image, torch.Tensor):
|
80 |
| - if not name: |
81 |
| - name = f"image {_image_count}" |
82 |
| - _image_count += 1 |
83 |
| - if is_label: |
84 |
| - await itk_viewer.setLabelImage(image.numpy()) |
85 |
| - else: |
86 |
| - await itk_viewer.setImage(image.numpy(), name) |
| 29 | + return image.numpy() |
87 | 30 | if HAVE_XARRAY:
|
88 | 31 | import xarray
|
89 | 32 | if isinstance(image, xarray.DataArray):
|
90 |
| - ndarray = xarray_data_array_to_numpy(image) |
91 |
| - name = image.name |
92 |
| - if not name: |
93 |
| - name = f"image {_image_count}" |
94 |
| - _image_count += 1 |
95 |
| - if is_label: |
96 |
| - await itk_viewer.setLabelImage(ndarray) |
97 |
| - else: |
98 |
| - await itk_viewer.setImage(ndarray, name) |
| 33 | + return xarray_data_array_to_numpy(image) |
99 | 34 | if isinstance(image, xarray.Dataset):
|
100 |
| - ndarray = xarray_data_set_to_numpy(image) |
101 |
| - if not name: |
102 |
| - name = f"image {_image_count}" |
103 |
| - _image_count += 1 |
104 |
| - if is_label: |
105 |
| - await itk_viewer.setLabelImage(ndarray) |
106 |
| - else: |
107 |
| - await itk_viewer.setImage(ndarray, name) |
| 35 | + return xarray_data_set_to_numpy(image) |
108 | 36 |
|
109 | 37 |
|
110 |
| -async def _set_viewer_point_sets(itk_viewer, point_sets): |
111 |
| - if isinstance(point_sets, itkwasm.PointSet): |
112 |
| - await itk_viewer.setPointSets(point_sets) |
113 |
| - elif isinstance(point_sets, np.ndarray): |
114 |
| - await itk_viewer.setPointSets(point_sets) |
115 |
| - elif isinstance(point_sets, zarr.Group): |
116 |
| - await itk_viewer.setPointSets(point_sets) |
| 38 | +async def _get_viewer_point_sets(itk_viewer, point_sets): |
117 | 39 | if HAVE_VTK:
|
118 | 40 | import vtk
|
119 | 41 | if isinstance(point_sets, vtk.vtkPolyData):
|
120 |
| - vtkjs_polydata = vtk_polydata_to_vtkjs(point_sets) |
121 |
| - await itk_viewer.setPointSets(vtkjs_polydata) |
| 42 | + return vtk_polydata_to_vtkjs(point_sets) |
122 | 43 | if HAVE_DASK:
|
123 | 44 | import dask
|
124 | 45 | if isinstance(point_sets, dask.array.core.Array):
|
125 |
| - ndarray = dask_array_to_ndarray(point_sets) |
126 |
| - await itk_viewer.setPointSets(ndarray) |
| 46 | + return dask_array_to_ndarray(point_sets) |
127 | 47 | if HAVE_TORCH:
|
128 | 48 | import torch
|
129 | 49 | if isinstance(point_sets, torch.Tensor):
|
130 |
| - await itk_viewer.setPointSets(point_sets.numpy()) |
| 50 | + return point_sets.numpy() |
131 | 51 | if HAVE_XARRAY:
|
132 | 52 | import xarray
|
133 | 53 | if isinstance(point_sets, xarray.DataArray):
|
134 |
| - ndarray = xarray_data_array_to_numpy(point_sets) |
135 |
| - await itk_viewer.setPointSets(ndarray) |
| 54 | + return xarray_data_array_to_numpy(point_sets) |
136 | 55 | if isinstance(point_sets, xarray.Dataset):
|
137 |
| - ndarray = xarray_data_set_to_numpy(point_sets) |
138 |
| - await itk_viewer.setPointSets(ndarray) |
| 56 | + return xarray_data_set_to_numpy(point_sets) |
139 | 57 |
|
140 | 58 |
|
141 | 59 | def _detect_render_type(data, input_type) -> RenderType:
|
|
0 commit comments