# deep-mi/fastsurfer architecture

> Architecture diagram of the GitHub repository deep-mi/fastsurfer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/deep-mi/fastsurfer
- Repository: https://github.com/deep-mi/fastsurfer

## Overview

##### Project Type and Purpose
- FastSurfer is a deep-learning based neuroimaging pipeline designed for fast and accurate segmentation and surface reconstruction. It serves as an alternative to FreeSurfer for processing T1‐weighted MRI data.
- Its purpose is twofold: (a) segmentation (whole brain, cerebellum, and hypothalamus sub-segmentation via FastSurferVINN, CerebNet, and HypVINN) and (b) surface reconstruction.

##### Main Components and Architectural Breakdown
- User Interface/Entry Point:
- The primary entry point is the shell script “run_fastsurfer.sh” that orchestrates the entire pipeline.
- Additional entry points include CLI interfaces for containerized invocations (Docker/Singularity) and native installations.
- Segmentation Modules:
- FastSurferCNN: Contains code for whole brain segmentation using the FastSurferVINN deep learning network.
- CerebNet: Focused on cerebellum segmentation/sub-segmentation.
- HypVINN: Handles hypothalamus sub-segmentation.
- Each module has its own configuration directory (with YAML, JSON files), data loaders, model definitions, and utility scripts.
- Data Handling and Pre-processing:
- Data loader and dataset directories in each segmentation module (e.g., “data_loader” in CerebNet, FastSurferCNN, and HypVINN) set up the data augmentation, formatting, and loading pipelines.
- Common configuration files and utility modules (e.g., load_config.py, augmentation.py) ensure standardization across modules.
- Surface Reconstruction:
- The “recon_surf” directory contains the pipeline for reconstructing cortical surfaces and mapping labels, mimicking the FreeSurfer approach.
- Auxiliary Components:
- Docker and Singularity directories contain container build scripts (Dockerfile, entrypoint.sh) and documentation to ensure portability.
- CI/CD (GitHub actions, workflows, Travis) are evident in the .github and .travis.yml files.
- Testing components under “test/quicktest” provide scripts for verifying image outputs and log validation.
- Documentation (“doc” folder) and Tutorials provide step-by-step examples that help explain overall usage.

##### Relationships and Data Flow
- Then split into parallel segmentation paths:
- FastSurferCNN (whole brain segmentation) feeds into downstream processing.
- CerebNet module processes cerebellum-specific segmentation.
- HypVINN operates as a dedicated hypothalamus segmenter.
- An orchestration layer (or pipeline coordinator) oversees passing outputs between segmentation modules and then directs them into the surface reconstruction module.
- Data loaders and configuration managers (supported by various config and utils modules) are depicted as shared components that feed parameters and pre-processed data to each segmentation engine.
- The integration with external dependencies (e.g., FreeSurfer license for the surface pipeline) should be noted.

##### Architectural Patterns and Design Principles
- Modular Design – Each segmentation module (FastSurferCNN, CerebNet, HypVINN) is self-contained with its own data loaders, models, and configuration files.
- Pipeline Orchestration – A clear data flow is maintained from input data, through segmentation networks, to surface reconstruction.
- Configuration and Extensibility – A variety of YAML, JSON, and Python configuration files indicate that the system is designed to be configurable and easily extended.
- Containerization and CI/CD – The use of Docker, Singularity, and GitHub workflows shows a modern DevOps integration.

##### Detailed Elements to be Included
- Segmentation Modules: FastSurferCNN, CerebNet, HypVINN with their individual configuration, data loader, and model blocks.
- Surface Reconstruction Module: “recon_surf” with dependency on previous segmentation outputs.
- Shared Components: Utility modules (in “utils”), configuration management (YAML/JSON files), and data augmentation (in “data_loader”).
- Testing and Validation: Indicate the presence of a “test” layer that validates output files/segmentation results.
- Deployment: Docker and Singularity container support shown as wrapping the core pipeline, with GitHub Actions and CI/CD integration.

## Diagram (Mermaid)

