Skip to content

BUG: Fix itk.imread KeyError on unknown ImageIO component type - #7151

Merged
hjmjohnson merged 1 commit into
InsightSoftwareConsortium:mainfrom
SID-6921:bug-imread-unknown-component-type
Nov 12, 2026
Merged

hjmjohnson merged 1 commit into
InsightSoftwareConsortium:mainfrom
SID-6921:bug-imread-unknown-component-type

Conversation

@SID-6921

@SID-6921 SID-6921 commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

An ImageIO that cannot interpret a file's component type reports "unknown", which itk.imread previously surfaced as a bare KeyError('unknown') with no indication of the cause or the remedy.

The component-type lookup now raises a KeyError naming the reported type and pointing at pixel_type= and at building a typed reader directly (itk.ImageFileReader.New, itk.ImageSeriesReader.New, and itk.MeshFileReader.New all reach this same code path and take no pixel_type= argument). _pixelTypeFromIO's matching branch, shared with itk.meshread, gets the same message for an unrecognized pixel type. fallback_only=True still retries on KeyError and still fails afterward, since no wrapped reader can decode a component type the ImageIO never identified.

The regression test writes a .mha file with an ElementType MetaIO cannot interpret, exercising the real ImageIO rather than a mock, and checks that the fallback_only=True retry also fails.

Copilot AI balanced review requested due to automatic review settings November 11, 2026 00:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@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 area:Python wrapping Python bindings for a class type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct labels Nov 11, 2026
@greptile-apps

greptile-apps Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge, with a non-blocking correction needed to the shared error guidance.

Findings

  1. P2 Reader errors give wrong guidance ▶

Summary

The PR replaces a bare component-type KeyError with a message naming the reported type and suggesting an explicit pixel type.

  • Image reads name component types they cannot map.
  • Image and mesh reads explain unmapped pixel types.

Both unnumbered previous findings are addressed: the float guard is present, and the test docstring describes its current contract rather than development history.

Reviews (2) · Last reviewed commit: "BUG: Name the unrecognized type in itk.i..." · Reviewed by Greptile

@hjmjohnson hjmjohnson 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 for tracking this down. The KeyError: 'unknown' is real, and the report is clear. I am requesting changes because the float fallback does not give the result the description promises, it changes what fallback_only=True does, and the test does not exercise the case it is named for. Details are in the line comments.

Points outside the changed lines
  • The .mha example in the description. For an ElementType it cannot interpret, MetaImageIO sets the pixel type to UNKNOWNPIXELTYPE as well (itkMetaImageIO.cxx:146-151). With this PR, _pixelTypeFromIO then raises RuntimeError("Unknown pixel type: unknown.") (template_class.py:296), so that file still fails, with a different exception and no image.
  • Commits. The second commit only repairs the first, so please squash them. The first commit's body is 14 lines; ITK's cap is 12 (Documentation/AI/prose-budget.md), and its last paragraph restates the diff.
  • Greptile's docstring thread is marked resolved but is only partly addressed; see the comment on the module docstring.
  • Non-blocking, description: the visible text is well over the three-sentence budget in prose-budget.md; the long sections belong in <details> blocks. The verification section says the change was run only against a 5.4.7 wheel with one file overwritten. CI has since run the new test in an ITK build, which is the result that counts.
  • Non-blocking question: if an AI assistant helped write this, Documentation/AI/pull-requests.md asks for the PR to be opened as a draft with a short disclosure in a collapsed block.

Findings marked "reasoned from the code" were not run in a build.

@hjmjohnson
hjmjohnson marked this pull request as draft November 11, 2026 19:03
@SID-6921

SID-6921 commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor Author

Addressed all five inline, and squashed the two commits since the second only repaired the first, like you pointed out.

On the .mha example: the failure mode now matches what you predicted for a real unknown type. itk.imread() on that file raises a KeyError naming the component type and pointing at pixel_type=, instead of the old bare KeyError('unknown'). Still no image, since the type really is unrecognized, and the new test checks exactly this against a real .mha, not a mock.

@SID-6921
SID-6921 force-pushed the bug-imread-unknown-component-type branch from 62db918 to 2310dea Compare November 11, 2026 19:16
@SID-6921
SID-6921 marked this pull request as ready for review November 11, 2026 19:18
An ImageIO that cannot interpret a file's component type reports
"unknown", for which itk.imread raised KeyError('unknown') with no hint
of the cause or the remedy.

The exception stays a KeyError, because itk.imread retries on KeyError
under fallback_only=True. The advice names a typed reader as well as
pixel_type=, because itk.ImageFileReader.New and the series and mesh
readers reach the same code and take no pixel_type argument.
@hjmjohnson
hjmjohnson force-pushed the bug-imread-unknown-component-type branch from 08596b0 to 1b1af40 Compare November 12, 2026 01:33
@hjmjohnson

Copy link
Copy Markdown
Member

@SID-6921 thank you for the quick rework. To save another round I pushed the remaining small items to your branch myself; you are still the author of the commit. Nothing else is needed from you unless you disagree with a change.

What changed in this push (content only, same base)
  • One commit. The two commits are squashed, since the second only reworded the message the first added. The commit message no longer says the two errors share a message, that _NewMeshReader accepts pixel_type=, or that the test replaced a mock.
  • Both error messages now give advice every caller can follow. itk.ImageFileReader.New(FileName=...), itk.ImageSeriesReader.New(...), and itk.MeshFileReader.New(...) reach the same code and take no pixel_type= argument, so the text names a typed reader as well. The earlier wording was my suggestion, so that imprecision was mine.
  • Test docstring states only the contract, and the comment that repeated it is gone.

Checked against a local ITK 6.0.0 Python build: the test passes with these changes and fails on unmodified main; itk.imread and itk.ImageFileReader.New both raise the new KeyError. pre-commit run --all-files passes.

One request: the PR description still describes the earlier float fallback and the mocked PNG test. Could you update it to match the code, or say if you would like me to?

@hjmjohnson
hjmjohnson dismissed their stale review November 12, 2026 01:34

Requested changes are addressed in 1b1af40; dismissing my change request.

@SID-6921

Copy link
Copy Markdown
Contributor Author

Updated the description to match: dropped the float fallback and mocked PNG test references, described the actual KeyError plus typed-reader advice and the real .mha test. Thanks for pushing the rework directly, agreed on all of it.

@hjmjohnson
hjmjohnson merged commit e61d4df into InsightSoftwareConsortium:main Nov 12, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Python wrapping Python bindings for a class 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.

3 participants