@@ -216,31 +216,59 @@ def set_data_dtype(self, datatype):
216216 raise MGHError ('datatype dtype "%s" not recognized' % datatype )
217217 self ._structarr ['type' ] = code
218218
219+ def _ndims (self ):
220+ ''' Get dimensionality of data
221+
222+ MGH does not encode dimensionality explicitly, so an image where the
223+ fourth dimension is 1 is treated as three-dimensional.
224+
225+ Returns
226+ -------
227+ ndims : 3 or 4
228+ '''
229+ return 3 + (self ._structarr ['dims' ][3 ] > 1 )
230+
219231 def get_zooms (self ):
220232 ''' Get zooms from header
221233
234+ Returns the spacing of voxels in the x, y, and z dimensions.
235+ For four-dimensional files, a fourth zoom is included, equal to the
236+ repetition time (TR) in ms.
237+
238+ To access only the spatial zooms, use `hdr['voxelsize']`.
239+
222240 Returns
223241 -------
224242 z : tuple
225243 tuple of header zoom values
226244 '''
227245 # Do not return time zoom (TR) if 3D image
228- tzoom = () if self . _structarr [ 'dims' ][ 3 ] == 1 else ( self [ 'tr' ],)
246+ tzoom = (self [ 'tr' ],)[: self . _ndims () > 3 ]
229247 return tuple (self ._structarr ['voxelsize' ]) + tzoom
230248
231249 def set_zooms (self , zooms ):
232250 ''' Set zooms into header fields
233251
234- See docstring for ``get_zooms`` for examples
252+ Sets the spaing of voxels in the x, y, and z dimensions.
253+ For four-dimensional files, a temporal zoom (repetition time, or TR, in
254+ ms) may be provided as a fourth sequence element.
255+
256+ Parameters
257+ ----------
258+ zooms : sequence
259+ sequence of floats specifying spatial and (optionally) temporal
260+ zooms
235261 '''
236262 hdr = self ._structarr
237263 zooms = np .asarray (zooms )
238- if len ( zooms ) != len ( hdr [ 'voxelsize' ]):
239- raise HeaderDataError ( 'Expecting %d zoom values for ndim'
240- % hdr [ 'voxelsize' ] )
241- if np .any (zooms < 0 ):
264+ ndims = self . _ndims ()
265+ if len ( zooms ) > ndims :
266+ raise HeaderDataError ( 'Expecting %d zoom values' % ndims )
267+ if np .any (zooms <= 0 ):
242268 raise HeaderDataError ('zooms must be positive' )
243- hdr ['voxelsize' ] = zooms
269+ hdr ['voxelsize' ] = zooms [:3 ]
270+ if len (zooms ) == 4 :
271+ hdr ['tr' ] = zooms [3 ]
244272
245273 def get_data_shape (self ):
246274 ''' Get shape of data
0 commit comments