Skip to main content

flexray-mlx

Segment anatomical structures in radiographs with FleXray and native MLX.

PyPI Python License Apple Silicon Hugging Face MedMLX

Model weights · MedMLX

Chest radiograph with lung, heart and individual rib segmentations
Input Output
Raster image or single-frame DICOM Structure masks and probabilities

Weights: 2.0 GB download, all five ensemble members. Peak MLX memory: 1.2 GB for a 1929×2207 chest radiograph with the default single model.

Use with MedMLX

The MedMLX MCP server 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 flexray for you.

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.

Install

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

Run

from flexray_mlx.medmlx import run

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

Omitting weights_path downloads MedMLX/flexray-mlx into the shared MedMLX cache on first use; interrupted downloads resume and later runs work offline. With MedMLX installed, medmlx setup flexray downloads it ahead of time. To use your own copy, pass a folder containing ensemble.json and members/flux0375/{config.yml,label_schema.json,model.safetensors,preprocessing.json} as weights_path. Automatic setup prepares the flagship (ensemble=False); ensemble=True requires all five members in an explicit folder. details.weights_revision records the downloaded commit, or null for a folder without a MedMLX download record.

Use label_name to select a structure.

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

DICOM files end in .dcm or .dicom (any case) or have DICM at byte 128. 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
display_source source_image.png 8-bit grayscale source the mask overlays
probabilities probabilities.npy float32 61×256×256 sigmoid probabilities on the padded model grid
prediction prediction.json Labels, selected channel, threshold, members and content box

FleXray thresholds on the model grid and exports no source-grid images; the PNGs are a MedMLX addition. The mask is thresholded first, then cropped to the content box and restored to the source size with nearest-neighbour resampling, so no new decisions come from interpolation. run returns {"outputs": {...}, "details": {...}}; details repeats the label and channel, threshold, members, TTA passes, seed, normalization, source size, content box and mask pixel count. Missing or incompatible assets raise AssetNotReadyError; invalid inputs or existing outputs raise InvalidInputError; non-finite probabilities raise ModelExecutionError before anything is written.

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.

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. Inputs are prepared images; preparation, sigmoid, ensembling, TTA and source-grid restoration are the caller's. 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. Inference needs Apple Silicon with Metal and has no CPU fallback.

License

Apache-2.0. See third-party notices. Model weights retain their publisher terms. FleXray weights are noncommercial (CC-BY-NC-4.0).

For research and education. Outputs require human review.

Metadata

Release files for flexray-mlx 0.2.7

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.2.7
File Size Uploaded Provenance Actions
flexray_mlx-0.2.7.tar.gz 38.1 kB Without provenance Details for flexray_mlx-0.2.7.tar.gz

Built distribution (wheel)

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

Total release size: 80.4 kB

Release files / flexray_mlx-0.2.7.tar.gz

Download URL flexray_mlx-0.2.7.tar.gz
Size 38.1 kB
Tags Source
SHA-256 checksum
How to use checksums
14d65200c0411ae87dd409a61e00a386873737fe1e1b51d8582d657e6f234223
BLAKE2b-256 checksum
How to use checksums
c7a70a2d5cfbf0fc24fb35cb2040e5fae22408cfce6a2c3fab6b727953e50fb0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","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.2.7-py3-none-any.whl

Download URL flexray_mlx-0.2.7-py3-none-any.whl
Size 42.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a2cc068e97907ef58001767d124fa76ade4d61e1f17c6475db8b5735c9a6c1b5
BLAKE2b-256 checksum
How to use checksums
86c987773248767136594e12c819fc025cba4fab4979d29dd589a00046b186a1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","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.2.7

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