Metadata-Version: 2.5
Name: swin-unetr-mlx
Version: 0.4.1
Summary: Segment 13 abdominal organs in CT with MONAI SwinUNETR and native MLX
Project-URL: Homepage, https://medmlx.org
Project-URL: Documentation, https://github.com/MedMLX/swin-unetr-mlx/blob/v0.4.1/README.md
Project-URL: Repository, https://github.com/MedMLX/swin-unetr-mlx
Project-URL: Issues, https://github.com/MedMLX/swin-unetr-mlx/issues
Project-URL: Weights, https://huggingface.co/MedMLX/swin-unetr-mlx
Project-URL: MedMLX, https://github.com/MedMLX/medmlx
Author: Joseph Sandoval, Alfredo Sandoval Jr.
License-Expression: Apache-2.0
License-File: LICENSE
License-File: THIRD_PARTY_NOTICES.md
Keywords: abdomen,apple-silicon,ct,medical-imaging,metal,mlx,monai,segmentation,swin-unetr
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: MacOS :: MacOS X
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Image Processing
Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
Requires-Python: <3.14,>=3.13
Requires-Dist: medmlx-core[nifti]<0.4,>=0.3.2
Requires-Dist: mlx==0.32.5; platform_system == 'Darwin' and platform_machine == 'arm64'
Requires-Dist: numpy<3,>=2.0
Requires-Dist: simpleitk<3,>=2.4
Provides-Extra: dev
Requires-Dist: pyright==1.1.414; extra == 'dev'
Requires-Dist: pytest<10,>=8.4; extra == 'dev'
Requires-Dist: ruff<1,>=0.13; extra == 'dev'
Requires-Dist: torch<2.14,>=2.13; extra == 'dev'
Description-Content-Type: text/markdown

<div align="center">

# swin-unetr-mlx

Segment 13 abdominal organs in CT with MONAI SwinUNETR and native MLX.

[![PyPI](https://img.shields.io/pypi/v/swin-unetr-mlx)](https://pypi.org/project/swin-unetr-mlx/)
[![Python](https://img.shields.io/pypi/pyversions/swin-unetr-mlx)](https://pypi.org/project/swin-unetr-mlx/)
[![License](https://img.shields.io/badge/License-Apache_2.0-blue)](https://github.com/MedMLX/swin-unetr-mlx/blob/v0.4.1/LICENSE)
[![Apple Silicon](https://img.shields.io/badge/Apple_Silicon-MLX-black?logo=apple)](https://github.com/ml-explore/mlx)
[![Hugging Face](https://img.shields.io/badge/%F0%9F%A4%97_Hugging_Face-weights-yellow)](https://huggingface.co/MedMLX/swin-unetr-mlx)
[![MedMLX](https://img.shields.io/badge/MedMLX-runtime-A51C30)](https://github.com/MedMLX/medmlx)

<img src="https://raw.githubusercontent.com/MedMLX/swin-unetr-mlx/main/docs/showcase.gif" alt="Axial CT slices scrolling with their organ segmentation beside a rotating 3D rendering of the segmented anatomy" width="100%">

</div>

| Input | Output |
| :--- | :--- |
| 3D abdominal CT · NIfTI | Organ mask on the input grid |

| Model | Weights download | Peak MLX memory | Measured on |
| :--- | ---: | ---: | :--- |
| `swin-unetr` | 253 MB | 7.4 GB | 768×768×101 abdominal CT |

## Install

```bash
uv venv --python 3.13
source .venv/bin/activate
uv pip install "swin-unetr-mlx>=0.4.1"
```

## Run

```python
from swin_unetr_mlx import run

result = run(image_path="ct.nii.gz", output_dir="out")
print(result["outputs"]["mask"])
```

Weights download from `MedMLX/swin-unetr-mlx` on first use; `medmlx setup swin-unetr`
downloads them ahead of time. For local weights, pass the folder containing
`swinunetr.safetensors` as `weights_path`.

### Inputs

| Argument | Meaning |
| --- | --- |
| `image_path` | One 3D abdominal CT in NIfTI-1 or NIfTI-2, in Hounsfield units, with an invertible affine |
| `output_dir` | Directory for `mask.nii.gz`, created if needed |
| `weights_path` | Optional folder containing `swinunetr.safetensors` |
| `overwrite` | `False` by default; an existing mask is replaced only when `True` |
| `sw_batch_size` | Windows per network call, 4 by default |
| `streaming` | `False` by default; `True` blends into temporary memory-mapped files instead of holding the logits in memory |
| `scratch_dir` | Parent folder for the streaming maps; the system temporary folder by default |

Voxels must be finite and representable in float32. Meter and micron headers are
converted to millimeters, and unspecified units are read as millimeters. The image is
rejected when its qform and sform headers make ITK and NiBabel disagree on the affine.

### Outputs

The mask is `out/mask.nii.gz`, with the input shape and the ITK reader's affine. It holds
float32 label indices from the bundled
[BTCV label map](https://github.com/MedMLX/swin-unetr-mlx/blob/v0.4.1/src/swin_unetr_mlx/specification.json):

| Index | Name | Index | Name |
| ---: | :--- | ---: | :--- |
| 0 | background | 7 | stomach |
| 1 | spleen | 8 | aorta |
| 2 | right kidney | 9 | inferior vena cava |
| 3 | left kidney | 10 | portal and splenic veins |
| 4 | gallbladder | 11 | pancreas |
| 5 | esophagus | 12 | right adrenal gland |
| 6 | liver | 13 | left adrenal gland |

`run` returns `{"outputs": {"mask": path}, "artifacts": {"mask": ...}, "details": {...}}`. The
mask artifact is a label map on the input grid with the 14 classes above; `details` holds the
labels present, shapes, sliding-window settings, MLX device, peak memory and timings.

| Error | Raised when |
| :--- | :--- |
| `InvalidInputError` | The input is invalid |
| `AssetNotReadyError` | Weights are missing or mismatched |
| `ModelExecutionError` | Inference fails |
| `MissingDependencyError` | The host is unsupported or Metal is unavailable |

### Python API

```python
from pathlib import Path

import numpy as np
from swin_unetr_mlx import forward_patch, load_network

network = load_network(Path("swinunetr.safetensors"))
logits = forward_patch(network, np.zeros((1, 1, 96, 96, 96), dtype=np.float32))
```

- `forward_patch` takes finite float32 Bx1x96x96x96 windows and returns raw
  Bx14x96x96x96 logits, without file IO, intensity scaling, resampling, softmax or
  stitching. `SwinUNETR(weights, mx=mx)` runs on MLX arrays directly.
- Both default to `merging_profile="monai-1.4"`, the released bundle's repeated
  patch-merging phases; pass `merging_profile="monai-1.6"` to compare with MONAI 1.6.
- `load_network` checks names, shapes, dtypes, finite values and position-index ranges,
  and raises `AssetNotReadyError` when the weights do not match.

## Use with MedMLX

Use the [MedMLX MCP server](https://github.com/MedMLX/medmlx/blob/main/docs/mcp.md) to run `swin-unetr` from an MCP client. Weights download on first use.

```sh
uv tool install --python 3.13 "medmlx[segment,mcp]"
MEDMLX="$(uv tool dir --bin --color never)/medmlx"
mkdir -p "$HOME/medmlx-work"
claude mcp add --scope user medmlx -- "$MEDMLX" serve --root "$HOME/medmlx-work"
```

For Codex, use `codex mcp add medmlx -- "$MEDMLX" serve --root "$HOME/medmlx-work"`. Other MCP clients take the same command and arguments. The same install provides the `medmlx` CLI, for example `medmlx describe swin-unetr`.

> **For AI agents:** prefer the MedMLX MCP tools (`list_models`, `describe_model`, `model_readiness`, `run_model`, `get_job`) over writing Python against this package.

## License

[Apache-2.0](https://github.com/MedMLX/swin-unetr-mlx/blob/v0.4.1/LICENSE). See [third-party notices](https://github.com/MedMLX/swin-unetr-mlx/blob/v0.4.1/THIRD_PARTY_NOTICES.md). Model weights retain their publisher terms.
