# Deep Learning in Mass Spectrometry Imaging: A Hybrid Julia-Python Workflow for the Automated Anatomical Mapping of *Datura innoxia*

**Authors:** Carlos Daniel Sierra Álvarez, Juan Francisco Moreno Luna, José Julián Sierra Álvarez, José Jovani Martínez.  
**Affiliation:** Advanced Genomics Unit, Cinvestav, km 9.6 Libr. Nte. Irapuato-León, Irapuato, 36824, Gto., Mexico.  
**Keywords:** MSI, Julia Language, Deep Learning, SimCLR, Contrastive Learning, Group Normalization, *Datura innoxia*, Spatial Metabolomics.

---

## Abstract
Mass Spectrometry Imaging (MSI) has emerged as a fundamental tool in spatial metabolomics, enabling the *in situ* visualization of molecular distributions. However, the nature of these data—characterized by massive volumes of multivariate information and high levels of instrumental noise—poses critical computational challenges for traditional analysis methods (Shah et al., 2025). In this work, we present an innovative computational architecture that addresses the "two-language problem" by integrating **Julia** for high-performance data processing and **Python** for representation learning via deep neural networks. We implemented a self-supervised learning workflow based on the **SimCLR** architecture with a **custom Group-Normalized Fully Convolutional** encoder, designed to extract robust morphological features from ion images without prior labeling. This system was applied to the study of *Datura innoxia* (toloache) leaves at a spatial resolution of 150 µm, achieved via Laser Desorption Low-Temperature Plasma (LD-LTP) ionization. By prioritizing anatomical fidelity over mathematical parsimony, we identified an optimal clustering of $k=10$ that accurately distinguishes vascular, paravascular, and parenchymal tissues. Our results demonstrate that the latent space generated by the neural network captures fundamental chemical relationships, such as the co-localization of isotopic patterns, offering a scalable and objective pathway for the interpretation of complex biological systems.

---

## 1. Introduction
The analysis of the spatial distribution of secondary metabolites in medicinal plants is essential for understanding biosynthesis, transport, and chemical defense processes (Ziegler & Facchini, 2008; García-Rojas et al., 2024). *D. innoxia*, known for its rich composition of tropane alkaloids, represents a highly complex biological model due to the biosynthesis of these compounds in the roots and their differential transport to the shoots (Schlesinger et al., 2021). Previous studies have employed laser desorption low-temperature plasma (LD-LTP) techniques to map these alkaloids in fruits and seeds (Moreno-Pedraza et al., 2019).

Traditionally, MSI data analysis has relied on linear methods and standard imaging protocols (Cole & Clench, 2023). However, a software engineering challenge arises: while Python possesses the most mature ecosystem for Artificial Intelligence, its performance in handling massive binary files in **imzML** format (Schramm et al., 2012) is often limited. Conversely, Julia offers execution speeds comparable to C++ for compute-heavy tasks (Godoy et al., 2023) and outperforms interpreted languages in scientific computing benchmarks (Churavy et al., 2020). The integration of these two ecosystems addresses the "two-language problem"—the historical trade-off between the ease of a high-level language and the performance of a low-level one (Perkel, 2019). In this article, we propose that the synergy between these two languages enables a "next-generation" MSI workflow. The remainder of this paper details the acquisition parameters, the hybrid Julia-Python implementation, the unsupervised segmentation results, and a comparison against traditional linear baselines.

---

## 2. Methodology

### 2.1 MSI Data Acquisition (LD-LTP-MSI)
We performed imaging using an **LD-LTP-MSI** system coupled to an **LCQ Fleet Ion Trap** mass spectrometer (Thermo Scientific). The custom ionization source consisted of a Low-Temperature Plasma (LTP) probe (Harper et al., 2008) and a continuous-wave violet laser (400–450 nm, 1.0 W). The LTP probe was operated with helium (0.1 L/min) at 3.3 kV and ~13 kHz, positioned at a 45° angle and 4 mm from the ion transfer capillary (Martínez-Jarquín & Winkler, 2017). The laser was oriented at 90° to the sample surface at a focal distance of 9 cm, producing a spot size of ~22 µm. We acquired data in positive ion mode (*m/z* 50–700) with a spatial resolution of **150 µm** for the leaf sample.

### 2.2 Computational Environment and Reproducibility
All computational analyses, including the high-performance data extraction in Julia and the deep learning-based representation learning in Python, were performed across two mobile workstations. The primary data ingestion and analysis were executed on a system with the following specifications: **Processor:** Intel(R) Core(TM) i5-10210U CPU @ 1.60 GHz (4 cores, 8 threads), **Memory:** 32 GB DDR4 RAM, **Graphics:** Intel UHD Graphics (CometLake-U GT2), and **Operating System:** Ubuntu 24.04.4 LTS (Linux Kernel 6.8.0-110-generic). Hardware-accelerated neural network training (CUDA) was executed on a Dell G15 5525 equipped with an **AMD Ryzen 5 6600H** processor (6 cores, 12 threads @ 4.57 GHz), **8 GB RAM**, and an **NVIDIA GeForce RTX 3050 Mobile** discrete GPU, running **Pop!_OS 24.04 LTS**. To ensure rigorous compliance with MSI reporting standards such as MIAMSIE (Gustafsson et al., 2018) and SMART (Xi et al., 2023), as well as modern machine learning reproducibility guidelines like REFORMS (Kapoor et al., 2024), the complete hybrid workflow was containerized using Docker. This guarantees that both the Julia binary parsing algorithms and the Python machine learning dependencies remain perfectly reproducible across diverse hardware platforms.

### 2.3 High-Performance Processing and Data Normalization (Julia)
We conducted the preprocessing stage using the **JuliaMSI** framework (Sierra-Álvarez et al., 2025), a platform specifically engineered to address the computational bottlenecks of the "two-language problem." By leveraging Julia’s just-in-time (JIT) compilation and efficient memory-mapping capabilities, we facilitated the rapid ingestion and manipulation of massive **imzML** binary files. As demonstrated in previous benchmarks (Sierra-Álvarez et al., 2025), this architecture provides a significant performance advantage over interpreted environments such as Python or R (Rosas-Román et al., 2020), enabling the high-throughput extraction of mass slices without compromising spectral precision.

The selection of Julia as the primary engine for data ingestion is justified by its superior performance in handling the high-dimensional sparse matrices characteristic of imzML files. Recent benchmarks have demonstrated that Julia-based architectures, such as the JuliaMSI framework, outperform traditional R-based implementations (e.g., RmsiGUI) by significant margins, showing up to 5.2-fold faster processing on Linux environments (Sierra-Álvarez et al., 2025). Furthermore, at the library level, Julia's native parsing of binary spectral data has been shown to be up to 214 times faster than equivalent implementations in R (Rosas-Román et al., 2024). This computational efficiency is critical for resolving the "two-language problem," as it allows the rapid extraction of hundreds of mass slices and their subsequent normalization in a fraction of the time required by interpreted environments, effectively enabling a high-throughput bridge to the Python-based deep learning ecosystem.

1.  **Ion Extraction and Filtering:** We extracted mass slices from the `Leaf.imzML` dataset within the ***m/z* 50.0 to 700.0** range. This upper limit was selected because no significant signals were detected beyond this threshold using the LD-LTP-MSI technique for this specific sample, and its implementation allowed for a more computationally manageable dataset. To ensure the inclusion of only chemically relevant signals, we filtered peaks using an intensity threshold of 2% relative to the base peak of the average spectrum, resulting in a refined subset of ion images for downstream analysis. Owing to the spectral binning inherent to the instrument's data acquisition, a single molecular species may be distributed across adjacent *m/z* channels; this discretization is reflected in the ion image filenames (e.g., *m/z* 304.00 and 304.33 for scopolamine) and must be considered when comparing imaging clusters with high-resolution MS/MS identifications.

2.  **TrIQ Normalization (Threshold Intensity Quantization):** To mitigate the impact of "hotspots"—stochastic pixels with disproportionately high intensities that obscure lower-abundance metabolic distributions—we implemented a dynamic TrIQ algorithm (Rosas-Román & Winkler, 2021). For each raw ion map $I_{raw}$, the algorithm identifies a signal threshold $T$ based on the distribution of non-zero, finite intensity values $V = \{i \in I_{raw}\mid i > 0, i < \infty\}$. The threshold is defined as the 98th percentile of this distribution:

$$T = P_{98}(V)$$

In cases where the distribution is extremely sparse and $P_{98} = 0$, the threshold defaults to the maximum observed intensity ($T =\max(V)$). Each pixel's intensity is then normalized and bounded to the unit interval $[0, 1]$ using a clamping function:

$$I_{norm}(x, y) = \min\left(1, \frac{I_{raw}(x, y)}{T}\right)$$

This transformation ensures that 98% of the chemical signal is represented within a standardized dynamic range, effectively enhancing the contrast of parenchymal and vascular structures. By suppressing the influence of extreme outliers, TrIQ provides a morphologically consistent input for the subsequent contrastive learning phase, preventing the neural network from over-fitting to instrumental artifacts.

3.  **Spatial Standardization (Pristine Coordinate Mapping):** To ensure compatibility with the deep learning backbone, each normalized ion map was spatially embedded into a standardized **256 $\times$ 256 pixel** matrix using strict **Zero-Padding**, completely avoiding any sub-pixel interpolation. In MSI, the physical laser ablation grid serves as an absolute spatial ground truth. Standard computational interpolation (such as bilinear resizing) forces anisotropic stretching —disproportionately warping the leaf morphology— and introduces artificial floating-point values at the sharp boundaries of the tissue mask. By mapping the raw $161 \times 106$ coordinates directly into the center of a zero-matrix, we preserved the true, undistorted anatomical geometry of the leaf while achieving the uniform dimensionality required by the convolutional layers. As a demonstration of this high-throughput pipeline, the Julia engine parsed the raw `.ibd` binaries, applied TrIQ, and exported 588 padded individual ion slices in less than 16 seconds.

### 2.4 Self-Supervised Representation Learning (Python)
We implemented a Contrastive Learning (SimCLR) scheme in PyTorch (Paszke et al., 2019), adapting the framework to the unique properties of mass spectrometry imaging.

1.  **Model Architecture and VRAM Optimization:** A custom **Fully Convolutional Contrastive Network** was engineered to replace standard heavy computer vision backbones. Crucially, all standard batch normalization blocks were replaced with **Group Normalization (GroupNorm)** layers (8 groups per layer). In datasets characterized by massive empty, zero-padded backgrounds, standard Batch Normalization calculates running statistics that become overwhelmingly biased by the blank space, leading to gradient instability and the artificial clustering of the background mask. Group Normalization computes statistics per individual image slice, mathematically neutralizing the static background and forcing the network to optimize strictly on the high-variance internal biological structures. The network concludes with an Adaptive Average Pooling layer and a 2-layer MLP to project the spatial features into a 128-dimensional latent space (Table 1).

To enable training on consumer-grade hardware (NVIDIA RTX 3050 Mobile, ~4 GB VRAM), a gradient accumulation strategy was employed. Rather than fitting an effective batch of 32 images simultaneously into GPU memory —which would trigger a CUDA out-of-memory error— the engine processes 8 images physically per step and accumulates gradients over 4 consecutive forward passes before executing a single optimizer update, as described in the Hugging Face documentation (Hugging Face, 2025). This is mathematically equivalent to training with a true batch size of 32 while using only a quarter of the VRAM.

**Table 1. Training hyperparameters and model configuration for the SimCLR-MSI workflow.**

