or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

classification.mdindex.mdmisc.mdmorphological.mdoptimizers.mdpolynomial.mdsmooth.mdspline.mdtwo-d.mdwhittaker.md

misc.mddocs/

0

# Miscellaneous Methods

1

2

Specialized baseline correction algorithms that don't fit into standard categories. These methods use unique approaches like simultaneous denoising and baseline estimation, or direct interpolation between user-defined baseline points. They provide alternative solutions for specific data types and use cases.

3

4

## Capabilities

5

6

### BEADS (Baseline Estimation And Denoising using Splines)

7

8

Advanced method that simultaneously performs baseline correction and noise reduction using spline-based optimization with multiple regularization terms.

9

10

```python { .api }

11

def beads(data, freq_cutoff=0.005, lam_0=1.0, lam_1=1.0, lam_2=1.0, asymmetry=6.0, filter_type=1, cost_function=2, max_iter=50, tol=1e-2, eps_0=1e-6, eps_1=1e-6, fit_parabola=True, smooth_half_window=None, x_data=None):

12

"""

13

Baseline Estimation And Denoising using Splines (BEADS).

14

15

Simultaneously estimates baseline and reduces noise through multi-objective

16

optimization with spline smoothing and asymmetric penalties.

17

18

Parameters:

19

- data (array-like): Input y-values to process for baseline and denoising

20

- freq_cutoff (float): Cutoff frequency for high-pass filter (0 < freq_cutoff < 0.5)

21

- lam_0 (float): Regularization parameter for baseline smoothness

22

- lam_1 (float): Regularization parameter for first derivative penalty

23

- lam_2 (float): Regularization parameter for second derivative penalty

24

- asymmetry (float): Asymmetry parameter for baseline-peak separation

25

- filter_type (int): Type of high-pass filter (1 or 2)

26

- cost_function (int): Cost function for optimization (1 or 2)

27

- max_iter (int): Maximum iterations for optimization convergence

28

- tol (float): Convergence tolerance for iterative fitting

29

- eps_0 (float): Tolerance parameter for baseline estimation

30

- eps_1 (float): Tolerance parameter for noise reduction

31

- fit_parabola (bool): Whether to fit parabolic trend to data

32

- smooth_half_window (int, optional): Half-window size for final smoothing

33

- x_data (array-like, optional): Input x-values

34

35

Returns:

36

tuple: (baseline, params) where params contains both baseline and denoised signal

37

Additional keys: 'denoised_signal', 'optimization_history'

38

"""

39

```

40

41

### Interpolated Baseline Points

42

43

Direct baseline estimation by interpolating between user-specified baseline points using various interpolation methods.

44

45

```python { .api }

46

def interp_pts(data, baseline_points, interp_method='linear', x_data=None):

47

"""

48

Interpolate baseline between specified baseline points.

49

50

Creates baseline by interpolating between manually identified or

51

algorithmically determined baseline points using specified interpolation method.

52

53

Parameters:

54

- data (array-like): Input y-values (used primarily for output array dimensions)

55

- baseline_points (array-like): Indices or (x,y) coordinates of baseline points

56

If 1D array: treated as indices into data array

57

If 2D array: treated as (x,y) coordinate pairs

58

- interp_method (str): Interpolation method for baseline construction

59

Options: 'linear', 'cubic', 'quadratic', 'nearest', 'pchip'

60

- x_data (array-like, optional): Input x-values for coordinate-based interpolation

61

62

Returns:

63

tuple: (baseline, params) where params contains interpolation details

64

Additional keys: 'interp_method', 'baseline_points_used', 'x_baseline_points'

65

"""

66

```

67

68

## Usage Examples

69

70

### Simultaneous baseline correction and denoising with BEADS:

71

72

