baselines 0.1.1

Baseline correction algorithms for signals, spectra, and images
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
//! Two-dimensional morphology and smoothing baseline algorithms.
//!
//! # References
//!
//! - M. Kneen and H. Annegarn, "Algorithm for fitting XRF, SEM and PIXE
//!   X-ray spectra backgrounds", *Nuclear Instruments and Methods in Physics
//!   Research Section B*, 1996.
//! - L. Dai et al., "An Automated Baseline Correction Method Based on
//!   Iterative Morphological Operations", *Applied Spectroscopy*, 2018.
//! - `pybaselines.Baseline2D` morphology methods are used as behavioral
//!   references.

use crate::data::{MatrixView, MatrixViewMut};
use crate::fit::{Fit2D, FitReport};
use crate::{BaselineError, Result};

const IMOR_DEFAULT_MAX_ITER: usize = 200;
const IMOR_DEFAULT_TOL: f64 = 1.0e-3;

/// Parameters for two-dimensional window-based morphology baselines.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Morphology2DParams {
    /// Full moving-window row count. Even values are accepted and use
    /// `window_rows / 2` samples on each side.
    pub window_rows: usize,
    /// Full moving-window column count. Even values are accepted and use
    /// `window_cols / 2` samples on each side.
    pub window_cols: usize,
}

impl Default for Morphology2DParams {
    fn default() -> Self {
        Self {
            window_rows: 7,
            window_cols: 7,
        }
    }
}

impl Morphology2DParams {
    fn validate(self) -> Result<()> {
        if self.window_rows == 0 {
            return Err(BaselineError::InvalidParameter {
                name: "window_rows",
                reason: "must be greater than zero",
            });
        }
        if self.window_cols == 0 {
            return Err(BaselineError::InvalidParameter {
                name: "window_cols",
                reason: "must be greater than zero",
            });
        }
        Ok(())
    }

    fn radii(self) -> (usize, usize) {
        (self.window_rows / 2, self.window_cols / 2)
    }
}

/// Parameters for [`imor`].
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Imor2DParams {
    /// Shared morphology window parameters.
    pub morphology: Morphology2DParams,
    /// Maximum number of IMor update iterations.
    pub max_iter: usize,
    /// Relative baseline-change tolerance.
    pub tol: f64,
}

impl Default for Imor2DParams {
    fn default() -> Self {
        Self {
            morphology: Morphology2DParams::default(),
            max_iter: IMOR_DEFAULT_MAX_ITER,
            tol: IMOR_DEFAULT_TOL,
        }
    }
}

impl Imor2DParams {
    fn validate(self) -> Result<()> {
        self.morphology.validate()?;
        if self.max_iter == 0 {
            return Err(BaselineError::InvalidParameter {
                name: "max_iter",
                reason: "must be greater than zero",
            });
        }
        if !self.tol.is_finite() || self.tol <= 0.0 {
            return Err(BaselineError::InvalidParameter {
                name: "tol",
                reason: "must be finite and positive",
            });
        }
        Ok(())
    }
}

/// Estimates a 2D rolling-ball style baseline.
///
/// # References
///
/// - `pybaselines.Baseline2D.rolling_ball` is used as a behavioral reference.
pub fn rolling_ball(input: MatrixView<'_>, params: Morphology2DParams) -> Result<Fit2D> {
    let mut baseline = vec![0.0; input.len()];
    let output = MatrixViewMut::row_major(&mut baseline, input.rows(), input.cols())?;
    let report = rolling_ball_into(input, params, output)?;
    Fit2D::new(baseline, input.rows(), input.cols(), report)
}

