1
- use ndarray:: { prelude:: * , IntoDimension , Slice } ;
2
1
use itertools:: Itertools ;
3
-
2
+ use ndarray :: { prelude :: * , IntoDimension , Slice } ;
4
3
5
4
use crate :: {
6
- Result ,
5
+ util :: dim_from_vec ,
7
6
CsapsError :: { ReshapeFrom2d , ReshapeTo2d } ,
8
- util :: dim_from_vec , Real
7
+ Real , Result ,
9
8
} ;
10
9
11
-
12
10
pub fn diff < ' a , T : ' a , D , V > ( data : V , axis : Option < Axis > ) -> Array < T , D >
13
11
where
14
12
T : Real < T > ,
15
13
D : Dimension ,
16
- V : AsArray < ' a , T , D >
14
+ V : AsArray < ' a , T , D > ,
17
15
{
18
16
let data_view = data. into ( ) ;
19
17
let axis = axis. unwrap_or_else ( || Axis ( data_view. ndim ( ) - 1 ) ) ;
@@ -24,11 +22,10 @@ where
24
22
& tail - & head
25
23
}
26
24
27
-
28
25
pub 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 > ,
32
29
{
33
30
let data_view = data. into ( ) ;
34
31
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>>
48
45
49
46
match data_view. permuted_axes ( axes) . into_shape ( new_shape) {
50
47
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
+ } ) ,
59
54
}
60
55
}
61
56
62
-
63
57
pub 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 ,
66
60
{
67
61
let ndim = data. ndim ( ) ;
68
62
let shape = data. shape ( ) . to_vec ( ) ;
69
63
let new_shape = [ shape[ 0 ..( ndim - 1 ) ] . iter ( ) . product ( ) , shape[ ndim - 1 ] ] ;
70
64
71
65
match data. into_shape ( new_shape) {
72
66
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
+ } ) ,
81
73
}
82
74
}
83
75
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 > ,
90
85
{
91
86
let shape = shape. into_dimension ( ) ;
92
87
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
106
101
107
102
let axes: D = dim_from_vec ( ndim, axes_tmp) ;
108
103
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
+ } ) ,
118
111
}
119
112
}
120
113
121
-
122
114
/// Returns the indices of the bins to which each value in input array belongs
123
115
///
124
116
/// This code works if `bins` is increasing
125
117
pub 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 > ,
132
123
{
133
124
let arr_view = arr. into ( ) ;
134
125
let bins_view = bins. into ( ) ;
135
126
136
127
let mut indices = Array1 :: zeros ( ( arr_view. len ( ) , ) ) ;
137
128
let mut kstart: usize = 0 ;
138
129
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
+ {
142
135
let mut k = kstart;
143
136
144
137
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>
158
151
indices
159
152
}
160
153
161
-
162
154
#[ cfg( test) ]
163
155
mod tests {
164
- use std:: f64;
165
- use ndarray:: { array, Array1 , Axis , Ix1 , Ix2 , Ix3 } ;
166
156
use crate :: ndarrayext:: * ;
157
+ use ndarray:: { array, Array1 , Axis , Ix1 , Ix2 , Ix3 } ;
158
+ use std:: f64;
167
159
168
160
#[ test]
169
161
fn test_diff_1d ( ) {
170
162
let a = array ! [ 1. , 2. , 3. , 4. , 5. ] ;
171
163
172
- assert_eq ! ( diff( & a, None ) ,
173
- array![ 1. , 1. , 1. , 1. ] ) ;
164
+ assert_eq ! ( diff( & a, None ) , array![ 1. , 1. , 1. , 1. ] ) ;
174
165
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. ] ) ;
177
167
}
178
168
179
169
#[ test]
180
170
fn test_diff_2d ( ) {
181
171
let a = array ! [ [ 1. , 2. , 3. , 4. ] , [ 1. , 2. , 3. , 4. ] ] ;
182
172
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. ] ] ) ;
185
174
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. ] ] ) ;
188
176
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. ] ] ) ;
191
178
}
192
179
193
180
#[ test]
194
181
fn test_diff_3d ( ) {
195
182
let a = array ! [ [ [ 1. , 2. , 3. ] , [ 1. , 2. , 3. ] ] , [ [ 1. , 2. , 3. ] , [ 1. , 2. , 3. ] ] ] ;
196
183
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
+ ) ;
208
203
}
209
204
210
205
#[ test]
@@ -218,8 +213,14 @@ mod tests {
218
213
fn test_to_2d_from_2d ( ) {
219
214
let a = array ! [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] ] ;
220
215
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
+ ) ;
223
224
}
224
225
225
226
#[ test]
@@ -229,7 +230,10 @@ mod tests {
229
230
// FIXME: incompatible memory layout
230
231
// assert_eq!(to_2d(&a, Axis(0)).unwrap(), array![[1, 7], [2, 8], [3, 9], [4, 10], [5, 11], [6, 12]]);
231
232
// 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
+ ) ;
233
237
}
234
238
235
239
#[ test]
@@ -241,13 +245,19 @@ mod tests {
241
245
#[ test]
242
246
fn test_to_2d_simple_from_2d ( ) {
243
247
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
+ ) ;
245
252
}
246
253
247
254
#[ test]
248
255
fn test_to_2d_simple_from_3d ( ) {
249
256
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
+ ) ;
251
261
}
252
262
253
263
#[ test]
@@ -258,7 +268,8 @@ mod tests {
258
268
259
269
let r = from_2d ( & a, s, Axis ( 2 ) )
260
270
. unwrap ( )
261
- . into_dimensionality :: < Ix3 > ( ) . unwrap ( ) ;
271
+ . into_dimensionality :: < Ix3 > ( )
272
+ . unwrap ( ) ;
262
273
263
274
assert_eq ! ( r, e) ;
264
275
}
@@ -369,7 +380,10 @@ mod tests {
369
380
370
381
let indices = digitize ( & xi, & edges) ;
371
382
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
+ )
373
387
}
374
388
375
389
#[ test]
@@ -389,6 +403,9 @@ mod tests {
389
403
390
404
let indices = digitize ( & xi, & edges) ;
391
405
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
+ )
393
410
}
394
411
}
0 commit comments