```python

73

import numpy as np

74

from pybaselines.misc import beads

75

76

# Sample noisy spectroscopic data with baseline drift

77

x = np.linspace(0, 1000, 1500)

78

baseline_true = 20 + 0.03 * x + 0.00002 * x**2

79

peaks = (200 * np.exp(-((x - 250) / 40)**2) +

80

150 * np.exp(-((x - 600) / 35)**2) +

81

180 * np.exp(-((x - 850) / 45)**2))

82

noise = np.random.normal(0, 5, len(x)) # Significant noise

83

data = baseline_true + peaks + noise

84

85

# BEADS performs both baseline correction and denoising

86

baseline, params = beads(data, lam_0=0.5, lam_1=5, lam_2=4, asymmetry=0.1)

87

corrected = data - baseline

88

denoised = params['denoised_signal'] # Denoised version of original data

89

90

print(f"Baseline estimation converged in {params.get('n_iter', 'N/A')} iterations")

91

print(f"Noise reduction factor: {np.std(data)/np.std(denoised):.2f}")

92

```

93

94

### Manual baseline point interpolation:

95

96

```python

97

from pybaselines.misc import interp_pts

98

99

# Define baseline points manually (could be from peak picking algorithms)

100

# Using indices into the data array

101

baseline_indices = [0, 150, 300, 500, 750, 1000, 1499] # Points known to be baseline

102

103

# Create baseline by linear interpolation

104

baseline, params = interp_pts(data, baseline_indices, interp_method='linear')

105

corrected = data - baseline

106

107

print(f"Interpolated between {len(baseline_indices)} baseline points")

108

print(f"Used {params['interp_method']} interpolation method")

109

```

110

111

### Advanced interpolation with coordinate pairs:

112

113

```python

114

# Alternative: specify (x, y) coordinate pairs for baseline points

115

x_points = np.array([0, 150, 300, 500, 750, 1000])

116

y_points = data[x_points] # or manually determined y-values

117

118

# Stack into (x, y) coordinate pairs

119

baseline_coords = np.column_stack([x_points, y_points])

120

121

# Use cubic spline interpolation for smooth baseline

122

baseline, params = interp_pts(data, baseline_coords,

123

interp_method='cubic', x_data=x)

124

corrected = data - baseline

125

```

126

127

### BEADS with parameter optimization for different noise levels:

128

129

```python

130

# Adjust BEADS parameters based on data characteristics

131

noise_level = np.std(np.diff(data)) # Estimate noise from differences

132

133

if noise_level > 5:

134

# High noise: stronger denoising

135

lam_1_opt = 10

136

lam_2_opt = 6

137

elif noise_level > 2:

138

# Medium noise: balanced approach

139

lam_1_opt = 5

140

lam_2_opt = 4

141

else:

142

# Low noise: gentle denoising

143

lam_1_opt = 2

144

lam_2_opt = 2

145

146

baseline, params = beads(data, lam_0=0.5, lam_1=lam_1_opt,

147

lam_2=lam_2_opt, asymmetry=0.1)

148

corrected = data - baseline

149

denoised = params['denoised_signal']

150

151

print(f"Optimized for noise level: {noise_level:.2f}")

152

```

153

154

### Combining methods - BEADS preprocessing with interpolation refinement:

155

156

```python

157

# First pass: BEADS for general baseline and denoising

158

baseline_beads, params_beads = beads(data, lam_0=0.8, lam_1=3, lam_2=3)

159

denoised_data = params_beads['denoised_signal']

160

161

# Second pass: identify remaining baseline points in denoised data

162

# (could use peak detection algorithms here)

163

from scipy.signal import find_peaks

164

peaks_idx, _ = find_peaks(denoised_data - baseline_beads, height=10)

165

166

# Find valleys between peaks as baseline points

167

baseline_points = []

168

for i in range(len(peaks_idx) - 1):

169

start_idx = peaks_idx[i] + 20 # Skip peak region

170

end_idx = peaks_idx[i + 1] - 20 # Skip next peak region

171

if start_idx < end_idx:

172

valley_idx = start_idx + np.argmin(denoised_data[start_idx:end_idx])

173

baseline_points.append(valley_idx)

174

175

# Add endpoints

176

baseline_points = [0] + baseline_points + [len(data) - 1]

177

178

# Refine baseline using interpolation

179

baseline_final, params_interp = interp_pts(denoised_data, baseline_points,

180

interp_method='cubic')

181

corrected_final = denoised_data - baseline_final

182

183

print(f"Two-stage correction: BEADS + interpolation with {len(baseline_points)} points")

184

```