# project-monai/monailabel architecture

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

- Interactive diagram: https://gitdiagram.com/project-monai/monailabel
- Repository: https://github.com/project-monai/monailabel

## Overview

##### Project Type and Purpose
- This is a Python-based server–client framework for AI-assisted interactive medical image annotation.
- The “MONAI Label Server” exposes REST endpoints; “Clients” include CLI tools and viewer plugins (3D Slicer, OHIF, QuPath, CVAT, DSA, etc.).
- Core functionality: load medical images, run inference/training tasks in response to user interactions, and store results back to datastores.

##### File Structure
- Top-level “monailabel” package:
- app.py / main.py – server bootstrap and FastAPI (Starlette) app
- endpoints/ – one module per REST route (infer, train, active learning, login, logs, proxy, etc.)
- interfaces/ – abstract interfaces for tasks, datastores, utils
- tasks/ – implementations of inference, training, scoring, active‐learning pipelines
- datastore/ – adapters for Local FS, DICOMWeb, CVAT, DSA, XNAT, plus helpers to convert and colorize
- utils/ – session management, asynchronous helper, transform pipelines
- “plugins/” directory: viewer-specific integration modules (OHIF, Slicer, QuPath, CVAT, etc.)
- “sample-apps/”: prebuilt app definitions (radiology, pathology, endoscopy, monaibundle) that load configs, model bundles, transforms.
- tests/ and docs/ indicate a CI/CD pipeline and extensive coverage.

##### README for Additional Insights
- The README describes a server–client model, lists supported viewers and datastores, and enumerates core features: interactive tools (DeepEdit, DeepGrow, SAM 2.0), active learning, model bundles.
- It clarifies that server and client can run on separate machines, communicating over HTTP/DICOMWeb.

- Clients / Viewers (purple):
– CLI client (monailabel client.py)
– Viewer plugins (OHIF, QuPath, 3DSlicer, CVAT, DSA, MITK)
- MONAI Label Server (blue):
– API Layer (FastAPI/Starlette + endpoint modules)
– Orchestration Layer (interfaces + task handlers)
– ML Engine (MONAI models, SAM 2D/3D, active learning logic)
– Utilities (async task manager, transforms, logging, sessions)
- Datastores (orange):
– Local filesystem (NIfTI, NRRD, TIFF, SVS, video frames + “labels/final”)
– DICOMWeb / PACS
– External annotation stores (CVAT, DSA, XNAT)
- Model Zoo / Bundles (green):
– Pretrained segmentation models, DeepEdit/DNN architecture, monaibundle archives
- External Services (red):
– DICOMWeb server, Authentication provider (if used), Database (optional for sessions/logs)

1. Client → Server (HTTP REST calls: /infer, /train, /activelearning)
2. API Layer → Orchestration Layer (delegates to interfaces/task modules)
3. Task module → Datastore adapter (load images, labels)
4. Task module → ML Engine (run inference/training)
5. ML Engine → Datastore adapter (write back predictions/metrics)
6. Task module → Async Task Manager (for long-running jobs)
7. Async Task Manager → Notification channel (polling or WebSocket updates)
8. Server ↔ External DICOMWeb / PACS (when configured)

c. Architectural Patterns & Principles:
- Layered architecture: clear separation between API, business logic (task handlers), and data layer (datastore).
- Plugin/Adapter pattern: viewer integrations and datastore connectors are pluggable modules.
- Dependency injection via configuration—apps load only required models, datastores, tasks.
- Asynchronous processing for heavy compute/training tasks.

