# wasserth/totalsegmentator architecture

> Architecture diagram of the GitHub repository wasserth/totalsegmentator, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/wasserth/totalsegmentator
- Repository: https://github.com/wasserth/totalsegmentator

## Overview

TotalSegmentator is a CT/MR anatomical segmentation and quantitative reporting toolkit. The principal workflow accepts NIfTI or DICOM input, converts and preprocesses it, selects a configured task, runs nnUNet models, postprocesses masks, and writes segmentation outputs and optional statistics. Separate report pipelines derive aorta, pulmonary, and spine measurements. The MCP server exposes task discovery and command execution. Core orchestration and several report internals were inferred from documented paths and sampled excerpts; unsampled wiring is shown conservatively.

Main entry points: [`totalsegmentator/python_api.py`](https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/python_api.py) (Python API), [`totalsegmentator_mcp/mcp_server.py`](https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator_mcp/mcp_server.py) (MCP Server), [`totalsegmentator/bin/TotalSegmentator.py`](https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/bin/TotalSegmentator.py) (CLI Commands), [`totalsegmentator/nnunet.py`](https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/nnunet.py) (nnUNet Runtime)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interface["Interfaces"]
  node_cli["CLI Commands"]
  node_mcp["MCP Server<br/>[mcp_server.py]"]
  node_task_registry["Task Registry<br/>[registry.py]"]
  node_python_api["Python API<br/>[python_api.py]"]
end

subgraph group_preprocess["Input Preparation"]
  node_dicom_io["DICOM Conversion<br/>[dicom_io.py]"]
  node_resampling["Resampling<br/>[resampling.py]"]
  node_cropping["ROI Cropping<br/>[cropping.py]"]
  node_task_config["Task Configuration"]
end

subgraph group_inference["Segmentation Inference"]
  node_nnunet["nnUNet Runtime<br/>[nnunet.py]"]
  node_cnn["Model Runner<br/>[cnn.py]"]
  node_postprocessing["Mask Postprocessing<br/>[postprocessing.py]"]
  node_map_binary["Class Mapping<br/>[map_to_binary.py]"]
end

subgraph group_outputs["Outputs And Stats"]
  node_seg_output["Segmentation Files"]
  node_statistics["Statistics<br/>[statistics.py]"]
  node_body_stats["Body Statistics"]
  node_preview["3D Preview<br/>[preview.py]"]
end

subgraph group_reports["Quantitative Reports"]
  node_aorta_pipeline["Aorta Pipeline<br/>[pipeline.py]"]
  node_aorta_measurements["Aorta Measurements<br/>[measurements.py]"]
  node_aorta_rendering["Aorta Report<br/>[rendering.py]"]
  node_pulmonary_pipeline["Pulmonary Pipeline<br/>[pipeline.py]"]
  node_pulmonary_rendering["Pulmonary Report<br/>[rendering.py]"]
  node_spine_report["Spine Report"]
end

node_user(("User Or System"))
node_external_models[("Model Weights")]

node_user -->|"invokes"| node_cli
node_user -.->|"requests"| node_mcp
node_cli -->|"calls"| node_python_api
node_cli -->|"reads tasks"| node_task_registry
node_mcp -->|"queries"| node_task_registry
node_mcp -->|"runs commands"| node_cli
node_mcp -->|"runs stats"| node_body_stats
node_python_api -->|"converts input"| node_dicom_io
node_python_api -->|"selects task"| node_task_config
node_python_api -->|"resamples image"| node_resampling
node_python_api -->|"crops regions"| node_cropping
node_python_api -->|"runs models"| node_cnn
node_task_config -->|"configures inference"| node_nnunet
node_cnn -->|"invokes runtime"| node_nnunet
node_external_models -.->|"provides weights"| node_nnunet
node_nnunet -->|"returns masks"| node_postprocessing
node_postprocessing -->|"maps classes"| node_map_binary
node_python_api -->|"writes masks"| node_seg_output
node_python_api -.->|"calculates stats"| node_statistics
node_statistics -->|"writes statistics"| node_seg_output
node_body_stats -->|"computes measurements"| node_statistics
node_python_api -.->|"renders preview"| node_preview
node_aorta_pipeline -->|"runs segmentation"| node_cnn
node_aorta_pipeline -->|"measures anatomy"| node_aorta_measurements
node_aorta_measurements -->|"builds report"| node_aorta_rendering
node_pulmonary_pipeline -->|"runs segmentation"| node_cnn
node_pulmonary_pipeline -->|"builds report"| node_pulmonary_rendering
node_spine_report -->|"reads masks"| node_seg_output

click node_cli "https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/bin/TotalSegmentator.py"
click node_mcp "https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator_mcp/mcp_server.py"
click node_task_registry "https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/registry.py"
click node_python_api "https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/python_api.py"
click node_dicom_io "https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/dicom_io.py"
click node_resampling "https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/resampling.py"
click node_cropping "https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/cropping.py"
click node_task_config "https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/map_tasks_config.py"
click node_nnunet "https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/nnunet.py"
click node_cnn "https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/cnn.py"
click node_postprocessing "https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/postprocessing.py"
click node_map_binary "https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/map_to_binary.py"
click node_seg_output "https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/serialization_utils.py"
click node_statistics "https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/statistics.py"
click node_body_stats "https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/bin/totalseg_get_body_stats.py"
click node_preview "https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/preview.py"
click node_aorta_pipeline "https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/aorta_report/pipeline.py"
click node_aorta_measurements "https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/aorta_report/measurements.py"
click node_aorta_rendering "https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/aorta_report/rendering.py"
click node_pulmonary_pipeline "https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/pulmonary_report/pipeline.py"
click node_pulmonary_rendering "https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/pulmonary_report/rendering.py"
click node_spine_report "https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/spine_report/measure_verte_height.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_cli,node_mcp,node_task_registry,node_python_api,node_user toneBlue
class node_dicom_io,node_resampling,node_cropping,node_task_config,node_external_models toneAmber
class node_nnunet,node_cnn,node_postprocessing,node_map_binary toneMint
class node_seg_output,node_statistics,node_body_stats,node_preview toneRose
class node_aorta_pipeline,node_aorta_measurements,node_aorta_rendering,node_pulmonary_pipeline,node_pulmonary_rendering,node_spine_report toneIndigo
```

## Components

### Interfaces

- [CLI Commands](https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/bin/TotalSegmentator.py): `totalsegmentator/bin/TotalSegmentator.py`
- [MCP Server](https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator_mcp/mcp_server.py): `totalsegmentator_mcp/mcp_server.py`
- [Task Registry](https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/registry.py): `totalsegmentator/registry.py`
- [Python API](https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/python_api.py): `totalsegmentator/python_api.py`

### Input Preparation

- [DICOM Conversion](https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/dicom_io.py): `totalsegmentator/dicom_io.py`
- [Resampling](https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/resampling.py): `totalsegmentator/resampling.py`
- [ROI Cropping](https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/cropping.py): `totalsegmentator/cropping.py`
- [Task Configuration](https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/map_tasks_config.py): `totalsegmentator/map_tasks_config.py`

### Segmentation Inference

- [nnUNet Runtime](https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/nnunet.py): `totalsegmentator/nnunet.py`
- [Model Runner](https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/cnn.py): `totalsegmentator/cnn.py`
- [Mask Postprocessing](https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/postprocessing.py): `totalsegmentator/postprocessing.py`
- [Class Mapping](https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/map_to_binary.py): `totalsegmentator/map_to_binary.py`

### Outputs And Stats

- [Segmentation Files](https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/serialization_utils.py): `totalsegmentator/serialization_utils.py`
- [Statistics](https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/statistics.py): `totalsegmentator/statistics.py`
- [Body Statistics](https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/bin/totalseg_get_body_stats.py): `totalsegmentator/bin/totalseg_get_body_stats.py`
- [3D Preview](https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/preview.py): `totalsegmentator/preview.py`

### Quantitative Reports

- [Aorta Pipeline](https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/aorta_report/pipeline.py): `totalsegmentator/aorta_report/pipeline.py`
- [Aorta Measurements](https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/aorta_report/measurements.py): `totalsegmentator/aorta_report/measurements.py`
- [Aorta Report](https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/aorta_report/rendering.py): `totalsegmentator/aorta_report/rendering.py`
- [Pulmonary Pipeline](https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/pulmonary_report/pipeline.py): `totalsegmentator/pulmonary_report/pipeline.py`
- [Pulmonary Report](https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/pulmonary_report/rendering.py): `totalsegmentator/pulmonary_report/rendering.py`
- [Spine Report](https://github.com/wasserth/totalsegmentator/blob/master/totalsegmentator/spine_report/measure_verte_height.py): `totalsegmentator/spine_report/measure_verte_height.py`

### Other components

- **User Or System**
- **Model Weights**

## Connections

- User Or System → CLI Commands: invokes
- User Or System → MCP Server: requests
- CLI Commands → Python API: calls
- CLI Commands → Task Registry: reads tasks
- MCP Server → Task Registry: queries
- MCP Server → CLI Commands: runs commands
- MCP Server → Body Statistics: runs stats
- Python API → DICOM Conversion: converts input
- Python API → Task Configuration: selects task
- Python API → Resampling: resamples image
- Python API → ROI Cropping: crops regions
- Python API → Model Runner: runs models
- Task Configuration → nnUNet Runtime: configures inference
- Model Runner → nnUNet Runtime: invokes runtime
- Model Weights → nnUNet Runtime: provides weights
- nnUNet Runtime → Mask Postprocessing: returns masks
- Mask Postprocessing → Class Mapping: maps classes
- Python API → Segmentation Files: writes masks
- Python API → Statistics: calculates stats
- Statistics → Segmentation Files: writes statistics
- Body Statistics → Statistics: computes measurements
- Python API → 3D Preview: renders preview
- Aorta Pipeline → Model Runner: runs segmentation
- Aorta Pipeline → Aorta Measurements: measures anatomy
- Aorta Measurements → Aorta Report: builds report
- Pulmonary Pipeline → Model Runner: runs segmentation
- Pulmonary Pipeline → Pulmonary Report: builds report
- Spine Report → Segmentation Files: reads masks
