Skip to content

Commit 6cd83f7

Browse files
committed
ENH: Validate units parameter in all get/set_zooms
1 parent 4babf08 commit 6cd83f7

File tree

6 files changed

+19
-3
lines changed

6 files changed

+19
-3
lines changed

nibabel/analyze.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,8 @@ def get_zooms(self, units='norm', raise_unknown=False):
690690
>>> hdr.get_zooms()
691691
(3.0, 4.0)
692692
"""
693+
if units not in ('norm', 'raw'):
694+
raise ValueError("`units` parameter must be 'norm' or 'raw'")
693695
hdr = self._structarr
694696
dims = hdr['dim']
695697
ndim = dims[0]
@@ -712,6 +714,8 @@ def set_zooms(self, zooms, units='norm'):
712714
spatial/temporal or as raw values to be interpreted according to
713715
format specification.
714716
"""
717+
if units not in ('norm', 'raw'):
718+
raise ValueError("`units` parameter must be 'norm' or 'raw'")
715719
hdr = self._structarr
716720
dims = hdr['dim']
717721
ndim = dims[0]

nibabel/ecat.py

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

581581
def get_zooms(self, frame=0, units='norm', raise_unknown=False):
582582
"""returns zooms ...pixdims"""
583+
if units not in ('norm', 'raw'):
584+
raise ValueError("`units` parameter must be 'norm' or 'raw'")
583585
subhdr = self.subheaders[frame]
584586
x_zoom = subhdr['x_pixel_size'] * 10
585587
y_zoom = subhdr['y_pixel_size'] * 10

nibabel/freesurfer/mghformat.py

Lines changed: 4 additions & 0 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()

nibabel/minc1.py

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

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

nibabel/nifti1.py

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

1813+
if units not in ('norm', 'raw') and not isinstance(units, tuple):
1814+
raise ValueError("`units` parameter must be 'norm', 'raw',"
1815+
" or a tuple of unit codes (see set_xyzt_units)")
18131816
super(Nifti1Header, self).set_zooms(zooms, units=units)
18141817

18151818
if isinstance(units, tuple):
@@ -1822,9 +1825,6 @@ def set_zooms(self, zooms, units=None):
18221825
elif len(zooms) > 3 and t_code in ('unknown', 'sec', 'msec', 'usec'):
18231826
t_code = 'sec'
18241827
self.set_xyzt_units(xyz_code, t_code)
1825-
elif units != 'raw':
1826-
raise ValueError("`units` parameter must be 'norm', 'raw',"
1827-
" or a tuple of unit codes (see set_xyzt_units)")
18281828

18291829
def _clean_after_mapping(self):
18301830
""" 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
@@ -241,6 +241,8 @@ def get_zooms(self, units='norm', raise_unknown=False):
241241
zooms : tuple
242242
Spacing between voxels along each axis
243243
'''
244+
if units not in ('norm', 'raw'):
245+
raise ValueError("`units` parameter must be 'norm' or 'raw'")
244246
return self._zooms
245247

246248
def set_zooms(self, zooms, units='norm'):
@@ -255,6 +257,8 @@ def set_zooms(self, zooms, units='norm'):
255257
spatial/temporal or as raw values to be interpreted according to
256258
format specification.
257259
'''
260+
if units not in ('norm', 'raw'):
261+
raise ValueError("`units` parameter must be 'norm' or 'raw'")
258262
zooms = tuple(float(z) for z in zooms)
259263
shape = self.get_data_shape()
260264
ndim = len(shape)

0 commit comments

Comments
 (0)