Skip to main content

SoilSpecTfm

SoilSpecTfm corrects, smooths and resamples soil infrared spectra before modeling. It covers scatter correction, Savitzky-Golay smoothing and derivatives, wavelet denoising, conversion to absorbance, resampling and trimming. Each transformer follows the scikit-learn API. You can chain them in a Pipeline, tune their parameters with GridSearchCV, and put them in front of any scikit-learn model.

SoilSpecData loads the Open Soil Spectral Library (OSSL) as spectra and soil properties ready for these transformers. The SoilSpecTfm documentation covers the full API.

Installation

pip install -U soilspectfm

To install the development version, with nbdev and SoilSpecData, run this in the project root with uv:

uv sync --extra dev

Features

Transformer What it does
SNV Centers and scales each spectrum (standard normal variate)
MSC Corrects each spectrum against a reference spectrum (multiplicative scatter correction)
SavitzkyGolay Smooths each spectrum, or takes its derivative
WaveletDenoise Removes noise by thresholding wavelet coefficients
ToAbsorbance Converts reflectance to absorbance
Resample Interpolates spectra onto new wavenumbers
Trim Keeps the wavenumbers inside a range

soilspectfm.datasets loads the two toy datasets, and soilspectfm.visualization plots spectra.

Quick start

from soilspectfm.all import * imports every transformer, the toy dataset loaders and the plotting functions. Load the toy MIR spectra that ship with the package, then preprocess them with standard normal variate (SNV), wavelet denoising and a first derivative:

from soilspectfm.all import *
from sklearn.pipeline import Pipeline
X, ws = load_toy_mir()
pipe = Pipeline([('snv', SNV()), ('denoise', WaveletDenoise()), ('deriv', SavitzkyGolay(window_length=11, polyorder=2, deriv=1))])
X_tfm = pipe.fit_transform(X)
X_tfm.shape
(50, 1701)

Compare the raw and preprocessed spectra:

plot_spectra_comparison(X, X_tfm, ws, transformed_title='SNV, wavelet denoising and first derivative');

Modeling OSSL data

SoilSpecData gives the spectra X and soil properties y of the OSSL samples. This example predicts cation exchange capacity (CEC) from MIR spectra with partial least squares regression, and scores it with 5-fold cross-validation. The first call to load_ossl downloads about 1 GB:

from soilspecdata.all import *
from sklearn.cross_decomposition import PLSRegression
from sklearn.model_selection import cross_val_score
X, y, ids = load_ossl().mir().xy('cec_usda.a723_cmolc.kg')
model = Pipeline([('snv', SNV()), ('deriv', SavitzkyGolay(window_length=11, polyorder=2, deriv=1)), ('pls', PLSRegression(n_components=10))])
cross_val_score(model, X, y, cv=5, scoring='r2')

Upgrading from 0.0.x

Version 0.1.0 changes the API of soilspectfm. To update code written for version 0.0.x:

  • soilspectfm.core is now soilspectfm.preprocessing. To import every transformer, loader and plotting function at once, use from soilspectfm.all import *.
  • SavitzkyGolay replaces TakeDerivative and SavGolSmooth. Its defaults are window_length=15, polyorder=3 and deriv=0, the old SavGolSmooth defaults. Replace SavGolSmooth(...) with SavitzkyGolay(...). Replace TakeDerivative() with SavitzkyGolay(window_length=11, polyorder=1, deriv=1), because the defaults differ.
  • MSC no longer accepts n_jobs.
  • Resample.fit raises a ValueError when target_x goes outside the range of source_x. Resample.transform now requires fit first.
  • soilspectfm.utils is now soilspectfm.datasets. It no longer exports toy_mir_url and toy_noisy_mir_url. The toy datasets ship with the package, and load_toy_mir and load_toy_noisy_mir read them without a network connection.

Contributing

Contributions are welcome. SoilSpecTfm is built with nbdev: the notebooks in nbs/ are the source of the code, the documentation and the tests. After changing a notebook, export the code, run the tests, clean the notebooks and rebuild the README with:

uv run nbdev-prepare

Report bugs and ask questions in GitHub issues.

License

SoilSpecTfm is under the Apache 2.0 licence.

Citation

Metadata

Release files for soilspectfm 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for soilspectfm 0.1.0
File Size Uploaded
soilspectfm-0.1.0.tar.gz 1.6 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for soilspectfm 0.1.0
File Interpreter ABI Platform
soilspectfm-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 3.3 MB

Release files / soilspectfm-0.1.0.tar.gz

Download URL soilspectfm-0.1.0.tar.gz
Size 1.6 MB
Tags Source
SHA-256 checksum
How to use checksums
486ff555ae499bbea364697877ec93bf2fef0d0645aa025705a1c66ab0930078
BLAKE2b-256 checksum
How to use checksums
b09851b0f43506ab09b06536928bcb8b720d40a1b6d834ffb21675b2623d13b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release files / soilspectfm-0.1.0-py3-none-any.whl

Download URL soilspectfm-0.1.0-py3-none-any.whl
Size 1.6 MB
Tags Python 3
SHA-256 checksum
How to use checksums
100f48fe495044ef1434c1d9df5eb4c45f255eb3a7fb4f6ed047d1e6a294f84b
BLAKE2b-256 checksum
How to use checksums
6261219c52b4e6e1b1d96d23082aa08f7325eeb1a553f42fb33a35aff1e6ad4b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release history Release notifications | RSS feed

0.1.1

2 release files

This release

0.1.0 This release

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page