Skip to content

CC midplane refinement#801

Merged
m-reuter merged 15 commits intoDeep-MI:devfrom
ClePol:CC_midplane_refinement
Apr 13, 2026
Merged

CC midplane refinement#801
m-reuter merged 15 commits intoDeep-MI:devfrom
ClePol:CC_midplane_refinement

Conversation

@ClePol
Copy link
Copy Markdown
Member

@ClePol ClePol commented Apr 8, 2026

This PR introduces a midplane refinement strategy for asymmetric brains.
The refinement strategy still relies on symmetry between structures but considers only structures close to the initially found mid-sagittal plane. This improves the midplane on previously challenging cases, while not affecting others.

The refinement can be disabled via flag. Also, it is not possible to completely skip midsagittal plane alignment for testing.

Copy link
Copy Markdown
Member

@dkuegler dkuegler left a comment

Choose a reason for hiding this comment

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

We should think about whether we want to move all the formatting changes out of this PR into a separate PR. I also have a number of formatting cleanup things, we should probably put those changes into a formatting PR...

Comment thread doc/api/CorpusCallosum.rst Outdated
Comment thread doc/scripts/fastsurfer_cc.rst Outdated
Comment thread doc/scripts/fastsurfer_cc.rst Outdated
Comment thread doc/scripts/fastsurfer_cc.rst
Comment thread .gitignore
Comment thread CorpusCallosum/registration/midsagittal_plane_alignment.py Outdated
Comment thread CorpusCallosum/registration/midsagittal_plane_alignment.py Outdated
Comment thread CorpusCallosum/registration/midsagittal_plane_alignment.py
Comment thread CorpusCallosum/registration/midsagittal_plane_alignment.py Outdated
@ClePol
Copy link
Copy Markdown
Member Author

ClePol commented Apr 10, 2026

We should think about whether we want to move all the formatting changes out of this PR into a separate PR. I also have a number of formatting cleanup things, we should probably put those changes into a formatting PR...

Since they are constrained to the CC module and separate commits, I think it wouldn't make a difference after this PR is merged

@ClePol
Copy link
Copy Markdown
Member Author

ClePol commented Apr 12, 2026

Adressed/replied to all coments @dkuegler

ClePol added 13 commits April 13, 2026 13:45
Return MidplaneRefinementResult/MidplaneDebugVolumes dataclasses for named field access.
Refine midsagittal LR correction heuristic
Create parent directories for CC VTK output
Adds --midplane_method {none,lr_shift,distance_map} argument to
fastsurfer_cc.py to allow comparing all three midplane refinement
conditions in a single codebase without branch switching. Defaults to
'distance_map' (existing behaviour). Debug volume saves are guarded so
they are skipped for non-distance_map methods.

Ignores /scripts/ in .gitignore (local experiment scripts only).
- Fix _prepare_lr_pair_labels: replace incorrect +1000 label offset
  assumption with an explicit FreeSurfer aseg left/right pair table
  (_ASEG_LR_PAIRS). The old code only ever matched WM (2,41) via the
  fallback; now all standard aseg bilateral structures are used.
- Fix midline_refine_shift_vox stored as int(): changed to float() so
  sub-voxel distance_map corrections are not truncated.
- Refactor: refine_midline_lr_shift now calls resample_segmentation_to_fsavg
  instead of duplicating the affine_transform logic.
- Add no_baseline flag to lr_shift diagnostics dict.
- Change aseg_nib (nibabelImage) parameter to aseg_data (np.ndarray) in
  both refine_midline_lr_shift and refine_midplane_with_distance_maps;
  callers extract the array before the call.
New choices (old → new):
  none         → fsaverage                  (fsaverage alignment only)
  lr_shift     → fsaverage_symmetry         (default: fsaverage + LR label-symmetry shift)
  distance_map → fsaverage_distance_map     (fsaverage + distance-map plane fit)
  [new]          center                     (center slice of input volume, no alignment)

The 'center' option skips register_centroids_to_fsavg entirely and uses
an identity vox2vox with the volume's own affine, setting the midplane
at shape[0]/2 in original image space.

Replace hardcoded FSAVERAGE_MIDDLE/vox_size on the fsavg2midslice line
with _base_middle_vox so 'center' uses the correct midpoint.
Move register_centroids_to_fsavg, refine_midline_lr_shift, and all
supporting helpers (_ASEG_LR_PAIRS, _prepare_lr_pair_labels,
_score_midline_shift) from fastsurfer_cc.py into midplane_refinement.py.

Add MidplaneTransformResult dataclass and find_midplane_transform(),
a single entry point that dispatches across all four midplane methods
("center", "fsaverage", "fsaverage_symmetry", "fsaverage_distance_map")
and returns a uniform result.

Replace the scattered midplane dispatch block in main() with one call
to find_midplane_transform(). Remove the five debug-output CLI flags
(--midplane_distance_map, --midplane_fit_mask, etc.) and their
corresponding save blocks, which were used only during development.
- Move _ASEG_LR_PAIRS comment to midplane_refinement.py where the
  constant now lives
- Remove [tool.uv.sources] whippersnappy git override from pyproject.toml
  (conflicts with the >=2.1 release pin kept from HEAD)
- Revert Dockerfile whippersnappy dep from libosmesa6 back to libegl1
  (HEAD's choice, overriding the no-gpu rendering change from 4a694e3)
@dkuegler dkuegler force-pushed the CC_midplane_refinement branch from 0352c0d to 6ec7400 Compare April 13, 2026 11:46
@dkuegler
Copy link
Copy Markdown
Member

I cleaned up the history a bit.

I guess we can merge this now.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds configurable midsagittal plane refinement options to the Corpus Callosum (CC) pipeline, improving robustness on asymmetric anatomies by refining alignment only near the initially estimated midplane.

Changes:

  • Introduces CorpusCallosum/registration/midsagittal_plane_alignment.py with multiple midplane strategies (fsaverage-only, symmetry-based LR shift refinement, distance-map plane fitting, and center-based mode).
  • Adds --midplane_method CLI flag in fastsurfer_cc.py and stores refinement diagnostics in output metrics.
  • Updates documentation and API docs to describe the new midplane extraction/refinement options; adds a small mesh I/O improvement.

Reviewed changes

Copilot reviewed 7 out of 8 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
doc/scripts/fastsurfer_cc.rst Documents --midplane_method and available midplane refinement modes.
doc/overview/modules/CC.md Updates pipeline overview wording/order related to midsagittal extraction.
doc/api/index.rst Adds CC registration module to API docs index.
doc/api/CorpusCallosum.registration.rst New Sphinx API stub for CorpusCallosum.registration.
CorpusCallosum/shape/mesh.py Ensures parent directory exists before writing VTK meshes.
CorpusCallosum/registration/midsagittal_plane_alignment.py New midplane alignment/refinement implementation and related dataclasses.
CorpusCallosum/fastsurfer_cc.py Wires in new midplane transform selection via --midplane_method and emits diagnostics.
.gitignore Ignores a top-level /scripts/** directory.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread CorpusCallosum/fastsurfer_cc.py Outdated
Comment thread CorpusCallosum/registration/midsagittal_plane_alignment.py Outdated
Comment thread CorpusCallosum/registration/midsagittal_plane_alignment.py
Comment thread CorpusCallosum/fastsurfer_cc.py
m-reuter and others added 2 commits April 13, 2026 16:10
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@m-reuter m-reuter merged commit 8101f69 into Deep-MI:dev Apr 13, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants