Skip to content

Commit

Permalink
BUG: wasm_type_from_image_type for VariableLengthVector
Browse files Browse the repository at this point in the history
And other fixes.
  • Loading branch information
thewtex committed May 2, 2023
1 parent 7141f37 commit 99f16a0
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 30 deletions.
28 changes: 28 additions & 0 deletions Wrapping/Generators/Python/Tests/helpers.py
Expand Up @@ -16,9 +16,37 @@
#
# ==========================================================================*/
from itk.support import helpers
import itk

name = "MedianImageFilter"

snake = helpers.camel_to_snake_case(name)

assert snake == "median_image_filter"

image = itk.Image[itk.F, 2].New()
image.SetRegions([10,10])
image.Allocate()

wasm_type = helpers.wasm_type_from_image_type(image)
assert wasm_type['dimension'] == 2
assert wasm_type['componentType'] == 'float32'
assert wasm_type['pixelType'] == 'Scalar'
assert wasm_type['components'] == 1

image_type = helpers.image_type_from_wasm_type(wasm_type)
assert image_type == type(image)

image = itk.VectorImage[itk.F, 2].New()
image.SetNumberOfComponentsPerPixel(4)
image.SetRegions([10,10])
image.Allocate()

wasm_type = helpers.wasm_type_from_image_type(image)
assert wasm_type['dimension'] == 2
assert wasm_type['componentType'] == 'float32'
assert wasm_type['pixelType'] == 'VariableLengthVector'
assert wasm_type['components'] == 4

image_type = helpers.image_type_from_wasm_type(wasm_type)
assert image_type == type(image)
60 changes: 30 additions & 30 deletions Wrapping/Generators/Python/itk/support/helpers.py
Expand Up @@ -250,18 +250,8 @@ def wasm_type_from_image_type(itkimage): # noqa: C901
import itk

component = itk.template(itkimage)[1][0]
if component == itk.UL:
if os.name == "nt":
return "uint32", "Scalar"
else:
return "uint64", "Scalar"
mangle = None
componentType = None
pixelType = "Scalar"
if component == itk.SL:
if os.name == "nt":
return "int32", "Scalar"
else:
return "int64", "Scalar"
if component in (
itk.SC,
itk.UC,
Expand All @@ -277,38 +267,44 @@ def wasm_type_from_image_type(itkimage): # noqa: C901
itk.UL,
itk.ULL,
):
mangle = component
componentType = python_to_js(component)
elif component in [i[1] for i in itk.Vector.items()]:
mangle = itk.template(component)[1][0]
componentType = python_to_js(itk.template(component)[1][0])
pixelType = "Vector"
elif component == itk.complex[itk.F]:
return "float32", "Complex"
componentType = "float32"
pixelType = "Complex"
elif component == itk.complex[itk.D]:
return "float64", "Complex"
componentType = "float64"
pixelType = "Complex"
elif component in [i[1] for i in itk.CovariantVector.items()]:
mangle = itk.template(component)[1][0]
pixelType = ("CovariantVector",)
componentType = python_to_js(itk.template(component)[1][0])
pixelType = "CovariantVector"
elif component in [i[1] for i in itk.Offset.items()]:
return "int64", "Offset"
componentType = "int64"
pixelType = "Offset"
elif component in [i[1] for i in itk.FixedArray.items()]:
mangle = itk.template(component)[1][0]
componentType = python_to_js(itk.template(component)[1][0])
pixelType = "FixedArray"
elif component in [i[1] for i in itk.RGBAPixel.items()]:
mangle = itk.template(component)[1][0]
componentType = python_to_js(itk.template(component)[1][0])
pixelType = "RGBA"
elif component in [i[1] for i in itk.RGBPixel.items()]:
mangle = itk.template(component)[1][0]
componentType = python_to_js(itk.template(component)[1][0])
pixelType = "RGB"
elif component in [i[1] for i in itk.SymmetricSecondRankTensor.items()]:
# SymmetricSecondRankTensor
mangle = itk.template(component)[1][0]
componentType = python_to_js(itk.template(component)[1][0])
pixelType = "SymmetrySecondRankTensor"
else:
raise RuntimeError(f"Unrecognized component type: {str(component)}")

if isinstance(itkimage, itk.VectorImage):
pixelType = "VariableLengthVector"

imageType = dict(
dimension=itkimage.GetImageDimension(),
componentType=python_to_js(mangle),
componentType=componentType,
pixelType=pixelType,
components=itkimage.GetNumberOfComponentsPerPixel(),
)
Expand All @@ -326,14 +322,18 @@ def image_type_from_wasm_type(jstype):
else:
return itk.Image[itk.complex, itk.D], np.float64

prefix = pixelType_to_prefix(pixelType)

postfix = ''
if pixelType != "Offset":
prefix += js_to_python(jstype["componentType"])
if pixelType not in ("Scalar", "RGB", "RGBA", "Complex"):
prefix += str(dimension)
prefix += str(dimension)
return getattr(itk.Image, prefix)
postfix += js_to_python(jstype["componentType"])
if pixelType not in ("Scalar", "RGB", "RGBA", "Complex", "VariableLengthVector"):
postfix += str(dimension)
postfix += str(dimension)

if pixelType == "VariableLengthVector":
return getattr(itk.VectorImage, postfix)
else:
prefix = pixelType_to_prefix(pixelType)
return getattr(itk.Image, f"{prefix}{postfix}")


def wasm_type_from_mesh_type(itkmesh):
Expand Down

0 comments on commit 99f16a0

Please sign in to comment.