1- __all__ = ['to_itk_image' , 'to_point_set' , 'to_geometry' ]
1+ __all__ = ['to_itk_image' , 'to_point_set' , 'to_geometry' , 'vtkjs_to_zarr' , 'zarr_to_vtkjs' ]
22
33import itk
44import numpy as np
@@ -41,6 +41,77 @@ def is_arraylike(arr):
4141 have_skan = True
4242except ImportError :
4343 pass
44+ have_zarr = False
45+ try :
46+ import zarr
47+ have_zarr = True
48+ except ImportError :
49+ pass
50+
51+ def vtkjs_to_zarr (vtkjs , group , chunks = True ):
52+ """Convert a vtk.js-like Python object to a Zarr Group.
53+
54+ Parameters
55+ ----------
56+
57+ vtkjs: dictionary, required
58+ The vtk.js-like data structure to convert.
59+
60+ group: zarr.Group, required
61+ The Zarr group to store the result.
62+
63+ chunks: bool or int or tuple of ints, optional
64+ The chunk size passed to zarr.creation.create.
65+ """
66+ for key , value in vtkjs .items ():
67+ if key == 'vtkClass' :
68+ group .attrs [key ] = value
69+ elif key == 'arrays' :
70+ for index , arr in enumerate (value ):
71+ vtkjs_to_zarr (arr ,
72+ group .create_group ('arrays/' + str (index ), True ),
73+ chunks = chunks )
74+ elif isinstance (value , dict ):
75+ vtkjs_to_zarr (value ,
76+ group .create_group (key , True ),
77+ chunks = chunks )
78+ elif isinstance (value , np .ndarray ):
79+ group .array (key , value , chunks = chunks )
80+ else :
81+ group .attrs [key ] = value
82+ return group
83+
84+ def zarr_to_vtkjs (group ):
85+ """Convert Zarr Group that contains vtk.js data structure to a Python-like object.
86+
87+ Parameters
88+ ----------
89+
90+ group: zarr.Group, required
91+ The Zarr group to convert.
92+ """
93+
94+ def process_group (group , result ):
95+ for key , value in group .attrs .items ():
96+ result [key ] = value
97+ for name , value in group .arrays ():
98+ result [name ] = np .asarray (value )
99+ for name , value in group .groups ():
100+ if name == 'arrays' :
101+ nested = []
102+ for index , subgroup in value .groups ():
103+ subresult = dict ()
104+ process_group (subgroup , subresult )
105+ nested .append (subresult )
106+ result [name ] = nested
107+ else :
108+ nested = dict ()
109+ process_group (value , nested )
110+ result [name ] = nested
111+ result = dict ()
112+ process_group (group , result )
113+ return result
114+
44115
45116_itk_pixel_to_vtkjs_type_components = {
46117 itk .SC : ('Int8Array' , 1 ),
@@ -53,7 +124,6 @@ def is_arraylike(arr):
53124 itk .D : ('Float64Array' , 1 ),
54125}
55126
56-
57127def _vtk_to_vtkjs (data_array ):
58128 from vtk .util .numpy_support import vtk_to_numpy
59129 # From vtkType.h
@@ -315,6 +385,8 @@ def to_point_set(point_set_like): # noqa: C901
315385 point_set ['cellData' ] = vtkjs_cell_data
316386
317387 return point_set
388+ elif isinstance (point_set_like , zarr .Group ):
389+ return zarr_to_vtkjs (point_set_like )
318390
319391 return None
320392
@@ -542,5 +614,7 @@ def to_geometry(geometry_like): # noqa: C901
542614 geometry_filter .Update ()
543615 geometry = to_geometry (geometry_filter .GetOutput ())
544616 return geometry
617+ elif isinstance (geometry_like , zarr .Group ):
618+ return zarr_to_vtkjs (geometry_like )
545619
546620 return None
0 commit comments