Skip to content

BUG: Accept rounding in ImageOrientationPatient when ordering series - #7177

Merged
hjmjohnson merged 1 commit into
InsightSoftwareConsortium:mainfrom
hjmjohnson:bug-gdcm-ippsorter-dircos-tolerance
Nov 16, 2026
Merged

hjmjohnson merged 1 commit into
InsightSoftwareConsortium:mainfrom
hjmjohnson:bug-gdcm-ippsorter-dircos-tolerance

Conversation

@hjmjohnson

@hjmjohnson hjmjohnson commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

GDCMSeriesFileNames (since its gdcm::IPPSorter backend, 50dfb13) sorts with IPPSorter's default direction-cosine tolerance of 0, which demands byte-identical ImageOrientationPatient strings. That is more stringent than ITK 5.4.8, whose SerieHelper took the slice normal from the first file and ordered every slice along it. Real GE MR series with independently rounded direction cosines now fail geometric ordering: the read throws, or with FailOnAmbiguousOrdering(false) falls back to Instance Number order and can produce a left-handed volume with reversed slices. This keeps the exact-match sort first and, only when it fails, accepts the series if every slice's unit row and column cosines are within 1 - cos <= 1e-6 (about 0.08 deg) of the first slice's. No release-5.4 backport is needed: 5.4 does not use IPPSorter.

Closes #7181. Related: #6467, #6469, #6468, #6656, #5357, #6766, #6940, #7152 (same files; whichever merges second needs a rebase), #6463, #7064.

Real data: where this was found

A 155-session clinical stroke MR test set from four vendors, read on current main with the settings dcm-classifier uses (SetUseSeriesDetails(true), FailOnAmbiguousOrdering(false)). Series rejected by IPPSorter only because their IOP strings differ by rounding:

Scanner Series Sessions
GE Genesis Signa 7 2
GE SIGNA Voyager 4 1

All other fallbacks were legitimate (multi-volume series with duplicate positions; localizers and other series with genuinely different planes); no other vendor's series failed this way.

Example (GE Genesis Signa, 2005): five 20-slice axial series, uniform 7.0 mm spacing, no gantry-tilt tag, one frame of reference, three distinct IOP strings per series differing by at most 1.6e-8. ITK 5.4.8: right-handed, ascending. main: IPPSorter fails, GetDidUseAmbiguousOrdering() is true, and the Instance Number fallback yields det(direction) = -1 with the slice axis reversed (physically consistent because of 5fdb32d, but a different voxel order than 5.4.8, which changed downstream CNN results). The new contract test reproduces this rounding pattern; re-reading these clinical series with the fix is still to be done.

Why 1e-6, measured

For small angles 1 - cos(theta) ~ theta^2 / 2, so 1e-6 admits at most 1.4 mrad (0.081 deg): 0.18 mm at 128 mm off-center, below one voxel at clinical MR resolutions (1e-4 would admit 0.81 deg, 1.8 mm).

On the 155-session set, every series with more than one IOP string, worst slice, 1 - n . n0 on unit normals:

Series Count Range
rounding only 11 0 to 4.8e-13
genuinely different orientation 100 7.6e-5 to 2

1e-6 sits more than six orders of magnitude above the largest rounding value and 76x below the smallest genuine difference. The check applies the same bound to the row and the column cosine separately, so an in-plane rotation or a flipped normal is rejected too (a normals-only check would accept the former).

Compared with 5.4.8: equivalent for rounding; stricter for mixed orientations, which 5.4.8 ordered without checking.

