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
//! Remove local noise from a spectrum using the denoising algorithm from MasSpike.
//!
use std::ops;
use std::slice;

use num_traits::Float;
use thiserror::Error;

use crate::histogram::Histogram;
use crate::search;

struct Window<'lifespan> {
    pub intensity_array: &'lifespan mut [f32],
    pub start_index: usize,
    pub end_index: usize,
    pub mean_intensity: f32,
    pub size: usize,
    pub histogram: Histogram<f32>,
    bins: usize,
}

impl<'transient, 'lifespan: 'transient> Window<'lifespan> {
    pub fn new(
        intensity_array: &'lifespan mut [f32],
        start_index: usize,
        end_index: usize,
        bins: usize,
    ) -> Window<'lifespan> {
        let histogram = Histogram::new(intensity_array, bins);
        let mut window = Window {
            size: intensity_array.len(),
            intensity_array,
            start_index,
            end_index,

            mean_intensity: 0.0,

            histogram,
            bins,
        };
        window.mean_intensity =
            window.intensity_array.iter().sum::<f32>() / (window.intensity_array.len() as f32);
        window
    }

    pub fn deduct_intensity(&'transient mut self, value: f32) {
        let n = self.intensity_array.len();
        let mut total = 0.0;
        for i in 0..n {
            self.intensity_array[i] -= value;
            if self.intensity_array[i] < 0.0 {
                self.intensity_array[i] = 0.0;
            }
            total += self.intensity_array[i];
        }
        self.mean_intensity = total / n as f32;
    }

    pub fn rebin_intensities(&mut self) {
        self.histogram.clear();
        self.histogram.populate(self.intensity_array, self.bins)
    }

    pub fn truncated_mean_at(&mut self, threshold: f32) -> f32 {
        if self.size == 0 {
            return 1e-6;
        }
        self.rebin_intensities();
        let n_count = self.bins;

        let mut mask_level = 0;
        for i_count in 0..n_count {
            if self.histogram.bin_count[i_count] > mask_level {
                mask_level = self.histogram.bin_count[i_count];
            }
        }
        mask_level = (mask_level as f32 * (1.0 - threshold)) as usize;
        let mut total = 0.0;
        let mut weight = 0.0;
        for i_count in 0..n_count {
            if mask_level < self.histogram.bin_count[i_count] {
                total += self.histogram.bin_edges[i_count + 1]
                    * self.histogram.bin_count[i_count] as f32;
                weight += self.histogram.bin_count[i_count] as f32
            }
        }
        total / weight
    }

    pub fn truncated_mean(&mut self) -> f32 {
        self.truncated_mean_at(0.95)
    }
}

struct NoiseRegion<'lifespan> {
    pub windows: &'lifespan mut [Window<'lifespan>],
    pub start_index: usize,
    pub end_index: usize,
    pub size: usize,
}

