1
1
import numpy as np
2
- from nose . tools import assert_raises
2
+ import pytest
3
3
from .test_cifti2io_axes import check_rewrite
4
4
import nibabel .cifti2 .cifti2_axes as axes
5
5
from copy import deepcopy
@@ -157,60 +157,60 @@ def test_brain_models():
157
157
158
158
# break brain model
159
159
bmt .affine = np .eye (4 )
160
- with assert_raises (ValueError ):
160
+ with pytest . raises (ValueError ):
161
161
bmt .affine = np .eye (3 )
162
- with assert_raises (ValueError ):
162
+ with pytest . raises (ValueError ):
163
163
bmt .affine = np .eye (4 ).flatten ()
164
164
165
165
bmt .volume_shape = (5 , 3 , 1 )
166
- with assert_raises (ValueError ):
166
+ with pytest . raises (ValueError ):
167
167
bmt .volume_shape = (5. , 3 , 1 )
168
- with assert_raises (ValueError ):
168
+ with pytest . raises (ValueError ):
169
169
bmt .volume_shape = (5 , 3 , 1 , 4 )
170
170
171
- with assert_raises (IndexError ):
171
+ with pytest . raises (IndexError ):
172
172
bmt ['thalamus_left' ]
173
173
174
174
# Test the constructor
175
175
bm_vox = axes .BrainModelAxis ('thalamus_left' , voxel = np .ones ((5 , 3 ), dtype = int ), affine = np .eye (4 ), volume_shape = (2 , 3 , 4 ))
176
176
assert np .all (bm_vox .name == ['CIFTI_STRUCTURE_THALAMUS_LEFT' ] * 5 )
177
177
assert np .array_equal (bm_vox .vertex , np .full (5 , - 1 ))
178
178
assert np .array_equal (bm_vox .voxel , np .full ((5 , 3 ), 1 ))
179
- with assert_raises (ValueError ):
179
+ with pytest . raises (ValueError ):
180
180
# no volume shape
181
181
axes .BrainModelAxis ('thalamus_left' , voxel = np .ones ((5 , 3 ), dtype = int ), affine = np .eye (4 ))
182
- with assert_raises (ValueError ):
182
+ with pytest . raises (ValueError ):
183
183
# no affine
184
184
axes .BrainModelAxis ('thalamus_left' , voxel = np .ones ((5 , 3 ), dtype = int ), volume_shape = (2 , 3 , 4 ))
185
- with assert_raises (ValueError ):
185
+ with pytest . raises (ValueError ):
186
186
# incorrect name
187
187
axes .BrainModelAxis ('random_name' , voxel = np .ones ((5 , 3 ), dtype = int ), affine = np .eye (4 ), volume_shape = (2 , 3 , 4 ))
188
- with assert_raises (ValueError ):
188
+ with pytest . raises (ValueError ):
189
189
# negative voxel indices
190
190
axes .BrainModelAxis ('thalamus_left' , voxel = - np .ones ((5 , 3 ), dtype = int ), affine = np .eye (4 ), volume_shape = (2 , 3 , 4 ))
191
- with assert_raises (ValueError ):
191
+ with pytest . raises (ValueError ):
192
192
# no voxels or vertices
193
193
axes .BrainModelAxis ('thalamus_left' , affine = np .eye (4 ), volume_shape = (2 , 3 , 4 ))
194
- with assert_raises (ValueError ):
194
+ with pytest . raises (ValueError ):
195
195
# incorrect voxel shape
196
196
axes .BrainModelAxis ('thalamus_left' , voxel = np .ones ((5 , 2 ), dtype = int ), affine = np .eye (4 ), volume_shape = (2 , 3 , 4 ))
197
197
198
198
bm_vertex = axes .BrainModelAxis ('cortex_left' , vertex = np .ones (5 , dtype = int ), nvertices = {'cortex_left' : 20 })
199
199
assert np .array_equal (bm_vertex .name , ['CIFTI_STRUCTURE_CORTEX_LEFT' ] * 5 )
200
200
assert np .array_equal (bm_vertex .vertex , np .full (5 , 1 ))
201
201
assert np .array_equal (bm_vertex .voxel , np .full ((5 , 3 ), - 1 ))
202
- with assert_raises (ValueError ):
202
+ with pytest . raises (ValueError ):
203
203
axes .BrainModelAxis ('cortex_left' , vertex = np .ones (5 , dtype = int ))
204
- with assert_raises (ValueError ):
204
+ with pytest . raises (ValueError ):
205
205
axes .BrainModelAxis ('cortex_left' , vertex = np .ones (5 , dtype = int ), nvertices = {'cortex_right' : 20 })
206
- with assert_raises (ValueError ):
206
+ with pytest . raises (ValueError ):
207
207
axes .BrainModelAxis ('cortex_left' , vertex = - np .ones (5 , dtype = int ), nvertices = {'cortex_left' : 20 })
208
208
209
209
# test from_mask errors
210
- with assert_raises (ValueError ):
210
+ with pytest . raises (ValueError ):
211
211
# affine should be 4x4 matrix
212
212
axes .BrainModelAxis .from_mask (np .arange (5 ) > 2 , affine = np .ones (5 ))
213
- with assert_raises (ValueError ):
213
+ with pytest . raises (ValueError ):
214
214
# only 1D or 3D masks accepted
215
215
axes .BrainModelAxis .from_mask (np .ones ((5 , 3 )))
216
216
@@ -226,27 +226,27 @@ def test_brain_models():
226
226
assert bm_added .volume_shape == bm_vox .volume_shape
227
227
228
228
axes .ParcelsAxis .from_brain_models ([('a' , bm_vox ), ('b' , bm_vox )])
229
- with assert_raises (Exception ):
229
+ with pytest . raises (Exception ):
230
230
bm_vox + get_label ()
231
231
232
232
bm_other_shape = axes .BrainModelAxis ('thalamus_left' , voxel = np .ones ((5 , 3 ), dtype = int ),
233
233
affine = np .eye (4 ), volume_shape = (4 , 3 , 4 ))
234
- with assert_raises (ValueError ):
234
+ with pytest . raises (ValueError ):
235
235
bm_vox + bm_other_shape
236
- with assert_raises (ValueError ):
236
+ with pytest . raises (ValueError ):
237
237
axes .ParcelsAxis .from_brain_models ([('a' , bm_vox ), ('b' , bm_other_shape )])
238
238
bm_other_affine = axes .BrainModelAxis ('thalamus_left' , voxel = np .ones ((5 , 3 ), dtype = int ),
239
239
affine = np .eye (4 ) * 2 , volume_shape = (2 , 3 , 4 ))
240
- with assert_raises (ValueError ):
240
+ with pytest . raises (ValueError ):
241
241
bm_vox + bm_other_affine
242
- with assert_raises (ValueError ):
242
+ with pytest . raises (ValueError ):
243
243
axes .ParcelsAxis .from_brain_models ([('a' , bm_vox ), ('b' , bm_other_affine )])
244
244
245
245
bm_vertex = axes .BrainModelAxis ('cortex_left' , vertex = np .ones (5 , dtype = int ), nvertices = {'cortex_left' : 20 })
246
246
bm_other_number = axes .BrainModelAxis ('cortex_left' , vertex = np .ones (5 , dtype = int ), nvertices = {'cortex_left' : 30 })
247
- with assert_raises (ValueError ):
247
+ with pytest . raises (ValueError ):
248
248
bm_vertex + bm_other_number
249
- with assert_raises (ValueError ):
249
+ with pytest . raises (ValueError ):
250
250
axes .ParcelsAxis .from_brain_models ([('a' , bm_vertex ), ('b' , bm_other_number )])
251
251
252
252
# test equalities
@@ -336,29 +336,29 @@ def test_parcels():
336
336
assert len (prc2 [3 :]['mixed' ][1 ]) == 1
337
337
assert prc2 [3 :]['mixed' ][1 ]['CIFTI_STRUCTURE_CORTEX_LEFT' ].shape == (3 , )
338
338
339
- with assert_raises (IndexError ):
339
+ with pytest . raises (IndexError ):
340
340
prc ['non_existent' ]
341
341
342
342
prc ['surface' ]
343
- with assert_raises (IndexError ):
343
+ with pytest . raises (IndexError ):
344
344
# parcel exists twice
345
345
prc2 ['surface' ]
346
346
347
347
# break parcels
348
348
prc .affine = np .eye (4 )
349
- with assert_raises (ValueError ):
349
+ with pytest . raises (ValueError ):
350
350
prc .affine = np .eye (3 )
351
- with assert_raises (ValueError ):
351
+ with pytest . raises (ValueError ):
352
352
prc .affine = np .eye (4 ).flatten ()
353
353
354
354
prc .volume_shape = (5 , 3 , 1 )
355
- with assert_raises (ValueError ):
355
+ with pytest . raises (ValueError ):
356
356
prc .volume_shape = (5. , 3 , 1 )
357
- with assert_raises (ValueError ):
357
+ with pytest . raises (ValueError ):
358
358
prc .volume_shape = (5 , 3 , 1 , 4 )
359
359
360
360
# break adding of parcels
361
- with assert_raises (Exception ):
361
+ with pytest . raises (Exception ):
362
362
prc + get_label ()
363
363
364
364
prc = get_parcels ()
@@ -367,12 +367,12 @@ def test_parcels():
367
367
368
368
other_prc = get_parcels ()
369
369
other_prc .affine = np .eye (4 ) * 2
370
- with assert_raises (ValueError ):
370
+ with pytest . raises (ValueError ):
371
371
prc + other_prc
372
372
373
373
other_prc = get_parcels ()
374
374
other_prc .volume_shape = (20 , 3 , 4 )
375
- with assert_raises (ValueError ):
375
+ with pytest . raises (ValueError ):
376
376
prc + other_prc
377
377
378
378
# test parcel equalities
@@ -396,13 +396,13 @@ def test_parcels():
396
396
prc_other = deepcopy (prc )
397
397
prc_other .volume_shape = (10 , 3 , 4 )
398
398
assert prc != prc_other
399
- with assert_raises (ValueError ):
399
+ with pytest . raises (ValueError ):
400
400
prc + prc_other
401
401
402
402
prc_other = deepcopy (prc )
403
403
prc_other .nvertices ['CIFTI_STRUCTURE_CORTEX_LEFT' ] = 80
404
404
assert prc != prc_other
405
- with assert_raises (ValueError ):
405
+ with pytest . raises (ValueError ):
406
406
prc + prc_other
407
407
408
408
prc_other = deepcopy (prc )
@@ -434,7 +434,7 @@ def test_parcels():
434
434
volume_shape = (2 , 3 , 4 ),
435
435
)
436
436
437
- with assert_raises (ValueError ):
437
+ with pytest . raises (ValueError ):
438
438
axes .ParcelsAxis (
439
439
voxels = [np .ones ((3 , 2 ), dtype = int )],
440
440
vertices = [{}],
@@ -466,7 +466,7 @@ def test_scalar():
466
466
467
467
# test equalities
468
468
assert sc != get_label ()
469
- with assert_raises (Exception ):
469
+ with pytest . raises (Exception ):
470
470
sc + get_label ()
471
471
472
472
sc_other = deepcopy (sc )
@@ -485,10 +485,10 @@ def test_scalar():
485
485
# test constructor
486
486
assert axes .ScalarAxis (['scalar_name' ], [{}]) == axes .ScalarAxis (['scalar_name' ])
487
487
488
- with assert_raises (ValueError ):
488
+ with pytest . raises (ValueError ):
489
489
axes .ScalarAxis ([['scalar_name' ]]) # wrong shape
490
490
491
- with assert_raises (ValueError ):
491
+ with pytest . raises (ValueError ):
492
492
axes .ScalarAxis (['scalar_name' ], [{}, {}]) # wrong size
493
493
494
494
@@ -514,7 +514,7 @@ def test_label():
514
514
# test equalities
515
515
lab = get_label ()
516
516
assert lab != get_scalar ()
517
- with assert_raises (Exception ):
517
+ with pytest . raises (Exception ):
518
518
lab + get_scalar ()
519
519
520
520
other_lab = deepcopy (lab )
@@ -540,10 +540,10 @@ def test_label():
540
540
# test constructor
541
541
assert axes .LabelAxis (['scalar_name' ], [{}], [{}]) == axes .LabelAxis (['scalar_name' ], [{}])
542
542
543
- with assert_raises (ValueError ):
543
+ with pytest . raises (ValueError ):
544
544
axes .LabelAxis ([['scalar_name' ]], [{}]) # wrong shape
545
545
546
- with assert_raises (ValueError ):
546
+ with pytest . raises (ValueError ):
547
547
axes .LabelAxis (['scalar_name' ], [{}, {}]) # wrong size
548
548
549
549
@@ -558,7 +558,7 @@ def test_series():
558
558
assert sr [3 ].unit == 'HERTZ'
559
559
sr [0 ].unit = 'hertz'
560
560
assert sr [0 ].unit == 'HERTZ'
561
- with assert_raises (ValueError ):
561
+ with pytest . raises (ValueError ):
562
562
sr [0 ].unit = 'non_existent'
563
563
564
564
sr = list (get_series ())
@@ -570,11 +570,17 @@ def test_series():
570
570
assert ((sr [1 ] + sr [0 ] + sr [0 ]).time == np .arange (11 ) * 10 + 8 ).all ()
571
571
assert sr [1 ][2 ] == 28
572
572
assert sr [1 ][- 2 ] == sr [1 ].time [- 2 ]
573
- assert_raises (ValueError , lambda : sr [0 ] + sr [2 ])
574
- assert_raises (ValueError , lambda : sr [2 ] + sr [1 ])
575
- assert_raises (ValueError , lambda : sr [0 ] + sr [3 ])
576
- assert_raises (ValueError , lambda : sr [3 ] + sr [1 ])
577
- assert_raises (ValueError , lambda : sr [3 ] + sr [2 ])
573
+
574
+ with pytest .raises (ValueError ):
575
+ sr [0 ] + sr [2 ]
576
+ with pytest .raises (ValueError ):
577
+ sr [2 ] + sr [1 ]
578
+ with pytest .raises (ValueError ):
579
+ sr [0 ] + sr [3 ]
580
+ with pytest .raises (ValueError ):
581
+ sr [3 ] + sr [1 ]
582
+ with pytest .raises (ValueError ):
583
+ sr [3 ] + sr [2 ]
578
584
579
585
# test slicing
580
586
assert (sr [0 ][1 :3 ].time == sr [0 ].time [1 :3 ]).all ()
@@ -590,16 +596,16 @@ def test_series():
590
596
assert (sr [0 ][3 :1 :- 1 ].time == sr [0 ].time [3 :1 :- 1 ]).all ()
591
597
assert (sr [0 ][1 :3 :- 1 ].time == sr [0 ].time [1 :3 :- 1 ]).all ()
592
598
593
- with assert_raises (IndexError ):
599
+ with pytest . raises (IndexError ):
594
600
assert sr [0 ][[0 , 1 ]]
595
- with assert_raises (IndexError ):
601
+ with pytest . raises (IndexError ):
596
602
assert sr [0 ][20 ]
597
- with assert_raises (IndexError ):
603
+ with pytest . raises (IndexError ):
598
604
assert sr [0 ][- 20 ]
599
605
600
606
# test_equalities
601
607
sr = next (get_series ())
602
- with assert_raises (Exception ):
608
+ with pytest . raises (Exception ):
603
609
sr + get_scalar ()
604
610
assert sr != sr [:2 ]
605
611
assert sr == sr [:]
0 commit comments