Skip to content

Commit a1ebe59

Browse files
committed
Merge pull request #543 from steelec/bug-fix_rotate_bvec
Bug fix rotate bvec
2 parents ea909c7 + 660a0da commit a1ebe59

File tree

1 file changed

+40
-16
lines changed
  • nipype/workflows/dmri/fsl

1 file changed

+40
-16
lines changed

nipype/workflows/dmri/fsl/dti.py

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ def transpose(samples_over_fibres):
1313
a = a.reshape(-1, 1)
1414
return a.T.tolist()
1515

16-
17-
def create_dmri_preprocessing(name="dMRI_preprocessing", fieldmap_registration=False):
16+
def create_dmri_preprocessing(name="dMRI_preprocessing", use_fieldmap=True, fieldmap_registration=False):
1817
"""Creates a workflow that chains the necessary pipelines to
19-
correct for motion, eddy currents, and susceptibility
20-
artifacts in EPI dMRI sequences.
18+
correct for motion, eddy currents, and, if selected, susceptibility
19+
artifacts in EPI dMRI sequences.
2120
2221
.. warning::
2322
@@ -63,9 +62,9 @@ def create_dmri_preprocessing(name="dMRI_preprocessing", fieldmap_registration=F
6362
6463
6564
Optional arguments::
66-
65+
use_fieldmap - True if there are fieldmap files that should be used (default True)
6766
fieldmap_registration - True if registration to fieldmap should be performed (default False)
68-
67+
6968
7069
"""
7170

@@ -83,17 +82,42 @@ def create_dmri_preprocessing(name="dMRI_preprocessing", fieldmap_registration=F
8382

8483
motion = create_motion_correct_pipeline()
8584
eddy = create_eddy_correct_pipeline()
86-
susceptibility = create_susceptibility_correct_pipeline(
85+
86+
if use_fieldmap: # we have a fieldmap, so lets use it (yay!)
87+
susceptibility = create_susceptibility_correct_pipeline(
8788
fieldmap_registration=fieldmap_registration)
88-
89-
pipeline.connect([
90-
(inputnode, motion, [('in_file', 'inputnode.in_file'), (
91-
'in_bvec', 'inputnode.in_bvec'), ('ref_num', 'inputnode.ref_num')]), (inputnode, eddy, [('ref_num', 'inputnode.ref_num')]), (motion, eddy, [('outputnode.motion_corrected', 'inputnode.in_file')]), (eddy, susceptibility, [('outputnode.eddy_corrected', 'inputnode.in_file')]), (inputnode, susceptibility, [('ref_num', 'inputnode.ref_num'), ('fieldmap_mag', 'inputnode.fieldmap_mag'), ('fieldmap_pha', 'inputnode.fieldmap_pha'), ('te_diff', 'inputnode.te_diff'), ('epi_echospacing', 'inputnode.epi_echospacing'), ('epi_rev_encoding', 'inputnode.epi_rev_encoding'), ('pi_accel_factor', 'inputnode.pi_accel_factor'), ('vsm_sigma', 'inputnode.vsm_sigma')]), (motion, outputnode, [('outputnode.out_bvec', 'bvec_rotated')]), (susceptibility, outputnode, [('outputnode.epi_corrected', 'dmri_corrected')])
92-
])
89+
90+
pipeline.connect([
91+
(inputnode, motion, [('in_file', 'inputnode.in_file'),
92+
('in_bvec', 'inputnode.in_bvec'),
93+
('ref_num', 'inputnode.ref_num')]),
94+
(inputnode, eddy, [('ref_num', 'inputnode.ref_num')]),
95+
(motion, eddy, [('outputnode.motion_corrected', 'inputnode.in_file')]),
96+
(eddy, susceptibility, [('outputnode.eddy_corrected', 'inputnode.in_file')]),
97+
(inputnode, susceptibility, [('ref_num', 'inputnode.ref_num'),
98+
('fieldmap_mag', 'inputnode.fieldmap_mag'),
99+
('fieldmap_pha', 'inputnode.fieldmap_pha'),
100+
('te_diff', 'inputnode.te_diff'),
101+
('epi_echospacing', 'inputnode.epi_echospacing'),
102+
('epi_rev_encoding', 'inputnode.epi_rev_encoding'),
103+
('pi_accel_factor', 'inputnode.pi_accel_factor'),
104+
('vsm_sigma', 'inputnode.vsm_sigma')]),
105+
(motion, outputnode, [('outputnode.out_bvec', 'bvec_rotated')]),
106+
(susceptibility, outputnode, [('outputnode.epi_corrected', 'dmri_corrected')])
107+
])
108+
else: # we don't have a fieldmap, so we just carry on without it :(
109+
pipeline.connect([
110+
(inputnode, motion, [('in_file', 'inputnode.in_file'),
111+
('in_bvec', 'inputnode.in_bvec'),
112+
('ref_num', 'inputnode.ref_num')]),
113+
(inputnode, eddy, [('ref_num', 'inputnode.ref_num')]),
114+
(motion, eddy, [('outputnode.motion_corrected', 'inputnode.in_file')]),
115+
(motion, outputnode, [('outputnode.out_bvec', 'bvec_rotated')]),
116+
(eddy, outputnode, [('outputnode.eddy_corrected', 'dmri_corrected')])
117+
])
93118

94119
return pipeline
95120

96-
97121
def create_bedpostx_pipeline(name="bedpostx"):
98122
"""Creates a pipeline that does the same as bedpostx script from FSL -
99123
calculates diffusion model parameters (distributions not MLE) voxelwise for
@@ -530,12 +554,12 @@ def _rotate_bvecs(in_bvec, in_matrix):
530554
name, _ = os.path.splitext(name)
531555
out_file = os.path.abspath('./%s_rotated.bvec' % name)
532556
bvecs = np.loadtxt(in_bvec)
533-
new_bvecs = [bvecs[:, 0]]
557+
new_bvecs = np.zeros(shape=bvecs.T.shape) #pre-initialise array, 3 col format
534558

535-
for i, vol_matrix in enumerate(in_matrix[1::]):
559+
for i, vol_matrix in enumerate(in_matrix[0::]): #start index at 0
536560
bvec = np.matrix(bvecs[:, i])
537561
rot = np.matrix(np.loadtxt(vol_matrix)[0:3, 0:3])
538-
new_bvecs.append((np.array(rot * bvec.T).T)[0])
562+
new_bvecs[i] = (np.array(rot * bvec.T).T)[0] #fill each volume with x,y,z as we go along
539563
np.savetxt(out_file, np.array(new_bvecs).T, fmt='%0.15f')
540564
return out_file
541565

0 commit comments

Comments
 (0)