Skip to content

Commit 7af29a2

Browse files
committed
Merge pull request #326 from bcipolli/issue-324
MRG: Use "optional_package" for scipy checks In a few places, scipy was still checked for in `try.. catch` blocks, rather than a call to `optional_package`. Switch those over. Made another related changes as well, just because I was there: * Turn `nibabel/tests/data/check_parrec_reslice.py` into an importable module, and fail more gracefully if `scipy` is not installed.
2 parents a84c907 + 9a2a8de commit 7af29a2

File tree

6 files changed

+41
-45
lines changed

6 files changed

+41
-45
lines changed

nibabel/imageclasses.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,9 @@
1515
from .freesurfer import MGHImage
1616
from .parrec import PARRECImage
1717
from .volumeutils import Recoder
18+
from .optpkg import optional_package
19+
_, have_scipy, _ = optional_package('scipy')
1820

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
2621

2722
# mapping of names to classes and class functionality
2823

nibabel/tests/data/check_parrec_reslice.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,19 @@
2424
import glob
2525
import numpy as np
2626
import numpy.linalg as npl
27-
np.set_printoptions(suppress=True, precision=4)
2827

2928
import nibabel as nib
3029
from nibabel import parrec
3130
from nibabel.affines import to_matvec
31+
from nibabel.optpkg import optional_package
32+
_, have_scipy, _ = optional_package('scipy')
3233

33-
from scipy import ndimage as spnd
3434

3535
def resample_img2img(img_to, img_from, order=1, out_class=nib.Nifti1Image):
36+
if not have_scipy:
37+
raise Exception('Scipy must be installed to run resample_img2img.')
38+
39+
from scipy import ndimage as spnd
3640
vox2vox = npl.inv(img_from.affine).dot(img_to.affine)
3741
rzs, trans = to_matvec(vox2vox)
3842
data = spnd.affine_transform(img_from.get_data(),
@@ -49,22 +53,24 @@ def gmean_norm(data):
4953
return data / gmean
5054

5155

52-
normal_fname = "Phantom_EPI_3mm_tra_SENSE_6_1.PAR"
53-
normal_img = parrec.load(normal_fname)
54-
normal_data = normal_img.get_data()
55-
normal_normed = gmean_norm(normal_data)
56+
if __name__ == '__main__':
57+
np.set_printoptions(suppress=True, precision=4)
58+
normal_fname = "Phantom_EPI_3mm_tra_SENSE_6_1.PAR"
59+
normal_img = parrec.load(normal_fname)
60+
normal_data = normal_img.get_data()
61+
normal_normed = gmean_norm(normal_data)
5662

57-
print("RMS of standard image {:<44}: {}".format(
58-
normal_fname,
59-
np.sqrt(np.sum(normal_normed ** 2))))
63+
print("RMS of standard image {:<44}: {}".format(
64+
normal_fname,
65+
np.sqrt(np.sum(normal_normed ** 2))))
6066

61-
for parfile in glob.glob("*.PAR"):
62-
if parfile == normal_fname:
63-
continue
64-
funny_img = parrec.load(parfile)
65-
fixed_img = resample_img2img(normal_img, funny_img)
66-
fixed_data = fixed_img.get_data()
67-
difference_data = normal_normed - gmean_norm(fixed_data)
68-
print('RMS resliced {:<52} : {}'.format(
69-
parfile,
70-
np.sqrt(np.sum(difference_data ** 2))))
67+
for parfile in glob.glob("*.PAR"):
68+
if parfile == normal_fname:
69+
continue
70+
funny_img = parrec.load(parfile)
71+
fixed_img = resample_img2img(normal_img, funny_img)
72+
fixed_data = fixed_img.get_data()
73+
difference_data = normal_normed - gmean_norm(fixed_data)
74+
print('RMS resliced {:<52} : {}'.format(
75+
parfile,
76+
np.sqrt(np.sum(difference_data ** 2))))

nibabel/tests/test_helpers.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
""" Helper functions for tests
22
"""
3+
from io import BytesIO
34

45
import numpy as np
56

6-
from io import BytesIO
77
from ..openers import Opener
88
from ..tmpdirs import InTemporaryDirectory
9+
from ..optpkg import optional_package
10+
_, have_scipy, _ = optional_package('scipy.io')
911

1012
from nose.tools import assert_true
1113
from numpy.testing import assert_array_equal
@@ -42,17 +44,18 @@ def bz2_mio_error():
4244
This won't cause a problem for scipy releases after Jan 24 2014 because of
4345
commit 98ef522d99 (in scipy)
4446
"""
45-
try:
46-
import scipy.io
47-
except ImportError:
47+
if not have_scipy:
4848
return True
49+
import scipy.io
50+
4951
with InTemporaryDirectory():
5052
with Opener('test.mat.bz2', 'wb') as fobj:
5153
try:
5254
scipy.io.savemat(fobj, {'a': 1}, format='4')
5355
except ValueError:
5456
return True
55-
return False
57+
else:
58+
return False
5659

5760

5861
def assert_data_similar(arr, params):

nibabel/tests/test_image_load_save.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,8 @@
1616
import numpy as np
1717

1818
# If we don't have scipy, then we cannot write SPM format files
19-
try:
20-
import scipy.io
21-
except ImportError:
22-
have_scipy = False
23-
else:
24-
have_scipy = True
25-
19+
from ..optpkg import optional_package
20+
_, have_scipy, _ = optional_package('scipy')
2621

2722
from .. import analyze as ana
2823
from .. import spm99analyze as spm99

nibabel/tests/test_loadsave.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
from ..spatialimages import ImageFileError
1515
from ..tmpdirs import InTemporaryDirectory, TemporaryDirectory
1616

17-
from .test_spm99analyze import have_scipy
17+
from ..optpkg import optional_package
18+
_, have_scipy, _ = optional_package('scipy')
1819

1920
from numpy.testing import (assert_almost_equal,
2021
assert_array_equal)

nibabel/tests/test_spm99analyze.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,8 @@
1616

1717
# Decorator to skip tests requiring save / load if scipy not available for mat
1818
# files
19-
try:
20-
import scipy
21-
except ImportError:
22-
have_scipy = False
23-
else:
24-
have_scipy = True
19+
from ..optpkg import optional_package
20+
_, have_scipy, _ = optional_package('scipy')
2521
scipy_skip = dec.skipif(not have_scipy, 'scipy not available')
2622

2723
from ..spm99analyze import (Spm99AnalyzeHeader, Spm99AnalyzeImage,

0 commit comments

Comments
 (0)