How raselmandol/brainseg works
1. Overall shape
- This is a desktop GUI application built around PyQt6 for brain MRI segmentation.
- The core runtime is a single-process GUI app that loads an image, runs PyTorch/UNet inference from a .pth checkpoint, and displays outputs in multiple synchronized views.
2. Entry points and startup
- brainseg/__main__.py and brainseg/main.py are the launch paths.
- They bootstrap the Qt application and hand control to the main window, so the app can be started either with python -m brainseg or the legacy script path.
3. Primary UI shell
- brainseg/main_window.py is the central composition layer.
- It likely owns the menu/toolbar, dock controls, file actions, run button, progress bar, and coordinates all subwindows and canvases.
- This is the main boundary between user interaction and the rest of the application.
4. Image viewing subsystem
- brainseg/canvas.py provides the interactive image panels.
- The README describes three synchronized canvases: original image, segmentation mask, and highlighted overlay.
- These views preserve zoom/pan state while brightness/contrast is adjusted, so the canvas layer is responsible for both rendering and navigation state.
5. Image preprocessing and display transforms
- brainseg/image_utils.py likely handles loading MRI slices, resizing, normalization, and conversion between file data and Qt displayable images.
- The README notes resizing to dimensions compatible with the model encoder, especially multiples of 32, which is an important preprocessing boundary before inference.
6. Model loading and inference
- brainseg/model.py is the ML boundary.
- It likely defines the UNet architecture and the logic for loading .pth checkpoints.
- The app validates selected models immediately and surfaces incompatibility errors, so model loading is an explicit step rather than an implicit runtime failure.
7. Background execution
- brainseg/worker.py is the asynchronous execution layer.
- It likely runs segmentation and/or validation off the UI thread, updating progress and returning results to the main window.
- This separation keeps the Qt event loop responsive during inference, which matters for larger checkpoints and CPU/GPU execution time.
8. Statistics and evaluation
- brainseg/statistics_tracker.py and brainseg/statistics_window.py form the measurement subsystem.
- The app records segmentation metrics such as Dice, Jaccard, Hausdorff, latency, and memory, then presents them in a dedicated statistics window with plots/trends.
- When ground truth is loaded, the tracker has both prediction and reference inputs for comparison.
9. Auxiliary windows and user configuration
- brainseg/settings_window.py, help_window.py, system_info_window.py, and statistics_window.py are supporting dialogs.
- These are separate UI surfaces for theme/brightness settings, user help, machine/environment details, and metrics review.
- Their separation suggests a modular dialog-based Qt architecture rather than a monolithic screen.
10. Theme, palette, and branding
- brainseg/theme.py and brainseg/intensity_palette.py handle visual styling.
- The app supports light/dark themes and accent colors, plus a custom color palette applied across controls and docks.
- This is a presentation-layer concern, distinct from image processing and inference.
11. Assets and packaging
- brainseg/assets contains bundled resources used by the UI.
- The repo is packaged for Python distribution via pyproject.toml, setup.py, and requirements.txt, with a GitHub Actions workflow in .github/workflows/ci-cd.yml.
- The README shows both source install and PyPI distribution (brainseg-ai), so packaging is part of the public architecture, not just development tooling.
12. Key data flow to capture in a diagram
- User opens an MRI image → image utilities prepare it → main window pushes it to the original canvas.
- User optionally loads ground truth and a .pth model → model layer validates the checkpoint.
- User runs segmentation → worker executes inference → predicted mask and overlay are rendered to their canvases.
- Metrics are computed/recorded → statistics tracker updates the statistics window.
- Settings/theme changes propagate back into the main UI and canvases without restarting the app.
13. External dependencies that materially shape the architecture
- PyQt6 defines the event-driven desktop UI model and separate dialog/window structure.
- PyTorch defines the inference/runtime boundary and the need for checkpoint validation.
- The project is oriented toward Windows desktop usage but organized as a Python package, so the architecture is portable at the code level even if the primary target platform is Windows.
Main entry points: main_window.py Main window · worker.py Worker · theme.py Theme · canvas.py Canvases
Components 21
App runtime
Qt desktop application, image processing, inference, and statistics.
- Entry point · Qt application startup via module or script entry.
brainseg/__main__.py - Legacy main · Legacy launch path that hands off to the Qt app.
brainseg/main.py - Image utils · Load, resize, normalize, and convert MRI data for display and model input.
brainseg/image_utils.py - UNet model · Checkpoint loading and segmentation model boundary.
brainseg/model.py - Worker · Runs inference and validation off the UI thread.
brainseg/worker.py - Stats tracker · Collects segmentation quality, latency, and memory metrics.
brainseg/statistics_tracker.py
User interface
Main window plus supporting dialogs and visualization surfaces.
- Main window · Central UI shell coordinating loading, inference, views, and dialogs.
brainseg/main_window.py - Canvases · Synchronized MRI, mask, and overlay panels with zoom and pan state.
brainseg/canvas.py - Stats window · Dedicated view for metrics, plots, and trends.
brainseg/statistics_window.py - Settings · Theme and display configuration dialog.
brainseg/settings_window.py - Help · User-facing help and guidance window.
brainseg/help_window.py - System info · Environment and machine details window.
brainseg/system_info_window.py - Theme · Light/dark styling and accent coordination across the UI.
brainseg/theme.py - Palette · Color mapping used for image display and overlays.
brainseg/intensity_palette.py - Assets · Bundled UI resources and packaged artwork.
brainseg/assets
Distribution
Packaging and release metadata for installing and shipping the app.
- Build config · Python package metadata and distribution configuration.
pyproject.toml - Setup script · Legacy installation and build entry for packaging.
setup.py - Requirements · Pinned runtime dependencies for installation.
requirements.txt - CI workflow · Automated build and publish pipeline.
.github/workflows/ci-cd.yml
Other components
- PyQt6 · Event-driven desktop GUI framework.
- PyTorch · Tensor inference runtime for the segmentation model.
Connections 23
- Entry point → Main window: boot · Starts the Qt application and opens the main shell.
- Legacy main → Main window: boot · Alternative launch path into the same UI shell.
- Main window → Canvases: render · Feeds loaded images and prediction results into synchronized views.
- Main window → Image utils: prepare · Uses preprocessing for load, resize, and display conversion.
- Main window → UNet model: load model · Validates and opens the selected checkpoint.
- Main window → Worker: run · Dispatches segmentation work without blocking the UI.
- Main window → Stats window: open · Shows metrics and trend reporting.
- Main window → Settings: open · Opens application settings and display controls.
- Main window → Help: open · Shows help content to the user.
- Main window → System info: open · Displays environment and system details.
- Main window → Theme: style · Applies global UI appearance and accent settings.
- Main window → Palette: palette · Uses intensity color mapping for display surfaces.
- Main window → Assets: load resources · Loads bundled icons and UI resources.
- Worker → UNet model: infer · Runs model inference on prepared image data.
- Worker → Stats tracker: record · Captures metrics from segmentation execution.
- Stats tracker → Stats window: report · Supplies measured values for display and plots.
- Image utils → Canvases: format · Converts image data into display-ready buffers.
- Theme → Settings: configure · Themes and visual preferences are adjusted here.
- Theme → Canvases: style · Canvas rendering follows global theme settings.
- Requirements → PyQt6: depends on · Declares the GUI framework dependency.
- Requirements → PyTorch: depends on · Declares the ML runtime dependency.
- Build config → Setup script: packaging · Project metadata and build configuration align package installation.
- CI workflow → Build config: build · CI uses project metadata to validate and ship releases.