Skip to content

Commit 1971efb

Browse files
committed
RF: Rename is_header to may_contain_header
Rename hdr variables to hdr_struct to reduce confusion Rename is_image to path_maybe_image
1 parent afc9c25 commit 1971efb

File tree

9 files changed

+35
-35
lines changed

9 files changed

+35
-35
lines changed

nibabel/analyze.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -893,15 +893,15 @@ def _chk_pixdims(hdr, fix=False):
893893
return hdr, rep
894894

895895
@classmethod
896-
def is_header(klass, binaryblock):
896+
def may_contain_header(klass, binaryblock):
897897
if len(binaryblock) < klass.sizeof_hdr:
898898
raise ValueError('Must pass a binary block >= %d bytes' %
899899
klass.sizeof_hdr)
900900

901-
hdr = np.ndarray(shape=(), dtype=header_dtype,
902-
buffer=binaryblock[:klass.sizeof_hdr])
903-
bs_hdr = hdr.byteswap()
904-
return 348 in (hdr['sizeof_hdr'], bs_hdr['sizeof_hdr'])
901+
hdr_struct = np.ndarray(shape=(), dtype=header_dtype,
902+
buffer=binaryblock[:klass.sizeof_hdr])
903+
bs_hdr_struct = hdr_struct.byteswap()
904+
return 348 in (hdr_struct['sizeof_hdr'], bs_hdr_struct['sizeof_hdr'])
905905

906906

907907
class AnalyzeImage(SpatialImage):

nibabel/loadsave.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def load(filename, **kwargs):
3636

3737
sniff = None
3838
for image_klass in all_image_classes:
39-
is_valid, sniff = image_klass.is_image(filename, sniff)
39+
is_valid, sniff = image_klass.path_maybe_image(filename, sniff)
4040
if is_valid:
4141
return image_klass.from_filename(filename, **kwargs)
4242

nibabel/minc1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ def data_from_fileobj(self, fileobj):
282282

283283
class Minc1Header(MincHeader):
284284
@classmethod
285-
def is_header(klass, binaryblock):
285+
def may_contain_header(klass, binaryblock):
286286
if len(binaryblock) < klass.sizeof_hdr:
287287
raise ValueError('Must pass a binary block >= %d bytes' %
288288
klass.sizeof_hdr)

nibabel/minc2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def get_scaled_data(self, sliceobj=()):
136136

137137
class Minc2Header(MincHeader):
138138
@classmethod
139-
def is_header(klass, binaryblock):
139+
def may_contain_header(klass, binaryblock):
140140
if len(binaryblock) < klass.sizeof_hdr:
141141
raise ValueError('Must pass a binary block >= %d bytes' %
142142
klass.sizeof_hdr)

nibabel/nifti1.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,14 +1612,14 @@ def _chk_xform_code(klass, code_type, hdr, fix):
16121612
return hdr, rep
16131613

16141614
@classmethod
1615-
def is_header(klass, binaryblock):
1615+
def may_contain_header(klass, binaryblock):
16161616
if len(binaryblock) < klass.sizeof_hdr:
16171617
raise ValueError('Must pass a binary block >= %d bytes' %
16181618
klass.sizeof_hdr)
16191619

1620-
hdr = np.ndarray(shape=(), dtype=header_dtype,
1621-
buffer=binaryblock[:klass.sizeof_hdr])
1622-
return hdr['magic'] in (b'ni1', b'n+1')
1620+
hdr_struct = np.ndarray(shape=(), dtype=header_dtype,
1621+
buffer=binaryblock[:klass.sizeof_hdr])
1622+
return hdr_struct['magic'] in (b'ni1', b'n+1')
16231623

16241624

16251625
class Nifti1PairHeader(Nifti1Header):

nibabel/nifti2.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,15 +222,15 @@ def _chk_eol_check(hdr, fix=False):
222222
return hdr, rep
223223

224224
@classmethod
225-
def is_header(klass, binaryblock):
225+
def may_contain_header(klass, binaryblock):
226226
if len(binaryblock) < klass.sizeof_hdr:
227227
raise ValueError('Must pass a binary block >= %d bytes' %
228228
klass.sizeof_hdr)
229229

230-
hdr = np.ndarray(shape=(), dtype=header_dtype,
231-
buffer=binaryblock[:klass.sizeof_hdr])
232-
bs_hdr = hdr.byteswap()
233-
return 540 in (hdr['sizeof_hdr'], bs_hdr['sizeof_hdr'])
230+
hdr_struct = np.ndarray(shape=(), dtype=header_dtype,
231+
buffer=binaryblock[:klass.sizeof_hdr])
232+
bs_hdr_struct = hdr_struct.byteswap()
233+
return 540 in (hdr_struct['sizeof_hdr'], bs_hdr_struct['sizeof_hdr'])
234234

235235

236236
class Nifti2PairHeader(Nifti2Header):

