Skip to content

Commit

Permalink
ENH: Python wrapping reads VLV pixel type correctly
Browse files Browse the repository at this point in the history
The test file is produced by 3D Slicer, which is a saved
segmentation of mouse tibia (down-sampled for test).
  • Loading branch information
dzenanz authored and thewtex committed Aug 11, 2021
1 parent c6d7dde commit 4ae3749
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Wrapping/Generators/Python/Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,13 @@ itk_python_add_test(NAME PythonMultiprocessLazyLoad
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/multiprocess_lazy_loading.py
DATA{${WrapITK_SOURCE_DIR}/images/cthead1.png} DATA{${WrapITK_SOURCE_DIR}/images/templated_pipeline.png} DATA{${WrapITK_SOURCE_DIR}/images/2th_cthead1.png}
)

# Test proper reading and writing of VariableLengthVector
itk_python_add_test(NAME PythonVLVReadWriteTest
TEST_DRIVER_ARGS
--compare ${ITK_TEST_OUTPUT_DIR}/TestVLV.seg.nrrd DATA{${WrapITK_SOURCE_DIR}/images/TestVLV.seg.nrrd}
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/readWriteVLV.py
DATA{${WrapITK_SOURCE_DIR}/images/TestVLV.seg.nrrd}
${ITK_TEST_OUTPUT_DIR}/TestVLV.seg.nrrd
59 85 58 5
)
31 changes: 31 additions & 0 deletions Wrapping/Generators/Python/Tests/readWriteVLV.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# ==========================================================================
#
# Copyright NumFOCUS
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ==========================================================================*/

import itk
import sys

if len(sys.argv) < 3:
print("Usage: " + sys.argv[0] + " in.seg.nrrd out.seg.nrrd")
sys.exit(1)

itk.auto_progress(2)

test_image = itk.imread(sys.argv[1], pixel_type=itk.VariableLengthVector[itk.UC])
for i in range(len(sys.argv) - 3):
assert test_image.shape[i] == int(sys.argv[i + 3])
itk.imwrite(test_image, sys.argv[2], compression=True)
10 changes: 9 additions & 1 deletion Wrapping/Generators/Python/itk/support/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,15 @@ def imread(
# Increase dimension if last dimension is not of size one.
if increase_dimension and image_IO.GetDimensions(dimension - 1) != 1:
dimension += 1
ImageType = itk.Image[pixel_type, dimension]
is_vlv = False
try:
is_vlv = itk.template(pixel_type)[0] is itk.VariableLengthVector
except KeyError:
pass
if is_vlv:
ImageType = itk.VectorImage[itk.template(pixel_type)[1][0], dimension]
else:
ImageType = itk.Image[pixel_type, dimension]
reader = template_reader_type[ImageType].New(**kwargs)
else:
reader = template_reader_type.New(**kwargs)
Expand Down
1 change: 1 addition & 0 deletions Wrapping/images/TestVLV.seg.nrrd.sha512
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9ad6479c834f2280bd2641caa7160e0016a340aa9fbf88f5afa5d7101ffbaaab7b72194a7e8678b4ffca89a25be6f370e19cf41d0b3c811986d5a49a4272929e

0 comments on commit 4ae3749

Please sign in to comment.