Skip to content

Commit e8b588f

Browse files
committed
WIP+BRK: apply RAS reorientation to image
Reorient data to LAS+ during conversion. This breaks the tests that expect the data to be in the same orientation for the PAR format as the converted NIfTI format.
1 parent d4318d4 commit e8b588f

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

bin/parrec2nii

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ from __future__ import division, print_function, absolute_import
55

66
from optparse import OptionParser, Option
77
import numpy as np
8+
import numpy.linalg as npl
89
import sys
910
import os
1011
import nibabel
@@ -14,6 +15,9 @@ from nibabel.mriutils import calculate_dwell_time, MRIError
1415
import nibabel.nifti1 as nifti1
1516
from nibabel.filename_parser import splitext_addext
1617
from nibabel.volumeutils import fname_ext_ul_case
18+
from nibabel.orientations import (io_orientation, inv_ornt_aff,
19+
apply_orientation)
20+
from nibabel.affines import apply_affine, from_matvec, to_matvec
1721

1822

1923
def get_opt_parser():
@@ -163,6 +167,17 @@ def proc_file(infile, opts):
163167
intercept = np.array([0.])
164168
in_data = np.array(pr_img.dataobj)
165169
out_dtype = np.float64
170+
# Reorient data block to LAS+ if necessary
171+
ornt = io_orientation(np.diag([-1, 1, 1, 1]).dot(affine))
172+
if np.all(ornt == [[0, 1],
173+
[1, 1],
174+
[2, 1]]): # already in LAS+
175+
t_aff = np.eye(4)
176+
else: # Not in LAS+
177+
t_aff = inv_ornt_aff(ornt, pr_img.shape)
178+
affine = np.dot(affine, t_aff)
179+
in_data = apply_orientation(in_data, ornt)
180+
# Make corresponding NIfTI image
166181
nimg = nifti1.Nifti1Image(in_data, affine, pr_hdr)
167182
nhdr = nimg.header
168183
nhdr.set_data_dtype(out_dtype)
@@ -198,6 +213,10 @@ def proc_file(infile, opts):
198213
verbose('No DTI volumes detected, bvals and bvecs not written')
199214
else:
200215
verbose('Writing .bvals and .bvecs files')
216+
# Transform bvecs with reorientation affine
217+
orig2new = npl.inv(t_aff)
218+
bv_reorient = from_matvec(to_matvec(orig2new)[0], [0, 0, 0])
219+
bvecs = apply_affine(bv_reorient, bvecs)
201220
with open(basefilename + '.bvals', 'w') as fid:
202221
# np.savetxt could do this, but it's just a loop anyway
203222
for val in bvals:

0 commit comments

Comments
 (0)