Skip to content

Commit

Permalink
BUG: Wrap PyVectorContainer for all VectorContainer templates
Browse files Browse the repository at this point in the history
  • Loading branch information
tbirdso committed Mar 25, 2021
1 parent a74827c commit 7216ed1
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 22 deletions.
5 changes: 3 additions & 2 deletions Modules/Bridge/NumPy/include/itkPyVectorContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,17 @@ namespace itk
*
* \ingroup ITKBridgeNumPy
*/
template <typename TElement>
template <typename TElementIdentifier, typename TElement>
class PyVectorContainer
{
public:
/** Standard "Self" type alias. */
using Self = PyVectorContainer;

/** Type of the data from which the buffer will be converted */
using ElementIdentiferType = TElementIdentifier;
using DataType = TElement;
using VectorContainerType = VectorContainer<IdentifierType, TElement>;
using VectorContainerType = VectorContainer<TElementIdentifier, TElement>;

/**
* Get an Array with the content of the vnl vector
Expand Down
10 changes: 5 additions & 5 deletions Modules/Bridge/NumPy/include/itkPyVectorContainer.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
namespace itk
{

template <class TElement>
template <typename TElementIdentifier, typename TElement>
PyObject *
PyVectorContainer<TElement>::_array_view_from_vector_container(VectorContainerType * vector)
PyVectorContainer<TElementIdentifier, TElement>::_array_view_from_vector_container(VectorContainerType * vector)
{
PyObject * memoryView = NULL;
Py_buffer pyBuffer;
Expand Down Expand Up @@ -56,9 +56,9 @@ PyVectorContainer<TElement>::_array_view_from_vector_container(VectorContainerTy
return memoryView;
}

template <class TElement>
const typename PyVectorContainer<TElement>::VectorContainerType::Pointer
PyVectorContainer<TElement>::_vector_container_from_array(PyObject * arr, PyObject * shape)
template <typename TElementIdentifier, typename TElement>
const typename PyVectorContainer<TElementIdentifier, TElement>::VectorContainerType::Pointer
PyVectorContainer<TElementIdentifier, TElement>::_vector_container_from_array(PyObject * arr, PyObject * shape)
{
PyObject * obj = NULL;
PyObject * shapeseq = NULL;
Expand Down
117 changes: 107 additions & 10 deletions Modules/Bridge/NumPy/wrapping/itkPyVectorContainer.wrap
Original file line number Diff line number Diff line change
@@ -1,19 +1,116 @@
itk_wrap_include("set")

itk_wrap_class("itk::PyVectorContainer")
UNIQUE(types "${WRAP_ITK_SCALAR};D;UI")
# double is always needed by KernelTransform
UNIQUE(scalar_types "${WRAP_ITK_SCALAR};D")
UNIQUE(index_types "IT;UC")
foreach(tt ${index_types})
foreach(t ${scalar_types})
# These are wrapped below with "vectypes"
if(NOT t IN_LIST WRAP_ITK_USIGN_INT)
itk_wrap_template("${ITKM_${tt}}${ITKM_${t}}" "${ITKT_${tt}},${ITKT_${t}}")
set(PyVectorContainerTypes ${ITKM_${tt}}${ITKM_${t}})
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/PyVectorContainerBuffer.i.in
${CMAKE_CURRENT_BINARY_DIR}/PyVectorContainerBuffer.i.temp
@ONLY)
file(READ ${CMAKE_CURRENT_BINARY_DIR}/PyVectorContainerBuffer.i.temp
PyBufferInterfaceTemp)
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/PyBuffer.i
${PyBufferInterfaceTemp})
endif()
endforeach()
endforeach()
UNIQUE(real_types "${WRAP_ITK_REAL};D")
foreach(d ${ITK_WRAP_IMAGE_DIMS})
foreach(t ${real_types})
itk_wrap_template("${ITKM_IT}${ITKM_V${t}${d}}" "${ITKT_IT},${ITKT_V${t}${d}}")
set(PyVectorContainerTypes ${ITKM_IT}${ITKM_V${t}${d}})
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/PyVectorContainerBuffer.i.in
${CMAKE_CURRENT_BINARY_DIR}/PyVectorContainerBuffer.i.temp
@ONLY)
file(READ ${CMAKE_CURRENT_BINARY_DIR}/PyVectorContainerBuffer.i.temp
PyBufferInterfaceTemp)
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/PyBuffer.i
${PyBufferInterfaceTemp})

itk_wrap_template("${ITKM_IT}${ITKM_P${t}${d}}" "${ITKT_IT},${ITKT_P${t}${d}}")
set(PyVectorContainerTypes ${ITKM_IT}${ITKM_P${t}${d}})
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/PyVectorContainerBuffer.i.in
${CMAKE_CURRENT_BINARY_DIR}/PyVectorContainerBuffer.i.temp
@ONLY)
file(READ ${CMAKE_CURRENT_BINARY_DIR}/PyVectorContainerBuffer.i.temp
PyBufferInterfaceTemp)
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/PyBuffer.i
${PyBufferInterfaceTemp})

itk_wrap_template("${ITKM_IT}M${ITKM_${t}}${d}${d}" "${ITKT_IT}, itk::Matrix< ${ITKT_${t}}, ${d}, ${d} >")
set(PyVectorContainerTypes ${ITKM_IT}M${ITKM_${t}}${d}${d})
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/PyVectorContainerBuffer.i.in
${CMAKE_CURRENT_BINARY_DIR}/PyVectorContainerBuffer.i.temp
@ONLY)
file(READ ${CMAKE_CURRENT_BINARY_DIR}/PyVectorContainerBuffer.i.temp
PyBufferInterfaceTemp)
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/PyBuffer.i
${PyBufferInterfaceTemp})

endforeach()
endforeach()
foreach(d ${ITK_WRAP_IMAGE_DIMS})
# Wrap itk.Point[itk.F, ${d}]
list(APPEND types PF${d})
itk_wrap_template("${ITKM_UC}${ITKM_O${d}}" "${ITKT_UC},${ITKT_O${d}}")
set(PyVectorContainerTypes ${ITKM_UC}${ITKM_O${d}})
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/PyVectorContainerBuffer.i.in
${CMAKE_CURRENT_BINARY_DIR}/PyVectorContainerBuffer.i.temp
@ONLY)
file(READ ${CMAKE_CURRENT_BINARY_DIR}/PyVectorContainerBuffer.i.temp
PyBufferInterfaceTemp)
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/PyBuffer.i
${PyBufferInterfaceTemp})

itk_wrap_template("${ITKM_UI}${ITKM_CID${d}}" "${ITKT_UI},${ITKT_CID${d}}")
set(PyVectorContainerTypes ${ITKM_UI}${ITKM_CID${d}})
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/PyVectorContainerBuffer.i.in
${CMAKE_CURRENT_BINARY_DIR}/PyVectorContainerBuffer.i.temp
@ONLY)
file(READ ${CMAKE_CURRENT_BINARY_DIR}/PyVectorContainerBuffer.i.temp
PyBufferInterfaceTemp)
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/PyBuffer.i
${PyBufferInterfaceTemp})

endforeach()
foreach(t ${types})
set(PixelType ${t})
itk_wrap_template("${ITKM_${t}}" "${ITKT_${t}}")
set(PyVectorContainerTypes ${ITKM_${t}})
# used in FastMarchingExtensionImageFilter
itk_wrap_template("${ITKM_UI}${ITKM_VUC1}" "${ITKT_UI},${ITKT_VUC1}")
set(PyVectorContainerTypes ${ITKM_UI}${ITKM_VUC1})
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/PyVectorContainerBuffer.i.in
${CMAKE_CURRENT_BINARY_DIR}/PyVectorContainerBuffer.i.temp
@ONLY)
file(READ ${CMAKE_CURRENT_BINARY_DIR}/PyVectorContainerBuffer.i.temp
PyBufferInterfaceTemp)
PyBufferInterfaceTemp)
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/PyBuffer.i
${PyBufferInterfaceTemp})
endforeach(t ${types})
${PyBufferInterfaceTemp})

itk_wrap_template("${ITKM_IT}S${ITKM_IT}" "${ITKT_IT}, std::set< ${ITKT_IT} >")
set(PyVectorContainerTypes ${ITKM_IT}S${ITKM_IT})
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/PyVectorContainerBuffer.i.in
${CMAKE_CURRENT_BINARY_DIR}/PyVectorContainerBuffer.i.temp
@ONLY)
file(READ ${CMAKE_CURRENT_BINARY_DIR}/PyVectorContainerBuffer.i.temp
PyBufferInterfaceTemp)
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/PyBuffer.i
${PyBufferInterfaceTemp})
#used in ImageRegistrationMethodv4
UNIQUE(vectypes "${ITKM_IT};UI;${WRAP_ITK_USIGN_INT}")
foreach(t1 ${vectypes})
foreach(t2 ${vectypes})
itk_wrap_template("${ITKM_${t1}}${ITKM_${t2}}" "${ITKT_${t1}}, ${ITKT_${t2}}")

set(PyVectorContainerTypes ${ITKM_${t1}}${ITKM_${t2}})
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/PyVectorContainerBuffer.i.in
${CMAKE_CURRENT_BINARY_DIR}/PyVectorContainerBuffer.i.temp
@ONLY)
file(READ ${CMAKE_CURRENT_BINARY_DIR}/PyVectorContainerBuffer.i.temp
PyBufferInterfaceTemp)
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/PyBuffer.i
${PyBufferInterfaceTemp})
endforeach()
endforeach()
itk_end_wrap_class()
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_NumPyBridge_VectorContainer(self):

if not (
hasattr(itk.VectorContainer, "ULLF")
and hasattr(itk.PyVectorContainer, "F")
and hasattr(itk.PyVectorContainer, "ULLF")
and hasattr(itk.Point, "F3")
and hasattr(itk.VectorContainer, "ULLPF3")
and hasattr(itk.Point, "F2")
Expand Down
15 changes: 11 additions & 4 deletions Wrapping/Generators/Python/itk/support/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,9 @@ def array_from_vector_container(container, ttype=None):
"""Get an Array with the content of the vector container"""
import itk

# Find container type
IndexType = itk.template(container)[1][0]

# Find container data type
if ttype is not None:
if isinstance(ttype, (tuple, list)):
if len(ttype) != 1:
Expand All @@ -402,7 +404,7 @@ def array_from_vector_container(container, ttype=None):
DataType = ttype
else:
DataType = itk.template(container)[1][1]
keys = [k for k in itk.PyVectorContainer.keys() if k[0] == DataType]
keys = [k for k in itk.PyVectorContainer.keys() if k == (IndexType, DataType)]
if len(keys) == 0:
raise RuntimeError("No suitable template parameter can be found.")
# Create numpy array of the type of the input container
Expand All @@ -413,6 +415,8 @@ def array_view_from_vector_container(container, ttype=None):
"""Get an Array view with the content of the vector container"""
import itk

IndexType = itk.template(container)[1][0]

# Find container type
if ttype is not None:
if isinstance(ttype, (tuple, list)):
Expand All @@ -423,7 +427,7 @@ def array_view_from_vector_container(container, ttype=None):
DataType = ttype
else:
DataType = itk.template(container)[1][1]
keys = [k for k in itk.PyVectorContainer.keys() if k[0] == DataType]
keys = [k for k in itk.PyVectorContainer.keys() if k == (IndexType, DataType)]
if len(keys) == 0:
raise RuntimeError("No suitable template parameter can be found.")
# Create numpy array of the type of the input container
Expand All @@ -434,6 +438,9 @@ def vector_container_from_array(arr, ttype=None):
"""Get a vector container from a Python array"""
import itk

# Return VectorContainer with 64-bit index type
IndexType = itk.ULL

# Find container type
if ttype is not None:
if isinstance(ttype, (tuple, list)):
Expand All @@ -444,7 +451,7 @@ def vector_container_from_array(arr, ttype=None):
DataType = ttype
else:
DataType = _get_itk_pixelid(arr)
keys = [k for k in itk.PyVectorContainer.keys() if k[0] == DataType]
keys = [k for k in itk.PyVectorContainer.keys() if k == (IndexType, DataType)]
if len(keys) == 0:
raise RuntimeError("No suitable template parameter can be found.")
# Create numpy array of the type of the input container
Expand Down

0 comments on commit 7216ed1

Please sign in to comment.