Skip to content

Commit

Permalink
fix(Python): Support Pyodide Image metadata conversion as Map
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Apr 19, 2023
1 parent f055478 commit 38a2fb3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/core/python/itkwasm/itkwasm/pyodide.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,11 @@ def to_js(py):
import js
if isinstance(py, Image):
image_dict = asdict(py)
print('to_js image dict', image_dict['imageType'])
image_dict['direction'] = image_dict['direction'].ravel()
if image_dict['data'] is not None:
image_dict['data'] = image_dict['data'].ravel()
if image_dict['metadata']:
image_dict['metadata'] = pyodide.ffi.to_js(image_dict['metadata'], 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)
Expand Down
5 changes: 5 additions & 0 deletions packages/core/python/itkwasm/test/test_pyodide.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ async def test_image_conversion(selenium, package_wheel):
image.size = [2, 2]

assert isinstance(image.metadata, dict)
image.metadata['a_string'] = 'some text'
image.metadata['an int'] = 3
assert image.data == None
image.data = np.arange(4, dtype=np.uint8).reshape((2,2))
image_js = to_js(image)
Expand All @@ -55,6 +57,9 @@ async def test_image_conversion(selenium, package_wheel):
assert image_py.size[0] == 2
assert image_py.size[1] == 2

assert image_py.metadata['a_string'] == 'some text'
assert image_py.metadata['an int'] == 3

assert isinstance(image_py.metadata, dict)
assert np.array_equal(image_py.data, np.arange(4, dtype=np.uint8).reshape((2,2)))

Expand Down

0 comments on commit 38a2fb3

Please sign in to comment.