/// Estimates a 2D rolling-ball baseline into an existing output buffer.
pub fn rolling_ball_into(
    input: MatrixView<'_>,
    params: Morphology2DParams,
    mut output: MatrixViewMut<'_>,
) -> Result<FitReport> {
    validate_input_output(input, &output, params)?;
    let (row_radius, col_radius) = params.radii();
    let shape = MorphologyShape::new(input.rows(), input.cols());
    let window = MorphologyWindow::new(row_radius, col_radius);
    let mut scratch = vec![0.0; input.len()];
    let mut eroded = vec![0.0; input.len()];
    let mut opened = vec![0.0; input.len()];
    opening_reflect_into(
        input.as_slice(),
        shape,
        window,
        &mut scratch,
        &mut eroded,
        &mut opened,
    );
    moving_mean_reflect(
        &opened,
        input.rows(),
        input.cols(),
        row_radius,
        col_radius,
        output.as_mut_slice(),
    );
    Ok(FitReport::new(1, true, 0.0))
}

/// Estimates a 2D top-hat baseline using morphological opening.
///
/// # References
///
/// - `pybaselines.Baseline2D.tophat` is used as a behavioral reference.
pub fn tophat(input: MatrixView<'_>, params: Morphology2DParams) -> Result<Fit2D> {
    let mut baseline = vec![0.0; input.len()];
    let output = MatrixViewMut::row_major(&mut baseline, input.rows(), input.cols())?;
    let report = tophat_into(input, params, output)?;
    Fit2D::new(baseline, input.rows(), input.cols(), report)
}

/// Estimates a 2D top-hat baseline into an existing output buffer.
pub fn tophat_into(
    input: MatrixView<'_>,
    params: Morphology2DParams,
    mut output: MatrixViewMut<'_>,
) -> Result<FitReport> {
    validate_input_output(input, &output, params)?;
    let (row_radius, col_radius) = params.radii();
    let shape = MorphologyShape::new(input.rows(), input.cols());
    let window = MorphologyWindow::new(row_radius, col_radius);
    let mut scratch = vec![0.0; input.len()];
    let mut eroded = vec![0.0; input.len()];
    opening_reflect_into(
        input.as_slice(),
        shape,
        window,
        &mut scratch,
        &mut eroded,
        output.as_mut_slice(),
    );
    Ok(FitReport::new(1, true, 0.0))
}

/// Estimates a 2D morphology baseline from an opening and averaged envelope.
///
/// # References
///
/// - `pybaselines.Baseline2D.mor` is used as a behavioral reference.
pub fn mor(input: MatrixView<'_>, params: Morphology2DParams) -> Result<Fit2D> {
    let mut baseline = vec![0.0; input.len()];
    let output = MatrixViewMut::row_major(&mut baseline, input.rows(), input.cols())?;
    let report = mor_into(input, params, output)?;
    Fit2D::new(baseline, input.rows(), input.cols(), report)
}

/// Estimates a 2D morphology baseline into an existing output buffer.
pub fn mor_into(
    input: MatrixView<'_>,
    params: Morphology2DParams,
    mut output: MatrixViewMut<'_>,
) -> Result<FitReport> {
    validate_input_output(input, &output, params)?;
    let (row_radius, col_radius) = params.radii();
    let shape = MorphologyShape::new(input.rows(), input.cols());
    let window = MorphologyWindow::new(row_radius, col_radius);
    let mut scratch = vec![0.0; input.len()];
    let mut opened = vec![0.0; input.len()];
    let mut eroded = vec![0.0; input.len()];
    let mut dilated = vec![0.0; input.len()];
    opening_reflect_into(
        input.as_slice(),
        shape,
        window,
        &mut scratch,
        &mut eroded,
        &mut opened,
    );
    average_opening_from_opened_reflect_into(
        &opened,
        shape,
        window,
        &mut scratch,
        &mut eroded,
        &mut dilated,
        output.as_mut_slice(),
    );
    for (target, opened) in output.as_mut_slice().iter_mut().zip(&opened) {
        *target = opened.min(*target);
    }
    Ok(FitReport::new(1, true, 0.0))
}

