# pbfslab/deepprep architecture

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

- Interactive diagram: https://gitdiagram.com/pbfslab/deepprep
- Repository: https://github.com/pbfslab/deepprep

## Overview

##### Project Type and Purpose
- DeepPrep is a neuroimaging preprocessing pipeline that uses deep‐learning modules (FastSurfer, FastCSR, SUGAR, SynthMorph) orchestrated by Nextflow.
- It ingests BIDS‐formatted MRI data, applies multiple deep‐learning inference steps, handles volumetric and surface spaces, and produces preprocessed outputs plus QC and runtime reports.

##### File Structure
- Top‐level “Docker” folder: Dockerfiles and Singularity definitions for CPU/GPU containers.
- “deepprep” folder:
– Subdirectories for each module: FastCSR, FastSurfer (wrapper around FreeSurfer), SUGAR, SynthMorph.
– Nextflow pipeline definitions under “nextflow/” (deepprep.nf, config files, cluster configs).
– QC scripts under “qc/” (qc.nf, QC Python bins).
– Web UI under “web/” (Python pages for interactive runs).
– Entry‐point shell script “deepprep.sh” and Python “setup.py” for CLI installation.
- “docs/”: Sphinx documentation sources.

##### Main Components
A. Data Ingestion
- BIDS-formatted MRI dataset (Anat & BOLD)
B. Workflow Manager
- Nextflow core (“deepprep.nf” + configs)
- Executor back‐ends: local, Docker, Singularity, SLURM
C. Deep‐Learning Modules (each in containers)
1. FastSurfer (cortical surface reconstruction)
2. FastCSR (cortical surface registration)
3. SUGAR (surface‐based registration multi‐level)
4. SynthMorph (volumetric normalization)
5. FreeSurfer licensing fallback
D. QC & Reporting
- QC Nextflow subworkflow (“qc.nf”) calling Python scripts → HTML/PDF reports
- Runtime profiling reports via Nextflow reports
E. Web Interface
- Streamlit‐style pages under “web/pages” for GUI runs
F. Container Layer
- Dockerfiles (project‐wide + per‐module)
- Singularity definitions
G. External Services & Config
- FreeSurfer license server/file
- HPC cluster scheduler (SLURM, etc.)

##### Relationships & Interactions
- Within Manager, fan out to each deep‐learning module in parallel (if resources permit).
- Modules read/write to a shared “work” directory or object store.
- Process outputs feed QC workflow and final “derivatives” directory.
- Web UI triggers Nextflow with user parameters and displays logs/reports.
- Container layer encloses Nextflow and module tasks.
- Scheduler (SLURM/Docker) sits under Nextflow for resource allocation.

##### Architectural Patterns & Design Principles
- Pipeline Orchestration Pattern: Nextflow defines directed acyclic graph (DAG) of processes.
- Modular Plugin Pattern: each algorithm is a self‐contained module with its own Dockerfile.
- Separation of Concerns: data ingestion, processing, QC, reporting, and UI are distinct layers.
- Infrastructure As Code: cluster and container configs versioned alongside pipeline code.

##### Technologies & Frameworks to Annotate
- Nextflow DSL (pipeline scripts .nf, .config)
- Python 3.x (modules, QC scripts, web UI)
- Docker & Singularity (containerization)
- BIDS standard (input/output structure)
- HPC schedulers (SLURM, local executors)
- Sphinx (documentation)

