Skip to content

Commit e81ac28

Browse files
author
Ben Cipollini
committed
sniff_size => sizeof_hdr
1 parent 8b55cc6 commit e81ac28

File tree

7 files changed

+25
-35
lines changed

7 files changed

+25
-35
lines changed

nibabel/analyze.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ class AnalyzeHeader(LabeledWrapStruct):
190190
has_data_intercept = False
191191

192192
sizeof_hdr = 348
193-
sniff_size = 348
194193

195194
def __init__(self,
196195
binaryblock=None,
@@ -895,11 +894,11 @@ def _chk_pixdims(hdr, fix=False):
895894

896895
@classmethod
897896
def is_header(klass, binaryblock):
898-
if len(binaryblock) < klass.sniff_size:
899-
raise ValueError('Must pass a binary block >= %d bytes' % klass.sniff_size)
897+
if len(binaryblock) < klass.sizeof_hdr:
898+
raise ValueError('Must pass a binary block >= %d bytes' % klass.sizeof_hdr)
900899

901900
hdr = np.ndarray(shape=(), dtype=header_dtype,
902-
buffer=binaryblock[:klass.sniff_size])
901+
buffer=binaryblock[:klass.sizeof_hdr])
903902
bs_hdr = hdr.byteswap()
904903
return 348 in (hdr['sizeof_hdr'], bs_hdr['sizeof_hdr'])
905904

nibabel/minc1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ class MincHeader(Header):
270270
"""
271271
# We don't use the data layout - this just in case we do later
272272
data_layout = 'C'
273-
sniff_size = 4
273+
sizeof_hdr = 4
274274

275275
def data_to_fileobj(self, data, fileobj, rescale=True):
276276
""" See Header class for an implementation we can't use """

nibabel/nifti1.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -558,9 +558,6 @@ class Nifti1Header(SpmAnalyzeHeader):
558558
pair_magic = b'ni1'
559559
single_magic = b'n+1'
560560

561-
# for sniffing type
562-
sniff_size = 348
563-
564561
# Quaternion threshold near 0, based on float32 precision
565562
quaternion_threshold = -np.finfo(np.float32).eps * 3
566563

@@ -1616,11 +1613,11 @@ def _chk_xform_code(klass, code_type, hdr, fix):
16161613

16171614
@classmethod
16181615
def is_header(klass, binaryblock):
1619-
if len(binaryblock) < klass.sniff_size:
1620-
raise ValueError('Must pass a binary block >= %d bytes' % klass.sniff_size)
1616+
if len(binaryblock) < klass.sizeof_hdr:
1617+
raise ValueError('Must pass a binary block >= %d bytes' % klass.sizeof_hdr)
16211618

16221619
hdr = np.ndarray(shape=(), dtype=header_dtype,
1623-
buffer=binaryblock[:klass.sniff_size])
1620+
buffer=binaryblock[:klass.sizeof_hdr])
16241621
return hdr['magic'] in (b'ni1', b'n+1')
16251622

16261623

nibabel/nifti2.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,6 @@ class Nifti2Header(Nifti1Header):
141141
# Size of header in sizeof_hdr field
142142
sizeof_hdr = 540
143143

144-
# sniff size to determine type
145-
sniff_size = 540
146-
147144
# Quaternion threshold near 0, based on float64 preicision
148145
quaternion_threshold = -np.finfo(np.float64).eps * 3
149146

@@ -226,11 +223,11 @@ def _chk_eol_check(hdr, fix=False):
226223

227224
@classmethod
228225
def is_header(klass, binaryblock):
229-
if len(binaryblock) < klass.sniff_size:
230-
raise ValueError('Must pass a binary block >= %d bytes' % klass.sniff_size)
226+
if len(binaryblock) < klass.sizeof_hdr:
227+
raise ValueError('Must pass a binary block >= %d bytes' % klass.sizeof_hdr)
231228

232229
hdr = np.ndarray(shape=(), dtype=header_dtype,
233-
buffer=binaryblock[:klass.sniff_size])
230+
buffer=binaryblock[:klass.sizeof_hdr])
234231
bs_hdr = hdr.byteswap()
235232
return 540 in (hdr['sizeof_hdr'], bs_hdr['sizeof_hdr'])
236233

nibabel/spatialimages.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -904,15 +904,15 @@ def is_image(klass, filename, sniff=None):
904904
break
905905

906906
try:
907-
klass_sniff_size = getattr(klass.header_class, 'sniff_size', 0)
907+
klass_sizeof_hdr = getattr(klass.header_class, 'sizeof_hdr', 0)
908908

