Skip to content

Commit 103db6b

Browse files
author
Guidotti Roberto
committed
TEST: cifti2 tests to pytest
1 parent 84df042 commit 103db6b

File tree

4 files changed

+454
-398
lines changed

4 files changed

+454
-398
lines changed

nibabel/cifti2/tests/test_axes.py

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import numpy as np
2-
from nose.tools import assert_raises
2+
import pytest
33
from .test_cifti2io_axes import check_rewrite
44
import nibabel.cifti2.cifti2_axes as axes
55
from copy import deepcopy
@@ -157,60 +157,60 @@ def test_brain_models():
157157

158158
# break brain model
159159
bmt.affine = np.eye(4)
160-
with assert_raises(ValueError):
160+
with pytest.raises(ValueError):
161161
bmt.affine = np.eye(3)
162-
with assert_raises(ValueError):
162+
with pytest.raises(ValueError):
163163
bmt.affine = np.eye(4).flatten()
164164

165165
bmt.volume_shape = (5, 3, 1)
166-
with assert_raises(ValueError):
166+
with pytest.raises(ValueError):
167167
bmt.volume_shape = (5., 3, 1)
168-
with assert_raises(ValueError):
168+
with pytest.raises(ValueError):
169169
bmt.volume_shape = (5, 3, 1, 4)
170170

171-
with assert_raises(IndexError):
171+
with pytest.raises(IndexError):
172172
bmt['thalamus_left']
173173

174174
# Test the constructor
175175
bm_vox = axes.BrainModelAxis('thalamus_left', voxel=np.ones((5, 3), dtype=int), affine=np.eye(4), volume_shape=(2, 3, 4))
176176
assert np.all(bm_vox.name == ['CIFTI_STRUCTURE_THALAMUS_LEFT'] * 5)
177177
assert np.array_equal(bm_vox.vertex, np.full(5, -1))
178178
assert np.array_equal(bm_vox.voxel, np.full((5, 3), 1))
179-
with assert_raises(ValueError):
179+
with pytest.raises(ValueError):
180180
# no volume shape
181181
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):
183183
# no affine
184184
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):
186186
# incorrect name
187187
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):
189189
# negative voxel indices
190190
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):
192192
# no voxels or vertices
193193
axes.BrainModelAxis('thalamus_left', affine=np.eye(4), volume_shape=(2, 3, 4))
194-
with assert_raises(ValueError):
194+
with pytest.raises(ValueError):
195195
# incorrect voxel shape
196196
axes.BrainModelAxis('thalamus_left', voxel=np.ones((5, 2), dtype=int), affine=np.eye(4), volume_shape=(2, 3, 4))
197197

198198
bm_vertex = axes.BrainModelAxis('cortex_left', vertex=np.ones(5, dtype=int), nvertices={'cortex_left': 20})
199199
assert np.array_equal(bm_vertex.name, ['CIFTI_STRUCTURE_CORTEX_LEFT'] * 5)
200200
assert np.array_equal(bm_vertex.vertex, np.full(5, 1))
201201
assert np.array_equal(bm_vertex.voxel, np.full((5, 3), -1))
202-
with assert_raises(ValueError):
202+
with pytest.raises(ValueError):
203203
axes.BrainModelAxis('cortex_left', vertex=np.ones(5, dtype=int))
204-
with assert_raises(ValueError):
204+
with pytest.raises(ValueError):
205205
axes.BrainModelAxis('cortex_left', vertex=np.ones(5, dtype=int), nvertices={'cortex_right': 20})
206-
with assert_raises(ValueError):
206+
with pytest.raises(ValueError):
207207
axes.BrainModelAxis('cortex_left', vertex=-np.ones(5, dtype=int), nvertices={'cortex_left': 20})
208208

209209
# test from_mask errors
210-
with assert_raises(ValueError):
210+
with pytest.raises(ValueError):
211211
# affine should be 4x4 matrix
212212
axes.BrainModelAxis.from_mask(np.arange(5) > 2, affine=np.ones(5))
213-
with assert_raises(ValueError):
213+
with pytest.raises(ValueError):
214214
# only 1D or 3D masks accepted
215215
axes.BrainModelAxis.from_mask(np.ones((5, 3)))
216216