/// Estimates an improved 2D morphology baseline.
///
/// # References
///
/// - `pybaselines.Baseline2D.imor` is used as a behavioral reference.
pub fn imor(input: MatrixView<'_>, params: Imor2DParams) -> Result<Fit2D> {
    let mut baseline = vec![0.0; input.len()];
    let output = MatrixViewMut::row_major(&mut baseline, input.rows(), input.cols())?;
    let report = imor_into(input, params, output)?;
    Fit2D::new(baseline, input.rows(), input.cols(), report)
}

/// Estimates an improved 2D morphology baseline into an existing output buffer.
pub fn imor_into(
    input: MatrixView<'_>,
    params: Imor2DParams,
    mut output: MatrixViewMut<'_>,
) -> Result<FitReport> {
    validate_imor_input_output(input, &output, params)?;
    let (row_radius, col_radius) = params.morphology.radii();
    let shape = MorphologyShape::new(input.rows(), input.cols());
    let window = MorphologyWindow::new(row_radius, col_radius);
    output.as_mut_slice().copy_from_slice(input.as_slice());
    let mut workspace = Morphology2DWorkspace::new(input.len());
    let mut tolerance = f64::INFINITY;

    for iter in 0..=params.max_iter {
        average_opening_reflect_into(output.as_slice(), shape, window, &mut workspace);
        for ((target, observed), opened) in workspace
            .next
            .iter_mut()
            .zip(input.as_slice())
            .zip(&workspace.averaged)
        {
            *target = observed.min(*opened);
        }
        tolerance = relative_change(output.as_slice(), &workspace.next);
        if tolerance < params.tol {
            return Ok(FitReport::new(iter + 1, true, tolerance));
        }
        output.as_mut_slice().copy_from_slice(&workspace.next);
    }

    Ok(FitReport::new(
        params.max_iter.saturating_add(1),
        false,
        tolerance,
    ))
}

/// Estimates a 2D moving-median noise baseline.
///
/// # References
///
/// - `pybaselines.Baseline2D.noise_median` is used as a behavioral reference.
pub fn noise_median(input: MatrixView<'_>, params: Morphology2DParams) -> Result<Fit2D> {
    let mut baseline = vec![0.0; input.len()];
    let output = MatrixViewMut::row_major(&mut baseline, input.rows(), input.cols())?;
    let report = noise_median_into(input, params, output)?;
    Fit2D::new(baseline, input.rows(), input.cols(), report)
}

/// Estimates a 2D moving-median noise baseline into an existing output buffer.
pub fn noise_median_into(
    input: MatrixView<'_>,
    params: Morphology2DParams,
    mut output: MatrixViewMut<'_>,
) -> Result<FitReport> {
    validate_input_output(input, &output, params)?;
    let (row_radius, col_radius) = params.radii();
    moving_median_reflect(
        input.as_slice(),
        input.rows(),
        input.cols(),
        row_radius,
        col_radius,
        output.as_mut_slice(),
    );
    Ok(FitReport::new(1, true, 0.0))
}

fn validate_input_output(
    input: MatrixView<'_>,
    output: &MatrixViewMut<'_>,
    params: Morphology2DParams,
) -> Result<()> {
    params.validate()?;
    let input_shape = input.shape();
    let output_shape = output.shape();
    if input_shape != output_shape {
        return Err(BaselineError::LengthMismatch {
            name: "output",
            expected: input_shape.len(),
            actual: output_shape.len(),
        });
    }
    Ok(())
}

fn validate_imor_input_output(
    input: MatrixView<'_>,
    output: &MatrixViewMut<'_>,
    params: Imor2DParams,
) -> Result<()> {
    params.validate()?;
    let input_shape = input.shape();
    let output_shape = output.shape();
    if input_shape != output_shape {
        return Err(BaselineError::LengthMismatch {
            name: "output",
            expected: input_shape.len(),
            actual: output_shape.len(),
        });
    }
    Ok(())
}

