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
use smartnoise_validator::errors::*;
use crate::NodeArguments;
use smartnoise_validator::base::{ReleaseNode};
use smartnoise_validator::utilities::take_argument;
use crate::components::Evaluable;
use ndarray::{ArrayD, Array};
use crate::utilities::get_num_columns;
use smartnoise_validator::{proto, Float};
impl Evaluable for proto::Mean {
fn evaluate(&self, _privacy_definition: &Option<proto::PrivacyDefinition>, mut arguments: NodeArguments) -> Result<ReleaseNode> {
Ok(ReleaseNode::new(mean(
&take_argument(&mut arguments, "data")?.array()?.float()?
)?.into()))
}
}
pub fn mean(data: &ArrayD<Float>) -> Result<ArrayD<Float>> {
let means = data.gencolumns().into_iter()
.map(|column| column.mean()).collect::<Option<Vec<Float>>>()
.ok_or_else(|| Error::from("attempted mean of an empty column"))?;
let array = match data.ndim() {
1 => Array::from_shape_vec(vec![], means),
2 => Array::from_shape_vec(vec![1 as usize, get_num_columns(&data)? as usize], means),
_ => return Err("invalid data shape for Mean".into())
};
match array {
Ok(array) => Ok(array),
Err(_) => Err("unable to package Mean result into an array".into())
}
}
#[cfg(test)]
mod test_mean {
use ndarray::{arr2};
use crate::components::mean::mean;
#[test]
fn test_mean() {
let data = arr2(&[ [1.,10.], [2., 20.], [3., 30.] ]).into_dyn();
let means = mean(&data).unwrap();
assert!(means == arr2(&[[2., 20.]]).into_dyn());
}
}