Skip to content

Commit 342116c

Browse files
committed
RF: Simplify affine conversion code
1 parent 8e89c9d commit 342116c

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

nibabel/freesurfer/mghformat.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from os.path import splitext
1414
import numpy as np
1515

16-
from ..affines import voxel_sizes
16+
from ..affines import voxel_sizes, from_matvec
1717
from ..volumeutils import (array_to_file, array_from_file, Recoder)
1818
from ..spatialimages import HeaderDataError, SpatialImage
1919
from ..fileholders import FileHolder
@@ -168,10 +168,8 @@ def get_affine(self):
168168
affine = np.eye(4)
169169
hdr = self._structarr
170170
MdcD = np.hstack((hdr['x_ras'], hdr['y_ras'], hdr['z_ras'])) * hdr['voxelsize']
171-
vol_center = MdcD.dot(hdr['dims'][:3].reshape(-1, 1)) / 2
172-
affine[:3, :3] = MdcD
173-
affine[:3, [3]] = hdr['c_ras'] - vol_center
174-
return affine
171+
vol_center = MdcD.dot(hdr['dims'][:3]) / 2
172+
return from_matvec(MdcD, hdr['c_ras'].T - vol_center)
175173

176174
# For compatibility with nifti (multiple affines)
177175
get_best_affine = get_affine
@@ -581,9 +579,7 @@ def _affine2header(self):
581579

582580
# Assign after we've had a chance to raise exceptions
583581
hdr['voxelsize'] = voxelsize
584-
hdr['x_ras'] = Mdc[:, [0]]
585-
hdr['y_ras'] = Mdc[:, [1]]
586-
hdr['z_ras'] = Mdc[:, [2]]
582+
hdr['x_ras'][:, 0], hdr['y_ras'][:, 0], hdr['z_ras'][:, 0] = Mdc.T
587583
hdr['c_ras'] = c_ras
588584

589585

0 commit comments

Comments
 (0)