# vendenix/brainmetasegmentatorui-back architecture

> Architecture diagram of the GitHub repository vendenix/brainmetasegmentatorui-back, 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/vendenix/brainmetasegmentatorui-back
- Repository: https://github.com/vendenix/brainmetasegmentatorui-back

## Overview

##### Main Components
- API Layer – Represented by api.py which exposes Flask routes. This layer acts as the main entry point for segmentation requests from the front-end.
- Segmentation Engine – This includes segmentation.py and the UNETR module housed in the unetr/ directory. This module is responsible for launching the deep learning model against incoming data.
- DICOM Integration – The Orthanc directory contains configuration (docker-compose.yml, orthanc.json) and proxy settings (nginx.conf) to integrate with the Orthanc DICOM Web Server. This server retrieves/manages the medical images.
- Database (BDD) – Contains MesuresSQLite.py and mesures.db to track measurements or logs from segmentation requests.
- Utilities & Supporting Modules – The meta and rt_utils directories provide auxiliary functions for data management, image processing, and handling of RT structs.
- Testing & Experimentation – The Testing directory contains notebooks and scripts which, while not part of the production flow, help understand data flow and verify component operations.
- External Dependencies – Docker (via docker-compose) for Orthanc, and PyTorch (as per requirements) for deep learning inference.

##### Relationships and Interactions
- The Front-end (a separate repository) sends segmentation requests to the API.
- The API (api.py) receives requests and may preprocess inputs before invoking the segmentation logic.
- Segmentation logic (segmentation.py) calls the UNETR deep learning model for brain metastasis segmentation. It interfaces with the UNETR modules and utilities.
- The API forwards DICOM image retrieval requests to Orthanc. Orthanc, running in Docker, fetches imaging studies and may also forward data back for anonymization or post-processing.
- Processed segmentation results or logs are stored in the SQLite database (BDD/mesures.db) and possibly further processed using utilities in meta/ and rt_utils/.
- The overall flow is supported by configuration files, environment settings, and deployment scripts which manage the production and development mode (e.g., start_services.bat for Windows).

##### Architectural Patterns and Design Principles
- Separation of Concerns: Clear division between API request handling, model execution, DICOM integration, and database management.
- Modular Design: Functional modules are segregated into their respective directories (e.g., unetr for deep learning, Orthanc for DICOM).
- Containerization: Orthanc and its proxy via Docker are used to encapsulate and deploy the medical image management server.
- Reusability: Utility libraries (in meta/ and rt_utils/) provide reusable functions and helpers that abstract common tasks.

##### Relevant Technologies and Frameworks
- Flask for the API layer (api.py).
- PyTorch/PyTorch Lightning for deep learning (as indicated by the unetr folder and instructions for installing PyTorch).
- Docker for containerizing Orthanc and ensuring consistency in the DICOM server deployment.
- SQLite for lightweight database management.
- Nginx acting as a proxy for Orthanc (see Orthanc/nginx.conf).

##### Final Steps
- Every component is labeled with its purpose (for instance, “Flask API (api.py)”, “Deep Learning Model (UNETR - unetr/)”, “DICOM Server (Orthanc)”).

## Diagram (Mermaid)

```mermaid
flowchart TD
    FE["Front-end (external)"]:::external
    API["Flask API (api.py)"]:::core
    SE["Segmentation Logic (segmentation.py)"]:::core
    DL["Deep Learning Model (UNETR - unetr/)"]:::core
    DCM["DICOM Server (Orthanc)"]:::container
    DB["Database (BDD)"]:::database
    UTM["Meta Utilities (meta/)"]:::support
    UTR["RT Utilities (rt_utils/)"]:::support
    TEST["Testing & Experimentation (Testing/)"]:::support

    %% Data Flow
    FE -->|"HTTP_Request"| API
    API -->|"Segment_Request"| SE
    SE -->|"Call_Model"| DL
    DL -->|"Model_Response"| SE
    API -->|"DICOM_Request"| DCM
    DCM -->|"Imaging_Data"| API
    SE -->|"Log_Data"| DB
    API -->|"Log_Data"| DB

    %% Support relationships
    SE ---|"Uses"| UTM
    SE ---|"Uses"| UTR
    API ---|"Utilizes"| UTM
    API ---|"Utilizes"| UTR

    %% Testing validations
    TEST -->|"Validates"| API
    TEST -->|"Validates"| SE

    %% Click Events
    click API "https://github.com/vendenix/brainmetasegmentatorui-back/blob/main/api.py"
    click SE "https://github.com/vendenix/brainmetasegmentatorui-back/blob/main/segmentation.py"
    click DL "https://github.com/vendenix/brainmetasegmentatorui-back/tree/main/unetr/"
    click DCM "https://github.com/vendenix/brainmetasegmentatorui-back/tree/main/Orthanc/"
    click DB "https://github.com/vendenix/brainmetasegmentatorui-back/tree/main/BDD/"
    click UTM "https://github.com/vendenix/brainmetasegmentatorui-back/tree/main/meta/"
    click UTR "https://github.com/vendenix/brainmetasegmentatorui-back/tree/main/rt_utils/"
    click TEST "https://github.com/vendenix/brainmetasegmentatorui-back/tree/main/Testing/"

    %% Styles
    classDef external fill:#FFEEAD,stroke:#FF9900,stroke-width:2px;
    classDef core fill:#C6EFCE,stroke:#008000,stroke-width:2px;
    classDef container fill:#DDEBF7,stroke:#2F75B5,stroke-width:2px;
    classDef database fill:#FCE4D6,stroke:#D06B53,stroke-width:2px;
    classDef support fill:#E7E6E6,stroke:#000000,stroke-width:2px;
```