@@ -226,27 +226,27 @@ def test_brain_models():
226226
assert bm_added.volume_shape == bm_vox.volume_shape
227227

228228
axes.ParcelsAxis.from_brain_models([('a', bm_vox), ('b', bm_vox)])
229-
with assert_raises(Exception):
229+
with pytest.raises(Exception):
230230
bm_vox + get_label()
231231

232232
bm_other_shape = axes.BrainModelAxis('thalamus_left', voxel=np.ones((5, 3), dtype=int),
233233
affine=np.eye(4), volume_shape=(4, 3, 4))
234-
with assert_raises(ValueError):
234+
with pytest.raises(ValueError):
235235
bm_vox + bm_other_shape
236-
with assert_raises(ValueError):
236+
with pytest.raises(ValueError):
237237
axes.ParcelsAxis.from_brain_models([('a', bm_vox), ('b', bm_other_shape)])
238238
bm_other_affine = axes.BrainModelAxis('thalamus_left', voxel=np.ones((5, 3), dtype=int),
239239
affine=np.eye(4) * 2, volume_shape=(2, 3, 4))
240-
with assert_raises(ValueError):
240+
with pytest.raises(ValueError):
241241
bm_vox + bm_other_affine
242-
with assert_raises(ValueError):
242+
with pytest.raises(ValueError):
243243
axes.ParcelsAxis.from_brain_models([('a', bm_vox), ('b', bm_other_affine)])
244244

245245
bm_vertex = axes.BrainModelAxis('cortex_left', vertex=np.ones(5, dtype=int), nvertices={'cortex_left': 20})
246246
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):
248248
bm_vertex + bm_other_number
249-
with assert_raises(ValueError):
249+
with pytest.raises(ValueError):
250250
axes.ParcelsAxis.from_brain_models([('a', bm_vertex), ('b', bm_other_number)])
251251

252252
# test equalities
@@ -336,29 +336,29 @@ def test_parcels():
336336
assert len(prc2[3:]['mixed'][1]) == 1
337337
assert prc2[3:]['mixed'][1]['CIFTI_STRUCTURE_CORTEX_LEFT'].shape == (3, )
338338

339-
with assert_raises(IndexError):
339+
with pytest.raises(IndexError):
340340
prc['non_existent']
341341

342342
prc['surface']
343-
with assert_raises(IndexError):
343+
with pytest.raises(IndexError):
344344
# parcel exists twice
345345
prc2['surface']
346346

347347
# break parcels
348348
prc.affine = np.eye(4)
349-
with assert_raises(ValueError):
349+
with pytest.raises(ValueError):
350350
prc.affine = np.eye(3)
351-
with assert_raises(ValueError):
351+
with pytest.raises(ValueError):
352352
prc.affine = np.eye(4).flatten()
353353

354354
prc.volume_shape = (5, 3, 1)
355-
with assert_raises(ValueError):
355+
with pytest.raises(ValueError):
356356
prc.volume_shape = (5., 3, 1)
357-
with assert_raises(ValueError):
357+
with pytest.raises(ValueError):
358358
prc.volume_shape = (5, 3, 1, 4)
359359

360360
# break adding of parcels
361-
with assert_raises(Exception):
361+
with pytest.raises(Exception):
362362
prc + get_label()
363363

364364
prc = get_parcels()
@@ -367,12 +367,12 @@ def test_parcels():
367367

368368
other_prc = get_parcels()
369369
other_prc.affine = np.eye(4) * 2
370-
with assert_raises(ValueError):
370+
with pytest.raises(ValueError):
371371
prc + other_prc
372372

373373
other_prc = get_parcels()
374374
other_prc.volume_shape = (20, 3, 4)
375-
with assert_raises(ValueError):
375+
with pytest.raises(ValueError):
376376
prc + other_prc
377377

378378
# test parcel equalities
@@ -396,13 +396,13 @@ def test_parcels():
396396
prc_other = deepcopy(prc)
397397
prc_other.volume_shape = (10, 3, 4)
398398
assert prc != prc_other
399-
with assert_raises(ValueError):
399+
with pytest.raises(ValueError):
400400
prc + prc_other
401401