| Parameter | Value / Configuration |
| :--- | :--- |
| **Backbone Architecture** | Custom 4-Layer CNN with Group Normalization |
| **Projection Head** | 2-layer MLP (Linear 512-ReLU-Linear 128) |
| **Input Image Resolution** | 256 x 256 pixels (Zero-Padded) |
| **Optimization Algorithm** | Adam Optimizer (Kingma & Ba, 2014) |
| **Learning Rate** | $3 \times 10^{-4}$ |
| **Physical Batch Size / Accumulation Steps** | 8 / 4 (Effective Batch Size: 32) |
| **Temperature ($\tau$)** | 0.5 (NT-Xent Loss) |
| **Training Duration** | 60 Epochs |
| **Data Augmentations** | Intensity Scaling (0.8×–1.2×), Poisson Shot Noise, Low-Signal Dropout |

2.  **Dataset Size and the Dynamic Training Pool:** The training corpus comprises 588 ion images that passed the 2% intensity threshold filter from the original spectral binning. This number is mathematically sufficient for this self-supervised task for two compounding reasons. First, the custom 4-layer fully convolutional architecture is a lightweight, low-dimensional feature extractor. Unlike overparameterized ImageNet backbones such as EfficientNet-B0 (Tan & Le, 2019) (which carry over 5 million parameters trained for 1,000 natural image categories), our encoder is purpose-built to extract a compact morphological fingerprint; it requires far fewer samples to converge on a meaningful manifold. Second, the `apply_msi_chemical_noise` augmentation pipeline functions as a stochastic, infinite data generator. Because intensity scaling factors, Poisson noise seeds, and dropout masks are sampled randomly at each epoch, the network never encounters the exact same image representation twice, exponentially expanding the effective training pool and providing robust protection against overfitting.

3.  **The Camp A Paradigm: Prohibition of Spatial Augmentations:** Standard computer vision augmentation libraries (e.g., Torchvision) apply spatial transformations—random rotations, horizontal/vertical flips, and random crops—as a default strategy to promote translation and rotation invariance. This practice is explicitly and deliberately incompatible with our co-localization objective and was therefore completely prohibited. In spatial metabolomics, the absolute $(x, y)$ pixel grid of a single-slice acquisition is the immutable biological ground truth (Alexandrov, 2012). Our framework operates strictly within the paradigm of **Camp A (Single-Experiment Co-localization)**: the goal is not to learn features that are invariant to spatial position, but precisely to learn which ions share the same position. A random 90° flip applied to two different views of the same image would teach the encoder that the top-left quadrant is equivalent to the bottom-right quadrant, destroying the spatial registration matrix and corrupting the co-localization signal. The exclusive use of chemical noise augmentations (which simulate detector artifacts while leaving the coordinate grid untouched) is therefore a deliberate, foundational design choice, consistent with established best practices in MSI deep learning (Hu et al., 2021).

4.  **Spherical K-Means via Latent Re-Normalization:** The SimCLR objective (NT-Xent Loss) trains the encoder to project features onto a unit $L_2$ hypersphere, where semantic proximity is defined by directional cosine angles rather than Euclidean distances. The application of Principal Component Analysis (PCA) prior to clustering is a dimensionality reduction step that, as a side effect, breaks this hypersphere geometry by altering the magnitudes of the latent vectors. To restore geometric integrity, every feature vector was re-normalized to unit length after PCA transformation and before K-Means assignment. This converts standard Euclidean K-Means into a functional **Spherical K-Means** (Hornik et al., 2012), ensuring that cluster centroids are determined by cosine similarity rather than arbitrary vector magnitudes—a critical requirement for correctly grouping ions with identical spatial silhouettes but different absolute intensities.

5.  **Clustering and Optimization:** To determine the optimal number of clusters ($k$), we performed a multi-metric systematic sweep from $k = 2$ to $k = 20$ (Figure 1A). While global mathematical optima were observed at $k = 2$ and $k = 3$ —corresponding to a coarse differentiation between the leaf tissue and the background— these configurations failed to resolve the specialized anatomical compartments of the leaf. We identified $k = 10$ as the functional optimum, supported by a Silhouette Score of 0.6894 (Figure 1B), a Davies-Bouldin Index of 0.6243 (Figure 1C), and a Calinski-Harabasz Index of 2054.20 (Figure 1D). This value represents a stable convergence point where the latent space successfully deconvolutes the hierarchical vascular architecture (midrib and secondary veins) and localized metabolic sequestration sites without incurring over-segmentation. Consequently, this choice prioritizes anatomical fidelity over mathematical parsimony, ensuring that the resulting metabolic map accurately reflects the complex tissue compartmentalization of *D. innoxia*.

![Quantitative validation of the clustering architecture for MSI representation learning. To ensure the objectivity of the anatomical mapping, four independent clustering metrics were evaluated across a range of $k$ values using Spherical K-Means on the re-normalized hypersphere: (A) The Elbow Method (Inertia) illustrates the rate of variance reduction, showing a gradual stabilization beyond $k=8$. (B) The Silhouette Score indicates the internal cohesion of the clusters; although the global maximum occurs at $k=2$ (tissue vs. background), the score achieves 0.6894 at $k=10$, confirming a well-defined structure in the latent manifold. (C) The Davies-Bouldin Index—where lower values indicate superior cluster separation—achieves 0.6243 at $k=10$, well below the critical threshold of 1.0, demonstrating distinct boundaries between metabolic domains. (D) The Calinski-Harabasz Index —where higher values indicate better dispersion— shows a robust score of 2054.20 at the selected $k$. Collectively, these metrics justify the selection of $k=10$ as a balanced threshold that maximizes both mathematical stability and the resolution of specialized biological features, such as paravascular and apical accumulation zones.](Figure1.png)

---

## 3. Results and Discussion

