Build, validate, and deploy predictive models from NIR spectroscopy data.
40+ algorithms • full chemometrics pipeline • one-click model export • runs 100% locally
An educational, open-source web application for near-infrared (NIR) spectroscopy. Upload your spectral data, walk through a guided pipeline -- preprocessing, exploration, variable selection, calibration, validation -- then export a trained model and standalone prediction script ready to deploy on embedded devices, lab instruments, or production lines.
No cloud. No accounts. Everything runs on your machine.
git clone <repo-url>
cd spectral_processing
python -m venv venv
venv\Scripts\activate # Windows
# source venv/bin/activate # Linux / macOS
pip install -r requirements.txt
python run.pyOpen http://localhost:5000 and start building models.
Upload Preprocess Explore Select Model Validate Export
────── ────────── ─────── ────── ───── ──────── ──────
CSV ──► SNV, MSC, ──► PCA, HCA, ──► SPA, GA, ──► PLS, SVR, ──► K-Fold, ──► .pkl
X & Y SG, deriv, ICA, SOM, CARS, iPLS CNN, SVM, LOO, perm script
baseline MCR-ALS LASSO RF, kNN test, T² config
| Stage | Methods |
|---|---|
| Preprocessing | Savitzky-Golay • 1st/2nd derivatives • SNV • MSC • baseline correction (AsLS, poly, rubberband) • detrend • normalization • wavelet denoising • OSC • cosmic ray removal |
| Exploration | PCA • HCA • ICA • MCR-ALS • CLS • Self-Organizing Maps • PARAFAC / Tucker3 |
| Variable Selection | SPA • iPLS • Genetic Algorithm • CARS • LASSO • Forward Stepwise • Correlation / MI filters |
| Regression | MLR • PCR • PLS • SVR • LWR • ANN/MLP • Ridge • LASSO • Elastic Net • Kernel PLS • 1D CNN |
| Classification | LDA • PLS-DA • OPLS-DA • SIMCA • kNN • SVM • Random Forest • Gradient Boosting • Logistic Regression • Naive Bayes • Mahalanobis |
| Validation | K-Fold • LOO • Venetian Blinds • Repeated K-Fold • Permutation test • Hotelling T² • Q-residuals |
| Metrics | RMSEP • R² • Bias • RPD • RER • Accuracy • Precision • Recall • F1 • Confusion Matrix |
After training and validating, click Export to download a deployment package (.zip):
| File | Purpose |
|---|---|
regression_model.pkl |
Trained model serialized with joblib |
predict_regression.py |
Self-contained prediction script -- preprocessing, variable selection, and inference are all embedded |
model_config.json |
Full config: preprocessing steps, wavelength indices, metrics |
README.txt |
Quick-start instructions |
pip install numpy pandas scikit-learn joblib scipy
python predict_regression.py unknown_spectra.csv
# => writes unknown_spectra_predictions.csv- Embedded devices -- Raspberry Pi, Jetson Nano, industrial PCs
- Lab instruments -- NIR sensors with a Python runtime
- Production QC -- real-time or batch inline predictions
- Edge / cloud -- integrate into existing data pipelines
- Offline environments -- no internet required after export
Spectral data and reference values are uploaded as separate CSV files.
Spectral data (X):
900,902,904,...,1700
0.453,0.461,0.472,...,0.321
0.412,0.418,0.425,...,0.298
- Column headers = wavelength values (numeric)
- Rows = samples
Reference values (Y):
moisture
12.5
8.3
- Single column, descriptive header
- One value per sample, matching row order
Included in data/sample/ for one-click loading:
| File | Contents |
|---|---|
nir_regression_spectra.csv |
80 samples × 400 wavelengths |
nir_regression_reference.csv |
Moisture reference values |
nir_classification_spectra.csv |
90 samples × 400 wavelengths |
nir_classification_reference.csv |
3 material class labels |
| Category | Packages |
|---|---|
| Web | Flask, Flask-Session |
| Core | NumPy, SciPy, Pandas, scikit-learn, joblib |
| Visualization | Plotly, Matplotlib |
| Deep Learning | PyTorch |
| Specialty | PyBaselines, PyWavelets, TensorLy, MiniSom, DEAP |
spectral_processing/
├── app/
│ ├── __init__.py # Flask app factory
│ ├── config.py # Configuration
│ ├── session_manager.py # Server-side session state
│ ├── routes/
│ │ ├── main.py # Upload, sample data, session
│ │ ├── preprocessing.py # Preprocessing pipeline
│ │ ├── exploratory.py # PCA, HCA, ICA, etc.
│ │ ├── variable_selection.py
│ │ ├── regression.py # Quantitative calibration
│ │ ├── classification.py # Supervised discrimination
│ │ ├── validation.py # Cross-validation & diagnostics
│ │ └── export.py # Model download & script gen
│ ├── processing/ # Algorithm implementations
│ │ ├── preprocessing.py
│ │ ├── exploratory.py
│ │ ├── variable_selection.py
│ │ ├── regression.py
│ │ ├── classification.py
│ │ ├── deep_learning.py
│ │ └── validation.py
│ ├── templates/ # Jinja2 HTML templates
│ └── static/ # CSS & JS
├── data/sample/ # Bundled sample datasets
├── requirements.txt
├── run.py # Entry point
└── README.md
- Fork the repository
- Create a feature branch (
git checkout -b feature/my-feature) - Commit your changes (
git commit -m "Add my feature") - Push to the branch (
git push origin feature/my-feature) - Open a Pull Request
MIT -- use freely for education, research, and production.
Built with Flask • scikit-learn • PyTorch • Plotly
Designed for chemometrics education and real-world NIR deployment