Skip to content

Commit a4d2252

Browse files
author
Ben Cipollini
committed
Code cleanup after self code review, fix for Python 3 'filter' issue.
1 parent a395d68 commit a4d2252

12 files changed

+35
-46
lines changed

nibabel/analyze.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ def _chk_datatype(klass, hdr, fix=False):
829829
dtype = klass._data_type_codes.dtype[code]
830830
except KeyError:
831831
rep.problem_level = 40
832-
rep.problem_msg = 'data code %d not recognized by %s' % (code, klass.__name__)
832+
rep.problem_msg = 'data code %d not recognized' % code
833833
else:
834834
if dtype.itemsize == 0:
835835
rep.problem_level = 40
@@ -899,10 +899,9 @@ class AnalyzeImage(SpatialImage):
899899
header_class = AnalyzeHeader
900900
files_types = (('image','.img'), ('header','.hdr'))
901901
_compressed_exts = ('.gz', '.bz2')
902-
has_affine = False
902+
903903
makeable = True
904904
rw = True
905-
nickname = 'analyze'
906905

907906
ImageArrayProxy = ArrayProxy
908907

nibabel/filename_parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ def types_filenames(template_fname, types_exts,
133133
elif found_ext == found_ext.lower():
134134
proc_ext = lambda s : s.lower()
135135
for name, ext in types_exts:
136-
if name in tfns: # priority to those found first.
137-
continue
136+
if name in tfns: # Allow multipe definitions of image, header, etc,
137+
continue # giving priority to those found first.
138138
if name == direct_set_name:
139139
tfns[name] = template_fname
140140
continue

nibabel/freesurfer/mghformat.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,7 @@ class MGHImage(SpatialImage):
461461
files_types = (('image', '.mgh'),
462462
('image', '.mgz'))
463463
_compressed_exts = (('.gz',))
464-
nickname = 'mgh'
465-
has_affine = True
464+
466465
makeable = True
467466
rw = True
468467

nibabel/loadsave.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,22 @@ def save(img, filename):
7272
lext = ext.lower()
7373

7474
# Special-case Nifti singles and Pairs
75-
from .nifti1 import Nifti1Image, Nifti1Pair # Inline imports, as this file
75+
from .nifti1 import Nifti1Image, Nifti1Pair # Inline imports, as this module
7676
from .nifti2 import Nifti2Image, Nifti2Pair # really shouldn't reference any image type
77-
if type(img) == Nifti1Image and ext in ('.img', '.hdr'):
77+
if type(img) == Nifti1Image and lext in ('.img', '.hdr'):
7878
klass = Nifti1Pair
79-
elif type(img) == Nifti2Image and ext in ('.img', '.hdr'):
79+
elif type(img) == Nifti2Image and lext in ('.img', '.hdr'):
8080
klass = Nifti2Pair
81-
elif type(img) == Nifti1Pair and ext == '.nii':
81+
elif type(img) == Nifti1Pair and lext == '.nii':
8282
klass = Nifti1Image
83-
elif type(img) == Nifti2Pair and ext == '.nii':
83+
elif type(img) == Nifti2Pair and lext == '.nii':
8484
klass = Nifti2Image
8585
else: # arbitrary conversion
86-
valid_klasses = filter(lambda klass: klass.is_valid_extension(lext),
86+
valid_klasses = filter(lambda klass: klass.is_valid_extension(ext),
8787
all_image_classes)
88-
if len(valid_klasses) > 0:
89-
klass = valid_klasses[0]
90-
else:
88+
try:
89+
klass = next(iter(valid_klasses))
90+
except StopIteration: # if iterator is empty
9191
raise ImageFileError('Cannot work out file type of "%s"' %
9292
filename)
9393
converted = klass.from_image(img)

nibabel/minc1.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,9 @@ class Minc1Image(SpatialImage):
301301
header_class = Minc1Header
302302
files_types = (('image', '.mnc'),)
303303
_compressed_exts = ('.gz', '.bz2')
304-
has_affine = True
304+
305305
makeable = True
306306
rw = False
307-
nickname = 'minc'
308307

309308
ImageArrayProxy = MincImageArrayProxy
310309

nibabel/nifti1.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,7 +1615,6 @@ class Nifti1Pair(analyze.AnalyzeImage):
16151615
""" Class for NIfTI1 format image, header pair
16161616
"""
16171617
header_class = Nifti1PairHeader
1618-
nickname = 'nifti_pair'
16191618
rw = True
16201619

16211620
def __init__(self, dataobj, affine, header=None,
@@ -1841,7 +1840,6 @@ class Nifti1Image(Nifti1Pair):
18411840
"""
18421841
header_class = Nifti1Header
18431842
files_types = (('image', '.nii'),)
1844-
nickname = 'nifti_single'
18451843

18461844
@staticmethod
18471845
def _get_fileholders(file_map):

nibabel/parrec.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,8 +1009,7 @@ class PARRECImage(SpatialImage):
10091009
"""PAR/REC image"""
10101010
header_class = PARRECHeader
10111011
files_types = (('image', '.rec'), ('header', '.par'))
1012-
nickname = 'par'
1013-
has_affine = True
1012+
10141013
makeable = False
10151014
rw = False
10161015

nibabel/spatialimages.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -326,15 +326,8 @@ class SpatialImage(object):
326326
files_types = (('image', None),)
327327
_compressed_exts = ()
328328

329-
@classmethod
330-
def is_valid_extension(klass, lext):
331-
return np.any([ft[1] == lext for ft in klass.files_types])
332-
333-
@classmethod
334-
def is_valid_filename(klass, filename):
335-
froot, ext, trailing = splitext_addext(filename, klass._compressed_exts)
336-
lext = ext.lower()
337-
return klass.is_valid_extension(lext)
329+
makeable = True # Used in test code
330+
rw = True # Used in test code
338331

339332
def __init__(self, dataobj, affine, header=None,
340333
extra=None, file_map=None):
@@ -879,15 +872,23 @@ def from_image(klass, img):
879872
klass.header_class.from_header(img.header),
880873
extra=img.extra.copy())
881874

875+
@classmethod
876+
def is_valid_extension(klass, ext):
877+
return np.any([ft[1] == ext.lower() for ft in klass.files_types])
878+
879+
@classmethod
880+
def is_valid_filename(klass, filename):
881+
froot, ext, trailing = splitext_addext(filename, klass._compressed_exts)
882+
return klass.is_valid_extension(ext)
883+
882884
@classmethod
883885
def is_image(klass, filename, sniff=None):
884886
froot, ext, trailing = splitext_addext(filename, klass._compressed_exts)
885-
lext = ext.lower()
886887

887-
if not klass.is_valid_extension(lext):
888+
if not klass.is_valid_extension(ext):
888889
return False, sniff
889-
elif (not hasattr(klass.header_class, 'sniff_size') or
890-
not hasattr(klass.header_class, 'is_header')):
890+
elif (getattr(klass.header_class, 'sniff_size', None) is None or
891+
getattr(klass.header_class, 'is_header', None) is None):
891892
return True, sniff
892893

893894
# Determine the metadata location, then sniff it

nibabel/spm2analyze.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ class Spm2AnalyzeImage(spm99.Spm99AnalyzeImage):
133133
""" Class for SPM2 variant of basic Analyze image
134134
"""
135135
header_class = Spm2AnalyzeHeader
136-
nickname = 'spm2analyze'
137136

138137
load = Spm2AnalyzeImage.load
139138
save = Spm2AnalyzeImage.instance_to_filename

nibabel/spm99analyze.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,9 @@ class Spm99AnalyzeImage(analyze.AnalyzeImage):
239239
files_types = (('image', '.img'),
240240
('header', '.hdr'),
241241
('mat','.mat'))
242-
has_affine = True
242+
243243
makeable = True
244244
rw = have_scipy
245-
nickname = 'spm99analyze'
246245

247246
@classmethod
248247
@kw_only_meth(1)

nibabel/tests/test_analyze.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from ..arraywriters import WriterError
3333

3434
from nose.tools import (assert_equal, assert_not_equal, assert_true,
35-
assert_false, assert_raises, assert_in)
35+
assert_false, assert_raises)
3636

3737
from numpy.testing import (assert_array_equal, assert_array_almost_equal)
3838

@@ -157,8 +157,8 @@ def test_log_checks(self):
157157
hdr['datatype'] = -1 # severity 40
158158
with suppress_warnings():
159159
fhdr, message, raiser = self.log_chk(hdr, 40)
160-
assert_in('data code -1 not recognized', message)
161-
assert_in('not attempting fix', message)
160+
assert_equal(message, 'data code -1 not recognized; '
161+
'not attempting fix')
162162

163163
assert_raises(*raiser)
164164
# datatype not supported

nibabel/tests/test_filehandles.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def test_multiload():
3434
if N > 5000:
3535
warn('It would take too long to test file handles, aborting')
3636
return
37-
arr = np.arange(24).reshape((2, 3, 4))
37+
arr = np.arange(24).reshape((2,3,4))
3838
img = Nifti1Image(arr, np.eye(4))
3939
imgs = []
4040
try:
@@ -43,10 +43,6 @@ def test_multiload():
4343
save(img, fname)
4444
for i in range(N):
4545
imgs.append(load(fname))
46-
except Exception as e:
47-
if 'i' in locals():
48-
e.message += ' (i == %d)' % i
49-
raise Exception(e.message)
5046
finally:
5147
del img, imgs
5248
shutil.rmtree(tmpdir)

0 commit comments

Comments
 (0)