Skip to content

Commit 46de36f

Browse files
committed
Merge pull request #1238 from oesteban/fix/NotDefined_b0_indices
[FIX] Global name b0_indices not defined
2 parents 1020c80 + f421c12 commit 46de36f

File tree

1 file changed

+48
-45
lines changed

1 file changed

+48
-45
lines changed

nipype/workflows/dmri/fsl/utils.py

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ def cleanup_edge_pipeline(name='Cleanup'):
1919
name='outputnode')
2020

2121
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')
2525
newmsk = pe.Node(fsl.MultiImageMaths(op_string='-sub %s -thr 0.5 -bin'),
2626
name='NewMask')
2727
applymsk = pe.Node(fsl.ApplyMask(nan2zeros=True), name='ApplyMask')
@@ -51,13 +51,13 @@ def vsm2warp(name='Shiftmap2Warping'):
5151
"""
5252
Converts a voxel shift map (vsm) to a displacements field (warp).
5353
"""
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')
5656
outputnode = pe.Node(niu.IdentityInterface(fields=['out_warp']),
5757
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')
6161
vsm = pe.Node(fsl.maths.BinaryMaths(operation='mul'), name='ScaleField')
6262
vsm2dfm = pe.Node(fsl.ConvertWarp(relwarp=True, out_relwarp=True),
6363
name='vsm2dfm')
@@ -81,33 +81,33 @@ def dwi_flirt(name='DWICoregistration', excl_nodiff=False,
8181
"""
8282
Generates a workflow for linear registration of dwi volumes
8383
"""
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')
8787

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')
9191
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')
9494
split = pe.Node(fsl.Split(dimension='t'), name='SplitDWIs')
9595
pick_ref = pe.Node(niu.Select(), name='Pick_b0')
9696
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')
100100
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'])
104104
flirt = pe.MapNode(fsl.FLIRT(**flirt_param), name='CoRegistration',
105105
iterfield=['in_file', 'in_matrix_file'])
106106
thres = pe.MapNode(fsl.Threshold(thresh=0.0), iterfield=['in_file'],
107107
name='RemoveNegative')
108108
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')
111111
wf = pe.Workflow(name=name)
112112
wf.connect([
113113
(inputnode, split, [('in_file', 'in_file')]),
@@ -141,10 +141,11 @@ def apply_all_corrections(name='UnwarpArtifacts'):
141141
the map of determinants of the jacobian.
142142
"""
143143

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')
148149
warps = pe.MapNode(fsl.ConvertWarp(relwarp=True),
149150
iterfield=['premat', 'postmat'],
150151
name='ConvertWarp')
@@ -406,8 +407,8 @@ def time_avg(in_file, index=[0], out_file=None):
406407
if len(index) == 1:
407408
data = imgs[0].get_data().astype(np.float32)
408409
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)
411412

412413
hdr = imgs[0].get_header().copy()
413414
hdr.set_data_shape(data.shape)
@@ -448,7 +449,9 @@ def b0_average(in_dwi, in_bval, max_b=10.0, out_file=None):
448449
out_file = op.abspath("%s_avg_b0%s" % (fname, ext))
449450

450451
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+
452455
b0s = [im.get_data().astype(np.float32)
453456
for im in imgs[index]]
454457
b0 = np.average(np.array(b0s), axis=0)
@@ -483,16 +486,16 @@ def rotate_bvecs(in_bvec, in_matrix):
483486

484487
if len(bvecs) != len(in_matrix):
485488
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)))
488491

489492
for bvec, mat in zip(bvecs, in_matrix):
490493
if np.all(bvec == 0.0):
491494
new_bvecs.append(bvec)
492495
else:
493496
invrot = np.linalg.inv(np.loadtxt(mat))[:3, :3]
494497
newbvec = invrot.dot(bvec)
495-
new_bvecs.append((newbvec/np.linalg.norm(newbvec)))
498+
new_bvecs.append((newbvec / np.linalg.norm(newbvec)))
496499

497500
np.savetxt(out_file, np.array(new_bvecs).T, fmt='%0.15f')
498501
return out_file
@@ -519,7 +522,7 @@ def eddy_rotate_bvecs(in_bvec, eddy_params):
519522

520523
if len(bvecs) != len(params):
521524
raise RuntimeError(('Number of b-vectors and rotation '
522-
'matrices should match.'))
525+
'matrices should match.'))
523526

524527
for bvec, row in zip(bvecs, params):
525528
if np.all(bvec == 0.0):
@@ -530,19 +533,19 @@ def eddy_rotate_bvecs(in_bvec, eddy_params):
530533
az = row[5]
531534

532535
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)]])
535538
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)]])
538541
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]])
541544
R = Rx.dot(Ry).dot(Rz)
542545

543546
invrot = np.linalg.inv(R)
544547
newbvec = invrot.dot(bvec)
545-
new_bvecs.append((newbvec/np.linalg.norm(newbvec)))
548+
new_bvecs.append((newbvec / np.linalg.norm(newbvec)))
546549

547550
np.savetxt(out_file, np.array(new_bvecs).T, fmt='%0.15f')
548551
return out_file
@@ -600,7 +603,7 @@ def siemens2rads(in_file, out_file=None):
600603

601604
imin = data.min()
602605
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
604607
hdr.set_data_dtype(np.float32)
605608
hdr.set_xyzt_units('mm')
606609
hdr['datatype'] = 16
@@ -624,7 +627,7 @@ def rads2radsec(in_file, delta_te, out_file=None):
624627
out_file = op.abspath('./%s_radsec.nii.gz' % fname)
625628

626629
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)
628631
nb.Nifti1Image(data, im.get_affine(),
629632
im.get_header()).to_filename(out_file)
630633
return out_file
@@ -705,7 +708,7 @@ def reorient_bvecs(in_dwi, old_dwi, in_bvec):
705708
sc_idx = np.where((np.abs(RS) != 1) & (RS != 0))
706709
S = np.ones_like(RS)
707710
S[sc_idx] = RS[sc_idx]
708-
R = RS/S
711+
R = RS / S
709712

710713
new_bvecs = [R.dot(b) for b in bvecs]
711714
np.savetxt(out_file, np.array(new_bvecs).T, fmt='%0.15f')

0 commit comments

Comments
 (0)