Change and tests
  • itkGDCMSeriesFileNames.cxx: SortByPosition(files, 0.0) as before; if it fails, OrientationsAgree() scans ImageOrientationPatient, normalizes each slice's row and column cosines, and requires 1 - r . r0 <= 1e-6 and 1 - c . c0 <= 1e-6; if so, SortByPosition(files, 1e-3) with a fresh IPPSorter. A nonzero IPPSorter tolerance compares unnormalized normals, so a flat 1e-6 there would reject identical six-decimal orientations whose normal's squared length is about 1.2e-6 to 1.5e-6 off one (found by Greptile on the first push); the loose retry only absorbs that length error after ITK has applied the real bound. Gantry-tilt, frame-of-reference, and duplicate-position checks in IPPSorter are unchanged.

  • itkGDCMSeriesFileNamesContractGTest.cxx (orientations derived from the test series' own oblique plane, slices taken in sorted name order):

    • OrientationRoundingStillOrdersGeometrically: alternate slices' IOP off by 1e-8 (the GE pattern) must order geometrically, right-handed.
    • IdenticalUnnormalizedOrientationOrders, RoundedUnnormalizedOrientationOrders: the plane rotated 45 deg in-plane at six decimals (and six vs seven) must order.
    • SlightlyTiltedSliceIsAmbiguous: a 0.5 deg tilt (1 - cos = 3.8e-5, accepted by the loose retry alone) must be rejected.
    • InPlaneRotatedSliceIsAmbiguous: same normal, rows and columns rotated 90 deg, must be rejected.
    • DifferentPlaneSliceIsAmbiguous: a sagittal slice must be rejected.
    • Each rejection test checks both the default throw and, with FailOnAmbiguousOrdering(false), GetDidUseAmbiguousOrdering().
  • Local run (Linux, GCC, Release; ITK main 772f60f with this PR's two files):

    Source Contract tests Failing
    main 13/15 the two rounding tests
    first revision (normals only) 14/15 InPlaneRotatedSliceIsAmbiguous
    this PR all 109 GDCM tests pass none

@github-actions github-actions Bot added type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances 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 Nov 16, 2026
@hjmjohnson
hjmjohnson marked this pull request as ready for review November 16, 2026 03:47
@greptile-apps

This comment was marked as resolved.

@hjmjohnson
hjmjohnson force-pushed the bug-gdcm-ippsorter-dircos-tolerance branch 2 times, most recently from 6dc3c16 to 93228d0 Compare November 16, 2026 04:05
@hjmjohnson
hjmjohnson marked this pull request as draft November 16, 2026 04:06
@hjmjohnson
hjmjohnson marked this pull request as ready for review November 16, 2026 04:06
@hjmjohnson
hjmjohnson requested a review from blowekamp November 16, 2026 13:49
@blowekamp

Copy link
Copy Markdown
Member

Commit 50dfb13 updates the migration guide. With this change I believe the guide is now inaccurate. I don't fully understand the issue, and there seems to be some occupation in the behavior and changes I haven't dug into to fully follow.

@hjmjohnson
hjmjohnson force-pushed the bug-gdcm-ippsorter-dircos-tolerance branch from 93228d0 to a08746b Compare November 16, 2026 14:44
@github-actions github-actions Bot added the area:Documentation Issues affecting the Documentation module label Nov 16, 2026
gdcm::IPPSorter's default tolerance of 0 demands byte-identical
ImageOrientationPatient strings, stricter than ITK 5.4's SerieHelper.
GE MR series with per-slice rounded direction cosines then failed
geometric ordering and threw, or fell back to Instance Number order and
could produce a left-handed volume with reversed slices.

Keep the exact-match sort first. When it fails, accept the series if
every slice's unit row and column cosines are within 1e-6 (1 - cos,
about 0.08 degrees) of the first slice's, and sort again with a loose
IPPSorter tolerance, which compares unnormalized normals.
@hjmjohnson
hjmjohnson force-pushed the bug-gdcm-ippsorter-dircos-tolerance branch from a08746b to c77f723 Compare November 16, 2026 14:45
@hjmjohnson

Copy link
Copy Markdown
Member Author

Thanks @blowekamp — here is the failure in its simplest form. Take a three-slice axial MR series, slices 7 mm apart. Each DICOM file stores its own ImageOrientationPatient as text, and this GE scanner rounds each slice's copy independently:

File Instance Number Position (z) ImageOrientationPatient
a.dcm 1 14 mm 1\0\0\0\1\0
b.dcm 2 7 mm 1\0\0\0\1\0.00000002
c.dcm 3 0 mm 1\0\0\0\1\0

The orientations are the same plane; the strings differ only in the eighth decimal.

  • ITK 5.4 (SerieHelper) took the normal from the first file and sorted by position along it: c, b, a (0, 7, 14 mm). Right-handed volume, slices bottom to top.
  • main today (IPPSorter, tolerance 0) treats differing strings as different orientations, so the sort fails. By default the read throws. With SetFailOnAmbiguousOrdering(false) (what dcm-classifier and many pipelines set) it falls back to Instance Number order: a, b, c (14, 7, 0 mm). The slice spacing comes out negative, and since 5fdb32d that gives a direction matrix with det = -1. The image is still in the right place physically, but the voxel array is upside down along z compared with 5.4, and anything that reads voxels directly (our CNNs, for instance) sees a different image.
  • This PR keeps the exact-match sort first. Only when it fails does it check that every slice's row and column cosines agree with the first slice's within 1 - cos <= 1e-6 (about 0.08 deg); here they agree to about 1e-16, so it sorts by position: c, b, a, exactly as 5.4 did.

In our 155-session clinical set this happened on 11 series from two GE scanner models (Genesis Signa and SIGNA Voyager) in 3 sessions, and nowhere else. Series with genuinely different orientations (localizers, a tilted slice, an in-plane rotated slice) are still rejected; 5.4 did not check those at all.

You're right that the migration guide no longer matched. The GDCMSeriesFileNames section now has a bullet for this (a08746b, since rebased onto main as c77f723); the table above is a simplified illustration, while the real GE series are oblique 20-slice volumes with rounding near 1.6e-8.

@hjmjohnson
hjmjohnson merged commit 4cfaa3c into InsightSoftwareConsortium:main Nov 16, 2026
19 checks passed
@seanm

seanm commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

@hjmjohnson I bisected this build failure: https://open.cdash.org/builds/11713417/build

[/.../ITK/Modules/ThirdParty/VNL/src/vxl/core/vnl/algo/vnl_determinant.h:8:6](https://github.com/InsightSoftwareConsortium/ITK/blob/e544ece6b8f301e85133fc7868146e2ac99b4b85/Modules/ThirdParty/VNL/src/vxl/core/vnl/algo/vnl_determinant.h): error: "vnl_determinant was removed; migrate to itk::bridge::Math::Determinant (itkBridgeMathDeterminant.h, Eigen-backed)." 8 | # error "vnl_determinant was removed; migrate to itk::bridge::Math::Determinant (itkBridgeMathDeterminant.h, Eigen-backed)."

to c77f723.

@seanm

seanm commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

I just bisected another thing to c77f723. This time a unit test of ours started failing with this commit.

GetDidUseAmbiguousOrdering() returned true before, and after this commit now returns false (for some particular DICOM files in this test).

In the new code, in GDCMSeriesFileNames::OrderSeries, the variable wasSortingAchieved evaluates to false || (true && true).

This all seems related to #6468 and #6766.

I'll have to dig more...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Documentation Issues affecting the Documentation module area:IO Issues affecting the IO module type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances 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.

GDCMSeriesFileNames rejects series whose ImageOrientationPatient differs only by rounding

3 participants