nibabel/spatialimages.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -877,13 +877,13 @@ def is_valid_extension(klass, ext):
877877
return np.any([ft[1] == ext.lower() for ft in klass.files_types])
878878

879879
@classmethod
880-
def is_image(klass, filename, sniff=None):
880+
def path_maybe_image(klass, filename, sniff=None):
881881
froot, ext, trailing = splitext_addext(filename,
882882
klass._compressed_exts)
883883

884884
if not klass.is_valid_extension(ext):
885885
return False, sniff
886-
elif not hasattr(klass.header_class, 'is_header'):
886+
elif not hasattr(klass.header_class, 'may_contain_header'):
887887
return True, sniff
888888

889889
# Determine the metadata location, then sniff it
@@ -909,14 +909,14 @@ def is_image(klass, filename, sniff=None):
909909
with ImageOpener(metadata_filename, 'rb') as fobj:
910910
sniff = fobj.read(sizeof_hdr)
911911

912-
is_header = klass.header_class.is_header(sniff)
912+
may_contain_header = klass.header_class.may_contain_header(sniff)
913913
except Exception:
914914
# Can happen if: file doesn't exist,
915915
# filesize < necessary sniff size (this happens!)
916916
# other unexpected errors.
917-
is_header = False
917+
may_contain_header = False
918918

919-
return is_header, sniff
919+
return may_contain_header, sniff
920920

921921
def __getitem__(self):
922922
''' No slicing or dictionary interface for images

nibabel/spm2analyze.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,16 @@ def get_slope_inter(self):
114114
return None, None
115115

116116
@classmethod
117-
def is_header(klass, binaryblock):
117+
def may_contain_header(klass, binaryblock):
118118
if len(binaryblock) < klass.sizeof_hdr:
119119
raise ValueError('Must pass a binary block >= %d bytes' %
120120
klass.sizeof_hdr)
121121

122-
hdr = np.ndarray(shape=(), dtype=header_dtype,
123-
buffer=binaryblock[:klass.sizeof_hdr])
124-
bs_hdr = hdr.byteswap()
122+
hdr_struct = np.ndarray(shape=(), dtype=header_dtype,
123+
buffer=binaryblock[:klass.sizeof_hdr])
124+
bs_hdr_struct = hdr_struct.byteswap()
125125
return (binaryblock[344:348] not in (b'ni1\x00', b'n+1\x00') and
126-
348 in (hdr['sizeof_hdr'], bs_hdr['sizeof_hdr']))
126+
348 in (hdr_struct['sizeof_hdr'], bs_hdr_struct['sizeof_hdr']))
127127

128128

129129
class Spm2AnalyzeImage(spm99.Spm99AnalyzeImage):

nibabel/tests/test_image_types.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# copyright and license terms.
77
#
88
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
9-
''' Tests for is_image / is_header functions '''
9+
''' Tests for is_image / may_contain_header functions '''
1010
from __future__ import division, print_function, absolute_import
1111

1212
import copy
@@ -21,7 +21,7 @@
2121
Spm2AnalyzeImage, Spm99AnalyzeImage,
2222
MGHImage, all_image_classes)
2323

24-
from nose.tools import assert_true, assert_equal, assert_raises
24+
from nose.tools import assert_true, assert_raises
2525

2626
DATA_PATH = pjoin(dirname(__file__), 'data')
2727

@@ -34,7 +34,7 @@ def wat(hdr):
3434
AnalyzeHeader]
3535
for klass in all_analyze_header_klasses:
3636
try:
37-
if klass.is_header(hdr.binaryblock):
37+
if klass.may_contain_header(hdr.binaryblock):
3838
return klass
3939
else:
4040
print('checked completed, but failed.')
@@ -106,16 +106,16 @@ def check_img(img_path, img_klass, sniff_mode, sniff, expect_success,
106106
"""Embedded function to do the actual checks expected."""
107107

108108
if sniff_mode == 'empty' and \
109-
hasattr(img_klass.header_class, 'is_header'):
110-
assert_raises(ValueError, img_klass.header_class.is_header,
111-
sniff)
109+
hasattr(img_klass.header_class, 'may_contain_header'):
110+
assert_raises(ValueError,
111+
img_klass.header_class.may_contain_header, sniff)
112112

113113
if sniff_mode == 'no_sniff':
114114
# Don't pass any sniff--not even "None"
115-
is_img, new_sniff = img_klass.is_image(img_path)
115+
is_img, new_sniff = img_klass.path_maybe_image(img_path)
116116
else:
117117
# Pass a sniff, but don't reuse across images.
118-
is_img, new_sniff = img_klass.is_image(img_path, sniff)
118+
is_img, new_sniff = img_klass.path_maybe_image(img_path, sniff)
119119

120120
if expect_success:
121121
# Check that the sniff returned is appropriate.

0 commit comments

Comments
 (0)