impl<'transient, 'lifespan: 'transient> NoiseRegion<'lifespan> {
    pub fn new(windows: &'lifespan mut [Window<'lifespan>]) -> NoiseRegion<'lifespan> {
        let mut inst = NoiseRegion {
            windows,
            start_index: 0,
            end_index: 0,
            size: 0,
        };
        if let Some(first) = inst.windows.first() {
            inst.start_index = first.start_index;
        }
        if let Some(last) = inst.windows.last() {
            inst.end_index = last.end_index;
        }
        inst.size = inst.windows.len();
        inst
    }

    pub fn noise_window(&mut self) -> Option<&mut Window<'lifespan>> {
        let i = 0;
        let n = self.size;

        if n == 0 {
            return None;
        }
        let mut minimum_window_index = i;
        let mut minimum = self[i].mean_intensity;
        for i in 1..n {
            let window = &self[i];
            if window.mean_intensity < minimum {
                minimum_window_index = i;
                minimum = window.mean_intensity;
            }
        }
        Some(&mut self[minimum_window_index])
    }

    pub fn noise_mean(&'transient mut self, scale: f32) -> f32 {
        match self.noise_window() {
            Some(noise_window) => noise_window.truncated_mean() * scale,
            None => 0.0,
        }
    }

    fn deduct_intensity_from_all_windows(&'transient mut self, noise: f32) {
        self.windows
            .iter_mut()
            .for_each(|w| w.deduct_intensity(noise));
    }

    pub fn denoise(&'lifespan mut self, scale: f32, maxiter: u32) -> f32 {
        if scale == 0.0 {
            return 0.0;
        }

        let mut noise_mean = self.noise_mean(scale);
        let first_mean = noise_mean;
        self.deduct_intensity_from_all_windows(noise_mean);
        let mut last_mean = noise_mean;
        noise_mean = self.noise_mean(scale);
        let mut niter = 1;
        while (last_mean - noise_mean).abs() > 1e-3 && niter < maxiter {
            niter += 1;
            last_mean = noise_mean;
            noise_mean = self.noise_mean(scale);
            self.deduct_intensity_from_all_windows(noise_mean);
        }
        first_mean - noise_mean
    }
}

impl<'lifespan> ops::Index<usize> for NoiseRegion<'lifespan> {
    type Output = Window<'lifespan>;

    fn index(&self, index: usize) -> &Self::Output {
        &self.windows[index]
    }
}

impl<'lifespan> ops::IndexMut<usize> for NoiseRegion<'lifespan> {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        &mut self.windows[index]
    }
}

fn windowed_spectrum<'lifespan>(
    mz_array: &'lifespan [f64],
    intensity_array: &'lifespan mut [f32],
    window_size: f64,
) -> Vec<Window<'lifespan>> {
    let n = mz_array.len();
    let mut windows: Vec<Window<'lifespan>> = Vec::new();

    if n < 2 {
        return windows;
    }
    let mz_min = mz_array.first().unwrap();
    let mz_max = *mz_array.last().unwrap();

    let step_size = window_size / 2.0;
    let mut center_mz = mz_min + step_size;

    let mut partition = intensity_array;
    while center_mz < mz_max {
        let lo_mz = center_mz - step_size;
        let hi_mz = center_mz + step_size;
        let (lo_i, hi_i) = search::find_between(mz_array, lo_mz, hi_mz);
        let mid_point = (mz_array[lo_i] + mz_array[hi_i]) / 2.0;
        let offset = {
            let mid = (hi_i + 1) - lo_i;
            if mid > partition.len() {
                partition.len()
            } else {
                mid
            }
        };
        let (chunk, rest) = partition.split_at_mut(offset);
        partition = rest;
        if lo_mz <= mid_point && mid_point <= hi_mz {
            windows.push(Window::new(
                chunk,
                lo_i,
                hi_i,
                10,
            ));
        } else {
            windows.push(Window::new(
                chunk,
                lo_i,
                hi_i,
                0,
            ))
        }
        center_mz += window_size;
    }
    windows
}

fn group_windows_by_width<'lifespan>(
    windows: &'lifespan mut [Window<'lifespan>],
    width: u32,
) -> Vec<NoiseRegion<'lifespan>> {
    let step = if width > 2 { width / 2 } else { 1 };

    let mut result = Vec::new();

    let mut i = step;
    let n = windows.len();

    let mut partition = windows;
    while i < n as u32 {
        let lo = i - step;
        let hi = i + step;
        let mid = {
            let mid = hi - lo;
            if mid > partition.len() as u32 {
                partition.len()
            } else {
                mid as usize
            }
        };
        let pair = partition.split_at_mut(mid);
        let rest = pair.1;
        partition = rest;
        let subset: &'lifespan mut [Window<'lifespan>] = pair.0;
        let region = NoiseRegion::new(subset);
        result.push(region);
        i += 2 * step;
    }
    result
}

#[derive(Debug, Clone, Copy, Error)]
pub enum DenoisingError {}

