Skip to content

Commit c487382

Browse files
committed
ENH: Add get_norm_zooms for zooms in mm/s units
1 parent 97447e2 commit c487382

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

nibabel/analyze.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,10 @@ def set_zooms(self, zooms):
710710
pixdims = hdr['pixdim']
711711
pixdims[1:ndim + 1] = zooms[:]
712712

713+
def get_norm_zooms(self, raise_unknown=False):
714+
''' Get zooms in mm/s units '''
715+
return self.get_zooms()
716+
713717
def as_analyze_map(self):
714718
""" Return header as mapping for conversion to Analyze types
715719

nibabel/nifti1.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,6 +1638,39 @@ def set_xyzt_units(self, xyz=None, t=None):
16381638
t_code = unit_codes[t]
16391639
self.structarr['xyzt_units'] = xyz_code + t_code
16401640

1641+
def get_norm_zooms(self, raise_unknown=False):
1642+
''' Get zooms in mm/s units '''
1643+
raw_zooms = self.get_zooms()
1644+
xyz_zooms = raw_zooms[:3]
1645+
t_zoom = raw_zooms[3] if len(raw_zooms) > 3 else None
1646+
1647+
xyz_code, t_code = self.get_xyzt_units()
1648+
xyz_msg = t_msg = ''
1649+
if xyz_code == 'unknown':
1650+
xyz_msg = 'Unknown spatial units'
1651+
xyz_code = 'mm'
1652+
if t_code == 'unknown' and t_zoom is not None:
1653+
t_msg = 'Unknown time units'
1654+
t_code = 'sec'
1655+
if raise_unknown and (xyz_msg, t_msg) != ('', ''):
1656+
if xyz_msg and t_msg:
1657+
msg = 'Unknown spatial and time units'
1658+
else:
1659+
msg = xyz_msg or t_msg
1660+
raise ValueError("Error: {}".format(msg))
1661+
if xyz_msg:
1662+
warnings.warn('{} - assuming mm'.format(xyz_msg))
1663+
if t_msg:
1664+
warnings.warn('{} - assuming sec'.format(t_msg))
1665+
1666+
xyz_factor = {'meter': 0.001, 'mm': 1, 'usec': 1000}[xyz_code]
1667+
t_factor = {'sec': 1, 'msec': 1000, 'usec': 1000000}[t_code]
1668+
1669+
xyz_zooms = tuple(np.array(xyz_zooms) / xyz_factor)
1670+
t_zoom = (t_zoom / t_factor,) if t_zoom is not None else ()
1671+
1672+
return xyz_zooms + t_zoom
1673+
16411674
def _clean_after_mapping(self):
16421675
''' Set format-specific stuff after converting header from mapping
16431676

nibabel/spatialimages.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,10 @@ def set_zooms(self, zooms):
242242
raise HeaderDataError('zooms must be positive')
243243
self._zooms = zooms
244244

245+
def get_norm_zooms(self, raise_unknown=False):
246+
''' Get zooms in mm/s units '''
247+
return self.get_zooms()
248+
245249
def get_base_affine(self):
246250
shape = self.get_data_shape()
247251
zooms = self.get_zooms()

0 commit comments

Comments
 (0)