forked from InsightSoftwareConsortium/ITK-Wasm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyodide.py
More file actions
266 lines (244 loc) · 12 KB
/
pyodide.py
File metadata and controls
266 lines (244 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
from dataclasses import dataclass, asdict
from typing import Optional
from .image import Image, ImageType
from .point_set import PointSet, PointSetType
from .mesh import Mesh, MeshType
from .polydata import PolyData, PolyDataType
from .transform import Transform, TransformType, TransformList
from .binary_file import BinaryFile
from .binary_stream import BinaryStream
from .text_file import TextFile
from .text_stream import TextStream
from .float_types import FloatTypes
from .int_types import IntTypes
from .json_compatible import JsonCompatible
from .to_numpy_array import buffer_to_numpy_array
@dataclass
class JsPackageConfig:
module_url: str
pipelines_base_url: Optional[str] = None
pipeline_worker_url: Optional[str] = None
class JsPackage:
def __init__(self, config: JsPackageConfig):
self._config = config
self._js_module = None
@property
def config(self):
return self._config
@config.setter
def config(self, value):
self._config = value
@property
async def js_module(self):
if self._js_module is not None:
return self._js_module
from pyodide.code import run_js
js_module = await run_js(f"import('{self._config.module_url}')")
if self._config.pipelines_base_url is not None:
js_module.setPipelinesBaseUrl(self._config.pipelines_base_url)
if self._config.pipeline_worker_url is not None:
js_module.setPipelineWorkerUrl(self._config.pipeline_worker_url)
self._js_module = js_module
return js_module
class JsResources:
def __init__(self):
self._web_worker = None
@property
def web_worker(self):
return self._web_worker
@web_worker.setter
def web_worker(self, value):
self._web_worker = value
js_resources = JsResources()
def to_py(js_proxy):
import pyodide
if hasattr(js_proxy, "constructor") and js_proxy.constructor.name == "Uint8Array":
return js_proxy.to_bytes()
elif isinstance(js_proxy, pyodide.ffi.JsArray):
return [to_py(value) for value in js_proxy]
elif hasattr(js_proxy, "imageType"):
image_dict = js_proxy.to_py()
image_type = ImageType(**image_dict["imageType"])
image_dict["imageType"] = image_type
dimension = image_type.dimension
component_type = image_type.componentType
image_dict["direction"] = buffer_to_numpy_array(str(FloatTypes.Float64), image_dict["direction"]).reshape(
(dimension, dimension)
)
shape = list(image_dict["size"])[::-1]
if image_type.components > 1:
shape.append(image_type.components)
if image_dict["data"] is not None:
image_dict["data"] = buffer_to_numpy_array(component_type, image_dict["data"]).reshape(tuple(shape))
return Image(**image_dict)
elif hasattr(js_proxy, "pointSetType"):
point_set_dict = js_proxy.to_py()
point_set_type = PointSetType(**point_set_dict["pointSetType"])
point_set_dict["pointSetType"] = point_set_type
dimension = point_set_type.dimension
point_component_type = point_set_type.pointComponentType
point_pixel_component_type = point_set_type.pointPixelComponentType
if point_set_dict["points"] is not None:
point_set_dict["points"] = buffer_to_numpy_array(point_component_type, point_set_dict["points"]).reshape(
(-1, dimension)
)
if point_set_dict["pointData"] is not None:
point_set_dict["pointData"] = buffer_to_numpy_array(point_pixel_component_type, point_set_dict["pointData"])
return PointSet(**point_set_dict)
elif hasattr(js_proxy, "meshType"):
mesh_dict = js_proxy.to_py()
mesh_type = MeshType(**mesh_dict["meshType"])
mesh_dict["meshType"] = mesh_type
dimension = mesh_type.dimension
point_component_type = mesh_type.pointComponentType
point_pixel_component_type = mesh_type.pointPixelComponentType
cell_component_type = mesh_type.cellComponentType
cell_pixel_component_type = mesh_type.cellPixelComponentType
if mesh_dict["points"] is not None:
mesh_dict["points"] = buffer_to_numpy_array(point_component_type, mesh_dict["points"]).reshape(
(-1, dimension)
)
if mesh_dict["pointData"] is not None:
mesh_dict["pointData"] = buffer_to_numpy_array(point_pixel_component_type, mesh_dict["pointData"])
if mesh_dict["cells"] is not None:
mesh_dict["cells"] = buffer_to_numpy_array(cell_component_type, mesh_dict["cells"])
if mesh_dict["cellData"] is not None:
mesh_dict["cellData"] = buffer_to_numpy_array(cell_pixel_component_type, mesh_dict["cellData"])
return Mesh(**mesh_dict)
elif hasattr(js_proxy, "polyDataType"):
polydata_dict = js_proxy.to_py()
polydata_type = PolyDataType(**polydata_dict["polyDataType"])
polydata_dict["polyDataType"] = polydata_type
point_pixel_component_type = polydata_type.pointPixelComponentType
cell_pixel_component_type = polydata_type.cellPixelComponentType
if polydata_dict["points"] is not None:
polydata_dict["points"] = buffer_to_numpy_array(str(FloatTypes.Float32), polydata_dict["points"]).reshape(
(-1, 3)
)
if polydata_dict["vertices"] is not None:
polydata_dict["vertices"] = buffer_to_numpy_array(str(IntTypes.UInt32), polydata_dict["vertices"])
if polydata_dict["lines"] is not None:
polydata_dict["lines"] = buffer_to_numpy_array(str(IntTypes.UInt32), polydata_dict["lines"])
if polydata_dict["polygons"] is not None:
polydata_dict["polygons"] = buffer_to_numpy_array(str(IntTypes.UInt32), polydata_dict["polygons"])
if polydata_dict["triangleStrips"] is not None:
polydata_dict["triangleStrips"] = buffer_to_numpy_array(
str(IntTypes.UInt32), polydata_dict["triangleStrips"]
)
if polydata_dict["pointData"] is not None:
polydata_dict["pointData"] = buffer_to_numpy_array(point_pixel_component_type, polydata_dict["pointData"])
if polydata_dict["cellData"] is not None:
polydata_dict["cellData"] = buffer_to_numpy_array(cell_pixel_component_type, polydata_dict["cellData"])
return PolyData(**polydata_dict)
elif hasattr(js_proxy, "transformType"):
transform_dict = js_proxy.to_py()
transform_type = TransformType(**transform_dict["transformType"])
transform_dict["transformType"] = transform_type
parameters_value_type = transform_type.parametersValueType
if transform_dict["fixedParameters"] is not None:
transform_dict["fixedParameters"] = buffer_to_numpy_array(
parameters_value_type, transform_dict["fixedParameters"]
)
if transform_dict["parameters"] is not None:
transform_dict["parameters"] = buffer_to_numpy_array(
parameters_value_type, transform_dict["parameters"]
)
return Transform(**transform_dict)
elif hasattr(js_proxy, "path") and hasattr(js_proxy, "data") and isinstance(js_proxy.data, str):
with open(js_proxy.path, "w") as fp:
fp.write(js_proxy.data)
return TextFile(path=js_proxy.path)
elif hasattr(js_proxy, "path") and hasattr(js_proxy, "data"):
with open(js_proxy.path, "wb") as fp:
js_proxy.data.to_file(fp)
return BinaryFile(path=js_proxy.path)
elif hasattr(js_proxy, "data") and isinstance(js_proxy.data, str):
text_stream_dict = js_proxy.to_py()
return TextStream(**text_stream_dict)
elif hasattr(js_proxy, "data"):
binary_stream_dict = js_proxy.to_py()
binary_stream_dict["data"] = bytes(binary_stream_dict["data"])
return BinaryStream(**binary_stream_dict)
elif isinstance(js_proxy, pyodide.ffi.JsProxy):
return js_proxy.to_py()
# int, etc
return js_proxy
def to_js(py, **kwargs):
import pyodide
import js
if isinstance(py, list):
js_array = pyodide.ffi.to_js([])
for value in py:
js_array.append(to_js(value))
return js_array
elif isinstance(py, Image):
image_dict = asdict(py)
image_dict["direction"] = image_dict["direction"].ravel()
if image_dict["data"] is not None:
image_dict["data"] = image_dict["data"].ravel()
if "metadata" in image_dict:
image_dict["metadata"] = pyodide.ffi.to_js(image_dict["metadata"], dict_converter=js.Map.new)
else:
image_dict["metadata"] = pyodide.ffi.to_js(dict(), dict_converter=js.Map.new)
return pyodide.ffi.to_js(image_dict, dict_converter=js.Object.fromEntries)
elif isinstance(py, PointSet):
point_set_dict = asdict(py)
if point_set_dict["points"] is not None:
point_set_dict["points"] = point_set_dict["points"].ravel()
if point_set_dict["pointData"] is not None:
point_set_dict["pointData"] = point_set_dict["pointData"].ravel()
return pyodide.ffi.to_js(point_set_dict, dict_converter=js.Object.fromEntries)
elif isinstance(py, Mesh):
mesh_dict = asdict(py)
if mesh_dict["points"] is not None:
mesh_dict["points"] = mesh_dict["points"].ravel()
if mesh_dict["pointData"] is not None:
mesh_dict["pointData"] = mesh_dict["pointData"].ravel()
if mesh_dict["cells"] is not None:
mesh_dict["cells"] = mesh_dict["cells"].ravel()
if mesh_dict["cellData"] is not None:
mesh_dict["cellData"] = mesh_dict["cellData"].ravel()
return pyodide.ffi.to_js(mesh_dict, dict_converter=js.Object.fromEntries)
elif isinstance(py, PolyData):
polydata_dict = asdict(py)
if polydata_dict["points"] is not None:
polydata_dict["points"] = polydata_dict["points"].ravel()
if polydata_dict["vertices"] is not None:
polydata_dict["vertices"] = polydata_dict["vertices"].ravel()
if polydata_dict["lines"] is not None:
polydata_dict["lines"] = polydata_dict["lines"].ravel()
if polydata_dict["polygons"] is not None:
polydata_dict["polygons"] = polydata_dict["polygons"].ravel()
if polydata_dict["triangleStrips"] is not None:
polydata_dict["triangleStrips"] = polydata_dict["triangleStrips"].ravel()
if polydata_dict["pointData"] is not None:
polydata_dict["pointData"] = polydata_dict["pointData"].ravel()
if polydata_dict["cellData"] is not None:
polydata_dict["cellData"] = polydata_dict["cellData"].ravel()
return pyodide.ffi.to_js(polydata_dict, dict_converter=js.Object.fromEntries)
elif isinstance(py, Transform):
transform_dict = asdict(py)
if transform_dict["fixedParameters"] is not None:
transform_dict["fixedParameters"] = transform_dict["fixedParameters"].ravel()
if transform_dict["parameters"] is not None:
transform_dict["parameters"] = transform_dict["parameters"].ravel()
return pyodide.ffi.to_js(transform_dict, dict_converter=js.Object.fromEntries)
elif isinstance(py, TextStream):
text_stream_dict = asdict(py)
return pyodide.ffi.to_js(text_stream_dict, dict_converter=js.Object.fromEntries)
elif isinstance(py, BinaryStream):
binary_stream_dict = asdict(py)
return pyodide.ffi.to_js(binary_stream_dict, dict_converter=js.Object.fromEntries)
elif isinstance(py, BinaryFile):
binary_file_dict = asdict(py)
with open(py.path, "rb") as fp:
data = fp.read()
binary_file_dict["data"] = data
return pyodide.ffi.to_js(binary_file_dict, dict_converter=js.Object.fromEntries)
elif isinstance(py, TextFile):
text_file_dict = asdict(py)
with open(py.path, "r") as fp:
data = fp.read()
text_file_dict["data"] = data
return pyodide.ffi.to_js(text_file_dict, dict_converter=js.Object.fromEntries)
return pyodide.ffi.to_js(py, **kwargs, dict_converter=js.Object.fromEntries)