Skip to content

Commit

Permalink
ENH: wrap NumpyBridge for dimension 1
Browse files Browse the repository at this point in the history
  • Loading branch information
arobert01 authored and Simon Rit committed Apr 15, 2020
1 parent 0832018 commit 72ed4df
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
9 changes: 6 additions & 3 deletions wrapping/PyBufferRTK.i.in
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
or a segfault.
"""

assert ndarr.ndim in ( 2, 3, 4 ), \
assert ndarr.ndim in ( 1, 2, 3, 4 ), \
"Only arrays of 2, 3 or 4 dimensions are supported."
if( not ndarr.flags['C_CONTIGUOUS'] and not ndarr.flags['F_CONTIGUOUS'] ):
raise ValueError("Array memory is not contiguous. Try converting your array with "
Expand All @@ -115,7 +115,7 @@
imgview = itkPyBuffer@PyBufferTypes@._GetImageViewFromArray( ndarr, ndarr.shape[-2::-1], ndarr.shape[-1] )
else:
imgview = itkPyBuffer@PyBufferTypes@._GetImageViewFromArray( ndarr, ndarr.shape[-1:0:-1], ndarr.shape[0] )
elif ndarr.ndim in ( 2, 3 ):
elif ndarr.ndim in ( 1, 2, 3 ):
imgview = itkPyBuffer@PyBufferTypes@._GetImageViewFromArray( ndarr, ndarr.shape[::-1], 1)

return imgview
Expand All @@ -141,7 +141,10 @@
same for an array and its transpose. If flipping is desired, see
*numpy.reshape*.
"""
from itkImageDuplicatorPython import itkImageDuplicator@PyBufferTypes@
try:
from itkImageDuplicatorPython import itkImageDuplicator@PyBufferTypes@
except ImportError:
from itkImageDuplicatorRTKPython import itkImageDuplicator@PyBufferTypes@

# Create a temporary image view of the array
imageView = itkPyBuffer@PyBufferTypes@.GetImageViewFromArray(ndarr, is_vector)
Expand Down
12 changes: 12 additions & 0 deletions wrapping/itkImageDuplicatorRTK.wrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
itk_wrap_class("itk::ImageDuplicator" POINTER)
# Making sure that all types wrapped in PyBuffer are also wrapped for ImageDuplicator
# as this filter is used in the Python NumPy bridge.
UNIQUE(types "D;UC;US;${WRAP_ITK_REAL}")
list(FIND ITK_WRAP_IMAGE_DIMS "1" _index)
foreach(t ${types})
if (${_index} EQUAL -1)
itk_wrap_template("I${ITKM_${t}}1" "itk::Image<${ITKT_${t}}, 1>")
endif()
endforeach()

itk_end_wrap_class()
3 changes: 3 additions & 0 deletions wrapping/itkImageSourceRTK.wrap
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ itk_wrap_class("itk::ImageSource" POINTER)
if (ITK_WRAP_unsigned_char)
itk_wrap_template("IUC1" "itk::Image<${ITKT_UC}, 1>")
endif()
if (ITK_WRAP_unsigned_short)
itk_wrap_template("IUS1" "itk::Image<${ITKT_US}, 1>")
endif()
endif()

# Wrap ITK dimension 4 missing types
Expand Down
17 changes: 17 additions & 0 deletions wrapping/itkPyBufferRTK.wrap
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,21 @@ itk_wrap_class("itk::PyBuffer")
endforeach(d)
endforeach(t)

UNIQUE(types "D;UC;US;${WRAP_ITK_REAL}")
list(FIND ITK_WRAP_IMAGE_DIMS "1" _indexdim)
foreach(t ${types})
if (${_indexdim} EQUAL -1)
set(PixelType ${t})
set(PyBufferTypes I${t}1)
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/PyBufferRTK.i.in
${CMAKE_CURRENT_BINARY_DIR}/PyBufferRTK.i.temp
@ONLY)
file(READ ${CMAKE_CURRENT_BINARY_DIR}/PyBufferRTK.i.temp
PyBufferInterfaceTemp)
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/PyBufferRTK.i
${PyBufferInterfaceTemp})
itk_wrap_template("I${t}1" "itk::Image<${ITKT_${t}}, 1>")
endif()
endforeach()

itk_end_wrap_class()

0 comments on commit 72ed4df

Please sign in to comment.