Skip to content

Commit

Permalink
test(mesh-io): add emscripten test_vtk
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Nov 28, 2023
1 parent 53c1488 commit ab054ce
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Source = "https://github.com/InsightSoftwareConsortium/itk-wasm"
[tool.hatch.envs.default.scripts]
test = [
"hatch build -t wheel",
"pytest --dist-dir=./dist --rt=chrome",
"pytest -s --dist-dir=./dist --rt=chrome",
]
download-pyodide = [
"curl -L https://github.com/pyodide/pyodide/releases/download/0.24.1/pyodide-0.24.1.tar.bz2 -o pyodide.tar.bz2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@

from pytest_pyodide import run_in_pyodide

from itkwasm_image_io_emscripten import __version__ as test_package_version
from itkwasm_mesh_io_emscripten import __version__ as test_package_version

@pytest.fixture
def package_wheel():
return f"itkwasm_image_io_emscripten-{test_package_version}-py3-none-any.whl"
return f"itkwasm_mesh_io_emscripten-{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') / 'cthead1.png',
Path('input') / 'biorad.pic',
Path('input') / 'brainweb165a10f17.mha',
Path('input') / 'cow.vtk',
]
data = {}
for f in test_files:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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_vtk_poly_data_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_emscripten import vtk_poly_data_read_mesh_async, vtk_poly_data_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_file_path = 'cow.vtk'
write_input_data_to_fs(input_data, test_file_path)

assert Path(test_file_path).exists()

could_read, mesh = await vtk_poly_data_read_mesh_async(test_file_path)
assert could_read
verify_mesh(mesh)

test_output_file_path = 'cow_out.vtk'

use_compression = False
could_write = await vtk_poly_data_write_mesh_async(mesh, test_output_file_path, use_compression)
assert could_write

could_read, mesh = await vtk_poly_data_read_mesh_async(test_output_file_path)
assert could_read
verify_mesh(mesh)
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ def verify_mesh(mesh):
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 np.allclose(mesh.points.ravel()[0], 3.71636)
assert np.allclose(mesh.points.ravel()[1], 2.34339)
print(mesh.points.shape)
assert mesh.numberOfCells == 3263
assert mesh.cellBufferSize == 18856
assert mesh.cells[0] == 4
Expand Down

0 comments on commit ab054ce

Please # to comment.