402402
prc_other = deepcopy(prc)
403403
prc_other.nvertices['CIFTI_STRUCTURE_CORTEX_LEFT'] = 80
404404
assert prc != prc_other
405-
with assert_raises(ValueError):
405+
with pytest.raises(ValueError):
406406
prc + prc_other
407407

408408
prc_other = deepcopy(prc)
@@ -434,7 +434,7 @@ def test_parcels():
434434
volume_shape=(2, 3, 4),
435435
)
436436

437-
with assert_raises(ValueError):
437+
with pytest.raises(ValueError):
438438
axes.ParcelsAxis(
439439
voxels=[np.ones((3, 2), dtype=int)],
440440
vertices=[{}],
@@ -466,7 +466,7 @@ def test_scalar():
466466

467467
# test equalities
468468
assert sc != get_label()
469-
with assert_raises(Exception):
469+
with pytest.raises(Exception):
470470
sc + get_label()
471471

472472
sc_other = deepcopy(sc)
@@ -485,10 +485,10 @@ def test_scalar():
485485
# test constructor
486486
assert axes.ScalarAxis(['scalar_name'], [{}]) == axes.ScalarAxis(['scalar_name'])
487487

488-
with assert_raises(ValueError):
488+
with pytest.raises(ValueError):
489489
axes.ScalarAxis([['scalar_name']]) # wrong shape
490490

491-
with assert_raises(ValueError):
491+
with pytest.raises(ValueError):
492492
axes.ScalarAxis(['scalar_name'], [{}, {}]) # wrong size
493493

494494

@@ -514,7 +514,7 @@ def test_label():
514514
# test equalities
515515
lab = get_label()
516516
assert lab != get_scalar()
517-
with assert_raises(Exception):
517+
with pytest.raises(Exception):
518518
lab + get_scalar()
519519

520520
other_lab = deepcopy(lab)
@@ -540,10 +540,10 @@ def test_label():
540540
# test constructor
541541
assert axes.LabelAxis(['scalar_name'], [{}], [{}]) == axes.LabelAxis(['scalar_name'], [{}])
542542

543-
with assert_raises(ValueError):
543+
with pytest.raises(ValueError):
544544
axes.LabelAxis([['scalar_name']], [{}]) # wrong shape
545545

546-
with assert_raises(ValueError):
546+
with pytest.raises(ValueError):
547547
axes.LabelAxis(['scalar_name'], [{}, {}]) # wrong size
548548

549549

@@ -558,7 +558,7 @@ def test_series():
558558
assert sr[3].unit == 'HERTZ'
559559
sr[0].unit = 'hertz'
560560
assert sr[0].unit == 'HERTZ'
561-
with assert_raises(ValueError):
561+
with pytest.raises(ValueError):
562562
sr[0].unit = 'non_existent'
563563

564564
sr = list(get_series())
@@ -570,11 +570,17 @@ def test_series():
570570
assert ((sr[1] + sr[0] + sr[0]).time == np.arange(11) * 10 + 8).all()
571571
assert sr[1][2] == 28
572572
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]
578584

579585
# test slicing
580586
assert (sr[0][1:3].time == sr[0].time[1:3]).all()
@@ -590,16 +596,16 @@ def test_series():
590596
assert (sr[0][3:1:-1].time == sr[0].time[3:1:-1]).all()
591597
assert (sr[0][1:3:-1].time == sr[0].time[1:3:-1]).all()
592598

593-
with assert_raises(IndexError):
599+
with pytest.raises(IndexError):
594600
assert sr[0][[0, 1]]
595-
with assert_raises(IndexError):
601+
with pytest.raises(IndexError):
596602
assert sr[0][20]
597-
with assert_raises(IndexError):
603+
with pytest.raises(IndexError):
598604
assert sr[0][-20]
599605

600606
# test_equalities
601607
sr = next(get_series())
602-
with assert_raises(Exception):
608+
with pytest.raises(Exception):
603609
sr + get_scalar()
604610
assert sr != sr[:2]
605611
assert sr == sr[:]

0 commit comments

Comments
 (0)