Skip to content

Commit 182b4f9

Browse files
committed
ENH: Validate units parameter in all get/set_zooms
1 parent 55a07f4 commit 182b4f9

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
@@ -693,6 +693,8 @@ def get_zooms(self, units='norm', raise_unknown=False):
693693
>>> hdr.get_zooms()
694694
(3.0, 4.0)
695695
'''
696+
if units not in ('norm', 'raw'):
697+
raise ValueError("`units` parameter must be 'norm' or 'raw'")
696698
hdr = self._structarr
697699
dims = hdr['dim']
698700
ndim = dims[0]
@@ -715,6 +717,8 @@ def set_zooms(self, zooms, units='norm'):
715717
spatial/temporal or as raw values to be interpreted according to
716718
format specification.
717719
'''
720+
if units not in ('norm', 'raw'):
721+
raise ValueError("`units` parameter must be 'norm' or 'raw'")
718722
hdr = self._structarr
719723
dims = hdr['dim']
720724
ndim = dims[0]

nibabel/ecat.py

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

584584
def get_zooms(self, frame=0, units='norm', raise_unknown=False):
585585
"""returns zooms ...pixdims"""
586+
if units not in ('norm', 'raw'):
587+
raise ValueError("`units` parameter must be 'norm' or 'raw'")
586588
subhdr = self.subheaders[frame]
587589
x_zoom = subhdr['x_pixel_size'] * 10
588590
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
@@ -302,7 +302,11 @@ def set_zooms(self, zooms, units='norm'):
302302
Zooms are specified in normalized units of mm/sec for
303303
spatial/temporal dimensions or as raw values to be stored in
304304
header.
305+
306+
.. _mghformat: https://surfer.nmr.mgh.harvard.edu/fswiki/FsTutorial/MghFormat#line-82
305307
'''
308+
if units not in ('norm', 'raw'):
309+
raise ValueError("`units` parameter must be 'norm' or 'raw'")
306310
hdr = self._structarr
307311
zooms = np.asarray(zooms)
308312
ndims = self._ndims()
@@ -317,12 +321,7 @@ def set_zooms(self, zooms, units='norm'):
317321
if zooms[3] < 0:
318322
raise HeaderDataError('TR must be non-negative; got {!r}'
319323
''.format(zooms[3]))
320-
if units == 'norm':
321-
tfactor = 1000
322-
elif units == 'raw':
323-
tfactor = 1
324-
else:
325-
raise ValueError("`units` parameter must be 'norm' or 'raw'")
324+
tfactor = 1000 if units == 'norm' else 1
326325
hdr['tr'] = zooms[3] * tfactor
327326

328327
def get_data_shape(self):

nibabel/minc1.py

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

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

nibabel/nifti1.py

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

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

17741777
if isinstance(units, tuple):
@@ -1781,9 +1784,6 @@ def set_zooms(self, zooms, units=None):
17811784
elif len(zooms) > 3 and t_code in ('unknown', 'sec', 'msec', 'usec'):
17821785
t_code = 'sec'
17831786
self.set_xyzt_units(xyz_code, t_code)
1784-
elif units != 'raw':
1785-
raise ValueError("`units` parameter must be 'norm', 'raw',"
1786-
" or a tuple of unit codes (see set_xyzt_units)")
17871787

17881788
def _clean_after_mapping(self):
17891789
''' 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
@@ -246,6 +246,8 @@ def get_zooms(self, units='norm', raise_unknown=False):
246246
zooms : tuple
247247
Spacing between voxels along each axis
248248
'''
249+
if units not in ('norm', 'raw'):
250+
raise ValueError("`units` parameter must be 'norm' or 'raw'")
249251
return self._zooms
250252

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

0 commit comments

Comments
 (0)