pelmesha
Peak Extraction Library for Mass spectrometry Enhanced by Statistical High-throughput Analysis
pelmesha is a Python package for processing Mass Spectrometry Imaging (MSI) data stored in .imzml (and, experimentally, .cdf) files. It loads raw spectra, processes them, detects peaks, corrects their m/z values with a kernel density estimate (KDE), and aggregates multiple samples/ROIs into a unified feature matrix.
Features
- Loading & metadata extraction — reads raw MSI data and builds a structured metadata HDF5 file (
*_ingredients.hdf5) containing sample metadata, per-ROI index ranges, m/z ranges, and spatial coordinates. - Configuration-driven processing pipeline — a configuration system (
Configs,PipelineConfigurator,PreparedDataSource) that validates parameters, distributes them to the pipeline steps, and supports YAML serialisation. The lightweightKDEConfigsclass is built on Pydantic. - Spectrum processing — smoothing, baseline correction, resampling to a uniform m/z scale, and alignment against reference peaks using a slightly modified version of the
msalignimplementation. - Peak picking — detection of peaks together with their area, FWHM points, peak-base boundaries, and signal-to-noise ratio.
- KDE-based m/z correction — peaks that wander slightly across spectra are grouped into single m/z values based on their kernel density estimate (using KDEpy). The library implements a custom bandwidth autoselection strategy that adapts to local peaks dispersion and m/z scale discretization, avoiding the pitfalls of classical bandwidth rules that are often unsuitable for sparse and noisy MSI peak lists with high modality.
- Multi-sample aggregation — builds a feature matrix from the peak lists of several samples and ROIs, with optional occurrence filtering, duplicate merging, pivoting, and coordinate merging.
- Reference peaks — generates a reference peak list from a reference source and uses it to align the other samples registered in the same
DataSet.
Installation
pip install pelmesha
Requires Python >= 3.10. The main dependencies are numpy, pandas, scipy, h5py, pyimzml, pybaselines, KDEpy, scikit-learn, xarray, pydantic, pyyaml, and pyarrow.
Quick start
1. Load data sources
Create a DataSet from a list of raw files (or directories):
from pelmesha import DataSet
ds = DataSet(sources=["/data/sample1.imzml", "/data/sample2.imzml"])
Each source is wrapped in a PreparedDataSource and registered by its sample name:
print(ds) # text table of the registered sources
ds["sample1"] # access a prepared source by sample name
2. Configure the processing pipeline
Per-source processing and KDE configurations can be adjusted either for all ROIs at once or for a single ROI:
# Update a parameter for all ROIs of one sample
ds["sample1"].update({"smooth_window": 7})
ds["sample1"].update_kde(bwc=1.5)
# Or configure a specific ROI directly
roi_config = ds["sample1"].roi_configs["R00"]
roi_config["SNR_threshold"] = 4
roi_config["smooth_algo"] = "GA"
kde_config = ds["sample1"].roi_kde_configs["R00"]
kde_config["bwc"] = 1.5
# Exclude specific methods from the pipeline by deleting them from the config
# This disables baseline correction for this RO
roi_config.delete('Baseline') # Baseline correction will be not implemented
# Change the algorithm or method using set_method
# This replaces the method with 'modpoly' and updates default parameters
roi_config.set_method('Baseline','modpoly')
Configurations are stored next to each source file and can be saved/loaded as YAML (*_processing_recipe.yaml and *_kde_recipe.yaml).
3. Process spectra and pick peaks
ds.process() # smoothing → baseline → resampling → alignment
ds.peakpick() # smoothing → baseline → resampling → alignment and detect peaks for every spectrum
These write *_processed_spectra.hdf5 and *_peaklists.hdf5 next to each source.
4. Estimate peak density
ds.estimate_peak_density_kde()
This writes the per-ROI peak probability density into *_peaks_density.hdf5.
💡 Note: estimate_peak_density_kde() uses an adaptive bandwidth selection method designed for MS data. Unlike classical approaches, it accounts for local peak density and outlier behavior, making it more robust for real-world spectra. For detailed configuration, see the KDEConfigs class and the “KDE algorithm specifics” section.
5. Build a feature matrix
fm = ds.feature_matrix(countf=10, pivot_values="Intensity")
Optionally save it as Parquet together with the coordinates:
fm = ds.feature_matrix(save_path="results/feature_matrix.parquet",
merge_with_coords=True)
6. Use reference peaks for alignment (optional)
Reference peaks are optional. If you want cross-sample alignment, generate the reference peak list before running process / peakpick on the other samples:
ds.set_reference_source("/data/reference.imzml")
ds.get_reference_peaks()
ds.set_align_peaks_from_ref()
set_align_peaks_from_ref then assigns the reference peaks to the selected samples/ROIs of the DataSet as their alignment targets.
Interactive tutorial
For a step‑by‑step interactive walkthrough with visualisations and additional tips, check out the tutorial Jupyter Notebook:
📘 Tutorial Notebook: Working with the pelmesha Package
⚠️ Note: The repository currently does not include sample .imzml or .cdf files (due to file size and licensing). We plan to add a minimal test dataset in a future release. For now, please run the pipeline on your data. The tutorial notebook demonstrates the workflow and expected outputs using representative data structures.
Pipeline steps
The per-spectrum processing pipeline consists of the following steps:
- Smoothing — moving-average (
MA), Gaussian (GA), or Savitzky–Golay (SG) filters. - Baseline correction — using the pybaselines library.
- Resampling — brings the data onto a uniform m/z scale (
resample_mz_scale). - Alignment — calibration and alignment relative to reference peaks using the bundled, slightly modified
msalignimplementation inAligner.
After peak picking, the probability density function (PDF) of the peaks is built and saved for every individual ROI, using the parameters specified for that ROI. Once both peak picking and the PDF estimation are complete, the resulting files are ready to be combined into a single dataset and to form a common feature matrix across all the samples and ROIs.
Kernel Density Estimation (KDE) Approach for Feature Matrix Construction
The pelmesha library uses Kernel Density Estimation (KDE) to build a probability density function (PDF) of peaks across the mass-to-charge (m/z) scale. This method is central to grouping slightly wandering peaks into stable m/z clusters, enabling the creation of a unified feature matrix from multiple Mass Spectrometry Imaging (MSI) samples.
How KDE Works in pelmesha
-
Segmentation of the m/z Scale
The m/z scale is divided into manageable segments using configurable parameters:split_peaks_min: minimum number of peaks per segment.split_mz_min: minimum m/z range between segments.
This segmentation allows parallel processing via Python’s multiprocessing, which is critical for handling large datasets efficiently.
-
Algorithm Selection
pelmeshasupports two algorithms:- FFTKDE (Fast Algorithm):
Assigns a single bandwidth to each segment, derived from the median FWHM (Full Width at Half Maximum) of all peaks in the segment. This is faster but less flexible and accurate. - TreeKDE (Default Algorithm):
Assigns individual bandwidths based on each peak’s FWHM. This method is more precise, but computationally heavier.
- FFTKDE (Fast Algorithm):
-
Handling Sparse Data In regions with very few data points (e.g., peaks with only 3 points), KDE can overfit. To avoid this,
pelmeshasets a minimum bandwidth threshold equal to the difference between adjacent m/z points. This ensures robustness even when the m/z sampling is coarse.Note: for this
pelmeshainterpreting m/z scale of data source on first processing, the algorithm:- Interpolates the m/z scale to handle non-continuous data with zero-thresholded dots or dealing with unique m/z scales per spectrum.
- Stores only polynomial interpolation coefficients to save memory, optimizing for efficiency.
Key Computational Advantage: Additivity
KDE’s mathematical property of additivity enables two crucial optimizations:
- Parallel Processing & Summation:
Segments are processed independently in parallel, then their results are summed to obtain the overall PDF. This drastically speeds up calculations for large datasets. - Combining Data Sources:
PDFs from different samples/regions-of-interest (ROIs) can be summed directly to build a dataset-wide feature matrix. This eliminates the need to reprocess all data together.
Why This Approach Is Better
Compared to traditional methods, pelmesha’s KDE-based approach offers:
- No Manual Tolerance Tuning
You don’t need to manually set m/z tolerance thresholds to merge peaks. The algorithm adapts automatically. - Adaptive Resolution Across m/z
The method adjusts to varying peak densities—using finer resolution where peaks are dense and coarser where they’re sparse. - Detection of Overlapping Peaks
It can distinguish signals from molecules with very close m/z values (provided spectra are well-calibrated and aligned). - Elimination of “Empty Features”
Fixed binning often creates empty bins with no signal. KDE avoids this by focusing only on regions with actual data.
Analogy to Other Methods
Think of this as adaptive binning:
- Unlike fixed binning,
pelmeshaautomatically finds bin boundaries based on where signal exists—no arbitrary bin widths. - Compared to peak-picking + tolerance-based aggregation, KDE provides a smoother, data-driven way to merge peaks, avoiding the pitfalls of rigid thresholds.
Summary:
pelmesha’s KDE approach combines statistical flexibility, computational efficiency, and ease of use—allowing you to construct meaningful feature matrices from complex MS datasets without manual parameter tweaking.
Project structure
| Module | Purpose |
|---|---|
filling |
DataSource, DataManager, and format-specific loaders (imzML, CDF). |
dough |
Utility classes: LinkedList, AdaptiveParameter, Indexator, SliceIndexator. |
cookbook |
Configuration system: Configs, PipelineConfigurator, PreparedDataSource, KDEConfigs. |
kneading |
Base pipeline functions: processing, smoothing, peak picking, KDE estimation, msalign. |
serving |
Orchestration and visualisation: DataSet, Pipeline, Drawer. |
align |
The Aligner class implementing signal calibration/alignment. |
utensils |
Assorted helper functions and constants. |
Contacts
- Bug reports & feature requests: GitHub Issues
- Direct contact: Kuzya-90@bk.ru
License
Distributed under the Apache-2.0 license. See LICENSE.txt.
Release files for pelmesha 0.7.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pelmesha-0.7.4.tar.gz | 425.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pelmesha-0.7.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 559.7 kB
Release files / pelmesha-0.7.4.tar.gz
| Download URL | pelmesha-0.7.4.tar.gz |
|---|---|
| Size | 425.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
527b812bbda4d87b5343d473a646a087086d6e62db33db179d41bc8585500ba5
|
|
BLAKE2b-256 checksum How to use checksums |
22c2f463e214f33a45484eabf5327f93ad4614d8339be8d65b06bd8c602f9fa8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 24, 2026.
Transparency logRelease files / pelmesha-0.7.4-py3-none-any.whl
| Download URL | pelmesha-0.7.4-py3-none-any.whl |
|---|---|
| Size | 133.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1a89a4200a562e6827c737f044ed1c598b41a6d27775b2ccd4adca673ab6a995
|
|
BLAKE2b-256 checksum How to use checksums |
6a55804942121d4a966b378acfe6e23336c808b3cc3464e68d4aca2eac430afb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 24, 2026.
Transparency log