refractored file structure of the project, docker scripts built for python and julia, improved documentation

This commit is contained in:
Pixelguy14
2026-06-02 17:40:59 -06:00
parent 35621b7cf1
commit 3143e29843
36 changed files with 741 additions and 138 deletions
+90 -27
View File
@@ -5,44 +5,107 @@ This repository contains the computational pipeline for the automated anatomical
## 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.
- **Empirical Clustering**: Automated anatomical segmentation using PCA and K-Means, optimized for vascular and parenchymal tissue differentiation.
- **CPU Optimized**: Designed to run on standard consumer hardware (no GPU required for inference).
- **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, TrIQ normalization, and image generation.
- `scripts_julia/`: Julia scripts for mass slice extraction and TrIQ normalization.
- `scripts_python/`: Python scripts for SimCLR training, feature extraction, and clustering.
- `Model/`: Contains the pre-trained weights (`msi_encoder_trained.pth`).
- `environment/`: Dependency list (`requirements.txt`).
- `Articule MSI/`: Manuscript draft and documentation.
- `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.
## Installation
## Reproducibility & Docker (Recommended)
To ensure identical environments for both Julia and Python, we provide a unified Docker setup. This setup includes the **JuliaMSI** framework.
### Python
It is recommended to use a virtual environment:
### 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
pip install -r environment/requirements.txt
docker build -t msi-hybrid-workflow .
```
### Julia
Ensure you have Julia installed and the following packages: `MSI.jl`, `ImzML.jl`, `Images.jl`, and `DataFrames.jl`.
### 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)**:
Run `scripts_julia/preparar_datos_simclr.jl` to convert `.imzML` data into normalized PNG ion maps.
2. **Training (Optional)**:
Run `scripts_python/CNN_proof_1.py` to retrain the SimCLR encoder.
3. **Clustering & Mapping**:
Use `scripts_python/cluster_msi_direct.py` to extract embeddings from the pre-trained model and generate the metabolic clusters.
4. **Visualization**:
Run `scripts_python/visualizar_clusters.py` to generate the final cluster galleries.
### 1. Preprocessing (Julia)
Leverage Julia's performance and the **JuliaMSI** framework.
## Citation & Acknowledgments
This implementation is based on the methodology proposed by:
> 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. DOI: [10.1039/d1sc04077d](https://doi.org/10.1039/d1sc04077d)
**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** - see the [LICENSE](LICENSE) file for details.
This project is licensed under the **MIT License**.