Skip to content

BUG: Fix case where DICOM series order has negative spacing - #6930

Merged
thewtex merged 2 commits into
InsightSoftwareConsortium:release-5.4from
blowekamp:backport-5357-r54
Oct 12, 2026
Merged

thewtex merged 2 commits into
InsightSoftwareConsortium:release-5.4from
blowekamp:backport-5357-r54

Conversation

@blowekamp

Copy link
Copy Markdown
Member

Backport of #5357 to release-5.4. Fixes incorrect direction-cosine sign when
DICOM slice order implies negative inter-slice spacing. Requested in #6859.

Backport notes

Cherry-picked the two upstream commits from #5357 unchanged. The GDCM module's
GoogleTest driver did not exist on release-5.4, so Modules/IO/GDCM/test/CMakeLists.txt
needed manual merge (kept existing compliance tests, added the
creategoogletestdriver wiring from upstream). Fixed two -Wshadow warnings
in itkImageSeriesReader.hxx introduced by the cherry-pick (release-5.4 reuses
a single loop variable j in this function; main declares a fresh one per loop).

AI assistance
  • Tool: Claude Code (claude-sonnet-5) via GitHub Copilot
  • Role: cherry-picked and adapted the two upstream commits, resolved the
    CMakeLists.txt conflict, fixed the shadow warnings, built and ran tests
  • All code reviewed and tested locally before pushing
Local validation
  • cmake --build --preset default (full incremental build): succeeds, no
    warnings in itkImageSeriesReader.hxx
  • ITKGDCMImageIOGTestDriver --gtest_filter=GDCMSeriesDirection.*: 17/17 passed
  • ctest -L ITKIOGDCM: 40/41 passed (pre-existing ITKIOGDCMKWStyleTest
    failure, identical on upstream main — module's ITKKWStyleOverwrite.txt
    doesn't disable the Namespace check for itkGDCMImageIOGTest.cxx; not a
    regression from this backport)
  • ctest -L ITKIOImageBase: 81/81 passed

If the spacing between slices is negative, then direction cosine
matrix was not correct with reguards to the sign.

This can be easily created with the "ReverseOrder" flag with the
series reader and DICOM data. With the correction, a DICOM series
mantains its correct physical location when this flag is enabled.

(cherry picked from commit 5fdb32d)
@github-actions github-actions Bot added type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances type:Infrastructure Infrastructure/ecosystem related changes, such as CMake or buildbots type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct area:IO Issues affecting the IO module labels Oct 6, 2026
@blowekamp
blowekamp marked this pull request as ready for review October 6, 2026 22:02
@greptile-apps

greptile-apps Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

[High risk] Fixes DICOM series direction calculation for reversed slice ordering.

The direction fix appears sound, but the required macro naming rule must be satisfied before merging.

Findings

  1. P2 Module runs miss new tests ▶
  2. P2 Driver build misses test data ▶
  3. P2 Random directions stay planar ▶
  4. P2 Macros omit the required prefix ▶
Summary

Corrects the slice direction sign when file order runs opposite to the direction reported by the image reader. Spacing stays positive, and the origin stays at the first read slice.

  • Adds synthetic and real-data checks for reversed order and physical positions.
  • The new tests need module labels, a working data prerequisite, and broader oblique coverage.
  • The helper macro names must satisfy the repository guide before merging.

Acknowledgment: blowekamp described the namespace check failure for itkGDCMImageIOGTest.cxx as known and present upstream. It was not reported as a finding.

Reviews (1) · Last reviewed commit: "ENH: Tests — Extensive regression suite ..."

itkGDCMImageIOGTest.cxx
itkGDCMSeriesDirectionGTest.cxx
)
creategoogletestdriver(ITKGDCMImageIO "${ITKIOGDCM-Test_LIBRARIES}" "${ITKGDCMImageIOGTests}")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Module runs miss new tests

The new tests are not included in ctest -L ITKIOGDCM. CreateGoogleTestDriver labels the executable, but it does not label the tests it discovers. Add ITKIOGDCM to the discovered tests so module-only runs include these regression checks.

Knowledge Base Used: Testing and continuous integration