909-
if not sniff or len(sniff) < klass_sniff_size:
909+
if not sniff or len(sniff) < klass_sizeof_hdr:
910910
# 1024 == large size, for efficiency (could iterate over imageclasses).
911-
sniff_size = np.max([1024, klass_sniff_size])
911+
sizeof_hdr = np.max([1024, klass_sizeof_hdr])
912912
with ImageOpener(metadata_filename, 'rb') as fobj:
913-
sniff = fobj.read(sniff_size)
913+
sniff = fobj.read(sizeof_hdr)
914914

915-
is_header = klass.header_class.is_header(sniff[:klass_sniff_size])
915+
is_header = klass.header_class.is_header(sniff[:klass_sizeof_hdr])
916916
except Exception as e:
917917
# Can happen if: file doesn't exist,
918918
# filesize < necessary sniff size (this happens!)

nibabel/spm2analyze.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ class Spm2AnalyzeHeader(spm99.Spm99AnalyzeHeader):
3535
# Copies of module level definitions
3636
template_dtype = header_dtype
3737

38-
# binary read size to determine type
39-
sniff_size = 348
40-
4138
def get_slope_inter(self):
4239
''' Get data scaling (slope) and intercept from header data
4340
@@ -118,11 +115,11 @@ def get_slope_inter(self):
118115

119116
@classmethod
120117
def is_header(klass, binaryblock):
121-
if len(binaryblock) < klass.sniff_size:
122-
raise ValueError('Must pass a binary block >= %d bytes' % klass.sniff_size)
118+
if len(binaryblock) < klass.sizeof_hdr:
119+
raise ValueError('Must pass a binary block >= %d bytes' % klass.sizeof_hdr)
123120

124121
hdr = np.ndarray(shape=(), dtype=header_dtype,
125-
buffer=binaryblock[:klass.sniff_size])
122+
buffer=binaryblock[:klass.sizeof_hdr])
126123
bs_hdr = hdr.byteswap()
127124
return (binaryblock[344:348] not in (b'ni1\x00', b'n+1\x00') and
128125
348 in (hdr['sizeof_hdr'], bs_hdr['sizeof_hdr']))

nibabel/tests/test_image_types.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ def check_img(img_path, img_klass, sniff_mode, sniff, expect_success, msg):
117117

118118
if expect_success:
119119
new_msg = '%s returned sniff==None (%s)' % (img_klass.__name__, msg)
120-
expected_sniff_size = getattr(img_klass.header_class, 'sniff_size', 0)
121-
current_sniff_size = len(new_sniff) if new_sniff is not None else 0
122-
assert_true(current_sniff_size >= expected_sniff_size, new_msg)
120+
expected_sizeof_hdr = getattr(img_klass.header_class, 'sizeof_hdr', 0)
121+
current_sizeof_hdr = len(new_sniff) if new_sniff is not None else 0
122+
assert_true(current_sizeof_hdr >= expected_sizeof_hdr, new_msg)
123123

124124
# Build a message to the user.
125125
new_msg = '%s (%s) image is%s a %s image.' % (
@@ -140,20 +140,20 @@ def check_img(img_path, img_klass, sniff_mode, sniff, expect_success, msg):
140140
else:
141141
return sniff
142142

143-
sniff_size = getattr(expected_img_klass.header_class, 'sniff_size', 0)
143+
sizeof_hdr = getattr(expected_img_klass.header_class, 'sizeof_hdr', 0)
144144

145145
for sniff_mode, sniff in dict(
146146
vanilla=None, # use the sniff of the previous item
147147
no_sniff=None, # Don't pass a sniff
148148
none=None, # pass None as the sniff, should query in fn
149149
empty='', # pass an empty sniff, should query in fn
150-
irrelevant='a' * (sniff_size - 1), # A too-small sniff, query
151-
bad_sniff='a' * sniff_size).items(): # Bad sniff, should fail.
150+
irrelevant='a' * (sizeof_hdr - 1), # A too-small sniff, query
151+
bad_sniff='a' * sizeof_hdr).items(): # Bad sniff, should fail.
152152

153153
for klass in img_klasses:
154154
if klass == expected_img_klass:
155155
expect_success = (sniff_mode not in ['bad_sniff'] or
156-
sniff_size == 0 or
156+
sizeof_hdr == 0 or
157157
klass == Minc1Image) # special case...
158158
elif (issubclass(klass, expected_img_klass) or
159159
issubclass(expected_img_klass, klass)):

0 commit comments

Comments
 (0)