Skip to content

Commit 3a437c0

Browse files
committed
Fix logging formatting in several interfaces.
Fix logging formatting in ants, cmtk, elastic, freesurfer, fsl, mrtrix, mrtrix3, nilearn, and spm interfaces. Okay, it was mostly in cmtk, io, and mrtrix. - In nipype/interfaces/io.py logging is imported multiple times, and iflogger is redefined a couple of times. Not sure if that’s the right way to go. - Is this okay: - `iflogger.debug('saving inputs {}', inputs)` (nipype/interfaces/base.py#L1210) - Also caught a couple of things in nipype/interfaces/cmtk/nx.py. Namely a couple of unused variables and at one point redefining `file`.
1 parent 026be4e commit 3a437c0

File tree

17 files changed

+135
-123
lines changed

17 files changed

+135
-123
lines changed

nipype/interfaces/ants/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# Local imports
1212
from ... import logging, LooseVersion
1313
from ..base import CommandLine, CommandLineInputSpec, traits, isdefined
14-
logger = logging.getLogger('interface')
14+
iflogger = logging.getLogger('interface')
1515

1616
# -Using -1 gives primary responsibilty to ITKv4 to do the correct
1717
# thread limitings.

nipype/interfaces/ants/registration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ class Registration(ANTSCommand):
672672
673673
One can use multiple similarity metrics in a single registration stage.The Node below first
674674
performs a linear registation using only the Mutual Information ('Mattes')-metric.
675-
In a second stage, it performs a non-linear registration ('Syn') using both a
675+
In a second stage, it performs a non-linear registration ('Syn') using both a
676676
Mutual Information and a local cross-correlation ('CC')-metric. Both metrics are weighted
677677
equally ('metric_weight' is .5 for both). The Mutual Information- metric uses 32 bins.
678678
The local cross-correlations (correlations between every voxel's neighborhoods) is computed

nipype/interfaces/cmtk/cmtk.py

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,11 @@ def create_allpoints_cmat(streamlines, roiData, voxelSize, n_rois):
120120

121121
connectivity_matrix = get_connectivity_matrix(n_rois, list_of_roi_crossed_lists)
122122
dis = n_fib - len(final_fiber_ids)
123-
iflogger.info("Found %i (%f percent out of %i fibers) fibers that start or terminate in a voxel which is not labeled. (orphans)" % (dis, dis * 100.0 / n_fib, n_fib))
124-
iflogger.info("Valid fibers: %i (%f percent)" % (n_fib - dis, 100 - dis * 100.0 / n_fib))
123+
iflogger.info('Found %i (%f percent out of %i fibers) fibers that start or '
124+
'terminate in a voxel which is not labeled. (orphans)',
125+
dis, dis * 100.0 / n_fib, n_fib)
126+
iflogger.info('Valid fibers: %i (%f percent)', n_fib - dis,
127+
100 - dis * 100.0 / n_fib)
125128
iflogger.info('Returning the intersecting point connectivity matrix')
126129
return connectivity_matrix, final_fiber_ids
127130

@@ -181,7 +184,7 @@ def cmat(track_file, roi_file, resolution_network_file, matrix_name, matrix_mat_
181184
en_fname = op.abspath(endpoint_name + '_endpoints.npy')
182185
en_fnamemm = op.abspath(endpoint_name + '_endpointsmm.npy')
183186

184-
iflogger.info('Reading Trackvis file {trk}'.format(trk=track_file))
187+
iflogger.info('Reading Trackvis file %s', track_file)
185188
fib, hdr = nb.trackvis.read(track_file, False)
186189
stats['orig_n_fib'] = len(fib)
187190

@@ -191,13 +194,13 @@ def cmat(track_file, roi_file, resolution_network_file, matrix_name, matrix_mat_
191194
(endpoints, endpointsmm) = create_endpoints_array(fib, roiVoxelSize)
192195

193196
# Output endpoint arrays
194-
iflogger.info('Saving endpoint array: {array}'.format(array=en_fname))
197+
iflogger.info('Saving endpoint array: %s', en_fname)
195198
np.save(en_fname, endpoints)
196-
iflogger.info('Saving endpoint array in mm: {array}'.format(array=en_fnamemm))
199+
iflogger.info('Saving endpoint array in mm: %s', en_fnamemm)
197200
np.save(en_fnamemm, endpointsmm)
198201

199202
n = len(fib)
200-
iflogger.info('Number of fibers {num}'.format(num=n))
203+
iflogger.info('Number of fibers: %i', n)
201204

202205
# Create empty fiber label array
203206
fiberlabels = np.zeros((n, 2))
@@ -244,7 +247,8 @@ def cmat(track_file, roi_file, resolution_network_file, matrix_name, matrix_mat_
244247
startROI = int(roiData[endpoints[i, 0, 0], endpoints[i, 0, 1], endpoints[i, 0, 2]])
245248
endROI = int(roiData[endpoints[i, 1, 0], endpoints[i, 1, 1], endpoints[i, 1, 2]])
246249
except IndexError:
247-
iflogger.error(("AN INDEXERROR EXCEPTION OCCURED FOR FIBER %s. PLEASE CHECK ENDPOINT GENERATION" % i))
250+
iflogger.error('AN INDEXERROR EXCEPTION OCCURED FOR FIBER %s. '
251+
'PLEASE CHECK ENDPOINT GENERATION', i)
248252
break
249253

250254
# Filter
@@ -256,7 +260,7 @@ def cmat(track_file, roi_file, resolution_network_file, matrix_name, matrix_mat_
256260
if startROI > nROIs or endROI > nROIs:
257261
iflogger.error("Start or endpoint of fiber terminate in a voxel which is labeled higher")
258262
iflogger.error("than is expected by the parcellation node information.")
259-
iflogger.error("Start ROI: %i, End ROI: %i" % (startROI, endROI))
263+
iflogger.error("Start ROI: %i, End ROI: %i", startROI, endROI)
260264
iflogger.error("This needs bugfixing!")
261265
continue
262266

@@ -296,8 +300,10 @@ def cmat(track_file, roi_file, resolution_network_file, matrix_name, matrix_mat_
296300
# make final fiber labels as array
297301
final_fiberlabels_array = np.array(final_fiberlabels, dtype=int)
298302

299-
iflogger.info("Found %i (%f percent out of %i fibers) fibers that start or terminate in a voxel which is not labeled. (orphans)" % (dis, dis * 100.0 / n, n))
300-
iflogger.info("Valid fibers: %i (%f percent)" % (n - dis, 100 - dis * 100.0 / n))
303+
iflogger.info('Found %i (%f percent out of %i fibers) fibers that start or '
304+
'terminate in a voxel which is not labeled. (orphans)',
305+
dis, dis * 100.0 / n, n)
306+
iflogger.info('Valid fibers: %i (%f%%)', n - dis, 100 - dis * 100.0 / n)
301307

302308
numfib = nx.Graph()
303309
numfib.add_nodes_from(G)
@@ -326,7 +332,7 @@ def cmat(track_file, roi_file, resolution_network_file, matrix_name, matrix_mat_
326332
fibmedian.add_edge(u, v, weight=di['fiber_length_median'])
327333
fibdev.add_edge(u, v, weight=di['fiber_length_std'])
328334

329-
iflogger.info('Writing network as {ntwk}'.format(ntwk=matrix_name))
335+
iflogger.info('Writing network as %s', matrix_name)
330336
nx.write_gpickle(G, op.abspath(matrix_name))
331337

332338
numfib_mlab = nx.to_numpy_matrix(numfib, dtype=int)
@@ -341,45 +347,49 @@ def cmat(track_file, roi_file, resolution_network_file, matrix_name, matrix_mat_
341347
if intersections:
342348
path, name, ext = split_filename(matrix_name)
343349
intersection_matrix_name = op.abspath(name + '_intersections') + ext
344-
iflogger.info('Writing intersection network as {ntwk}'.format(ntwk=intersection_matrix_name))
350+
iflogger.info('Writing intersection network as %s', intersection_matrix_name)
345351
nx.write_gpickle(I, intersection_matrix_name)
346352

347353
path, name, ext = split_filename(matrix_mat_name)
348354
if not ext == '.mat':
349355
ext = '.mat'
350356
matrix_mat_name = matrix_mat_name + ext
351357

352-
iflogger.info('Writing matlab matrix as {mat}'.format(mat=matrix_mat_name))
358+
iflogger.info('Writing matlab matrix as %s', matrix_mat_name)
353359
sio.savemat(matrix_mat_name, numfib_dict)
354360

355361
if intersections:
356362
intersect_dict = {'intersections': intersection_matrix}
357363
intersection_matrix_mat_name = op.abspath(name + '_intersections') + ext
358-
iflogger.info('Writing intersection matrix as {mat}'.format(mat=intersection_matrix_mat_name))
364+
iflogger.info('Writing intersection matrix as %s', intersection_matrix_mat_name)
359365
sio.savemat(intersection_matrix_mat_name, intersect_dict)
360366

361367
mean_fiber_length_matrix_name = op.abspath(name + '_mean_fiber_length') + ext
362-
iflogger.info('Writing matlab mean fiber length matrix as {mat}'.format(mat=mean_fiber_length_matrix_name))
368+
iflogger.info('Writing matlab mean fiber length matrix as %s',
369+
mean_fiber_length_matrix_name)
363370
sio.savemat(mean_fiber_length_matrix_name, fibmean_dict)
364371

365372
median_fiber_length_matrix_name = op.abspath(name + '_median_fiber_length') + ext
366-
iflogger.info('Writing matlab median fiber length matrix as {mat}'.format(mat=median_fiber_length_matrix_name))
373+
iflogger.info('Writing matlab median fiber length matrix as %s',
374+
median_fiber_length_matrix_name)
367375
sio.savemat(median_fiber_length_matrix_name, fibmedian_dict)
368376

369377
fiber_length_std_matrix_name = op.abspath(name + '_fiber_length_std') + ext
370-
iflogger.info('Writing matlab fiber length deviation matrix as {mat}'.format(mat=fiber_length_std_matrix_name))
378+
iflogger.info('Writing matlab fiber length deviation matrix as %s',
379+
fiber_length_std_matrix_name)
371380
sio.savemat(fiber_length_std_matrix_name, fibdev_dict)
372381

373382
fiberlengths_fname = op.abspath(endpoint_name + '_final_fiberslength.npy')
374-
iflogger.info("Storing final fiber length array as %s" % fiberlengths_fname)
383+
iflogger.info('Storing final fiber length array as %s', fiberlengths_fname)
375384
np.save(fiberlengths_fname, final_fiberlength_array)
376385

377386
fiberlabels_fname = op.abspath(endpoint_name + '_filtered_fiberslabel.npy')
378-
iflogger.info("Storing all fiber labels (with orphans) as %s" % fiberlabels_fname)
387+
iflogger.info('Storing all fiber labels (with orphans) as %s', fiberlabels_fname)
379388
np.save(fiberlabels_fname, np.array(fiberlabels, dtype=np.int32),)
380389

381390
fiberlabels_noorphans_fname = op.abspath(endpoint_name + '_final_fiberslabels.npy')
382-
iflogger.info("Storing final fiber labels (no orphans) as %s" % fiberlabels_noorphans_fname)
391+
iflogger.info('Storing final fiber labels (no orphans) as %s',
392+
fiberlabels_noorphans_fname)
383393
np.save(fiberlabels_noorphans_fname, final_fiberlabels_array)
384394

385395
iflogger.info("Filtering tractography - keeping only no orphan fibers")
@@ -389,7 +399,7 @@ def cmat(track_file, roi_file, resolution_network_file, matrix_name, matrix_mat_
389399
stats['intersections_percent'] = float(stats['intersections_n_fib']) / float(stats['orig_n_fib']) * 100
390400

391401
out_stats_file = op.abspath(endpoint_name + '_statistics.mat')
392-
iflogger.info("Saving matrix creation statistics as %s" % out_stats_file)
402+
iflogger.info('Saving matrix creation statistics as %s', out_stats_file)
393403
sio.savemat(out_stats_file, stats)
394404

395405

@@ -401,7 +411,7 @@ def save_fibers(oldhdr, oldfib, fname, indices):
401411
outstreams.append(oldfib[i])
402412
n_fib_out = len(outstreams)
403413
hdrnew['n_count'] = n_fib_out
404-
iflogger.info("Writing final non-orphan fibers as %s" % fname)
414+
iflogger.info('Writing final non-orphan fibers as %s', fname)
405415
nb.trackvis.write(fname, outstreams, hdrnew)
406416
return n_fib_out
407417

@@ -620,22 +630,22 @@ class ROIGen(BaseInterface):
620630
def _run_interface(self, runtime):
621631
aparc_aseg_file = self.inputs.aparc_aseg_file
622632
aparcpath, aparcname, aparcext = split_filename(aparc_aseg_file)
623-
iflogger.info('Using Aparc+Aseg file: {name}'.format(name=aparcname + aparcext))
633+
iflogger.info('Using Aparc+Aseg file: %s', aparcname + aparcext)
624634
niiAPARCimg = nb.load(aparc_aseg_file, mmap=NUMPY_MMAP)
625635
niiAPARCdata = niiAPARCimg.get_data()
626636
niiDataLabels = np.unique(niiAPARCdata)
627637
numDataLabels = np.size(niiDataLabels)
628-
iflogger.info('Number of labels in image: {n}'.format(n=numDataLabels))
638+
iflogger.info('Number of labels in image: %s', numDataLabels)
629639

630640
write_dict = True
631641
if self.inputs.use_freesurfer_LUT:
632642
self.LUT_file = self.inputs.freesurfer_dir + '/FreeSurferColorLUT.txt'
633-
iflogger.info('Using Freesurfer LUT: {name}'.format(name=self.LUT_file))
643+
iflogger.info('Using Freesurfer LUT: %s', self.LUT_file)
634644
prefix = 'fsLUT'
635645
elif not self.inputs.use_freesurfer_LUT and isdefined(self.inputs.LUT_file):
636646
self.LUT_file = op.abspath(self.inputs.LUT_file)
637647
lutpath, lutname, lutext = split_filename(self.LUT_file)
638-
iflogger.info('Using Custom LUT file: {name}'.format(name=lutname + lutext))
648+
iflogger.info('Using Custom LUT file: %s', lutname + lutext)
639649
prefix = lutname
640650
else:
641651
prefix = 'hardcoded'
@@ -652,14 +662,14 @@ def _run_interface(self, runtime):
652662
dict_file = op.abspath(prefix + '_' + aparcname + '.pck')
653663

654664
if write_dict:
655-
iflogger.info('Lookup table: {name}'.format(name=op.abspath(self.LUT_file)))
665+
iflogger.info('Lookup table: %s', op.abspath(self.LUT_file))
656666
LUTlabelsRGBA = np.loadtxt(self.LUT_file, skiprows=4, usecols=[0, 1, 2, 3, 4, 5], comments='#',
657667
dtype={'names': ('index', 'label', 'R', 'G', 'B', 'A'), 'formats': ('int', '|S30', 'int', 'int', 'int', 'int')})
658668
numLUTLabels = np.size(LUTlabelsRGBA)
659669
if numLUTLabels < numDataLabels:
660670
iflogger.error('LUT file provided does not contain all of the regions in the image')
661671
iflogger.error('Removing unmapped regions')
662-
iflogger.info('Number of labels in LUT: {n}'.format(n=numLUTLabels))
672+
iflogger.info('Number of labels in LUT: %s', numLUTLabels)
663673
LUTlabelDict = {}
664674

665675
""" Create dictionary for input LUT table"""
@@ -687,7 +697,7 @@ def _run_interface(self, runtime):
687697
iflogger.info('Grey matter mask created')
688698
greyMaskLabels = np.unique(niiGM)
689699
numGMLabels = np.size(greyMaskLabels)
690-
iflogger.info('Number of grey matter labels: {num}'.format(num=numGMLabels))
700+
iflogger.info('Number of grey matter labels: %s', numGMLabels)
691701

692702
labelDict = {}
693703
GMlabelDict = {}
@@ -697,7 +707,7 @@ def _run_interface(self, runtime):
697707
if write_dict:
698708
GMlabelDict['originalID'] = mapDict[label]
699709
except:
700-
iflogger.info('Label {lbl} not in provided mapping'.format(lbl=label))
710+
iflogger.info('Label %s not in provided mapping', label)
701711
if write_dict:
702712
del GMlabelDict
703713
GMlabelDict = {}
@@ -708,11 +718,11 @@ def _run_interface(self, runtime):
708718

709719
roi_image = nb.Nifti1Image(niiGM, niiAPARCimg.affine,
710720
niiAPARCimg.header)
711-
iflogger.info('Saving ROI File to {path}'.format(path=roi_file))
721+
iflogger.info('Saving ROI File to %s', roi_file)
712722
nb.save(roi_image, roi_file)
713723

714724
if write_dict:
715-
iflogger.info('Saving Dictionary File to {path} in Pickle format'.format(path=dict_file))
725+
iflogger.info('Saving Dictionary File to %s in Pickle format', dict_file)
716726
with open(dict_file, 'w') as f:
717727
pickle.dump(labelDict, f)
718728
return runtime
@@ -785,7 +795,7 @@ class CreateNodes(BaseInterface):
785795
def _run_interface(self, runtime):
786796
iflogger.info('Creating nodes...')
787797
create_nodes(self.inputs.roi_file, self.inputs.resolution_network_file, self.inputs.out_filename)
788-
iflogger.info('Saving node network to {path}'.format(path=op.abspath(self.inputs.out_filename)))
798+
iflogger.info('Saving node network to %s', op.abspath(self.inputs.out_filename))
789799
return runtime
790800

791801
def _list_outputs(self):

nipype/interfaces/cmtk/nbs.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ def _run_interface(self, runtime):
118118
node_ntwk_name = self.inputs.in_group1[0]
119119

120120
node_network = nx.read_gpickle(node_ntwk_name)
121-
iflogger.info('Populating node dictionaries with attributes from {node}'.format(node=node_ntwk_name))
121+
iflogger.info('Populating node dictionaries with attributes from %s',
122+
node_ntwk_name)
122123

123124
for nid, ndata in node_network.nodes(data=True):
124125
nbsgraph.nodes[nid] = ndata
@@ -127,12 +128,12 @@ def _run_interface(self, runtime):
127128
path = op.abspath('NBS_Result_' + details)
128129
iflogger.info(path)
129130
nx.write_gpickle(nbsgraph, path)
130-
iflogger.info('Saving output NBS edge network as {out}'.format(out=path))
131+
iflogger.info('Saving output NBS edge network as %s', path)
131132

132133
pval_path = op.abspath('NBS_P_vals_' + details)
133134
iflogger.info(pval_path)
134135
nx.write_gpickle(nbs_pval_graph, pval_path)
135-
iflogger.info('Saving output p-value network as {out}'.format(out=pval_path))
136+
iflogger.info('Saving output p-value network as %s', pval_path)
136137
return runtime
137138

138139
def _list_outputs(self):

0 commit comments

Comments
 (0)