Skip to content

Commit 9e664a2

Browse files
committed
FIX/ENH: get/set_data_shape more robustly
1 parent b1bf5e7 commit 9e664a2

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

nibabel/freesurfer/mghformat.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,12 @@ def set_zooms(self, zooms):
314314
def get_data_shape(self):
315315
''' Get shape of data
316316
'''
317-
dims = self._header_data['dims'][:]
317+
shape = tuple(self._header_data['dims'])
318318
# If last dimension (nframes) is 1, remove it because
319319
# we want to maintain 3D and it's redundant
320-
if int(dims[-1]) == 1:
321-
dims = dims[:-1]
322-
return tuple(int(d) for d in dims)
320+
if shape[3] == 1:
321+
shape = shape[:3]
322+
return shape
323323

324324
def set_data_shape(self, shape):
325325
''' Set shape of data
@@ -329,15 +329,10 @@ def set_data_shape(self, shape):
329329
shape : sequence
330330
sequence of integers specifying data array shape
331331
'''
332-
dims = self._header_data['dims']
333-
# If len(dims) is 3, add a dimension. MGH header always
334-
# needs 4 dimensions.
335-
if len(shape) == 3:
336-
shape = list(shape)
337-
shape.append(1)
338-
shape = tuple(shape)
339-
dims[:] = shape
340-
self._header_data['delta'][:] = 1.0
332+
shape = tuple(shape)
333+
if len(shape) > 4:
334+
raise ValueError("Shape may be at most 4 dimensional")
335+
self._header_data['dims'] = shape + (1,) * (4 - len(shape))
341336

342337
def get_data_bytespervox(self):
343338
''' Get the number of bytes per voxel of the data

0 commit comments

Comments
 (0)