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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use smartnoise_validator::errors::*;
use crate::NodeArguments;
use smartnoise_validator::base::{Value, Array, Jagged, ReleaseNode, IndexKey};
use smartnoise_validator::utilities::{standardize_numeric_argument, standardize_categorical_argument, standardize_null_target_argument, take_argument};
use crate::components::Evaluable;
use ndarray::ArrayD;
use crate::utilities::get_num_columns;
use smartnoise_validator::{proto, Float, Integer};
use std::hash::Hash;
impl Evaluable for proto::Clamp {
fn evaluate(&self, _privacy_definition: &Option<proto::PrivacyDefinition>, mut arguments: NodeArguments) -> Result<ReleaseNode> {
if arguments.contains_key::<IndexKey>(&"categories".into()) {
match (take_argument(&mut arguments, "data")?, take_argument(&mut arguments, "categories")?, take_argument(&mut arguments, "null_value")?) {
(Value::Array(data), Value::Jagged(categories), Value::Array(nulls)) => Ok(match (data, categories, nulls) {
(Array::Bool(data), Jagged::Bool(categories), Array::Bool(nulls)) =>
clamp_categorical(data, categories, nulls)?.into(),
(Array::Float(_), Jagged::Float(_), Array::Float(_)) =>
return Err("float clamping is not supported".into()),
(Array::Int(data), Jagged::Int(categories), Array::Int(nulls)) =>
clamp_categorical(data, categories, nulls)?.into(),
(Array::Str(data), Jagged::Str(categories), Array::Str(nulls)) =>
clamp_categorical(data, categories, nulls)?.into(),
_ => return Err("types of data, categories, and null must be consistent".into())
}),
_ => return Err("data must be ArrayND, categories must be Vector2DJagged, and null must be ArrayND".into())
}
}
else {
match (take_argument(&mut arguments, "data")?, take_argument(&mut arguments, "lower")?, take_argument(&mut arguments, "upper")?) {
(Value::Array(data), Value::Array(lower), Value::Array(upper)) => Ok(match (data, lower, upper) {
(Array::Float(data), Array::Float(lower), Array::Float(upper)) =>
clamp_numeric_float(data, lower, upper)?.into(),
(Array::Int(data), Array::Int(lower), Array::Int(upper)) =>
clamp_numeric_integer(data, lower, upper)?.into(),
_ => return Err("data, lower, and upper must all have type f64".into())
}),
_ => return Err("data, lower, and upper must all be ArrayND".into())
}
}.map(ReleaseNode::new)
}
}
pub fn clamp_numeric_float(
mut data: ArrayD<Float>, lower: ArrayD<Float>, upper: ArrayD<Float>
)-> Result<ArrayD<Float>> {
let num_columns = get_num_columns(&data)?;
data.gencolumns_mut().into_iter()
.zip(standardize_numeric_argument(lower, num_columns)?.into_iter())
.zip(standardize_numeric_argument(upper, num_columns)?.into_iter())
.for_each(|((mut column, min), max)| column.iter_mut()
.filter(|v| !v.is_nan())
.for_each(|v| *v = min.max(max.min(*v))));
Ok(data)
}
pub fn clamp_numeric_integer(
mut data: ArrayD<Integer>, lower: ArrayD<Integer>, upper: ArrayD<Integer>
)-> Result<ArrayD<Integer>> {
let num_columns = get_num_columns(&data)?;
data.gencolumns_mut().into_iter()
.zip(standardize_numeric_argument(lower, num_columns)?.into_iter())
.zip(standardize_numeric_argument(upper, num_columns)?.into_iter())
.for_each(|((mut column, min), max)| column.iter_mut()
.for_each(|v| *v = *min.max(max.min(v))));
Ok(data)
}
pub fn clamp_categorical<T: Ord + Hash + Clone>(
mut data: ArrayD<T>,
categories: Vec<Vec<T>>,
null_value: ArrayD<T>
) -> Result<ArrayD<T>> where T:Clone, T:PartialEq, T:Default {
let num_columns = get_num_columns(&data)?;
data.gencolumns_mut().into_iter()
.zip(standardize_categorical_argument(categories.to_vec(), num_columns)?)
.zip(standardize_null_target_argument(null_value, num_columns)?)
.for_each(|((mut column, categories), null)| column.iter_mut()
.filter(|v| !categories.contains(v))
.for_each(|v| *v = null.clone()));
Ok(data)
}