mlx-segment-ct 0.3.1__tar.gz → 0.3.3__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.

Potentially problematic release.


This version of mlx-segment-ct might be problematic. Click here for more details.

Files changed (25) hide show
  1. {mlx_segment_ct-0.3.1 → mlx_segment_ct-0.3.3}/PKG-INFO +12 -5
  2. {mlx_segment_ct-0.3.1 → mlx_segment_ct-0.3.3}/README.md +11 -4
  3. {mlx_segment_ct-0.3.1 → mlx_segment_ct-0.3.3}/pyproject.toml +1 -1
  4. {mlx_segment_ct-0.3.1 → mlx_segment_ct-0.3.3}/src/mlx_segment_ct/__init__.py +1 -1
  5. {mlx_segment_ct-0.3.1 → mlx_segment_ct-0.3.3}/src/mlx_segment_ct/_types.py +3 -0
  6. {mlx_segment_ct-0.3.1 → mlx_segment_ct-0.3.3}/src/mlx_segment_ct/bundle_transforms.py +21 -9
  7. {mlx_segment_ct-0.3.1 → mlx_segment_ct-0.3.3}/src/mlx_segment_ct/medmlx.py +34 -8
  8. {mlx_segment_ct-0.3.1 → mlx_segment_ct-0.3.3}/.gitignore +0 -0
  9. {mlx_segment_ct-0.3.1 → mlx_segment_ct-0.3.3}/LICENSE +0 -0
  10. {mlx_segment_ct-0.3.1 → mlx_segment_ct-0.3.3}/THIRD_PARTY_NOTICES.md +0 -0
  11. {mlx_segment_ct-0.3.1 → mlx_segment_ct-0.3.3}/src/mlx_segment_ct/activations.py +0 -0
  12. {mlx_segment_ct-0.3.1 → mlx_segment_ct-0.3.3}/src/mlx_segment_ct/correction.py +0 -0
  13. {mlx_segment_ct-0.3.1 → mlx_segment_ct-0.3.3}/src/mlx_segment_ct/correction_inferer.py +0 -0
  14. {mlx_segment_ct-0.3.1 → mlx_segment_ct-0.3.3}/src/mlx_segment_ct/instance_norm.py +0 -0
  15. {mlx_segment_ct-0.3.1 → mlx_segment_ct-0.3.3}/src/mlx_segment_ct/interactive.py +0 -0
  16. {mlx_segment_ct-0.3.1 → mlx_segment_ct-0.3.3}/src/mlx_segment_ct/labels.py +0 -0
  17. {mlx_segment_ct-0.3.1 → mlx_segment_ct-0.3.3}/src/mlx_segment_ct/mlx_network.py +0 -0
  18. {mlx_segment_ct-0.3.1 → mlx_segment_ct-0.3.3}/src/mlx_segment_ct/mlx_point.py +0 -0
  19. {mlx_segment_ct-0.3.1 → mlx_segment_ct-0.3.3}/src/mlx_segment_ct/mlx_point_window.py +0 -0
  20. {mlx_segment_ct-0.3.1 → mlx_segment_ct-0.3.3}/src/mlx_segment_ct/mlx_upsample.py +0 -0
  21. {mlx_segment_ct-0.3.1 → mlx_segment_ct-0.3.3}/src/mlx_segment_ct/mlx_worker.py +0 -0
  22. {mlx_segment_ct-0.3.1 → mlx_segment_ct-0.3.3}/src/mlx_segment_ct/projections.py +0 -0
  23. {mlx_segment_ct-0.3.1 → mlx_segment_ct-0.3.3}/src/mlx_segment_ct/py.typed +0 -0
  24. {mlx_segment_ct-0.3.1 → mlx_segment_ct-0.3.3}/src/mlx_segment_ct/restoration.py +0 -0
  25. {mlx_segment_ct-0.3.1 → mlx_segment_ct-0.3.3}/src/mlx_segment_ct/streaming.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: mlx-segment-ct
3
- Version: 0.3.1
3
+ Version: 0.3.3
4
4
  Summary: Native MLX implementation of NV-Segment-CT (VISTA-3D)
5
5
  Project-URL: Homepage, https://huggingface.co/MedMLX/vista3d-ct-mlx
6
6
  Author: Joseph Sandoval, Alfredo Sandoval Jr.
@@ -29,7 +29,7 @@ Description-Content-Type: text/markdown
29
29
 
30
30
  Segment CT anatomy automatically or with anatomy names and clicks, using NVIDIA VISTA-3D in native MLX.
31
31
 
