Skip to content

Commit 8a3ed16

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

File tree

12 files changed

+34
-45
lines changed

12 files changed

+34
-45
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
@@ -131,8 +131,8 @@ def types_filenames(template_fname, types_exts,
131131
elif found_ext == found_ext.lower():
132132
proc_ext = lambda s: s.lower()
133133
for name, ext in types_exts:
134-
if name in tfns: # priority to those found first.
135-
continue
134+
if name in tfns: # Allow multipe definitions of image, header, etc,
135+
continue # giving priority to those found first.
136136
if name == direct_set_name:
137137
tfns[name] = template_fname
138138
continue

nibabel/freesurfer/mghformat.py

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

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
@@ -300,10 +300,9 @@ class Minc1Image(SpatialImage):
300300
header_class = Minc1Header
301301
files_types = (('image', '.mnc'),)
302302
_compressed_exts = ('.gz', '.bz2')
303-
has_affine = True
303+
304304
makeable = True
305305
rw = False
306-
nickname = 'minc'
307306

308307
ImageArrayProxy = MincImageArrayProxy
309308

nibabel/nifti1.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,7 +1634,6 @@ class Nifti1Pair(analyze.AnalyzeImage):
16341634
""" Class for NIfTI1 format image, header pair
16351635
"""
16361636
header_class = Nifti1PairHeader
1637-
nickname = 'nifti_pair'
16381637
rw = True
16391638

16401639
def __init__(self, dataobj, affine, header=None,
@@ -1860,7 +1859,6 @@ class Nifti1Image(Nifti1Pair):
18601859
"""
18611860
header_class = Nifti1Header
18621861
files_types = (('image', '.nii'),)
1863-
nickname = 'nifti_single'
18641862

18651863
@staticmethod
18661864
def _get_fileholders(file_map):

nibabel/parrec.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,8 +1020,7 @@ class PARRECImage(SpatialImage):
10201020
"""PAR/REC image"""
10211021
header_class = PARRECHeader
10221022
files_types = (('image', '.rec'), ('header', '.par'))
1023-
nickname = 'par'
1024-
has_affine = True
1023+
10251024
makeable = False
10261025
rw = False
10271026

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):
@@ -878,15 +871,23 @@ def from_image(klass, img):
878871
klass.header_class.from_header(img.header),
879872
extra=img.extra.copy())
880873

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

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

892893
# Determine the metadata location, then sniff it

nibabel/spm2analyze.py

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

137136
load = Spm2AnalyzeImage.load
138137
save = Spm2AnalyzeImage.instance_to_filename

nibabel/spm99analyze.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ class Spm99AnalyzeImage(analyze.AnalyzeImage):
242242
has_affine = True
243243
makeable = True
244244
rw = have_scipy
245-
nickname = 'spm99analyze'
246245

247246
@classmethod
248247
@kw_only_meth(1)

0 commit comments

Comments
 (0)