Skip to main content

flexray-mlx

Segment anatomical structures in radiographs with FleXray and native MLX.

PyPI Python License Apple Silicon Hugging Face MedMLX

Chest radiograph with lung, heart and individual rib segmentations
Input Output
Raster image or single-frame DICOM Structure masks and probabilities
Model Weights download Peak MLX memory Measured on
flexray 2.0 GB for all five ensemble members 1.2 GB 1929×2207 chest radiograph, default single model

Install

uv venv --python 3.13
source .venv/bin/activate
uv pip install "flexray-mlx>=0.3.1"

Run

from flexray_mlx import run

result = run(image_path="chest.png", output_dir="out")
print(result["outputs"]["mask"])

Use label_name to select a structure.

ensemble Weights
False (default) The flagship flux0375. It downloads from MedMLX/flexray-mlx on first use; medmlx setup flexray downloads it ahead of time
True All five members, in an explicit weights_path folder

A local folder holds ensemble.json and members/flux0375/{config.yml,label_schema.json,model.safetensors,preprocessing.json}.

Inputs and outputs

Argument Meaning
image_path One radiograph: an image Pillow reads (PNG, TIFF, ...) or a single-frame 2D DICOM
output_dir Output directory, created if missing
weights_path Optional bundle folder, as above
label_name Structure to threshold into the mask, lungs by default (any label, such as heart, rib_5 or vertebra_l1)
ensemble False by default; True averages all five released members
threshold Probability cutoff in [0, 1], 0.5 by default
tta_samples Passes per member, 1 by default: one plain view plus augmented views
tta_seed None by default, unseeded as in FleXray; an integer reproduces Torch CPU augmentation draws
normalization_scheme None keeps the bundle's; or none, minmax, percentile_minmax or standardize
overwrite False by default; existing outputs are replaced only when True
member_residency all by default; one loads ensemble members one at a time, spooling per-view probabilities to a temporary folder, with identical results
  • DICOM: files end in .dcm or .dicom (any case), or have no suffix and DICM at byte 128; other suffixes, such as .ima, are read as raster images. Accepting a file does not mean the model suits its modality.
  • Output paths must differ from each other, the input and the bundle files, including links, even with overwrite.
Output File Content
mask source_image_label.png uint8 0/1 mask of the selected structure on the source pixel grid
preview source_image.png 8-bit grayscale source the mask overlays
probabilities probabilities.npy float32 61×256×256 sigmoid probabilities on the padded model grid
metadata prediction.json Labels, selected channel, threshold, members and content box
  • Source-grid PNGs: FleXray thresholds on the model grid and exports no source-grid images; the PNGs are a MedMLX addition. The mask is thresholded first, then resized with nearest neighbor to the padded canvas and unpadded, which inverts the preprocessing geometry, so no new decisions come from interpolation.
  • Return value: {"outputs": {...}, "artifacts": {...}, "details": {...}}. artifacts marks the mask as a binary mask named for label_name on the input's pixel grid, and the probabilities as a field on the model grid; run metadata is in details.
Error Raised when
AssetNotReadyError Assets are missing or incompatible
InvalidInputError Inputs are invalid or outputs already exist
ModelExecutionError Probabilities are non-finite; raised before anything is written

Ensemble members. The published bundle holds only flux0375; its ensemble.json still lists all five members in order (flux0375, flux000, flux025, flux050, flux075). For ensemble=True, use the upstream snapshot with all five member folders.

Augmentation and normalization. Augmented views follow FleXray's tta_v3 chain: flips, inversion, CLAHE or gamma, contrast, sharpness and Gaussian noise. Gamma on negative normalized pixels can produce NaN, which the runner rejects. The released weights were trained with percentile normalization; other schemes are not validated.

Python API

from pathlib import Path

import numpy as np
from flexray_mlx import FleXrayMlxModel

model = FleXrayMlxModel(Path("flexray/members/flux0375"))
logits = model(np.zeros((1, 1, 256, 256), dtype=np.float32))  # Bx1x256x256 -> Bx61x256x256
  • The member folder needs the released config.yml and model.safetensors. Loading converts convolution weights to MLX layout and every tensor to float32, keeping the names, and rejects tensor names or shapes that differ from the released network.
  • Inputs are prepared images; preparation, sigmoid, ensembling, TTA and source-grid restoration are up to you.
  • flexray_mlx.tta.predict_with_tta(forward_fn, images, tta_samples=..., seed=..., normalizer=...) takes one or more forward callables and already normalized float32 BxCxHxW images, and returns (logits, probabilities). Pass the normalize method of flexray_mlx.medmlx.ImagePreprocessing as normalizer to follow the bundle. flexray_mlx.tta.tta_probabilities takes the same arguments and returns only the probabilities.

Use with MedMLX

Use the MedMLX MCP server to run flexray from an MCP client. Weights download on first use.

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 flexray.

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. See third-party notices. Model weights retain their publisher terms. FleXray weights are noncommercial (CC-BY-NC-4.0).

Metadata

Release files for flexray-mlx 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flexray-mlx 0.3.1
File Size Uploaded Provenance Actions
flexray_mlx-0.3.1.tar.gz 42.7 kB Without provenance Details for flexray_mlx-0.3.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for flexray-mlx 0.3.1
File Interpreter ABI Platform Actions
flexray_mlx-0.3.1-py3-none-any.whl Python 3 none any Details for flexray_mlx-0.3.1-py3-none-any.whl

Total release size: 89.9 kB

Release files / flexray_mlx-0.3.1.tar.gz

Download URL flexray_mlx-0.3.1.tar.gz
Size 42.7 kB
Tags Source
SHA-256 checksum
How to use checksums
b2981cea3f4ffa91eb9b357c9bd5e3662d7c6f7400ce80f4d62b370e28632111
BLAKE2b-256 checksum
How to use checksums
6397dc62e5dd10a70c998c2546da83f6f620406cefdcfd93e24713bd71e95195
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / flexray_mlx-0.3.1-py3-none-any.whl

Download URL flexray_mlx-0.3.1-py3-none-any.whl
Size 47.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
81919f36038a175bc3af702a94ba120b0e8e596e95995f77eda519f5ea015b04
BLAKE2b-256 checksum
How to use checksums
b78ef629581f9745822644f1b889c807664df363843fa5a54f973e8236dd0419
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for flexray-mlx 0.3.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page