Legacy — Python documentation

This section preserves the previous Python documentation and its detailed references. Start with the common user and developer guide for the current task-oriented documentation. The Python SDK remains supported; this section keeps its existing guides available.

NIRS4ALL Logo
CIRAD Logo

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.

Start with the common user and developer guide: choose your language, define data, evaluate candidates, save the refit model and predict. Use the detailed references when you need exact parameters.

Common workflow guide

Task-oriented chapters for Python, R, JS/WASM, MATLAB/Octave, Rust and CLI.

Learn to build, understand and reuse pipelines
Concepts

Pipelines, datasets, branching, generators, cross-validation, augmentation, and deployment.

Concepts
User Guide

Step-by-step how-to guides for preprocessing, stacking, export, and more.

User Guide
Reference

Complete API reference, pipeline syntax, and operator catalog by category.

Reference
Developer Guide

Architecture, internals, the controller pattern, and contribution guidelines.

Developer Guide
Native V1 migration

Candidate architecture, workspace conversion, API/ABI/schema identities, support window, examples, and FAQ.

Native V1 migration and compatibility guide

Documentation

Quick Start

import nirs4all
from sklearn.preprocessing import MinMaxScaler
from sklearn.cross_decomposition import PLSRegression
from sklearn.model_selection import ShuffleSplit

# Define and run a pipeline in one step
result = nirs4all.run(
    pipeline=[
        MinMaxScaler(),
        ShuffleSplit(n_splits=3),
        {"model": PLSRegression(n_components=10)}
    ],
    dataset="path/to/data",
    verbose=1
)

print(f"Best RMSE: {result.best_rmse:.4f}")
result.export("exports/best_model.n4a")

Try Without Data

No dataset? Generate synthetic NIRS spectra to get started:

import nirs4all

# Generate realistic synthetic NIRS data
dataset = nirs4all.generate.regression(
    n_samples=500,
    components=["water", "protein", "lipid"],
    complexity="realistic",
    random_state=42
)

# Run a pipeline on synthetic data
result = nirs4all.run(
    pipeline=[MinMaxScaler(), {"model": PLSRegression(10)}],
    dataset=dataset
)
print(f"RMSE: {result.best_rmse:.4f}")

Tip

See Interactive Example Browser for 50+ working examples organized by topic.

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:

  1. 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…

  2. Data Splitting Methods:

    • Kennard Stone

    • SPXY

    • Random sampling

    • Stratified sampling

    • K-means

    • And more…

  3. Model Integration:

    • Scikit-learn models

    • TensorFlow/Keras models

    • Pre-configured neural networks dedicated to the NIRS: nicon & decon (see publication below)

    • PyTorch models (via extensions)

    • JAX models (via extensions)

  4. Model Fine-tuning:

    • Hyperparameter optimization with Optuna

    • Grid search and random search

    • Cross-validation strategies

  5. Visualization:

    • Preprocessing effect visualization

    • Model performance visualization

    • Feature importance analysis

    • Classification metrics

    • Residual analysis

  6. Synthetic Data Generation:

    • Physically-motivated NIRS spectra (Beer-Lambert law)

    • Predefined spectral components (water, protein, lipid, etc.)

    • Configurable complexity levels for testing

    • Classification and regression targets

    • Export to CSV for loader testing

Installation

Basic Installation

pip install nirs4all

This installs the core library with scikit-learn support. Deep learning frameworks are optional.

With Additional ML Frameworks

# With TensorFlow support (CPU)
pip install nirs4all[tensorflow]

# With TensorFlow support (GPU)
pip install nirs4all[gpu]

# 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]

# With all ML frameworks and GPU support for TensorFlow
pip install nirs4all[all-gpu]

Development Installation

For developers who want to contribute:

git clone https://github.com/gbeurier/nirs4all.git
cd nirs4all
pip install -e .[dev]

Research Applications

NIRS4ALL has been successfully used in published research:

Houngbo, M. E., Desfontaines, L., Diman, J. L., Arnau, G., Mestres, C., Davrieux, F., Rouan, L., Beurier, G., Marie‐Magdeleine, C., Meghar, K., Alamu, E. O., Otegbayo, B. O., & Cornet, D. (2024). Convolutional neural network allows amylose content prediction in yam (Dioscorea alata L.) flour using near infrared spectroscopy. Journal of the Science of Food and Agriculture, 104(8), 4915-4921. John Wiley & Sons, Ltd.

How to Cite

If you use NIRS4ALL in your research, please cite:

@software{beurier2025nirs4all,
  author = {Gregory Beurier and Denis Cornet and Camille Noûs and Lauriane Rouan},
  title = {nirs4all is all your nirs: Open spectroscopy for everyone},
  url = {https://github.com/gbeurier/nirs4all},
  version = {1.4.3},
  year = {2026},
}

License

nirs4all is distributed under a dual license: by default AGPL-3.0-or-later (with GPL-3.0-or-later and CeCILL-2.1 variants available), plus a separate commercial license for closed-source use. See the LICENSE file for the full terms.