Skip to content

Commit 4931cb3

Browse files
committed
RF - make load save tests more explicit, also avoiding need for scipy
1 parent 9688ae5 commit 4931cb3

File tree

1 file changed

+45
-24
lines changed

1 file changed

+45
-24
lines changed

nibabel/tests/test_image_load_save.py

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,23 @@
1616

1717
import numpy as np
1818

19+
# If we don't have scipy, then we cannot write SPM format files
20+
try:
21+
import scipy.io
22+
except ImportError:
23+
have_scipy = False
24+
else:
25+
have_scipy = True
26+
27+
1928
import nibabel as nib
2029
import nibabel.analyze as ana
2130
import nibabel.spm99analyze as spm99
2231
import nibabel.spm2analyze as spm2
2332
import nibabel.nifti1 as ni1
2433
import nibabel.loadsave as nils
34+
from .. import (Nifti1Image, Nifti1Pair, MincImage, Spm2AnalyzeImage,
35+
Spm99AnalyzeImage, AnalyzeImage)
2536

2637
from ..tmpdirs import InTemporaryDirectory
2738

@@ -122,12 +133,7 @@ def test_save_load():
122133
# windows errors when trying to open files or delete the
123134
# temporary directory.
124135
del re_img
125-
try:
126-
import scipy.io
127-
except ImportError:
128-
# ignore if there is no matfile reader, and restart
129-
pass
130-
else:
136+
if have_scipy: # skip we we cannot read .mat files
131137
spm2.save(img, sifn)
132138
re_img2 = nils.load(sifn)
133139
yield assert_true(isinstance(re_img2, spm2.Spm2AnalyzeImage))
@@ -218,26 +224,41 @@ def test_negative_load_save():
218224
yield assert_array_almost_equal(re_img.get_data(), data, 4)
219225

220226

221-
@parametric
222227
def test_filename_save():
228+
# This is to test the logic in the load and save routines, relating
229+
# extensions to filetypes
230+
# Tuples of class, ext, loadedclass
231+
inklass_ext_loadklasses = (
232+
(Nifti1Image, '.nii', Nifti1Image),
233+
(Nifti1Image, '.img', Nifti1Pair),
234+
(MincImage, '.nii', Nifti1Image),
235+
(MincImage, '.img', Nifti1Pair),
236+
(Spm2AnalyzeImage, '.nii', Nifti1Image),
237+
(Spm2AnalyzeImage, '.img', Spm2AnalyzeImage),
238+
(Spm99AnalyzeImage, '.nii', Nifti1Image),
239+
(Spm99AnalyzeImage, '.img', Spm99AnalyzeImage),
240+
(AnalyzeImage, '.nii', Nifti1Image),
241+
(AnalyzeImage, '.img', Spm2AnalyzeImage),
242+
)
223243
shape = (2, 4, 6)
224244
affine = np.diag([1, 2, 3, 1])
225245
data = np.arange(np.prod(shape), dtype='f4').reshape(shape)
226-
for r_class_def in nib.class_map.values():
227-
r_class = r_class_def['class']
228-
img = r_class(data, affine)
229-
for w_class_def in nib.class_map.values():
230-
if not w_class_def['rw']: # can't write this class
246+
for inklass, out_ext, loadklass in inklass_ext_loadklasses:
247+
if not have_scipy:
248+
# We can't load a SPM analyze type without scipy. These types have
249+
# a 'mat' file (the type we can't load)
250+
if ('mat', '.mat') in loadklass.files_types:
231251
continue
232-
out_ext = w_class_def['ext']
233-
try:
234-
pth = mkdtemp()
235-
fname = pjoin(pth, 'image' + out_ext)
236-
nib.save(img, fname)
237-
rt_img = nib.load(fname)
238-
yield assert_array_almost_equal(rt_img.get_data(), data)
239-
# delete image to allow file close. Otherwise windows
240-
# raises an error when trying to delete the directory
241-
del rt_img
242-
finally:
243-
shutil.rmtree(pth)
252+
img = inklass(data, affine)
253+
try:
254+
pth = mkdtemp()
255+
fname = pjoin(pth, 'image' + out_ext)
256+
nib.save(img, fname)
257+
rt_img = nib.load(fname)
258+
assert_array_almost_equal(rt_img.get_data(), data)
259+
assert_true(isinstance(rt_img, loadklass))
260+
# delete image to allow file close. Otherwise windows
261+
# raises an error when trying to delete the directory
262+
del rt_img
263+
finally:
264+
shutil.rmtree(pth)

0 commit comments

Comments
 (0)