#[derive(Debug, Clone, Copy)]
struct MorphologyShape {
    rows: usize,
    cols: usize,
}

impl MorphologyShape {
    fn new(rows: usize, cols: usize) -> Self {
        Self { rows, cols }
    }

    fn len(self) -> usize {
        self.rows * self.cols
    }
}

#[derive(Debug, Clone, Copy)]
struct MorphologyWindow {
    row_radius: usize,
    col_radius: usize,
}

impl MorphologyWindow {
    fn new(row_radius: usize, col_radius: usize) -> Self {
        Self {
            row_radius,
            col_radius,
        }
    }
}

#[derive(Debug, Clone)]
struct Morphology2DWorkspace {
    scratch: Vec<f64>,
    opened: Vec<f64>,
    eroded: Vec<f64>,
    dilated: Vec<f64>,
    averaged: Vec<f64>,
    next: Vec<f64>,
}

impl Morphology2DWorkspace {
    fn new(len: usize) -> Self {
        Self {
            scratch: vec![0.0; len],
            opened: vec![0.0; len],
            eroded: vec![0.0; len],
            dilated: vec![0.0; len],
            averaged: vec![0.0; len],
            next: vec![0.0; len],
        }
    }
}

fn opening_reflect_into(
    data: &[f64],
    shape: MorphologyShape,
    window: MorphologyWindow,
    scratch: &mut [f64],
    eroded: &mut [f64],
    output: &mut [f64],
) {
    moving_min_reflect_with_workspace(data, shape, window, scratch, eroded);
    moving_max_reflect_with_workspace(eroded, shape, window, scratch, output);
}

fn average_opening_reflect_into(
    data: &[f64],
    shape: MorphologyShape,
    window: MorphologyWindow,
    workspace: &mut Morphology2DWorkspace,
) {
    opening_reflect_into(
        data,
        shape,
        window,
        &mut workspace.scratch,
        &mut workspace.eroded,
        &mut workspace.opened,
    );
    average_opening_from_opened_reflect_into(
        &workspace.opened,
        shape,
        window,
        &mut workspace.scratch,
        &mut workspace.eroded,
        &mut workspace.dilated,
        &mut workspace.averaged,
    );
}

fn average_opening_from_opened_reflect_into(
    opened: &[f64],
    shape: MorphologyShape,
    window: MorphologyWindow,
    scratch: &mut [f64],
    eroded: &mut [f64],
    dilated: &mut [f64],
    output: &mut [f64],
) {
    moving_max_reflect_with_workspace(opened, shape, window, scratch, dilated);
    moving_min_reflect_with_workspace(opened, shape, window, scratch, eroded);
    for ((target, dilated), eroded) in output.iter_mut().zip(dilated).zip(eroded) {
        *target = 0.5 * (*dilated + *eroded);
    }
}

fn moving_min_reflect_with_workspace(
    data: &[f64],
    shape: MorphologyShape,
    window: MorphologyWindow,
    scratch: &mut [f64],
    output: &mut [f64],
) {
    moving_extreme_reflect_with_workspace(
        data,
        shape,
        window,
        scratch,
        output,
        f64::INFINITY,
        f64::min,
    );
}

fn moving_max_reflect_with_workspace(
    data: &[f64],
    shape: MorphologyShape,
    window: MorphologyWindow,
    scratch: &mut [f64],
    output: &mut [f64],
) {
    moving_extreme_reflect_with_workspace(
        data,
        shape,
        window,
        scratch,
        output,
        f64::NEG_INFINITY,
        f64::max,
    );
}