pub struct DenoisingArrayPair<'lifespan> {
    pub mz_array: &'lifespan [f64],
    pub intensity_array: &'lifespan mut [f32],
    pub scale: f32,
}

#[derive(Clone, Debug)]
pub struct SignalBackgroundDenoiser {
    pub window_size: f64,
    pub region_size: u32,
}

impl Default for SignalBackgroundDenoiser {
    fn default() -> SignalBackgroundDenoiser {
        SignalBackgroundDenoiser {
            window_size: 1.0,
            region_size: 10,
        }
    }
}

impl<'transient, 'lifespan: 'transient> SignalBackgroundDenoiser {
    pub fn prepare_spectrum(
        &self,
        mz_array: &'lifespan [f64],
        intensity_array: &'lifespan mut [f32],
        scale: f32,
    ) -> DenoisingArrayPair<'lifespan> {
        DenoisingArrayPair {
            mz_array,
            intensity_array,
            scale,
        }
    }

    pub fn denoise_inplace(
        &self,
        pair: &'lifespan mut DenoisingArrayPair,
    ) -> Result<f32, DenoisingError> {
        let mut windows = windowed_spectrum(pair.mz_array, pair.intensity_array, self.window_size);
        let mut regions = group_windows_by_width(&mut windows, self.region_size);
        let mut total = 0.0;
        let n = regions.len();
        for region in regions.iter_mut() {
            total += region.denoise(pair.scale, 10);
        }
        let average_noise_reduction = total / n as f32;
        Ok(average_noise_reduction)
    }

    pub fn denoise(
        &self,
        mz_array: &[f64],
        intensity_array: &mut [f32],
        scale: f32,
    ) -> Result<f32, DenoisingError> {
        let mut pair = self.prepare_spectrum(mz_array, intensity_array, scale);
        // average noise reduction
        self.denoise_inplace(&mut pair)
    }
}

/// Remove background noise from a spectrum **in-place**, returning the same slice of memory.
/// # Arguments
/// * `mz_array` - The m/z array for the spectrum. This _should_ be relatively evenly spaced for the
///                assumptions of this algorithm to work, so a profile spectrum is recommended.
/// * `intensity_array` - The intensity for each m/z in the spectrum. This buffer will be modified
///                       in place, removing background noise using the `MasSpike` algorithm and
///                       is carried forward as the return value.
/// * `scale` - The multiplicity of the noise to remove. When `scale` is small, local noise levels may
///             be exhausted in one window before the noise is appreciably depleted in the region, leading
///             to still-noisy spectra.
///
pub fn denoise<'b>(
    mz_array: &[f64],
    intensity_array: &'b mut [f32],
    scale: f32,
) -> Result<&'b [f32], DenoisingError> {
    let denoiser = SignalBackgroundDenoiser::default();
    match denoiser.denoise(mz_array, intensity_array, scale) {
        Ok(_noise) => Ok(intensity_array),
        Err(err) => Err(err),
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::peak_picker::PeakPicker;
    use crate::test_data::{NOISE, X, Y};

    use std::fs;
    use std::io;
    use std::io::prelude::*;

    #[test]
    fn test_denoise() -> io::Result<()> {
        let mut yhat: Vec<f32> = Y
            .iter()
            .zip(NOISE.iter())
            .map(|(y, e)| y * 1.0 + e)
            .collect();

        let mut acc = Vec::new();
        let mut picker = PeakPicker::default();
        picker.signal_to_noise_threshold = 3.0;
        picker.discover_peaks(&X, &yhat, &mut acc).unwrap();
        assert_eq!(acc.len(), 19);

        let denoiser = SignalBackgroundDenoiser::default();
        denoiser.denoise(&X, &mut yhat, 5.0).unwrap();
        let mut acc2 = Vec::new();
        picker.discover_peaks(&X, &yhat, &mut acc2).unwrap();
        assert_eq!(acc2.len(), 2);
        Ok(())
    }
}