4
4
5
5
import numpy as np
6
6
from numpy .testing import assert_array_equal
7
- from nose . tools import assert_raises , eq_ as eq
7
+ import pytest
8
8
9
9
10
- from zarr .indexing import (normalize_integer_selection , replace_ellipsis , oindex , oindex_set )
10
+ from zarr .indexing import (normalize_integer_selection , replace_ellipsis , oindex ,
11
+ oindex_set )
11
12
import zarr
12
13
13
14
14
15
def test_normalize_integer_selection ():
15
16
16
- eq ( 1 , normalize_integer_selection (1 , 100 ) )
17
- eq ( 99 , normalize_integer_selection (- 1 , 100 ) )
18
- with assert_raises (IndexError ):
17
+ assert 1 == normalize_integer_selection (1 , 100 )
18
+ assert 99 == normalize_integer_selection (- 1 , 100 )
19
+ with pytest . raises (IndexError ):
19
20
normalize_integer_selection (100 , 100 )
20
- with assert_raises (IndexError ):
21
+ with pytest . raises (IndexError ):
21
22
normalize_integer_selection (1000 , 100 )
22
- with assert_raises (IndexError ):
23
+ with pytest . raises (IndexError ):
23
24
normalize_integer_selection (- 1000 , 100 )
24
25
25
26
26
27
def test_replace_ellipsis ():
27
28
28
29
# 1D, single item
29
- eq (( 0 ,), replace_ellipsis (0 , (100 ,) ))
30
+ assert ( 0 ,) == replace_ellipsis (0 , (100 ,))
30
31
31
32
# 1D
32
- eq (( slice (None ),), replace_ellipsis (Ellipsis , (100 ,) ))
33
- eq (( slice (None ),), replace_ellipsis (slice (None ), (100 ,) ))
34
- eq (( slice (None , 100 ),), replace_ellipsis (slice (None , 100 ), (100 ,) ))
35
- eq (( slice (0 , None ),), replace_ellipsis (slice (0 , None ), (100 ,) ))
36
- eq (( slice (None ),), replace_ellipsis ((slice (None ), Ellipsis ), (100 ,) ))
37
- eq (( slice (None ),), replace_ellipsis ((Ellipsis , slice (None )), (100 ,) ))
33
+ assert ( slice (None ),) == replace_ellipsis (Ellipsis , (100 ,))
34
+ assert ( slice (None ),) == replace_ellipsis (slice (None ), (100 ,))
35
+ assert ( slice (None , 100 ),) == replace_ellipsis (slice (None , 100 ), (100 ,))
36
+ assert ( slice (0 , None ),) == replace_ellipsis (slice (0 , None ), (100 ,))
37
+ assert ( slice (None ),) == replace_ellipsis ((slice (None ), Ellipsis ), (100 ,))
38
+ assert ( slice (None ),) == replace_ellipsis ((Ellipsis , slice (None )), (100 ,))
38
39
39
40
# 2D, single item
40
- eq (( 0 , 0 ), replace_ellipsis ((0 , 0 ), (100 , 100 ) ))
41
- eq (( - 1 , 1 ), replace_ellipsis ((- 1 , 1 ), (100 , 100 ) ))
41
+ assert ( 0 , 0 ) == replace_ellipsis ((0 , 0 ), (100 , 100 ))
42
+ assert ( - 1 , 1 ) == replace_ellipsis ((- 1 , 1 ), (100 , 100 ))
42
43
43
44
# 2D, single col/row
44
- eq (( 0 , slice (None )), replace_ellipsis ((0 , slice (None )), (100 , 100 ) ))
45
- eq (( 0 , slice (None )), replace_ellipsis ((0 ,), (100 , 100 ) ))
46
- eq (( slice (None ), 0 ), replace_ellipsis ((slice (None ), 0 ), (100 , 100 ) ))
45
+ assert ( 0 , slice (None )) == replace_ellipsis ((0 , slice (None )), (100 , 100 ))
46
+ assert ( 0 , slice (None )) == replace_ellipsis ((0 ,), (100 , 100 ))
47
+ assert ( slice (None ), 0 ) == replace_ellipsis ((slice (None ), 0 ), (100 , 100 ))
47
48
48
49
# 2D slice
49
- eq ((slice (None ), slice (None )),
50
- replace_ellipsis (Ellipsis , (100 , 100 )))
51
- eq ((slice (None ), slice (None )),
52
- replace_ellipsis (slice (None ), (100 , 100 )))
53
- eq ((slice (None ), slice (None )),
54
- replace_ellipsis ((slice (None ), slice (None )), (100 , 100 )))
55
- eq ((slice (None ), slice (None )),
56
- replace_ellipsis ((Ellipsis , slice (None )), (100 , 100 )))
57
- eq ((slice (None ), slice (None )),
58
- replace_ellipsis ((slice (None ), Ellipsis ), (100 , 100 )))
59
- eq ((slice (None ), slice (None )),
60
- replace_ellipsis ((slice (None ), Ellipsis , slice (None )), (100 , 100 )))
61
- eq ((slice (None ), slice (None )),
62
- replace_ellipsis ((Ellipsis , slice (None ), slice (None )), (100 , 100 )))
63
- eq ((slice (None ), slice (None )),
64
- replace_ellipsis ((slice (None ), slice (None ), Ellipsis ), (100 , 100 )))
50
+ assert ((slice (None ), slice (None )) ==
51
+ replace_ellipsis (Ellipsis , (100 , 100 )))
52
+ assert ((slice (None ), slice (None )) ==
53
+ replace_ellipsis (slice (None ), (100 , 100 )))
54
+ assert ((slice (None ), slice (None )) ==
55
+ replace_ellipsis ((slice (None ), slice (None )), (100 , 100 )))
56
+ assert ((slice (None ), slice (None )) ==
57
+ replace_ellipsis ((Ellipsis , slice (None )), (100 , 100 )))
58
+ assert ((slice (None ), slice (None )) ==
59
+ replace_ellipsis ((slice (None ), Ellipsis ), (100 , 100 )))
60
+ assert ((slice (None ), slice (None )) ==
61
+ replace_ellipsis ((slice (None ), Ellipsis , slice (None )), (100 , 100 )))
62
+ assert ((slice (None ), slice (None )) ==
63
+ replace_ellipsis ((Ellipsis , slice (None ), slice (None )), (100 , 100 )))
64
+ assert ((slice (None ), slice (None )) ==
65
+ replace_ellipsis ((slice (None ), slice (None ), Ellipsis ), (100 , 100 )))
65
66
66
67
67
68
def test_get_basic_selection_0d ():
@@ -73,8 +74,8 @@ def test_get_basic_selection_0d():
73
74
74
75
assert_array_equal (a , z .get_basic_selection (Ellipsis ))
75
76
assert_array_equal (a , z [...])
76
- eq ( 42 , z .get_basic_selection (() ))
77
- eq ( 42 , z [()])
77
+ assert 42 == z .get_basic_selection (())
78
+ assert 42 == z [()]
78
79
79
80
# test out param
80
81
b = np .zeros_like (a )
@@ -88,12 +89,12 @@ def test_get_basic_selection_0d():
88
89
z [()] = value
89
90
assert_array_equal (a , z .get_basic_selection (Ellipsis ))
90
91
assert_array_equal (a , z [...])
91
- eq ( a [()], z .get_basic_selection (() ))
92
- eq ( a [()], z [()])
93
- eq ( b'aaa' , z .get_basic_selection ((), fields = 'foo' ) )
94
- eq ( b'aaa' , z ['foo' ])
95
- eq ( a [['foo' , 'bar' ]], z .get_basic_selection ((), fields = ['foo' , 'bar' ]) )
96
- eq ( a [['foo' , 'bar' ]], z ['foo' , 'bar' ])
92
+ assert a [()] == z .get_basic_selection (())
93
+ assert a [()] == z [()]
94
+ assert b'aaa' == z .get_basic_selection ((), fields = 'foo' )
95
+ assert b'aaa' == z ['foo' ]
96
+ assert a [['foo' , 'bar' ]] == z .get_basic_selection ((), fields = ['foo' , 'bar' ])
97
+ assert a [['foo' , 'bar' ]] == z ['foo' , 'bar' ]
97
98
# test out param
98
99
b = np .zeros_like (a )
99
100
z .get_basic_selection (Ellipsis , out = b )
@@ -201,9 +202,9 @@ def test_get_basic_selection_1d():
201
202
[0 , 1 ], # fancy indexing
202
203
]
203
204
for selection in bad_selections :
204
- with assert_raises (IndexError ):
205
+ with pytest . raises (IndexError ):
205
206
z .get_basic_selection (selection )
206
- with assert_raises (IndexError ):
207
+ with pytest . raises (IndexError ):
207
208
z [selection ]
208
209
209
210
@@ -277,9 +278,9 @@ def test_get_basic_selection_2d():
277
278
(slice (None ), [0 , 1 ]),
278
279
]
279
280
for selection in bad_selections :
280
- with assert_raises (IndexError ):
281
+ with pytest . raises (IndexError ):
281
282
z .get_basic_selection (selection )
282
- with assert_raises (IndexError ):
283
+ with pytest . raises (IndexError ):
283
284
z [selection ]
284
285
285
286
@@ -316,17 +317,17 @@ def test_set_basic_selection_0d():
316
317
assert_array_equal (a , z )
317
318
# with fields
318
319
z .set_basic_selection (Ellipsis , v ['foo' ], fields = 'foo' )
319
- eq ( v ['foo' ], z ['foo' ])
320
- eq ( a ['bar' ], z ['bar' ])
321
- eq ( a ['baz' ], z ['baz' ])
320
+ assert v ['foo' ] == z ['foo' ]
321
+ assert a ['bar' ] == z ['bar' ]
322
+ assert a ['baz' ] == z ['baz' ]
322
323
z ['bar' ] = v ['bar' ]
323
- eq ( v ['foo' ], z ['foo' ])
324
- eq ( v ['bar' ], z ['bar' ])
325
- eq ( a ['baz' ], z ['baz' ])
324
+ assert v ['foo' ] == z ['foo' ]
325
+ assert v ['bar' ] == z ['bar' ]
326
+ assert a ['baz' ] == z ['baz' ]
326
327
# multiple field assignment not supported
327
- with assert_raises (IndexError ):
328
+ with pytest . raises (IndexError ):
328
329
z .set_basic_selection (Ellipsis , v [['foo' , 'bar' ]], fields = ['foo' , 'bar' ])
329
- with assert_raises (IndexError ):
330
+ with pytest . raises (IndexError ):
330
331
z [..., 'foo' , 'bar' ] = v [['foo' , 'bar' ]]
331
332
332
333
@@ -353,11 +354,11 @@ def test_get_orthogonal_selection_1d_bool():
353
354
_test_get_orthogonal_selection (a , z , ix )
354
355
355
356
# test errors
356
- with assert_raises (IndexError ):
357
+ with pytest . raises (IndexError ):
357
358
z .oindex [np .zeros (50 , dtype = bool )] # too short
358
- with assert_raises (IndexError ):
359
+ with pytest . raises (IndexError ):
359
360
z .oindex [np .zeros (2000 , dtype = bool )] # too long
360
- with assert_raises (IndexError ):
361
+ with pytest . raises (IndexError ):
361
362
z .oindex [[[True , False ], [False , True ]]] # too many dimensions
362
363
363
364
@@ -398,9 +399,9 @@ def test_get_orthogonal_selection_1d_int():
398
399
[[2 , 4 ], [6 , 8 ]], # too many dimensions
399
400
]
400
401
for selection in bad_selections :
401
- with assert_raises (IndexError ):
402
+ with pytest . raises (IndexError ):
402
403
z .get_orthogonal_selection (selection )
403
- with assert_raises (IndexError ):
404
+ with pytest . raises (IndexError ):
404
405
z .oindex [selection ]
405
406
406
407
@@ -461,9 +462,9 @@ def test_get_orthogonal_selection_2d():
461
462
_test_get_orthogonal_selection (a , z , selection )
462
463
463
464
for selection in basic_selections_2d_bad :
464
- with assert_raises (IndexError ):
465
+ with pytest . raises (IndexError ):
465
466
z .get_orthogonal_selection (selection )
466
- with assert_raises (IndexError ):
467
+ with pytest . raises (IndexError ):
467
468
z .oindex [selection ]
468
469
469
470
@@ -766,9 +767,9 @@ def test_get_coordinate_selection_1d():
766
767
[- (a .shape [0 ] + 1 )], # out of bounds
767
768
]
768
769
for selection in bad_selections :
769
- with assert_raises (IndexError ):
770
+ with pytest . raises (IndexError ):
770
771
z .get_coordinate_selection (selection )
771
- with assert_raises (IndexError ):
772
+ with pytest . raises (IndexError ):
772
773
z .vindex [selection ]
773
774
774
775
@@ -816,16 +817,16 @@ def test_get_coordinate_selection_2d():
816
817
[1 , 0 , 0 ]])
817
818
_test_get_coordinate_selection (a , z , (ix0 , ix1 ))
818
819
819
- with assert_raises (IndexError ):
820
+ with pytest . raises (IndexError ):
820
821
selection = slice (5 , 15 ), [1 , 2 , 3 ]
821
822
z .get_coordinate_selection (selection )
822
- with assert_raises (IndexError ):
823
+ with pytest . raises (IndexError ):
823
824
selection = [1 , 2 , 3 ], slice (5 , 15 )
824
825
z .get_coordinate_selection (selection )
825
- with assert_raises (IndexError ):
826
+ with pytest . raises (IndexError ):
826
827
selection = Ellipsis , [1 , 2 , 3 ]
827
828
z .get_coordinate_selection (selection )
828
- with assert_raises (IndexError ):
829
+ with pytest . raises (IndexError ):
829
830
selection = Ellipsis
830
831
z .get_coordinate_selection (selection )
831
832
@@ -864,9 +865,9 @@ def test_set_coordinate_selection_1d():
864
865
_test_set_coordinate_selection (v , a , z , ix )
865
866
866
867
for selection in coordinate_selections_1d_bad :
867
- with assert_raises (IndexError ):
868
+ with pytest . raises (IndexError ):
868
869
z .set_coordinate_selection (selection , 42 )
869
- with assert_raises (IndexError ):
870
+ with pytest . raises (IndexError ):
870
871
z .vindex [selection ] = 42
871
872
872
873
@@ -948,9 +949,9 @@ def test_get_mask_selection_1d():
948
949
[[True , False ], [False , True ]], # too many dimensions
949
950
]
950
951
for selection in bad_selections :
951
- with assert_raises (IndexError ):
952
+ with pytest . raises (IndexError ):
952
953
z .get_mask_selection (selection )
953
- with assert_raises (IndexError ):
954
+ with pytest . raises (IndexError ):
954
955
z .vindex [selection ]
955
956
956
957
@@ -969,11 +970,11 @@ def test_get_mask_selection_2d():
969
970
_test_get_mask_selection (a , z , ix )
970
971
971
972
# test errors
972
- with assert_raises (IndexError ):
973
+ with pytest . raises (IndexError ):
973
974
z .vindex [np .zeros ((1000 , 5 ), dtype = bool )] # too short
974
- with assert_raises (IndexError ):
975
+ with pytest . raises (IndexError ):
975
976
z .vindex [np .zeros ((2000 , 10 ), dtype = bool )] # too long
976
- with assert_raises (IndexError ):
977
+ with pytest . raises (IndexError ):
977
978
z .vindex [[True , False ]] # wrong no. dimensions
978
979
979
980
@@ -1002,9 +1003,9 @@ def test_set_mask_selection_1d():
1002
1003
_test_set_mask_selection (v , a , z , ix )
1003
1004
1004
1005
for selection in mask_selections_1d_bad :
1005
- with assert_raises (IndexError ):
1006
+ with pytest . raises (IndexError ):
1006
1007
z .set_mask_selection (selection , 42 )
1007
- with assert_raises (IndexError ):
1008
+ with pytest . raises (IndexError ):
1008
1009
z .vindex [selection ] = 42
1009
1010
1010
1011
@@ -1039,7 +1040,7 @@ def test_get_selection_out():
1039
1040
z .get_basic_selection (selection , out = out )
1040
1041
assert_array_equal (expect , out [:])
1041
1042
1042
- with assert_raises (TypeError ):
1043
+ with pytest . raises (TypeError ):
1043
1044
z .get_basic_selection (Ellipsis , out = [])
1044
1045
1045
1046
# orthogonal selections
@@ -1200,9 +1201,9 @@ def test_get_selections_with_fields():
1200
1201
assert_array_equal (expect , actual )
1201
1202
1202
1203
# missing/bad fields
1203
- with assert_raises (IndexError ):
1204
+ with pytest . raises (IndexError ):
1204
1205
z .get_basic_selection (Ellipsis , fields = ['notafield' ])
1205
- with assert_raises (IndexError ):
1206
+ with pytest . raises (IndexError ):
1206
1207
z .get_basic_selection (Ellipsis , fields = slice (None ))
1207
1208
1208
1209
@@ -1229,22 +1230,23 @@ def test_set_selections_with_fields():
1229
1230
1230
1231
for fields in fields_fixture :
1231
1232
1232
- # currently multi-field assignment is not supported in numpy, so we won't support it either
1233
+ # currently multi-field assignment is not supported in numpy, so we won't support
1234
+ # it either
1233
1235
if isinstance (fields , list ) and len (fields ) > 1 :
1234
- with assert_raises (IndexError ):
1236
+ with pytest . raises (IndexError ):
1235
1237
z .set_basic_selection (Ellipsis , v , fields = fields )
1236
- with assert_raises (IndexError ):
1238
+ with pytest . raises (IndexError ):
1237
1239
z .set_orthogonal_selection ([0 , 2 ], v , fields = fields )
1238
- with assert_raises (IndexError ):
1240
+ with pytest . raises (IndexError ):
1239
1241
z .set_coordinate_selection ([0 , 2 ], v , fields = fields )
1240
- with assert_raises (IndexError ):
1242
+ with pytest . raises (IndexError ):
1241
1243
z .set_mask_selection ([True , False , True ], v , fields = fields )
1242
1244
1243
1245
else :
1244
1246
1245
1247
if isinstance (fields , list ) and len (fields ) == 1 :
1246
- # work around numpy does not support multi-field assignment even if there is only
1247
- # one field
1248
+ # work around numpy does not support multi-field assignment even if there
1249
+ # is only one field
1248
1250
key = fields [0 ]
1249
1251
elif isinstance (fields , list ) and len (fields ) == 0 :
1250
1252
# work around numpy ambiguity about what is a field selection
0 commit comments