Comment on lines +473 to +480
set_property(
TARGET
ITKGDCMImageIOGTestDriver
APPEND
PROPERTY
DEPENDS
ITKData
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Driver build misses test data

Setting the target's DEPENDS property does not make ITKData a build prerequisite. If the external data has not been fetched, building this driver alone leaves ReadSlicesReverseOrder without its DICOM input. Use add_dependencies(ITKGDCMImageIOGTestDriver ITKData) instead.

Suggested change
set_property(
TARGET
ITKGDCMImageIOGTestDriver
APPEND
PROPERTY
DEPENDS
ITKData
)
add_dependencies(ITKGDCMImageIOGTestDriver ITKData)

Knowledge Base Used: Testing and continuous integration

Comment on lines +807 to +812
std::array<double, 3> row = normalize3({ { ca, sa * cb, sa * sb } });
std::array<double, 3> col = normalize3({ { -sa, ca * cb, ca * sb } });
// Re-orthogonalize col against row (Gram–Schmidt) to keep DICOM happy.
double dot = row[0] * col[0] + row[1] * col[1] + row[2] * col[2];
col = normalize3({ { col[0] - dot * row[0], col[1] - dot * row[1], col[2] - dot * row[2] } });
auto n = normalize3(cross(row, col));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Random directions stay planar

The random orientation formula always gives a slice normal of (0, -sin(b), cos(b)). Changing a never changes that normal. The two oblique tests use the same formula, so none of these cases covers a slice direction with three nonzero components. Add a rotation about another axis or generate a full 3D frame to cover that case.

Comment on lines +31 to +32
#define _STRING(s) #s
#define TOSTRING(s) std::string(_STRING(s))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Macros omit the required prefix

The new _STRING and TOSTRING macros violate the guide's required ITK_UPPERCASE_MACRO naming pattern. The same names appear in itkGDCMSeriesDirectionGTest.cxx. Rename them with an ITK_ prefix in both files. This repository requirement must be satisfied before merging.

Context Used: AGENTS.md (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@blowekamp blowekamp added this to the ITK 5.4.8 milestone Oct 7, 2026
@blowekamp blowekamp mentioned this pull request Oct 7, 2026
@blowekamp

blowekamp commented Oct 8, 2026 •

Copy link
Copy Markdown
Member Author

@thewtex @dzenanz @hjmjohnson A user (@zhurong2020) requested that (t)his DICOM fix be back ported to the 5.4 branch. What do you think of this change being back ported? greptile seems to be finding new issues in the test that is in main but just back ported. Should the test be removed from the back port?

EDIT: missing t

@zhurong2020

Copy link
Copy Markdown

Thanks, Bradley. To clarify the attribution: the fix is #5357 (yours, with Hans's test). My part was the reproducer, the downstream report and the backport request.

On whether it is worth backporting: any 5.4-based reader of series that carry a negative SpacingBetweenSlices (common on Philips CT) gets a volume that is silently mirrored along the slice axis, which downstream segmentation does not detect. On the test, I defer to the maintainers.

@hjmjohnson

Copy link
Copy Markdown
Member

@blowekamp I read greptile's findings as test-quality improvements (module label, a true build dependency on the test data, broader oblique-orientation coverage), not defects in the fix. The fix is sound and validated (17/17 new tests pass). For ROI I think the test is fine in the backport as is, and we should improve it in main. Draft #6940 does that, one commit per finding.

The _STRING/TOSTRING macro-prefix finding is intentionally not applied: it is the existing idiom in 17 test .cxx files and is local to the translation unit.

@blowekamp

Copy link
Copy Markdown
Member Author

I read greptile's findings as test-quality improvements (module label, a true build dependency on the test data, broader oblique-orientation coverage), not defects in the fix.

The non-deterministic nature of the reviews is difficult for back-ports of changes. The detected initial review comments and addressed in the main commit, but them more are created when the patch is back-ported. I am not sure if any policies could or should be changed to address this.

@hjmjohnson

Copy link
Copy Markdown
Member

@blowekamp I view this as "don't let pursuit of perfection be the enemy of the good". This backport is good as is and shouldn't be held up if resources can't reach perfection. I'll approve, but you can decide whether you have time/resources/desire to accept this good result as is, or wait for the #6940 from upstream.

I'd merge this backport as is. The 6940 improvements are test-based and have better ROI on main than in the backport.

@thewtex thewtex left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @blowekamp !

Agree with Hans re: perfect is the enemy of the good, fixing in the test code can be made on main, is not required here.

@thewtex
thewtex merged commit 070c113 into InsightSoftwareConsortium:release-5.4 Oct 12, 2026
17 checks passed
blowekamp added a commit to blowekamp/SimpleITK that referenced this pull request Nov 3, 2026
…notes

Add a Bug Fixes from ITK 5.4.8 section linking to SimpleITK issue
SimpleITK#2292 and its upstream fix in InsightSoftwareConsortium/ITK#6930,
which corrects an incorrect direction matrix computed by
ImageSeriesReader when SpacingBetweenSlices sign disagreed with the
slice ordering.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:IO Issues affecting the IO module type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances type:Infrastructure Infrastructure/ecosystem related changes, such as CMake or buildbots type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants