Metadata-Version: 2.5
Name: unest-mlx
Version: 0.4.0
Summary: Segment kidney cortex, medulla and collecting system in contrast-enhanced CT with UNesT and native MLX
Project-URL: Homepage, https://medmlx.org
Project-URL: Documentation, https://github.com/MedMLX/unest-mlx/blob/v0.4.0/README.md
Project-URL: Repository, https://github.com/MedMLX/unest-mlx
Project-URL: Issues, https://github.com/MedMLX/unest-mlx/issues
Project-URL: Weights, https://huggingface.co/MedMLX/unest-renal-ct-mlx
Project-URL: MedMLX, https://github.com/MedMLX/medmlx
Author: Joseph Sandoval, Alfredo Sandoval Jr.
License-Expression: Apache-2.0 AND BSD-3-Clause
License-File: LICENSE
License-File: THIRD_PARTY_NOTICES.md
Keywords: apple-silicon,ct,kidney,medical-imaging,metal,mlx,monai,segmentation,unest
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.4; platform_system == 'Darwin' and platform_machine == 'arm64'
Requires-Dist: nibabel<6,>=5.3
Requires-Dist: numpy<3,>=2.0
Provides-Extra: dev
Requires-Dist: monai==1.6.1; 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.13.0; extra == 'dev'
Description-Content-Type: text/markdown

<div align="center">

# unest-mlx

Segment kidney cortex, medulla and collecting system in contrast-enhanced CT with UNesT and native MLX.

[![PyPI](https://img.shields.io/pypi/v/unest-mlx)](https://pypi.org/project/unest-mlx/)
[![Python](https://img.shields.io/pypi/pyversions/unest-mlx)](https://pypi.org/project/unest-mlx/)
[![License](https://img.shields.io/badge/License-Apache_2.0-blue)](https://github.com/MedMLX/unest-mlx/blob/v0.4.0/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/unest-renal-ct-mlx)
[![MedMLX](https://img.shields.io/badge/MedMLX-runtime-A51C30)](https://github.com/MedMLX/medmlx)

<img src="https://raw.githubusercontent.com/MedMLX/unest-mlx/main/docs/showcase.png" alt="Contrast CT → kidney cortex, medulla and collecting system, with a detailed renal view." width="100%">

</div>

| Input | Output |
| :--- | :--- |
| Contrast-enhanced CT · NIfTI | Renal labels on the input grid |

| Model | Weights download | Peak MLX memory | Measured on |
| :--- | ---: | ---: | :--- |
| `unest` | 349 MB | 7.2 GB | 478×277×161 contrast CT |

## Install

```bash
uv venv --python 3.13
source .venv/bin/activate
uv pip install "unest-mlx>=0.4.0"
```

## Run

```python
from unest_mlx import run

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

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

### Inputs and outputs

| Argument | Meaning |
| --- | --- |
| `image_path` | One 3D CT in NIfTI (`.nii` or `.nii.gz`), in Hounsfield units |
| `output_dir` | Directory for `mask.nii.gz` |
| `weights_path` | Optional folder containing `unest.safetensors` |
| `overwrite` | `False` by default, which refuses to replace an existing mask |
| `sw_batch_size` | Windows per network call, 4 by default; fewer lower peak memory |

- **Input:** arterial or portal-venous contrast-enhanced CT. On the non-contrast scans
  we tried, the kidneys were missed. Spatial units must be millimeters or unspecified;
  meters and microns are not converted.
- **Mask:** float32 labels on the input grid as the reader interprets its header:

  | Label | Structure |
  | ---: | :--- |
  | 0 | Background |
  | 1 | Kidney cortex |
  | 2 | Medulla |
  | 3 | Pelvicalyceal system |

- **Return value:** `{"outputs": {"mask": path}, "artifacts": {"mask": ...}, "details": {...}}`.
  The mask artifact is a label map on the input grid with the four labels above; `details`
  holds label counts, shapes, orientation, sliding-window settings, MLX device and elapsed time.
- **Errors:** invalid inputs raise `InvalidInputError`; missing or mismatched weights
  raise `AssetNotReadyError` with the download location.

### Python API

```python
import mlx.core as mx
import numpy as np
from unest_mlx import UNesT, UNesTPredictor, validate_weights

loaded = mx.load("unest.safetensors")
weights = validate_weights({key: np.array(value) for key, value in loaded.items()})
network = UNesT({key: mx.array(value) for key, value in weights.items()}, mx=mx)
predictor = UNesTPredictor(network, mx=mx)

volume = np.zeros((1, 1, 96, 96, 96), dtype=np.float32)  # preprocessed CT, Bx1xDxHxW
logits = predictor(volume)  # NumPy float32, Bx4xDxHxW
```

`UNesTPredictor` uses the upstream 96³ windows with overlap 0.5 and constant blending;
`sw_batch_size` defaults to four.

## Use with MedMLX

Use the [MedMLX MCP server](https://github.com/MedMLX/medmlx/blob/main/docs/mcp.md) to run `unest` 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 unest`.

> **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/unest-mlx/blob/v0.4.0/LICENSE). See [third-party notices](https://github.com/MedMLX/unest-mlx/blob/v0.4.0/THIRD_PARTY_NOTICES.md). Model weights retain their publisher terms.
