Fix image reorientation and cropping bugs in conform workflow (fixes #764)#767
Draft
dkuegler wants to merge 17 commits intoDeep-MI:devfrom
Draft
Fix image reorientation and cropping bugs in conform workflow (fixes #764)#767dkuegler wants to merge 17 commits intoDeep-MI:devfrom
dkuegler wants to merge 17 commits intoDeep-MI:devfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This work-in-progress PR addresses off-by-one errors in MGH header handling related to the Pxyz_c calculation, which represents the center of the image but is offset by half a voxel. The fix introduces two feature flags to optionally correct the affine calculation bug in nibabel's MGHHeader.get_affine() method.
Changes:
- Introduces two feature flags (FIX_MGH_AFFINE_CALCULATION and FIX_CENTER_NOT_CENTER) to control the fix behavior
- Adds helper functions get_affine_from_mgh() and get_affine_from_any() to provide corrected affine calculations
- Modifies offset calculation in map_image() to account for off-by-one errors when flipping orientation
- Updates center calculation logic in prepare_mgh_header() with conditional fix based on feature flags
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
b059c18 to
d8b8d42
Compare
b053d7f to
90d5f5e
Compare
a4e572a to
8d15c5c
Compare
744a9f2 to
f7bff6b
Compare
f7bff6b to
e32ffa1
Compare
Member
Author
…bugfix to the property SubjectDirectory.asegdkt_segfile
Use this function in srun_fastsurfer and run_fastsurfer.sh
- make quick_qc.py follow the general formatting style - add ty into style extra (pyproject.toml) - Make brainvolstats satisfy ty typing rules
Add en_US.UTF-8 and C.UTF-8 to fix locale warnings. Update Singularity doc (workdir warning). Fix sep=>end argument in build.py's print statements on --print. Fix the default tag of the docker image, if no tag name is given (added v) and the --tag_dev default (<DEVICE>-dev instead of dev<DEVICE>-)
Formatting Cleanup
Fix formatting
This commit refactors the image orientation and resampling code to fix two critical issues with image handling. First, it corrects the image cropping behavior to ensure proper alignment after reorientation (fixes Deep-MI#764). Second, it addresses the image center offset issue that occurs when flipping image orientations, which was caused by MGH format assumptions about voxel center positioning (relates to freesurfer #1358). A new Reorientation class encapsulates the orientation transformation logic for improved maintainability and correctness. Comprehensive unit tests validate the orientation transformations and world coordinate consistency.
Remove pandas-stubs dependency Remove unused code Add deprecated callback implementation for Python <=3.13 replace Numpy 1 function deprecate_with_doc by deprecated for numpy 2 Add NPY201 rule to pyproject.toml
9678e1b to
539d7ad
Compare
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.
Problem Statement
This PR addresses two related issues with FastSurfer's image reorientation and conforming pipeline:
Issue 1 - Incorrect Image Cropping (#764): The previous implementation had bugs in how images were cropped during reorientation, leading to misaligned data being processed by downstream models. This caused spatial errors in the segmentation pipeline.
Issue 2 - Image Center Offset During Orientation Flipping (related to FreeSurfer #1358): When images are flipped to change orientation (e.g., LAS→RAS), the affine matrix must correctly maintain the image center. The original code did not properly account for the MGH format's definition of image center at
Ni/2instead of(Ni-1)/2, causing the image content to shift in world coordinates during reorientation.Solution
This PR introduces a comprehensive refactoring of the image orientation code:
ReorientationClass: Encapsulates all orientation transformation logic in a dedicated class that:to_target_orientation,orientation_to_ornts)run_prediction.pywith a clearer APIChanges
Testing
All changes are covered by new comprehensive unit tests that verify:
Backward Compatibility
to_target_orientation()has been removed (useReorientation.from_target_orientation()instead)orientation_to_ornts()has been removed (internal functionality now in Reorientation class)Related Issues