Skip to content

Commit 72fa2d2

Browse files
committed
TEST: Suppress and check warnings in streamlines
1 parent 7acde5a commit 72fa2d2

File tree

5 files changed

+100
-81
lines changed

5 files changed

+100
-81
lines changed

nibabel/streamlines/tests/test_array_sequence.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ def test_creating_arraysequence_from_generator(self):
100100
seq_with_buffer = ArraySequence(gen_2, buffer_size=256)
101101

102102
# Check buffer size effect
103-
assert seq_with_buffer.data.shape == seq.data.shape
103+
with pytest.warns(Warning):
104+
assert seq_with_buffer.data.shape == seq.data.shape
104105
assert seq_with_buffer._buffer_size > seq._buffer_size
105106

106107
# Check generator result

nibabel/streamlines/tests/test_streamlines.py

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import tempfile
44
import numpy as np
55

6+
import pytest
7+
68
from os.path import join as pjoin
79

810
import nibabel as nib
@@ -140,8 +142,10 @@ def test_load_empty_file(self):
140142
else:
141143
assert type(tfile.tractogram), LazyTractogram
142144

143-
assert_tractogram_equal(tfile.tractogram,
144-
DATA['empty_tractogram'])
145+
with pytest.warns(None) as w:
146+
assert_tractogram_equal(tfile.tractogram,
147+
DATA['empty_tractogram'])
148+
assert len(w) == lazy_load
145149

146150
def test_load_simple_file(self):
147151
for lazy_load in [False, True]:
@@ -155,8 +159,10 @@ def test_load_simple_file(self):
155159
else:
156160
assert type(tfile.tractogram), LazyTractogram
157161

158-
assert_tractogram_equal(tfile.tractogram,
159-
DATA['simple_tractogram'])
162+
with pytest.warns(None) as w:
163+
assert_tractogram_equal(tfile.tractogram,
164+
DATA['simple_tractogram'])
165+
assert len(w) == lazy_load
160166

161167
def test_load_complex_file(self):
162168
for lazy_load in [False, True]:
@@ -180,8 +186,10 @@ def test_load_complex_file(self):
180186
data = DATA['data_per_streamline']
181187
tractogram.data_per_streamline = data
182188

183-
assert_tractogram_equal(tfile.tractogram,
184-
tractogram)
189+
with pytest.warns(None) as w:
190+
assert_tractogram_equal(tfile.tractogram,
191+
tractogram)
192+
assert len(w) == lazy_load
185193

