1- use ndarray:: { prelude:: * , IntoDimension , Slice } ;
21use itertools:: Itertools ;
3-
2+ use ndarray :: { prelude :: * , IntoDimension , Slice } ;
43
54use crate :: {
6- Result ,
5+ util :: dim_from_vec ,
76 CsapsError :: { ReshapeFrom2d , ReshapeTo2d } ,
8- util :: dim_from_vec , Real
7+ Real , Result ,
98} ;
109
11-
1210pub fn diff < ' a , T : ' a , D , V > ( data : V , axis : Option < Axis > ) -> Array < T , D >
1311where
1412 T : Real < T > ,
1513 D : Dimension ,
16- V : AsArray < ' a , T , D >
14+ V : AsArray < ' a , T , D > ,
1715{
1816 let data_view = data. into ( ) ;
1917 let axis = axis. unwrap_or_else ( || Axis ( data_view. ndim ( ) - 1 ) ) ;
@@ -24,11 +22,10 @@ where
2422 & tail - & head
2523}
2624
27-
2825pub fn to_2d < ' a , T : ' a , D , I > ( data : I , axis : Axis ) -> Result < ArrayView2 < ' a , T > >
29- where
30- D : Dimension ,
31- I : AsArray < ' a , T , D > ,
26+ where
27+ D : Dimension ,
28+ I : AsArray < ' a , T , D > ,
3229{
3330 let data_view = data. into ( ) ;
3431 let ndim = data_view. ndim ( ) ;
@@ -48,45 +45,43 @@ pub fn to_2d<'a, T: 'a, D, I>(data: I, axis: Axis) -> Result<ArrayView2<'a, T>>
4845
4946 match data_view. permuted_axes ( axes) . into_shape ( new_shape) {
5047 Ok ( view_2d) => Ok ( view_2d) ,
51- Err ( error) => Err (
52- ReshapeTo2d {
53- input_shape : shape,
54- output_shape : new_shape. to_vec ( ) ,
55- axis : axis. 0 ,
56- source : error,
57- }
58- )
48+ Err ( error) => Err ( ReshapeTo2d {
49+ input_shape : shape,
50+ output_shape : new_shape. to_vec ( ) ,
51+ axis : axis. 0 ,
52+ source : error,
53+ } ) ,
5954 }
6055}
6156
62-
6357pub fn to_2d_simple < ' a , T : ' a , D > ( data : ArrayView < ' a , T , D > ) -> Result < ArrayView2 < ' a , T > >
64- where
65- D : Dimension
58+ where
59+ D : Dimension ,
6660{
6761 let ndim = data. ndim ( ) ;
6862 let shape = data. shape ( ) . to_vec ( ) ;
6963 let new_shape = [ shape[ 0 ..( ndim - 1 ) ] . iter ( ) . product ( ) , shape[ ndim - 1 ] ] ;
7064
7165 match data. into_shape ( new_shape) {
7266 Ok ( data_2d) => Ok ( data_2d) ,
73- Err ( error) => Err (
74- ReshapeTo2d {
75- input_shape : shape,
76- output_shape : new_shape. to_vec ( ) ,
77- axis : ndim - 1 ,
78- source : error,
79- }
80- )
67+ Err ( error) => Err ( ReshapeTo2d {
68+ input_shape : shape,
69+ output_shape : new_shape. to_vec ( ) ,
70+ axis : ndim - 1 ,
71+ source : error,
72+ } ) ,
8173 }
8274}
8375
84-
85- pub fn from_2d < ' a , T : ' a , D , S , I > ( data : I , shape : S , axis : Axis ) -> Result < ArrayView < ' a , T , S :: Dim > >
86- where
87- D : Dimension ,
88- S : IntoDimension < Dim = D > ,
89- I : AsArray < ' a , T , Ix2 > ,
76+ pub fn from_2d < ' a , T : ' a , D , S , I > (
77+ data : I ,
78+ shape : S ,
79+ axis : Axis ,
80+ ) -> Result < ArrayView < ' a , T , S :: Dim > >
81+ where
82+ D : Dimension ,
83+ S : IntoDimension < Dim = D > ,
84+ I : AsArray < ' a , T , Ix2 > ,
9085{
9186 let shape = shape. into_dimension ( ) ;
9287 let ndim = shape. ndim ( ) ;
@@ -106,39 +101,37 @@ pub fn from_2d<'a, T: 'a, D, S, I>(data: I, shape: S, axis: Axis) -> Result<Arra
106101
107102 let axes: D = dim_from_vec ( ndim, axes_tmp) ;
108103 Ok ( view_nd. permuted_axes ( axes) )
109- } ,
110- Err ( error) => Err (
111- ReshapeFrom2d {
112- input_shape : data_view. shape ( ) . to_vec ( ) ,
113- output_shape : new_shape_vec,
114- axis : axis. 0 ,
115- source : error,
116- }
117- )
104+ }
105+ Err ( error) => Err ( ReshapeFrom2d {
106+ input_shape : data_view. shape ( ) . to_vec ( ) ,
107+ output_shape : new_shape_vec,
108+ axis : axis. 0 ,
109+ source : error,
110+ } ) ,
118111 }
119112}
120113
121-
122114/// Returns the indices of the bins to which each value in input array belongs
123115///
124116/// This code works if `bins` is increasing
125117pub fn digitize < ' a , T : ' a , A , B > ( arr : A , bins : B ) -> Array1 < usize >
126- where
127- T : Real < T > ,
128- // T: Clone + NdFloat + AlmostEqual,
129-
130- A : AsArray < ' a , T , Ix1 > ,
131- B : AsArray < ' a , T , Ix1 > ,
118+ where
119+ T : Real < T > ,
120+ // T: Clone + NdFloat + AlmostEqual,
121+ A : AsArray < ' a , T , Ix1 > ,
122+ B : AsArray < ' a , T , Ix1 > ,
132123{
133124 let arr_view = arr. into ( ) ;
134125 let bins_view = bins. into ( ) ;
135126
136127 let mut indices = Array1 :: zeros ( ( arr_view. len ( ) , ) ) ;
137128 let mut kstart: usize = 0 ;
138129
139- for ( i, & a) in arr_view. iter ( ) . enumerate ( )
140- . sorted_by ( |e1, e2| e1. 1 . partial_cmp ( e2. 1 ) . unwrap ( ) ) {
141-
130+ for ( i, & a) in arr_view
131+ . iter ( )
132+ . enumerate ( )
133+ . sorted_by ( |e1, e2| e1. 1 . partial_cmp ( e2. 1 ) . unwrap ( ) )
134+ {
142135 let mut k = kstart;
143136
144137 for bins_win in bins_view. slice ( s ! [ kstart..] ) . windows ( 2 ) {
@@ -158,53 +151,55 @@ pub fn digitize<'a, T: 'a, A, B>(arr: A, bins: B) -> Array1<usize>
158151 indices
159152}
160153
161-
162154#[ cfg( test) ]
163155mod tests {
164- use std:: f64;
165- use ndarray:: { array, Array1 , Axis , Ix1 , Ix2 , Ix3 } ;
166156 use crate :: ndarrayext:: * ;
157+ use ndarray:: { array, Array1 , Axis , Ix1 , Ix2 , Ix3 } ;
158+ use std:: f64;
167159
168160 #[ test]
169161 fn test_diff_1d ( ) {
170162 let a = array ! [ 1. , 2. , 3. , 4. , 5. ] ;
171163
172- assert_eq ! ( diff( & a, None ) ,
173- array![ 1. , 1. , 1. , 1. ] ) ;
164+ assert_eq ! ( diff( & a, None ) , array![ 1. , 1. , 1. , 1. ] ) ;
174165
175- assert_eq ! ( diff( & a, Some ( Axis ( 0 ) ) ) ,
176- array![ 1. , 1. , 1. , 1. ] ) ;
166+ assert_eq ! ( diff( & a, Some ( Axis ( 0 ) ) ) , array![ 1. , 1. , 1. , 1. ] ) ;
177167 }
178168
179169 #[ test]
180170 fn test_diff_2d ( ) {
181171 let a = array ! [ [ 1. , 2. , 3. , 4. ] , [ 1. , 2. , 3. , 4. ] ] ;
182172
183- assert_eq ! ( diff( & a, None ) ,
184- array![ [ 1. , 1. , 1. ] , [ 1. , 1. , 1. ] ] ) ;
173+ assert_eq ! ( diff( & a, None ) , array![ [ 1. , 1. , 1. ] , [ 1. , 1. , 1. ] ] ) ;
185174
186- assert_eq ! ( diff( & a, Some ( Axis ( 0 ) ) ) ,
187- array![ [ 0. , 0. , 0. , 0. ] ] ) ;
175+ assert_eq ! ( diff( & a, Some ( Axis ( 0 ) ) ) , array![ [ 0. , 0. , 0. , 0. ] ] ) ;
188176
189- assert_eq ! ( diff( & a, Some ( Axis ( 1 ) ) ) ,
190- array![ [ 1. , 1. , 1. ] , [ 1. , 1. , 1. ] ] ) ;
177+ assert_eq ! ( diff( & a, Some ( Axis ( 1 ) ) ) , array![ [ 1. , 1. , 1. ] , [ 1. , 1. , 1. ] ] ) ;
191178 }
192179
193180 #[ test]
194181 fn test_diff_3d ( ) {
195182 let a = array ! [ [ [ 1. , 2. , 3. ] , [ 1. , 2. , 3. ] ] , [ [ 1. , 2. , 3. ] , [ 1. , 2. , 3. ] ] ] ;
196183
197- assert_eq ! ( diff( & a, None ) ,
198- array![ [ [ 1. , 1. ] , [ 1. , 1. ] ] , [ [ 1. , 1. ] , [ 1. , 1. ] ] ] ) ;
199-
200- assert_eq ! ( diff( & a, Some ( Axis ( 0 ) ) ) ,
201- array![ [ [ 0. , 0. , 0. ] , [ 0. , 0. , 0. ] ] ] ) ;
202-
203- assert_eq ! ( diff( & a, Some ( Axis ( 1 ) ) ) ,
204- array![ [ [ 0. , 0. , 0. ] ] , [ [ 0. , 0. , 0. ] ] ] ) ;
205-
206- assert_eq ! ( diff( & a, Some ( Axis ( 2 ) ) ) ,
207- array![ [ [ 1. , 1. ] , [ 1. , 1. ] ] , [ [ 1. , 1. ] , [ 1. , 1. ] ] ] ) ;
184+ assert_eq ! (
185+ diff( & a, None ) ,
186+ array![ [ [ 1. , 1. ] , [ 1. , 1. ] ] , [ [ 1. , 1. ] , [ 1. , 1. ] ] ]
187+ ) ;
188+
189+ assert_eq ! (
190+ diff( & a, Some ( Axis ( 0 ) ) ) ,
191+ array![ [ [ 0. , 0. , 0. ] , [ 0. , 0. , 0. ] ] ]
192+ ) ;
193+
194+ assert_eq ! (
195+ diff( & a, Some ( Axis ( 1 ) ) ) ,
196+ array![ [ [ 0. , 0. , 0. ] ] , [ [ 0. , 0. , 0. ] ] ]
197+ ) ;
198+
199+ assert_eq ! (
200+ diff( & a, Some ( Axis ( 2 ) ) ) ,
201+ array![ [ [ 1. , 1. ] , [ 1. , 1. ] ] , [ [ 1. , 1. ] , [ 1. , 1. ] ] ]
202+ ) ;
208203 }
209204
210205 #[ test]
@@ -218,8 +213,14 @@ mod tests {
218213 fn test_to_2d_from_2d ( ) {
219214 let a = array ! [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] ] ;
220215
221- assert_eq ! ( to_2d( & a, Axis ( 0 ) ) . unwrap( ) , array![ [ 1 , 5 ] , [ 2 , 6 ] , [ 3 , 7 ] , [ 4 , 8 ] ] ) ;
222- assert_eq ! ( to_2d( & a, Axis ( 1 ) ) . unwrap( ) , array![ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] ] ) ;
216+ assert_eq ! (
217+ to_2d( & a, Axis ( 0 ) ) . unwrap( ) ,
218+ array![ [ 1 , 5 ] , [ 2 , 6 ] , [ 3 , 7 ] , [ 4 , 8 ] ]
219+ ) ;
220+ assert_eq ! (
221+ to_2d( & a, Axis ( 1 ) ) . unwrap( ) ,
222+ array![ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] ]
223+ ) ;
223224 }
224225
225226 #[ test]
@@ -229,7 +230,10 @@ mod tests {
229230 // FIXME: incompatible memory layout
230231 // assert_eq!(to_2d(&a, Axis(0)).unwrap(), array![[1, 7], [2, 8], [3, 9], [4, 10], [5, 11], [6, 12]]);
231232 // assert_eq!(to_2d(&a, Axis(1)).unwrap(), array![[1, 4], [2, 5], [3, 6], [7, 10], [8, 11], [9, 12]]);
232- assert_eq ! ( to_2d( & a, Axis ( 2 ) ) . unwrap( ) , array![ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] , [ 10 , 11 , 12 ] ] ) ;
233+ assert_eq ! (
234+ to_2d( & a, Axis ( 2 ) ) . unwrap( ) ,
235+ array![ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] , [ 10 , 11 , 12 ] ]
236+ ) ;
233237 }
234238
235239 #[ test]
@@ -241,13 +245,19 @@ mod tests {
241245 #[ test]
242246 fn test_to_2d_simple_from_2d ( ) {
243247 let a = array ! [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] ] ;
244- assert_eq ! ( to_2d_simple( a. view( ) ) . unwrap( ) , array![ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] ] ) ;
248+ assert_eq ! (
249+ to_2d_simple( a. view( ) ) . unwrap( ) ,
250+ array![ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] ]
251+ ) ;
245252 }
246253
247254 #[ test]
248255 fn test_to_2d_simple_from_3d ( ) {
249256 let a = array ! [ [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] , [ [ 7 , 8 , 9 ] , [ 10 , 11 , 12 ] ] ] ;
250- assert_eq ! ( to_2d_simple( a. view( ) ) . unwrap( ) , array![ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] , [ 10 , 11 , 12 ] ] ) ;
257+ assert_eq ! (
258+ to_2d_simple( a. view( ) ) . unwrap( ) ,
259+ array![ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] , [ 10 , 11 , 12 ] ]
260+ ) ;
251261 }
252262
253263 #[ test]
@@ -258,7 +268,8 @@ mod tests {
258268
259269 let r = from_2d ( & a, s, Axis ( 2 ) )
260270 . unwrap ( )
261- . into_dimensionality :: < Ix3 > ( ) . unwrap ( ) ;
271+ . into_dimensionality :: < Ix3 > ( )
272+ . unwrap ( ) ;
262273
263274 assert_eq ! ( r, e) ;
264275 }
@@ -369,7 +380,10 @@ mod tests {
369380
370381 let indices = digitize ( & xi, & edges) ;
371382
372- assert_eq ! ( indices, array![ 1 , 1 , 1 , 1 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 3 , 3 , 3 , 3 , 3 ] )
383+ assert_eq ! (
384+ indices,
385+ array![ 1 , 1 , 1 , 1 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 3 , 3 , 3 , 3 , 3 ]
386+ )
373387 }
374388
375389 #[ test]
@@ -389,6 +403,9 @@ mod tests {
389403
390404 let indices = digitize ( & xi, & edges) ;
391405
392- assert_eq ! ( indices, array![ 0 , 1 , 0 , 2 , 2 , 1 , 0 , 3 , 4 , 4 , 3 , 3 , 2 , 2 , 1 , 0 ] )
406+ assert_eq ! (
407+ indices,
408+ array![ 0 , 1 , 0 , 2 , 2 , 1 , 0 , 3 , 4 , 4 , 3 , 3 , 2 , 2 , 1 , 0 ]
409+ )
393410 }
394411}
0 commit comments