Skip to content

Commit 0129ea5

Browse files
author
Ben Cipollini
committed
Fix for #324 - use consistent syntax for have_scipy.
1 parent ad95b4e commit 0129ea5

File tree

7 files changed

+43
-49
lines changed

7 files changed

+43
-49
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')[1]
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: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,20 @@
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')[1]
3233

33-
from scipy import ndimage as spnd
3434

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

5156

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)
57+
if __name__ == '__main__':
58+
np.set_printoptions(suppress=True, precision=4)
59+
normal_fname = "Phantom_EPI_3mm_tra_SENSE_6_1.PAR"
60+
normal_img = parrec.load(normal_fname)
61+
normal_data = normal_img.get_data()
62+
normal_normed = gmean_norm(normal_data)
5663

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

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))))
68+
for parfile in glob.glob("*.PAR"):
69+
if parfile == normal_fname:
70+
continue
71+
funny_img = parrec.load(parfile)
72+
fixed_img = resample_img2img(normal_img, funny_img)
73+
fixed_data = fixed_img.get_data()
74+
difference_data = normal_normed - gmean_norm(fixed_data)
75+
print('RMS resliced {:<52} : {}'.format(
76+
parfile,
77+
np.sqrt(np.sum(difference_data ** 2))))

nibabel/tests/test_helpers.py

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

45
import numpy as np
6+
from nose.tools import assert_true
7+
from numpy.testing import assert_array_equal
58

6-
from io import BytesIO
79
from ..openers import Opener
10+
from ..optpkg import optional_package
811
from ..tmpdirs import InTemporaryDirectory
9-
10-
from nose.tools import assert_true
11-
from numpy.testing import assert_array_equal
12+
have_scipy = optional_package('scipy')[1]
1213

1314

1415
def bytesio_filemap(klass):
@@ -42,13 +43,12 @@ def bz2_mio_error():
4243
This won't cause a problem for scipy releases after Jan 24 2014 because of
4344
commit 98ef522d99 (in scipy)
4445
"""
45-
try:
46-
import scipy.io
47-
except ImportError:
46+
if not have_scipy:
4847
return True
4948
with InTemporaryDirectory():
5049
with Opener('test.mat.bz2', 'wb') as fobj:
5150
try:
51+
import scipy.io
5252
scipy.io.savemat(fobj, {'a': 1}, format='4')
5353
except ValueError:
5454
return True

nibabel/tests/test_image_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
import numpy as np
2929

3030
from ..optpkg import optional_package
31-
_, have_scipy, _ = optional_package('scipy')
32-
_, have_h5py, _ = optional_package('h5py')
31+
have_scipy = optional_package('scipy')[1]
32+
have_h5py = optional_package('h5py')[1]
3333

3434
from .. import (AnalyzeImage, Spm99AnalyzeImage, Spm2AnalyzeImage,
3535
Nifti1Pair, Nifti1Image, Nifti2Pair, Nifti2Image,

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')[1]
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')[1]
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')[1]
2521
scipy_skip = dec.skipif(not have_scipy, 'scipy not available')
2622

2723
from ..spm99analyze import (Spm99AnalyzeHeader, Spm99AnalyzeImage,

0 commit comments

Comments
 (0)