|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- |
| 4 | +# vi: set ft=python sts=4 ts=4 sw=4 et: |
| 5 | +# |
| 6 | +# @Author: oesteban - [email protected] |
| 7 | +# @Date: 2014-06-02 12:06:50 |
| 8 | +# @Last Modified by: oesteban |
| 9 | +# @Last Modified time: 2014-06-02 14:09:50 |
| 10 | +"""The :py:mod:`nipype.interfaces.elastix` provides the interface to |
| 11 | +the elastix registration software. |
| 12 | +
|
| 13 | +.. note:: http://elastix.isi.uu.nl/ |
| 14 | +
|
| 15 | +
|
| 16 | +""" |
| 17 | + |
| 18 | +import os.path as op |
| 19 | +import re |
| 20 | + |
| 21 | +from ..base import (CommandLine, CommandLineInputSpec, isdefined, |
| 22 | + TraitedSpec, File, traits, InputMultiPath) |
| 23 | + |
| 24 | + |
| 25 | +from ... import logging |
| 26 | +logger = logging.getLogger('interface') |
| 27 | + |
| 28 | + |
| 29 | +class RegistrationInputSpec(CommandLineInputSpec): |
| 30 | + fixed_image = File(exists=True, mandatory=True, argstr='-f %s', |
| 31 | + desc='fixed image') |
| 32 | + moving_image = File(exists=True, mandatory=True, argstr='-m %s', |
| 33 | + desc='moving image') |
| 34 | + |
| 35 | + output_path = traits.Directory('./', exists=True, mandatory=True, usedefault=True, |
| 36 | + argstr='-out %s', desc='output directory') |
| 37 | + |
| 38 | + parameters = InputMultiPath(File(exists=True), mandatory=True, argstr='-p %s...', |
| 39 | + desc='parameter file, elastix handles 1 or more -p') |
| 40 | + |
| 41 | + fixed_mask = File(exists=True, argstr='-fMask %s', desc='mask for fixed image') |
| 42 | + moving_mask = File(exists=True, argstr='-mMask %s', desc='mask for moving image') |
| 43 | + initial_transform = File(exists=True, argstr='-t0 %s', |
| 44 | + desc='parameter file for initial transform') |
| 45 | + num_threads = traits.Int(1, argstr='-threads %01d', |
| 46 | + desc='set the maximum number of threads of elastix') |
| 47 | + |
| 48 | + |
| 49 | +class RegistrationOutputSpec(TraitedSpec): |
| 50 | + transform = InputMultiPath(File(exists=True), desc='output transform') |
| 51 | + warped_file = File(desc='input moving image warped to fixed image') |
| 52 | + warped_files = InputMultiPath(File(), desc=('input moving image warped to' |
| 53 | + ' fixed image at each level')) |
| 54 | + warped_files_flags = traits.List(traits.Bool(False), |
| 55 | + desc='flag indicating if warped image was generated') |
| 56 | + |
| 57 | + |
| 58 | +class Registration(CommandLine): |
| 59 | + """Elastix nonlinear registration interface |
| 60 | +
|
| 61 | + Example |
| 62 | + ------- |
| 63 | +
|
| 64 | + >>> from nipype.interfaces.elastix import Registration |
| 65 | + >>> reg = Registration() |
| 66 | + >>> reg.inputs.fixed_image = 'fixed1.nii' |
| 67 | + >>> reg.inputs.moving_image = 'moving1.nii' |
| 68 | + >>> reg.inputs.parameters = ['elastix.txt'] |
| 69 | + >>> reg.cmdline |
| 70 | + 'elastix -f fixed1.nii -m moving1.nii -p elastix.txt -out ./' |
| 71 | + """ |
| 72 | + |
| 73 | + _cmd = 'elastix' |
| 74 | + input_spec = RegistrationInputSpec |
| 75 | + output_spec = RegistrationOutputSpec |
| 76 | + |
| 77 | + def _list_outputs(self): |
| 78 | + outputs = self._outputs().get() |
| 79 | + |
| 80 | + out_dir = op.abspath(self.inputs.output_path) |
| 81 | + |
| 82 | + opts = [ 'WriteResultImage', 'ResultImageFormat' ] |
| 83 | + regex = re.compile(r'^\((\w+)\s(.+)\)$') |
| 84 | + |
| 85 | + outputs['transform'] = [] |
| 86 | + outputs['warped_files'] = [] |
| 87 | + outputs['warped_files_flags'] = [] |
| 88 | + |
| 89 | + for i,params in enumerate(self.inputs.parameters): |
| 90 | + config = {} |
| 91 | + |
| 92 | + with open(params, 'r') as f: |
| 93 | + for line in f.readlines(): |
| 94 | + line = line.strip() |
| 95 | + if not line.startswith('//') and line: |
| 96 | + m = regex.search(line) |
| 97 | + if m: |
| 98 | + value = self._cast(m.group(2).strip()) |
| 99 | + config[m.group(1).strip()] = value |
| 100 | + |
| 101 | + outputs['transform'].append(op.join(out_dir, |
| 102 | + 'TransformParameters.%01d.txt' % i )) |
| 103 | + |
| 104 | + warped_file = None |
| 105 | + if config['WriteResultImage']: |
| 106 | + warped_file = op.join(out_dir, |
| 107 | + 'result.%01d.%s' %(i,config['ResultImageFormat'])) |
| 108 | + |
| 109 | + outputs['warped_files'].append(warped_file) |
| 110 | + outputs['warped_files_flags'].append(config['WriteResultImage']) |
| 111 | + |
| 112 | + if outputs['warped_files_flags'][-1]: |
| 113 | + outputs['warped_file'] = outputs['warped_files'][-1] |
| 114 | + |
| 115 | + return outputs |
| 116 | + |
| 117 | + |
| 118 | + def _cast(self,val): |
| 119 | + if val.startswith('"') and val.endswith('"'): |
| 120 | + if val == '"true"': |
| 121 | + return True |
| 122 | + elif val == '"false"': |
| 123 | + return False |
| 124 | + else: |
| 125 | + return val[1:-1] |
| 126 | + |
| 127 | + try: |
| 128 | + return int(val) |
| 129 | + except ValueError: |
| 130 | + try: |
| 131 | + return float(val) |
| 132 | + except ValueError: |
| 133 | + return val |
0 commit comments