Skip to content

Commit

Permalink
ENH: Expose itk::VariableLengthVector operators in Python wrapping
Browse files Browse the repository at this point in the history
Allows `itk.VariableLengthVector`s to be added directly in Python, i.e.
`v3 = v1 + v2` now succeeds where `v1`, `v2`, `v3` are all the same
specialization of `itk.VariableLengthVector`.
  • Loading branch information
tbirdso authored and hjmjohnson committed Feb 23, 2022
1 parent 7b39981 commit 0e84a05
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions Modules/Core/Common/wrapping/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ if(ITK_WRAP_PYTHON)
itk_python_add_test(NAME itkImageDuplicatorPythonTest COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/itkImageDuplicatorTest.py)
itk_python_add_test(NAME itkImagePythonTest COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/itkImageTest.py)
itk_python_add_test(NAME itkMatrixPythonTest COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/itkMatrixTest.py)
itk_python_add_test(NAME itkVariableLengthVectorPythonTest COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/itkVariableLengthVectorTest.py)
endif()
57 changes: 57 additions & 0 deletions Modules/Core/Common/wrapping/test/itkVariableLengthVectorTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# ==========================================================================
#
# 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
itk.auto_progress(2)

n_channels = 31

# Verify UC addition operation
vector_type = itk.VariableLengthVector[itk.UC]
vector1 = vector_type(n_channels)
vector2 = vector_type(n_channels)
assert len(vector1) == n_channels and len(vector2) == n_channels

vector1.Fill(16)
for idx in range(n_channels):
vector2[idx] = idx

sum = vector1 + vector2
print(f'UC sum: {sum}')

for idx in range(n_channels):
assert sum[idx] == 16 + idx, "Got unexpected result from vector sum"

# Verify float addition operation
vector_float_type = itk.VariableLengthVector[itk.F]
vector3 = vector_float_type(n_channels)
vector4 = vector_float_type(n_channels)
assert len(vector3) == n_channels and len(vector4) == n_channels

vector3.Fill(0.5)
for idx in range(n_channels):
vector4.SetElement(idx, 0.1 * idx)

float_sum = vector3 + vector4
print(f'float sum: {float_sum}')

tolerance = 1e-6
for idx in range(n_channels):
diff = abs(float_sum[idx] - (0.5 + 0.1 * idx))
print(f'float sum[{idx}]: {float_sum[idx]:0.9f} diff: {diff:0.2e}')
assert diff < tolerance, "Got unexpected result from vector float sum"
6 changes: 6 additions & 0 deletions Wrapping/Generators/Python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,12 @@ macro(itk_wrap_simple_type_python wrap_class swig_name)
ADD_PYTHON_CONFIG_TEMPLATE("list" "std::list" "listitkLightObject" "itk::LightObject")
endif()

if("${cpp_name}" STREQUAL "itk::VariableLengthVector")
if(NOT ("${template_params}" MATCHES "std::complex")) # TODO cover complex types
set(ITK_WRAP_PYTHON_SWIG_EXT "${ITK_WRAP_PYTHON_SWIG_EXT}DECL_PYTHON_VARIABLELENGTHVECTOR_CLASS(${swig_name}, ${template_params})\n")
endif()
endif()

if("${swig_name}" STREQUAL "itkObject")
set(ITK_WRAP_PYTHON_SWIG_EXT "${ITK_WRAP_PYTHON_SWIG_EXT}DECL_PYTHON_OBJECT_CLASS(${swig_name})\n")
endif()
Expand Down
30 changes: 30 additions & 0 deletions Wrapping/Generators/Python/PyBase/pyBase.i
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,36 @@ str = str
%enddef


%define DECL_PYTHON_VARIABLELENGTHVECTOR_CLASS(swig_name, type)

%extend swig_name {
swig_name __add__(swig_name v2) {
return *self + v2;
}
swig_name __sub__(swig_name v2) {
return *self - v2;
}
type __getitem__(unsigned long d) {
if (d >= self->GetNumberOfElements()) { throw std::out_of_range("swig_name index out of range."); }
return self->operator[]( d );
}
void __setitem__(unsigned long d, type v) {
if (d >= self->GetNumberOfElements()) { throw std::out_of_range("swig_name index out of range."); }
self->operator[]( d ) = v;
}
unsigned int __len__() {
return self->GetNumberOfElements();
}
std::string __repr__() {
std::ostringstream msg;
msg << "swig_name (" << *self << ")";
return msg.str();
}
}

%enddef


%define DECL_PYTHON_OBJECT_CLASS(swig_name)

%pythonprepend itkObject::AddObserver %{
Expand Down

0 comments on commit 0e84a05

Please sign in to comment.