fraug/augmenters/
drop.rs

1use super::base::Augmenter;
2use tracing::{info_span};
3/// Augmenter that drops data points in series
4///
5/// Drops `percentage` % of data points and replaces them with `default`
6pub struct Drop {
7    pub name: String,
8    pub percentage: f64,
9    pub default: f64,
10    p: f64,
11}
12
13impl Drop {
14    /// Creates new drop augmenter
15    ///
16    /// When `default` is `None`, it is set to `0.0`
17    pub fn new(percentage: f64, default: Option<f64>) -> Self {
18        Drop {
19            name: "Drop".to_string(),
20            percentage,
21            default: default.unwrap_or(0.0),
22            p: 1.0,
23        }
24    }
25}
26
27impl Augmenter for Drop {
28    fn augment_one(&self, x: &[f64]) -> Vec<f64> {
29        let span = info_span!("", step = "augment_one");
30        let _enter = span.enter();
31        x.iter()
32            .map(|val| {
33                if rand::random::<f64>() < self.percentage {
34                    self.default
35                } else {
36                    *val
37                }
38            })
39            .collect()
40    }
41
42    fn get_probability(&self) -> f64 {
43        self.p
44    }
45
46    fn set_probability(&mut self, probability: f64) {
47        self.p = probability;
48    }
49
50    fn get_name(&self) ->String {
51        self.name.clone()
52    }
53}