|
13 | 13 | from os.path import splitext
|
14 | 14 | import numpy as np
|
15 | 15 |
|
| 16 | +from ..affines import voxel_sizes |
16 | 17 | from ..volumeutils import (array_to_file, array_from_file, Recoder)
|
17 | 18 | from ..spatialimages import HeaderDataError, SpatialImage
|
18 | 19 | from ..fileholders import FileHolder
|
@@ -152,15 +153,14 @@ def get_affine(self):
|
152 | 153 | from the zooms ( delta ), direction cosines ( Mdc ), RAS centers (
|
153 | 154 | c_ras ) and the dimensions.
|
154 | 155 | '''
|
| 156 | + affine = np.eye(4) |
155 | 157 | hdr = self._structarr
|
156 | 158 | d = np.diag(hdr['delta'])
|
157 | 159 | pcrs_c = hdr['dims'][:3] / 2.0
|
158 |
| - Mdc = np.hstack((hdr['x_ras'], hdr['y_ras'], hdr['z_ras'])) |
159 |
| - pxyz_0 = hdr['c_ras'] - Mdc.dot(d).dot(pcrs_c.reshape(3, 1)) |
160 |
| - M = np.eye(4, 4) |
161 |
| - M[:3, :3] = np.dot(Mdc, d) |
162 |
| - M[:3, [3]] = pxyz_0 |
163 |
| - return M |
| 160 | + MdcD = np.hstack((hdr['x_ras'], hdr['y_ras'], hdr['z_ras'])).dot(d) |
| 161 | + affine[:3, :3] = MdcD |
| 162 | + affine[:3, [3]] = hdr['c_ras'] - MdcD.dot(pcrs_c.reshape(3, 1)) |
| 163 | + return affine |
164 | 164 |
|
165 | 165 | # For compatibility with nifti (multiple affines)
|
166 | 166 | get_best_affine = get_affine
|
@@ -527,16 +527,14 @@ def _write_data(self, mghfile, data, header):
|
527 | 527 | def _affine2header(self):
|
528 | 528 | """ Unconditionally set affine into the header """
|
529 | 529 | hdr = self._header
|
530 |
| - shape = self._dataobj.shape |
| 530 | + shape = np.array(self._dataobj.shape[:3]).reshape(3, 1) |
531 | 531 | # for more information, go through save_mgh.m in FreeSurfer dist
|
532 | 532 | MdcD = self._affine[:3, :3]
|
533 |
| - delta = np.sqrt(np.sum(MdcD * MdcD, axis=0)) |
| 533 | + delta = voxel_sizes(self._affine) |
534 | 534 | Mdc = MdcD / np.tile(delta, (3, 1))
|
535 |
| - Pcrs_c = np.array([0, 0, 0, 1], dtype=np.float) |
536 |
| - Pcrs_c[:3] = np.array(shape[:3]) / 2.0 |
537 |
| - c_ras = self._affine.dot(Pcrs_c.reshape(4, 1)) |
| 535 | + c_ras = self._affine.dot(np.vstack((shape, [1]))) |
538 | 536 |
|
539 |
| - hdr['delta'][:] = delta |
| 537 | + hdr['delta'] = delta |
540 | 538 | hdr['x_ras'] = Mdc[:, [0]]
|
541 | 539 | hdr['y_ras'] = Mdc[:, [1]]
|
542 | 540 | hdr['z_ras'] = Mdc[:, [2]]
|
|
0 commit comments