How m2aia/pym2aia works
Project type and purpose
• This is a Python library (pyM2aia) for mass spectrometry imaging (MSI) data access, signal processing, and batch generation. It exposes a high-level Python API and wraps a native C++ MITK-based backend for reading/writing imzML files and performing heavy-duty processing.
File structure
• src/m2aia/
• ImageIO.py – imzML reader/writer interface
• Dataset.py, Library.py – high-level data structures
• Generators.py – base batch-generation classes
• src/m2aia/keras & src/m2aia/torch
• Framework-specific batch generators (TensorFlow/PyTorch)
• src/m2aia/utils – signal-processing utilities (binning, peaks, volcano plot)
• setup.py, requirements.txt – packaging and dependencies
• tests/ – integration and unit tests with sample data
• docs/ – Sphinx documentation
README for additional insights
• Core features: imzML I/O, baseline correction, normalization, smoothing, batch generators, continuous centroid writing
• External dependencies: SimpleITK for image handling, TensorFlow and PyTorch for training pipelines, native MITK/C++ libraries via m2PythonWrapper
Main components
a. Python API Layer (green)
• m2aia.ImageIO – ImzMLReader/Writer
• m2aia.Dataset & m2aia.Library – data abstractions
• m2aia.Generators – abstract BatchGenerator
• m2aia.keras.KerasGenerators & m2aia.torch.PyTorchGenerators – framework-specific generators
• m2aia.utils – signal-processing modules (binning, peaks, volcano)
b. Native Backend Layer (blue)
• C++ MITK-based core libraries (libMitkXY.so/.dll)
• m2PythonWrapper.cpp – SWIG/pybind11 wrapper that exposes C++ to Python
c. External Dependencies (orange)
• SimpleITK – grayscale image objects
• TensorFlow / PyTorch – model training loops consuming batch generators
• imzML files (input) and continuous centroid imzML (output)
Relationships and data flow
1. User code calls Python API (ImzMLReader)
2. Python API invokes C++ backend via m2PythonWrapper
3. C++ backend reads imzML metadata/spectra and returns image arrays (SimpleITK objects)
4. Python utilities apply baseline correction, normalization, smoothing
5. BatchGenerators fetch data (spatial, spectral, spatio-spectral) either in-memory or streaming from disk
6. Generators feed TensorFlow or PyTorch training pipelines
7. Python API calls WriteContinuousCentroidImzML → forwards to C++ writer → outputs imzML file
Architectural patterns and design principles
• Layered architecture:
• Presentation (Python API)
• Processing (Python utilities & generators)
• Persistence/IO (C++ MITK libraries)
• Separation of concerns: distinct modules for I/O, processing, and framework integration
• Adapter/Wrapper pattern: m2PythonWrapper exposes native C++ to Python seamlessly
• Strategy pattern: interchangeable signal-processing functions and batch-generator backends
Details to capture
• Package entry points (m2aia/__init__.py) and setup scripts
• CI workflows (.github/workflows) and test harness (tests/) as auxiliary components
• Config files (requirements.txt, .devcontainer) ensuring reproducibility