9
9
10
10
from ...tmpdirs import InTemporaryDirectory
11
11
12
- from nose .tools import assert_true
12
+
13
+ import pytest
13
14
import numpy as np
14
- from numpy .testing import assert_equal , assert_raises , dec , assert_allclose , assert_array_equal
15
+ from numpy .testing import assert_allclose , assert_array_equal
15
16
16
17
from .. import (read_geometry , read_morph_data , read_annot , read_label ,
17
18
write_geometry , write_morph_data , write_annot )
18
19
from ..io import _pack_rgb
19
20
20
21
from ...tests .nibabel_data import get_nibabel_data , needs_nibabel_data
21
22
from ...fileslice import strided_scalar
22
- from ...testing import clear_and_catch_warnings
23
+ from ...testing_pytest import clear_and_catch_warnings
23
24
24
25
DATA_SDIR = 'fsaverage'
25
26
35
36
data_path = pjoin (nib_data , 'nitest-freesurfer' , DATA_SDIR )
36
37
have_freesurfer = isdir (data_path )
37
38
38
- freesurfer_test = dec .skipif (
39
- not have_freesurfer ,
40
- 'cannot find freesurfer {0} directory' .format (DATA_SDIR ))
41
-
39
+ freesurfer_test = pytest .mark .skipif (not have_freesurfer ,
40
+ reason = 'cannot find freesurfer {0} directory' .format (DATA_SDIR ))
42
41
43
42
def _hash_file_content (fname ):
44
43
hasher = hashlib .md5 ()
@@ -53,19 +52,18 @@ def test_geometry():
53
52
"""Test IO of .surf"""
54
53
surf_path = pjoin (data_path , "surf" , "%s.%s" % ("lh" , "inflated" ))
55
54
coords , faces = read_geometry (surf_path )
56
- assert_equal ( 0 , faces .min () )
57
- assert_equal ( coords .shape [0 ], faces .max () + 1 )
55
+ assert 0 == faces .min ()
56
+ assert coords .shape [0 ] == faces .max () + 1
58
57
59
58
surf_path = pjoin (data_path , "surf" , "%s.%s" % ("lh" , "sphere" ))
60
59
coords , faces , volume_info , create_stamp = read_geometry (
61
60
surf_path , read_metadata = True , read_stamp = True )
62
61
63
- assert_equal (0 , faces .min ())
64
- assert_equal (coords .shape [0 ], faces .max () + 1 )
65
- assert_equal (9 , len (volume_info ))
66
- assert_equal ([2 , 0 , 20 ], volume_info ['head' ])
67
- assert_equal (u'created by greve on Thu Jun 8 19:17:51 2006' ,
68
- create_stamp )
62
+ assert 0 == faces .min ()
63
+ assert coords .shape [0 ] == faces .max () + 1
64
+ assert 9 == len (volume_info )
65
+ assert np .array_equal ([2 , 0 , 20 ], volume_info ['head' ])
66
+ assert create_stamp == 'created by greve on Thu Jun 8 19:17:51 2006'
69
67
70
68
# Test equivalence of freesurfer- and nibabel-generated triangular files
71
69
# with respect to read_geometry()
@@ -82,7 +80,8 @@ def test_geometry():
82
80
for key in ('xras' , 'yras' , 'zras' , 'cras' ):
83
81
assert_allclose (volume_info2 [key ], volume_info [key ],
84
82
rtol = 1e-7 , atol = 1e-30 )
85
- assert_equal (volume_info2 ['cras' ], volume_info ['cras' ])
83
+
84
+ assert np .array_equal (volume_info2 ['cras' ], volume_info ['cras' ])
86
85
with open (surf_path , 'rb' ) as fobj :
87
86
np .fromfile (fobj , ">u1" , 3 )
88
87
read_create_stamp = fobj .readline ().decode ().rstrip ('\n ' )
@@ -92,27 +91,27 @@ def test_geometry():
92
91
with clear_and_catch_warnings () as w :
93
92
warnings .filterwarnings ('always' , category = DeprecationWarning )
94
93
read_geometry (surf_path , read_metadata = True )
95
- assert_true ( any ( 'volume information contained' in str ( ww . message )
96
- for ww in w ) )
97
- assert_true ( any ('extension code' in str (ww .message ) for ww in w ) )
94
+
95
+ assert any ( 'volume information contained' in str ( ww . message ) for ww in w )
96
+ assert any ('extension code' in str (ww .message ) for ww in w )
98
97
volume_info ['head' ] = [1 , 2 ]
99
98
with clear_and_catch_warnings () as w :
100
99
write_geometry (surf_path , coords , faces , create_stamp , volume_info )
101
- assert_true ( any ('Unknown extension' in str (ww .message ) for ww in w ) )
100
+ assert any ('Unknown extension' in str (ww .message ) for ww in w )
102
101
volume_info ['a' ] = 0
103
- assert_raises (ValueError , write_geometry , surf_path , coords ,
104
- faces , create_stamp , volume_info )
102
+ with pytest . raises (ValueError ):
103
+ write_geometry ( surf_path , coords , faces , create_stamp , volume_info )
105
104
106
- assert_equal ( create_stamp , read_create_stamp )
105
+ assert create_stamp == read_create_stamp
107
106
108
- np .testing . assert_array_equal (coords , coords2 )
109
- np .testing . assert_array_equal (faces , faces2 )
107
+ assert np .array_equal (coords , coords2 )
108
+ assert np .array_equal (faces , faces2 )
110
109
111
110
# Validate byte ordering
112
111
coords_swapped = coords .byteswap ().newbyteorder ()
113
112
faces_swapped = faces .byteswap ().newbyteorder ()
114
- np .testing . assert_array_equal (coords_swapped , coords )
115
- np .testing . assert_array_equal (faces_swapped , faces )
113
+ assert np .array_equal (coords_swapped , coords )
114
+ assert np .array_equal (faces_swapped , faces )
116
115
117
116
118
117
@freesurfer_test
@@ -122,28 +121,28 @@ def test_quad_geometry():
122
121
new_quad = pjoin (get_nibabel_data (), 'nitest-freesurfer' , 'subjects' ,
123
122
'bert' , 'surf' , 'lh.inflated.nofix' )
124
123
coords , faces = read_geometry (new_quad )
125
- assert_equal ( 0 , faces .min () )
126
- assert_equal ( coords .shape [0 ], faces .max () + 1 )
124
+ assert 0 == faces .min ()
125
+ assert coords .shape [0 ] == ( faces .max () + 1 )
127
126
with InTemporaryDirectory ():
128
127
new_path = 'test'
129
128
write_geometry (new_path , coords , faces )
130
129
coords2 , faces2 = read_geometry (new_path )
131
- assert_equal (coords , coords2 )
132
- assert_equal (faces , faces2 )
130
+ assert np . array_equal (coords ,coords2 )
131
+ assert np . array_equal (faces , faces2 )
133
132
134
133
135
134
@freesurfer_test
136
135
def test_morph_data ():
137
136
"""Test IO of morphometry data file (eg. curvature)."""
138
137
curv_path = pjoin (data_path , "surf" , "%s.%s" % ("lh" , "curv" ))
139
138
curv = read_morph_data (curv_path )
140
- assert_true ( - 1.0 < curv .min () < 0 )
141
- assert_true ( 0 < curv .max () < 1.0 )
139
+ assert - 1.0 < curv .min () < 0
140
+ assert 0 < curv .max () < 1.0
142
141
with InTemporaryDirectory ():
143
142
new_path = 'test'
144
143
write_morph_data (new_path , curv )
145
144
curv2 = read_morph_data (new_path )
146
- assert_equal (curv2 , curv )
145
+ assert np . array_equal (curv2 , curv )
147
146
148
147
149
148
def test_write_morph_data ():
@@ -156,17 +155,17 @@ def test_write_morph_data():
156
155
for shape in okay_shapes :
157
156
write_morph_data ('test.curv' , values .reshape (shape ))
158
157
# Check ordering is preserved, regardless of shape
159
- assert_equal (values , read_morph_data ('test.curv' ))
160
- assert_raises (ValueError , write_morph_data , 'test.curv' ,
161
- np .zeros (shape ), big_num )
158
+ assert np .array_equal (read_morph_data ('test.curv' ), values )
159
+
160
+ with pytest .raises (ValueError ):
161
+ write_morph_data ('test.curv' , np .zeros (shape ), big_num )
162
162
# Windows 32-bit overflows Python int
163
163
if np .dtype (np .int ) != np .dtype (np .int32 ):
164
- assert_raises ( ValueError , write_morph_data , 'test.curv' ,
165
- strided_scalar ((big_num ,)))
164
+ with pytest . raises ( ValueError ):
165
+ write_morph_data ( 'test.curv' , strided_scalar ((big_num ,)))
166
166
for shape in bad_shapes :
167
- assert_raises (ValueError , write_morph_data , 'test.curv' ,
168
- values .reshape (shape ))
169
-
167
+ with pytest .raises (ValueError ):
168
+ write_morph_data ('test.curv' , values .reshape (shape ))
170
169
171
170
@freesurfer_test
172
171
def test_annot ():
@@ -177,18 +176,18 @@ def test_annot():
177
176
hash_ = _hash_file_content (annot_path )
178
177
179
178
labels , ctab , names = read_annot (annot_path )
180
- assert_true ( labels .shape == (163842 , ) )
181
- assert_true ( ctab .shape == (len (names ), 5 ) )
179
+ assert labels .shape == (163842 , )
180
+ assert ctab .shape == (len (names ), 5 )
182
181
183
182
labels_orig = None
184
183
if a == 'aparc' :
185
184
labels_orig , _ , _ = read_annot (annot_path , orig_ids = True )
186
185
np .testing .assert_array_equal (labels == - 1 , labels_orig == 0 )
187
186
# Handle different version of fsaverage
188
187
if hash_ == 'bf0b488994657435cdddac5f107d21e8' :
189
- assert_true ( np .sum (labels_orig == 0 ) == 13887 )
188
+ assert np .sum (labels_orig == 0 ) == 13887
190
189
elif hash_ == 'd4f5b7cbc2ed363ac6fcf89e19353504' :
191
- assert_true ( np .sum (labels_orig == 1639705 ) == 13327 )
190
+ assert np .sum (labels_orig == 1639705 ) == 13327
192
191
else :
193
192
raise RuntimeError ("Unknown freesurfer file. Please report "
194
193
"the problem to the maintainer of nibabel." )
@@ -203,11 +202,11 @@ def test_annot():
203
202
if labels_orig is not None :
204
203
labels_orig_2 , _ , _ = read_annot (annot_path , orig_ids = True )
205
204
206
- np .testing . assert_array_equal (labels , labels2 )
205
+ assert np .array_equal (labels , labels2 )
207
206
if labels_orig is not None :
208
- np .testing . assert_array_equal (labels_orig , labels_orig_2 )
209
- np .testing . assert_array_equal (ctab , ctab2 )
210
- assert_equal ( names , names2 )
207
+ assert np .array_equal (labels_orig , labels_orig_2 )
208
+ assert np .array_equal (ctab , ctab2 )
209
+ assert names == names2
211
210
212
211
213
212
def test_read_write_annot ():
@@ -269,12 +268,10 @@ def test_write_annot_fill_ctab():
269
268
# values back.
270
269
badannot = (10 * np .arange (nlabels , dtype = np .int32 )).reshape (- 1 , 1 )
271
270
rgbal = np .hstack ((rgba , badannot ))
272
- print (labels )
273
271
with clear_and_catch_warnings () as w :
274
272
write_annot (annot_path , labels , rgbal , names , fill_ctab = False )
275
- assert_true (
276
- any ('Annotation values in {} will be incorrect' .format (
277
- annot_path ) == str (ww .message ) for ww in w ))
273
+ assert any ('Annotation values in {} will be incorrect' .format (annot_path ) == str (ww .message )
274
+ for ww in w )
278
275
labels2 , rgbal2 , names2 = read_annot (annot_path , orig_ids = True )
279
276
names2 = [n .decode ('ascii' ) for n in names2 ]
280
277
assert np .all (np .isclose (rgbal2 [:, :4 ], rgba ))
@@ -288,9 +285,8 @@ def test_write_annot_fill_ctab():
288
285
rgbal [:, 2 ] * (2 ** 16 ))
289
286
with clear_and_catch_warnings () as w :
290
287
write_annot (annot_path , labels , rgbal , names , fill_ctab = False )
291
- assert_true (
292
- not any ('Annotation values in {} will be incorrect' .format (
293
- annot_path ) == str (ww .message ) for ww in w ))
288
+ assert all ('Annotation values in {} will be incorrect' .format (annot_path ) != str (ww .message )
289
+ for ww in w )
294
290
labels2 , rgbal2 , names2 = read_annot (annot_path )
295
291
names2 = [n .decode ('ascii' ) for n in names2 ]
296
292
assert np .all (np .isclose (rgbal2 [:, :4 ], rgba ))
@@ -348,13 +344,13 @@ def test_label():
348
344
label_path = pjoin (data_path , "label" , "lh.cortex.label" )
349
345
label = read_label (label_path )
350
346
# XXX : test more
351
- assert_true ( label .min () >= 0 )
352
- assert_true ( label .max () <= 163841 )
353
- assert_true ( label .shape [0 ] <= 163842 )
347
+ assert label .min () >= 0
348
+ assert label .max () <= 163841
349
+ assert label .shape [0 ] <= 163842
354
350
355
351
labels , scalars = read_label (label_path , True )
356
- assert_true ( np .all (labels == label ) )
357
- assert_true ( len (labels ) == len (scalars ) )
352
+ assert np .all (labels == label )
353
+ assert len (labels ) == len (scalars )
358
354
359
355
360
356
def test_write_annot_maxstruct ():
@@ -371,6 +367,6 @@ def test_write_annot_maxstruct():
371
367
# Validate the file can be read
372
368
rt_labels , rt_ctab , rt_names = read_annot (annot_path )
373
369
# Check round-trip
374
- assert_array_equal (labels , rt_labels )
375
- assert_array_equal (rgba , rt_ctab [:, :4 ])
376
- assert_equal ( names , [n .decode ('ascii' ) for n in rt_names ])
370
+ assert np . array_equal (labels , rt_labels )
371
+ assert np . array_equal (rgba , rt_ctab [:, :4 ])
372
+ assert names == [n .decode ('ascii' ) for n in rt_names ]
0 commit comments