Skip to content

Fix the matplotlib backend for qc plotting#797

Merged
m-reuter merged 2 commits intoDeep-MI:devfrom
dkuegler:fix/mpl-backend
Mar 7, 2026
Merged

Fix the matplotlib backend for qc plotting#797
m-reuter merged 2 commits intoDeep-MI:devfrom
dkuegler:fix/mpl-backend

Conversation

@dkuegler
Copy link
Member

@dkuegler dkuegler commented Mar 4, 2026

HypVINN creates qc snapshots, if the --qc__snaps falg is passed, but this uses the "default" backend of matplotlib.

So qc_snapshot generation fails, when the default backend is not available, e.g. default qtagg is not available because qt is not installed/available.

To resolve, we force the add matplotlib backend with a context manager.
The contextmanager solution is also propagated into the CorpusCallosum module, where a similar problem was addressed with manual backend switches.

@dkuegler dkuegler marked this pull request as draft March 4, 2026 15:00
Changes
- Add `noop_context` and `backend` context manager for safe matplotlib backend switching FastSurferCNN/utils/__init__.py
- Use `backend('agg')` context to fix --qc_snaps failure when qt unavailable in HypVINN/utils/visualization_utils.py, CorpusCallosum/shape/endpoint_heuristic.py, CorpusCallosum/shape/postprocessing.py and CorpusCallosum/utils/visualization.py
@dkuegler dkuegler marked this pull request as ready for review March 4, 2026 15:05
@dkuegler dkuegler requested a review from Copilot March 4, 2026 15:05
Copy link
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 aims to make QC snapshot/plot generation more robust in headless environments by introducing a Matplotlib-backend context manager and using it in HypVINN and CorpusCallosum plotting paths (instead of relying on whatever the default GUI backend is).

Changes:

  • Add FastSurferCNN.utils.plotting.backend() context manager to temporarily switch Matplotlib backends.
  • Use the backend context for HypVINN QC snapshot generation and CorpusCallosum plotting/debug plotting.
  • Update HypVINN run_prediction.py execution flow (thread executor usage + improved QC future error reporting).

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
HypVINN/utils/visualization_utils.py Wrap QC snapshot figure creation/saving in a forced agg backend context; refactor cropping logic.
HypVINN/run_prediction.py Switch executor selection logic and improve QC snapshot async error handling/logging.
FastSurferCNN/utils/plotting.py New backend-switching context manager utility.
FastSurferCNN/utils/init.py Export new noop_context utility.
CorpusCallosum/utils/visualization.py Use backend context (or no-op) for plotting depending on whether saving is requested.
CorpusCallosum/shape/postprocessing.py Replace manual backend switching with backend context for interactive debug plots.
CorpusCallosum/shape/metrics.py Replace manual backend switching with backend context for interactive debug plots.
CorpusCallosum/shape/endpoint_heuristic.py Replace manual backend switching with backend context for interactive debug plots + minor plotting arg tweaks.
CorpusCallosum/segmentation/segmentation_postprocessing.py Replace manual backend switching with backend context for interactive debug plots.

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

…and use the global executor instances instead of local instances

- Only import matplotlib.pyplot after backend context switches to avoid unavailable backend errors
- Log warnings if qc image generation failed in HypVINN

Clean up formatting
@m-reuter m-reuter merged commit b2d4cc7 into Deep-MI:dev Mar 7, 2026
2 checks passed
@dkuegler dkuegler deleted the fix/mpl-backend branch March 9, 2026 08:00
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.

3 participants