Note
Go to the end to download the full example code
NDDataset baseline correction
In this example, we perform a baseline correction of a 2D NDDataset
interactively, using the multivariate method and a pchip/polynomial interpolation.
For comparison, we also use the asls`and `snip models.
Import the library
import spectrochempy as scp
Load and prepare the dataset
datadir = scp.preferences.datadir
nd = scp.read_omnic(datadir / "irdata" / "nh4y-activation.spg")
Keep only the spectral region of interest (use floats for coordinate-based slicing)
Plot the raw dataset

Quick linear baseline removal with basc:
Shift to positive values

Multivariate baseline correction with pchip interpolation
Create a Baseline object using a multivariate approach with pchip
(piecewise cubic Hermite) interpolation:
blc = scp.Baseline(
log_level="WARNING",
multivariate=True,
model="polynomial",
order="pchip",
n_components=5,
)
Define the reference regions for the baseline:
blc.ranges = [
[1556.30, 1568.26],
[1795.00, 1956.75],
[3766.03, 3915.81],
[4574.26, 4616.04],
[4980.10, 4998.01],
[5437.52, 5994.70],
]
Fit the model:
The baseline and corrected datasets are stored in the processor:
Plot the corrected dataset:
_ = corrected.plot()

A detailed view with region annotations:

Compare individual spectra (corrected, baseline, original):


Switch to a polynomial model
The pchip interpolation seems too rigid in some regions.
We can change the model without redefining the Baseline object:
Refit and compare:


_ = corrected.plot()

Try the AsLS model
The Asymmetric Least Squares model (Eilers and Boelens, 2005) offers
a different trade-off. The mu parameter controls smoothness and
asymmetry controls the weighting.
blc.multivariate = False
blc.model = "asls"
blc.mu = 10**9
blc.asymmetry = 0.002
_ = blc.fit(ndp)
/home/runner/work/spectrochempy/tempdirs/scp_yf458zyp/src/spectrochempy/processing/baselineprocessing/baselineprocessing.py:527: SparseEfficiencyWarning: spsolve requires A be CSC or CSR matrix format
z = spsolve(C, w * y)


_ = corrected.plot()

Try the SNIP model
The Statistics-sensitive Non-linear Iterative Peak-clipping method:
blc.multivariate = False
blc.model = "snip"
blc.snip_width = 200
_ = blc.fit(ndp)


_ = corrected.plot()

This ends the example ! The following line can be uncommented if no plot shows when running the .py script with python
# scp.show()
Total running time of the script: ( 0 minutes 7.264 seconds)