Skip to content

Commit 4c27592

Browse files
committed
FIX: Correct and simplify NIFTI logic
1 parent 531d0e5 commit 4c27592

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

nibabel/nifti1.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,22 +1687,22 @@ def get_zooms(self, units=None, raise_unknown=False):
16871687
raise ValueError("`units` parameter must be 'norm' or 'raw'")
16881688

16891689
xyz_zooms = raw_zooms[:3]
1690-
t_zoom = raw_zooms[3] if len(raw_zooms) > 3 else None
1690+
t_zoom = raw_zooms[3:4] # Tuple of length 0 or 1
16911691

16921692
xyz_code, t_code = self.get_xyzt_units()
16931693
xyz_msg = t_msg = ''
16941694
if xyz_code == 'unknown':
16951695
xyz_msg = 'Unknown spatial units'
16961696
xyz_code = 'mm'
1697-
if t_zoom is not None:
1697+
if t_zoom:
16981698
if t_code == 'unknown':
16991699
t_msg = 'Unknown time units'
17001700
t_code = 'sec'
17011701
elif t_code in ('hz', 'ppm', 'rads'):
17021702
t_msg = 'Unconvertible temporal units: {}'.format(t_code)
17031703

1704-
if raise_unknown and (xyz_msg, t_msg) != ('', ''):
1705-
if xyz_msg and t_msg:
1704+
if raise_unknown and (xyz_msg or t_msg.startswith('Unknown')):
1705+
if xyz_msg and t_msg.startswith('Unknown'):
17061706
msg = 'Unknown spatial and time units'
17071707
else:
17081708
msg = xyz_msg or t_msg
@@ -1718,12 +1718,10 @@ def get_zooms(self, units=None, raise_unknown=False):
17181718
xyz_factor = {'meter': 1000, 'mm': 1, 'micron': 0.001}[xyz_code]
17191719
xyz_zooms = tuple(np.array(xyz_zooms) * xyz_factor)
17201720

1721-
if t_zoom is not None:
1721+
if t_zoom:
17221722
t_factor = {'sec': 1, 'msec': 0.001, 'usec': 0.000001,
17231723
'hz': 1, 'ppm': 1, 'rads': 1}[t_code]
1724-
t_zoom = (t_zoom * t_factor,)
1725-
else:
1726-
t_zoom = ()
1724+
t_zoom = (t_zoom[0] * t_factor,)
17271725

17281726
return xyz_zooms + t_zoom
17291727

@@ -1774,7 +1772,7 @@ def set_zooms(self, zooms, units=None):
17741772
if units not in ('norm', 'raw') and not isinstance(units, tuple):
17751773
raise ValueError("`units` parameter must be 'norm', 'raw',"
17761774
" or a tuple of unit codes (see set_xyzt_units)")
1777-
super(Nifti1Header, self).set_zooms(zooms, units=units)
1775+
super(Nifti1Header, self).set_zooms(zooms, units='raw')
17781776

17791777
if isinstance(units, tuple):
17801778
self.set_xyzt_units(*units)

0 commit comments

Comments
 (0)