Skip to main content

bioio-imzml

CI PyPI License

A basic BioIO reader plugin for imzML mass spectrometry imaging (MSI) data, read with pyimzML.

Installation

pip install bioio-imzml

Requires a sibling .imzML + .ibd file pair (the standard imzML layout).

Usage

from bioio import BioImage

img = BioImage("sample.imzML")
img.dims.order  # "TCZYX" -- C is the m/z axis
img.channel_names  # "<m/z>±<tolerance>" strings, e.g. "798.5400±0.0000"
img.data  # (T, C, Z, Y, X) numpy array

imzML-specific options (mz, mz_step, n_bins, mz_tolerance_absolute, mz_tolerance_relative) work the same way through BioImage, since it forwards unrecognized keyword arguments straight to the reader. Pass reader=bioio_imzml.Reader to skip plugin auto-detection (useful when more than one installed plugin could claim the file):

import bioio_imzml
from bioio import BioImage

# "processed" mode files (one m/z axis per pixel) need target channels:
img = BioImage("sample.imzML", reader=bioio_imzml.Reader, mz=[798.54, 826.57, 885.55])

# reject a target with no real peak nearby instead of returning whatever
# peak happens to be closest, however far away. mz_tolerance_absolute and
# mz_tolerance_relative are both in the same units as mz (m/z) -- relative
# is a plain fraction, not a ppm count, so convert yourself (3 ppm = 3e-6).
# They combine per channel as: tolerance = absolute + m/z * relative
img = BioImage(
    "sample.imzML",
    reader=bioio_imzml.Reader,
    mz=[798.54, 826.57],
    mz_tolerance_absolute=0.005,
    mz_tolerance_relative=3e-6,  # 3 ppm
)
img.reader.mz_tolerance  # the resulting per-channel tolerance, e.g. [0.0074, 0.0075]
img.channel_names  # ["798.5400±0.0074", "826.5700±0.0075"]

# leave both unset and "processed" mode files get a tolerance for free:
# half the distance to each target's nearest neighboring target, so windows
# never overlap (a lone target with no neighbor is left unbounded).
img = BioImage("sample.imzML", reader=bioio_imzml.Reader, mz=[798.54, 826.57, 885.55])
img.reader.mz_tolerance  # e.g. [14.015, 14.015, 29.49] (half the gaps above/below)

# or let the reader pick evenly spaced channels across the file's m/z range,
# either a fixed count (n_bins) or a fixed step (mz_step) in m/z units:
img = BioImage("sample.imzML", reader=bioio_imzml.Reader, n_bins=512)
img = BioImage("sample.imzML", reader=bioio_imzml.Reader, mz_step=0.1)

Auto peak-picking

Don't know which m/z channels a file actually has signal at? auto_pick_peaks finds candidate peaks on the file's mean spectrum, then drops candidates that are too rare across pixels or spatially unstructured (noise/matrix artifacts rather than real signal):

import bioio_imzml
from bioio import BioImage

# pin a tolerance and reuse it for picking and extraction, so extraction
# matches what pixel_frequency/spatial_chaos actually scored. Size it to
# bin_width, not to ppm mass-accuracy precision: candidates come from a
# bin_width-binned mean spectrum, so a candidate's reported m/z can be off
# from the true peak by up to ~bin_width/2.
bin_width = 0.05
tol_abs = bin_width

result = bioio_imzml.auto_pick_peaks(
    "sample.imzML",
    min_mz=650,
    max_mz=850,
    bin_width=bin_width,
    mz_tolerance_absolute=tol_abs,
)
result.mzs  # candidate m/z values, sorted by descending intensity
result.pixel_frequency  # fraction of pixels with signal, one per mz
result.spatial_chaos  # 0 (structured) .. 1 (spatially random), one per mz

