Skip to content

Commit 3f3b450

Browse files
committed
ENH: Add get_norm_zooms for zooms in mm/s units
1 parent 943e5e8 commit 3f3b450

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
@@ -706,6 +706,10 @@ def set_zooms(self, zooms):
706706
pixdims = hdr['pixdim']
707707
pixdims[1:ndim + 1] = zooms[:]
708708

709+
def get_norm_zooms(self, raise_unknown=False):
710+
''' Get zooms in mm/s units '''
711+
return self.get_zooms()
712+
709713
def as_analyze_map(self):
710714
""" Return header as mapping for conversion to Analyze types
711715

nibabel/nifti1.py

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

1680+
def get_norm_zooms(self, raise_unknown=False):
1681+
''' Get zooms in mm/s units '''
1682+
raw_zooms = self.get_zooms()
1683+
xyz_zooms = raw_zooms[:3]
1684+
t_zoom = raw_zooms[3] if len(raw_zooms) > 3 else None
1685+
1686+
xyz_code, t_code = self.get_xyzt_units()
1687+
xyz_msg = t_msg = ''
1688+
if xyz_code == 'unknown':
1689+
xyz_msg = 'Unknown spatial units'
1690+
xyz_code = 'mm'
1691+
if t_code == 'unknown' and t_zoom is not None:
1692+
t_msg = 'Unknown time units'
1693+
t_code = 'sec'
1694+
if raise_unknown and (xyz_msg, t_msg) != ('', ''):
1695+
if xyz_msg and t_msg:
1696+
msg = 'Unknown spatial and time units'
1697+
else:
1698+
msg = xyz_msg or t_msg
1699+
raise ValueError("Error: {}".format(msg))
1700+
if xyz_msg:
1701+
warnings.warn('{} - assuming mm'.format(xyz_msg))
1702+
if t_msg:
1703+
warnings.warn('{} - assuming sec'.format(t_msg))
1704+
1705+
xyz_factor = {'meter': 0.001, 'mm': 1, 'usec': 1000}[xyz_code]
1706+
t_factor = {'sec': 1, 'msec': 1000, 'usec': 1000000}[t_code]
1707+
1708+
xyz_zooms = tuple(np.array(xyz_zooms) / xyz_factor)
1709+
t_zoom = (t_zoom / t_factor,) if t_zoom is not None else ()
1710+
1711+
return xyz_zooms + t_zoom
1712+
16801713
def _clean_after_mapping(self):
16811714
""" Set format-specific stuff after converting header from mapping
16821715

nibabel/spatialimages.py

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

241+
def get_norm_zooms(self, raise_unknown=False):
242+
''' Get zooms in mm/s units '''
243+
return self.get_zooms()
244+
241245
def get_base_affine(self):
242246
shape = self.get_data_shape()
243247
zooms = self.get_zooms()

0 commit comments

Comments
 (0)