@@ -19,9 +19,9 @@ def cleanup_edge_pipeline(name='Cleanup'):
19
19
name = 'outputnode' )
20
20
21
21
fugue = pe .Node (fsl .FUGUE (save_fmap = True , despike_2dfilter = True ,
22
- despike_threshold = 2.1 ), name = 'Despike' )
23
- erode = pe .Node (fsl .maths .MathsCommand (nan2zeros = True ,
24
- args = '-kernel 2D -ero' ), name = 'MskErode' )
22
+ despike_threshold = 2.1 ), name = 'Despike' )
23
+ erode = pe .Node (fsl .maths .MathsCommand (
24
+ nan2zeros = True , args = '-kernel 2D -ero' ), name = 'MskErode' )
25
25
newmsk = pe .Node (fsl .MultiImageMaths (op_string = '-sub %s -thr 0.5 -bin' ),
26
26
name = 'NewMask' )
27
27
applymsk = pe .Node (fsl .ApplyMask (nan2zeros = True ), name = 'ApplyMask' )
@@ -51,13 +51,13 @@ def vsm2warp(name='Shiftmap2Warping'):
51
51
"""
52
52
Converts a voxel shift map (vsm) to a displacements field (warp).
53
53
"""
54
- inputnode = pe .Node (niu .IdentityInterface (fields = [ 'in_vsm' ,
55
- 'in_ref' , 'scaling' , 'enc_dir' ]), name = 'inputnode' )
54
+ inputnode = pe .Node (niu .IdentityInterface (
55
+ fields = [ 'in_vsm' , 'in_ref' , 'scaling' , 'enc_dir' ]), name = 'inputnode' )
56
56
outputnode = pe .Node (niu .IdentityInterface (fields = ['out_warp' ]),
57
57
name = 'outputnode' )
58
- fixhdr = pe .Node (niu .Function (input_names = [ 'in_file' , 'in_file_hdr' ],
59
- output_names = ['out_file' ], function = copy_hdr ) ,
60
- name = 'Fix_hdr' )
58
+ fixhdr = pe .Node (niu .Function (
59
+ input_names = [ 'in_file' , 'in_file_hdr' ], output_names = ['out_file' ],
60
+ function = copy_hdr ), name = 'Fix_hdr' )
61
61
vsm = pe .Node (fsl .maths .BinaryMaths (operation = 'mul' ), name = 'ScaleField' )
62
62
vsm2dfm = pe .Node (fsl .ConvertWarp (relwarp = True , out_relwarp = True ),
63
63
name = 'vsm2dfm' )
@@ -81,33 +81,33 @@ def dwi_flirt(name='DWICoregistration', excl_nodiff=False,
81
81
"""
82
82
Generates a workflow for linear registration of dwi volumes
83
83
"""
84
- inputnode = pe .Node (niu .IdentityInterface (fields = [ 'reference' ,
85
- 'in_file' , 'ref_mask' , 'in_xfms' , 'in_bval' ]),
86
- name = 'inputnode' )
84
+ inputnode = pe .Node (niu .IdentityInterface (
85
+ fields = [ 'reference' , 'in_file' , 'ref_mask' , 'in_xfms' , 'in_bval' ]),
86
+ name = 'inputnode' )
87
87
88
- initmat = pe .Node (niu .Function (input_names = [ 'in_bval' , 'in_xfms' ,
89
- 'excl_nodiff' ], output_names = [ 'init_xfms ' ],
90
- function = _checkinitxfm ), name = 'InitXforms' )
88
+ initmat = pe .Node (niu .Function (
89
+ input_names = [ 'in_bval' , 'in_xfms' , 'excl_nodiff ' ],
90
+ output_names = [ 'init_xfms' ], function = _checkinitxfm ), name = 'InitXforms' )
91
91
initmat .inputs .excl_nodiff = excl_nodiff
92
- dilate = pe .Node (fsl .maths .MathsCommand (nan2zeros = True ,
93
- args = '-kernel sphere 5 -dilM' ), name = 'MskDilate' )
92
+ dilate = pe .Node (fsl .maths .MathsCommand (
93
+ nan2zeros = True , args = '-kernel sphere 5 -dilM' ), name = 'MskDilate' )
94
94
split = pe .Node (fsl .Split (dimension = 't' ), name = 'SplitDWIs' )
95
95
pick_ref = pe .Node (niu .Select (), name = 'Pick_b0' )
96
96
n4 = pe .Node (ants .N4BiasFieldCorrection (dimension = 3 ), name = 'Bias' )
97
- enhb0 = pe .Node (niu .Function (input_names = [ 'in_file' , 'in_mask' ,
98
- 'clip_limit' ], output_names = [ 'out_file ' ],
99
- function = enhance ), name = 'B0Equalize' )
97
+ enhb0 = pe .Node (niu .Function (
98
+ input_names = [ 'in_file' , 'in_mask' , 'clip_limit ' ],
99
+ output_names = [ 'out_file' ], function = enhance ), name = 'B0Equalize' )
100
100
enhb0 .inputs .clip_limit = 0.015
101
- enhdw = pe .MapNode (niu .Function (input_names = [ 'in_file' , 'in_mask' ],
102
- output_names = ['out_file' ], function = enhance ) ,
103
- name = 'DWEqualize' , iterfield = ['in_file' ])
101
+ enhdw = pe .MapNode (niu .Function (
102
+ input_names = [ 'in_file' , 'in_mask' ], output_names = ['out_file' ],
103
+ function = enhance ), name = 'DWEqualize' , iterfield = ['in_file' ])
104
104
flirt = pe .MapNode (fsl .FLIRT (** flirt_param ), name = 'CoRegistration' ,
105
105
iterfield = ['in_file' , 'in_matrix_file' ])
106
106
thres = pe .MapNode (fsl .Threshold (thresh = 0.0 ), iterfield = ['in_file' ],
107
107
name = 'RemoveNegative' )
108
108
merge = pe .Node (fsl .Merge (dimension = 't' ), name = 'MergeDWIs' )
109
- outputnode = pe .Node (niu .IdentityInterface (fields = [ 'out_file' ,
110
- 'out_xfms' ]), name = 'outputnode' )
109
+ outputnode = pe .Node (niu .IdentityInterface (
110
+ fields = [ 'out_file' , 'out_xfms' ]), name = 'outputnode' )
111
111
wf = pe .Workflow (name = name )
112
112
wf .connect ([
113
113
(inputnode , split , [('in_file' , 'in_file' )]),
@@ -141,10 +141,11 @@ def apply_all_corrections(name='UnwarpArtifacts'):
141
141
the map of determinants of the jacobian.
142
142
"""
143
143
144
- inputnode = pe .Node (niu .IdentityInterface (fields = ['in_sdc' ,
145
- 'in_hmc' , 'in_ecc' , 'in_dwi' ]), name = 'inputnode' )
146
- outputnode = pe .Node (niu .IdentityInterface (fields = ['out_file' , 'out_warp' ,
147
- 'out_coeff' , 'out_jacobian' ]), name = 'outputnode' )
144
+ inputnode = pe .Node (niu .IdentityInterface (
145
+ fields = ['in_sdc' , 'in_hmc' , 'in_ecc' , 'in_dwi' ]), name = 'inputnode' )
146
+ outputnode = pe .Node (niu .IdentityInterface (
147
+ fields = ['out_file' , 'out_warp' , 'out_coeff' , 'out_jacobian' ]),
148
+ name = 'outputnode' )
148
149
warps = pe .MapNode (fsl .ConvertWarp (relwarp = True ),
149
150
iterfield = ['premat' , 'postmat' ],
150
151
name = 'ConvertWarp' )
@@ -248,7 +249,7 @@ def hmc_split(in_file, in_bval, ref_num=0, lowbval=5.0):
248
249
249
250
volid = lowbs [0 ]
250
251
if (isdefined (ref_num ) and (ref_num < len (lowbs ))):
251
- volid = [ ref_num ]
252
+ volid = ref_num
252
253
253
254
if volid == 0 :
254
255
data = data [..., 1 :]
@@ -265,8 +266,8 @@ def hmc_split(in_file, in_bval, ref_num=0, lowbval=5.0):
265
266
out_mov = op .abspath ('hmc_mov.nii.gz' )
266
267
out_bval = op .abspath ('bval_split.txt' )
267
268
268
- hdr .set_data_shape (refdata .shape )
269
269
refdata = data [..., volid ]
270
+ hdr .set_data_shape (refdata .shape )
270
271
nb .Nifti1Image (refdata , im .get_affine (), hdr ).to_filename (out_ref )
271
272
272
273
hdr .set_data_shape (data .shape )
@@ -406,8 +407,8 @@ def time_avg(in_file, index=[0], out_file=None):
406
407
if len (index ) == 1 :
407
408
data = imgs [0 ].get_data ().astype (np .float32 )
408
409
else :
409
- data = np .average (np .array ([im .get_data ().astype (np .float32 ) for im in imgs ]),
410
- axis = 0 )
410
+ data = np .average (np .array ([im .get_data ().astype (np .float32 )
411
+ for im in imgs ]), axis = 0 )
411
412
412
413
hdr = imgs [0 ].get_header ().copy ()
413
414
hdr .set_data_shape (data .shape )
@@ -448,7 +449,9 @@ def b0_average(in_dwi, in_bval, max_b=10.0, out_file=None):
448
449
out_file = op .abspath ("%s_avg_b0%s" % (fname , ext ))
449
450
450
451
imgs = np .array (nb .four_to_three (nb .load (in_dwi )))
451
- index = b0_indices (in_bval , max_b = max_b )
452
+ bval = np .loadtxt (in_bval )
453
+ index = np .argwhere (bval <= max_b ).flatten ().tolist ()
454
+
452
455
b0s = [im .get_data ().astype (np .float32 )
453
456
for im in imgs [index ]]
454
457
b0 = np .average (np .array (b0s ), axis = 0 )
@@ -483,16 +486,16 @@ def rotate_bvecs(in_bvec, in_matrix):
483
486
484
487
if len (bvecs ) != len (in_matrix ):
485
488
raise RuntimeError (('Number of b-vectors (%d) and rotation '
486
- 'matrices (%d) should match.' ) % (len (bvecs ),
487
- len (in_matrix )))
489
+ 'matrices (%d) should match.' ) % (len (bvecs ),
490
+ len (in_matrix )))
488
491
489
492
for bvec , mat in zip (bvecs , in_matrix ):
490
493
if np .all (bvec == 0.0 ):
491
494
new_bvecs .append (bvec )
492
495
else :
493
496
invrot = np .linalg .inv (np .loadtxt (mat ))[:3 , :3 ]
494
497
newbvec = invrot .dot (bvec )
495
- new_bvecs .append ((newbvec / np .linalg .norm (newbvec )))
498
+ new_bvecs .append ((newbvec / np .linalg .norm (newbvec )))
496
499
497
500
np .savetxt (out_file , np .array (new_bvecs ).T , fmt = '%0.15f' )
498
501
return out_file
@@ -519,7 +522,7 @@ def eddy_rotate_bvecs(in_bvec, eddy_params):
519
522
520
523
if len (bvecs ) != len (params ):
521
524
raise RuntimeError (('Number of b-vectors and rotation '
522
- 'matrices should match.' ))
525
+ 'matrices should match.' ))
523
526
524
527
for bvec , row in zip (bvecs , params ):
525
528
if np .all (bvec == 0.0 ):
@@ -530,19 +533,19 @@ def eddy_rotate_bvecs(in_bvec, eddy_params):
530
533
az = row [5 ]
531
534
532
535
Rx = np .array ([[1.0 , 0.0 , 0.0 ],
533
- [0.0 , cos (ax ), - sin (ax )],
534
- [0.0 , sin (ax ), cos (ax )]])
536
+ [0.0 , cos (ax ), - sin (ax )],
537
+ [0.0 , sin (ax ), cos (ax )]])
535
538
Ry = np .array ([[cos (ay ), 0.0 , sin (ay )],
536
- [0.0 , 1.0 , 0.0 ],
537
- [- sin (ay ), 0.0 , cos (ay )]])
539
+ [0.0 , 1.0 , 0.0 ],
540
+ [- sin (ay ), 0.0 , cos (ay )]])
538
541
Rz = np .array ([[cos (az ), - sin (az ), 0.0 ],
539
- [sin (az ), cos (az ), 0.0 ],
540
- [0.0 , 0.0 , 1.0 ]])
542
+ [sin (az ), cos (az ), 0.0 ],
543
+ [0.0 , 0.0 , 1.0 ]])
541
544
R = Rx .dot (Ry ).dot (Rz )
542
545
543
546
invrot = np .linalg .inv (R )
544
547
newbvec = invrot .dot (bvec )
545
- new_bvecs .append ((newbvec / np .linalg .norm (newbvec )))
548
+ new_bvecs .append ((newbvec / np .linalg .norm (newbvec )))
546
549
547
550
np .savetxt (out_file , np .array (new_bvecs ).T , fmt = '%0.15f' )
548
551
return out_file
@@ -600,7 +603,7 @@ def siemens2rads(in_file, out_file=None):
600
603
601
604
imin = data .min ()
602
605
imax = data .max ()
603
- data = (2.0 * math .pi * (data - imin )/ (imax - imin )) - math .pi
606
+ data = (2.0 * math .pi * (data - imin ) / (imax - imin )) - math .pi
604
607
hdr .set_data_dtype (np .float32 )
605
608
hdr .set_xyzt_units ('mm' )
606
609
hdr ['datatype' ] = 16
@@ -624,7 +627,7 @@ def rads2radsec(in_file, delta_te, out_file=None):
624
627
out_file = op .abspath ('./%s_radsec.nii.gz' % fname )
625
628
626
629
im = nb .load (in_file )
627
- data = im .get_data ().astype (np .float32 ) * (1.0 / delta_te )
630
+ data = im .get_data ().astype (np .float32 ) * (1.0 / delta_te )
628
631
nb .Nifti1Image (data , im .get_affine (),
629
632
im .get_header ()).to_filename (out_file )
630
633
return out_file
@@ -705,7 +708,7 @@ def reorient_bvecs(in_dwi, old_dwi, in_bvec):
705
708
sc_idx = np .where ((np .abs (RS ) != 1 ) & (RS != 0 ))
706
709
S = np .ones_like (RS )
707
710
S [sc_idx ] = RS [sc_idx ]
708
- R = RS / S
711
+ R = RS / S
709
712
710
713
new_bvecs = [R .dot (b ) for b in bvecs ]
711
714
np .savetxt (out_file , np .array (new_bvecs ).T , fmt = '%0.15f' )
0 commit comments