Skip to content

Commit 531d0e5

Browse files
committed
ENH: Validate units parameter in all get/set_zooms
1 parent 3849178 commit 531d0e5

File tree

6 files changed

+20
-9
lines changed

6 files changed

+20
-9
lines changed

nibabel/analyze.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,8 @@ def get_zooms(self, units='norm', raise_unknown=False):
694694
>>> hdr.get_zooms()
695695
(3.0, 4.0)
696696
'''
697+
if units not in ('norm', 'raw'):
698+
raise ValueError("`units` parameter must be 'norm' or 'raw'")
697699
hdr = self._structarr
698700
dims = hdr['dim']
699701
ndim = dims[0]
@@ -716,6 +718,8 @@ def set_zooms(self, zooms, units='norm'):
716718
spatial/temporal or as raw values to be interpreted according to
717719
format specification.
718720
'''
721+
if units not in ('norm', 'raw'):
722+
raise ValueError("`units` parameter must be 'norm' or 'raw'")
719723
hdr = self._structarr
720724
dims = hdr['dim']
721725
ndim = dims[0]

nibabel/ecat.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,8 @@ def get_frame_affine(self, frame=0):
584584

585585
def get_zooms(self, frame=0, units='norm', raise_unknown=False):
586586
"""returns zooms ...pixdims"""
587+
if units not in ('norm', 'raw'):
588+
raise ValueError("`units` parameter must be 'norm' or 'raw'")
587589
subhdr = self.subheaders[frame]
588590
x_zoom = subhdr['x_pixel_size'] * 10
589591
y_zoom = subhdr['y_pixel_size'] * 10

nibabel/freesurfer/mghformat.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,11 @@ def set_zooms(self, zooms, units='norm'):
301301
Zooms are specified in normalized units of mm/sec for
302302
spatial/temporal dimensions or as raw values to be stored in
303303
header.
304+
305+
.. _mghformat: https://surfer.nmr.mgh.harvard.edu/fswiki/FsTutorial/MghFormat#line-82
304306
'''
307+
if units not in ('norm', 'raw'):
308+
raise ValueError("`units` parameter must be 'norm' or 'raw'")
305309
hdr = self._structarr
306310
zooms = np.asarray(zooms)
307311
ndims = self._ndims()
@@ -316,12 +320,7 @@ def set_zooms(self, zooms, units='norm'):
316320
if zooms[3] < 0:
317321
raise HeaderDataError('TR must be non-negative; got {!r}'
318322
''.format(zooms[3]))
319-
if units == 'norm':
320-
tfactor = 1000
321-
elif units == 'raw':
322-
tfactor = 1
323-
else:
324-
raise ValueError("`units` parameter must be 'norm' or 'raw'")
323+
tfactor = 1000 if units == 'norm' else 1
325324
hdr['tr'] = zooms[3] * tfactor
326325

327326
def get_data_shape(self):

nibabel/minc1.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ def get_data_shape(self):
9595

9696
def get_zooms(self, units='norm', raise_unknown=False):
9797
""" Get real-world sizes of voxels """
98+
if units not in ('norm', 'raw'):
99+
raise ValueError("`units` parameter must be 'norm' or 'raw'")
98100
# zooms must be positive; but steps in MINC can be negative
99101
return tuple([abs(float(dim.step)) if hasattr(dim, 'step') else 1.0
100102
for dim in self._dims])

nibabel/nifti1.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,6 +1771,9 @@ def set_zooms(self, zooms, units=None):
17711771
''.format(self.__class__.__name__),
17721772
FutureWarning, stacklevel=2)
17731773

1774+
if units not in ('norm', 'raw') and not isinstance(units, tuple):
1775+
raise ValueError("`units` parameter must be 'norm', 'raw',"
1776+
" or a tuple of unit codes (see set_xyzt_units)")
17741777
super(Nifti1Header, self).set_zooms(zooms, units=units)
17751778

17761779
if isinstance(units, tuple):
@@ -1783,9 +1786,6 @@ def set_zooms(self, zooms, units=None):
17831786
elif len(zooms) > 3 and t_code in ('unknown', 'sec', 'msec', 'usec'):
17841787
t_code = 'sec'
17851788
self.set_xyzt_units(xyz_code, t_code)
1786-
elif units != 'raw':
1787-
raise ValueError("`units` parameter must be 'norm', 'raw',"
1788-
" or a tuple of unit codes (see set_xyzt_units)")
17891789

17901790
def _clean_after_mapping(self):
17911791
''' Set format-specific stuff after converting header from mapping

nibabel/spatialimages.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ def get_zooms(self, units='norm', raise_unknown=False):
245245
zooms : tuple
246246
Spacing between voxels along each axis
247247
'''
248+
if units not in ('norm', 'raw'):
249+
raise ValueError("`units` parameter must be 'norm' or 'raw'")
248250
return self._zooms
249251

250252
def set_zooms(self, zooms, units='norm'):
@@ -259,6 +261,8 @@ def set_zooms(self, zooms, units='norm'):
259261
spatial/temporal or as raw values to be interpreted according to
260262
format specification.
261263
'''
264+
if units not in ('norm', 'raw'):
265+
raise ValueError("`units` parameter must be 'norm' or 'raw'")
262266
zooms = tuple(float(z) for z in zooms)
263267
shape = self.get_data_shape()
264268
ndim = len(shape)

0 commit comments

Comments
 (0)