```mermaid
flowchart TD
    MRI["Input MRI (T1w/T2w)"]:::input
    RunFS["run_fastsurfer.sh"]:::entry
    MRI -->|"start"| RunFS

    subgraph "Segmentation Modules"
        subgraph "FastSurferCNN - Whole Brain Segmentation"
            FS["FastSurferCNN"]:::module
            FS_dl["Data Loader"]:::data
            FS_conf["Configuration"]:::config
            FS_utils["Utilities"]:::utils
            FS_dl --> FS
            FS_conf --> FS
            FS_utils --> FS
        end
        subgraph "CerebNet - Cerebellum Segmentation"
            CN["CerebNet"]:::module
            CN_dl["Data Loader"]:::data
            CN_conf["Configuration"]:::config
            CN_utils["Utilities"]:::utils
            CN_dl --> CN
            CN_conf --> CN
            CN_utils --> CN
        end
        subgraph "HypVINN - Hypothalamus Segmentation"
            HV["HypVINN"]:::module
            HV_dl["Data Loader"]:::data
            HV_conf["Configuration"]:::config
            HV_utils["Utilities"]:::utils
            HV_dl --> HV
            HV_conf --> HV
            HV_utils --> HV
        end
    end

    RunFS --> FS
    RunFS --> CN
    RunFS --> HV

    PC["Pipeline Coordinator"]:::orchestrator
    FS --> PC
    CN --> PC
    HV --> PC

    RS["Recon-Surf (Surface Reconstruction)"]:::recon
    PC -->|"segmentation_outputs"| RS

    subgraph "Deployment & Integration"
        D["Docker"]:::deploy
        S["Singularity"]:::deploy
        GH["GitHub Actions"]:::ci
        TV["Travis CI"]:::ci
        T["Testing"]:::test
        DOC["Documentation"]:::doc
        TUT["Tutorials"]:::doc
    end

    RunFS -->|"containerized"| D
    RunFS -->|"containerized"| S
    RunFS -->|"CI/CD_trigger"| GH
    RunFS -->|"CI/CD_trigger"| TV
    RS -->|"validated_by"| T
    RS -->|"refer_docs"| DOC
    RS -->|"refer_tutorials"| TUT

    %% Click Events
    click RunFS "https://github.com/deep-mi/fastsurfer/blob/dev/run_fastsurfer.sh"
    click FS "https://github.com/deep-mi/fastsurfer/tree/dev/FastSurferCNN/"
    click FS_dl "https://github.com/deep-mi/fastsurfer/tree/dev/FastSurferCNN/data_loader/"
    click FS_conf "https://github.com/deep-mi/fastsurfer/tree/dev/FastSurferCNN/config/"
    click FS_utils "https://github.com/deep-mi/fastsurfer/tree/dev/FastSurferCNN/utils/"
    click CN "https://github.com/deep-mi/fastsurfer/tree/dev/CerebNet/"
    click CN_dl "https://github.com/deep-mi/fastsurfer/tree/dev/CerebNet/data_loader/"
    click CN_conf "https://github.com/deep-mi/fastsurfer/tree/dev/CerebNet/config/"
    click CN_utils "https://github.com/deep-mi/fastsurfer/tree/dev/CerebNet/utils/"
    click HV "https://github.com/deep-mi/fastsurfer/tree/dev/HypVINN/"
    click HV_dl "https://github.com/deep-mi/fastsurfer/tree/dev/HypVINN/data_loader/"
    click HV_conf "https://github.com/deep-mi/fastsurfer/tree/dev/HypVINN/config/"
    click HV_utils "https://github.com/deep-mi/fastsurfer/tree/dev/HypVINN/utils/"
    click RS "https://github.com/deep-mi/fastsurfer/tree/dev/recon_surf/"
    click D "https://github.com/deep-mi/fastsurfer/tree/dev/Docker/"
    click S "https://github.com/deep-mi/fastsurfer/tree/dev/Singularity/"
    click GH "https://github.com/deep-mi/fastsurfer/tree/dev/.github/"
    click TV "https://github.com/deep-mi/fastsurfer/blob/dev/.travis.yml"
    click T "https://github.com/deep-mi/fastsurfer/tree/dev/test/quicktest/"
    click DOC "https://github.com/deep-mi/fastsurfer/tree/dev/doc/"
    click TUT "https://github.com/deep-mi/fastsurfer/tree/dev/Tutorial/"

    %% Styles
    classDef input fill:#AFEEEE,stroke:#333,stroke-width:2px;
    classDef entry fill:#FFD700,stroke:#333,stroke-width:2px;
    classDef module fill:#B0E0E6,stroke:#333,stroke-width:1px;
    classDef data fill:#E6E6FA,stroke:#333,stroke-width:1px;
    classDef config fill:#98FB98,stroke:#333,stroke-width:1px;
    classDef utils fill:#F0E68C,stroke:#333,stroke-width:1px;
    classDef orchestrator fill:#FFA07A,stroke:#333,stroke-width:2px;
    classDef recon fill:#90EE90,stroke:#333,stroke-width:2px;
    classDef deploy fill:#ADD8E6,stroke:#333,stroke-width:1px;
    classDef ci fill:#FFB6C1,stroke:#333,stroke-width:1px;
    classDef test fill:#D3D3D3,stroke:#333,stroke-width:1px;
    classDef doc fill:#FFE4B5,stroke:#333,stroke-width:1px;
```
