@@ -216,31 +216,59 @@ def set_data_dtype(self, datatype):
216
216
raise MGHError ('datatype dtype "%s" not recognized' % datatype )
217
217
self ._structarr ['type' ] = code
218
218
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
+
219
231
def get_zooms (self ):
220
232
''' Get zooms from header
221
233
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
+
222
240
Returns
223
241
-------
224
242
z : tuple
225
243
tuple of header zoom values
226
244
'''
227
245
# 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 ]
229
247
return tuple (self ._structarr ['voxelsize' ]) + tzoom
230
248
231
249
def set_zooms (self , zooms ):
232
250
''' Set zooms into header fields
233
251
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
235
261
'''
236
262
hdr = self ._structarr
237
263
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 ):
242
268
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 ]
244
272
245
273
def get_data_shape (self ):
246
274
''' Get shape of data
0 commit comments