Skip to content

CC midplane refinement#801

Open
ClePol wants to merge 17 commits intoDeep-MI:devfrom
ClePol:CC_midplane_refinement
Open

CC midplane refinement#801
ClePol wants to merge 17 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.

dkuegler and others added 17 commits March 31, 2026 12:36
…o 2.0:

1. pyproject.toml — Version pin updated
whippersnappy>=1.3.1 → whippersnappy>=2.0
2. requirements.mac.txt — Version pin updated
whippersnappy>=1.3.1 → whippersnappy>=2.0
3. run_fastsurfer.sh — Removed xvfb orchestration (lines 767–794 removed, line 1184 simplified)
Removed the entire maybe_xvfb block: xvfb-run detection, the glfw/OpenGL/whippersnappy.core import test, and all associated warning messages. Whippersnappy 2.0 renders headlessly via native EGL — no virtual framebuffer needed.
Removed "${maybe_xvfb[@]}" prefix from the CC module command invocation.
Removed the obsolete comment about xvfb-run wrapping.
4. tools/Docker/Dockerfile — Slimmed runtime dependencies
whippersnappy_opengl_deps="xvfb libglib2.0-0 libxkbcommon-x11-0 libgl1 libegl1 libfontconfig1 libdbus-1-3" → whippersnappy_egl_deps="libegl1 libgl1 libfontconfig1"
Removed: xvfb (virtual framebuffer, replaced by native EGL), libglib2.0-0 (X11/GUI stack), libxkbcommon-x11-0 (X11 keyboard), libdbus-1-3 (D-Bus, X11-related)
Kept: libegl1 (EGL rendering), libgl1 (OpenGL), libfontconfig1 (font rendering for captions/colorbars)
5. CorpusCallosum/shape/mesh.py — Updated import and API call
Removed import OpenGL.GL pre-check (whippersnappy 2.0 manages its own GL context)
Changed from whippersnappy.core import snap1 → from whippersnappy import snap1 (new top-level import)
Removed the except Exception handler about xvfb (no longer relevant with EGL)
Updated snap1() call: positional first arg → mesh=, overlaypath= → overlay= (new API parameter names)
6. CorpusCallosum/cc_visualization.py — Updated version references
Help text and warning messages: whippersnappy>=1.3.1 → whippersnappy>=2.0
7. CorpusCallosum/shape/postprocessing.py — Updated error messages
ImportError message: removed "glfw or OpenGL" (whippersnappy handles these internally)
Generic Exception message: replaced xvfb guidance with EGL/libegl1 guidance
…face and overlay files.

Use direct passing of the surface and overlay data instead of writing the files to disc first.
Remove and simplify commands, arguments and the like that are a relict of older whippersnappy versions, now fully requiring whippersnappy 2.
Address github co-pilot suggestions
replace libegl with libmesa in docker for no-gpu rendering
Move distance-map midsagittal refinement logic from fastsurfer_cc.py into
CorpusCallosum/midplane_refinement.py. Replace tuple return with
MidplaneRefinementResult/MidplaneDebugVolumes dataclasses for named field
access. Fix missing convert_numpy_to_json_serializable import and ruff
warnings (F821, B905, E501).
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)
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.

2 participants