Skip to content

Commit 3f56c7a

Browse files
committed
RF: Make direction cosines explicit, not transposed
1 parent a594e40 commit 3f56c7a

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

nibabel/freesurfer/mghformat.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
('dof', '>i4'),
3333
('goodRASFlag', '>i2'),
3434
('delta', '>f4', (3,)),
35-
('Mdc', '>f4', (3, 3)),
35+
('x_ras', '>f4', (3, 1)),
36+
('y_ras', '>f4', (3, 1)),
37+
('z_ras', '>f4', (3, 1)),
3638
('Pxyz_c', '>f4', (3,))
3739
]
3840
# Optional footer. Also has more stuff after this, optionally
@@ -153,7 +155,7 @@ def get_affine(self):
153155
hdr = self._structarr
154156
d = np.diag(hdr['delta'])
155157
pcrs_c = hdr['dims'][:3] / 2.0
156-
Mdc = hdr['Mdc'].T
158+
Mdc = np.vstack((hdr['x_ras'], hdr['y_ras'], hdr['z_ras']))
157159
pxyz_0 = hdr['Pxyz_c'] - np.dot(Mdc, np.dot(d, pcrs_c))
158160
M = np.eye(4, 4)
159161
M[0:3, 0:3] = np.dot(Mdc, d)
@@ -315,9 +317,9 @@ def default_structarr(klass, endianness=None):
315317
hdr_data['type'] = 3
316318
hdr_data['goodRASFlag'] = 1
317319
hdr_data['delta'][:] = np.array([1, 1, 1])
318-
hdr_data['Mdc'][:, 0] = np.array([-1, 0, 0]) # x_ras
319-
hdr_data['Mdc'][:, 1] = np.array([0, 0, 1]) # y_ras
320-
hdr_data['Mdc'][:, 2] = np.array([0, -1, 0]) # z_ras
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]])
321323
hdr_data['Pxyz_c'] = np.array([0, 0, 0]) # c_ras
322324
hdr_data['mrparms'] = np.array([0, 0, 0, 0])
323325
return hdr_data
@@ -327,9 +329,9 @@ def _set_affine_default(self):
327329
'''
328330
self._structarr['goodRASFlag'] = 1
329331
self._structarr['delta'][:] = np.array([1, 1, 1])
330-
hdr_data['Mdc'][:, 0] = np.array([-1, 0, 0]) # x_ras
331-
hdr_data['Mdc'][:, 1] = np.array([0, 0, 1]) # y_ras
332-
hdr_data['Mdc'][:, 2] = np.array([0, -1, 0]) # z_ras
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]])
333335
self._structarr['Pxyz_c'][:] = np.array([0, 0, 0]) # c_ras
334336

335337
def writehdr_to(self, fileobj):
@@ -535,7 +537,9 @@ def _affine2header(self):
535537
Pxyz_c = np.dot(self._affine, Pcrs_c)
536538

537539
hdr['delta'][:] = delta
538-
hdr['Mdc'][:, :] = Mdc.T
540+
hdr['x_ras'] = Mdc[:, [0]]
541+
hdr['y_ras'] = Mdc[:, [1]]
542+
hdr['z_ras'] = Mdc[:, [2]]
539543
hdr['Pxyz_c'][:] = Pxyz_c[:3]
540544

541545

0 commit comments

Comments
 (0)