d. Key Technologies & Libraries:
- Python 3.8+; FastAPI (Starlette) for REST
- Pydantic for request/response models
- MONAI for deep learning models, transforms
- PyTorch or TensorFlow backends (depending on sample app)
- Docker for containerized deployment
- CUCIM/CuPy optional GPU acceleration
- Third-party viewers use REST or DICOMWeb clients

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Clients Layer
    subgraph "Clients / Viewers"
        direction TB
        CLI["CLI Client"]:::client
        OHIF["OHIF Plugin"]:::client
        OHIFv3["OHIFv3 Extension"]:::client
        Slicer["3D Slicer Plugin"]:::client
        QuPath["QuPath Plugin"]:::client
        CVATPlugin["CVAT Plugin"]:::client
        DSAPlugin["DSA Plugin"]:::client
        MITK["MITK Plugin"]:::client
    end

    %% Server Layer
    subgraph "MONAI Label Server"
        direction TB
        App["App & Main"]:::server
        API["API Layer\n(endpoints)"]:::server
        Orch["Orchestration Layer\n(interfaces)"]:::server
        Tasks["Task Handlers & Pipelines"]:::server
        ML["ML Engine\n(DeepEdit, SAM2.0, Scribbles)"]:::model
        Utils["Utilities\n(async manager, transforms, sessions)"]:::server
    end

    %% Datastores Layer
    subgraph "Datastores"
        direction TB
        LocalFS["Local FS Datastore"]:::data
        DICOMWeb["DICOMWeb / PACS"]:::data
        ExtAnno["External Annotation Stores\n(CVAT, DSA, XNAT)"]:::data
    end

    %% Model Bundles
    subgraph "Model Bundles"
        direction TB
        Zoo["Pretrained Model Zoo & Bundles"]:::model
    end

    %% External Services
    subgraph "External Services"
        direction TB
        Auth["Auth Provider"]:::external
        DB["Database (Sessions/Logs)"]:::external
    end

    %% Relationships
    CLI -->|"HTTP REST Calls"| API
    OHIF -->|"HTTP REST Calls"| API
    OHIFv3 -->|"HTTP REST Calls"| API
    Slicer -->|"HTTP REST Calls"| API
    QuPath -->|"HTTP REST Calls"| API
    CVATPlugin -->|"HTTP REST Calls"| API
    DSAPlugin -->|"HTTP REST Calls"| API
    MITK -->|"HTTP REST Calls"| API

    API -->|"delegates to"| Orch
    Orch -->|"invokes"| Tasks
    Tasks -->|"load/store images"| LocalFS
    Tasks -->|"load/store images"| DICOMWeb
    Tasks -->|"load/store labels"| ExtAnno
    Tasks -->|"run inference/training"| ML
    ML -->|"writes predictions/metrics"| LocalFS
    ML -->|"writes predictions/metrics"| DICOMWeb
    ML -->|"writes predictions/metrics"| ExtAnno

    Tasks -->|".run_async()"| Utils
    Utils -->|".notify() (dashed for async)"| CLI

    API -->|"proxy DICOMWeb"| DICOMWeb
    API -->|"auth session"| Auth
    API -->|"log data"| DB

    Zoo -->|"provides models to"| ML

    %% Click Events
    click CLI "https://github.com/project-monai/monailabel/blob/main/monailabel/client/client.py"
    click App "https://github.com/project-monai/monailabel/blob/main/monailabel/app.py"
    click App "https://github.com/project-monai/monailabel/blob/main/monailabel/main.py"
    click API "https://github.com/project-monai/monailabel/tree/main/monailabel/endpoints/"
    click Orch "https://github.com/project-monai/monailabel/tree/main/monailabel/interfaces/"
    click Tasks "https://github.com/project-monai/monailabel/tree/main/monailabel/tasks/"
    click ML "https://github.com/project-monai/monailabel/tree/main/monailabel/deepedit/"
    click ML "https://github.com/project-monai/monailabel/tree/main/monailabel/sam2/"
    click ML "https://github.com/project-monai/monailabel/tree/main/monailabel/scribbles/"
    click LocalFS "https://github.com/project-monai/monailabel/tree/main/monailabel/datastore/"
    click LocalFS "https://github.com/project-monai/monailabel/tree/main/monailabel/datastore/utils/"
    click Utils "https://github.com/project-monai/monailabel/tree/main/monailabel/utils/async_tasks/"
    click Utils "https://github.com/project-monai/monailabel/tree/main/monailabel/transform/"
    click Utils "https://github.com/project-monai/monailabel/blob/main/monailabel/utils/sessions.py"
    click Utils "https://github.com/project-monai/monailabel/tree/main/monailabel/utils/others/"
    click OHIF "https://github.com/project-monai/monailabel/tree/main/plugins/ohif/monai-label/src/"
    click OHIFv3 "https://github.com/project-monai/monailabel/tree/main/plugins/ohifv3/extensions/monai-label/src/"
    click Slicer "https://github.com/project-monai/monailabel/tree/main/plugins/slicer/MONAILabel/"
    click QuPath "https://github.com/project-monai/monailabel/tree/main/plugins/qupath/src/main/java/qupath/lib/extension/monailabel/"
    click CVATPlugin "https://github.com/project-monai/monailabel/tree/main/plugins/cvat/"
    click DSAPlugin "https://github.com/project-monai/monailabel/tree/main/plugins/dsa/cli/"
    click MITK "https://github.com/project-monai/monailabel/tree/main/plugins/mitk/"
    click Zoo "https://github.com/project-monai/monailabel/tree/main/sample-apps/"

    %% Styles
    classDef client fill:#B19CD9,stroke:#333,stroke-width:1px
    classDef server fill:#8CBED6,stroke:#333,stroke-width:1px
    classDef data fill:#F4A460,stroke:#333,stroke-width:1px
    classDef model fill:#90EE90,stroke:#333,stroke-width:1px
    classDef external fill:#FF6347,stroke:#333,stroke-width:1px
```
