0
# Whittaker-Smoothing Methods
1
2
Penalized least squares methods using Whittaker smoothing for baseline correction. These methods iteratively reweight data points to minimize the influence of peaks while maintaining baseline smoothness through regularization. They excel at handling spectra with varying peak widths and complex baseline curvature.
3
4
## Capabilities
5
6
### Asymmetric Least Squares (AsLS)
7
8
The foundational asymmetric least squares method that assigns lower weights to positive deviations (peaks) and higher weights to negative deviations (baseline).
9
10
```python { .api }
11
def asls(data, lam=1e6, p=1e-2, diff_order=2, max_iter=50, tol=1e-3, weights=None, x_data=None):
12
"""
13
Asymmetric Least Squares baseline correction.
14
15
Parameters:
16
- data (array-like): Input y-values to fit baseline
17
- lam (float): Smoothing parameter. Higher values create smoother baselines
18
- p (float): Asymmetry parameter (0 < p < 1). Lower values give less weight to positive residuals
19
- diff_order (int): Order of differential matrix (typically 2)
20
- max_iter (int): Maximum iterations for convergence
21
- tol (float): Convergence tolerance for iterative fitting
22
- weights (array-like, optional): Initial weight array
23
- x_data (array-like, optional): Input x-values
24
25
Returns:
26
tuple: (baseline, params) where params contains 'weights' and 'tol_history'
27
"""
28
```
29
30
### Improved AsLS (IAsLS)
31
32
Enhanced version of AsLS with adaptive parameter adjustment for better convergence and peak handling.
33
34
```python { .api }
35
def iasls(data, x_data=None, lam=1e6, p=1e-2, lam_1=1e-4, max_iter=50, tol=1e-3, weights=None, diff_order=2):
36
"""
37
Improved Asymmetric Least Squares baseline correction.
38
39
Parameters:
40
- data (array-like): Input y-values to fit baseline
41
- x_data (array-like, optional): Input x-values
42
- lam (float): Primary smoothing parameter
43
- p (float): Asymmetry parameter
44
- lam_1 (float): Secondary smoothing parameter for adaptive adjustment
45
- max_iter (int): Maximum iterations
46
- tol (float): Convergence tolerance
47
- weights (array-like, optional): Initial weight array
48
- diff_order (int): Order of differential matrix
49
50
Returns:
51
tuple: (baseline, params)
52
"""
53
```
54
55
### Adaptive Iteratively Reweighted PLS (airPLS)
56
57
Automatically adjusts weights based on data characteristics without requiring manual parameter tuning.
58
59
```python { .api }
60
def airpls(data, lam=1e6, diff_order=2, max_iter=50, tol=1e-3, weights=None, x_data=None, check_finite=True):
61
"""
62
Adaptive iteratively reweighted Penalized Least Squares.
63
64
Parameters:
65
- data (array-like): Input y-values to fit baseline
66
- lam (float): Smoothing parameter
67
- diff_order (int): Order of differential matrix
68
- max_iter (int): Maximum iterations
69
- tol (float): Convergence tolerance
70
- weights (array-like, optional): Initial weight array
71
- x_data (array-like, optional): Input x-values
72
- check_finite (bool): Whether to check for finite values in input data
73
74
Returns:
75
tuple: (baseline, params)
76
"""
77
```
78
79
### Asymmetrically Reweighted PLS (arPLS)
80
81
Uses asymmetric reweighting to handle both positive and negative deviations from the baseline.
82
83
```python { .api }
84
def arpls(data, lam=1e5, diff_order=2, max_iter=50, tol=1e-3, weights=None, x_data=None):
85
"""
86
Asymmetrically reweighted Penalized Least Squares.
87
88
Parameters:
89
- data (array-like): Input y-values to fit baseline
90
- lam (float): Smoothing parameter
91
- diff_order (int): Order of differential matrix
92
- max_iter (int): Maximum iterations
93
- tol (float): Convergence tolerance
94
- weights (array-like, optional): Initial weight array
95
- x_data (array-like, optional): Input x-values
96
97
Returns:
98
tuple: (baseline, params)
99
"""
100
```
101
102
### Doubly Reweighted PLS (drPLS)
103
104
Applies dual reweighting strategies for improved baseline estimation in complex spectra.
105
106
```python { .api }
107
def drpls(data, lam=1e5, eta=0.5, max_iter=50, tol=1e-3, weights=None, diff_order=2, x_data=None):
108
"""
109
Doubly reweighted Penalized Least Squares.
110
111
Parameters:
112
- data (array-like): Input y-values to fit baseline
113
- lam (float): Smoothing parameter
114
- eta (float): Weighting parameter for dual reweighting (0 < eta < 1)
115
- max_iter (int): Maximum iterations
116
- tol (float): Convergence tolerance
117
- weights (array-like, optional): Initial weight array
118
- diff_order (int): Order of differential matrix
119
- x_data (array-like, optional): Input x-values
120
121
Returns:
122
tuple: (baseline, params)
123
"""
124
```
125
126
### Improved arPLS (IarPLS)
127
128
Enhanced version of arPLS with better convergence properties and peak detection.
129
130
```python { .api }
131
def iarpls(data, lam=1e5, diff_order=2, max_iter=50, tol=1e-3, weights=None, x_data=None):
132
"""
133
Improved asymmetrically reweighted Penalized Least Squares.
134
135
Parameters:
136
- data (array-like): Input y-values to fit baseline
137
- lam (float): Smoothing parameter
138
- diff_order (int): Order of differential matrix
139
- max_iter (int): Maximum iterations
140
- tol (float): Convergence tolerance
141
- weights (array-like, optional): Initial weight array
142
- x_data (array-like, optional): Input x-values
143
144
Returns:
145
tuple: (baseline, params)
146
"""
147
```
148
149
### Adaptive Smoothness PLS (asPLS)
150
151
Adapts smoothness based on local data characteristics for optimal baseline-peak separation.
152
153
```python { .api }
154
def aspls(data, lam=1e5, diff_order=2, max_iter=100, tol=1e-3, weights=None, alpha=None, x_data=None):
155
"""
156
Adaptive smoothness Penalized Least Squares.
157
158
Parameters:
159
- data (array-like): Input y-values to fit baseline
160
- lam (float): Smoothing parameter
161
- diff_order (int): Order of differential matrix
162
- max_iter (int): Maximum iterations
163
- tol (float): Convergence tolerance
164
- weights (array-like, optional): Initial weight array
165
- alpha (float, optional): Adaptive smoothness parameter
166
- x_data (array-like, optional): Input x-values
167
168
Returns:
169
tuple: (baseline, params)
170
"""
171
```
172
173
### Peaked Signal's AsLS Algorithm (psalsa)
174
175
Specifically designed for data with sharp, well-defined peaks using selective peak screening.
176
177
```python { .api }
178
def psalsa(data, lam=1e5, p=0.5, k=None, diff_order=2, max_iter=50, tol=1e-3, weights=None, x_data=None):
179
"""
180
Peaked Signal's Asymmetric Least Squares Algorithm.
181
182
Parameters:
183
- data (array-like): Input y-values to fit baseline
184
- lam (float): Smoothing parameter
185
- p (float): Asymmetry parameter
186
- k (int, optional): Number of points for peak screening
187
- diff_order (int): Order of differential matrix
188
- max_iter (int): Maximum iterations
189
- tol (float): Convergence tolerance
190
- weights (array-like, optional): Initial weight array
191
- x_data (array-like, optional): Input x-values
192
193
Returns:
194
tuple: (baseline, params)
195
"""
196
```
197
198
### Derivative Peak-Screening AsLS (derpsalsa)
199
200
Uses derivative information for enhanced peak detection and screening in complex spectra.
201
202
```python { .api }
203
def derpsalsa(data, lam=1e6, p=1e-2, k=None, diff_order=2, max_iter=50, tol=1e-3, weights=None, x_data=None):
204
"""
205
Derivative Peak-Screening Asymmetric Least Squares Algorithm.
206
207
Parameters:
208
- data (array-like): Input y-values to fit baseline
209
- lam (float): Smoothing parameter
210
- p (float): Asymmetry parameter
211
- k (int, optional): Number of points for peak screening
212
- diff_order (int): Order of differential matrix
213
- max_iter (int): Maximum iterations
214
- tol (float): Convergence tolerance
215
- weights (array-like, optional): Initial weight array
216
- x_data (array-like, optional): Input x-values
217
218
Returns:
219
tuple: (baseline, params)
220
"""
221
```
222
223
### Bayesian Reweighted PLS (BrPLS)
224
225
Incorporates Bayesian statistical principles for optimal weight determination.
226
227
```python { .api }
228
def brpls(data, x_data=None, lam=1e5, diff_order=2, max_iter=50, tol=1e-3, max_iter_2=50, tol_2=1e-3, weights=None):
229
"""
230
Bayesian Reweighted Penalized Least Squares.
231
232
Parameters:
233
- data (array-like): Input y-values to fit baseline
234
- x_data (array-like, optional): Input x-values
235
- lam (float): Smoothing parameter
236
- diff_order (int): Order of differential matrix
237
- max_iter (int): Maximum iterations for outer loop
238
- tol (float): Convergence tolerance for outer loop
239
- max_iter_2 (int): Maximum iterations for inner loop
240
- tol_2 (float): Convergence tolerance for inner loop
241
- weights (array-like, optional): Initial weight array
242
243
Returns:
244
tuple: (baseline, params)
245
"""
246
```
247
248
### Locally Symmetric Reweighted PLS (LSRPLS)
249
250
Uses local symmetry properties for improved baseline estimation in symmetric peak regions.
251
252
```python { .api }
253
def lsrpls(data, x_data=None, lam=1e5, diff_order=2, max_iter=50, tol=1e-3, weights=None):
254
"""
255
Locally Symmetric Reweighted Penalized Least Squares.
256
257
Parameters:
258
- data (array-like): Input y-values to fit baseline
259
- x_data (array-like, optional): Input x-values
260
- lam (float): Smoothing parameter
261
- diff_order (int): Order of differential matrix
262
- max_iter (int): Maximum iterations
263
- tol (float): Convergence tolerance
264
- weights (array-like, optional): Initial weight array
265
266
Returns:
267
tuple: (baseline, params)
268
"""
269
```
270
271
## Usage Examples
272
273
### Basic AsLS baseline correction:
274
275
```python
276
import numpy as np
277
from pybaselines.whittaker import asls
278
279
# Sample data with peaks and baseline drift
280
x = np.linspace(0, 1000, 1000)
281
data = 100 * np.exp(-((x - 200) / 30)**2) + 10 + 0.01 * x
282
283
# Apply AsLS baseline correction
284
baseline, params = asls(data, lam=1e6, p=1e-2, x_data=x)
285
corrected = data - baseline
286
287
print(f"Converged in {len(params['tol_history'])} iterations")
288
```
289
290
### Automatic baseline correction with airPLS:
291
292
```python
293
from pybaselines.whittaker import airpls
294
295
# No manual parameter tuning required
296
baseline, params = airpls(data, lam=1e6)
297
corrected = data - baseline
298
```