fraug/augmenters/
repeat.rs

1use crate::Dataset;
2
3use super::base::Augmenter;
4use tracing::info_span;
5/// Augmenter that repeats all data rows `n` times
6///
7/// Resource intensive because the data needs to be copied `n` times
8///
9/// Only works with `augment_batch` because the data needs to be cloned
10pub struct Repeat {
11    pub name: String,
12    pub n: usize,
13    p: f64,
14}
15
16impl Repeat {
17    pub fn new(times: usize) -> Self {
18        assert!(times > 0);
19        Repeat {
20            name: "Repeat".to_string(),
21            n: times,
22            p: 1.0,
23        }
24    }
25}
26
27impl Augmenter for Repeat {
28    fn augment_batch(&self, input: &mut Dataset, _parallel: bool, per_sample: bool) {
29        let span = info_span!("", component = self.get_name());
30        let _enter = span.enter();
31
32        let features: Vec<Vec<f64>> = input.features.clone();
33        let labels: Vec<String> = input.labels.clone();
34
35        for _ in 0..self.n - 1 {
36            input.features.append(&mut features.clone());
37            input.labels.append(&mut labels.clone());
38        }
39    }
40
41    /// Not implemented!
42    fn augment_one(&self, _x: &[f64]) -> Vec<f64> {
43        let span = info_span!("", step = "augment_one");
44        let _enter = span.enter();
45        unimplemented!("Repeat augmenter only works on a dataset directly!");
46    }
47
48    fn get_probability(&self) -> f64 {
49        self.p
50    }
51
52    /// Not implemented!
53    fn set_probability(&mut self, _probability: f64) {
54        unimplemented!(
55            "It is not possible to change the probability of {}",
56            self.name
57        );
58    }
59
60    fn get_name(&self) -> String {
61        self.name.clone()
62    }
63
64    fn supports_per_sample(&self) -> bool {
65        false
66    }
67}