Standard preprocessing of time × wavelength response matrices
(e.g. HPLC-DAD/UV data), consisting of: (i) optional subtraction of a
reference band of wavelengths, (ii) optional interpolation in either
dimension for dimensionality reduction, (iii) optional smoothing along the
spectral axis, and (iv) optional baseline correction along the time axis.
For densely sampled data (e.g., UV-VIS spectra), the size of the matrix can
be reduced by interpolation. By default, the data are smoothed along the
spectral axis using cubic smoothing splines (smooth.spline), then
baseline-corrected along the time axis by asymmetric least squares
(asysm). Three other baseline estimators are available
through baseline, and baseline = "none" skips the step; see
baseline_estimators.
Usage
preprocess(
X,
dim1,
dim2,
remove.time.baseline = NULL,
baseline = c("als", "arpls", "aspls", "snip", "none"),
attach_baseline = FALSE,
zero_floor = FALSE,
smooth_spectra = TRUE,
reference_wavelengths = NULL,
interpolate_rows = TRUE,
interpolate_cols = TRUE,
cl = 2,
show_progress = NULL,
outlier_cutoff = 5/60,
...,
maxI = NULL,
spec.smooth = NULL
)Arguments
- X
A numeric matrix (time × wavelength) or a list of such matrices. Row names must correspond to time points and column names to wavelengths.
- dim1
Numeric vector specifying the target time grid, usually built with
seq. The data will be interpolated onto these time points, which must be strictly increasing. Chromatograms that do not cover the whole ofdim1are dropped with a warning. Writingc(from, to, by = x)in place ofseq(from, to, by = x)yields a three-element vector rather than a sequence, so it raises an error. Ifdim1is omitted, the grid runs in steps of0.01(in the time units of the data) across the range shared by all chromatograms.- dim2
Numeric vector specifying the target wavelength grid, usually built with
seq. The data will be interpolated onto these wavelengths, which must be strictly increasing and lie within the range of the original wavelengths; otherwise an error is raised. Ignored wheninterpolate_cols = FALSE. If omitted, the existing wavelengths are kept.- remove.time.baseline
The
remove.time.baselineargument is deprecated. Please usebaseline = "none"instead.- baseline
Baseline estimator to use along the time axis. Either asymmetric least squares (
"als", the default, viaasysm), asymmetrically reweighted penalized least squares ("arpls"), adaptive smoothness parameter penalized least squares ("aspls"), statistics-sensitive non-linear iterative peak-clipping ("snip"), or"none"to skip baseline correction along the time axis altogether. Seebaseline_estimatorsfor a description of each.- attach_baseline
Logical. Whether to retain the estimated baseline alongside the corrected data as an
"estimated_baseline"attribute. The parameter that produced it (stiffness, ormfor"snip") is always recorded, as a"baseline_param"attribute, whether or not the baseline itself is kept. The baseline is subtracted either way; this argument only controls whether the estimate is kept for inspection. Defaults toFALSE: the baseline is estimated separately for each wavelength, so it is the same size as the data and retaining it roughly doubles memory use. Adding the attribute back to the returned data recovers the uncorrected matrix only whenzero_floor = FALSEandmaxIandreference_wavelengthsare unset: clipping alters the data without altering the stored estimate,maxIrescales both by a factor derived from the clipped data, and the subtracted reference band is not stored.- zero_floor
Logical. Whether to clip negative values in the corrected data to zero. Defaults to
FALSE. Clipping discards the half of the noise that falls below zero, which hides the negative dips that show where a baseline was over-subtracted, so it is turned off by default.- smooth_spectra
Logical, indicating whether smoothing should be done along the spectral axis, according to
smooth.spline. Skipped for data with fewer than four wavelengths. Defaults toTRUE.- reference_wavelengths
Numeric vector of length 2 giving a band of wavelengths,
c(from, to), whose mean is subtracted from every wavelength at each time point, like the reference wavelength of a diode-array detector. This removes offsets shared by all wavelengths, such as lamp drift or refractive-index effects, before the baseline is estimated. Choose a band where nothing in the samples absorbs. Either bound may beNAto leave that side open. Defaults toNULL(no subtraction).- interpolate_rows
Logical. Whether to interpolate along the time axis (
dim1). Defaults toTRUE.- interpolate_cols
Logical. Whether to interpolate along the spectral axis (
dim2). Defaults toTRUE.- cl
Either an integer specifying the number of cores to use for parallel processing or a cluster object created by
makeCluster. Defaults to2. On Windows integer values will be ignored.- show_progress
Logical. Whether to show progress bar. Defaults to
TRUEifpbapplyis installed.- outlier_cutoff
Threshold (in minutes) for excluding chromatograms that end earlier than expected. Samples ending more than this value before the median end time are removed with a warning. The value is always in minutes, whatever time unit the chromatograms are stored in. Defaults to
5/60, i.e. 5 seconds. Only applies whendim1is not specified.- ...
Additional arguments to the selected
baselineestimator: e.g.stiffnessandpfor"als"(asysm),stiffnessfor"arpls",stiffnessandkfor"aspls", orm(required) for"snip". BecausemaxIfollows...in the signature, it must be named in full: an abbreviation such asmaxis passed to the estimator, which rejects it.- maxI
If supplied, each chromatogram is rescaled so that its maximum intensity equals
maxI.- spec.smooth
The
spec.smoothargument is deprecated. Please usesmooth_spectrainstead.
Value
The function returns the preprocessed data matrix (or list of matrices), with row names and column names indicating the time points and wavelengths, respectively.
Details
The operations are applied in the order given above: reference subtraction
first, on the original wavelengths, then interpolation, then spectral
smoothing, then baseline correction. Because the
baseline is estimated after the time axis is interpolated, stiffness is
tied to whichever dim1 grid was chosen: the penalty applies on the index
axis, so the same value gives a different baseline on a differently sampled
grid. Choose dim1 first, then tune stiffness against that grid.
Note
Adapted from the preprocess function in the alsace package by
Ron Wehrens: https://github.com/rwehrens/alsace/blob/master/R/preprocess.R.
References
Baek, S.-J., Park, A., Ahn, Y.-J., and Choo, J. 2015. Baseline correction using asymmetrically reweighted penalized least squares smoothing. Analyst 140:250-257. doi:10.1039/C4AN01061B .
Zhang, F., Tang, X., Tong, A., Wang, B., Wang, J., Lv, Y., Tang, C., and Wang, J. 2020. Baseline correction for infrared spectra using adaptive smoothness parameter penalized least squares method. Spectroscopy Letters 53:222-233. doi:10.1080/00387010.2020.1730908 .
Ryan, C.G., Clayton, E., Griffin, W.L., Sie, S.H., and Cousens, D.R. 1988. SNIP, a statistics-sensitive background treatment for the quantitative analysis of PIXE spectra in geoscience applications. Nuclear Instruments and Methods in Physics Research B 34:396-402. doi:10.1016/0168-583X(88)90063-8 .
Erb, D. 2025. pybaselines: A Python library of algorithms for the baseline correction of experimental data. doi:10.5281/zenodo.5608581 .
See also
baseline_estimators, correct_baseline, crop_chroms,
get_baseline
Other preprocessing functions:
correct_baseline(),
crop_chroms(),
subtract_blanks()
