|
16 | 16 |
|
17 | 17 | import numpy as np
|
18 | 18 |
|
| 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 | + |
19 | 28 | import nibabel as nib
|
20 | 29 | import nibabel.analyze as ana
|
21 | 30 | import nibabel.spm99analyze as spm99
|
22 | 31 | import nibabel.spm2analyze as spm2
|
23 | 32 | import nibabel.nifti1 as ni1
|
24 | 33 | import nibabel.loadsave as nils
|
| 34 | +from .. import (Nifti1Image, Nifti1Pair, MincImage, Spm2AnalyzeImage, |
| 35 | + Spm99AnalyzeImage, AnalyzeImage) |
25 | 36 |
|
26 | 37 | from ..tmpdirs import InTemporaryDirectory
|
27 | 38 |
|
@@ -122,12 +133,7 @@ def test_save_load():
|
122 | 133 | # windows errors when trying to open files or delete the
|
123 | 134 | # temporary directory.
|
124 | 135 | 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 |
131 | 137 | spm2.save(img, sifn)
|
132 | 138 | re_img2 = nils.load(sifn)
|
133 | 139 | yield assert_true(isinstance(re_img2, spm2.Spm2AnalyzeImage))
|
@@ -218,26 +224,41 @@ def test_negative_load_save():
|
218 | 224 | yield assert_array_almost_equal(re_img.get_data(), data, 4)
|
219 | 225 |
|
220 | 226 |
|
221 |
| -@parametric |
222 | 227 | 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 | + ) |
223 | 243 | shape = (2, 4, 6)
|
224 | 244 | affine = np.diag([1, 2, 3, 1])
|
225 | 245 | 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: |
231 | 251 | 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