Skip to content

Commit a4d3866

Browse files
committed
feat(mesh-io): python wasi mz3 support
1 parent 968322e commit a4d3866

14 files changed

+401
-0
lines changed

packages/mesh-io/python/itkwasm-mesh-io-wasi/itkwasm_mesh_io_wasi/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from .free_surfer_ascii_write_mesh import free_surfer_ascii_write_mesh
1313
from .free_surfer_binary_read_mesh import free_surfer_binary_read_mesh
1414
from .free_surfer_binary_write_mesh import free_surfer_binary_write_mesh
15+
from .mz3_read_mesh import mz3_read_mesh
16+
from .mz3_write_mesh import mz3_write_mesh
1517
from .obj_read_mesh import obj_read_mesh
1618
from .obj_write_mesh import obj_write_mesh
1719
from .off_read_mesh import off_read_mesh
@@ -27,6 +29,8 @@
2729
from .wasm_zstd_read_mesh import wasm_zstd_read_mesh
2830
from .wasm_zstd_write_mesh import wasm_zstd_write_mesh
2931

32+
from .mz3_read_point_set import mz3_read_point_set
33+
from .mz3_write_point_set import mz3_write_point_set
3034
from .obj_read_point_set import obj_read_point_set
3135
from .obj_write_point_set import obj_write_point_set
3236
from .off_read_point_set import off_read_point_set

packages/mesh-io/python/itkwasm-mesh-io-wasi/itkwasm_mesh_io_wasi/extension_to_mesh_io.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
('.byu', 'byu'),
66
('.fsa', 'free_surfer_ascii'),
77
('.fsb', 'free_surfer_binary'),
8+
('.mz3', 'mz3'),
89
('.obj', 'obj'),
910
('.off', 'off'),
1011
('.stl', 'stl'),

packages/mesh-io/python/itkwasm-mesh-io-wasi/itkwasm_mesh_io_wasi/extension_to_point_set_io.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from collections import OrderedDict
22

