Skip to content

Commit dcde43c

Browse files
committed
BUG: Set ElementType for wrapping PyVectorContainer
This is required in the CMake configure_file template to produce the correct return type.
1 parent 370336c commit dcde43c

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

Modules/Bridge/NumPy/wrapping/PyVectorContainerBuffer.i.in

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
itksize = vector_container.Size()
1515
container_type = itk.template(vector_container)
1616
if isinstance(container_type[1][1], type):
17-
element_type = itk.template(container_type[1][1])
18-
dimension = element_type[1][1]
17+
container_element_type = itk.template(container_type[1][1])
18+
dimension = container_element_type[1][1]
1919
shape = (itksize, dimension)
2020
else:
2121
shape = (itksize,)
2222

23-
pixelType = "@PixelType@"
24-
numpydatatype = _get_numpy_pixelid(pixelType)
23+
element_type = "@ElementType@"
24+
numpydatatype = _get_numpy_pixelid(element_type)
2525
memview = itkPyVectorContainer@PyVectorContainerTypes@._array_view_from_vector_container(vector_container)
2626
ndarrview = np.asarray(memview).view(dtype = numpydatatype).reshape(shape).view(np.ndarray)
2727

Modules/Bridge/NumPy/wrapping/itkPyVectorContainer.wrap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ itk_wrap_class("itk::PyVectorContainer")
66
UNIQUE(index_types "IT;UC")
77
foreach(tt ${index_types})
88
foreach(t ${scalar_types})
9+
set(ElementType ${t})
910
# These are wrapped below with "vectypes"
1011
if(NOT t IN_LIST WRAP_ITK_USIGN_INT)
1112
itk_wrap_template("${ITKM_${tt}}${ITKM_${t}}" "${ITKT_${tt}},${ITKT_${t}}")
@@ -23,6 +24,7 @@ itk_wrap_class("itk::PyVectorContainer")
2324
UNIQUE(real_types "${WRAP_ITK_REAL};D")
2425
foreach(d ${ITK_WRAP_IMAGE_DIMS})
2526
foreach(t ${real_types})
27+
set(ElementType ${t})
2628
itk_wrap_template("${ITKM_IT}${ITKM_V${t}${d}}" "${ITKT_IT},${ITKT_V${t}${d}}")
2729
set(PyVectorContainerTypes ${ITKM_IT}${ITKM_V${t}${d}})
2830
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/PyVectorContainerBuffer.i.in
@@ -57,6 +59,7 @@ itk_wrap_class("itk::PyVectorContainer")
5759
endforeach()
5860
foreach(d ${ITK_WRAP_IMAGE_DIMS})
5961
itk_wrap_template("${ITKM_UC}${ITKM_O${d}}" "${ITKT_UC},${ITKT_O${d}}")
62+
set(ElementType ${ITKM_OT})
6063
set(PyVectorContainerTypes ${ITKM_UC}${ITKM_O${d}})
6164
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/PyVectorContainerBuffer.i.in
6265
${CMAKE_CURRENT_BINARY_DIR}/PyVectorContainerBuffer.i.temp
@@ -79,6 +82,7 @@ itk_wrap_class("itk::PyVectorContainer")
7982
endforeach()
8083
# used in FastMarchingExtensionImageFilter
8184
itk_wrap_template("${ITKM_UI}${ITKM_VUC1}" "${ITKT_UI},${ITKT_VUC1}")
85+
set(ElementType UC)
8286
set(PyVectorContainerTypes ${ITKM_UI}${ITKM_VUC1})
8387
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/PyVectorContainerBuffer.i.in
8488
${CMAKE_CURRENT_BINARY_DIR}/PyVectorContainerBuffer.i.temp
@@ -89,6 +93,7 @@ itk_wrap_class("itk::PyVectorContainer")
8993
${PyBufferInterfaceTemp})
9094

9195
itk_wrap_template("${ITKM_IT}S${ITKM_IT}" "${ITKT_IT}, std::set< ${ITKT_IT} >")
96+
set(ElementType IT)
9297
set(PyVectorContainerTypes ${ITKM_IT}S${ITKM_IT})
9398
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/PyVectorContainerBuffer.i.in
9499
${CMAKE_CURRENT_BINARY_DIR}/PyVectorContainerBuffer.i.temp
@@ -101,6 +106,7 @@ itk_wrap_template("${ITKM_IT}S${ITKM_IT}" "${ITKT_IT}, std::set< ${ITKT_IT} >
101106
UNIQUE(vectypes "${ITKM_IT};UI;${WRAP_ITK_USIGN_INT}")
102107
foreach(t1 ${vectypes})
103108
foreach(t2 ${vectypes})
109+
set(ElementType ${t2})
104110
itk_wrap_template("${ITKM_${t1}}${ITKM_${t2}}" "${ITKT_${t1}}, ${ITKT_${t2}}")
105111

106112
set(PyVectorContainerTypes ${ITKM_${t1}}${ITKM_${t2}})

Modules/Bridge/NumPy/wrapping/test/itkPyVectorContainerTest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def test_NumPyBridge_VectorContainer(self):
5151
v1.SetElement(2, 4)
5252
v1.SetElement(3, 5)
5353
arr = itk.array_view_from_vector_container(v1)
54+
assert arr.dtype == np.float32
5455
v2 = itk.vector_container_from_array(arr)
5556
self.assertEqual(v1.Size(), arr.shape[0])
5657
self.assertEqual(v1.Size(), v2.Size())

Wrapping/Generators/Python/itk/support/extras.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,8 @@ def array_from_vector_container(container, ttype=None):
392392
"""Get an Array with the content of the vector container"""
393393
import itk
394394

395-
IndexType = itk.template(container)[1][0]
395+
container_template = itk.template(container)
396+
IndexType = container_template[1][0]
396397

397398
# Find container data type
398399
if ttype is not None:
@@ -403,7 +404,7 @@ def array_from_vector_container(container, ttype=None):
403404
else:
404405
DataType = ttype
405406
else:
406-
DataType = itk.template(container)[1][1]
407+
DataType = container_template[1][1]
407408
keys = [k for k in itk.PyVectorContainer.keys() if k == (IndexType, DataType)]
408409
if len(keys) == 0:
409410
raise RuntimeError("No suitable template parameter can be found.")
@@ -415,7 +416,8 @@ def array_view_from_vector_container(container, ttype=None):
415416
"""Get an Array view with the content of the vector container"""
416417
import itk
417418

418-
IndexType = itk.template(container)[1][0]
419+
container_template = itk.template(container)
420+
IndexType = container_template[1][0]
419421

420422
# Find container type
421423
if ttype is not None:
@@ -426,7 +428,7 @@ def array_view_from_vector_container(container, ttype=None):
426428
else:
427429
DataType = ttype
428430
else:
429-
DataType = itk.template(container)[1][1]
431+
DataType = container_template[1][1]
430432
keys = [k for k in itk.PyVectorContainer.keys() if k == (IndexType, DataType)]
431433
if len(keys) == 0:
432434
raise RuntimeError("No suitable template parameter can be found.")

0 commit comments

Comments
 (0)