0
# Polynomial Fitting Methods
1
2
Polynomial-based baseline correction using various fitting strategies and outlier handling approaches. These methods fit polynomial functions to data while minimizing the influence of peaks through iterative weighting, masking, or robust fitting techniques. They are suitable for simple baseline shapes and when interpretable parameters are desired.
3
4
## Capabilities
5
6
### Basic Polynomial Fitting
7
8
Standard least squares polynomial fitting without iterative reweighting.
9
10
```python { .api }
11
def poly(data, x_data=None, poly_order=2, weights=None, return_coef=False):
12
"""
13
Basic polynomial baseline fitting using least squares.
14
15
Parameters:
16
- data (array-like): Input y-values to fit baseline
17
- x_data (array-like, optional): Input x-values
18
- poly_order (int): Order of polynomial (0=constant, 1=linear, 2=quadratic, etc.)
19
- weights (array-like, optional): Weight array for data points
20
- return_coef (bool): Whether to return polynomial coefficients in params
21
22
Returns:
23
tuple: (baseline, params) where params may contain 'coef' if return_coef=True
24
"""
25
```
26
27
### Modified Polynomial (modpoly)
28
29
Iteratively fits polynomials while masking data points that deviate significantly from the current fit.
30
31
```python { .api }
32
def modpoly(data, poly_order=2, tol=1e-3, max_iter=250, weights=None, use_original=False, mask_initial_peaks=True, num_std=1.0, x_data=None, return_coef=False):
33
"""
34
Modified polynomial baseline with iterative peak masking.
35
36
Parameters:
37
- data (array-like): Input y-values to fit baseline
38
- poly_order (int): Order of polynomial
39
- tol (float): Convergence tolerance for coefficient changes
40
- max_iter (int): Maximum iterations
41
- weights (array-like, optional): Initial weight array
42
- use_original (bool): Whether to compare against original data for convergence
43
- mask_initial_peaks (bool): Whether to mask obvious peaks in first iteration
44
- num_std (float): Number of standard deviations for peak masking threshold
45
- x_data (array-like, optional): Input x-values
46
- return_coef (bool): Whether to return polynomial coefficients
47
48
Returns:
49
tuple: (baseline, params) containing 'weights' and optionally 'coef'
50
"""
51
```
52
53
### Improved Modified Polynomial (imodpoly)
54
55
Enhanced version of modpoly with better peak detection and convergence criteria.
56
57
```python { .api }
58
def imodpoly(data, x_data=None, poly_order=2, tol=1e-3, max_iter=250, weights=None, use_original=False, mask_initial_peaks=False, return_coef=False):
59
"""
60
Improved modified polynomial baseline with enhanced peak masking.
61
62
Parameters:
63
- data (array-like): Input y-values to fit baseline
64
- poly_order (int): Order of polynomial
65
- tol (float): Convergence tolerance
66
- max_iter (int): Maximum iterations
67
- weights (array-like, optional): Initial weight array
68
- use_original (bool): Whether to use original data for convergence check
69
- mask_initial_peaks (bool): Whether to mask peaks initially
70
- return_coef (bool): Whether to return coefficients
71
72
Returns:
73
tuple: (baseline, params)
74
"""
75
```
76
77
### Penalized Polynomial
78
79
Combines polynomial fitting with penalty functions for outlier rejection and robust baseline estimation.
80
81
```python { .api }
82
def penalized_poly(data, x_data=None, poly_order=2, tol=1e-3, max_iter=250, weights=None, cost_function='asymmetric_truncated_quadratic', threshold=None, alpha_factor=0.99, ensure_finite=True, return_coef=False):
83
"""
84
Penalized polynomial baseline with robust cost functions.
85
86
Parameters:
87
- data (array-like): Input y-values to fit baseline
88
- poly_order (int): Order of polynomial
89
- tol (float): Convergence tolerance
90
- max_iter (int): Maximum iterations
91
- weights (array-like, optional): Initial weight array
92
- cost_function (str): Penalty function type ('asymmetric_truncated_quadratic', 'symmetric_truncated_quadratic', etc.)
93
- threshold (float, optional): Penalty threshold
94
- alpha_factor (float): Factor for threshold adaptation (0 < alpha_factor < 1)
95
- ensure_finite (bool): Whether to ensure all values are finite
96
- return_coef (bool): Whether to return coefficients
97
98
Returns:
99
tuple: (baseline, params)
100
"""
101
```
102
103
### Quantile Regression
104
105
Fits polynomials using quantile regression to estimate baseline at specified quantile levels.
106
107
```python { .api }
108
def quant_reg(data, poly_order=2, quantile=0.05, tol=1e-6, max_iter=1000, weights=None, eps=None, x_data=None, return_coef=False):
109
"""
110
Quantile regression polynomial baseline fitting.
111
112
Parameters:
113
- data (array-like): Input y-values to fit baseline
114
- poly_order (int): Order of polynomial
115
- quantile (float): Quantile level for regression (0 < quantile < 1)
116
- tol (float): Convergence tolerance for optimization
117
- max_iter (int): Maximum iterations for optimization
118
- weights (array-like, optional): Weight array
119
- eps (float, optional): Smoothing parameter for quantile loss
120
- x_data (array-like, optional): Input x-values
121
- return_coef (bool): Whether to return coefficients
122
123
Returns:
124
tuple: (baseline, params)
125
"""
126
```
127
128
### LOESS/LOWESS Regression
129
130
Locally weighted regression using polynomials for flexible baseline fitting that adapts to local data characteristics.
131
132
```python { .api }
133
def loess(data, x_data=None, fraction=0.2, total_points=None, poly_order=1, scale=3.0, tol=1e-6, max_iter=250, weights=None, use_threshold=False, return_coef=False):
134
"""
135
LOESS (locally weighted regression) baseline fitting.
136
137
Parameters:
138
- data (array-like): Input y-values to fit baseline
139
- x_data (array-like, optional): Input x-values
140
- fraction (float): Fraction of data points used for local regression (0 < fraction <= 1)
141
- total_points (int, optional): Alternative to fraction - exact number of points
142
- poly_order (int): Order of local polynomial (0=constant, 1=linear, 2=quadratic)
143
- scale (float): Scale factor for robust weighting
144
- tol (float): Convergence tolerance
145
- max_iter (int): Maximum iterations for robust fitting
146
- weights (array-like, optional): Global weight array
147
- use_threshold (bool): Whether to use threshold-based robust weighting
148
- return_coef (bool): Whether to return local coefficients
149
150
Returns:
151
tuple: (baseline, params)
152
"""
153
```
154
155
### Goldindec Algorithm
156
157
Combines polynomial fitting with peak detection and iterative coefficient adjustment.
158
159
```python { .api }
160
def goldindec(data, x_data=None, poly_order=2, tol=1e-3, max_iter=250, weights=None, cost_function='asymmetric_truncated_quadratic', threshold=None, alpha_factor=0.99, return_coef=False):
161
"""
162
Goldindec polynomial baseline algorithm with peak detection.
163
164
Parameters:
165
- data (array-like): Input y-values to fit baseline
166
- poly_order (int): Order of polynomial
167
- tol (float): Convergence tolerance
168
- max_iter (int): Maximum iterations
169
- weights (array-like, optional): Initial weight array
170
- cost_function (str): Cost function for penalty
171
- threshold (float, optional): Penalty threshold
172
- alpha_factor (float): Threshold adaptation factor
173
- return_coef (bool): Whether to return coefficients
174
175
Returns:
176
tuple: (baseline, params)
177
"""
178
```
179
180
## Usage Examples
181
182
### Basic polynomial baseline:
183
184
```python
185
import numpy as np
186
from pybaselines.polynomial import poly
187
188
# Sample data with quadratic baseline
189
x = np.linspace(0, 100, 1000)
190
true_baseline = 10 + 0.1 * x + 0.001 * x**2
191
peaks = 50 * np.exp(-((x - 30) / 5)**2) + 30 * np.exp(-((x - 70) / 8)**2)
192
data = true_baseline + peaks + np.random.normal(0, 1, len(x))
193
194
# Fit quadratic polynomial baseline
195
baseline, params = poly(data, poly_order=2, x_data=x, return_coef=True)
196
corrected = data - baseline
197
198
print(f"Polynomial coefficients: {params['coef']}")
199
```
200
201
### Iterative polynomial with peak masking:
202
203
```python
204
from pybaselines.polynomial import modpoly
205
206
# Automatically mask peaks and fit polynomial
207
baseline, params = modpoly(data, poly_order=2, num_std=1.5, x_data=x)
208
corrected = data - baseline
209
210
# Check which points were masked
211
masked_points = params['weights'] < 0.5
212
print(f"Masked {np.sum(masked_points)} peak points")
213
```
214
215
### Quantile regression for robust fitting:
216
217
```python
218
from pybaselines.polynomial import quant_reg
219
220
# Fit polynomial at 5th percentile (robust to outliers)
221
baseline, params = quant_reg(data, poly_order=2, quantile=0.05, x_data=x)
222
corrected = data - baseline
223
```