Interactive Example Browserο
NIRS4ALL ships with 70+ comprehensive examples organized into three categories, each demonstrating real-world spectroscopy workflows. Browse by difficulty, topic, or learning path to find the perfect starting point for your use case.
Quick Startο
Start with the beginner learning path: 4 examples covering basics to get you analyzing spectra in 30 minutes.
Jump directly to examples organized by topic: data handling, preprocessing, models, pipelines, etc.
Running Examplesο
All examples are executable Python scripts located in examples/ with sample datasets included:
# Run from project root
cd examples
# Run all user examples
./run.sh -c user
# Run specific example
python user/01_getting_started/U01_hello_world.py
# Run with visualizations
python user/01_getting_started/U02_basic_regression.py --plots --show
Beginner Learning Pathο
Start here if youβre new to nirs4all. Follow this 4-step path to understand core concepts:
β ββββ | ~1 minute
examples/user/01_getting_started/U01_hello_world.py
Your first pipeline in 20 lines. Train a PLS model on NIRS data and get predictions.
Topics: nirs4all.run() basics, pipeline structure, reading results
β β βββ | ~3 minutes
examples/user/01_getting_started/U02_basic_regression.py
Add preprocessing, feature augmentation, and visualization to your pipeline.
Topics: SNV, derivatives, PredictionAnalyzer, comparing models
β ββββ | ~2 minutes
examples/user/02_data_handling/U01_flexible_inputs.py
Learn all the ways to load data: numpy arrays, dictionaries, SpectroDataset objects.
Topics: Data formats, partition specification, dataset configuration
β β βββ | ~3 minutes
examples/user/03_preprocessing/U01_preprocessing_basics.py
Explore NIRS-specific preprocessing: scatter correction, derivatives, smoothing.
Topics: SNV, MSC, Detrend, Savitzky-Golay, wavelets
Next Steps: After completing this path, explore Intermediate Learning Path or browse examples by topic below.
Intermediate Learning Pathο
Ready to build production-ready models? Follow this path:
β β βββ | ~3 minutes
examples/user/04_models/U01_multi_model.py
Compare multiple models in one run using the _or_ generator syntax.
Topics: Model selection, generator syntax, performance comparison
β β βββ | ~4 minutes
examples/user/05_cross_validation/U01_cv_strategies.py
Proper validation with K-Fold, stratified splits, and group-aware CV.
Topics: KFold, ShuffleSplit, StratifiedKFold, custom splits
β β βββ | ~3 minutes
examples/user/06_deployment/U01_save_load_predict.py
Export trained models and make predictions on new data.
Topics: Model persistence, prediction API, workspace management
β β βββ | ~4 minutes
examples/user/07_explainability/U01_shap_basics.py
Understand model predictions with SHAP waterfall and spectral plots.
Topics: SHAP integration, feature importance, visualization
Next Steps: Explore Advanced Learning Path or dive into specific topics in the sections below.
Advanced Learning Pathο
Master advanced features for research and production systems:
β β β ββ | ~5 minutes
examples/developer/01_advanced_pipelines/D01_branching_basics.py
Create parallel preprocessing branches and compare results.
Topics: Duplication branches, separation branches, branch comparison
β β β ββ | ~5 minutes
examples/developer/02_generators/D01_generator_syntax.py
Master the generator syntax for automated pipeline exploration.
Topics: _or_, _range_, _grid_, _zip_, _chain_
β β β β β | ~6 minutes
examples/developer/06_internals/D01_session_workflow.py
Use sessions for stateful multi-step workflows with training, prediction, and retraining.
Topics: Session API, persistence, multi-run workflows
β β β β β | ~6 minutes
examples/developer/06_internals/D03_cache_performance.py
Optimize large pipeline runs with step caching and CoW snapshots.
Topics: CacheConfig, memory optimization, performance tuning
Browse by Topicο
π Getting Started (4 examples)ο
Perfect first examples for understanding nirs4all basics.
Example |
Difficulty |
Description |
|---|---|---|
β ββββ |
Your first pipeline in 20 lines |
|
β β βββ |
Preprocessing, feature augmentation, visualization |
|
β β βββ |
Classification pipelines and confusion matrices |
|
β β βββ |
Heatmaps, candlestick charts, prediction analysis |
Key APIs: nirs4all.run(), PredictionAnalyzer, RunResult
πΎ Data Handling (6 examples)ο
Loading data, multi-source datasets, synthetic data generation.
Example |
Difficulty |
Description |
|---|---|---|
β ββββ |
All data input formats: arrays, dicts, SpectroDataset |
|
β β βββ |
Analyze multiple datasets in one run |
|
β β β ββ |
Combine NIR, markers, and other data sources |
|
β β βββ |
Resample, downsample, focus on spectral regions |
|
β β βββ |
Generate synthetic datasets with |
|
β β β ββ |
SyntheticDatasetBuilder with metadata and batch effects |
Key APIs: SpectroDataset, DatasetConfigs, nirs4all.generate(), SyntheticDatasetBuilder
π¬ Preprocessing (4 examples)ο
NIRS-specific preprocessing operators and signal conversions.
Example |
Difficulty |
Description |
|---|---|---|
β β βββ |
SNV, MSC, Detrend, derivatives, smoothing, wavelets |
|
β β β ββ |
Explore preprocessing combinations with |
|
β β β ββ |
Data augmentation for training set expansion |
|
β β β ββ |
Convert between absorbance, reflectance, transmittance |
Key APIs: SNV, MSC, Detrend, SavitzkyGolay, Derivative, ToAbsorbance, FromAbsorbance
π€ Models (7 examples)ο
Model training, hyperparameter tuning, ensembles, PLS variants, and AOM models.
Example |
Difficulty |
Description |
|---|---|---|
β β βββ |
Compare multiple models with |
|
β β β ββ |
Automated tuning with grid, random, Bayesian search |
|
β β β ββ |
StackingRegressor, VotingRegressor, meta-learners |
|
β β β β β |
PLSRegression, IKPLS, OPLS, SparsePLS, IntervalPLS |
|
β β β β β |
Advanced model fine-tuning workflows |
|
β β βββ |
Fixed-recipe TabPFN regressor for NIRS |
|
β β β β β |
AOM-PLS, AOM-Ridge, AutoSelector, Blender, FastAOM with pipeline folds |
Key APIs: finetune_params, StackingRegressor, PLS variants, AOM models, hyperparameter search
β Cross-Validation (6 examples)ο
Validation strategies, group splitting, outlier filtering, aggregation.
Example |
Difficulty |
Description |
|---|---|---|
β β βββ |
KFold, ShuffleSplit, StratifiedKFold, custom splits |
|
β β β ββ |
GroupKFold, repetition-aware splitting |
|
β β β ββ |
Outlier detection with |
|
β β β ββ |
Aggregate predictions from repeated measurements |
|
β β β ββ |
Mark samples with |
|
β β β β β |
Advanced exclusion with βanyβ vs βallβ modes |
Key APIs: GroupKFold, YOutlierFilter, XOutlierFilter, tag, exclude, repetition
π Deployment (4 examples)ο
Model persistence, bundle export, sklearn integration, workspace management.
Example |
Difficulty |
Description |
|---|---|---|
β β βββ |
Basic prediction workflow with workspace |
|
β β βββ |
Export to .n4a and .n4a.py portable bundles |
|
β β βββ |
Session context, SQLite storage, library management |
|
β β βββ |
Use NIRSPipeline as sklearn estimator for GridSearchCV |
Key APIs: nirs4all.predict(), export(), NIRSPipeline, nirs4all.session()
π Explainability (3 examples)ο
SHAP-based feature importance and variable selection.
Example |
Difficulty |
Description |
|---|---|---|
β β βββ |
SHAP waterfall, spectral, and beeswarm plots |
|
β β β ββ |
Custom SHAP explainers with NIRSPipeline wrapper |
|
β β β ββ |
CARS, MC-UVE for variable selection |
Key APIs: nirs4all.explain(), NIRSPipeline, SHAP integration, feature selection
Developer Examples (30 examples)ο
Advanced examples for extending nirs4all and building complex workflows.
πΏ Advanced Pipelines (7 examples)ο
Branching, merging, separation, stacking, value mapping.
Example |
Difficulty |
Description |
|---|---|---|
β β β ββ |
Duplication and separation branches |
|
β β β β β |
BranchAnalyzer for statistical comparison |
|
β β β β β |
Feature merge, prediction merge, concat merge |
|
β β β β β |
Multi-source branching and merging |
|
β β β β β |
MetaModel, StackingConfig, multi-level stacking |
|
β β β β β |
by_tag, by_metadata, by_filter, by_source |
|
β β β β β |
Group metadata values for custom branching |
Key APIs: branch, merge, by_tag, by_metadata, by_source, MetaModel, StackingConfig
π² Generators & Synthetic Data (9 examples)ο
Generator syntax, synthetic data generation, application domains, instruments.
Example |
Difficulty |
Description |
|---|---|---|
β β β ββ |
|
|
β β β β β |
|
|
β β β β β |
Lazy iteration, batch processing, variant inspection |
|
β β β β β |
Hierarchical parameter spaces |
|
β β β β β |
Build custom spectral component libraries |
|
β β β β β |
Use synthetic data for reproducible testing |
|
β β β β β |
Overtone calculation, combination bands, H-bonding |
|
β β β β β |
Agriculture, food, pharma, environmental domains |
|
β β β β β |
Simulate FOSS, Bruker, SCiO, multi-sensor instruments |
Key APIs: _or_, _range_, _grid_, _zip_, generator constraints, SyntheticNIRSGenerator, application domains
π§ Deep Learning (4 examples)ο
PyTorch, JAX, TensorFlow integration with the @framework decorator.
Example |
Difficulty |
Description |
|---|---|---|
β β β β β |
Built-in nicon, decon, transformer, custom models |
|
β β β β β |
JAX/Flax integration with JIT compilation |
|
β β β β β |
TensorFlow/Keras nicon and decon architectures |
|
β β β β β |
Same model across PyTorch, JAX, TensorFlow |
Key APIs: @framework decorator, nicon, decon, transformer, custom DL models
π Transfer Learning (3 examples)ο
Calibration transfer, retraining modes, PCA geometry analysis.
Example |
Difficulty |
Description |
|---|---|---|
β β β β β |
TransferPreprocessingSelector for transfer problems |
|
β β β β β |
full, transfer, finetune retraining modes |
|
β β β ββ |
PreprocPCAEvaluator for geometry preservation |
Key APIs: nirs4all.retrain(), TransferPreprocessingSelector, retrain modes
βοΈ Advanced Features (3 examples)ο
Metadata branching, concat transform, repetition transform.
Example |
Difficulty |
Description |
|---|---|---|
β β β β β |
Partition by metadata with value mapping |
|
β β β ββ |
Concatenate multiple transformer outputs |
|
β β β β β |
Convert repetitions to sources or preprocessing variants |
Key APIs: by_metadata, concat_transform, rep_to_sources, rep_to_pp
π§ Internals (3 examples)ο
Session API, custom controllers, cache optimization.
Example |
Difficulty |
Description |
|---|---|---|
β β β β β |
Stateful workflows with save/load/persist |
|
β β β β β |
Extend nirs4all with custom operator controllers |
|
β β β β β |
Step caching, CoW snapshots, memory optimization |
Key APIs: nirs4all.session(), @register_controller, CacheConfig, controller architecture
Reference Examples (7 examples)ο
Comprehensive reference examples for advanced features.
Example |
Difficulty |
Description |
|---|---|---|
β β βββ |
Complete pipeline syntax reference |
|
β β βββ |
All generator keywords and patterns |
|
β β β β β |
Integration test for all pipeline keywords |
|
β β βββ |
Matrix effects simulation in synthetic data |
|
β β βββ |
Quality assessment for synthetic spectra |
|
β β β β β |
Fit synthetic data to match real spectra |
Find Examples by Featureο
By Feature Typeο
Caching: D03: Cache Performance
Branching: D01: Branching Basics, D06: Separation Branches
Merging: D03: Merge Basics, D04: Merge Sources
Stacking: U03: Stacking Ensembles, D05: Meta Stacking
Generators: D01: Generator Syntax, D02: Generator Advanced
Session API: U03: Workspace Management, D01: Session Workflow
Synthetic Data: U05: Synthetic Data, U06: Advanced Synthetic, D05-D09: Synthetic Generators
Multi-Source: U03: Multi-Source, D04: Merge Sources
Aggregation: U04: Repetition Aggregation, D03: Repetition Transform
Signal Types: U04: Signal Conversion
Outlier Filtering: U03: Sample Exclusion, U06: Exclusion Strategies
Deep Learning: D01-D04: Deep Learning Examples
Custom Controllers: D02: Custom Controllers
Next Stepsο
After exploring examples:
User Guide: User Guide - Complete documentation with detailed explanations
API Reference: API Reference - Full API documentation
Concepts: Concepts - The ideas behind pipelines, datasets, and workflows
Developer Guide: Developer Guide - Deep dive into architecture and design patterns
Getting Started: Quickstart - Installation and first steps
Questions? Check the Frequently Asked Questions or raise an issue on GitHub.