# loli/medpy architecture

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

- Interactive diagram: https://gitdiagram.com/loli/medpy
- Repository: https://github.com/loli/medpy

## Overview

MedPy is a Python library and command-line toolkit for high-dimensional medical-image processing. A typical user loads an image and its spatial metadata, applies image or label operations, then evaluates results or saves them; the scripts also expose focused workflows such as binary resampling. The package additionally provides feature extraction, patch iteration, graph-cut segmentation, and image-similarity metrics. The README establishes the broad product scope, while the sampled source supports only a few specific implementation links, so uncertain cross-module wiring is omitted.

Main entry points: [`bin/medpy_binary_resampling.py`](https://github.com/loli/medpy/blob/master/bin/medpy_binary_resampling.py) (Binary resampling command), [`medpy/io/load.py`](https://github.com/loli/medpy/blob/master/medpy/io/load.py) (Image loading), [`medpy/io/header.py`](https://github.com/loli/medpy/blob/master/medpy/io/header.py) (Spatial metadata), [`medpy/filter/image.py`](https://github.com/loli/medpy/blob/master/medpy/filter/image.py) (Image filters)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_io["Image I/O"]
  node_load["Image loading<br/>[load.py]"]
  node_header["Spatial metadata<br/>[header.py]"]
  node_save["Image saving<br/>[save.py]"]
end

subgraph group_processing["Image Processing"]
  node_filter_image["Image filters<br/>[image.py]"]
  node_filter_binary["Binary filters<br/>[binary.py]"]
  node_filter_label["Label filters<br/>[label.py]"]
  node_filter_smoothing["Smoothing and noise<br/>[smoothing.py]"]
  node_intensity_standardization["Intensity standardization"]
end

subgraph group_analysis["Analysis and Features"]
  node_features["Image features"]
  node_patches["Patch iteration<br/>[patchwise.py]"]
  node_neighbours["Neighbour graphs<br/>[knn.py]"]
  node_metrics["Image metrics"]
end

subgraph group_segmentation["Graph-Cut Segmentation"]
  node_graph_generate["Graph generation<br/>[generate.py]"]
  node_graph_energy["Energy terms<br/>[energy_voxel.py]"]
  node_graph_solve["Graph solver<br/>[graph.py]"]
  node_graph_write["Segmentation output<br/>[write.py]"]
end

subgraph group_tools["Command-Line Tools"]
  node_resample_cli["Binary resampling command"]
end

node_user(("Python / CLI user"))

node_user -->|"loads image"| node_load
node_user -->|"processes image"| node_filter_image
node_user -->|"processes mask"| node_filter_binary
node_user -->|"processes labels"| node_filter_label
node_user -->|"smooths image"| node_filter_smoothing
node_user -->|"standardizes intensities"| node_intensity_standardization
node_user -->|"extracts features"| node_features
node_user -->|"iterates patches"| node_patches
node_user -->|"builds neighbours"| node_neighbours
node_user -->|"evaluates results"| node_metrics
node_user -->|"constructs segmentation graph"| node_graph_generate
node_user -->|"runs command"| node_resample_cli
node_resample_cli -->|"loads input"| node_load
node_resample_cli -->|"updates spacing"| node_header

click node_load "https://github.com/loli/medpy/blob/master/medpy/io/load.py"
click node_header "https://github.com/loli/medpy/blob/master/medpy/io/header.py"
click node_save "https://github.com/loli/medpy/blob/master/medpy/io/save.py"
click node_filter_image "https://github.com/loli/medpy/blob/master/medpy/filter/image.py"
click node_filter_binary "https://github.com/loli/medpy/blob/master/medpy/filter/binary.py"
click node_filter_label "https://github.com/loli/medpy/blob/master/medpy/filter/label.py"
click node_filter_smoothing "https://github.com/loli/medpy/blob/master/medpy/filter/smoothing.py"
click node_intensity_standardization "https://github.com/loli/medpy/blob/master/medpy/filter/IntensityRangeStandardization.py"
click node_features "https://github.com/loli/medpy/tree/master/medpy/features"
click node_patches "https://github.com/loli/medpy/blob/master/medpy/iterators/patchwise.py"
click node_neighbours "https://github.com/loli/medpy/blob/master/medpy/neighbours/knn.py"
click node_metrics "https://github.com/loli/medpy/tree/master/medpy/metric"
click node_graph_generate "https://github.com/loli/medpy/blob/master/medpy/graphcut/generate.py"
click node_graph_energy "https://github.com/loli/medpy/blob/master/medpy/graphcut/energy_voxel.py"
click node_graph_solve "https://github.com/loli/medpy/blob/master/medpy/graphcut/graph.py"
click node_graph_write "https://github.com/loli/medpy/blob/master/medpy/graphcut/write.py"
click node_resample_cli "https://github.com/loli/medpy/blob/master/bin/medpy_binary_resampling.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_load,node_header,node_save,node_user toneBlue
class node_filter_image,node_filter_binary,node_filter_label,node_filter_smoothing,node_intensity_standardization toneAmber
class node_features,node_patches,node_neighbours,node_metrics toneMint
class node_graph_generate,node_graph_energy,node_graph_solve,node_graph_write toneRose
class node_resample_cli toneIndigo
```

## Components

### Image I/O

- [Image loading](https://github.com/loli/medpy/blob/master/medpy/io/load.py): `medpy/io/load.py`
- [Spatial metadata](https://github.com/loli/medpy/blob/master/medpy/io/header.py): `medpy/io/header.py`
- [Image saving](https://github.com/loli/medpy/blob/master/medpy/io/save.py): `medpy/io/save.py`

### Image Processing

- [Image filters](https://github.com/loli/medpy/blob/master/medpy/filter/image.py): `medpy/filter/image.py`
- [Binary filters](https://github.com/loli/medpy/blob/master/medpy/filter/binary.py): `medpy/filter/binary.py`
- [Label filters](https://github.com/loli/medpy/blob/master/medpy/filter/label.py): `medpy/filter/label.py`
- [Smoothing and noise](https://github.com/loli/medpy/blob/master/medpy/filter/smoothing.py): `medpy/filter/smoothing.py`
- [Intensity standardization](https://github.com/loli/medpy/blob/master/medpy/filter/IntensityRangeStandardization.py): `medpy/filter/IntensityRangeStandardization.py`

### Analysis and Features

- [Image features](https://github.com/loli/medpy/tree/master/medpy/features): `medpy/features`
- [Patch iteration](https://github.com/loli/medpy/blob/master/medpy/iterators/patchwise.py): `medpy/iterators/patchwise.py`
- [Neighbour graphs](https://github.com/loli/medpy/blob/master/medpy/neighbours/knn.py): `medpy/neighbours/knn.py`
- [Image metrics](https://github.com/loli/medpy/tree/master/medpy/metric): `medpy/metric`

### Graph-Cut Segmentation

- [Graph generation](https://github.com/loli/medpy/blob/master/medpy/graphcut/generate.py): `medpy/graphcut/generate.py`
- [Energy terms](https://github.com/loli/medpy/blob/master/medpy/graphcut/energy_voxel.py): `medpy/graphcut/energy_voxel.py`
- [Graph solver](https://github.com/loli/medpy/blob/master/medpy/graphcut/graph.py): `medpy/graphcut/graph.py`
- [Segmentation output](https://github.com/loli/medpy/blob/master/medpy/graphcut/write.py): `medpy/graphcut/write.py`

### Command-Line Tools

- [Binary resampling command](https://github.com/loli/medpy/blob/master/bin/medpy_binary_resampling.py): `bin/medpy_binary_resampling.py`

### Other components

- **Python / CLI user**

## Connections

- Python / CLI user → Image loading: loads image
- Python / CLI user → Image filters: processes image
- Python / CLI user → Binary filters: processes mask
- Python / CLI user → Label filters: processes labels
- Python / CLI user → Smoothing and noise: smooths image
- Python / CLI user → Intensity standardization: standardizes intensities
- Python / CLI user → Image features: extracts features
- Python / CLI user → Patch iteration: iterates patches
- Python / CLI user → Neighbour graphs: builds neighbours
- Python / CLI user → Image metrics: evaluates results
- Python / CLI user → Graph generation: constructs segmentation graph
- Python / CLI user → Binary resampling command: runs command
- Binary resampling command → Image loading: loads input
- Binary resampling command → Spatial metadata: updates spacing
