Metadata-Version: 2.2
Name: soilspectfm
Version: 0.0.1
Summary: Soil infrared spectra preprocessing utilities
Home-page: https://github.com/franckalbinet/soilspectfm
Author: Franck Albinet
Author-email: franckalbinet@gmail.com
License: Apache Software License 2.0
Keywords: nbdev jupyter notebook python
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Natural Language :: English
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: License :: OSI Approved :: Apache Software License
Requires-Python: >=3.7
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: fastcore
Requires-Dist: scikit-learn
Requires-Dist: matplotlib
Provides-Extra: dev
Requires-Dist: soilspecdata; extra == "dev"
Requires-Dist: nbdev; extra == "dev"
Requires-Dist: ipykernel; extra == "dev"
Dynamic: author
Dynamic: author-email
Dynamic: classifier
Dynamic: description
Dynamic: description-content-type
Dynamic: home-page
Dynamic: keywords
Dynamic: license
Dynamic: provides-extra
Dynamic: requires-dist
Dynamic: requires-python
Dynamic: summary

# SoilSpecTfm


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

> A Python package providing scikit-learn compatible transforms for
> spectroscopic data preprocessing.

It’s designed to work seamlessly with both MIR (Mid-Infrared) and VISNIR
(Visible-Near Infrared) spectral data.

**WORK IN PROGRESS**

## Installation

``` bash
pip install soilspectfm
```

## Quick Start

``` python
from soilspectfm.core import SNV, TakeDerivative, ToAbsorbance
from sklearn.pipeline import Pipeline
```

### Loading OSSL dataset

Let’s use OSSL dataset as an example using
[SoilSpecData](https://fr.anckalbi.net/soilspecdata/) package.

``` python
from soilspecdata.datasets.ossl import get_ossl
```

``` python
ossl = get_ossl()
mir_data = ossl.get_mir()
```

### Preprocessing pipeline

Implemented transforms developed so far include:

- **Baseline corrections**:

  - [x]
    [`SNV`](https://franckalbinet.github.io/soilspectfm/core.html#snv):
    Standard Normal Variate
  - [x]
    [`MSC`](https://franckalbinet.github.io/soilspectfm/core.html#msc):
    Multiplicative Scatter Correction
  - [ ] `Detrend`: Detrend the spectrum (SOON)
  - [ ] `ALS`: Asymmetric Least Squares detrend the spectrum (SOON)

- **Derivatives**:

  - [x]
    [`TakeDerivative`](https://franckalbinet.github.io/soilspectfm/core.html#takederivative):
    Take derivative (1st, 2nd, etc.) of the spectrum and apply
    Savitzky-Golay smoothing
  - [ ] `GapSegmentDerivative`: …

- **Smoothing**:

  - [ ] `WaveletDenoise`: Wavelet denoising
  - [ ] `SavGolSmooth`: Savitzky-Golay smoothing

- **Other transformations**:

  - [x]
    [`ToAbsorbance`](https://franckalbinet.github.io/soilspectfm/core.html#toabsorbance):
    Transform the spectrum to absorbance
  - [ ] `Resample`: Resample the spectrum to a new wavenumber range

Transforms are fully compatible with
[scikit-learn](https://scikit-learn.org/stable/) and can be used in a
pipeline as follows:

``` python
pipe = Pipeline([
    ('snv', SNV()), # Standard Normal Variate transformation
    ('deriv', TakeDerivative(window_length=11, polyorder=2, deriv=1)) # First derivative
])

X_tfm = pipe.fit_transform(mir_data.spectra)
```

### Quick visualization

``` python
from soilspectfm.visualization import plot_spectra
from matplotlib import pyplot as plt
```

``` python
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(15, 7))

ax1 = plot_spectra(
    mir_data.spectra, 
    mir_data.wavenumbers,
    ax=ax1,
    ascending=False,
    color='black',
    alpha=0.6,
    lw=0.5,
    xlabel='Wavenumber (cm$^{-1}$)',
    title='Raw Spectra'
)

ax2 = plot_spectra(
    X_tfm,
    mir_data.wavenumbers,
    ax=ax2,
    ascending=False,
    color='steelblue',
    alpha=0.6,
    lw=0.5,
    xlabel='Wavenumber (cm$^{-1}$)',
    title='SNV + Derivative (1st order) Transformed Spectra'
)

plt.tight_layout()
```

![](index_files/figure-commonmark/cell-7-output-1.png)

## Dependencies

- fastcore
- numpy
- scipy
- scikit-learn
- matplotlib

## Further references

- https://orange-spectroscopy.readthedocs.io/en/latest/widgets/preprocess-spectra.html

## Contributing

### Developer guide

If you are new to using `nbdev` here are some useful pointers to get you
started.

Install spectfm in Development mode:

``` sh
# make sure spectfm package is installed in development mode
$ pip install -e .

# make changes under nbs/ directory
# ...

# compile to have changes apply to spectfm
$ nbdev_prepare
```

## License

This project is licensed under the Apache2 License - see the LICENSE
file for details.

## Support

For questions and support, please [open an
issue](https://github.com/franckalbinet/spectfm/issues) on GitHub.
