# kushall-07/brain-tumor-segmentation architecture

> Architecture diagram of the GitHub repository kushall-07/brain-tumor-segmentation, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kushall-07/brain-tumor-segmentation
- Repository: https://github.com/kushall-07/brain-tumor-segmentation

## Overview

#### Purpose and entry points
This is a local full-stack MRI brain-tumor segmentation system. Users upload four NIfTI modalities (T1, T1ce, T2, FLAIR); a Python inference backend produces a four-class segmentation and tumor measurements; a React client displays progress, results, and 3D volumes.

Runtime entry points are FastAPI at `api/main.py` (served via Uvicorn) and the Vite/React client at `frontend/src/main.jsx`. Offline training starts at `training/train.py`.

#### API orchestration boundary
The FastAPI layer owns HTTP contracts, validation, job lifecycle, and research metadata. Route handlers delegate work rather than exposing model internals directly.
- `api/main.py`
- `api/routes.py`
- `api/jobs.py`
- `api/schemas.py`
- `api/services.py`

The principal control flow is upload → modality/format validation → asynchronous or tracked prediction job → polling/progress → result and artifact retrieval. API errors are normalized through `api/exceptions.py`.

#### Inference pipeline
Inference accepts the four uploaded NIfTI volumes, loads the designated SwinUNETR checkpoint, applies the model pipeline, postprocesses class labels, and emits prediction artifacts plus derived analysis. The runtime checkpoint contract is under `outputs/exp_swinunetr_4class_et_fixed/checkpoints/best_mean_dice.pt`.
- `inference/predict.py`
- `api/services.py`
- `utils/inference_utils.py`
- `utils/postprocess.py`
- `utils/postprocess_3d.py`

This is the key boundary between request-scoped uploaded data and GPU/CPU PyTorch/MONAI execution. NiBabel, NumPy, and SciPy support NIfTI/image operations; NVIDIA CUDA is recommended but CPU execution is supported.

#### Model and data contracts
The learned model is SwinUNETR, selected through a model factory; a 3D U-Net implementation is also retained. Training and inference share BraTS-oriented multi-modal and label conventions.
- `models/model_factory.py`
- `models/swinunetr.py`
- `models/unet3d.py`
- `datasets/brats_dataset.py`
- `datasets/brats_inference.py`

The segmentation output has background plus NCR/NET, edema, and enhancing-tumor labels. Dataset loading is relevant for optional BraTS training, while runtime inference intentionally requires no local dataset.

#### Analysis, postprocessing, and persisted outputs
Postprocessing and evaluation utilities convert raw model output into clinically oriented regions and measurements, including per-class volume/dimensions and BraTS metrics. Generated artifacts are persisted beneath experiment/output directories rather than a database.
- `utils/segmentation_evaluation.py`
- `utils/brats_metrics.py`
- `utils/analyze_3d_components.py`
- `outputs/exp_swinunetr_4class_et_fixed/predictions/`
- `outputs/exp_swinunetr_4class_et_fixed/metrics/`

Existing experiment folders contain checkpoints, metrics, NIfTI predictions, and HTML visualizations. They are both model-artifact infrastructure and evidence/reproducibility outputs.

#### Frontend workflow and visualization
React Router separates upload/prediction, processing, result, and model-evaluation screens. Frontend service modules call the FastAPI API; local pending-upload state bridges navigation between upload and processing. NiiVue renders NIfTI scans and segmentation overlays interactively.
- `frontend/src/router/AppRouter.jsx`
- `frontend/src/pages/PredictPage.jsx`
- `frontend/src/pages/Processing.jsx`
- `frontend/src/pages/Results.jsx`
- `frontend/src/services/predictionService.js`
- `frontend/src/components/NiiVueViewer.jsx`

The client also presents research-oriented model details, timing, limitations, validation metrics, and optional ground-truth comparisons through `frontend/src/pages/ModelEvaluation.jsx` and `frontend/src/components/research/`.

#### Training and operational environment
Training includes preflight and dry-run stages before full model training, with checkpointing, metrics logging, VRAM checks, and qualitative visualization scripts.
- `training/preflight_check.py`
- `training/dry_run.py`
- `training/train.py`
- `configs/config.py`
- `utils/checkpoint_utils.py`
- `utils/experiment_logger.py`

Material runtimes are Python 3.11, PyTorch/MONAI, FastAPI/Uvicorn, Node.js 18+, React/Vite, and browser-side NiiVue. The architecture is local-process oriented: no cloud service, queue, database, or external API is indicated.

Main entry points: [`training/train.py`](https://github.com/kushall-07/brain-tumor-segmentation/blob/main/training/train.py) (Training pipeline), [`inference/predict.py`](https://github.com/kushall-07/brain-tumor-segmentation/blob/main/inference/predict.py) (Prediction runner), [`frontend/src/router/AppRouter.jsx`](https://github.com/kushall-07/brain-tumor-segmentation/blob/main/frontend/src/router/AppRouter.jsx) (Workflow screens), [`frontend/src/pages/Processing.jsx`](https://github.com/kushall-07/brain-tumor-segmentation/blob/main/frontend/src/pages/Processing.jsx) (Progress and results)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["React client"]
  node_react_entry["Vite React entry<br/>React/Vite<br/>[main.jsx]"]
  node_client_router["Workflow screens<br/>React Router<br/>[AppRouter.jsx]"]
  node_predict_page["Upload and prediction<br/>React page<br/>[PredictPage.jsx]"]
  node_processing_results["Progress and results<br/>React pages<br/>[Processing.jsx]"]
  node_prediction_service["API client<br/>frontend service"]
  node_volume_viewer["NIfTI volume viewer<br/>NiiVue component<br/>[NiiVueViewer.jsx]"]
end

subgraph group_api["API orchestration"]
  node_fastapi_entry["FastAPI application<br/>FastAPI/Uvicorn<br/>[main.py]"]
  node_routes["HTTP routes<br/>API routes<br/>[routes.py]"]
  node_job_lifecycle["Tracked jobs<br/>job lifecycle<br/>[jobs.py]"]
  node_api_service["Prediction service<br/>orchestration service<br/>[services.py]"]
end

subgraph group_inference["Inference pipeline"]
  node_inference_runner["Prediction runner<br/>PyTorch/MONAI inference<br/>[predict.py]"]
  node_model_factory["Model selection<br/>model factory<br/>[model_factory.py]"]
  node_inference_transforms["NIfTI preprocessing<br/>image pipeline<br/>[inference_utils.py]"]
  node_postprocessing["Labels and measurements<br/>postprocessing<br/>[postprocess_3d.py]"]
end

subgraph group_artifacts["Experiment artifacts"]
  node_checkpoint["Best model checkpoint<br/>PyTorch checkpoint<br/>[best_mean_dice.pt]"]
  node_generated_outputs[("Predictions and metrics<br/>persisted artifacts")]
end

subgraph group_training["Training"]
  node_training_entry["Training pipeline<br/>offline training<br/>[train.py]"]
  node_brats_dataset["BraTS dataset contract<br/>training dataset<br/>[brats_dataset.py]"]
  node_training_config["Training configuration<br/>experiment config<br/>[config.py]"]
end

node_react_entry -->|"mounts"| node_client_router
node_client_router -->|"routes to"| node_predict_page
node_client_router -->|"routes to"| node_processing_results
node_predict_page -->|"uploads four modalities"| node_prediction_service
node_processing_results -->|"polls job and fetches results"| node_prediction_service
node_processing_results -->|"displays volumes"| node_volume_viewer
node_prediction_service -->|"HTTP"| node_fastapi_entry
node_fastapi_entry -->|"registers"| node_routes
node_routes -->|"creates and queries"| node_job_lifecycle
node_job_lifecycle -->|"runs tracked prediction"| node_api_service
node_api_service -->|"invokes"| node_inference_runner
node_inference_runner -->|"loads and prepares NIfTI"| node_inference_transforms
node_inference_runner -->|"constructs model"| node_model_factory
node_inference_runner -->|"loads weights"| node_checkpoint
node_inference_runner -->|"raw class output"| node_postprocessing
node_postprocessing -->|"persists predictions and analysis"| node_generated_outputs
node_routes -->|"retrieves artifacts"| node_generated_outputs
node_training_entry -->|"trains on"| node_brats_dataset
node_training_entry -->|"uses"| node_training_config
node_training_entry -->|"trains selected model"| node_model_factory
node_training_entry -->|"writes best checkpoint"| node_checkpoint
node_training_entry -->|"logs metrics and visualizations"| node_generated_outputs

click node_react_entry "https://github.com/kushall-07/brain-tumor-segmentation/blob/main/frontend/src/main.jsx"
click node_client_router "https://github.com/kushall-07/brain-tumor-segmentation/blob/main/frontend/src/router/AppRouter.jsx"
click node_predict_page "https://github.com/kushall-07/brain-tumor-segmentation/blob/main/frontend/src/pages/PredictPage.jsx"
click node_processing_results "https://github.com/kushall-07/brain-tumor-segmentation/blob/main/frontend/src/pages/Processing.jsx"
click node_prediction_service "https://github.com/kushall-07/brain-tumor-segmentation/blob/main/frontend/src/services/predictionService.js"
click node_volume_viewer "https://github.com/kushall-07/brain-tumor-segmentation/blob/main/frontend/src/components/NiiVueViewer.jsx"
click node_fastapi_entry "https://github.com/kushall-07/brain-tumor-segmentation/blob/main/api/main.py"
click node_routes "https://github.com/kushall-07/brain-tumor-segmentation/blob/main/api/routes.py"
click node_job_lifecycle "https://github.com/kushall-07/brain-tumor-segmentation/blob/main/api/jobs.py"
click node_api_service "https://github.com/kushall-07/brain-tumor-segmentation/blob/main/api/services.py"
click node_inference_runner "https://github.com/kushall-07/brain-tumor-segmentation/blob/main/inference/predict.py"
click node_model_factory "https://github.com/kushall-07/brain-tumor-segmentation/blob/main/models/model_factory.py"
click node_inference_transforms "https://github.com/kushall-07/brain-tumor-segmentation/blob/main/utils/inference_utils.py"
click node_postprocessing "https://github.com/kushall-07/brain-tumor-segmentation/blob/main/utils/postprocess_3d.py"
click node_checkpoint "https://github.com/kushall-07/brain-tumor-segmentation/blob/main/outputs/exp_swinunetr_4class_et_fixed/checkpoints/best_mean_dice.pt"
click node_training_entry "https://github.com/kushall-07/brain-tumor-segmentation/blob/main/training/train.py"
click node_brats_dataset "https://github.com/kushall-07/brain-tumor-segmentation/blob/main/datasets/brats_dataset.py"
click node_training_config "https://github.com/kushall-07/brain-tumor-segmentation/blob/main/configs/config.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_react_entry,node_client_router,node_predict_page,node_processing_results,node_prediction_service,node_volume_viewer toneBlue
class node_fastapi_entry,node_routes,node_job_lifecycle,node_api_service toneAmber
class node_inference_runner,node_model_factory,node_inference_transforms,node_postprocessing toneMint
class node_checkpoint,node_generated_outputs toneRose
class node_training_entry,node_brats_dataset,node_training_config toneIndigo
```

## Components

### React client

- [Vite React entry](https://github.com/kushall-07/brain-tumor-segmentation/blob/main/frontend/src/main.jsx): React/Vite · `frontend/src/main.jsx`
- [Workflow screens](https://github.com/kushall-07/brain-tumor-segmentation/blob/main/frontend/src/router/AppRouter.jsx): React Router · Upload, processing, results, and evaluation flows. · `frontend/src/router/AppRouter.jsx`
- [Upload and prediction](https://github.com/kushall-07/brain-tumor-segmentation/blob/main/frontend/src/pages/PredictPage.jsx): React page · `frontend/src/pages/PredictPage.jsx`
- [Progress and results](https://github.com/kushall-07/brain-tumor-segmentation/blob/main/frontend/src/pages/Processing.jsx): React pages · `frontend/src/pages/Processing.jsx`
- [API client](https://github.com/kushall-07/brain-tumor-segmentation/blob/main/frontend/src/services/predictionService.js): frontend service · Submits jobs and polls progress/results. · `frontend/src/services/predictionService.js`
- [NIfTI volume viewer](https://github.com/kushall-07/brain-tumor-segmentation/blob/main/frontend/src/components/NiiVueViewer.jsx): NiiVue component · Renders scans and segmentation overlays. · `frontend/src/components/NiiVueViewer.jsx`

### API orchestration

- [FastAPI application](https://github.com/kushall-07/brain-tumor-segmentation/blob/main/api/main.py): FastAPI/Uvicorn · `api/main.py`
- [HTTP routes](https://github.com/kushall-07/brain-tumor-segmentation/blob/main/api/routes.py): API routes · Upload, job polling, result, and artifact contracts. · `api/routes.py`
- [Tracked jobs](https://github.com/kushall-07/brain-tumor-segmentation/blob/main/api/jobs.py): job lifecycle · Owns asynchronous prediction state and progress. · `api/jobs.py`
- [Prediction service](https://github.com/kushall-07/brain-tumor-segmentation/blob/main/api/services.py): orchestration service · Bridges request-scoped uploads to inference execution. · `api/services.py`

### Inference pipeline

- [Prediction runner](https://github.com/kushall-07/brain-tumor-segmentation/blob/main/inference/predict.py): PyTorch/MONAI inference · Loads four modalities and runs SwinUNETR. · `inference/predict.py`
- [Model selection](https://github.com/kushall-07/brain-tumor-segmentation/blob/main/models/model_factory.py): model factory · Selects SwinUNETR; retains 3D U-Net option. · `models/model_factory.py`
- [NIfTI preprocessing](https://github.com/kushall-07/brain-tumor-segmentation/blob/main/utils/inference_utils.py): image pipeline · Applies shared inference loading and transforms. · `utils/inference_utils.py`
- [Labels and measurements](https://github.com/kushall-07/brain-tumor-segmentation/blob/main/utils/postprocess_3d.py): postprocessing · Derives tumor regions, volumes, and dimensions. · `utils/postprocess_3d.py`

### Experiment artifacts

- [Best model checkpoint](https://github.com/kushall-07/brain-tumor-segmentation/blob/main/outputs/exp_swinunetr_4class_et_fixed/checkpoints/best_mean_dice.pt): PyTorch checkpoint · `outputs/exp_swinunetr_4class_et_fixed/checkpoints/best_mean_dice.pt`
- **Predictions and metrics**: persisted artifacts · NIfTI predictions, measurements, and visualizations.

### Training

- [Training pipeline](https://github.com/kushall-07/brain-tumor-segmentation/blob/main/training/train.py): offline training · Runs preflight, dry-run, training, checkpointing, and logging. · `training/train.py`
- [BraTS dataset contract](https://github.com/kushall-07/brain-tumor-segmentation/blob/main/datasets/brats_dataset.py): training dataset · Defines shared multimodal inputs and four-class labels. · `datasets/brats_dataset.py`
- [Training configuration](https://github.com/kushall-07/brain-tumor-segmentation/blob/main/configs/config.py): experiment config · `configs/config.py`

## Connections

- Vite React entry → Workflow screens: mounts
- Workflow screens → Upload and prediction: routes to
- Workflow screens → Progress and results: routes to
- Upload and prediction → API client: uploads four modalities
- Progress and results → API client: polls job and fetches results
- Progress and results → NIfTI volume viewer: displays volumes
- API client → FastAPI application: HTTP
- FastAPI application → HTTP routes: registers
- HTTP routes → Tracked jobs: creates and queries
- Tracked jobs → Prediction service: runs tracked prediction
- Prediction service → Prediction runner: invokes
- Prediction runner → NIfTI preprocessing: loads and prepares NIfTI
- Prediction runner → Model selection: constructs model
- Prediction runner → Best model checkpoint: loads weights
- Prediction runner → Labels and measurements: raw class output
- Labels and measurements → Predictions and metrics: persists predictions and analysis
- HTTP routes → Predictions and metrics: retrieves artifacts
- Training pipeline → BraTS dataset contract: trains on
- Training pipeline → Training configuration: uses
- Training pipeline → Model selection: trains selected model
- Training pipeline → Best model checkpoint: writes best checkpoint
- Training pipeline → Predictions and metrics: logs metrics and visualizations