32
- [CT weights](https://huggingface.co/MedMLX/vista3d-ct-mlx) · [CTMR weights](https://huggingface.co/MedMLX/vista3d-ctmr-mlx)
32
+ [Docs](https://github.com/MedMLX/mlx-segment-ct/blob/main/docs/technical.md) · [CT weights](https://huggingface.co/MedMLX/vista3d-ct-mlx) · [CTMR weights](https://huggingface.co/MedMLX/vista3d-ctmr-mlx) · [MedMLX](https://github.com/MedMLX)
33
33
 
34
34
  | Input | Output |
35
35
  | :--- | :--- |
@@ -37,15 +37,22 @@ Segment CT anatomy automatically or with anatomy names and clicks, using NVIDIA
37
37
 
38
38
  **Apple Silicon · Metal · Python 3.13 / 3.14** · [uv](https://docs.astral.sh/uv/)
39
39
 
40
- ## Quick start
40
+ ## Install
41
41
 
42
42
  ```bash
43
- uv pip install "mlx-segment-ct>=0.3.1"
43
+ uv venv --python 3.13
44
+ source .venv/bin/activate
45
+ uv pip install "mlx-segment-ct>=0.3.2"
44
46
  ```
45
47
 
48
+ NIfTI files explicitly marked in meters or microns are rejected. Inputs with
49
+ unknown spatial units are assumed to use millimeters.
50
+
46
51
  The click coordinates below are examples: replace them with voxels inside your
47
52
  image and its preprocessed foreground crop. `1` includes a point; `0` excludes it.
48
53
 
54
+ ## Run
55
+
49
56
  ```python
50
57
  from mlx_segment_ct.medmlx import run
51
58
 
@@ -76,6 +83,6 @@ weights report `null` unless the folder carries a MedMLX download revision.
76
83
 
77
84
  ## License
78
85
 
79
- Apache-2.0; license and third-party notices are included in the distribution. Model weights retain their publisher terms.
86
+ [Apache-2.0](https://github.com/MedMLX/mlx-segment-ct/blob/main/LICENSE). See [third-party notices](https://github.com/MedMLX/mlx-segment-ct/blob/main/THIRD_PARTY_NOTICES.md). Model weights retain their publisher terms.
80
87
 
81
88
  For research and education. Outputs require human review.
@@ -2,7 +2,7 @@
2
2
 
3
3
  Segment CT anatomy automatically or with anatomy names and clicks, using NVIDIA VISTA-3D in native MLX.
4
4
 
5
- [CT weights](https://huggingface.co/MedMLX/vista3d-ct-mlx) · [CTMR weights](https://huggingface.co/MedMLX/vista3d-ctmr-mlx)
5
+ [Docs](https://github.com/MedMLX/mlx-segment-ct/blob/main/docs/technical.md) · [CT weights](https://huggingface.co/MedMLX/vista3d-ct-mlx) · [CTMR weights](https://huggingface.co/MedMLX/vista3d-ctmr-mlx) · [MedMLX](https://github.com/MedMLX)
6
6
 
7
7
  | Input | Output |
8
8
  | :--- | :--- |
@@ -10,15 +10,22 @@ Segment CT anatomy automatically or with anatomy names and clicks, using NVIDIA
10
10
 
11
11
  **Apple Silicon · Metal · Python 3.13 / 3.14** · [uv](https://docs.astral.sh/uv/)
12
12
 
13
- ## Quick start
13
+ ## Install
14
14
 
15
15
  ```bash
16
- uv pip install "mlx-segment-ct>=0.3.1"
16
+ uv venv --python 3.13
17
+ source .venv/bin/activate
18
+ uv pip install "mlx-segment-ct>=0.3.2"
17
19
  ```
18
20
 
21
+ NIfTI files explicitly marked in meters or microns are rejected. Inputs with
22
+ unknown spatial units are assumed to use millimeters.
23
+
19
24
  The click coordinates below are examples: replace them with voxels inside your
20
25
  image and its preprocessed foreground crop. `1` includes a point; `0` excludes it.
21
26
 
27
+ ## Run
28
+
22
29
  ```python
23
30
  from mlx_segment_ct.medmlx import run
24
31
 
@@ -49,6 +56,6 @@ weights report `null` unless the folder carries a MedMLX download revision.
49
56
 
50
57
  ## License
51
58
 
52
- Apache-2.0; license and third-party notices are included in the distribution. Model weights retain their publisher terms.
59
+ [Apache-2.0](https://github.com/MedMLX/mlx-segment-ct/blob/main/LICENSE). See [third-party notices](https://github.com/MedMLX/mlx-segment-ct/blob/main/THIRD_PARTY_NOTICES.md). Model weights retain their publisher terms.
53
60
 
54
61
  For research and education. Outputs require human review.
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "mlx-segment-ct"
7
- version = "0.3.1"
7
+ version = "0.3.3"
8
8
  description = "Native MLX implementation of NV-Segment-CT (VISTA-3D)"
9
9
  readme = "README.md"
10
10
  classifiers = ["Operating System :: MacOS :: MacOS X"]
@@ -25,7 +25,7 @@ if TYPE_CHECKING:
25
25
  from mlx_segment_ct.mlx_worker import automatic_logits
26
26
  from mlx_segment_ct.streaming import LabelReducer, bounded_labels, rolling_logits
27
27
 
28
- __version__ = "0.3.1"
28
+ __version__ = "0.3.3"
29
29
 
30
30
  _EXPORTS = {
31
31
  "AutomaticModel": "mlx_segment_ct.mlx_network",
@@ -305,6 +305,9 @@ class NiftiHeader(Protocol):
305
305
  def set_zooms(self, zooms: Sequence[float] | HostArray) -> None:
306
306
  """Set voxel spacing in the image header."""
307
307
  ...
308
+ def get_xyzt_units(self) -> tuple[str, str]:
309
+ """Return the declared spatial and temporal units."""
310
+ ...
308
311
  def as_byteswapped(self, endianness: str | None = None) -> NiftiHeader:
309
312
  """Return a header with the requested byte order."""
310
313
  ...
@@ -197,9 +197,10 @@ def load_volume(path: Path) -> tuple[HostArray, DoubleArray]:
197
197
 
198
198
  Accepts a 3D NIfTI-1/2 image or a 4D image with one trailing channel.
199
199
  Returns the float32 (1, X, Y, Z) array and the float64 affine. Header
200
- spatial units are not converted; downstream spacing assumes affine values
201
- are in millimeters. The affine is checked for finiteness and invertibility,
202
- but image voxel values are not checked for finiteness here.
200
+ spatial units must be millimeters or unknown, with unknown assumed to mean
201
+ millimeters. Explicit meter or micron units are rejected rather than rescaled.
202
+ The affine is checked for finiteness and invertibility, but image voxel values
203
+ are not checked for finiteness here.
203
204
  """
204
205
  try:
205
206
  image = nifti_api.load(str(path))
@@ -208,6 +209,15 @@ def load_volume(path: Path) -> tuple[HostArray, DoubleArray]:
208
209
  if not isinstance(image, nib.Nifti1Image | nib.Nifti2Image):
209
210
  raise InvalidInputError(f"NV-Segment-CT requires a NIfTI image: {path}")
210
211
  image = _correct_nifti_header_if_necessary(cast("NiftiImage", image))
212
+ try:
213
+ spatial_units, _ = image.header.get_xyzt_units()
214
+ except KeyError as exc:
215
+ raise InvalidInputError(f"NV-Segment-CT has an invalid NIfTI unit code: {path}") from exc
216
+ if spatial_units not in ("mm", "unknown"):
217
+ raise InvalidInputError(
218
+ f"NV-Segment-CT spatial units are {spatial_units!r}; convert the affine "
219
+ "and header to millimeters before inference"
220
+ )
211
221
  affine = np.array(image.affine, copy=True)
212
222
  header = image.header.as_byteswapped("<") if _swappable(image.header) else image.header
213
223
  dim = header["dim"]
@@ -238,16 +248,18 @@ def preprocess(
238
248
  NV-Segment-CT's fixed range (``"range"``) or NV-Segment-CTMR's percentiles.
239
249
 
240
250
  ``image`` is float32 ``(1, X, Y, Z)`` as ``load_volume`` returns it, and ``affine``
241
- its voxel-to-world matrix (NIfTI RAS, assumed millimeters). Finite values are
242
- scaled to [0, 1] in the usual nondegenerate case (see
243
- ``scale_intensity_percentiles`` for the equal-percentile case); NaNs can
244
- survive preprocessing. The result is on the RAS-oriented 1.5 mm foreground crop.
251
+ its voxel-to-world matrix (NIfTI RAS, assumed millimeters). Voxel values must
252
+ be finite. They are scaled to [0, 1] in the usual nondegenerate case (see
253
+ ``scale_intensity_percentiles`` for the equal-percentile case). The result is
254
+ on the RAS-oriented 1.5 mm foreground crop.
245
255
 
246
256
  Raises:
247
- InvalidInputError: no voxel is above zero after resampling, or an ``extra``
248
- volume does not resample onto the image grid.
257
+ InvalidInputError: image voxels are non-finite, no voxel is above zero after
258
+ resampling, or an ``extra`` volume does not resample onto the image grid.
249
259
  ValueError: unknown ``intensity``.
250
260
  """
261
+ if not np.isfinite(image).all():
262
+ raise InvalidInputError("NV-Segment-CT image contains non-finite voxel intensities")
251
263
  resampled_image, image_affine = _spacing(image, affine, "bilinear")
252
264
  resampled = _Resampled(affine, _shape(image))
253
265
  start, end = _foreground_box(resampled_image)
@@ -125,8 +125,10 @@ def run(
125
125
  interpolates the scores trilinearly at each source voxel and decides its label
126
126
  there, so boundaries follow the source resolution. ``"bundle"`` decides on the
127
127
  1.5 mm grid and restores labels with nearest interpolation, as the NVIDIA bundle does.
128
- Input affine values are assumed to be millimeters; NIfTI spatial-unit codes are
129
- neither validated nor converted. Output NIfTI headers are newly constructed.
128
+ Input voxel intensities must be finite. Spatial units must be millimeters or
129
+ unknown (assumed millimeters); other declared units require conversion before
130
+ inference. Output NIfTI headers are newly constructed and do not retain the
131
+ source unit code.
130
132
 
131
133
  The published ``MedMLX/vista3d-ct-mlx`` bundle supplies ``automatic.safetensors``
132
134
  and ``correction.safetensors`` for ``checkpoint="NV-Segment-CT"``. The
@@ -233,7 +235,7 @@ def run(
233
235
  f"NV-Segment-CT output exists: {destination}; pass overwrite=true to replace it"
234
236
  )
235
237
  weights_root = (
236
- model_weights_dir("vista3d-ct-mlx")
238
+ model_weights_dir("mlx-segment-ct")
237
239
  if weights_path is None else Path(weights_path).expanduser()
238
240
  )
239
241
  weights_dir = _weights_dir(weights_root, correction=point_mode)
@@ -270,13 +272,37 @@ def run_ctmr(
270
272
  """Run the CTMR variant with its own automatic model folder and fixed settings.
271
273
 
272
274
  Omitted weights use ``MedMLX/vista3d-ctmr-mlx``; an explicit folder contains
273
- ``automatic.safetensors`` and, for point inference, ``correction.safetensors``.
274
- Inputs, prompts and output contracts match ``run``. NVIDIA does not support
275
- point segmentation with this checkpoint. CTMR preprocessing and
276
- label rules are fixed internally, so CT weights are never substituted.
275
+ ``automatic.safetensors``. CTMR preprocessing and label rules are fixed
276
+ internally, so CT weights are never substituted. The shared point/correction
277
+ arguments are forwarded to ``run``, but NVIDIA does not support point
278
+ segmentation with this checkpoint. Their presence is not a qualification
279
+ of CTMR point inference.
280
+
281
+ Args:
282
+ image_path: Single-channel CT or MRI NIfTI volume; declared spatial units
283
+ must be millimeters or unknown (assumed millimeters).
284
+ output_dir: Directory for the restored label map.
285
+ weights_path: Optional model folder containing ``automatic.safetensors``;
286
+ omit to reuse the automatic CTMR download.
287
+ segment_all: Request all classes for the selected modality; no prompts also
288
+ selects this mode.
289
+ prompt: Anatomy names instead of numeric class IDs.
290
+ label_prompt: Numeric foreground IDs instead of anatomy names.
291
+ previous_logits_path: Advanced shared correction input; requires point
292
+ prompts, which NVIDIA does not support for CTMR.
293
+ points: Advanced shared source-grid ``(x, y, z)`` point coordinates;
294
+ CTMR point inference is not supported by NVIDIA.
295
+ point_labels: Include/exclude codes paired with ``points``.
296
+ point_start: Index of the first new click for the shared point interface.
297
+ interactive: Require the shared point-prompt mode; see its support limit above.
298
+ overwrite: Replace an existing output; inputs remain protected from aliasing.
299
+ modality: All-classes set: ``CT_BODY``, ``MRI_BODY`` or ``MRI_BRAIN``.
300
+ None selects ``MRI_BODY``. Inapplicable to class or point prompts.
301
+ restoration: ``scores`` interpolates scores before selecting labels;
302
+ ``bundle`` restores labels with the bundle's nearest-neighbor rule.
277
303
  """
278
304
  folder = (
279
- model_weights_dir("vista3d-ctmr-mlx")
305
+ model_weights_dir("mlx-segment-ctmr")
280
306
  if weights_path is None else Path(weights_path).expanduser()
281
307
  )
282
308
  return run(
File without changes