-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mesh-io): add read_mesh, read_mesh_async, write_mesh, write_mesh…
…_async
- Loading branch information
Showing
19 changed files
with
367 additions
and
14 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
2 changes: 0 additions & 2 deletions
2
packages/mesh-io/python/itkwasm-mesh-io-emscripten/itkwasm_mesh_io_emscripten/__init__.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
2 changes: 1 addition & 1 deletion
2
packages/mesh-io/python/itkwasm-mesh-io-emscripten/itkwasm_mesh_io_emscripten/_version.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 |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "0.1.0" | ||
__version__ = "0.2.0" |
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
packages/mesh-io/python/itkwasm-mesh-io-wasi/itkwasm_mesh_io_wasi/_version.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 |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "0.1.0" | ||
__version__ = "0.2.0" |
7 changes: 5 additions & 2 deletions
7
packages/mesh-io/python/itkwasm-mesh-io/itkwasm_mesh_io/__init__.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
2 changes: 1 addition & 1 deletion
2
packages/mesh-io/python/itkwasm-mesh-io/itkwasm_mesh_io/_version.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 |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "0.1.0" | ||
__version__ = "0.2.0" |
36 changes: 36 additions & 0 deletions
36
packages/mesh-io/python/itkwasm-mesh-io/itkwasm_mesh_io/read_mesh.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,36 @@ | ||
import os | ||
from typing import Optional, Union | ||
|
||
from itkwasm import ( | ||
environment_dispatch, | ||
Mesh, | ||
) | ||
|
||
def read_mesh( | ||
serialized_mesh: os.PathLike, | ||
information_only: bool = False, | ||
) -> Mesh: | ||
"""Read an mesh file format and convert it to the itk-wasm file format | ||
:param serialized_mesh: Input mesh serialized in the file format | ||
:type serialized_mesh: os.PathLike | ||
:param information_only: Only read mesh metadata -- do not read pixel data. | ||
:type information_only: bool | ||
:return: Output mesh | ||
:rtype: Mesh | ||
""" | ||
func = environment_dispatch("itkwasm_mesh_io", "read_mesh") | ||
output = func(serialized_mesh, information_only=information_only) | ||
return output | ||
|
||
def meshread( | ||
serialized_mesh: os.PathLike, | ||
information_only: bool = False, | ||
) -> Mesh: | ||
return read_mesh(serialized_mesh, information_only=information_only) | ||
|
||
meshread.__doc__ = f"""{read_mesh.__doc__} | ||
Alias for read_mesh. | ||
""" |
36 changes: 36 additions & 0 deletions
36
packages/mesh-io/python/itkwasm-mesh-io/itkwasm_mesh_io/read_mesh_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,36 @@ | ||
import os | ||
from typing import Optional, Union | ||
|
||
from itkwasm import ( | ||
environment_dispatch, | ||
Mesh, | ||
) | ||
|
||
async def read_mesh_async( | ||
serialized_mesh: os.PathLike, | ||
information_only: bool = False, | ||
) -> Mesh: | ||
"""Read an mesh file format and convert it to the itk-wasm file format | ||
:param serialized_mesh: Input mesh serialized in the file format | ||
:type serialized_mesh: os.PathLike | ||
:param information_only: Only read mesh metadata -- do not read pixel data. | ||
:type information_only: bool | ||
:return: Output mesh | ||
:rtype: Mesh | ||
""" | ||
func = environment_dispatch("itkwasm_mesh_io", "read_mesh_async") | ||
output = await func(serialized_mesh, information_only=information_only) | ||
return output | ||
|
||
async def meshread_async( | ||
serialized_mesh: os.PathLike, | ||
information_only: bool = False, | ||
) -> Mesh: | ||
return await read_mesh_async(serialized_mesh, information_only=information_only) | ||
|
||
meshread_async.__doc__ = f"""{read_mesh_async.__doc__} | ||
Alias for read_mesh. | ||
""" |
43 changes: 43 additions & 0 deletions
43
packages/mesh-io/python/itkwasm-mesh-io/itkwasm_mesh_io/write_mesh.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,43 @@ | ||
import os | ||
from typing import Dict, Tuple, Optional, List, Any | ||
|
||
from itkwasm import ( | ||
environment_dispatch, | ||
Mesh, | ||
) | ||
|
||
def write_mesh( | ||
mesh: Mesh, | ||
serialized_mesh: str, | ||
information_only: bool = False, | ||
use_compression: bool = False, | ||
) -> None: | ||
"""Write an itk-wasm file format converted to an mesh file format | ||
:param mesh: Input mesh | ||
:type mesh: Mesh | ||
:param serialized_mesh: Output mesh serialized in the file format. | ||
:type serialized_mesh: str | ||
:param information_only: Only write mesh metadata -- do not write pixel data. | ||
:type information_only: bool | ||
:param use_compression: Use compression in the written file | ||
:type use_compression: bool | ||
""" | ||
func = environment_dispatch("itkwasm_mesh_io", "write_mesh") | ||
func(mesh, serialized_mesh, information_only=information_only, use_compression=use_compression) | ||
return | ||
|
||
def meshwrite( | ||
mesh: Mesh, | ||
serialized_mesh: os.PathLike, | ||
information_only: bool = False, | ||
use_compression: bool = False, | ||
) -> None: | ||
return write_mesh(mesh, serialized_mesh, information_only=information_only, use_compression=use_compression) | ||
|
||
meshwrite.__doc__ = f"""{write_mesh.__doc__} | ||
Alias for write_mesh. | ||
""" |
43 changes: 43 additions & 0 deletions
43
packages/mesh-io/python/itkwasm-mesh-io/itkwasm_mesh_io/write_mesh_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,43 @@ | ||
import os | ||
from typing import Dict, Tuple, Optional, List, Any | ||
|
||
from itkwasm import ( | ||
environment_dispatch, | ||
Mesh, | ||
) | ||
|
||
async def write_mesh_async( | ||
mesh: Mesh, | ||
serialized_mesh: str, | ||
information_only: bool = False, | ||
use_compression: bool = False, | ||
) -> None: | ||
"""Write an itk-wasm file format converted to an mesh file format | ||
:param mesh: Input mesh | ||
:type mesh: Mesh | ||
:param serialized_mesh: Output mesh serialized in the file format. | ||
:type serialized_mesh: str | ||
:param information_only: Only write mesh metadata -- do not write pixel data. | ||
:type information_only: bool | ||
:param use_compression: Use compression in the written file | ||
:type use_compression: bool | ||
""" | ||
func = environment_dispatch("itkwasm_mesh_io", "write_mesh_async") | ||
await func(mesh, serialized_mesh, information_only=information_only, use_compression=use_compression) | ||
return | ||
|
||
async def meshwrite_async( | ||
mesh: Mesh, | ||
serialized_mesh: os.PathLike, | ||
information_only: bool = False, | ||
use_compression: bool = False, | ||
) -> None: | ||
return await write_mesh_async(mesh, serialized_mesh, information_only=information_only, use_compression=use_compression) | ||
|
||
meshwrite_async.__doc__ = f"""{write_mesh_async.__doc__} | ||
Alias for write_mesh. | ||
""" |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import pytest | ||
import sys | ||
|
||
if sys.version_info < (3,10): | ||
pytest.skip("Skipping pyodide tests on older Python", allow_module_level=True) | ||
|
||
from pytest_pyodide import run_in_pyodide | ||
|
||
from itkwasm_mesh_io import __version__ as test_package_version | ||
|
||
@pytest.fixture | ||
def package_wheel(): | ||
return f"itkwasm_mesh_io-{test_package_version}-py3-none-any.whl" | ||
|
||
@pytest.fixture | ||
def input_data(): | ||
from pathlib import Path | ||
input_base_path = Path('..', '..', 'test', 'data') | ||
test_files = [ | ||
Path('input') / 'cow.vtk', | ||
] | ||
data = {} | ||
for f in test_files: | ||
with open(input_base_path / f, 'rb') as fp: | ||
data[str(f.name)] = fp.read() | ||
return data |
48 changes: 48 additions & 0 deletions
48
packages/mesh-io/python/itkwasm-mesh-io/tests/test_read_write_mesh.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,48 @@ | ||
from itkwasm import IntTypes, FloatTypes | ||
import numpy as np | ||
|
||
from itkwasm_mesh_io import read_mesh, meshread, write_mesh, meshwrite | ||
|
||
from .common import test_input_path, test_output_path | ||
|
||
test_input_file_path = test_input_path / "cow.vtk" | ||
test_output_file_path = test_output_path / "read-write-cow.vtk" | ||
|
||
def verify_mesh(mesh): | ||
assert mesh.meshType.dimension == 3 | ||
assert mesh.meshType.pointComponentType == FloatTypes.Float32 | ||
assert mesh.meshType.pointPixelComponentType == IntTypes.Int8 | ||
assert mesh.numberOfPoints == 2903 | ||
assert np.allclose(mesh.points[0],3.71636) | ||
assert np.allclose(mesh.points[1],2.34339) | ||
assert mesh.numberOfCells == 3263 | ||
assert mesh.cellBufferSize == 18856 | ||
assert mesh.cells[0] == 4 | ||
assert mesh.cells[1] == 4 | ||
assert mesh.cells[2] == 250 | ||
|
||
def test_read_mesh(): | ||
mesh = read_mesh(test_input_file_path) | ||
verify_mesh(mesh) | ||
|
||
def test_meshread(): | ||
mesh = meshread(test_input_file_path) | ||
verify_mesh(mesh) | ||
|
||
def test_write_mesh(): | ||
mesh = read_mesh(test_input_file_path) | ||
|
||
use_compression = False | ||
write_mesh(mesh, test_output_file_path, use_compression=use_compression) | ||
|
||
mesh = read_mesh(test_output_file_path) | ||
verify_mesh(mesh) | ||
|
||
def test_meshwrite(): | ||
mesh = meshread(test_input_file_path) | ||
|
||
use_compression = False | ||
meshwrite(mesh, test_output_file_path, use_compression=use_compression) | ||
|
||
mesh = meshread(test_output_file_path) | ||
verify_mesh(mesh) |
52 changes: 52 additions & 0 deletions
52
packages/mesh-io/python/itkwasm-mesh-io/tests/test_read_write_mesh_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,52 @@ | ||
import sys | ||
|
||
if sys.version_info < (3,10): | ||
pytest.skip("Skipping pyodide tests on older Python", allow_module_level=True) | ||
|
||
from pytest_pyodide import run_in_pyodide | ||
from .fixtures import package_wheel, input_data | ||
|
||
@run_in_pyodide(packages=['micropip', 'numpy']) | ||
async def test_read_write_mesh_async(selenium, package_wheel, input_data): | ||
import micropip | ||
await micropip.install(package_wheel) | ||
def write_input_data_to_fs(input_data, filename): | ||
with open(filename, 'wb') as fp: | ||
fp.write(input_data[filename]) | ||
|
||
from pathlib import Path | ||
|
||
from itkwasm import FloatTypes, IntTypes | ||
import numpy as np | ||
|
||
from itkwasm_mesh_io import read_mesh_async, write_mesh_async | ||
|
||
|
||
def verify_mesh(mesh): | ||
assert mesh.meshType.dimension == 3 | ||
assert mesh.meshType.pointComponentType == FloatTypes.Float32 | ||
assert mesh.meshType.pointPixelComponentType == IntTypes.Int8 | ||
assert mesh.numberOfPoints == 2903 | ||
assert np.allclose(mesh.points.ravel()[0], 3.71636) | ||
assert np.allclose(mesh.points.ravel()[1], 2.34339) | ||
assert mesh.numberOfCells == 3263 | ||
assert mesh.cellBufferSize == 18856 | ||
assert mesh.cells[0] == 4 | ||
assert mesh.cells[1] == 4 | ||
assert mesh.cells[2] == 250 | ||
|
||
test_input_file_path = 'cow.vtk' | ||
test_output_file_path = "read-write-cow.vtk" | ||
write_input_data_to_fs(input_data, test_input_file_path) | ||
|
||
assert Path(test_input_file_path).exists() | ||
|
||
|
||
mesh = await read_mesh_async(test_input_file_path) | ||
verify_mesh(mesh) | ||
|
||
use_compression = False | ||
await write_mesh_async(mesh, test_output_file_path, use_compression=use_compression) | ||
|
||
mesh = await read_mesh_async(test_output_file_path) | ||
verify_mesh(mesh) |
Oops, something went wrong.