InsightSoftwareConsortium / InsightSoftwareConsortium/ITK

Inconsistent `itk::VariableLengthVector` addition results

Open
#3,218 0 comments 0 reactions 0 assignees View on GitHub
area:Python wrapping type:Bug
Dominant language
C++
Stars
1.7k
Forks
748
Avg merge
1d 1h
Merged PRs (30d)
64

Description

### Description

`itk.VariableLengthVector` addition with the `operator+` override sometimes results in incorrect output data. The behavior is specifically observed when arithmetic occurs inside a C++ image filter that is wrapped for Python and executed from a Python console.

Examining [itkVariableLengthVector.h](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Core/Common/include/itkVariableLengthVector.h) reveals that the `operator+` override makes use of metaprogramming techniques for speedup while the `operator+=` override performs standard iterative element-wise vector addition. Replacing `v = v + v2` with `v += v2` has been observed to fix the issue as a viable workaround.

The behavior was observed in a development fork of ITKUltrasound at [https://github.com/dzenanz/ITKUltrasound/commit/281052e8b2fd4fef9a2c1ea6ad1310bcfca8feb1](https://github.com/dzenanz/ITKUltrasound/commit/281052e8b2fd4fef9a2c1ea6ad1310bcfca8feb1). The `PythonSpectra1DAveragingImageFilterTest` ctest procedure attempts to average together several copies of a single-pixel 31-channel input image. The test inconsistently fails due to vector addition returning incorrect results to the output image.

### Steps to Reproduce

1. Build ITK Ultrasound Python wrapping from [https://github.com/dzenanz/ITKUltrasound/commit/281052e8b2fd4fef9a2c1ea6ad1310bcfca8feb1](https://github.com/dzenanz/ITKUltrasound/commit/281052e8b2fd4fef9a2c1ea6ad1310bcfca8feb1) against a viable ITK Python 5.3 build tree.
2. In the ITKUltrasound build directory, with the appropriate Python virtual environment activated, run the offending test with `ctest -C Release -R PythonSpectra1DAveragingImageFilterTest`

### Expected behavior

Test passes, output image is an average of input image channels

### Actual behavior

Test sometimes fails due to output image being out of tolerance with baseline. Examining the output image shows that wildly different pixel values are written, i.e. the average of an input channel with input values `0.1, 0.1, 0.1` is on the order of `e+39`.

### Reproducibility

around 40%

### Versions

ITK 5.3rc03

### Environment

Windows 10, CMake 3.16.9, Python 3.8

### Additional Information

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.