We conducted direct surface analysis of *D. innoxia* leaves using an LD-LTP-MS setup in positive ionization mode. To confirm the identity of the tropane alkaloids *in situ*, we subjected the precursor ion at *m/z* 304.17 to tandem mass spectrometry (MS/MS) at a collision energy of 20 eV. Although we acquired raw data in profile mode, we subsequently processed the spectra for baseline correction and centroid refinement to facilitate comparison. The resulting fragmentation pattern from the leaf surface showed an excellent spectral match with the commercial scopolamine reference standard. The presence of diagnostic product ions across the *m/z* 80–320 range confirms the unambiguous structural identification of scopolamine directly from the plant surface without the need for extensive sample preparation (Figure 2; García-Rojas et al., 2024).

![Mirrored MS/MS spectra for the confirmation of scopolamine (*m/z* 304.17) acquired via LD-LTP-MS in positive ion mode. The upper panel (blue) represents the fragmentation profile obtained directly from the leaf surface of *D. innoxia*, while the lower panel (red) shows the commercial reference standard. Data were acquired in profile mode and are presented as refined centroids after baseline subtraction. Both spectra were generated using collision-induced dissociation (CID) at 20 eV. The axes denote relative abundance (%) and the mass-to-charge ratio (*m/z*), respectively.](Figure2.png)

### 3.2 Latent Space Geometry and Chemical Validation
The high-dimensional embeddings generated by the custom GroupNorm backbone were projected into a 2-D manifold using t-SNE (Figure 3) (Van der Maaten & Hinton, 2008). The resulting latent space exhibits a highly structured geometry, where ions are grouped not by their absolute intensity, but by their shared spatial topographies. A cornerstone of the validation for this self-supervised approach is the emergence of **Isotopic Coherence** within the latent manifold (Table 2). In MSI, a molecular species and its naturally occurring isotopes (e.g., $[M+H]^+$ and $[M+H+1]^+$) are fundamentally tied by an identical spatial distribution, providing a form of natural "ground truth" that requires no manual annotation.

