Repository navigation
BUG: Accept rounding in ImageOrientationPatient when ordering series - #7177
Open
hjmjohnson wants to merge 1 commit into
Open
hjmjohnson wants to merge 1 commit into
hjmjohnson wants to merge 1 commit into
Conversation
hjmjohnson
marked this pull request as ready for review
November 16, 2026 03:47
This comment was marked as resolved.
This comment was marked as resolved.
hjmjohnson
force-pushed
the
bug-gdcm-ippsorter-dircos-tolerance
branch
from
November 16, 2026 03:56
775aa21 to
6dc3c16
Compare
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
force-pushed
the
bug-gdcm-ippsorter-dircos-tolerance
branch
from
November 16, 2026 04:05
6dc3c16 to
93228d0
Compare
hjmjohnson
marked this pull request as draft
November 16, 2026 04:06
hjmjohnson
marked this pull request as ready for review
November 16, 2026 04:06
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
GDCMSeriesFileNames(since itsgdcm::IPPSorterbackend, 50dfb13) sorts withIPPSorter's default direction-cosine tolerance of 0, which demands byte-identical ImageOrientationPatient strings. That is more stringent than ITK 5.4.8, whoseSerieHelpertook 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 withFailOnAmbiguousOrdering(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 within1 - cos <= 1e-6(about 0.08 deg) of the first slice's. Norelease-5.4backport is needed: 5.4 does not useIPPSorter.Real data: where this was found
A 155-session clinical stroke MR test set from four vendors, read on current
mainwith the settings dcm-classifier uses (SetUseSeriesDetails(true),FailOnAmbiguousOrdering(false)). Series rejected byIPPSorteronly because their IOP strings differ by rounding: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:IPPSorterfails,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, so1e-6admits at most 1.4 mrad (0.081 deg): 0.18 mm at 128 mm off-center, below one voxel at clinical MR resolutions (1e-4would admit 0.81 deg, 1.8 mm).On the 155-session set, every series with more than one IOP string, worst slice,
1 - n . n0on unit normals:1e-6sits 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 requires1 - r . r0 <= 1e-6and1 - c . c0 <= 1e-6; if so,SortByPosition(files, 1e-3)with a freshIPPSorter. A nonzeroIPPSortertolerance compares unnormalized normals, so a flat1e-6there 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 inIPPSorterare 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.FailOnAmbiguousOrdering(false),GetDidUseAmbiguousOrdering().Local run (Linux, GCC, Release; ITK
main772f60f with this PR's two files):mainInPlaneRotatedSliceIsAmbiguous