##### Essential Details to Capture
- The two major workflows: anatomical (T1w) and functional (BOLD) pipelines
- Interdependency among modules (e.g., FastSurfer output → FastCSR input)
- QC subworkflow triggered post‐processing
- Container orchestration by Nextflow executors
- Support for Docker vs. Singularity vs. local vs. HPC
- Web UI invoking the same Nextflow pipeline with custom parameters

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Data Ingestion Layer
    subgraph "BIDS Input" 
        direction TB
        A1["anat_get_t1w_file_in_bids.py"]:::datastore
        A2["bold_get_bold_file_in_bids.py"]:::datastore
    end

    %% Workflow Manager
    subgraph "deepprep/nextflow" 
        direction TB
        NF["deepprep.nf"]:::workflow
        CONF1["deepprep.common.config"]:::workflow
        CONF2["deepprep.local.config"]:::workflow
        CONF3["cluster configs"]:::workflow
    end

    %% Execution Backends
    subgraph "Executors" 
        direction TB
        ExecLocal["Local Executor"]:::infra
        ExecDocker["Docker Executor"]:::infra
        ExecSing["Singularity Executor"]:::infra
        ExecSLURM["SLURM Executor"]:::infra
    end

    %% Container Layer
    subgraph "Containers" 
        direction TB
        CProj["Docker/ubuntu22.04.Dockerfile"]:::container
        CSing["Docker/singularity_ubuntu22.04.def"]:::container
        CModFS["deepprep/FastCSR/docker/Dockerfile_cpu&gpu"]:::container
    end

    %% Deep-Learning Modules
    subgraph "Deep-Learning Modules" 
        direction TB
        M1["FastSurfer"]:::module
        M2["FastCSR"]:::module
        M3["SUGAR"]:::module
        M4["SynthMorph"]:::module
        M5["FreeSurfer License"]:::module
    end

    %% QC & Reporting
    subgraph "QC & Reporting" 
        direction TB
        QCWF["qc.nf"]:::workflow
        QCScripts["QC Python scripts"]:::module
        Reports["reports-spec-deepprep.yml"]:::workflow
    end

    %% Web Interface
    subgraph "Web UI" 
        direction TB
        UI1["DeepPrep.py"]:::ui
        UI2["4_🖥️Terminal.py"]:::ui
        UI3["Streamlit Pages"]:::ui
    end

    %% Outputs
    subgraph "Outputs"
        direction TB
        OutDer["Derivatives Directory"]:::datastore
        OutQC["QC & HTML/PDF Reports"]:::datastore
        OutRun["Nextflow Runtime Reports"]:::datastore
    end

    %% Data Flow Connections
    A1 -->|reads| NF
    A2 -->|reads| NF

    NF --> ExecLocal
    NF --> ExecDocker
    NF --> ExecSing
    NF --> ExecSLURM

    ExecLocal --> CProj
    ExecDocker --> CProj
    ExecSing --> CSing
    ExecSLURM --> CSing

    CProj --> M1
    CProj --> M2
    CProj --> M3
    CProj --> M4
    CProj --> M5

    M1 -->|output| M2
    M2 -->|output| M3
    M3 -->|output| M4
    M4 -->|output| OutDer
    M5 -->|license| M1

    M1 --> QCWF
    M2 --> QCWF
    M3 --> QCWF
    M4 --> QCWF

    QCWF --> QCScripts
    QCScripts --> Reports
    Reports --> OutQC
    QCWF --> OutRun

    %% Web UI Interactions
    UI1 -->|triggers| NF
    UI2 -->|triggers| NF
    UI3 -->|displays| OutQC
    UI3 -->|displays| OutRun

    %% Click Events - Data Ingestion
    click A1 "https://github.com/pbfslab/deepprep/blob/main/deepprep/nextflow/bin/anat_get_t1w_file_in_bids.py"
    click A2 "https://github.com/pbfslab/deepprep/blob/main/deepprep/nextflow/bin/bold_get_bold_file_in_bids.py"

    %% Click Events - Workflow Manager
    click NF "https://github.com/pbfslab/deepprep/blob/main/deepprep/nextflow/deepprep.nf"
    click CONF1 "https://github.com/pbfslab/deepprep/blob/main/deepprep/nextflow/deepprep.common.config"
    click CONF2 "https://github.com/pbfslab/deepprep/blob/main/deepprep/nextflow/deepprep.local.config"
    click CONF3 "https://github.com/pbfslab/deepprep/blob/main/deepprep/nextflow/cluster/deepprep.slurm"

    %% Click Events - Modules
    click M1 "https://github.com/pbfslab/deepprep/tree/main/deepprep/FastSurfer/"
    click M2 "https://github.com/pbfslab/deepprep/tree/main/deepprep/FastCSR/"
    click M3 "https://github.com/pbfslab/deepprep/tree/main/deepprep/SUGAR/"
    click M4 "https://github.com/pbfslab/deepprep/tree/main/deepprep/SynthMorph/"
    click M5 "https://github.com/pbfslab/deepprep/blob/main/deepprep/FreeSurfer/license.txt"

    %% Click Events - QC & Reporting
    click QCWF "https://github.com/pbfslab/deepprep/blob/main/deepprep/qc/qc.nf"
    click QCScripts "https://github.com/pbfslab/deepprep/tree/main/deepprep/qc/bin/"

    %% Click Events - Web UI
    click UI1 "https://github.com/pbfslab/deepprep/blob/main/deepprep/web/DeepPrep.py"
    click UI2 "https://github.com/pbfslab/deepprep/blob/main/deepprep/web/4_🖥️Terminal.py"
    click UI3 "https://github.com/pbfslab/deepprep/tree/main/deepprep/web/pages/"

    %% Click Events - Containers
    click CProj "https://github.com/pbfslab/deepprep/blob/main/Docker/ubuntu22.04.Dockerfile"
    click CSing "https://github.com/pbfslab/deepprep/blob/main/Docker/singularity_ubuntu22.04.def"
    click CModFS "https://github.com/pbfslab/deepprep/tree/main/deepprep/FastCSR/docker/"
    
    %% Styles
    classDef workflow fill:#dae8fc,stroke:#6c8ebf;
    classDef module fill:#d5e8d4,stroke:#82b366;
    classDef datastore fill:#fff2cc,stroke:#d6b656;
    classDef ui fill:#f8cecc,stroke:#b85450;
    classDef container stroke-dasharray: 5 5,fill:#ffffff,stroke:#888888;
    classDef infra fill:#e1d5e7,stroke:#9673a6;
```
