Files
MSI_Julia_CNN_Release/README.md
T

112 lines
4.8 KiB
Markdown

# Deep Learning in Mass Spectrometry Imaging: A Hybrid Julia-Python Workflow
This repository contains the computational pipeline for the automated anatomical mapping of *Datura innoxia* (toloache) using Mass Spectrometry Imaging (MSI). The workflow addresses the "two-language problem" by leveraging **Julia** for high-performance data preprocessing and **Python** for self-supervised representation learning.
## Key Features
- **High-Performance Preprocessing**: Rapid `.imzML` ingestion and TrIQ normalization using Julia.
- **Self-Supervised Learning**: Contrastive learning (SimCLR) with an EfficientNet-B0 backbone implemented in PyTorch.
- **Reproducibility**: Integrated Docker environment and fixed random seeds for consistent results.
- **Performance Benchmarking**: Real-time monitoring of execution time and RAM usage across the hybrid pipeline.
- **CUDA/GPU Support**: Optimized for NVIDIA hardware for accelerated training and inference.
- **Hybrid Bridge**: Automated data transfer between Julia and Python ecosystems via standardized intermediate files.
## Project Structure
- `scripts_julia/`: Julia scripts for mass slice extraction and TrIQ normalization.
- `scripts_python/`: Python scripts for SimCLR training, feature extraction, and clustering.
- `models/`: Contains the pre-trained weights (`msi_encoder_trained.pth`).
- `environment/`: Dependency files (`requirements.txt`, `Project.toml`).
- `figures/`: Generated plots and cluster galleries.
- `data/`: (User-provided) Location for `.imzML` and mask files.
## Reproducibility & Docker (Recommended)
To ensure identical environments for both Julia and Python, we provide a unified Docker setup. This setup includes the **JuliaMSI** framework.
### Building the Container
Before building, ensure the `JuliaMSI` repository is present in the root directory (it was cloned from `https://git.nube-gran.de/Julian/JuliaMSI`).
```bash
docker build -t msi-hybrid-workflow .
```
### Running the Container (CPU Mode)
```bash
docker run -it --rm -v $(pwd):/app msi-hybrid-workflow
```
### Running the Container (GPU/CUDA Mode)
```bash
docker run -it --rm --gpus all -v $(pwd):/app msi-hybrid-workflow
```
## Usage Workflow
### 1. Preprocessing (Julia)
Leverage Julia's performance and the **JuliaMSI** framework.
**Run the MSI Bridge (Normalization & PNG Export):**
```bash
docker run -it --rm -v $(pwd):/app msi-hybrid-workflow \
julia --threads auto --project=/app/environment scripts_julia/julia_msi_bridge.jl
```
**Optional: Plot Cluster Spectra:**
```bash
docker run -it --rm -v $(pwd):/app msi-hybrid-workflow \
julia --threads auto --project=/app/environment scripts_julia/graph_cluster_spectra.jl
```
### 2. Representation Learning (Python)
Train the SimCLR encoder to extract morphological features using CUDA.
**Run CNN Training (SimCLR):**
```bash
docker run -it --rm --gpus all -v $(pwd):/app msi-hybrid-workflow \
python3 scripts_python/CNN_proof_1.py
```
*Note: The script automatically detects CUDA and logs per-epoch RAM usage.*
### 3. Clustering & Validation
Determine optimal clusters and perform quantitative validation.
**Evaluate Clustering:**
```bash
docker run -it --rm --gpus all -v $(pwd):/app msi-hybrid-workflow \
python3 scripts_python/evaluate_clustering.py
```
**Quantitative Validation:**
```bash
docker run -it --rm --gpus all -v $(pwd):/app msi-hybrid-workflow \
python3 scripts_python/quantitative_validation.py
```
### 4. Visualization
Generate the final galleries and t-SNE maps.
**Generate t-SNE Maps:**
```bash
docker run -it --rm --gpus all -v $(pwd):/app msi-hybrid-workflow \
python3 scripts_python/graficar_tsne_msi.py
```
**Create Cluster Galleries:**
```bash
docker run -it --rm --gpus all -v $(pwd):/app msi-hybrid-workflow \
python3 scripts_python/visualize_clusters.py
```
## Benchmarking & Hardware Realism
The pipeline is optimized for efficiency on consumer-grade hardware.
- **Julia Stage**: Uses `BenchmarkTools.jl` to profile binary parsing.
- **Python Stage**: Uses `psutil` and `CUDA` monitoring to track hardware overhead.
- **Determinism**: All stochastic steps are locked with `seed=42`.
## Julia-Python Bridge Details
The "two-language problem" is solved by an intermediate file-system bridge. Julia performs binary MSI parsing and **Threshold Intensity Quantization (TrIQ)**. The resulting data is saved as high-contrast 224x224 grayscale PNG images, allowing the Python PyTorch pipeline to consume it efficiently.
## Citation
If you use this workflow or the JuliaMSI framework, please cite:
> Sierra-Álvarez, J. J., et al. (2025). JuliaMSI: A high-performance graphical platform for mass spectrometry imaging data analysis. *Analytica Chimica Acta*, 1377, 344613. DOI: [10.1016/j.aca.2025.344613](https://doi.org/10.1016/j.aca.2025.344613)
## License
This project is licensed under the **MIT License**.