lesiondiffusion-mlx 0.2.5__tar.gz → 0.2.6__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- lesiondiffusion_mlx-0.2.6/.gitignore +38 -0
- lesiondiffusion_mlx-0.2.6/PKG-INFO +183 -0
- lesiondiffusion_mlx-0.2.6/README.md +135 -0
- {lesiondiffusion_mlx-0.2.5 → lesiondiffusion_mlx-0.2.6}/pyproject.toml +35 -6
- {lesiondiffusion_mlx-0.2.5 → lesiondiffusion_mlx-0.2.6}/src/lesiondiffusion_mlx/__init__.py +1 -1
- {lesiondiffusion_mlx-0.2.5 → lesiondiffusion_mlx-0.2.6}/src/lesiondiffusion_mlx/medmlx.py +3 -1
- lesiondiffusion_mlx-0.2.5/.gitignore +0 -26
- lesiondiffusion_mlx-0.2.5/PKG-INFO +0 -80
- lesiondiffusion_mlx-0.2.5/README.md +0 -46
- {lesiondiffusion_mlx-0.2.5 → lesiondiffusion_mlx-0.2.6}/LICENSE +0 -0
- {lesiondiffusion_mlx-0.2.5 → lesiondiffusion_mlx-0.2.6}/THIRD_PARTY_NOTICES.md +0 -0
- {lesiondiffusion_mlx-0.2.5 → lesiondiffusion_mlx-0.2.6}/src/lesiondiffusion_mlx/_arrays.py +0 -0
- {lesiondiffusion_mlx-0.2.5 → lesiondiffusion_mlx-0.2.6}/src/lesiondiffusion_mlx/_attention.py +0 -0
- {lesiondiffusion_mlx-0.2.5 → lesiondiffusion_mlx-0.2.6}/src/lesiondiffusion_mlx/_nax_conv.py +0 -0
- {lesiondiffusion_mlx-0.2.5 → lesiondiffusion_mlx-0.2.6}/src/lesiondiffusion_mlx/_nifti.py +0 -0
- {lesiondiffusion_mlx-0.2.5 → lesiondiffusion_mlx-0.2.6}/src/lesiondiffusion_mlx/_reference.py +0 -0
- {lesiondiffusion_mlx-0.2.5 → lesiondiffusion_mlx-0.2.6}/src/lesiondiffusion_mlx/_schedules.py +0 -0
- {lesiondiffusion_mlx-0.2.5 → lesiondiffusion_mlx-0.2.6}/src/lesiondiffusion_mlx/_source_half.py +0 -0
- {lesiondiffusion_mlx-0.2.5 → lesiondiffusion_mlx-0.2.6}/src/lesiondiffusion_mlx/_spatial.py +0 -0
- {lesiondiffusion_mlx-0.2.5 → lesiondiffusion_mlx-0.2.6}/src/lesiondiffusion_mlx/_typing.py +0 -0
- {lesiondiffusion_mlx-0.2.5 → lesiondiffusion_mlx-0.2.6}/src/lesiondiffusion_mlx/mlx_model.py +0 -0
- {lesiondiffusion_mlx-0.2.5 → lesiondiffusion_mlx-0.2.6}/src/lesiondiffusion_mlx/mlx_pipeline.py +0 -0
- {lesiondiffusion_mlx-0.2.5 → lesiondiffusion_mlx-0.2.6}/src/lesiondiffusion_mlx/mlx_sampling.py +0 -0
- {lesiondiffusion_mlx-0.2.5 → lesiondiffusion_mlx-0.2.6}/src/lesiondiffusion_mlx/py.typed +0 -0
- {lesiondiffusion_mlx-0.2.5 → lesiondiffusion_mlx-0.2.6}/src/lesiondiffusion_mlx/reference.json +0 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# MedMLX standard
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*.egg-info/
|
|
5
|
+
.venv
|
|
6
|
+
build/
|
|
7
|
+
dist/
|
|
8
|
+
.pytest_cache/
|
|
9
|
+
.ruff_cache/
|
|
10
|
+
.mypy_cache/
|
|
11
|
+
.hypothesis/
|
|
12
|
+
.coverage
|
|
13
|
+
htmlcov/
|
|
14
|
+
.DS_Store
|
|
15
|
+
# Never commit secrets (Hugging Face tokens)
|
|
16
|
+
.env
|
|
17
|
+
# Never commit data or weights
|
|
18
|
+
/data/
|
|
19
|
+
/work/
|
|
20
|
+
/weights/
|
|
21
|
+
/models/
|
|
22
|
+
/checkpoints/
|
|
23
|
+
/outputs/
|
|
24
|
+
*.nii
|
|
25
|
+
*.nii.gz
|
|
26
|
+
*.dcm
|
|
27
|
+
*.nrrd
|
|
28
|
+
*.mha
|
|
29
|
+
*.mhd
|
|
30
|
+
*.safetensors
|
|
31
|
+
*.pt
|
|
32
|
+
*.pth
|
|
33
|
+
*.ckpt
|
|
34
|
+
*.bin
|
|
35
|
+
*.h5
|
|
36
|
+
*.npy
|
|
37
|
+
*.npz
|
|
38
|
+
!tests/fixtures/**/*.npz
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: lesiondiffusion-mlx
|
|
3
|
+
Version: 0.2.6
|
|
4
|
+
Summary: Add synthetic lesions to CT with native MLX, with controls for organ, size, placement and phenotype
|
|
5
|
+
Project-URL: Homepage, https://medmlx.org
|
|
6
|
+
Project-URL: Documentation, https://github.com/MedMLX/lesiondiffusion-mlx/blob/v0.2.6/README.md
|
|
7
|
+
Project-URL: Repository, https://github.com/MedMLX/lesiondiffusion-mlx
|
|
8
|
+
Project-URL: Issues, https://github.com/MedMLX/lesiondiffusion-mlx/issues
|
|
9
|
+
Project-URL: Weights, https://huggingface.co/MedMLX/lesiondiffusion-mlx
|
|
10
|
+
Project-URL: MedMLX, https://github.com/MedMLX/medmlx
|
|
11
|
+
Author: Joseph Sandoval, Alfredo Sandoval Jr.
|
|
12
|
+
License-Expression: Apache-2.0 AND MIT AND BSD-3-Clause
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
License-File: THIRD_PARTY_NOTICES.md
|
|
15
|
+
Keywords: apple-silicon,ct,diffusion,image-synthesis,lesion,medical-imaging,metal,mlx
|
|
16
|
+
Classifier: Development Status :: 4 - Beta
|
|
17
|
+
Classifier: Intended Audience :: Developers
|
|
18
|
+
Classifier: Intended Audience :: Science/Research
|
|
19
|
+
Classifier: Operating System :: MacOS :: MacOS X
|
|
20
|
+
Classifier: Programming Language :: Python :: 3
|
|
21
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
24
|
+
Classifier: Topic :: Scientific/Engineering :: Image Processing
|
|
25
|
+
Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
|
|
26
|
+
Requires-Python: <3.14,>=3.13
|
|
27
|
+
Requires-Dist: medmlx-core==0.2.6
|
|
28
|
+
Requires-Dist: mlx==0.32.4; platform_system == 'Darwin' and platform_machine == 'arm64'
|
|
29
|
+
Requires-Dist: nibabel<6,>=5.3
|
|
30
|
+
Requires-Dist: numpy<3,>=2.2
|
|
31
|
+
Requires-Dist: safetensors<1,>=0.6
|
|
32
|
+
Requires-Dist: scipy<2,>=1.15
|
|
33
|
+
Requires-Dist: tokenizers<1,>=0.21
|
|
34
|
+
Provides-Extra: dev
|
|
35
|
+
Requires-Dist: einops<0.9,>=0.8.1; extra == 'dev'
|
|
36
|
+
Requires-Dist: hypothesis<7,>=6.100; extra == 'dev'
|
|
37
|
+
Requires-Dist: pillow<14,>=13; extra == 'dev'
|
|
38
|
+
Requires-Dist: pyright==1.1.414; extra == 'dev'
|
|
39
|
+
Requires-Dist: pytest<10,>=8.4; extra == 'dev'
|
|
40
|
+
Requires-Dist: ruff<1,>=0.13; extra == 'dev'
|
|
41
|
+
Requires-Dist: scipy-stubs<1.19,>=1.18.1.1; extra == 'dev'
|
|
42
|
+
Requires-Dist: torch<2.14,>=2.13; extra == 'dev'
|
|
43
|
+
Provides-Extra: reference
|
|
44
|
+
Requires-Dist: einops==0.8.2; extra == 'reference'
|
|
45
|
+
Requires-Dist: omegaconf==2.3.1; extra == 'reference'
|
|
46
|
+
Requires-Dist: transformers==5.19.0; extra == 'reference'
|
|
47
|
+
Description-Content-Type: text/markdown
|
|
48
|
+
|
|
49
|
+
<div align="center">
|
|
50
|
+
|
|
51
|
+
# lesiondiffusion-mlx
|
|
52
|
+
|
|
53
|
+
Add synthetic lesions to CT with native MLX, with controls for organ, size, placement and phenotype.
|
|
54
|
+
|
|
55
|
+
[](https://pypi.org/project/lesiondiffusion-mlx/)
|
|
56
|
+
[](https://pypi.org/project/lesiondiffusion-mlx/)
|
|
57
|
+
[](https://github.com/MedMLX/lesiondiffusion-mlx/blob/v0.2.6/LICENSE)
|
|
58
|
+
[](https://github.com/ml-explore/mlx)
|
|
59
|
+
[](https://huggingface.co/MedMLX/lesiondiffusion-mlx)
|
|
60
|
+
[](https://github.com/MedMLX/medmlx)
|
|
61
|
+
|
|
62
|
+
[Model weights](https://huggingface.co/MedMLX/lesiondiffusion-mlx) · [MedMLX](https://github.com/MedMLX/medmlx)
|
|
63
|
+
|
|
64
|
+
<img src="https://raw.githubusercontent.com/MedMLX/lesiondiffusion-mlx/v0.2.6/docs/showcase.png" alt="CT with a synthetic lesion placed inside an organ mask" width="100%">
|
|
65
|
+
|
|
66
|
+
</div>
|
|
67
|
+
|
|
68
|
+
| | |
|
|
69
|
+
| :--- | :--- |
|
|
70
|
+
| **Input** | CT + aligned binary organ mask |
|
|
71
|
+
| **Output** | Synthetic CT and lesion mask |
|
|
72
|
+
| **Hardware** | Apple Silicon with Metal; **18.74 GB** peak MLX memory for `lesiondiffusion` in source mode on a 160×160×128, 1 mm RAS crop. |
|
|
73
|
+
|
|
74
|
+
The [v0.2.5 measurement](https://github.com/MedMLX/lesiondiffusion-mlx/blob/v0.2.6/docs/technical.md#upstream-decisions-and-qualification)
|
|
75
|
+
used the AMOS case 0008 crop and 200 diffusion steps with
|
|
76
|
+
`inference_mode="source"`: FP32 LMNet and text encoder, FP16 LINet and VQ.
|
|
77
|
+
It does not measure the default conditioned mode or larger volumes. Peak MLX
|
|
78
|
+
memory excludes host preprocessing and other applications.
|
|
79
|
+
|
|
80
|
+
## Use with MedMLX
|
|
81
|
+
|
|
82
|
+
The [MedMLX MCP server](https://github.com/MedMLX/medmlx/blob/main/docs/mcp.md) is the lowest-friction way to use this model. It installs this runtime, downloads its weights on first use, and lets an AI agent describe, check and run `lesiondiffusion` for you.
|
|
83
|
+
|
|
84
|
+
```sh
|
|
85
|
+
uv tool install --python 3.13 "medmlx[generate,mcp]"
|
|
86
|
+
MEDMLX="$(uv tool dir --bin --color never)/medmlx"
|
|
87
|
+
mkdir -p "$HOME/medmlx-work"
|
|
88
|
+
claude mcp add --scope user medmlx -- "$MEDMLX" serve --root "$HOME/medmlx-work"
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
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 lesiondiffusion`.
|
|
92
|
+
|
|
93
|
+
> **For AI agents:** prefer the MedMLX MCP tools (`list_models`, `describe_model`, `model_readiness`, `run_model`, `get_job`) over writing Python against this package.
|
|
94
|
+
|
|
95
|
+
## Install
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
uv venv --python 3.13
|
|
99
|
+
source .venv/bin/activate
|
|
100
|
+
uv pip install "lesiondiffusion-mlx>=0.2.6"
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Run
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
from lesiondiffusion_mlx.medmlx import run
|
|
107
|
+
|
|
108
|
+
result = run(
|
|
109
|
+
image_path="ct_1mm_ras.nii.gz",
|
|
110
|
+
organ_mask_path="liver_mask.nii.gz",
|
|
111
|
+
output_dir="out",
|
|
112
|
+
organ="liver",
|
|
113
|
+
lesion_type="tumor",
|
|
114
|
+
bbox_size_mm=[30, 30, 20],
|
|
115
|
+
placement="inside_parenchyma",
|
|
116
|
+
)
|
|
117
|
+
print(result["outputs"]["synthetic_image"])
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Omitting `weights_path` downloads `MedMLX/lesiondiffusion-mlx` into the shared MedMLX
|
|
121
|
+
cache on first use; interrupted downloads resume and later runs work offline. With
|
|
122
|
+
MedMLX installed, `medmlx setup lesiondiffusion` downloads it ahead of time. To use your
|
|
123
|
+
own copy, pass a folder containing `model.safetensors` and `tokenizer.json` as
|
|
124
|
+
`weights_path`. Conversion receipts are not required. `details.weights_revision` records
|
|
125
|
+
the downloaded commit, or `null` for a folder without a MedMLX download record.
|
|
126
|
+
|
|
127
|
+
### Inputs and outputs
|
|
128
|
+
|
|
129
|
+
| Argument | Meaning |
|
|
130
|
+
| --- | --- |
|
|
131
|
+
| `image_path` | One 3D CT in NIfTI (`.nii` or `.nii.gz`), in Hounsfield units |
|
|
132
|
+
| `organ_mask_path` | Binary organ mask (background 0, organ 1) on the CT grid |
|
|
133
|
+
| `output_dir` | Directory for the outputs; created if missing |
|
|
134
|
+
| `organ` | A key of `lesiondiffusion_mlx.medmlx.SOURCE_ORGAN_LABELS`, such as `liver` |
|
|
135
|
+
| `lesion_type` | Free text such as `"tumor"` |
|
|
136
|
+
| `bbox_size_mm` | Lesion box extent (X, Y, Z) in mm; required unless `lesion_mask_path` is given |
|
|
137
|
+
| `placement` | `inside_parenchyma`, `protruding_from_parenchyma`, `within_lumen` or `wall_thickening` |
|
|
138
|
+
| `lesion_mask_path` | Optional lesion shape (background 0, lesion 1) to inpaint instead of generating one |
|
|
139
|
+
| `anatomy_mask_path` | Optional 104-label surrounding-organ map that conditions mask generation |
|
|
140
|
+
| `bbox_center_voxel` | Optional `[I, J, K]` box center on the standardized grid; placement is automatic otherwise |
|
|
141
|
+
| `diffusion_steps`, `seed` | Sampling controls; the upstream defaults are 200 and 23 |
|
|
142
|
+
| `inference_mode` | `"conditioned"` (default) or `"source"`; see below |
|
|
143
|
+
| `output_region` | `"full"` (default) or `"box"` |
|
|
144
|
+
| `weights_path` | Optional folder containing `model.safetensors` and `tokenizer.json` |
|
|
145
|
+
| `overwrite` | `False` by default; existing outputs are never replaced |
|
|
146
|
+
|
|
147
|
+
The phenotype text arguments `shape`, `density`, `density_variation`, `enhancement`,
|
|
148
|
+
`surface`, `adjacent_relationship` and `specific_features` default to the upstream
|
|
149
|
+
wording (irregular, hypodense, heterogeneous, non-contrast CT, ill-defined margin).
|
|
150
|
+
They condition the generator but do not guarantee a particular HU contrast or
|
|
151
|
+
appearance.
|
|
152
|
+
|
|
153
|
+
The CT and masks must be finite, axis-aligned grids that match after they are
|
|
154
|
+
reoriented and resampled to RAS 1 mm; oblique or sheared grids are refused. They must
|
|
155
|
+
fit the 64×152×152 mask crop and the 128×128×128 inpainting crop (Z, Y, X); with
|
|
156
|
+
`lesion_mask_path` only the inpainting crop applies. A supplied lesion mask must be a
|
|
157
|
+
single connected component that overlaps the organ mask, so split multifocal
|
|
158
|
+
segmentations first. The box guides generation, but generated lesions can extend past
|
|
159
|
+
the box or organ.
|
|
160
|
+
|
|
161
|
+
The outputs are `synthetic_ct.nii.gz` (float32 HU), `lesion_mask.nii.gz` and
|
|
162
|
+
`organ_mask.nii.gz` (uint8) on the standardized grid. The decoder reconstructs the
|
|
163
|
+
whole CT, so voxels outside the lesion can change. `output_region="box"` keeps the
|
|
164
|
+
generated CT only over the lesion box, feathers it into the input over 6 mm and also
|
|
165
|
+
writes the full reconstruction as `raw_synthetic_ct.nii.gz`. `run` returns
|
|
166
|
+
`{"outputs": {...}, "details": {...}}` with the generation settings, quality checks,
|
|
167
|
+
stage timing and weights revision. Invalid inputs raise `InvalidInputError`; missing
|
|
168
|
+
or mismatched weights raise `AssetNotReadyError`. Synthetic output is not ground truth.
|
|
169
|
+
|
|
170
|
+
### Inference modes
|
|
171
|
+
|
|
172
|
+
`"conditioned"`, the default, enables the inpainting network's trained text context,
|
|
173
|
+
which the pinned upstream configuration turns off, and runs FP16 convolution, fused
|
|
174
|
+
attention and compiled denoisers natively. `"source"` follows the upstream inference
|
|
175
|
+
configuration and arithmetic for reproduction; its results can differ from
|
|
176
|
+
conditioned output. MLX and Torch random streams differ, so a seed repeats results
|
|
177
|
+
only within MLX. Inference needs Apple Silicon with Metal and has no CPU fallback.
|
|
178
|
+
|
|
179
|
+
## License
|
|
180
|
+
|
|
181
|
+
[Apache-2.0](https://github.com/MedMLX/lesiondiffusion-mlx/blob/v0.2.6/LICENSE). See [third-party notices](https://github.com/MedMLX/lesiondiffusion-mlx/blob/v0.2.6/THIRD_PARTY_NOTICES.md). Model weights retain their publisher terms.
|
|
182
|
+
|
|
183
|
+
For research and education. Outputs require human review.
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
# lesiondiffusion-mlx
|
|
4
|
+
|
|
5
|
+
Add synthetic lesions to CT with native MLX, with controls for organ, size, placement and phenotype.
|
|
6
|
+
|
|
7
|
+
[](https://pypi.org/project/lesiondiffusion-mlx/)
|
|
8
|
+
[](https://pypi.org/project/lesiondiffusion-mlx/)
|
|
9
|
+
[](https://github.com/MedMLX/lesiondiffusion-mlx/blob/v0.2.6/LICENSE)
|
|
10
|
+
[](https://github.com/ml-explore/mlx)
|
|
11
|
+
[](https://huggingface.co/MedMLX/lesiondiffusion-mlx)
|
|
12
|
+
[](https://github.com/MedMLX/medmlx)
|
|
13
|
+
|
|
14
|
+
[Model weights](https://huggingface.co/MedMLX/lesiondiffusion-mlx) · [MedMLX](https://github.com/MedMLX/medmlx)
|
|
15
|
+
|
|
16
|
+
<img src="https://raw.githubusercontent.com/MedMLX/lesiondiffusion-mlx/v0.2.6/docs/showcase.png" alt="CT with a synthetic lesion placed inside an organ mask" width="100%">
|
|
17
|
+
|
|
18
|
+
</div>
|
|
19
|
+
|
|
20
|
+
| | |
|
|
21
|
+
| :--- | :--- |
|
|
22
|
+
| **Input** | CT + aligned binary organ mask |
|
|
23
|
+
| **Output** | Synthetic CT and lesion mask |
|
|
24
|
+
| **Hardware** | Apple Silicon with Metal; **18.74 GB** peak MLX memory for `lesiondiffusion` in source mode on a 160×160×128, 1 mm RAS crop. |
|
|
25
|
+
|
|
26
|
+
The [v0.2.5 measurement](https://github.com/MedMLX/lesiondiffusion-mlx/blob/v0.2.6/docs/technical.md#upstream-decisions-and-qualification)
|
|
27
|
+
used the AMOS case 0008 crop and 200 diffusion steps with
|
|
28
|
+
`inference_mode="source"`: FP32 LMNet and text encoder, FP16 LINet and VQ.
|
|
29
|
+
It does not measure the default conditioned mode or larger volumes. Peak MLX
|
|
30
|
+
memory excludes host preprocessing and other applications.
|
|
31
|
+
|
|
32
|
+
## Use with MedMLX
|
|
33
|
+
|
|
34
|
+
The [MedMLX MCP server](https://github.com/MedMLX/medmlx/blob/main/docs/mcp.md) is the lowest-friction way to use this model. It installs this runtime, downloads its weights on first use, and lets an AI agent describe, check and run `lesiondiffusion` for you.
|
|
35
|
+
|
|
36
|
+
```sh
|
|
37
|
+
uv tool install --python 3.13 "medmlx[generate,mcp]"
|
|
38
|
+
MEDMLX="$(uv tool dir --bin --color never)/medmlx"
|
|
39
|
+
mkdir -p "$HOME/medmlx-work"
|
|
40
|
+
claude mcp add --scope user medmlx -- "$MEDMLX" serve --root "$HOME/medmlx-work"
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
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 lesiondiffusion`.
|
|
44
|
+
|
|
45
|
+
> **For AI agents:** prefer the MedMLX MCP tools (`list_models`, `describe_model`, `model_readiness`, `run_model`, `get_job`) over writing Python against this package.
|
|
46
|
+
|
|
47
|
+
## Install
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
uv venv --python 3.13
|
|
51
|
+
source .venv/bin/activate
|
|
52
|
+
uv pip install "lesiondiffusion-mlx>=0.2.6"
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Run
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
from lesiondiffusion_mlx.medmlx import run
|
|
59
|
+
|
|
60
|
+
result = run(
|
|
61
|
+
image_path="ct_1mm_ras.nii.gz",
|
|
62
|
+
organ_mask_path="liver_mask.nii.gz",
|
|
63
|
+
output_dir="out",
|
|
64
|
+
organ="liver",
|
|
65
|
+
lesion_type="tumor",
|
|
66
|
+
bbox_size_mm=[30, 30, 20],
|
|
67
|
+
placement="inside_parenchyma",
|
|
68
|
+
)
|
|
69
|
+
print(result["outputs"]["synthetic_image"])
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Omitting `weights_path` downloads `MedMLX/lesiondiffusion-mlx` into the shared MedMLX
|
|
73
|
+
cache on first use; interrupted downloads resume and later runs work offline. With
|
|
74
|
+
MedMLX installed, `medmlx setup lesiondiffusion` downloads it ahead of time. To use your
|
|
75
|
+
own copy, pass a folder containing `model.safetensors` and `tokenizer.json` as
|
|
76
|
+
`weights_path`. Conversion receipts are not required. `details.weights_revision` records
|
|
77
|
+
the downloaded commit, or `null` for a folder without a MedMLX download record.
|
|
78
|
+
|
|
79
|
+
### Inputs and outputs
|
|
80
|
+
|
|
81
|
+
| Argument | Meaning |
|
|
82
|
+
| --- | --- |
|
|
83
|
+
| `image_path` | One 3D CT in NIfTI (`.nii` or `.nii.gz`), in Hounsfield units |
|
|
84
|
+
| `organ_mask_path` | Binary organ mask (background 0, organ 1) on the CT grid |
|
|
85
|
+
| `output_dir` | Directory for the outputs; created if missing |
|
|
86
|
+
| `organ` | A key of `lesiondiffusion_mlx.medmlx.SOURCE_ORGAN_LABELS`, such as `liver` |
|
|
87
|
+
| `lesion_type` | Free text such as `"tumor"` |
|
|
88
|
+
| `bbox_size_mm` | Lesion box extent (X, Y, Z) in mm; required unless `lesion_mask_path` is given |
|
|
89
|
+
| `placement` | `inside_parenchyma`, `protruding_from_parenchyma`, `within_lumen` or `wall_thickening` |
|
|
90
|
+
| `lesion_mask_path` | Optional lesion shape (background 0, lesion 1) to inpaint instead of generating one |
|
|
91
|
+
| `anatomy_mask_path` | Optional 104-label surrounding-organ map that conditions mask generation |
|
|
92
|
+
| `bbox_center_voxel` | Optional `[I, J, K]` box center on the standardized grid; placement is automatic otherwise |
|
|
93
|
+
| `diffusion_steps`, `seed` | Sampling controls; the upstream defaults are 200 and 23 |
|
|
94
|
+
| `inference_mode` | `"conditioned"` (default) or `"source"`; see below |
|
|
95
|
+
| `output_region` | `"full"` (default) or `"box"` |
|
|
96
|
+
| `weights_path` | Optional folder containing `model.safetensors` and `tokenizer.json` |
|
|
97
|
+
| `overwrite` | `False` by default; existing outputs are never replaced |
|
|
98
|
+
|
|
99
|
+
The phenotype text arguments `shape`, `density`, `density_variation`, `enhancement`,
|
|
100
|
+
`surface`, `adjacent_relationship` and `specific_features` default to the upstream
|
|
101
|
+
wording (irregular, hypodense, heterogeneous, non-contrast CT, ill-defined margin).
|
|
102
|
+
They condition the generator but do not guarantee a particular HU contrast or
|
|
103
|
+
appearance.
|
|
104
|
+
|
|
105
|
+
The CT and masks must be finite, axis-aligned grids that match after they are
|
|
106
|
+
reoriented and resampled to RAS 1 mm; oblique or sheared grids are refused. They must
|
|
107
|
+
fit the 64×152×152 mask crop and the 128×128×128 inpainting crop (Z, Y, X); with
|
|
108
|
+
`lesion_mask_path` only the inpainting crop applies. A supplied lesion mask must be a
|
|
109
|
+
single connected component that overlaps the organ mask, so split multifocal
|
|
110
|
+
segmentations first. The box guides generation, but generated lesions can extend past
|
|
111
|
+
the box or organ.
|
|
112
|
+
|
|
113
|
+
The outputs are `synthetic_ct.nii.gz` (float32 HU), `lesion_mask.nii.gz` and
|
|
114
|
+
`organ_mask.nii.gz` (uint8) on the standardized grid. The decoder reconstructs the
|
|
115
|
+
whole CT, so voxels outside the lesion can change. `output_region="box"` keeps the
|
|
116
|
+
generated CT only over the lesion box, feathers it into the input over 6 mm and also
|
|
117
|
+
writes the full reconstruction as `raw_synthetic_ct.nii.gz`. `run` returns
|
|
118
|
+
`{"outputs": {...}, "details": {...}}` with the generation settings, quality checks,
|
|
119
|
+
stage timing and weights revision. Invalid inputs raise `InvalidInputError`; missing
|
|
120
|
+
or mismatched weights raise `AssetNotReadyError`. Synthetic output is not ground truth.
|
|
121
|
+
|
|
122
|
+
### Inference modes
|
|
123
|
+
|
|
124
|
+
`"conditioned"`, the default, enables the inpainting network's trained text context,
|
|
125
|
+
which the pinned upstream configuration turns off, and runs FP16 convolution, fused
|
|
126
|
+
attention and compiled denoisers natively. `"source"` follows the upstream inference
|
|
127
|
+
configuration and arithmetic for reproduction; its results can differ from
|
|
128
|
+
conditioned output. MLX and Torch random streams differ, so a seed repeats results
|
|
129
|
+
only within MLX. Inference needs Apple Silicon with Metal and has no CPU fallback.
|
|
130
|
+
|
|
131
|
+
## License
|
|
132
|
+
|
|
133
|
+
[Apache-2.0](https://github.com/MedMLX/lesiondiffusion-mlx/blob/v0.2.6/LICENSE). See [third-party notices](https://github.com/MedMLX/lesiondiffusion-mlx/blob/v0.2.6/THIRD_PARTY_NOTICES.md). Model weights retain their publisher terms.
|
|
134
|
+
|
|
135
|
+
For research and education. Outputs require human review.
|
|
@@ -4,16 +4,41 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "lesiondiffusion-mlx"
|
|
7
|
-
version = "0.2.
|
|
8
|
-
description = "
|
|
7
|
+
version = "0.2.6"
|
|
8
|
+
description = "Add synthetic lesions to CT with native MLX, with controls for organ, size, placement and phenotype"
|
|
9
9
|
readme = "README.md"
|
|
10
|
-
requires-python = ">=3.13,<3.
|
|
11
|
-
classifiers = [
|
|
10
|
+
requires-python = ">=3.13,<3.14"
|
|
11
|
+
classifiers = [
|
|
12
|
+
"Development Status :: 4 - Beta",
|
|
13
|
+
"Intended Audience :: Developers",
|
|
14
|
+
"Intended Audience :: Science/Research",
|
|
15
|
+
"Operating System :: MacOS :: MacOS X",
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
18
|
+
"Programming Language :: Python :: 3.13",
|
|
19
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
20
|
+
"Topic :: Scientific/Engineering :: Image Processing",
|
|
21
|
+
"Topic :: Scientific/Engineering :: Medical Science Apps.",
|
|
22
|
+
]
|
|
23
|
+
keywords = [
|
|
24
|
+
"mlx",
|
|
25
|
+
"apple-silicon",
|
|
26
|
+
"metal",
|
|
27
|
+
"medical-imaging",
|
|
28
|
+
"ct",
|
|
29
|
+
"lesion",
|
|
30
|
+
"diffusion",
|
|
31
|
+
"image-synthesis",
|
|
32
|
+
]
|
|
12
33
|
license = "Apache-2.0 AND MIT AND BSD-3-Clause"
|
|
13
34
|
license-files = ["LICENSE", "THIRD_PARTY_NOTICES.md"]
|
|
14
|
-
authors = [
|
|
35
|
+
authors = [
|
|
36
|
+
{ name = "Joseph Sandoval" },
|
|
37
|
+
{ name = "Alfredo Sandoval Jr." },
|
|
38
|
+
]
|
|
15
39
|
dependencies = [
|
|
16
|
-
"medmlx-core==0.2.
|
|
40
|
+
"medmlx-core==0.2.6",
|
|
41
|
+
"mlx==0.32.4; platform_system == 'Darwin' and platform_machine == 'arm64'",
|
|
17
42
|
"nibabel>=5.3,<6",
|
|
18
43
|
"numpy>=2.2,<3",
|
|
19
44
|
"safetensors>=0.6,<1",
|
|
@@ -23,8 +48,11 @@ dependencies = [
|
|
|
23
48
|
|
|
24
49
|
[project.urls]
|
|
25
50
|
Homepage = "https://medmlx.org"
|
|
51
|
+
Documentation = "https://github.com/MedMLX/lesiondiffusion-mlx/blob/v0.2.6/README.md"
|
|
26
52
|
Repository = "https://github.com/MedMLX/lesiondiffusion-mlx"
|
|
53
|
+
Issues = "https://github.com/MedMLX/lesiondiffusion-mlx/issues"
|
|
27
54
|
Weights = "https://huggingface.co/MedMLX/lesiondiffusion-mlx"
|
|
55
|
+
MedMLX = "https://github.com/MedMLX/medmlx"
|
|
28
56
|
|
|
29
57
|
[project.entry-points."medmlx.models"]
|
|
30
58
|
lesiondiffusion = "lesiondiffusion_mlx.medmlx:MODEL"
|
|
@@ -55,6 +83,7 @@ addopts = "--strict-config --strict-markers"
|
|
|
55
83
|
[tool.uv]
|
|
56
84
|
constraint-dependencies = ["setuptools>=84.1.0", "filelock>=4.2.1", "certifi>=2026.10.19"]
|
|
57
85
|
environments = ["sys_platform == 'darwin' and platform_machine == 'arm64'"]
|
|
86
|
+
required-environments = ["sys_platform == 'darwin' and platform_machine == 'arm64'"]
|
|
58
87
|
|
|
59
88
|
[tool.ruff]
|
|
60
89
|
line-length = 100
|
|
@@ -102,7 +102,7 @@ _RAW_SYNTHETIC_CT = "raw_synthetic_ct.nii.gz"
|
|
|
102
102
|
_LESION_MASK = "lesion_mask.nii.gz"
|
|
103
103
|
_ORGAN_MASK = "organ_mask.nii.gz"
|
|
104
104
|
|
|
105
|
-
MODEL: Final[dict[str, str]] = {
|
|
105
|
+
MODEL: Final[dict[str, str | float]] = {
|
|
106
106
|
"id": MODEL_ID,
|
|
107
107
|
"label": "LesionDiffusion CT lesion insertion",
|
|
108
108
|
"task": "generation",
|
|
@@ -111,6 +111,8 @@ MODEL: Final[dict[str, str]] = {
|
|
|
111
111
|
"Insert one phenotype-controlled synthetic lesion into a CT, given a binary organ "
|
|
112
112
|
"mask, or inpaint a supplied binary lesion mask. Outputs use a RAS 1 mm grid."
|
|
113
113
|
),
|
|
114
|
+
# README: source mode, 200 steps, 160x160x128 RAS 1 mm AMOS case 0008 crop.
|
|
115
|
+
"peak_memory_gb": 18.74,
|
|
114
116
|
}
|
|
115
117
|
|
|
116
118
|
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
__pycache__/
|
|
2
|
-
*.py[cod]
|
|
3
|
-
.venv/
|
|
4
|
-
*.egg-info/
|
|
5
|
-
dist/
|
|
6
|
-
build/
|
|
7
|
-
.pytest_cache/
|
|
8
|
-
.ruff_cache/
|
|
9
|
-
|
|
10
|
-
# Checkpoints and converted weights are never committed.
|
|
11
|
-
checkpoints/
|
|
12
|
-
weights/
|
|
13
|
-
*.safetensors
|
|
14
|
-
*.npz
|
|
15
|
-
!tests/fixtures/*.npz
|
|
16
|
-
!tests/fixtures/*/*.npz
|
|
17
|
-
*.pt
|
|
18
|
-
*.pth
|
|
19
|
-
*.ckpt
|
|
20
|
-
|
|
21
|
-
# Imaging data is never committed.
|
|
22
|
-
data/
|
|
23
|
-
*.nii
|
|
24
|
-
*.nii.gz
|
|
25
|
-
*.dcm
|
|
26
|
-
.DS_Store
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.5
|
|
2
|
-
Name: lesiondiffusion-mlx
|
|
3
|
-
Version: 0.2.5
|
|
4
|
-
Summary: Native MLX implementation of LesionDiffusion
|
|
5
|
-
Project-URL: Homepage, https://medmlx.org
|
|
6
|
-
Project-URL: Repository, https://github.com/MedMLX/lesiondiffusion-mlx
|
|
7
|
-
Project-URL: Weights, https://huggingface.co/MedMLX/lesiondiffusion-mlx
|
|
8
|
-
Author: Joseph Sandoval, Alfredo Sandoval Jr.
|
|
9
|
-
License-Expression: Apache-2.0 AND MIT AND BSD-3-Clause
|
|
10
|
-
License-File: LICENSE
|
|
11
|
-
License-File: THIRD_PARTY_NOTICES.md
|
|
12
|
-
Classifier: Operating System :: MacOS :: MacOS X
|
|
13
|
-
Requires-Python: <3.15,>=3.13
|
|
14
|
-
Requires-Dist: medmlx-core==0.2.5
|
|
15
|
-
Requires-Dist: nibabel<6,>=5.3
|
|
16
|
-
Requires-Dist: numpy<3,>=2.2
|
|
17
|
-
Requires-Dist: safetensors<1,>=0.6
|
|
18
|
-
Requires-Dist: scipy<2,>=1.15
|
|
19
|
-
Requires-Dist: tokenizers<1,>=0.21
|
|
20
|
-
Provides-Extra: dev
|
|
21
|
-
Requires-Dist: einops<0.9,>=0.8.1; extra == 'dev'
|
|
22
|
-
Requires-Dist: hypothesis<7,>=6.100; extra == 'dev'
|
|
23
|
-
Requires-Dist: pillow<14,>=13; extra == 'dev'
|
|
24
|
-
Requires-Dist: pyright==1.1.414; extra == 'dev'
|
|
25
|
-
Requires-Dist: pytest<10,>=8.4; extra == 'dev'
|
|
26
|
-
Requires-Dist: ruff<1,>=0.13; extra == 'dev'
|
|
27
|
-
Requires-Dist: scipy-stubs<1.19,>=1.18.1.1; extra == 'dev'
|
|
28
|
-
Requires-Dist: torch<2.14,>=2.13; extra == 'dev'
|
|
29
|
-
Provides-Extra: reference
|
|
30
|
-
Requires-Dist: einops==0.8.2; extra == 'reference'
|
|
31
|
-
Requires-Dist: omegaconf==2.3.1; extra == 'reference'
|
|
32
|
-
Requires-Dist: transformers==5.19.0; extra == 'reference'
|
|
33
|
-
Description-Content-Type: text/markdown
|
|
34
|
-
|
|
35
|
-
# lesiondiffusion-mlx
|
|
36
|
-
|
|
37
|
-
Add synthetic lesions to CT with native MLX, with controls for organ, size, placement and phenotype.
|
|
38
|
-
|
|
39
|
-
[Model weights](https://huggingface.co/MedMLX/lesiondiffusion-mlx)
|
|
40
|
-
|
|
41
|
-
| Input | Output |
|
|
42
|
-
| :--- | :--- |
|
|
43
|
-
| CT + aligned binary organ mask | Synthetic CT and lesion mask |
|
|
44
|
-
|
|
45
|
-
**Apple Silicon · Metal · Python 3.13 / 3.14** · [uv](https://docs.astral.sh/uv/)
|
|
46
|
-
|
|
47
|
-
## Quick start
|
|
48
|
-
|
|
49
|
-
```bash
|
|
50
|
-
uv venv --python 3.13
|
|
51
|
-
source .venv/bin/activate
|
|
52
|
-
uv pip install "lesiondiffusion-mlx>=0.2.5"
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
```python
|
|
56
|
-
from lesiondiffusion_mlx.medmlx import run
|
|
57
|
-
|
|
58
|
-
result = run(
|
|
59
|
-
image_path="ct_1mm_ras.nii.gz",
|
|
60
|
-
organ_mask_path="liver_mask.nii.gz",
|
|
61
|
-
output_dir="out",
|
|
62
|
-
organ="liver",
|
|
63
|
-
lesion_type="tumor",
|
|
64
|
-
bbox_size_mm=[30, 30, 20],
|
|
65
|
-
placement="inside_parenchyma",
|
|
66
|
-
)
|
|
67
|
-
print(result["outputs"]["synthetic_image"])
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
Weights download from `MedMLX/lesiondiffusion-mlx` on first use. A local native
|
|
71
|
-
folder can also be passed as `weights_path`; it needs `model.safetensors` and
|
|
72
|
-
`tokenizer.json`. Conversion receipts are not required.
|
|
73
|
-
|
|
74
|
-
Use `lesion_mask_path` instead of `bbox_size_mm` to inpaint a supplied lesion shape. Inputs must support the documented crop sizes. Outputs use a RAS 1 mm grid; voxels outside the lesion can change. Synthetic output is not ground truth.
|
|
75
|
-
|
|
76
|
-
## License
|
|
77
|
-
|
|
78
|
-
Apache-2.0; license and third-party notices are included in the distribution. Model weights retain their publisher terms.
|
|
79
|
-
|
|
80
|
-
For research and education. Outputs require human review.
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
# lesiondiffusion-mlx
|
|
2
|
-
|
|
3
|
-
Add synthetic lesions to CT with native MLX, with controls for organ, size, placement and phenotype.
|
|
4
|
-
|
|
5
|
-
[Model weights](https://huggingface.co/MedMLX/lesiondiffusion-mlx)
|
|
6
|
-
|
|
7
|
-
| Input | Output |
|
|
8
|
-
| :--- | :--- |
|
|
9
|
-
| CT + aligned binary organ mask | Synthetic CT and lesion mask |
|
|
10
|
-
|
|
11
|
-
**Apple Silicon · Metal · Python 3.13 / 3.14** · [uv](https://docs.astral.sh/uv/)
|
|
12
|
-
|
|
13
|
-
## Quick start
|
|
14
|
-
|
|
15
|
-
```bash
|
|
16
|
-
uv venv --python 3.13
|
|
17
|
-
source .venv/bin/activate
|
|
18
|
-
uv pip install "lesiondiffusion-mlx>=0.2.5"
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
```python
|
|
22
|
-
from lesiondiffusion_mlx.medmlx import run
|
|
23
|
-
|
|
24
|
-
result = run(
|
|
25
|
-
image_path="ct_1mm_ras.nii.gz",
|
|
26
|
-
organ_mask_path="liver_mask.nii.gz",
|
|
27
|
-
output_dir="out",
|
|
28
|
-
organ="liver",
|
|
29
|
-
lesion_type="tumor",
|
|
30
|
-
bbox_size_mm=[30, 30, 20],
|
|
31
|
-
placement="inside_parenchyma",
|
|
32
|
-
)
|
|
33
|
-
print(result["outputs"]["synthetic_image"])
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
Weights download from `MedMLX/lesiondiffusion-mlx` on first use. A local native
|
|
37
|
-
folder can also be passed as `weights_path`; it needs `model.safetensors` and
|
|
38
|
-
`tokenizer.json`. Conversion receipts are not required.
|
|
39
|
-
|
|
40
|
-
Use `lesion_mask_path` instead of `bbox_size_mm` to inpaint a supplied lesion shape. Inputs must support the documented crop sizes. Outputs use a RAS 1 mm grid; voxels outside the lesion can change. Synthetic output is not ground truth.
|
|
41
|
-
|
|
42
|
-
## License
|
|
43
|
-
|
|
44
|
-
Apache-2.0; license and third-party notices are included in the distribution. Model weights retain their publisher terms.
|
|
45
|
-
|
|
46
|
-
For research and education. Outputs require human review.
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{lesiondiffusion_mlx-0.2.5 → lesiondiffusion_mlx-0.2.6}/src/lesiondiffusion_mlx/_attention.py
RENAMED
|
File without changes
|
{lesiondiffusion_mlx-0.2.5 → lesiondiffusion_mlx-0.2.6}/src/lesiondiffusion_mlx/_nax_conv.py
RENAMED
|
File without changes
|
|
File without changes
|
{lesiondiffusion_mlx-0.2.5 → lesiondiffusion_mlx-0.2.6}/src/lesiondiffusion_mlx/_reference.py
RENAMED
|
File without changes
|
{lesiondiffusion_mlx-0.2.5 → lesiondiffusion_mlx-0.2.6}/src/lesiondiffusion_mlx/_schedules.py
RENAMED
|
File without changes
|
{lesiondiffusion_mlx-0.2.5 → lesiondiffusion_mlx-0.2.6}/src/lesiondiffusion_mlx/_source_half.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{lesiondiffusion_mlx-0.2.5 → lesiondiffusion_mlx-0.2.6}/src/lesiondiffusion_mlx/mlx_model.py
RENAMED
|
File without changes
|
{lesiondiffusion_mlx-0.2.5 → lesiondiffusion_mlx-0.2.6}/src/lesiondiffusion_mlx/mlx_pipeline.py
RENAMED
|
File without changes
|
{lesiondiffusion_mlx-0.2.5 → lesiondiffusion_mlx-0.2.6}/src/lesiondiffusion_mlx/mlx_sampling.py
RENAMED
|
File without changes
|
|
File without changes
|
{lesiondiffusion_mlx-0.2.5 → lesiondiffusion_mlx-0.2.6}/src/lesiondiffusion_mlx/reference.json
RENAMED
|
File without changes
|