NIRS Analyses made easy.
Project description
NIRS4ALL is a comprehensive machine learning library specifically designed for Near-Infrared Spectroscopy (NIRS) data analysis. It bridges the gap between spectroscopic data and machine learning by providing a unified framework for data loading, preprocessing, model training, and evaluation.
What is Near-Infrared Spectroscopy (NIRS)?
Near-Infrared Spectroscopy (NIRS) is a rapid and non-destructive analytical technique that uses the near-infrared region of the electromagnetic spectrum (approximately 700-2500 nm). NIRS measures how near-infrared light interacts with the molecular bonds in materials, particularly C-H, N-H, and O-H bonds, providing information about the chemical composition of samples.
Key advantages of NIRS:
- Non-destructive analysis
- Minimal sample preparation
- Rapid results (seconds to minutes)
- Potential for on-line/in-line implementation
- Simultaneous measurement of multiple parameters
Common applications:
- Agriculture: soil analysis, crop quality assessment
- Food industry: quality control, authenticity verification
- Pharmaceutical: raw material verification, process monitoring
- Medical: tissue monitoring, brain imaging
- Environmental: pollutant detection, water quality monitoring
Features
NIRS4ALL offers a wide range of functionalities:
-
Spectrum Preprocessing:
- Baseline correction
- Standard normal variate (SNV)
- Robust normal variate
- Savitzky-Golay filtering
- Normalization
- Detrending
- Multiplicative scatter correction
- Derivative computation
- Gaussian filtering
- Haar wavelet transformation
- And more
-
Data Splitting Methods:
- Kennard Stone
- SPXY
- Random sampling
- Stratified sampling
- K-means
- And more
-
Model Integration:
- Scikit-learn models
- TensorFlow/Keras models
- PyTorch models (via extensions)
- JAX models (via extensions)
-
Model Fine-tuning:
- Hyperparameter optimization with Optuna
- Grid search and random search
- Cross-validation strategies
-
Visualization:
- Preprocessing effect visualization
- Model performance visualization
- Feature importance analysis
- Classification metrics
- Residual analysis
Installation
Basic Installation
pip install nirs4all
Install TensorFlow cpu support by default
With Additional ML Frameworks
# With PyTorch support
pip install nirs4all[torch]
# With Keras support
pip install nirs4all[keras]
# With JAX support
pip install nirs4all[jax]
# With all ML frameworks
pip install nirs4all[all]
Development Installation
For developers who want to contribute:
git clone https://github.com/gbeurier/nirs4all.git
cd nirs4all
pip install -e .[dev]
Installation Testing
After installing nirs4all, you can verify your installation and environment using the built-in CLI test commands:
# Basic installation test: checks required dependencies and versions
nirs4all -test_install
# Full installation test: checks dependencies and runs a TensorFlow test
nirs4all -full_test_install
# Integration test: runs a full pipeline on sample data (Random Forest, PLS fine-tuning, and a simple CNN)
nirs4all -test_integration
Each command will print a summary of the test results and alert you to any missing dependencies or issues with your environment.
You can also check the installed version:
nirs4all --version
Quick Start
import numpy as np
import matplotlib.pyplot as plt
from nirs4all.data.dataset_loader import get_dataset
from nirs4all.transformations import StandardNormalVariate as SNV, SavitzkyGolay as SG
from nirs4all.core.runner import ExperimentRunner
from nirs4all.core.config import Config
from sklearn.model_selection import RepeatedKFold
from sklearn.preprocessing import MinMaxScaler, RobustScaler
from sklearn.cross_decomposition import PLSRegression
# Define a simple processing pipeline
pipeline = [
RobustScaler(), # Scale the data
{"split": RepeatedKFold(n_splits=3, n_repeats=1)}, # Define cross-validation splits
{"features": [None, SG, SNV, [SG, SNV]}, # Provide 4 versions of the spectra (original, Savitzky-Golay, SNV, Savgol then SNV)
MinMaxScaler() # Scale the data again after splitting
]
# Define scaler for y
y_scaler = MinMaxScaler()
# Create a configuration
config = Config("path/to/your/data", pipeline, y_scaler, PLSRegression(n_components=10), None, 42)
# Run the experiment
runner = ExperimentRunner(config)
datasets, predictions, scores, _ = runner.run()
# Print results
print("Model Performance:")
for i, score in enumerate(scores):
print(f"Model {i+1}:")
for j, fold_score in enumerate(score[:-3]):
print(f" Fold {j+1}: {fold_score}")
print(f" Mean: {score[-3]}")
print(f" Best: {score[-2]}")
print(f" Weighted Mean: {score[-1]}")
Advanced Usage
For more advanced usage, please refer to the comprehensive walkthrough notebook which covers:
- Data Loading and Exploration
- Basic Processing Pipeline
- Training scikit-learn Models
- Training TensorFlow Models
- Fine-tuning Models
- Advanced Pipeline with Custom Transformations
- Running Multiple Configurations in Parallel
- Advanced Data Visualization
- Transformation Effects Visualization
- Model Performance Analysis
- Feature Importance Analysis
- Prediction Visualization
- Classification Metrics
- Residual Analysis
- Model Deployment
Documentation
Detailed documentation will be soon available at https://nirs4all.readthedocs.io/
Dependencies
- numpy (>=1.20.0)
- pandas (>=1.0.0)
- scipy (>=1.5.0)
- scikit-learn (>=0.24.0)
- PyWavelets (>=1.1.0)
- joblib (>=0.16.0)
- jsonschema (>=3.2.0)
- kennard-stone (>=0.5.0)
- twinning (>=0.0.5)
- optuna (>=2.0.0)
Optional Dependencies
- tensorflow (>=2.10.0) - For TensorFlow models
- torch (>=2.0.0) - For PyTorch models
- keras (>=3.0.0) - For Keras models
- jax (>=0.4.10) & jaxlib (>=0.4.10) - For JAX models
How to Cite
If you use NIRS4ALL in your research, please cite:
@software{beurier2025nirs4all,
author = {Gregory Beurier and Denis Cornet and Lauriane Rouan},
title = {NIRS4ALL: Unlocking Spectroscopy for Everyone},
url = {https://github.com/gbeurier/nirs4all},
version = {0.0.1},
year = {2025},
}
License
This project is licensed under the CECILL-2.1 License - see the LICENSE file for details.
Acknowledgments
- CIRAD for supporting this research
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nirs4all-0.1.0.tar.gz.
File metadata
- Download URL: nirs4all-0.1.0.tar.gz
- Upload date:
- Size: 210.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d93b17f5de7035f6265c4c32c124ccc6d1cf3402db1382113d62347a7873de47
|
|
| MD5 |
5d2fba47841208aeb1a2613147f116c1
|
|
| BLAKE2b-256 |
15e46960117b3439b8ea284b12a0ddebe0b5a957b8d931c161f253f4fa38b6b0
|
Provenance
The following attestation bundles were made for nirs4all-0.1.0.tar.gz:
Publisher:
publish.yml on GBeurier/nirs4all
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
nirs4all-0.1.0.tar.gz -
Subject digest:
d93b17f5de7035f6265c4c32c124ccc6d1cf3402db1382113d62347a7873de47 - Sigstore transparency entry: 584358889
- Sigstore integration time:
-
Permalink:
GBeurier/nirs4all@470713936a4743c738dbd81c350c21937cf35343 -
Branch / Tag:
refs/tags/0.1.0 - Owner: https://github.com/GBeurier
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@470713936a4743c738dbd81c350c21937cf35343 -
Trigger Event:
release
-
Statement type:
File details
Details for the file nirs4all-0.1.0-py3-none-any.whl.
File metadata
- Download URL: nirs4all-0.1.0-py3-none-any.whl
- Upload date:
- Size: 272.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7c8d73e7540d0c95b98ce5d7bab5b7a99280086c2dab6e25e0d6973e9d32b2a1
|
|
| MD5 |
4a00286538d43f8b4b7875f60051862b
|
|
| BLAKE2b-256 |
b3be8457fce331197356bc42a77d2b13547183fcd8ca488e59383743b5df91a7
|
Provenance
The following attestation bundles were made for nirs4all-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on GBeurier/nirs4all
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
nirs4all-0.1.0-py3-none-any.whl -
Subject digest:
7c8d73e7540d0c95b98ce5d7bab5b7a99280086c2dab6e25e0d6973e9d32b2a1 - Sigstore transparency entry: 584358891
- Sigstore integration time:
-
Permalink:
GBeurier/nirs4all@470713936a4743c738dbd81c350c21937cf35343 -
Branch / Tag:
refs/tags/0.1.0 - Owner: https://github.com/GBeurier
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@470713936a4743c738dbd81c350c21937cf35343 -
Trigger Event:
release
-
Statement type: