-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(mesh-io): python read and write point set wasi tests
- Loading branch information
Showing
29 changed files
with
666 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,5 +12,4 @@ | |
('.iwm', 'wasm'), | ||
('.iwm.cbor', 'wasm'), | ||
('.iwm.cbor.zst', 'wasmZstd'), | ||
('.bmp', 'bmp'), | ||
]) |
10 changes: 10 additions & 0 deletions
10
...python/itkwasm-mesh-io-emscripten/itkwasm_mesh_io_emscripten/extension_to_point_set_io.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from collections import OrderedDict | ||
|
||
extension_to_point_set_io = OrderedDict([ | ||
('.vtk', 'vtkPolyData'), | ||
('.obj', 'obj'), | ||
('.off', 'off'), | ||
('.iwm', 'wasm'), | ||
('.iwm.cbor', 'wasm'), | ||
('.iwm.cbor.zst', 'wasmZstd'), | ||
]) |
8 changes: 8 additions & 0 deletions
8
...esh-io/python/itkwasm-mesh-io-emscripten/itkwasm_mesh_io_emscripten/point_set_io_index.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
point_set_io_index = [ | ||
'vtk_poly_data', | ||
'obj', | ||
'off', | ||
'wasm', | ||
'wasm_zstd', | ||
] | ||
|
87 changes: 87 additions & 0 deletions
87
...h-io/python/itkwasm-mesh-io-emscripten/itkwasm_mesh_io_emscripten/read_point_set_async.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import os | ||
from typing import Optional, Union | ||
from pathlib import Path | ||
|
||
from itkwasm import ( | ||
PointSet, | ||
BinaryFile, | ||
) | ||
|
||
from .js_package import js_package | ||
|
||
from itkwasm.pyodide import ( | ||
to_js, | ||
to_py, | ||
js_resources | ||
) | ||
|
||
from .extension_to_point_set_io import extension_to_point_set_io | ||
from .point_set_io_index import point_set_io_index | ||
|
||
async def read_point_set_async( | ||
serialized_point_set: os.PathLike, | ||
information_only: bool = False, | ||
) -> PointSet: | ||
"""Read an point set file format and convert it to the ITK-Wasm file format. | ||
:param serialized_point_set: Input point set serialized in the file format | ||
:type serialized_point_set: os.PathLike | ||
:param information_only: Only read point set metadata -- do not read pixel data. | ||
:type information_only: bool | ||
:return: Output point set | ||
:rtype: PointSet | ||
""" | ||
js_module = await js_package.js_module | ||
web_worker = js_resources.web_worker | ||
|
||
kwargs = {} | ||
if information_only: | ||
kwargs["informationOnly"] = to_js(information_only) | ||
|
||
extension = ''.join(Path(serialized_point_set).suffixes) | ||
|
||
io = None | ||
if extension in extension_to_point_set_io: | ||
func = f"{extension_to_point_set_io[extension]}ReadPointSet" | ||
io = getattr(js_module, func) | ||
else: | ||
for ioname in point_set_io_index: | ||
func = f"{ioname}ReadPointSet" | ||
io = getattr(js_module, func) | ||
outputs = await io(to_js(BinaryFile(serialized_point_set)), webWorker=web_worker, noCopy=True, **kwargs) | ||
outputs_object_map = outputs.as_object_map() | ||
web_worker = outputs_object_map['webWorker'] | ||
js_resources.web_worker = web_worker | ||
could_read = to_py(outputs_object_map['couldRead']) | ||
if could_read: | ||
point_set = to_py(outputs_object_map['point_set']) | ||
return point_set | ||
|
||
if io is None: | ||
raise RuntimeError(f"Could not find an point_set reader for {extension}") | ||
|
||
outputs = await io(to_js(BinaryFile(serialized_point_set)), webWorker=web_worker, noCopy=True, **kwargs) | ||
outputs_object_map = outputs.as_object_map() | ||
web_worker = outputs_object_map['webWorker'] | ||
could_read = to_py(outputs_object_map['couldRead']) | ||
|
||
if not could_read: | ||
raise RuntimeError(f"Could not read {serialized_point_set}") | ||
|
||
js_resources.web_worker = web_worker | ||
|
||
point_set = to_py(outputs_object_map['point_set']) | ||
|
||
return point_set | ||
|
||
async def pointsetread_async( | ||
serialized_point_set: os.PathLike, | ||
information_only: bool = False, | ||
) -> PointSet: | ||
return await read_point_set_async(serialized_point_set, information_only=information_only) | ||
|
||
pointsetread_async.__doc__ = f"""{read_point_set_async.__doc__} | ||
Alias for read_point_set_async. | ||
""" |
94 changes: 94 additions & 0 deletions
94
...-io/python/itkwasm-mesh-io-emscripten/itkwasm_mesh_io_emscripten/write_point_set_async.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import os | ||
import importlib | ||
from pathlib import Path | ||
from typing import Optional, Union | ||
|
||
from itkwasm import PointSet, PixelTypes, IntTypes, FloatTypes, BinaryFile | ||
|
||
from itkwasm.pyodide import ( | ||
to_js, | ||
to_py, | ||
js_resources | ||
) | ||
|
||
from .js_package import js_package | ||
|
||
from .extension_to_point_set_io import extension_to_point_set_io | ||
from .point_set_io_index import point_set_io_index | ||
|
||
async def write_point_set_async( | ||
point_set: PointSet, | ||
serialized_point_set: os.PathLike, | ||
information_only: bool = False, | ||
use_compression: bool = False, | ||
) -> None: | ||
"""Write an ITK-Wasm PointSet to a point set file format. | ||
:param point_set: Input point set | ||
:type point_set: PointSet | ||
:param serialized_point_set: Output point set serialized in the file format. | ||
:type serialized_point_set: str | ||
:param information_only: Only write point set metadata -- do not write pixel data. | ||
:type information_only: bool | ||
:param use_compression: Use compression in the written file | ||
:type use_compression: bool | ||
:param serialized_point_set: Input point set serialized in the file format | ||
:type serialized_point_set: os.PathLike | ||
""" | ||
js_module = await js_package.js_module | ||
web_worker = js_resources.web_worker | ||
|
||
kwargs = {} | ||
if information_only: | ||
kwargs["informationOnly"] = to_js(information_only) | ||
if use_compression: | ||
kwargs["useCompression"] = to_js(use_compression) | ||
|
||
extension = ''.join(Path(serialized_point_set).suffixes) | ||
|
||
io = None | ||
if extension in extension_to_point_set_io: | ||
func = f"{extension_to_point_set_io[extension]}WritePointSet" | ||
io = getattr(js_module, func) | ||
else: | ||
for ioname in point_set_io_index: | ||
func = f"{ioname}WritePointSet" | ||
io = getattr(js_module, func) | ||
outputs = await io(to_js(point_set), to_js(serialized_point_set), webWorker=web_worker, noCopy=True, **kwargs) | ||
outputs_object_map = outputs.as_object_map() | ||
web_worker = outputs_object_map['webWorker'] | ||
js_resources.web_worker = web_worker | ||
could_write = to_py(outputs_object_map['couldWrite']) | ||
if could_write: | ||
to_py(outputs_object_map['serializedPointSet']) | ||
return | ||
|
||
if io is None: | ||
raise RuntimeError(f"Could not find an point_set writer for {extension}") | ||
|
||
outputs = await io(to_js(point_set), to_js(serialized_point_set), webWorker=web_worker, noCopy=True, **kwargs) | ||
outputs_object_map = outputs.as_object_map() | ||
web_worker = outputs_object_map['webWorker'] | ||
js_resources.web_worker = web_worker | ||
could_write = to_py(outputs_object_map['couldWrite']) | ||
|
||
if not could_write: | ||
raise RuntimeError(f"Could not write {serialized_point_set}") | ||
|
||
to_py(outputs_object_map['serializedPointSet']) | ||
|
||
async def point_setwrite_async( | ||
point_set: PointSet, | ||
serialized_point_set: os.PathLike, | ||
information_only: bool = False, | ||
use_compression: bool = False, | ||
) -> None: | ||
return write_point_set_async(point_set, serialized_point_set, information_only=information_only, use_compression=use_compression) | ||
|
||
point_setwrite_async.__doc__ = f"""{write_point_set_async.__doc__} | ||
Alias for write_point_set. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,5 +12,4 @@ | |
('.iwm', 'wasm'), | ||
('.iwm.cbor', 'wasm'), | ||
('.iwm.cbor.zst', 'wasm_zstd'), | ||
('.bmp', 'bmp'), | ||
]) |
10 changes: 10 additions & 0 deletions
10
...ges/mesh-io/python/itkwasm-mesh-io-wasi/itkwasm_mesh_io_wasi/extension_to_point_set_io.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from collections import OrderedDict | ||
|
||
extension_to_point_set_io = OrderedDict([ | ||
('.vtk', 'vtk_poly_data'), | ||
('.obj', 'obj'), | ||
('.off', 'off'), | ||
('.iwm', 'wasm'), | ||
('.iwm.cbor', 'wasm'), | ||
('.iwm.cbor.zst', 'wasm_zstd'), | ||
]) |
8 changes: 8 additions & 0 deletions
8
packages/mesh-io/python/itkwasm-mesh-io-wasi/itkwasm_mesh_io_wasi/point_set_io_index.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
point_set_io_index = [ | ||
'vtk_poly_data', | ||
'obj', | ||
'off', | ||
'wasm', | ||
'wasm_zstd', | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.