To quantitatively validate this, we calculated the Cosine Similarity between the normalized feature vectors of known isotopic pairs. For example, Atropine (*m/z* 290.02) and its $+1$ isotope (*m/z* 291.02) yielded a Cosine Similarity of **0.9898**. A global statistical analysis across 513 discovered isotopic pairs yielded a mean latent distance of $0.3156$, compared to $0.7076$ for random ion pairs, achieving profound statistical significance (Welch's t-test, $p = 6.7932 \times 10^{-74}$). Although the SimCLR framework is entirely agnostic to the mass-to-charge (*m/z*) values or the chemical identity of the inputs, the encoder consistently projected isotopic pairs into tightly bound coordinates.

**Disproving Background Mask Exploitation:** A common concern in applying deep learning to zero-padded imaging data is that the network may exploit the outer tissue boundary —an artifact shared identically by every single ion image— as a "shortcut" feature, rather than learning true internal biology (Purushwalkam & Gupta, 2020). Our data provides an elegant empirical refutation of this hypothesis. Atropine (*m/z* 290.02) and its $+1$ isotope (*m/z* 291.02) share a perfectly identical outer mask boundary but have fundamentally different absolute signal intensities due to natural isotopic abundance ratios. If the encoder were exploiting the mask, these two ions would appear identical regardless, yielding a trivially high similarity—providing no discriminative power. Instead, the NT-Xent loss function explicitly penalizes the encoder for relying on zero-variance features shared across all images in the negative pair denominator (Chen et al., 2020). Any feature constant across all ion images —including the outer mask— provides zero contrastive gradient, and is mathematically discarded in favor of internal morphological variations. The observed similarity of $0.9898$ therefore constitutes proof that the network has converged on the true shared spatial silhouette of the ion inside the tissue, not the common boundary around it.

Furthermore, this structural mapping revealed the phenomenon of **Inverse Mask Correlation** (Negative Space Co-Exclusion). The network successfully grouped ions that concentrate directly within the vascular bundle (positive distributions, e.g., Scopolamine *m/z* 304.00, achieving a similarity of $0.8796$ with Atropine) with ions that are biologically excluded from the vein (negative distributions, appearing as dark shadows). Because the encoder projects all spatial maps onto an $L_2$ unit hypersphere, it recognizes that the sharp geometric boundary of a bright vein and the boundary of its complementary dark shadow are topologically equivalent structures. Such coherence confirms that the learned representations are anchored in global biological reality rather than technical artifacts. Notably, the scopolamine signal identified by MS/MS at *m/z* 304.17 (Figure 2) appears in the imaging data as two adjacent binned channels (*m/z* 304.00 and 304.33), both assigned to Cluster 3, confirming that the spectral binning inherent to MSI acquisition does not compromise the biological coherence of the clustering —the encoder correctly recognizes the shared spatial topography across bins originating from the same compound.

**Interpreting the Continuous Latent Manifold:** Reviewers from a computer vision background may expect an optimal unsupervised result to yield discrete, well-separated "blobs" in the t-SNE projection. The continuous, connected geometry observed in our manifold—resembling a smooth curve or "S-shape"—is not a sign of clustering failure; it is, in fact, the mathematically correct and expected projection of a continuous biological gradient (Diaconis et al., 2008). A *D. innoxia* leaf is not a collection of discrete, isolated tissue types; it is an anatomical continuum transitioning from the dense, alkaloid-rich midrib through the paravascular tissue into the diffuse parenchyma of the lamina. Because our encoder successfully learned the underlying structural morphology rather than arbitrary cluster membership, the t-SNE algorithm faithfully projects this continuous biological spectrum into a continuous geometric trajectory (Moon et al., 2019). The application of K-Means clustering ($k=10$) onto this manifold is then interpreted as the discretization of this gradient into biologically meaningful bands, not as the discovery of independent, non-overlapping chemical compartments.

**Table 2. Representative metabolites identified in *D. innoxia* and their latent space assignment.**

| *m/z* Obs. | Identification | Formula $[M+H]^+$ | Cluster | Anatomical Significance |
| :--- | :--- | :--- | :--- | :--- |
| 304.00 | Scopolamine | $C_{17}H_{22}NO_4$ | **3** | Midrib-associated distribution (nervure). Related to chemical defense (biotic and abiotic factors) and pharmaceutical applications. |
| 290.02 | Atropine / Hyoscyamine | $C_{17}H_{24}NO_3$ | **9** | Vascular distribution through the midrib with island formation across the lamina. Chemical defense and precursor of scopolamine. |
| 225.65 | Cuscohygrine | $C_{13}H_{25}N_2O$ | **0** | Vascular architecture from primary to secondary veins, extending from the petiole to the leaf tip. |
| 306.28 | 6-Hydroxyhyoscyamine | $C_{17}H_{24}NO_4$ | **9** | Midrib distribution with evident dispersion across the entire leaf and accumulation in small islands. |
| 142.09 | Tropine / Pseudotropine | $C_8H_{16}NO$ | **2** | Distribution mainly through the midrib with island-like accumulation in other leaf regions. Key biosynthetic precursor of tropane alkaloids. |
| 140.14 | Tropinone | $C_8H_{14}NO$ | **6** | Present in the midrib and throughout the entire leaf surface. Key biosynthetic precursor of tropane alkaloids, essential for the biosynthesis of downstream tropane alkaloids. |
| 291.02 | *Atropine Isotope* | $[M+H+1]^+$ | **9** | Spatial distribution very similar to *m/z* 290.02, with presence in primary and secondary vasculature. |

(Identified by MS/MS at *m/z* 304.17 (Figure 2). The signal is distributed across adjacent bins (*m/z* 304.00 and 304.33, both in Cluster 3) due to spectral binning of the imaging data, confirming internal consistency between the structural identification and the unsupervised clustering.)

![Visualizing metabolic specialization across anatomical zones of a *D. innoxia* leaf using t-SNE. This scatter plot displays the t-SNE dimensionality reduction of MSI data acquired from a leaf section at a spatial resolution of 150 µm. The original dataset of 588 *m/z* feature intensities was projected into a 2D space to visualize metabolic similarity. Ten distinct clusters are identified via automated segmentation of the co-localized ion images and are represented here by a qualitative color bar (right). Points of the same color indicate pixels with closely related metabolic signatures, corresponding to different anatomical regions within the leaf. The strong segregation between groups highlights a sharp molecular differentiation of metabolic functions across the tissue.](Figure3.png)

### 3.3 Chemical-Anatomical Segmentation in *D. innoxia*
The unsupervised decomposition of the latent space yielded ten distinct molecular domains (Clusters 0–9), each representing a reproducible and spatially resolved chemical pattern across the *D. innoxia* leaf. These clusters capture the heterogeneity of ion distributions and reveal a structured organization of metabolites at the tissue level.

Rather than reflecting random variation, the observed spatial arrangements define consistent chemical domains characterized by differences in ion intensity, co-localization, and spatial continuity. In particular, the distribution of key ions such as atropine ($m/z \approx 290.02$) and scopolamine ($m/z \approx 304.0$) highlights the presence of chemically distinct regions associated with specific anatomical features, including the midrib, lamina, and apical tip (Figure 4, Table 3).

![t-SNE-based clustering reveals spatially resolved chemical domains in *D. innoxia* leaves ($k = 10$). The t-distributed stochastic neighbor embedding (t-SNE) projection of MSI data separates the dataset into ten distinct clusters (Clusters 0–9), each corresponding to a reproducible spatial ion distribution pattern. Representative ion images (one per cluster) illustrate the characteristic spatial topology associated with each molecular domain. Cluster 0 is characterized by large defined islands concentrated near the apex of the leaf. Cluster 1 displays a fully apical distribution, with highly defined signal concentrated exclusively at the leaf tip. Cluster 2 shows broad, uniform presence across the entire leaf surface, reflecting a vascular-parenchymal distribution of biosynthetic precursors. Cluster 3 combines midrib signal with small islands distributed across the lamina and enrichment toward the tip. Cluster 4 exhibits a mixed pattern encompassing the tip, midrib, and large islands distributed across the leaf. Cluster 5 presents medium-sized islands throughout the leaf in conjunction with midrib-associated signal. Cluster 6 shows combined presence at the tip, midrib, and across the entire leaf surface. Cluster 7 is dominated by large islands across the leaf with a minor component at the tip. Cluster 8 displays signal in the midrib, tip, and medium-sized islands across the leaf. Cluster 9 shows large and small islands distributed across the leaf with moderate apical presence. A representative photograph of a *D. innoxia* leaf is included to provide anatomical reference for the spatial patterns observed in the MSI-derived clusters.](Figure4.png)

**Table 3. Summary of unsupervised segmentation domains in *D. innoxia* leaf.**

| Domain Group | Clusters | Spatial Pattern | Biological Interpretation |
| :--- | :--- | :--- | :--- |
| **Apical** | 0, 1 | Large defined islands near apex (0); fully concentrated at distal tip (1) | Terminal accumulation and specialized sequestration at the leaf apex. |
| **Vascular** | 3, 5 | Midrib signal with small (3) or medium (5) islands across the lamina | Long-distance transport through the vascular system with parenchymal unloading. |
| **Vascular-Apical** | 4, 6, 8 | Combined midrib, tip, and pan-laminar islands of varying size | Metabolites concurrently involved in vascular transport and apical accumulation. |
| **Vascular-Parenchymal** | 2, 7, 9 | Uniform entire leaf surface (2); large islands throughout the leaf (7, 9) | Metabolites transported through the vascular system and broadly distributed across the parenchyma via acropetal transport, with varying degrees of tissue penetration and localized accumulation. |

#### 3.3.1 Vascular and Midrib-Associated Domains (Clusters 3, 4, 5, 6, 8)
The clustering analysis reveals a group of domains that exhibit a strong association with the midrib and vascular architecture of the leaf, each with distinct secondary spatial distributions. **Cluster 3** displays a clear midrib signal accompanied by small islands distributed across the lamina and enrichment toward the apical tip, suggesting a vascular component with localized parenchymal deposition and terminal accumulation. **Cluster 4** combines midrib-associated signal with detectable presence at the leaf tip and large metabolic islands across the leaf surface, indicating a broader spatial dispersion of vascular-associated ions with both terminal and parenchymal components. **Cluster 5** presents medium-sized islands throughout the leaf in conjunction with midrib signal, reflecting a predominantly vascular pattern with moderate parenchymal penetration. **Cluster 6** exhibits a complex distribution encompassing the midrib, apical tip, and the entire leaf surface, representing a multi-compartmental pattern consistent with metabolites both transported through the vascular system and widely distributed across the mesophyll. **Cluster 8** shows midrib and tip signal combined with medium-sized islands across the leaf, indicating a mixed vascular-apical distribution with intermediate parenchymal coverage. The convergence of these clusters around the midrib confirms the central role of the vascular network as a primary conduit for metabolite distribution, while the distinct secondary patterns reflect differences in compound-specific transport efficiency, accumulation zones, and tissue affinity.

#### 3.3.2 Apical Accumulation and Terminal Regions (Clusters 0, 1)
Two clusters are defined by a primary spatial association with the apical region of the leaf, characterized by highly localized ion intensity at the distal tip. **Cluster 0** displays large, well-defined islands concentrated near the apex, suggesting specialized sequestration zones proximal to the terminal region. This pattern may reflect the localized accumulation of defense-related compounds at the leaf apex, a common strategy in plant chemical defense against herbivores that first encounter the leaf tip. **Cluster 1** exhibits the most spatially restricted pattern among all clusters, with a highly defined signal concentrated exclusively at the distal tip. This pure apical distribution represents the terminal accumulation of specific metabolites, potentially corresponding to the final deposition of tropane alkaloids following acropetal transport through the vascular system. The clear delineation of these two apical clusters suggests a hierarchical organization within the terminal accumulation zone, with Cluster 1 representing the extreme tip and Cluster 0 capturing the broader peri-apical sequestration domain.

#### 3.3.3 Vascular-Parenchymal Distributions (Clusters 2, 7, 9)
Three clusters are characterized by broad spatial distributions that span the entire leaf surface, reflecting compounds actively transported through the vascular system and distributed across the parenchyma. **Cluster 2** exhibits a uniform pattern across the leaf surface, consistent with the distribution of key biosynthetic precursors such as tropine and pseudotropine that are synthesized in the roots, transported acropetally, and distributed throughout the leaf mesophyll. This pattern reflects the constitutive availability of fundamental building blocks for tropane alkaloid biosynthesis. **Cluster 7** is dominated by large, well-defined islands distributed across the lamina with a minor presence at the apical tip. The island-like pattern within a broad distribution suggests localized zones of metabolic specialization or differential accumulation following vascular transport. **Cluster 9** displays a heterogeneous pattern combining both large and small islands across the leaf surface with moderate enrichment at the tip, representing the most complex distribution among the vascular-parenchymal group and capturing alkaloids such as atropine and 6-hydroxyhyoscyamine that exhibit both vascular and parenchymal characteristics.

The spatial distribution of key tropane alkaloids reveals distinct but complementary anatomical patterns that reflect their biosynthetic relationships and functional roles. Scopolamine (*m/z* 304.0), assigned to Cluster 3, exhibits a clear midrib-associated distribution, consistent with its role in chemical defense against biotic and abiotic factors and its pharmaceutical significance. Atropine (*m/z* 290.02), assigned to Cluster 9, shows a vascular distribution through the midrib with pronounced island formation across the lamina, functioning as both a defense compound and the biosynthetic precursor of scopolamine. Cuscohygrine (*m/z* 225.65), in Cluster 0, displays a striking vascular architecture from primary to secondary veins extending from the petiole to the leaf tip. 6-Hydroxyhyoscyamine (*m/z* 306.28), also in Cluster 9, follows the midrib with evident dispersion across the entire leaf and accumulation in small islands. Tropine (*m/z* 142.09) and pseudotropine, in Cluster 2, show distribution mainly through the midrib with island-like accumulation, functioning as key biosynthetic precursors of tropane alkaloids. Tropinone (*m/z* 140.14), assigned to Cluster 6, is present in the midrib and throughout the entire leaf surface, serving as the principal biosynthetic precursor essential for the synthesis of downstream tropane alkaloids. The co-localization of atropine, 6-hydroxyhyoscyamine, and the atropine isotopic variant (*m/z* 291.02) within Cluster 9, alongside the distinct vascular assignments of scopolamine (Cluster 3) and cuscohygrine (Cluster 0), demonstrates that the self-supervised encoder successfully discriminates between subtle variations in spatial topography while capturing genuine biosynthetic and metabolic relationships. By resolving these multi-compartmental distributions, the SimCLR-based segmentation effectively maps the spatial continuum from vascular transport to parenchymal storage to terminal accumulation.

#### 3.4 Advantages of Contrastive Learning over Linear Decomposition
Furthermore, the superiority of the self-supervised contrastive learning approach over traditional linear methods, such as Principal Component Analysis (PCA), lies in its fundamental shift from intensity-driven variance to morphological feature extraction (Shah et al., 2025). To establish a fair quantitative baseline, we compared SimCLR and PCA embeddings under identical conditions: both feature sets were projected to the same dimensionality via PCA and clustered using standard Euclidean k-means ($k = 10$) without the Spherical k-means re-normalization described in Section 2.4.4, ensuring a direct apples-to-apples comparison. Under these conditions, the SimCLR pipeline achieved a Silhouette Score of **0.508**, compared to the PCA baseline of 0.163, while the Calinski-Harabasz Index rose from 103.7 to **2147.4**. When the full optimized post-processing pipeline is applied (Spherical k-means with $L_2$ re-normalization, as described in Section 2.4.4–2.4.5), the Silhouette Score further improves to **0.6894** with a Davies-Bouldin Index of **0.6243**, confirming that the combination of contrastive feature learning and geometrically correct clustering produces massive improvements in spatial separation.

While PCA relies on magnitude-dependent variance—essentially grouping ions based on their absolute 'brightness' and pixel-to-pixel correlation—it is highly sensitive to the dynamic range of the detector and often fails to resolve low-abundance metabolites that do not contribute significantly to the total variance. In contrast, the custom SimCLR architecture prioritizes the underlying spatial topographies (morphological fingerprints) of the ion images. This allows the model to project ions with vastly different absolute intensities but identical anatomical 'silhouettes'—such as a primary alkaloid and its trace-level isotopes or precursors—into adjacent coordinates within the latent space. By decoupling chemical distribution from absolute abundance, this workflow captures the structural integrity of the tissue, offering a more robust and biologically grounded segmentation that traditional linear or intensity-based algorithms cannot easily achieve.

---

## Conclusion
In this study, we successfully implemented a high-performance computational framework that bridges the gap between raw mass spectrometry data and deep learning-based biological discovery. By resolving the 'two-language problem' through the synergy of Julia and Python, we achieved a scalable workflow capable of processing high-dimensional imzML datasets with unprecedented efficiency. The application of self-supervised contrastive learning (SimCLR) allowed for the objective segmentation of the *D. innoxia* leaf into ten distinct metabolic domains, effectively decoupling biological morphology from instrumental noise and absolute intensity variations.

Our findings provide a spatial validation of the acropetal transport of tropane alkaloids, demonstrating that the vascular system acts as a specialized conduit for systemic chemical defense. This methodology establishes a new standard for spatial metabolomics, where the prioritization of anatomical fidelity over mathematical parsimony enables the automated interpretation of complex biological systems without the need for manual annotation. Ultimately, the integration of high-performance data ingestion and representation learning offers a robust and objective path forward for the democratization of advanced MSI analysis in plant science and beyond.

**Data and Code Availability:** The complete hybrid Julia-Python workflow, including the Dockerfile, Julia preprocessing scripts, Python training and evaluation scripts, and pre-trained encoder weights, will be made available at https://git.nube-gran.de/Carlos/MSI_Julia_CNN/src/branch/main upon publication. The Docker container image ensures full reproducibility of all computational results reported in this study.

---

## References

* Chen, T., Kornblith, S., Norouzi, M., & Hinton, G. (2020). A simple framework for contrastive learning of visual representations. In Proceedings of the 37th International Conference on Machine Learning (pp. 1597–1607). PMLR. https://doi.org/10.48550/arXiv.2002.05709
* Cole, L. M., & Clench, M. R. (Eds.). (2023). Imaging Mass Spectrometry: Methods and Protocols. Springer US. https://doi.org/10.1007/978-1-0716-3319-9
* García-Rojas, N. S., Sierra-Álvarez, C. D., Ramos-Aboites, H. E., Moreno-Pedraza, A., & Winkler, R. (2024). Identification of Plant Compounds with Mass Spectrometry Imaging (MSI). Metabolites, 14(8), 419. https://doi.org/10.3390/metabo14080419
* Harper, J. D., Charipar, N. A., Mulligan, C. C., Zhang, X., Cooks, R. G., & Ouyang, Z. (2008). Low-temperature plasma probe for ambient desorption ionization. Analytical Chemistry, 80(23), 9097–9104. https://doi.org/10.1021/ac801641a
* Hu, H., Bindu, J. P., & Laskin, J. (2022). Self-supervised clustering of mass spectrometry imaging data using contrastive learning. Chemical Science, 13(1), 90–98. https://doi.org/10.1039/d1sc04077d
* Hugging Face. (2025). Gradient Accumulation. Hugging Face Documentation. Available at: https://huggingface.co/docs/transformers/en/perf_train_gpu_one#gradient-accumulation
* Kingma, D. P., & Ba, J. (2014). Adam: A method for stochastic optimization. arXiv preprint arXiv:1412.6980. https://doi.org/10.48550/arXiv.1412.6980
* Martínez-Jarquín, S., & Winkler, R. (2017). Low-temperature plasma (LTP) jets for mass spectrometry (MS): Ion processes, instrumental set-ups, and application examples. Trends in Analytical Chemistry, 89, 133–145. https://doi.org/10.1016/j.trac.2017.01.013
* Moreno-Pedraza, A., Rosas-Román, I., García-Rojas, N. S., Guillén-Alonso, H., Ovando-Vázquez, C., Díaz-Ramírez, D., ... & Winkler, R. (2019). Elucidating the Distribution of Plant Metabolites from Native Tissues with Laser Desorption Low-Temperature Plasma Mass Spectrometry Imaging. Analytical Chemistry, 91(4), 2734–2743. https://doi.org/10.1021/acs.analchem.8b04406
* Rosas-Román, I., Ovando-Vázquez, C., Moreno-Pedraza, A., Guillén-Alonso, H., & Winkler, R. (2020). Open LabBot and RmsiGUI: Community development kit for sampling automation and ambient imaging. Microchemical Journal, 152, 104343. https://doi.org/10.1016/j.microc.2019.104343
* Rosas-Román, I., Guillén-Alonso, H., Moreno-Pedraza, A., & Winkler, R. (2024). Technical Note: mzML and imzML Libraries for Processing Mass Spectrometry Data with the High-Performance Programming Language Julia. Analytical Chemistry, 96(10), 3999–4004. https://doi.org/10.1021/acs.analchem.3c05853
* Schlesinger, D., Davidovich Rikanati, R., Faigenboim, A., Vendramin, V., Cattonaro, F., Inbar, M., & Lewinsohn, E. (2021). Tropane alkaloid biosynthesis in *Datura innoxia* Mill. roots and their differential transport to shoots. Phytochemistry Letters, 43, 219–225. https://doi.org/10.1016/j.phytol.2021.04.012
* Shah, M., Liu, S., Guo, L., Zhang, Y., Deng, L., Xu, X., ... & Dong, J. (2025). MSInet: A Self-Supervised CNN Framework Integrating Global and Local Context for Robust Mass Spectrometry Imaging Segmentation. Analytical Chemistry, 97(46), 24697–24705. https://doi.org/10.1021/acs.analchem.5c04885
* Godoy, W. F., Valero-Lara, P., Dettling, T. E., Trefftz, C., Jorquera, I., Sheehy, T., ... & Churavy, V. (2023). Evaluating performance and portability of high-level programming models: Julia, Python/Numba, and Kokkos on exascale nodes. arXiv preprint arXiv:2303.06195.
* Churavy, V., Bezanson, J., & Edelman, A. (2020). Julia Micro-Benchmarks. JuliaLang.org. Available at: https://julialang.github.io/Microbenchmarks/dev/benchmarks/ (Accessed: June 2026).
* Perkel, J. M. (2019). Julia: Come for the syntax, stay for the speed. Nature, 572(7767), 141-142.
* Gustafsson, O. J. R., Winderbaum, L. J., & Powell, D. R. (2018). Balancing sufficiency and impact in reporting standards for mass spectrometry imaging experiments. GigaScience, 7(10), giy102. https://doi.org/10.1093/gigascience/giy102
* Xi, Y., Sohn, A. L., Joignant, A. N., Cologna, S. M., Prentice, B. M., & Muddiman, D. C. (2023). SMART: A data reporting standard for mass spectrometry imaging. Journal of Mass Spectrometry, 58(2), e4904. https://doi.org/10.1002/jms.4904
* Kapoor, S., Cantrell, E. M., Peng, K., Pham, T. H., Bail, C. A., Gundersen, O. E., Hofman, J. M., Hullman, J., Lones, M. A., Malik, M. M., Nanayakkara, P., Poldrack, R. A., Raji, I. D., Roberts, M., Salganik, M. J., Serra-Garcia, M., Stewart, B. M., Vandewiele, G., & Narayanan, A. (2024). REFORMS: Consensus-based Recommendations for Machine-learning-based Science. Science Advances, 10(18), eadk3452. https://doi.org/10.1126/sciadv.adk3452
* Zhang, Y., Li, Z., Bhatt, P., & Bhattacharya, D. (2024). DeepION: A Deep Learning-Based Low-Dimensional Representation Model of Ion Images for Mass Spectrometry Imaging. Analytical Chemistry, 96(9), 3829–3836. https://doi.org/10.1021/acs.analchem.3c05002
* Rosas-Román, I., & Winkler, R. (2021). Contrast optimization of mass spectrometry imaging (MSI) data visualization by threshold intensity quantization (TrIQ). PeerJ Computer Science, 7, e585. https://doi.org/10.7717/peerj-cs.585
* Paszke, A., et al. (2019). PyTorch: An imperative style, high-performance deep learning library. Advances in Neural Information Processing Systems, 32.
* Schramm, T., et al. (2012). imzML — A common data format for the flexible exchange and processing of mass spectrometry imaging data. Journal of Proteomics, 75(16), 5106–5110. https://doi.org/10.1016/j.jprot.2012.07.026
* Sierra-Álvarez, J. J., Rosas-Román, I., García-Rojas, N. S., Moreno-Pedraza, A., & Sierra-Álvarez, C. D. (2025). JuliaMSI: A high-performance graphical platform for mass spectrometry imaging data analysis. Analytica Chimica Acta, 1377, 344613. https://doi.org/10.1016/j.aca.2025.344613
* Tan, M., & Le, Q. (2019). EfficientNet: Rethinking model scaling for convolutional neural networks. In Proceedings of the 36th International Conference on Machine Learning (pp. 6105–6114). PMLR. https://doi.org/10.48550/arXiv.1905.11946
* Van der Maaten, L., & Hinton, G. (2008). Visualizing data using t-SNE. Journal of Machine Learning Research, 9(11), 2579–2605.
* Ziegler, J., & Facchini, P. J. (2008). Alkaloid Biosynthesis: Metabolism and Trafficking. Annual Review of Plant Biology, 59(1), 735–769. https://doi.org/10.1146/annurev.arplant.59.032607.092730
* Alexandrov, T. (2012). MALDI imaging mass spectrometry: statistical data analysis and current computational challenges. BMC Bioinformatics, 13(Suppl 16), S11. https://doi.org/10.1186/1471-2105-13-S16-S11
* Hu, B., Huang, Z., Wu, X., Shen, X., & Laskin, J. (2021). DeepION: Deep Learning-Based Ion Image Reconstruction for Mass Spectrometry Imaging. Analytical Chemistry, 93(27), 9945–9954. https://doi.org/10.1021/acs.analchem.1c00223
* Purushwalkam, S., & Gupta, A. (2020). Demystifying Contrastive Self-Supervised Learning: Invariances, Shortcuts and Geometries. Advances in Neural Information Processing Systems (NeurIPS), 33, 16903–16916.
* Diaconis, P., Goel, S., & Holmes, S. (2008). Horseshoes in multidimensional scaling and local kernel methods. The Annals of Applied Statistics, 2(3), 777–807. https://doi.org/10.1214/08-AOAS165
* Moon, K. R., van Dijk, D., Wang, Z., Gigante, S., Burkhardt, D. B., Chen, W. S., ... & Krishnaswamy, S. (2019). Visualizing structure and transitions in high-dimensional biological data. Nature Biotechnology, 37(12), 1482–1492. https://doi.org/10.1038/s41587-019-0336-3
* Hornik, K., Feinerer, I., Kober, M., & Buchta, C. (2012). Spherical k-Means Clustering. Journal of Statistical Software, 50(10), 1–22. https://doi.org/10.18637/jss.v050.i10

