fraug/augmenters/
crop.rs

1use super::base::Augmenter;
2use tracing::info_span;
3
4/// Augmenter that crops each series into a random continuous slice of specified `size`
5/// 
6/// Also known as window slicing
7pub struct Crop {
8    pub name: String,
9    pub size: usize,
10    p: f64,
11}
12
13impl Crop {
14    pub fn new(size: usize) -> Self {
15        Crop {
16            name: "Crop".to_string(),
17            size,
18            p: 1.0,
19        }
20    }
21
22    fn get_slice(&self, x: &[f64]) -> Vec<f64> {
23        let n = x.len();
24
25        if self.size >= n {
26            return x.to_vec();
27        }
28
29        let start: usize = rand::random_range(0..(n - self.size + 1));
30
31        x[start..(start + self.size)].to_vec()
32    }
33}
34
35impl Augmenter for Crop {
36    fn augment_one(&self, x: &[f64]) -> Vec<f64> {
37        let span = info_span!("", step = "augment_one");
38        let _enter = span.enter();
39        self.get_slice(x)
40    }
41
42    fn get_probability(&self) -> f64 {
43        self.p
44    }
45
46    /// Not implemented!
47    /// 
48    /// The `Crop` augmenter always augments all rows so that all 
49    /// series in a batch have the same length
50    fn set_probability(&mut self, _probability: f64) {
51        unimplemented!(
52            "It is not possible to change the probability of {}: ",
53            self.name
54        );
55    }
56
57    fn get_name(&self) ->String {
58        self.name.clone()
59    }
60}