186194
def test_save_tractogram_file(self):
187195
tractogram = Tractogram(DATA['streamlines'],
@@ -193,15 +201,14 @@ def test_save_tractogram_file(self):
193201
nib.streamlines.save(trk_file, "dummy.trk", header={})
194202

195203
# Wrong extension.
196-
with clear_and_catch_warnings(record=True,
197-
modules=[nib.streamlines]) as w:
204+
with pytest.warns(None) as w:
198205
trk_file = trk.TrkFile(tractogram)
199206
with self.assertRaises(ValueError):
200207
nib.streamlines.save(trk_file, "dummy.tck", header={})
201208

202-
assert len(w) == 1
203-
assert issubclass(w[0].category, ExtensionWarning)
204-
assert "extension" in str(w[0].message)
209+
assert len(w) == 1
210+
assert issubclass(w[0].category, ExtensionWarning)
211+
assert "extension" in str(w[0].message)
205212

206213
with InTemporaryDirectory():
207214
nib.streamlines.save(trk_file, "dummy.trk")
@@ -237,33 +244,32 @@ def test_save_complex_file(self):
237244
with InTemporaryDirectory():
238245
filename = 'streamlines' + ext
239246

240-
with clear_and_catch_warnings(record=True,
241-
modules=[trk]) as w:
247+
with pytest.warns(None) as w:
242248
nib.streamlines.save(complex_tractogram, filename)
243249

244-
# If streamlines format does not support saving data
245-
# per point or data per streamline, warning messages
246-
# should be issued.
247-
nb_expected_warnings = \
248-
((not cls.SUPPORTS_DATA_PER_POINT) +
249-
(not cls.SUPPORTS_DATA_PER_STREAMLINE))
250+
# If streamlines format does not support saving data
251+
# per point or data per streamline, warning messages
252+
# should be issued.
253+
nb_expected_warnings = \
254+
((not cls.SUPPORTS_DATA_PER_POINT) +
255+
(not cls.SUPPORTS_DATA_PER_STREAMLINE))
250256

251-
assert len(w) == nb_expected_warnings
252-
for i in range(nb_expected_warnings):
253-
assert issubclass(w[i].category, Warning)
257+
assert len(w) == nb_expected_warnings
258+
for i in range(nb_expected_warnings):
259+
assert issubclass(w[i].category, Warning)
254260

255-
tractogram = Tractogram(DATA['streamlines'],
256-
affine_to_rasmm=np.eye(4))
261+
tractogram = Tractogram(DATA['streamlines'],
262+
affine_to_rasmm=np.eye(4))
257263

258-
if cls.SUPPORTS_DATA_PER_POINT:
259-
tractogram.data_per_point = DATA['data_per_point']
264+
if cls.SUPPORTS_DATA_PER_POINT:
265+
tractogram.data_per_point = DATA['data_per_point']
260266

261-
if cls.SUPPORTS_DATA_PER_STREAMLINE:
262-
data = DATA['data_per_streamline']
263-
tractogram.data_per_streamline = data
267+
if cls.SUPPORTS_DATA_PER_STREAMLINE:
268+
data = DATA['data_per_streamline']
269+
tractogram.data_per_streamline = data
264270

265-
tfile = nib.streamlines.load(filename, lazy_load=False)
266-
assert_tractogram_equal(tfile.tractogram, tractogram)
271+
tfile = nib.streamlines.load(filename, lazy_load=False)
272+
assert_tractogram_equal(tfile.tractogram, tractogram)
267273

268274
def test_save_sliced_tractogram(self):
269275
tractogram = Tractogram(DATA['streamlines'],

nibabel/streamlines/tests/test_tck.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from os.path import join as pjoin
55

66
from io import BytesIO
7-
from nibabel.py3k import asbytes
87

98
from ..array_sequence import ArraySequence
109
from ..tractogram import Tractogram
@@ -48,12 +47,16 @@ class TestTCK(unittest.TestCase):
4847
def test_load_empty_file(self):
4948
for lazy_load in [False, True]:
5049
tck = TckFile.load(DATA['empty_tck_fname'], lazy_load=lazy_load)
51-
assert_tractogram_equal(tck.tractogram, DATA['empty_tractogram'])
50+
with pytest.warns(None) as w:
51+
assert_tractogram_equal(tck.tractogram, DATA['empty_tractogram'])
52+
assert len(w) == lazy_load
5253

5354
def test_load_simple_file(self):
5455
for lazy_load in [False, True]:
5556
tck = TckFile.load(DATA['simple_tck_fname'], lazy_load=lazy_load)
56-
assert_tractogram_equal(tck.tractogram, DATA['simple_tractogram'])
57+
with pytest.warns(None) as w:
58+
assert_tractogram_equal(tck.tractogram, DATA['simple_tractogram'])
59+
assert len(w) == lazy_load
5760

5861
# Force TCK loading to use buffering.
5962
buffer_size = 1. / 1024**2 # 1 bytes
@@ -87,44 +90,42 @@ def test_load_simple_file_in_big_endian(self):
8790
for lazy_load in [False, True]:
8891
tck = TckFile.load(DATA['simple_tck_big_endian_fname'],
8992
lazy_load=lazy_load)
90-
assert_tractogram_equal(tck.tractogram, DATA['simple_tractogram'])
93+
with pytest.warns(None) as w:
94+
assert_tractogram_equal(tck.tractogram, DATA['simple_tractogram'])
95+
assert len(w) == lazy_load
9196
assert tck.header['datatype'] == 'Float32BE'
9297

9398
def test_load_file_with_wrong_information(self):
9499
tck_file = open(DATA['simple_tck_fname'], 'rb').read()
95100

96101
# Simulate a TCK file where `datatype` has not the right endianness.
97-
new_tck_file = tck_file.replace(asbytes("Float32LE"),
98-
asbytes("Float32BE"))
102+
new_tck_file = tck_file.replace(b"Float32LE", b"Float32BE")
99103

100104
with pytest.raises(DataError):
101105
TckFile.load(BytesIO(new_tck_file))
102106

103107
# Simulate a TCK file with unsupported `datatype`.
104-
new_tck_file = tck_file.replace(asbytes("Float32LE"),
105-
asbytes("int32"))
108+
new_tck_file = tck_file.replace(b"Float32LE", b"int32")
106109
with pytest.raises(HeaderError):
107110
TckFile.load(BytesIO(new_tck_file))
108111

109112
# Simulate a TCK file with no `datatype` field.
110113
new_tck_file = tck_file.replace(b"datatype: Float32LE\n", b"")
111114
# Need to adjust data offset.
112115
new_tck_file = new_tck_file.replace(b"file: . 67\n", b"file: . 47\n")
113-
with clear_and_catch_warnings(record=True, modules=[tck_module]) as w:
116+
with pytest.warns(HeaderWarning) as w:
114117
tck = TckFile.load(BytesIO(new_tck_file))
115-
assert len(w) == 1
116-
assert issubclass(w[0].category, HeaderWarning)
117-
assert "Missing 'datatype'" in str(w[0].message)
118-
assert_array_equal(tck.header['datatype'], "Float32LE")
118+
assert len(w) == 1
119+
assert "Missing 'datatype'" in str(w[0].message)
120+
assert_array_equal(tck.header['datatype'], "Float32LE")
119121

120122
# Simulate a TCK file with no `file` field.
121123
new_tck_file = tck_file.replace(b"\nfile: . 67", b"")
122-
with clear_and_catch_warnings(record=True, modules=[tck_module]) as w:
124+
with pytest.warns(HeaderWarning) as w:
123125
tck = TckFile.load(BytesIO(new_tck_file))
124-
assert len(w) == 1
125-
assert issubclass(w[0].category, HeaderWarning)
126-
assert "Missing 'file'" in str(w[0].message)
127-
assert_array_equal(tck.header['file'], ". 56")
126+
assert len(w) == 1
127+
assert "Missing 'file'" in str(w[0].message)
128+
assert_array_equal(tck.header['file'], ". 56")
128129

129130
# Simulate a TCK file with `file` field pointing to another file.
130131
new_tck_file = tck_file.replace(b"file: . 67\n",

nibabel/streamlines/tests/test_tractogram.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,8 @@ def test_lazy_tractogram_creation(self):
811811

812812
# Empty `LazyTractogram`
813813
tractogram = LazyTractogram()
814-
check_tractogram(tractogram)
814+
with pytest.warns(Warning):
815+
check_tractogram(tractogram)
815816
assert tractogram.affine_to_rasmm is None
816817

817818
# Create tractogram with streamlines and other data
@@ -832,7 +833,8 @@ def test_lazy_tractogram_creation(self):
832833
def test_lazy_tractogram_from_data_func(self):
833834
# Create an empty `LazyTractogram` yielding nothing.
834835
tractogram = LazyTractogram.from_data_func(lambda: iter([]))
835-
check_tractogram(tractogram)
836+
with pytest.warns(Warning):
837+
check_tractogram(tractogram)
836838

837839
# Create `LazyTractogram` from a generator function yielding
838840
# TractogramItem.
@@ -852,7 +854,8 @@ def _data_gen():
852854
data_for_points)
853855

854856
tractogram = LazyTractogram.from_data_func(_data_gen)
855-
assert_tractogram_equal(tractogram, DATA['tractogram'])
857+
with pytest.warns(Warning):
858+
assert_tractogram_equal(tractogram, DATA['tractogram'])
856859

857860
# Creating a LazyTractogram from not a corouting should raise an error.
858861
with pytest.raises(TypeError):
@@ -921,10 +924,11 @@ def test_lazy_tractogram_apply_affine(self):
921924
assert_array_equal(transformed_tractogram._affine_to_apply, affine)
922925
assert_array_equal(transformed_tractogram.affine_to_rasmm,
923926
np.dot(np.eye(4), np.linalg.inv(affine)))
924-
check_tractogram(transformed_tractogram,
925-
streamlines=[s*scaling for s in DATA['streamlines']],
926-
data_per_streamline=DATA['data_per_streamline'],
927-
data_per_point=DATA['data_per_point'])
927+
with pytest.warns(Warning):
928+
check_tractogram(transformed_tractogram,
929+
streamlines=[s*scaling for s in DATA['streamlines']],
930+
data_per_streamline=DATA['data_per_streamline'],
931+
data_per_point=DATA['data_per_point'])
928932

929933
# Apply affine again and check the affine_to_rasmm.
930934
transformed_tractogram = transformed_tractogram.apply_affine(affine)
@@ -946,10 +950,11 @@ def test_lazy_tractogram_apply_affine(self):
946950
transformed_tractogram = tractogram.apply_affine(affine)
947951
assert_array_equal(transformed_tractogram._affine_to_apply, affine)
948952
assert transformed_tractogram.affine_to_rasmm is None
949-
check_tractogram(transformed_tractogram,
950-
streamlines=[s*scaling for s in DATA['streamlines']],
951-
data_per_streamline=DATA['data_per_streamline'],
952-
data_per_point=DATA['data_per_point'])
953+
with pytest.warns(Warning):
954+
check_tractogram(transformed_tractogram,
955+
streamlines=[s*scaling for s in DATA['streamlines']],
956+
data_per_streamline=DATA['data_per_streamline'],
957+
data_per_point=DATA['data_per_point'])
953958

954959
# Calling apply_affine with lazy=False should fail for LazyTractogram.
955960
tractogram = DATA['lazy_tractogram'].copy()
@@ -1019,4 +1024,5 @@ def test_lazy_tractogram_copy(self):
10191024
DATA['lazy_tractogram']._affine_to_apply)
10201025

10211026
# Check the data are the equivalent.
1022-
assert_tractogram_equal(tractogram, DATA['tractogram'])
1027+
with pytest.warns(Warning):
1028+
assert_tractogram_equal(tractogram, DATA['tractogram'])

nibabel/streamlines/tests/test_trk.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,23 @@ class TestTRK(unittest.TestCase):
8787
def test_load_empty_file(self):
8888
for lazy_load in [False, True]:
8989
trk = TrkFile.load(DATA['empty_trk_fname'], lazy_load=lazy_load)
90-
assert_tractogram_equal(trk.tractogram, DATA['empty_tractogram'])
90+
with pytest.warns(None) as w:
91+
assert_tractogram_equal(trk.tractogram, DATA['empty_tractogram'])
92+
assert len(w) == lazy_load
9193

9294
def test_load_simple_file(self):
9395
for lazy_load in [False, True]:
9496
trk = TrkFile.load(DATA['simple_trk_fname'], lazy_load=lazy_load)
95-
assert_tractogram_equal(trk.tractogram, DATA['simple_tractogram'])
97+
with pytest.warns(None) as w:
98+
assert_tractogram_equal(trk.tractogram, DATA['simple_tractogram'])
99+
assert len(w) == lazy_load
96100

97101
def test_load_complex_file(self):
98102
for lazy_load in [False, True]:
99103
trk = TrkFile.load(DATA['complex_trk_fname'], lazy_load=lazy_load)
100-
assert_tractogram_equal(trk.tractogram, DATA['complex_tractogram'])
104+
with pytest.warns(None) as w:
105+
assert_tractogram_equal(trk.tractogram, DATA['complex_tractogram'])
106+
assert len(w) == lazy_load
101107

102108
def trk_with_bytes(self, trk_key='simple_trk_fname', endian='<'):
103109
""" Return example trk file bytes and struct view onto bytes """
@@ -129,12 +135,11 @@ def test_load_file_with_wrong_information(self):
129135
# Simulate a TRK where `vox_to_ras` is not recorded (i.e. all zeros).
130136
trk_struct, trk_bytes = self.trk_with_bytes()
131137
trk_struct[Field.VOXEL_TO_RASMM] = np.zeros((4, 4))
132-
with clear_and_catch_warnings(record=True, modules=[trk_module]) as w:
138+
with pytest.warns(HeaderWarning) as w:
133139
trk = TrkFile.load(BytesIO(trk_bytes))
134-
assert len(w) == 1
135-
assert issubclass(w[0].category, HeaderWarning)
136-
assert "identity" in str(w[0].message)
137-
assert_array_equal(trk.affine, np.eye(4))
140+
assert len(w) == 1
141+
assert "identity" in str(w[0].message)
142+
assert_array_equal(trk.affine, np.eye(4))
138143

139144
# Simulate a TRK where `vox_to_ras` is invalid.
140145
trk_struct, trk_bytes = self.trk_with_bytes()
@@ -146,11 +151,10 @@ def test_load_file_with_wrong_information(self):
146151
# Simulate a TRK file where `voxel_order` was not provided.
147152
trk_struct, trk_bytes = self.trk_with_bytes()
148153
trk_struct[Field.VOXEL_ORDER] = b''
149-
with clear_and_catch_warnings(record=True, modules=[trk_module]) as w:
154+
with pytest.warns(HeaderWarning) as w:
150155
TrkFile.load(BytesIO(trk_bytes))
151-
assert len(w) == 1
152-
assert issubclass(w[0].category, HeaderWarning)
153-
assert "LPS" in str(w[0].message)
156+
assert len(w) == 1
157+
assert "LPS" in str(w[0].message)
154158

155159
# Simulate a TRK file with an unsupported version.
156160
trk_struct, trk_bytes = self.trk_with_bytes()
@@ -186,13 +190,12 @@ def test_load_trk_version_1(self):
186190
assert_array_equal(trk.affine, np.diag([2, 3, 4, 1]))
187191
# Next check that affine assumed identity if version 1.
188192
trk_struct['version'] = 1
189-
with clear_and_catch_warnings(record=True, modules=[trk_module]) as w:
193+
with pytest.warns(HeaderWarning) as w:
190194
trk = TrkFile.load(BytesIO(trk_bytes))
191-
assert len(w) == 1
192-
assert issubclass(w[0].category, HeaderWarning)
193-
assert "identity" in str(w[0].message)
194-
assert_array_equal(trk.affine, np.eye(4))
195-
assert_array_equal(trk.header['version'], 1)
195+
assert len(w) == 1
196+
assert "identity" in str(w[0].message)
197+
assert_array_equal(trk.affine, np.eye(4))
198+
assert_array_equal(trk.header['version'], 1)
196199

197200
def test_load_complex_file_in_big_endian(self):
198201
trk_struct, trk_bytes = self.trk_with_bytes(
@@ -206,7 +209,9 @@ def test_load_complex_file_in_big_endian(self):
206209
for lazy_load in [False, True]:
207210
trk = TrkFile.load(DATA['complex_trk_big_endian_fname'],
208211
lazy_load=lazy_load)
209-
assert_tractogram_equal(trk.tractogram, DATA['complex_tractogram'])
212+
with pytest.warns(None) as w:
213+
assert_tractogram_equal(trk.tractogram, DATA['complex_tractogram'])
214+
assert len(w) == lazy_load
210215

211216
def test_tractogram_file_properties(self):
212217
trk = TrkFile.load(DATA['simple_trk_fname'])

0 commit comments

Comments
 (0)