Skip to content

Commit baab901

Browse files
authored
Merge pull request #881 from orduek/test/pytest_freesurfer
TEST: Convert FreeSurfer tests to pytest
2 parents 5f4436f + 27f0f03 commit baab901

File tree

5 files changed

+150
-146
lines changed

5 files changed

+150
-146
lines changed

.azure-pipelines/windows.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ jobs:
102102
-I test_round_trip ^
103103
-I test_rstutils ^
104104
-I test_scaling ^
105+
-I test_wrapstruct ^
106+
-I test_io ^
105107
-I test_scripts ^
106108
-I test_spaces ^
107109
-I test_testing ^

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,4 @@ doc/source/reference
8585
venv/
8686
.buildbot.patch
8787
.vscode
88+
for_testing/

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ script:
195195
-I test_round_trip \
196196
-I test_rstutils \
197197
-I test_scaling \
198+
-I test_wrapstruct \
199+
-I test_io \
198200
-I test_scripts \
199201
-I test_spaces \
200202
-I test_testing \

nibabel/freesurfer/tests/test_io.py

Lines changed: 61 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@
99

1010
from ...tmpdirs import InTemporaryDirectory
1111

12-
from nose.tools import assert_true
12+
13+
import pytest
1314
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
1516

1617
from .. import (read_geometry, read_morph_data, read_annot, read_label,
1718
write_geometry, write_morph_data, write_annot)
1819
from ..io import _pack_rgb
1920

2021
from ...tests.nibabel_data import get_nibabel_data, needs_nibabel_data
2122
from ...fileslice import strided_scalar
22-
from ...testing import clear_and_catch_warnings
23+
from ...testing_pytest import clear_and_catch_warnings
2324

2425
DATA_SDIR = 'fsaverage'
2526

@@ -35,10 +36,8 @@
3536
data_path = pjoin(nib_data, 'nitest-freesurfer', DATA_SDIR)
3637
have_freesurfer = isdir(data_path)
3738

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))
4241

4342
def _hash_file_content(fname):
4443
hasher = hashlib.md5()
@@ -53,19 +52,18 @@ def test_geometry():
5352
"""Test IO of .surf"""
5453
surf_path = pjoin(data_path, "surf", "%s.%s" % ("lh", "inflated"))
5554
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
5857

5958
surf_path = pjoin(data_path, "surf", "%s.%s" % ("lh", "sphere"))
6059
coords, faces, volume_info, create_stamp = read_geometry(
6160
surf_path, read_metadata=True, read_stamp=True)
6261

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'
6967

7068
# Test equivalence of freesurfer- and nibabel-generated triangular files
7169
# with respect to read_geometry()
@@ -82,7 +80,8 @@ def test_geometry():
8280
for key in ('xras', 'yras', 'zras', 'cras'):
8381
assert_allclose(volume_info2[key], volume_info[key],
8482
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'])
8685
with open(surf_path, 'rb') as fobj:
8786
np.fromfile(fobj, ">u1", 3)
8887
read_create_stamp = fobj.readline().decode().rstrip('\n')
@@ -92,27 +91,27 @@ def test_geometry():
9291
with clear_and_catch_warnings() as w:
9392
warnings.filterwarnings('always', category=DeprecationWarning)
9493
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)
9897
volume_info['head'] = [1, 2]
9998
with clear_and_catch_warnings() as w:
10099
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)
102101
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)
105104

106-
assert_equal(create_stamp, read_create_stamp)
105+
assert create_stamp == read_create_stamp
107106

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)
110109

111110
# Validate byte ordering
112111
coords_swapped = coords.byteswap().newbyteorder()
113112
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)
116115

117116

118117
@freesurfer_test
@@ -122,28 +121,28 @@ def test_quad_geometry():
122121
new_quad = pjoin(get_nibabel_data(), 'nitest-freesurfer', 'subjects',
123122
'bert', 'surf', 'lh.inflated.nofix')
124123
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)
127126
with InTemporaryDirectory():
128127
new_path = 'test'
129128
write_geometry(new_path, coords, faces)
130129
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)
133132

