Skip to content

Commit 6bc9603

Browse files
committed
RF: delta -> voxelsize; simplify conversion code
1 parent 9bc71b6 commit 6bc9603

File tree

1 file changed

+34
-37
lines changed

1 file changed

+34
-37
lines changed

nibabel/freesurfer/mghformat.py

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
('type', '>i4'),
3333
('dof', '>i4'),
3434
('ras_good', '>i2'),
35-
('delta', '>f4', (3,)),
35+
('voxelsize', '>f4', (3,)),
3636
('x_ras', '>f4', (3, 1)),
3737
('y_ras', '>f4', (3, 1)),
3838
('z_ras', '>f4', (3, 1)),
@@ -155,11 +155,10 @@ def get_affine(self):
155155
'''
156156
affine = np.eye(4)
157157
hdr = self._structarr
158-
d = np.diag(hdr['delta'])
159-
pcrs_c = hdr['dims'][:3] / 2.0
160-
MdcD = np.hstack((hdr['x_ras'], hdr['y_ras'], hdr['z_ras'])).dot(d)
158+
MdcD = np.hstack((hdr['x_ras'], hdr['y_ras'], hdr['z_ras'])) * hdr['voxelsize']
159+
vol_center = MdcD.dot(hdr['dims'][:3].reshape(-1, 1)) / 2
161160
affine[:3, :3] = MdcD
162-
affine[:3, [3]] = hdr['c_ras'] - MdcD.dot(pcrs_c.reshape(3, 1))
161+
affine[:3, [3]] = hdr['c_ras'] - vol_center
163162
return affine
164163

165164
# For compatibility with nifti (multiple affines)
@@ -174,8 +173,8 @@ def get_vox2ras_tkr(self):
174173
''' Get the vox2ras-tkr transform. See "Torig" here:
175174
https://surfer.nmr.mgh.harvard.edu/fswiki/CoordinateSystems
176175
'''
177-
ds = np.array(self._structarr['delta'])
178-
ns = (np.array(self._structarr['dims'][:3]) * ds) / 2.0
176+
ds = self._structarr['voxelsize']
177+
ns = self._structarr['dims'][:3] * ds / 2.0
179178
v2rtkr = np.array([[-ds[0], 0, 0, ns[0]],
180179
[0, 0, ds[2], -ns[2]],
181180
[0, -ds[1], 0, ns[1]],
@@ -213,9 +212,7 @@ def get_zooms(self):
213212
z : tuple
214213
tuple of header zoom values
215214
'''
216-
hdr = self._structarr
217-
zooms = hdr['delta']
218-
return tuple(zooms[:])
215+
return tuple(self._structarr['voxelsize'])
219216

220217
def set_zooms(self, zooms):
221218
''' Set zooms into header fields
@@ -224,13 +221,12 @@ def set_zooms(self, zooms):
224221
'''
225222
hdr = self._structarr
226223
zooms = np.asarray(zooms)
227-
if len(zooms) != len(hdr['delta']):
224+
if len(zooms) != len(hdr['voxelsize']):
228225
raise HeaderDataError('Expecting %d zoom values for ndim'
229-
% hdr['delta'])
226+
% hdr['voxelsize'])
230227
if np.any(zooms < 0):
231228
raise HeaderDataError('zooms must be positive')
232-
delta = hdr['delta']
233-
delta[:] = zooms[:]
229+
hdr['voxelsize'] = zooms
234230

235231
def get_data_shape(self):
236232
''' Get shape of data
@@ -311,27 +307,27 @@ def default_structarr(klass, endianness=None):
311307
312308
Ignores byte order; always big endian
313309
'''
314-
hdr_data = super(MGHHeader, klass).default_structarr()
315-
hdr_data['version'] = 1
316-
hdr_data['dims'][:] = np.array([1, 1, 1, 1])
317-
hdr_data['type'] = 3
318-
hdr_data['ras_good'] = 1
319-
hdr_data['delta'][:] = np.array([1, 1, 1])
320-
hdr_data['x_ras'] = np.array([[-1], [0], [0]])
321-
hdr_data['y_ras'] = np.array([[0], [0], [1]])
322-
hdr_data['z_ras'] = np.array([[0], [-1], [0]])
323-
hdr_data['c_ras'] = 0
324-
hdr_data['mrparms'] = np.array([0, 0, 0, 0])
325-
return hdr_data
310+
structarr = super(MGHHeader, klass).default_structarr()
311+
structarr['version'] = 1
312+
structarr['dims'] = 1
313+
structarr['type'] = 3
314+
structarr['ras_good'] = 1
315+
structarr['voxelsize'] = 1
316+
structarr['x_ras'] = [[-1], [0], [0]]
317+
structarr['y_ras'] = [[0], [0], [1]]
318+
structarr['z_ras'] = [[0], [-1], [0]]
319+
structarr['c_ras'] = 0
320+
structarr['mrparms'] = 0
321+
return structarr
326322

327323
def _set_affine_default(self):
328324
''' If ras_good flag is 0, set the default affine
329325
'''
330326
self._structarr['ras_good'] = 1
331-
self._structarr['delta'][:] = np.array([1, 1, 1])
332-
self._structarr['x_ras'] = np.array([[-1], [0], [0]])
333-
self._structarr['y_ras'] = np.array([[0], [0], [1]])
334-
self._structarr['z_ras'] = np.array([[0], [-1], [0]])
327+
self._structarr['voxelsize'] = 1
328+
self._structarr['x_ras'] = [[-1], [0], [0]]
329+
self._structarr['y_ras'] = [[0], [0], [1]]
330+
self._structarr['z_ras'] = [[0], [-1], [0]]
335331
self._structarr['c_ras'] = 0
336332

337333
def writehdr_to(self, fileobj):
@@ -535,18 +531,19 @@ def _write_data(self, mghfile, data, header):
535531
def _affine2header(self):
536532
""" Unconditionally set affine into the header """
537533
hdr = self._header
538-
shape = np.array(self._dataobj.shape[:3]).reshape(3, 1)
534+
shape = np.array(self._dataobj.shape[:3]).reshape(-1, 1)
535+
539536
# for more information, go through save_mgh.m in FreeSurfer dist
540-
MdcD = self._affine[:3, :3]
541-
delta = voxel_sizes(self._affine)
542-
Mdc = MdcD / np.tile(delta, (3, 1))
543-
c_ras = self._affine.dot(np.vstack((shape, [1])))
537+
voxelsize = voxel_sizes(self._affine)
538+
Mdc = self._affine[:3, :3] / voxelsize
539+
c_ras = self._affine.dot(np.vstack((shape / 2, [1])))[:3]
544540

545-
hdr['delta'] = delta
541+
# Assign after we've had a chance to raise exceptions
542+
hdr['voxelsize'] = voxelsize
546543
hdr['x_ras'] = Mdc[:, [0]]
547544
hdr['y_ras'] = Mdc[:, [1]]
548545
hdr['z_ras'] = Mdc[:, [2]]
549-
hdr['c_ras'] = c_ras[:3]
546+
hdr['c_ras'] = c_ras
550547

551548

552549
load = MGHImage.load

0 commit comments

Comments
 (0)