fn moving_extreme_reflect_with_workspace(
    data: &[f64],
    shape: MorphologyShape,
    window: MorphologyWindow,
    scratch: &mut [f64],
    output: &mut [f64],
    initial: f64,
    combine: fn(f64, f64) -> f64,
) {
    debug_assert_eq!(data.len(), shape.len());
    debug_assert_eq!(scratch.len(), data.len());
    debug_assert_eq!(output.len(), data.len());

    for row in 0..shape.rows {
        let row_start = row * shape.cols;
        for col in 0..shape.cols {
            let mut best = initial;
            for window_col in window_indices(col, shape.cols, window.col_radius) {
                best = combine(best, data[row_start + window_col]);
            }
            scratch[row_start + col] = best;
        }
    }

    for row in 0..shape.rows {
        for col in 0..shape.cols {
            let mut best = initial;
            for window_row in window_indices(row, shape.rows, window.row_radius) {
                best = combine(best, scratch[window_row * shape.cols + col]);
            }
            output[row * shape.cols + col] = best;
        }
    }
}

fn moving_mean_reflect(
    data: &[f64],
    rows: usize,
    cols: usize,
    row_radius: usize,
    col_radius: usize,
    output: &mut [f64],
) {
    for row in 0..rows {
        for col in 0..cols {
            let mut sum = 0.0;
            let mut count = 0;
            for window_row in window_indices(row, rows, row_radius) {
                let start = window_row * cols;
                for window_col in window_indices(col, cols, col_radius) {
                    sum += data[start + window_col];
                    count += 1;
                }
            }
            output[row * cols + col] = sum / count as f64;
        }
    }
}

fn moving_median_reflect(
    data: &[f64],
    rows: usize,
    cols: usize,
    row_radius: usize,
    col_radius: usize,
    output: &mut [f64],
) {
    let mut window = Vec::with_capacity((2 * row_radius + 1) * (2 * col_radius + 1));
    for row in 0..rows {
        for col in 0..cols {
            window.clear();
            for window_row in window_indices(row, rows, row_radius) {
                let start = window_row * cols;
                for window_col in window_indices(col, cols, col_radius) {
                    window.push(data[start + window_col]);
                }
            }
            window.sort_by(f64::total_cmp);
            output[row * cols + col] = median_sorted(&window);
        }
    }
}

fn median_sorted(values: &[f64]) -> f64 {
    let mid = values.len() / 2;
    if values.len().is_multiple_of(2) {
        0.5 * (values[mid - 1] + values[mid])
    } else {
        values[mid]
    }
}

fn window_indices(center: usize, len: usize, radius: usize) -> impl Iterator<Item = usize> {
    let window_len = 2 * radius + 1;
    (0..window_len).map(move |offset| {
        let raw = center as isize + offset as isize - radius as isize;
        reflect_index(raw, len)
    })
}

fn reflect_index(index: isize, len: usize) -> usize {
    debug_assert!(len > 0);
    if len == 1 {
        return 0;
    }
    let len = len as isize;
    let mut reflected = index;
    while reflected < 0 || reflected >= len {
        if reflected < 0 {
            reflected = -reflected - 1;
        } else {
            reflected = 2 * len - reflected - 1;
        }
    }
    reflected as usize
}

fn relative_change(previous: &[f64], next: &[f64]) -> f64 {
    let numerator = previous
        .iter()
        .zip(next)
        .map(|(left, right)| {
            let diff = right - left;
            diff * diff
        })
        .sum::<f64>()
        .sqrt();
    let denominator = previous
        .iter()
        .map(|value| value * value)
        .sum::<f64>()
        .sqrt();
    numerator / denominator.max(f64::EPSILON)
}

#[cfg(test)]
mod tests {
    use super::{Morphology2DParams, rolling_ball};
    use crate::MatrixView;

    #[test]
    fn morphology_preserves_constant_surface() {
        let data = vec![2.0; 20];
        let view = MatrixView::row_major(&data, 4, 5).unwrap();
        let fit = rolling_ball(view, Morphology2DParams::default()).unwrap();
        assert!(
            fit.baseline
                .iter()
                .all(|value| (*value - 2.0).abs() < 1e-12)
        );
    }
}