134133

135134
@freesurfer_test
136135
def test_morph_data():
137136
"""Test IO of morphometry data file (eg. curvature)."""
138137
curv_path = pjoin(data_path, "surf", "%s.%s" % ("lh", "curv"))
139138
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
142141
with InTemporaryDirectory():
143142
new_path = 'test'
144143
write_morph_data(new_path, curv)
145144
curv2 = read_morph_data(new_path)
146-
assert_equal(curv2, curv)
145+
assert np.array_equal(curv2, curv)
147146

148147

149148
def test_write_morph_data():
@@ -156,17 +155,17 @@ def test_write_morph_data():
156155
for shape in okay_shapes:
157156
write_morph_data('test.curv', values.reshape(shape))
158157
# 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)
162162
# Windows 32-bit overflows Python int
163163
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,)))
166166
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))
170169

171170
@freesurfer_test
172171
def test_annot():
@@ -177,18 +176,18 @@ def test_annot():
177176
hash_ = _hash_file_content(annot_path)
178177

179178
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)
182181

183182
labels_orig = None
184183
if a == 'aparc':
185184
labels_orig, _, _ = read_annot(annot_path, orig_ids=True)
186185
np.testing.assert_array_equal(labels == -1, labels_orig == 0)
187186
# Handle different version of fsaverage
188187
if hash_ == 'bf0b488994657435cdddac5f107d21e8':
189-
assert_true(np.sum(labels_orig == 0) == 13887)
188+
assert np.sum(labels_orig == 0) == 13887
190189
elif hash_ == 'd4f5b7cbc2ed363ac6fcf89e19353504':
191-
assert_true(np.sum(labels_orig == 1639705) == 13327)
190+
assert np.sum(labels_orig == 1639705) == 13327
192191
else:
193192
raise RuntimeError("Unknown freesurfer file. Please report "
194193
"the problem to the maintainer of nibabel.")
@@ -203,11 +202,11 @@ def test_annot():
203202
if labels_orig is not None:
204203
labels_orig_2, _, _ = read_annot(annot_path, orig_ids=True)
205204

206-
np.testing.assert_array_equal(labels, labels2)
205+
assert np.array_equal(labels, labels2)
207206
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
211210

212211

213212
def test_read_write_annot():
@@ -269,12 +268,10 @@ def test_write_annot_fill_ctab():
269268
# values back.
270269
badannot = (10 * np.arange(nlabels, dtype=np.int32)).reshape(-1, 1)
271270
rgbal = np.hstack((rgba, badannot))
272-
print(labels)
273271
with clear_and_catch_warnings() as w:
274272
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)
278275
labels2, rgbal2, names2 = read_annot(annot_path, orig_ids=True)
279276
names2 = [n.decode('ascii') for n in names2]
280277
assert np.all(np.isclose(rgbal2[:, :4], rgba))
@@ -288,9 +285,8 @@ def test_write_annot_fill_ctab():
288285
rgbal[:, 2] * (2 ** 16))
289286
with clear_and_catch_warnings() as w:
290287
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)
294290
labels2, rgbal2, names2 = read_annot(annot_path)
295291
names2 = [n.decode('ascii') for n in names2]
296292
assert np.all(np.isclose(rgbal2[:, :4], rgba))
@@ -348,13 +344,13 @@ def test_label():
348344
label_path = pjoin(data_path, "label", "lh.cortex.label")
349345
label = read_label(label_path)
350346
# 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
354350

355351
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)
358354

359355

360356
def test_write_annot_maxstruct():
@@ -371,6 +367,6 @@ def test_write_annot_maxstruct():
371367
# Validate the file can be read
372368
rt_labels, rt_ctab, rt_names = read_annot(annot_path)
373369
# 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

Comments
 (0)