33
extension_to_point_set_io = OrderedDict([
4+
('.mz3', 'mz3'),
45
('.vtk', 'vtk_poly_data'),
56
('.obj', 'obj'),
67
('.off', 'off'),

packages/mesh-io/python/itkwasm-mesh-io-wasi/itkwasm_mesh_io_wasi/mesh_io_index.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
'byu',
44
'free_surfer_ascii',
55
'free_surfer_binary',
6+
'mz3',
7+
'obj',
68
'obj',
79
'off',
810
'stl',
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Generated file. To retain edits, remove this comment.
2+
3+
from pathlib import Path, PurePosixPath
4+
import os
5+
from typing import Dict, Tuple, Optional, List, Any
6+
7+
from importlib_resources import files as file_resources
8+
9+
_pipeline = None
10+
11+
from itkwasm import (
12+
InterfaceTypes,
13+
PipelineOutput,
14+
PipelineInput,
15+
Pipeline,
16+
BinaryFile,
17+
Mesh,
18+
)
19+
20+
def mz3_read_mesh(
21+
serialized_mesh: os.PathLike,
22+
information_only: bool = False,
23+
) -> Tuple[Any, Mesh]:
24+
"""Read a mesh file format and convert it to the itk-wasm file format
25+
26+
:param serialized_mesh: Input mesh serialized in the file format
27+
:type serialized_mesh: os.PathLike
28+
29+
:param information_only: Only read mesh metadata -- do not read pixel data.
30+
:type information_only: bool
31+
32+
:return: Whether the input could be read. If false, the output mesh is not valid.
33+
:rtype: Any
34+
35+
:return: Output mesh
36+
:rtype: Mesh
37+
"""
38+
global _pipeline
39+
if _pipeline is None:
40+
_pipeline = Pipeline(file_resources('itkwasm_mesh_io_wasi').joinpath(Path('wasm_modules') / Path('mz3-read-mesh.wasi.wasm')))
41+
42+
pipeline_outputs: List[PipelineOutput] = [
43+
PipelineOutput(InterfaceTypes.JsonCompatible),
44+
PipelineOutput(InterfaceTypes.Mesh),
45+
]
46+
47+
pipeline_inputs: List[PipelineInput] = [
48+
PipelineInput(InterfaceTypes.BinaryFile, BinaryFile(PurePosixPath(serialized_mesh))),
49+
]
50+
51+
args: List[str] = ['--memory-io',]
52+
# Inputs
53+
if not Path(serialized_mesh).exists():
54+
raise FileNotFoundError("serialized_mesh does not exist")
55+
args.append(str(PurePosixPath(serialized_mesh)))
56+
# Outputs
57+
could_read_name = '0'
58+
args.append(could_read_name)
59+
60+
mesh_name = '1'
61+
args.append(mesh_name)
62+
63+
# Options
64+
input_count = len(pipeline_inputs)
65+
if information_only:
66+
args.append('--information-only')
67+
68+
69+
outputs = _pipeline.run(args, pipeline_outputs, pipeline_inputs)
70+
71+
result = (
72+
outputs[0].data,
73+
outputs[1].data,
74+
)
75+
return result
76+
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Generated file. To retain edits, remove this comment.
2+
3+
from pathlib import Path, PurePosixPath
4+
import os
5+
from typing import Dict, Tuple, Optional, List, Any
6+
7+
from importlib_resources import files as file_resources
8+
9+
_pipeline = None
10+
11+
from itkwasm import (
12+
InterfaceTypes,
13+
PipelineOutput,
14+
PipelineInput,
15+
Pipeline,
16+
BinaryFile,
17+
PointSet,
18+
)
19+
20+
def mz3_read_point_set(
21+
serialized_point_set: os.PathLike,
22+
information_only: bool = False,
23+
) -> Tuple[Any, PointSet]:
24+
"""Read a point set file format and convert it to the itk-wasm file format
25+
26+
:param serialized_point_set: Input point set serialized in the file format
27+
:type serialized_point_set: os.PathLike
28+
29+
:param information_only: Only read point set metadata -- do not read pixel data.
30+
:type information_only: bool
31+
32+
:return: Whether the input could be read. If false, the output point set is not valid.
33+
:rtype: Any
34+
35+
:return: Output point set
36+
:rtype: PointSet
37+
"""
38+
global _pipeline
39+
if _pipeline is None:
40+
_pipeline = Pipeline(file_resources('itkwasm_mesh_io_wasi').joinpath(Path('wasm_modules') / Path('mz3-read-point-set.wasi.wasm')))
41+
42+
pipeline_outputs: List[PipelineOutput] = [
43+
PipelineOutput(InterfaceTypes.JsonCompatible),
44+
PipelineOutput(InterfaceTypes.PointSet),
45+
]
46+
47+
pipeline_inputs: List[PipelineInput] = [
48+
PipelineInput(InterfaceTypes.BinaryFile, BinaryFile(PurePosixPath(serialized_point_set))),
49+
]
50+
51+
args: List[str] = ['--memory-io',]
52+
# Inputs
53+
if not Path(serialized_point_set).exists():
54+
raise FileNotFoundError("serialized_point_set does not exist")
55+
args.append(str(PurePosixPath(serialized_point_set)))
56+
# Outputs
57+
could_read_name = '0'
58+
args.append(could_read_name)
59+
60+
point_set_name = '1'
61+
args.append(point_set_name)
62+
63+
# Options
64+
input_count = len(pipeline_inputs)
65+
if information_only:
66+
args.append('--information-only')
67+
68+
69+
outputs = _pipeline.run(args, pipeline_outputs, pipeline_inputs)
70+
71+
result = (
72+
outputs[0].data,
73+
outputs[1].data,
74+
)
75+
return result
76+
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Generated file. To retain edits, remove this comment.
2+
3+
from pathlib import Path, PurePosixPath
4+
import os
5+
from typing import Dict, Tuple, Optional, List, Any
6+
7+
from importlib_resources import files as file_resources
8+
9+
_pipeline = None
10+
11+
from itkwasm import (
12+
InterfaceTypes,
13+
PipelineOutput,
14+
PipelineInput,
15+
Pipeline,
16+
Mesh,
17+
BinaryFile,
18+
)
19+
20+
def mz3_write_mesh(
21+
mesh: Mesh,
22+
serialized_mesh: str,
23+
information_only: bool = False,
24+
use_compression: bool = False,
25+
binary_file_type: bool = False,
26+
) -> Tuple[Any]:
27+
"""Write an itk-wasm file format converted to an mesh file format
28+
29+
:param mesh: Input mesh
30+
:type mesh: Mesh
31+
32+
:param serialized_mesh: Output mesh
33+
:type serialized_mesh: str
34+
35+
:param information_only: Only write mesh metadata -- do not write pixel data.
36+
:type information_only: bool
37+
38+
:param use_compression: Use compression in the written file, if supported
39+
:type use_compression: bool
40+
41+
:param binary_file_type: Use a binary file type in the written file, if supported
42+
:type binary_file_type: bool
43+
44+
:return: Whether the input could be written. If false, the output mesh is not valid.
45+
:rtype: Any
46+
"""
47+
global _pipeline
48+
if _pipeline is None:
49+
_pipeline = Pipeline(file_resources('itkwasm_mesh_io_wasi').joinpath(Path('wasm_modules') / Path('mz3-write-mesh.wasi.wasm')))
50+
51+
pipeline_outputs: List[PipelineOutput] = [
52+
PipelineOutput(InterfaceTypes.JsonCompatible),
53+
PipelineOutput(InterfaceTypes.BinaryFile, BinaryFile(PurePosixPath(serialized_mesh))),
54+
]
55+
56+
pipeline_inputs: List[PipelineInput] = [
57+
PipelineInput(InterfaceTypes.Mesh, mesh),
58+
]
59+
60+
args: List[str] = ['--memory-io',]
61+
# Inputs
62+
args.append('0')
63+
# Outputs
64+
could_write_name = '0'
65+
args.append(could_write_name)
66+
67+
serialized_mesh_name = str(PurePosixPath(serialized_mesh))
68+
args.append(serialized_mesh_name)
69+
70+
# Options
71+
input_count = len(pipeline_inputs)
72+
if information_only:
73+
args.append('--information-only')
74+
75+
if use_compression:
76+
args.append('--use-compression')
77+
78+
if binary_file_type:
79+
args.append('--binary-file-type')
80+
81+
82+
outputs = _pipeline.run(args, pipeline_outputs, pipeline_inputs)
83+
84+
result = outputs[0].data
85+
return result
86+
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Generated file. To retain edits, remove this comment.
2+
3+
from pathlib import Path, PurePosixPath
4+
import os
5+
from typing import Dict, Tuple, Optional, List, Any
6+
7+
from importlib_resources import files as file_resources
8+
9+
_pipeline = None
10+
11+
from itkwasm import (
12+
InterfaceTypes,
13+
PipelineOutput,
14+
PipelineInput,
15+
Pipeline,
16+
PointSet,
17+
BinaryFile,
18+
)
19+
20+
def mz3_write_point_set(
21+
point_set: PointSet,
22+
serialized_point_set: str,
23+
information_only: bool = False,
24+
use_compression: bool = False,
25+
binary_file_type: bool = False,
26+
) -> Tuple[Any]:
27+
"""Write an ITK-Wasm file format converted to a point set file format
28+
29+
:param point_set: Input point set
30+
:type point_set: PointSet
31+
32+
:param serialized_point_set: Output point set
33+
:type serialized_point_set: str
34+
35+
:param information_only: Only write point set metadata -- do not write pixel data.
36+
:type information_only: bool
37+
38+
:param use_compression: Use compression in the written file, if supported
39+
:type use_compression: bool
40+
41+
:param binary_file_type: Use a binary file type in the written file, if supported
42+
:type binary_file_type: bool
43+
44+
:return: Whether the input could be written. If false, the output mesh is not valid.
45+
:rtype: Any
46+
"""
47+
global _pipeline
48+
if _pipeline is None:
49+
_pipeline = Pipeline(file_resources('itkwasm_mesh_io_wasi').joinpath(Path('wasm_modules') / Path('mz3-write-point-set.wasi.wasm')))
50+
51+
pipeline_outputs: List[PipelineOutput] = [
52+
PipelineOutput(InterfaceTypes.JsonCompatible),
53+
PipelineOutput(InterfaceTypes.BinaryFile, BinaryFile(PurePosixPath(serialized_point_set))),
54+
]
55+
56+
pipeline_inputs: List[PipelineInput] = [
57+
PipelineInput(InterfaceTypes.PointSet, point_set),
58+
]
59+
60+
args: List[str] = ['--memory-io',]
61+
# Inputs
62+
args.append('0')
63+
# Outputs
64+
could_write_name = '0'
65+
args.append(could_write_name)
66+
67+
serialized_point_set_name = str(PurePosixPath(serialized_point_set))
68+
args.append(serialized_point_set_name)
69+
70+
# Options
71+
input_count = len(pipeline_inputs)
72+
if information_only:
73+
args.append('--information-only')
74+
75+
if use_compression:
76+
args.append('--use-compression')
77+
78+
if binary_file_type:
79+
args.append('--binary-file-type')
80+
81+
82+
outputs = _pipeline.run(args, pipeline_outputs, pipeline_inputs)
83+
84+
result = outputs[0].data
85+
return result
86+

packages/mesh-io/python/itkwasm-mesh-io-wasi/itkwasm_mesh_io_wasi/point_set_io_index.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
point_set_io_index = [
22
'vtk_poly_data',
3+
'mz3',
34
'obj',
45
'off',
56
'wasm',

0 commit comments

Comments
 (0)