Skip to content

Commit 12f9073

Browse files
committed
[ENH] Add "profiling" outputs to ants.Registration
Adds a `profiling` flag that enables parsing of the standard output from ANTS. This first version only reads two values: the total elapsed time and the final value of the metric.
1 parent d82a18f commit 12f9073

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

nipype/interfaces/ants/registration.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,8 @@ class RegistrationInputSpec(ANTSCommandInputSpec):
379379
low=0.0, high=1.0, value=0.0, argstr='%s', usedefault=True, desc="The Lower quantile to clip image ranges")
380380

381381
verbose = traits.Bool(argstr='-v', default=False)
382+
profiling = traits.Bool(True, usedefault=True,
383+
desc='generate profiling output fields')
382384

383385

384386
class RegistrationOutputSpec(TraitedSpec):
@@ -395,6 +397,8 @@ class RegistrationOutputSpec(TraitedSpec):
395397
warped_image = File(desc="Outputs warped image")
396398
inverse_warped_image = File(desc="Outputs the inverse of the warped image")
397399
save_state = File(desc="The saved registration state to be restored")
400+
metric_value = traits.Float(desc='the final value of metric')
401+
elapsed_time = traits.Float(desc='the total elapsed time as reported by ANTs')
398402

399403

400404
class Registration(ANTSCommand):
@@ -648,6 +652,24 @@ class Registration(ANTSCommand):
648652
_quantilesDone = False
649653
_linear_transform_names = ['Rigid', 'Affine', 'Translation', 'CompositeAffine', 'Similarity']
650654

655+
def _run_interface(self, runtime, correct_return_codes=(0,)):
656+
runtime = super(Registration, self)._run_interface(runtime)
657+
658+
# Parse some profiling info
659+
if self.inputs.profiling:
660+
lines = runtime.stdout.split('\n')
661+
for l in lines[::-1]:
662+
# This should be the last line
663+
if l.strip().startswith('Total elapsed time:'):
664+
setattr(self, '_elapsed_time', float(
665+
l.strip().replace('Total elapsed time: ', '')))
666+
elif 'DIAGNOSTIC' in l:
667+
setattr(self, '_metric_value', float(
668+
l.split(',')[2]))
669+
break
670+
671+
return runtime
672+
651673
def _format_metric(self, index):
652674
"""
653675
Format the antsRegistration -m metric argument(s).
@@ -981,4 +1003,7 @@ def _list_outputs(self):
9811003
outputs['inverse_warped_image'] = os.path.abspath(inv_out_filename)
9821004
if len(self.inputs.save_state):
9831005
outputs['save_state'] = os.path.abspath(self.inputs.save_state)
1006+
if self.inputs.profiling:
1007+
outputs['metric_value'] = getattr(self, '_metric_value')
1008+
outputs['elapsed_time'] = getattr(self, '_elapsed_time')
9841009
return outputs

nipype/interfaces/ants/tests/test_auto_Registration.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ def test_Registration_inputs():
7272
),
7373
output_warped_image=dict(hash_files=False,
7474
),
75+
profiling=dict(usedefault=True,
76+
),
7577
radius_bins_item_trait=dict(),
7678
radius_bins_stage_trait=dict(),
7779
radius_or_number_of_bins=dict(requires=['metric_weight'],
@@ -125,10 +127,12 @@ def test_Registration_inputs():
125127

126128
def test_Registration_outputs():
127129
output_map = dict(composite_transform=dict(),
130+
elapsed_time=dict(),
128131
forward_invert_flags=dict(),
129132
forward_transforms=dict(),
130133
inverse_composite_transform=dict(),
131134
inverse_warped_image=dict(),
135+
metric_value=dict(),
132136
reverse_invert_flags=dict(),
133137
reverse_transforms=dict(),
134138
save_state=dict(),

0 commit comments

Comments
 (0)