if len(result.mzs) == 0:
    # min_pixel_frequency/max_spatial_chaos defaults can reject every
    # candidate on data with sparse per-pixel peak-picking (e.g.
    # single-cell-resolution processed-mode files) -- loosen or disable a
    # filter rather than pass an empty mz list on to Reader/BioImage:
    result = bioio_imzml.auto_pick_peaks(
        "sample.imzML",
        min_mz=650,
        max_mz=850,
        bin_width=bin_width,
        mz_tolerance_absolute=tol_abs,
        max_spatial_chaos=None,
    )

img = BioImage(
    "sample.imzML",
    reader=bioio_imzml.Reader,
    mz=result.mzs,
    mz_tolerance_absolute=tol_abs,
)

Tune detection sensitivity (snr_threshold, min_relative_intensity, min_separation_mz) and the quality filters (min_pixel_frequency, max_spatial_chaos) as keyword arguments; see the docstring for defaults. snr_threshold, min_pixel_frequency, and max_spatial_chaos each accept None to disable that filter -- passing None for both quality filters also skips the per-pixel pass over the file entirely (the slow part), leaving result.pixel_frequency/result.spatial_chaos as NaN. bioio_imzml.peak_picking also exposes the individual steps -- mean_spectrum, find_peaks_in_spectrum, and pixel_frequency_and_spatial_chaos -- to inspect intermediate results or why a candidate was dropped before committing to thresholds.

Continuous vs. processed mode

imzML stores spectra in one of two ways:

  • continuous: every pixel shares one m/z axis, so intensities already line up across pixels. Detected automatically (identical m/z byte offset and length for every spectrum) and read directly -- no resampling, no channel arguments needed.
  • processed: each pixel has its own m/z axis (typical for high-resolution profile data). There's no single true channel set, so this reader resamples every spectrum onto shared target m/z values by nearest-neighbor lookup, given via mz= or auto-generated with n_bins=.

reader.is_continuous reports which case applies to a given file.

Development

uv sync --extra test
uv run pytest
uv run ruff check .
uv run ruff format .
uv run ty check

Bump the version (updates pyproject.toml) and tag a release to publish to PyPI via CI:

uv version --bump patch  # or minor / major
git commit -am "Bump version"
git tag "v$(uv version --short)"
git push --tags

License

BSD-3-Clause

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bioio_imzml-0.2.2.tar.gz (116.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bioio_imzml-0.2.2-py3-none-any.whl (21.3 kB view details)

Uploaded Python 3

File details

Details for the file bioio_imzml-0.2.2.tar.gz.

File metadata

  • Download URL: bioio_imzml-0.2.2.tar.gz
  • Upload date:
  • Size: 116.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for bioio_imzml-0.2.2.tar.gz
Algorithm Hash digest
SHA256 2b6a7b9d1d34a229816367aa0c8eefa49b4c402e94cc4fb81be184fd264670c2
MD5 ddf0be738ca2126caaac3b6c17754bea
BLAKE2b-256 63e30aa153f72d87425c9ca0366587f5a2920936545a119e63842e6ea325a0ab

See more details on using hashes here.

Provenance

The following attestation bundles were made for bioio_imzml-0.2.2.tar.gz:

Publisher: ci.yml on DBP008/bioio-imzml

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bioio_imzml-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: bioio_imzml-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 21.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for bioio_imzml-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 72a9aa200480d84f7e1903b339535bb4f4cb17403f82ba2cfe23c0bb86050083
MD5 098733959ed3f7a2bd127f18ee92d232
BLAKE2b-256 b42912d67ad63c6ee557dab0afd5e1553691b0aaa597f93831ce047b86f293ea

See more details on using hashes here.

Provenance

The following attestation bundles were made for bioio_imzml-0.2.2-py3-none-any.whl:

Publisher: ci.yml on DBP008/bioio-imzml

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.7.0

2 files

0.6.0

2 files

0.5.0

2 files

0.4.1

2 files

0.4.0

2 files

0.3.2

2 files

0.3.1

2 files

0.3.0

2 files

This release

0.2.2 This release

2 files

0.2.1

2 files

0.1.2

2 files

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