• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tspooner / lfa / 102

pending completion
102

push

travis-ci

web-flow
Merge pull request #17 from tspooner/ft-serde

New manifest feature: "serialize"

19 of 19 new or added lines in 15 files covered. (100.0%)

1313 of 1748 relevant lines covered (75.11%)

0.75 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.19
/src/basis/fixed/constant.rs
1
use crate::{
2
    basis::Projector,
3
    core::Features,
4
    geometry::{Card, Space},
5
};
6

7
/// Fixed uniform basis projector.
8
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
9
#[derive(Copy, Clone, Debug)]
×
10
pub struct Constant {
11
    n_features: usize,
×
12
    value: f64,
×
13
}
14

15
impl Constant {
16
    pub fn new(n_features: usize, value: f64) -> Self {
1✔
17
        Constant {
1✔
18
            n_features: n_features,
1✔
19
            value: value,
1✔
20
        }
21
    }
1✔
22

23
    pub fn zeros(n_features: usize) -> Self { Constant::new(n_features, 0.0) }
1✔
24

25
    pub fn ones(n_features: usize) -> Self { Constant::new(n_features, 1.0) }
1✔
26
}
27

28
impl Space for Constant {
29
    type Value = Features;
30

31
    fn dim(&self) -> usize { self.n_features }
1✔
32

33
    fn card(&self) -> Card { unimplemented!() }
×
34
}
35

36
impl<I: ?Sized> Projector<I> for Constant {
37
    fn project(&self, _: &I) -> Features { vec![self.value; self.n_features].into() }
1✔
38
}
39

40
/// Fixed uniform basis projector.
41
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
42
#[derive(Clone, Debug)]
×
43
pub struct Indices {
44
    n_features: usize,
×
45
    active_features: Vec<usize>,
×
46
}
47

48
impl Indices {
49
    pub fn new(n_features: usize, active_features: Vec<usize>) -> Self {
1✔
50
        Indices {
1✔
51
            n_features,
1✔
52
            active_features,
1✔
53
        }
54
    }
1✔
55
}
56

57
impl Space for Indices {
58
    type Value = Features;
59

60
    fn dim(&self) -> usize { self.n_features }
1✔
61

62
    fn card(&self) -> Card { unimplemented!() }
×
63
}
64

65
impl<I: ?Sized> Projector<I> for Indices {
66
    fn project(&self, _: &I) -> Features { self.active_features.iter().cloned().collect() }
1✔
67
}
68

69
#[cfg(test)]
70
mod tests {
71
    use super::*;
72
    use quickcheck::quickcheck;
73

74
    #[test]
75
    fn test_project_zeros() {
1✔
76
        fn prop_output(length: usize, input: Vec<f64>) -> bool {
1✔
77
            match Constant::zeros(length).project(&input) {
1✔
78
                Features::Sparse(_) => false,
1✔
79
                Features::Dense(activations) => {
1✔
80
                    activations.len() == length && activations.into_iter().all(|&v| v == 0.0)
1✔
81
                },
82
            }
1✔
83
        }
1✔
84

85
        quickcheck(prop_output as fn(usize, Vec<f64>) -> bool);
1✔
86
    }
1✔
87

88
    #[test]
89
    fn test_project_ones() {
1✔
90
        fn prop_output(length: usize, input: Vec<f64>) -> bool {
1✔
91
            match Constant::ones(length).project(&input) {
1✔
92
                Features::Sparse(_) => false,
1✔
93
                Features::Dense(activations) => {
1✔
94
                    activations.len() == length && activations.into_iter().all(|&v| v == 1.0)
1✔
95
                },
96
            }
1✔
97
        }
1✔
98

99
        quickcheck(prop_output as fn(usize, Vec<f64>) -> bool);
1✔
100
    }
1✔
101

102
    #[test]
103
    fn test_project_general() {
1✔
104
        fn prop_output(length: usize, value: f64, input: Vec<f64>) -> bool {
1✔
105
            match Constant::new(length, value).project(&input) {
1✔
106
                Features::Sparse(_) => false,
1✔
107
                Features::Dense(activations) => {
1✔
108
                    activations.len() == length && activations.into_iter().all(|&v| v == value)
1✔
109
                },
110
            }
1✔
111
        }
1✔
112

113
        quickcheck(prop_output as fn(usize, f64, Vec<f64>) -> bool);
1✔
114
    }
1✔
115
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2023 Coveralls, Inc