Skip to main content

utah2/combinators/
aggregate.rs

1//! Utah aggregation combinators.
2
3use super::*;
4use ndarray::Array;
5
6#[derive(Clone, Debug)]
7pub struct Sum<'a, I: 'a, T: 'a>
8    where I: Iterator<Item = Window<'a, T>> + 'a,
9          T: UtahNum
10{
11    data: I,
12    other: Vec<String>,
13    axis: UtahAxis,
14}
15
16impl<'a, I, T> Sum<'a, I, T>
17    where I: Iterator<Item = Window<'a, T>>,
18          T: UtahNum
19{
20    pub fn new(df: I, other: Vec<String>, axis: UtahAxis) -> Sum<'a, I, T> {
21
22        Sum {
23            data: df,
24            other: other,
25            axis: axis,
26        }
27    }
28}
29
30impl<'a, I, T> Iterator for Sum<'a, I, T>
31    where I: Iterator<Item = Window<'a, T>>,
32          T: UtahNum
33{
34    type Item = T;
35    fn next(&mut self) -> Option<Self::Item> {
36        match self.data.next() {
37
38            None => return None,
39            Some((_, dat)) => return Some(dat.scalar_sum()),
40        }
41    }
42}
43
44#[derive(Clone, Debug)]
45pub struct Mean<'a, I: 'a, T: 'a>
46    where I: Iterator<Item = Window<'a, T>>,
47          T: UtahNum
48{
49    data: I,
50    other: Vec<String>,
51    axis: UtahAxis,
52}
53
54impl<'a, I, T> Mean<'a, I, T>
55    where I: Iterator<Item = Window<'a, T>>,
56          T: UtahNum + 'a
57{
58    pub fn new(df: I, other: Vec<String>, axis: UtahAxis) -> Mean<'a, I, T> {
59
60        Mean {
61            data: df,
62            other: other,
63            axis: axis,
64        }
65    }
66}
67
68impl<'a, I, T> Iterator for Mean<'a, I, T>
69    where I: Iterator<Item = Window<'a, T>>,
70          T: UtahNum + 'a
71{
72    type Item = T;
73    fn next(&mut self) -> Option<Self::Item> {
74        match self.data.next() {
75            Some((_, dat)) => {
76                let size = dat.fold(T::zero(), |acc, _| acc + T::one());
77                let mean = dat.scalar_sum() / size;
78                Some(mean)
79            }
80            None => return None,
81
82        }
83    }
84}
85
86
87#[derive(Clone)]
88pub struct Max<'a, I: 'a, T: 'a>
89    where I: Iterator<Item = Window<'a, T>>,
90          T: UtahNum
91{
92    data: I,
93    other: Vec<String>,
94    axis: UtahAxis,
95}
96
97impl<'a, I, T> Max<'a, I, T>
98    where I: Iterator<Item = Window<'a, T>>,
99          T: UtahNum + 'a
100{
101    pub fn new(df: I, other: Vec<String>, axis: UtahAxis) -> Max<'a, I, T> {
102
103        Max {
104            data: df,
105            other: other,
106            axis: axis,
107        }
108    }
109}
110
111impl<'a, I, T> Iterator for Max<'a, I, T>
112    where I: Iterator<Item = Window<'a, T>>,
113          T: UtahNum + Ord + 'a
114{
115    type Item = T;
116    fn next(&mut self) -> Option<Self::Item> {
117        match self.data.next() {
118            None => return None,
119            Some((_, dat)) => return dat.iter().max().map(|x| x.clone()),
120        }
121
122
123
124    }
125}
126
127
128#[derive(Clone, Debug)]
129pub struct Min<'a, I: 'a, T: 'a>
130    where I: Iterator<Item = Window<'a, T>>,
131          T: UtahNum
132{
133    data: I,
134    other: Vec<String>,
135    axis: UtahAxis,
136}
137
138impl<'a, I, T> Min<'a, I, T>
139    where I: Iterator<Item = Window<'a, T>>,
140          T: UtahNum + 'a
141{
142    pub fn new(df: I, other: Vec<String>, axis: UtahAxis) -> Min<'a, I, T> {
143
144        Min {
145            data: df,
146            other: other,
147            axis: axis,
148        }
149    }
150}
151
152impl<'a, I, T> Iterator for Min<'a, I, T>
153    where I: Iterator<Item = Window<'a, T>>,
154          T: UtahNum + Ord
155{
156    type Item = T;
157    fn next(&mut self) -> Option<Self::Item> {
158        match self.data.next() {
159            None => return None,
160            Some((_, dat)) => return dat.iter().min().map(|x| x.clone()),
161        }
162
163
164
165    }
166}
167
168#[derive(Clone)]
169pub struct Stdev<'a, I: 'a, T: 'a>
170    where I: Iterator<Item = Window<'a, T>>,
171          T: UtahNum
172{
173    data: I,
174    other: Vec<String>,
175    axis: UtahAxis,
176}
177
178impl<'a, I, T> Stdev<'a, I, T>
179    where I: Iterator<Item = Window<'a, T>>,
180          T: UtahNum + 'a
181{
182    pub fn new(df: I, other: Vec<String>, axis: UtahAxis) -> Stdev<'a, I, T> {
183
184        Stdev {
185            data: df,
186            other: other,
187            axis: axis,
188        }
189    }
190}
191
192
193impl<'a, I, T> ToDataFrame<'a, T, T> for Mean<'a, I, T>
194    where I: Iterator<Item = Window<'a, T>>,
195          T: UtahNum
196{
197    fn as_df(self) -> Result<DataFrame<T>> {
198        let other = self.other.clone();
199        let axis = self.axis.clone();
200        let c: Vec<_> = self.collect();
201        let res_dim = match axis {
202            UtahAxis::Row => (other.len(), 1),
203            UtahAxis::Column => (1, other.len()),
204        };
205
206
207
208        let d = Array::from_shape_vec(res_dim, c).unwrap();
209        let def = vec!["0"];
210        match axis {
211            UtahAxis::Row => {
212                let df = DataFrame::new(d).columns(&def[..])?.index(&other[..])?;
213                Ok(df)
214            }
215            UtahAxis::Column => {
216                let df = DataFrame::new(d).columns(&other[..])?.index(&def[..])?;
217                Ok(df)
218            }
219
220        }
221    }
222    fn as_matrix(self) -> Result<Matrix<T>> {
223        let other = self.other.clone();
224        let axis = self.axis.clone();
225        let c: Vec<_> = self.collect();
226        let res_dim = match axis {
227            UtahAxis::Row => (other.len(), 1),
228            UtahAxis::Column => (1, other.len()),
229        };
230
231        Ok(Array::from_shape_vec(res_dim, c).unwrap())
232
233
234    }
235
236    fn as_array(self) -> Result<Row<T>> {
237
238        let c: Vec<_> = self.collect();
239        Ok(Array::from(c))
240    }
241}
242
243
244
245impl<'a, I, T> ToDataFrame<'a, T, T> for Max<'a, I, T>
246    where I: Iterator<Item = Window<'a, T>>,
247          T: UtahNum + Ord
248{
249    fn as_df(self) -> Result<DataFrame<T>> {
250        let other = self.other.clone();
251        let axis = self.axis.clone();
252        let c: Vec<_> = self.collect();
253        let res_dim = match axis {
254            UtahAxis::Row => (other.len(), 1),
255            UtahAxis::Column => (1, other.len()),
256        };
257
258
259
260        let d = Array::from_shape_vec(res_dim, c).unwrap();
261        let def = vec!["0"];
262        match axis {
263            UtahAxis::Row => {
264                let df = DataFrame::new(d).columns(&def[..])?.index(&other[..])?;
265                Ok(df)
266            }
267            UtahAxis::Column => {
268                let df = DataFrame::new(d).columns(&other[..])?.index(&def[..])?;
269                Ok(df)
270            }
271
272        }
273
274    }
275    fn as_matrix(self) -> Result<Matrix<T>> {
276        let other = self.other.clone();
277        let axis = self.axis.clone();
278        let c: Vec<_> = self.collect();
279        let res_dim = match axis {
280            UtahAxis::Row => (other.len(), 1),
281            UtahAxis::Column => (1, other.len()),
282        };
283
284        Ok(Array::from_shape_vec(res_dim, c).unwrap())
285
286
287    }
288    fn as_array(self) -> Result<Row<T>> {
289
290        let c: Vec<_> = self.collect();
291        Ok(Array::from(c))
292    }
293}
294
295
296impl<'a, I, T> ToDataFrame<'a, T, T> for Min<'a, I, T>
297    where I: Iterator<Item = Window<'a, T>>,
298          T: UtahNum + Ord
299{
300    fn as_df(self) -> Result<DataFrame<T>> {
301        let other = self.other.clone();
302        let axis = self.axis.clone();
303        let c: Vec<_> = self.collect();
304        let res_dim = match axis {
305            UtahAxis::Row => (other.len(), 1),
306            UtahAxis::Column => (1, other.len()),
307        };
308
309
310
311        let d = Array::from_shape_vec(res_dim, c).unwrap();
312        let def = vec!["0"];
313        match axis {
314            UtahAxis::Row => {
315                let df = DataFrame::new(d).columns(&def[..])?.index(&other[..])?;
316                Ok(df)
317            }
318            UtahAxis::Column => {
319                let df = DataFrame::new(d).columns(&other[..])?.index(&def[..])?;
320                Ok(df)
321            }
322
323        }
324    }
325    fn as_matrix(self) -> Result<Matrix<T>> {
326        let other = self.other.clone();
327        let axis = self.axis.clone();
328        let c: Vec<_> = self.collect();
329        let res_dim = match axis {
330            UtahAxis::Row => (other.len(), 1),
331            UtahAxis::Column => (1, other.len()),
332        };
333
334        Ok(Array::from_shape_vec(res_dim, c).unwrap())
335
336
337    }
338
339    fn as_array(self) -> Result<Row<T>> {
340
341        let c: Vec<_> = self.collect();
342        Ok(Array::from(c))
343    }
344}
345
346
347impl<'a, I, T> ToDataFrame<'a, T, T> for Sum<'a, I, T>
348    where I: Iterator<Item = Window<'a, T>>,
349          T: UtahNum
350{
351    fn as_df(self) -> Result<DataFrame<T>> {
352        let other = self.other.clone();
353        let axis = self.axis.clone();
354        let c: Vec<_> = self.collect();
355        let res_dim = match axis {
356            UtahAxis::Row => (other.len(), 1),
357            UtahAxis::Column => (1, other.len()),
358        };
359
360        let d = Array::from_shape_vec(res_dim, c).unwrap();
361        let def = vec!["0"];
362        match axis {
363            UtahAxis::Row => {
364                let df = DataFrame::new(d).columns(&def[..])?.index(&other[..])?;
365                Ok(df)
366            }
367            UtahAxis::Column => {
368                let df = DataFrame::new(d).columns(&other[..])?.index(&def[..])?;
369                Ok(df)
370            }
371
372        }
373    }
374    fn as_matrix(self) -> Result<Matrix<T>> {
375        let other = self.other.clone();
376        let axis = self.axis.clone();
377        let c: Vec<_> = self.collect();
378        let res_dim = match axis {
379            UtahAxis::Row => (other.len(), 1),
380            UtahAxis::Column => (1, other.len()),
381        };
382
383        Ok(Array::from_shape_vec(res_dim, c).unwrap())
384
385
386    }
387
388    fn as_array(self) -> Result<Row<T>> {
389
390        let c: Vec<_> = self.collect();
391        Ok(Array::from(c))
392    }
393}