0
# Spline-Based Methods
1
2
Baseline correction algorithms using spline functions for flexible, smooth baseline estimation. These methods combine the smoothness of splines with various fitting strategies including penalized regression, quantile fitting, and morphological operations. They excel at handling complex baseline curvature while maintaining computational efficiency through knot-based approximation.
3
4
## Capabilities
5
6
### Mixture Model Spline Baseline
7
8
Estimates baseline using a mixture model approach with B-spline basis functions for optimal baseline-peak separation.
9
10
```python { .api }
11
def mixture_model(data, lam=1e5, p=1e-2, num_knots=100, spline_degree=3, diff_order=3, max_iter=50, tol=1e-3, weights=None, symmetric=False, num_bins=None):
12
"""
13
Mixture model baseline using splines.
14
15
Parameters:
16
- data (array-like): Input y-values to fit baseline
17
- lam (float): Smoothing parameter for spline regularization
18
- p (float): Asymmetry parameter for peak/baseline separation
19
- num_knots (int): Number of knots for spline basis
20
- spline_degree (int): Degree of B-spline basis functions
21
- diff_order (int): Order of difference penalty matrix
22
- max_iter (int): Maximum iterations for convergence
23
- tol (float): Convergence tolerance
24
- weights (array-like, optional): Initial weight array
25
- symmetric (bool): Whether to use symmetric weighting
26
- num_bins (int, optional): Number of histogram bins for mixture modeling
27
28
Returns:
29
tuple: (baseline, params) containing spline coefficients and fit statistics
30
"""
31
```
32
33
### Iterative Reweighted Spline Quantile Regression
34
35
Combines spline fitting with quantile regression for robust baseline estimation resistant to outliers.
36
37
```python { .api }
38
def irsqr(data, lam=100, quantile=0.05, num_knots=100, spline_degree=3, diff_order=3, max_iter=100, tol=1e-6, weights=None, x_data=None):
39
"""
40
Iterative reweighted spline quantile regression.
41
42
Parameters:
43
- data (array-like): Input y-values to fit baseline
44
- lam (float): Smoothing parameter
45
- quantile (float): Quantile level for regression (0 < quantile < 1)
46
- num_knots (int): Number of spline knots
47
- spline_degree (int): Degree of spline basis
48
- diff_order (int): Order of difference penalty
49
- max_iter (int): Maximum iterations
50
- tol (float): Convergence tolerance
51
- weights (array-like, optional): Initial weight array
52
- x_data (array-like, optional): Input x-values
53
54
Returns:
55
tuple: (baseline, params)
56
"""
57
```
58
59
### Corner-Cutting Algorithm
60
61
Geometric baseline estimation using iterative corner-cutting operations on spline representations.
62
63
```python { .api }
64
def corner_cutting(data, x_data=None, max_iter=100):
65
"""
66
Corner-cutting baseline algorithm.
67
68
Parameters:
69
- data (array-like): Input y-values to fit baseline
70
- x_data (array-like, optional): Input x-values
71
- max_iter (int): Maximum iterations for corner cutting
72
73
Returns:
74
tuple: (baseline, params) with geometric fitting information
75
"""
76
```
77
78
### Penalized Spline AsLS (pspline_asls)
79
80
Spline-based implementation of Asymmetric Least Squares using B-spline basis functions.
81
82
```python { .api }
83
def pspline_asls(data, lam=1e3, p=1e-2, num_knots=100, spline_degree=3, diff_order=2, max_iter=50, tol=1e-3, weights=None, x_data=None):
84
"""
85
Penalized spline version of AsLS.
86
87
Parameters:
88
- data (array-like): Input y-values to fit baseline
89
- lam (float): Smoothing parameter
90
- p (float): Asymmetry parameter
91
- num_knots (int): Number of spline knots
92
- spline_degree (int): Degree of B-spline basis
93
- diff_order (int): Order of difference penalty
94
- max_iter (int): Maximum iterations
95
- tol (float): Convergence tolerance
96
- weights (array-like, optional): Initial weight array
97
- x_data (array-like, optional): Input x-values
98
99
Returns:
100
tuple: (baseline, params)
101
"""
102
```
103
104
### Penalized Spline IAsLS (pspline_iasls)
105
106
Enhanced spline-based AsLS with improved convergence and parameter adaptation.
107
108
```python { .api }
109
def pspline_iasls(data, x_data=None, lam=1e1, p=1e-2, lam_1=1e-4, num_knots=100, spline_degree=3, diff_order=2, max_iter=50, tol=1e-3, weights=None):
110
"""
111
Penalized spline version of IAsLS.
112
113
Parameters:
114
- data (array-like): Input y-values to fit baseline
115
- x_data (array-like, optional): Input x-values
116
- lam (float): Primary smoothing parameter
117
- p (float): Asymmetry parameter
118
- lam_1 (float): Secondary smoothing parameter for adaptation
119
- num_knots (int): Number of spline knots
120
- spline_degree (int): Degree of B-spline basis
121
- diff_order (int): Order of difference penalty
122
- max_iter (int): Maximum iterations
123
- tol (float): Convergence tolerance
124
- weights (array-like, optional): Initial weight array
125
126
Returns:
127
tuple: (baseline, params)
128
"""
129
```
130
131
### Penalized Spline airPLS (pspline_airpls)
132
133
Spline implementation of adaptive iteratively reweighted PLS for automatic parameter adjustment.
134
135
```python { .api }
136
def pspline_airpls(data, lam=1e3, num_knots=100, spline_degree=3, diff_order=2, max_iter=50, tol=1e-3, weights=None, x_data=None, normalize_weights=False):
137
"""
138
Penalized spline version of airPLS.
139
140
Parameters:
141
- data (array-like): Input y-values to fit baseline
142
- lam (float): Smoothing parameter
143
- num_knots (int): Number of spline knots
144
- spline_degree (int): Degree of B-spline basis
145
- diff_order (int): Order of difference penalty
146
- max_iter (int): Maximum iterations
147
- tol (float): Convergence tolerance
148
- weights (array-like, optional): Initial weight array
149
- x_data (array-like, optional): Input x-values
150
- normalize_weights (bool): Whether to normalize final weights
151
152
Returns:
153
tuple: (baseline, params)
154
"""
155
```
156
157
### Penalized Spline arPLS (pspline_arpls)
158
159
Spline version of asymmetrically reweighted PLS with flexible basis representation.
160
161
```python { .api }
162
def pspline_arpls(data, lam=1e3, num_knots=100, spline_degree=3, diff_order=2, max_iter=50, tol=1e-3, weights=None, x_data=None):
163
"""
164
Penalized spline version of arPLS.
165
166
Parameters:
167
- data (array-like): Input y-values to fit baseline
168
- lam (float): Smoothing parameter
169
- num_knots (int): Number of spline knots
170
- spline_degree (int): Degree of B-spline basis
171
- diff_order (int): Order of difference penalty
172
- max_iter (int): Maximum iterations
173
- tol (float): Convergence tolerance
174
- weights (array-like, optional): Initial weight array
175
- x_data (array-like, optional): Input x-values
176
177
Returns:
178
tuple: (baseline, params)
179
"""
180
```
181
182
### Penalized Spline drPLS (pspline_drpls)
183
184
Doubly reweighted PLS using spline basis with enhanced robustness for complex spectra.
185
186
```python { .api }
187
def pspline_drpls(data, lam=1e3, eta=0.5, num_knots=100, spline_degree=3, diff_order=2, max_iter=50, tol=1e-3, weights=None, x_data=None):
188
"""
189
Penalized spline version of drPLS.
190
191
Parameters:
192
- data (array-like): Input y-values to fit baseline
193
- lam (float): Smoothing parameter
194
- eta (float): Weighting parameter for dual reweighting
195
- num_knots (int): Number of spline knots
196
- spline_degree (int): Degree of B-spline basis
197
- diff_order (int): Order of difference penalty
198
- max_iter (int): Maximum iterations
199
- tol (float): Convergence tolerance
200
- weights (array-like, optional): Initial weight array
201
- x_data (array-like, optional): Input x-values
202
203
Returns:
204
tuple: (baseline, params)
205
"""
206
```
207
208
### Penalized Spline IarPLS (pspline_iarpls)
209
210
Improved spline-based arPLS with enhanced convergence and peak handling capabilities.
211
212
```python { .api }
213
def pspline_iarpls(data, lam=1e3, num_knots=100, spline_degree=3, diff_order=2, max_iter=50, tol=1e-3, weights=None, x_data=None):
214
"""
215
Penalized spline version of IarPLS.
216
217
Parameters:
218
- data (array-like): Input y-values to fit baseline
219
- lam (float): Smoothing parameter
220
- num_knots (int): Number of spline knots
221
- spline_degree (int): Degree of B-spline basis
222
- diff_order (int): Order of difference penalty
223
- max_iter (int): Maximum iterations
224
- tol (float): Convergence tolerance
225
- weights (array-like, optional): Initial weight array
226
- x_data (array-like, optional): Input x-values
227
228
Returns:
229
tuple: (baseline, params)
230
"""
231
```
232
233
### Penalized Spline asPLS (pspline_aspls)
234
235
Adaptive smoothness PLS using splines for optimal local smoothing based on data characteristics.
236
237
```python { .api }
238
def pspline_aspls(data, lam=1e4, num_knots=100, spline_degree=3, diff_order=2, max_iter=100, tol=1e-3, weights=None, alpha=None, x_data=None, asymmetric_coef=0.5):
239
"""
240
Penalized spline version of asPLS.
241
242
Parameters:
243
- data (array-like): Input y-values to fit baseline
244
- lam (float): Smoothing parameter
245
- num_knots (int): Number of spline knots
246
- spline_degree (int): Degree of B-spline basis
247
- diff_order (int): Order of difference penalty
248
- max_iter (int): Maximum iterations
249
- tol (float): Convergence tolerance
250
- weights (array-like, optional): Initial weight array
251
- alpha (float, optional): Adaptive smoothness parameter
252
- x_data (array-like, optional): Input x-values
253
- asymmetric_coef (float): Asymmetry coefficient
254
255
Returns:
256
tuple: (baseline, params)
257
"""
258
```
259
260
### Penalized Spline psalsa (pspline_psalsa)
261
262
Spline implementation of peaked signal's AsLS algorithm with selective peak screening.
263
264
```python { .api }
265
def pspline_psalsa(data, lam=1e3, p=0.5, k=None, num_knots=100, spline_degree=3, diff_order=2, max_iter=50, tol=1e-3, weights=None, x_data=None):
266
"""
267
Penalized spline version of psalsa.
268
269
Parameters:
270
- data (array-like): Input y-values to fit baseline
271
- lam (float): Smoothing parameter
272
- p (float): Asymmetry parameter
273
- k (int, optional): Number of points for peak screening
274
- num_knots (int): Number of spline knots
275
- spline_degree (int): Degree of B-spline basis
276
- diff_order (int): Order of difference penalty
277
- max_iter (int): Maximum iterations
278
- tol (float): Convergence tolerance
279
- weights (array-like, optional): Initial weight array
280
- x_data (array-like, optional): Input x-values
281
282
Returns:
283
tuple: (baseline, params)
284
"""
285
```
286
287
### Penalized Spline derpsalsa (pspline_derpsalsa)
288
289
Derivative peak-screening AsLS using splines with enhanced peak detection capabilities.
290
291
```python { .api }
292
def pspline_derpsalsa(data, lam=1e2, p=1e-2, k=None, num_knots=100, spline_degree=3, diff_order=2, max_iter=50, tol=1e-3, weights=None, smooth_half_window=None, num_smooths=16, x_data=None, pad_kwargs=None, **kwargs):
293
"""
294
Penalized spline version of derpsalsa.
295
296
Parameters:
297
- data (array-like): Input y-values to fit baseline
298
- lam (float): Smoothing parameter
299
- p (float): Asymmetry parameter
300
- k (int, optional): Number of points for peak screening
301
- num_knots (int): Number of spline knots
302
- spline_degree (int): Degree of B-spline basis
303
- diff_order (int): Order of difference penalty
304
- max_iter (int): Maximum iterations
305
- tol (float): Convergence tolerance
306
- weights (array-like, optional): Initial weight array
307
- smooth_half_window (int, optional): Half-window for derivative smoothing
308
- num_smooths (int): Number of smoothing operations
309
- x_data (array-like, optional): Input x-values
310
- pad_kwargs (dict, optional): Padding parameters
311
312
Returns:
313
tuple: (baseline, params)
314
"""
315
```
316
317
### Morphological Penalized Spline (mpspline)
318
319
Combines morphological operations with penalized spline fitting for robust baseline estimation.
320
321
```python { .api }
322
def mpspline(data, half_window=None, lam=1e3, p=0.0, num_knots=100, spline_degree=3, diff_order=2, tol=1e-3, max_iter=50, weights=None, x_data=None, pad_kwargs=None, **kwargs):
323
"""
324
Morphological penalized spline baseline correction.
325
326
Parameters:
327
- data (array-like): Input y-values to fit baseline
328
- half_window (int, optional): Half-window size for morphological operations
329
- lam (float): Smoothing parameter for spline regularization
330
- p (float): Asymmetry parameter
331
- num_knots (int): Number of spline knots
332
- spline_degree (int): Degree of B-spline basis
333
- diff_order (int): Order of difference penalty
334
- tol (float): Convergence tolerance
335
- max_iter (int): Maximum iterations
336
- weights (array-like, optional): Initial weight array
337
- x_data (array-like, optional): Input x-values
338
- pad_kwargs (dict, optional): Padding parameters for morphological operations
339
340
Returns:
341
tuple: (baseline, params)
342
"""
343
```
344
345
### Penalized Spline BrPLS (pspline_brpls)
346
347
Bayesian reweighted PLS using splines with statistical weight optimization.
348
349
```python { .api }
350
def pspline_brpls(data, x_data=None, lam=1e3, num_knots=100, spline_degree=3, diff_order=2, max_iter=50, tol=1e-3, max_iter_2=50, tol_2=1e-3, weights=None):
351
"""
352
Penalized spline version of BrPLS.
353
354
Parameters:
355
- data (array-like): Input y-values to fit baseline
356
- x_data (array-like, optional): Input x-values
357
- lam (float): Smoothing parameter
358
- num_knots (int): Number of spline knots
359
- spline_degree (int): Degree of B-spline basis
360
- diff_order (int): Order of difference penalty
361
- max_iter (int): Maximum iterations for outer loop
362
- tol (float): Convergence tolerance for outer loop
363
- max_iter_2 (int): Maximum iterations for inner loop
364
- tol_2 (float): Convergence tolerance for inner loop
365
- weights (array-like, optional): Initial weight array
366
367
Returns:
368
tuple: (baseline, params)
369
"""
370
```
371
372
### Penalized Spline LSRPLS (pspline_lsrpls)
373
374
Locally symmetric reweighted PLS using splines for symmetric peak region handling.
375
376
```python { .api }
377
def pspline_lsrpls(data, x_data=None, lam=1e3, num_knots=100, spline_degree=3, diff_order=2, max_iter=50, tol=1e-3, weights=None):
378
"""
379
Penalized spline version of LSRPLS.
380
381
Parameters:
382
- data (array-like): Input y-values to fit baseline
383
- x_data (array-like, optional): Input x-values
384
- lam (float): Smoothing parameter
385
- num_knots (int): Number of spline knots
386
- spline_degree (int): Degree of B-spline basis
387
- diff_order (int): Order of difference penalty
388
- max_iter (int): Maximum iterations
389
- tol (float): Convergence tolerance
390
- weights (array-like, optional): Initial weight array
391
392
Returns:
393
tuple: (baseline, params)
394
"""
395
```
396
397
## Usage Examples
398
399
### Flexible spline baseline with mixture model:
400
401
```python
402
import numpy as np
403
from pybaselines.spline import mixture_model
404
405
# Sample data with complex baseline curvature
406
x = np.linspace(0, 1000, 2000)
407
baseline_true = 50 + 20 * np.sin(0.01 * x) + 0.005 * x**2
408
peaks = 100 * np.exp(-((x - 200) / 30)**2) + 80 * np.exp(-((x - 600) / 40)**2)
409
data = baseline_true + peaks + np.random.normal(0, 2, len(x))
410
411
# Estimate baseline using mixture model
412
baseline, params = mixture_model(data, lam=1e5, num_knots=50, spline_degree=3)
413
corrected = data - baseline
414
415
print(f"Used {params.get('num_knots', 50)} knots for spline approximation")
416
```
417
418
### Robust quantile spline fitting:
419
420
```python
421
from pybaselines.spline import irsqr
422
423
# Robust baseline at 5th percentile using splines
424
baseline, params = irsqr(data, quantile=0.05, num_knots=100, lam=100)
425
corrected = data - baseline
426
427
# More flexible than polynomial quantile regression for complex baselines
428
```
429
430
### High-efficiency spline AsLS:
431
432
```python
433
from pybaselines.spline import pspline_asls
434
435
# Fast spline-based AsLS with fewer knots for efficiency
436
baseline, params = pspline_asls(data, lam=1e3, p=1e-2, num_knots=50)
437
corrected = data - baseline
438
439
# Computationally efficient while maintaining smoothness
440
```