|
| 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 - code@oscaresteban.es |
| 7 | +# @Date: 2014-06-02 12:06:50 |
| 8 | +# @Last Modified by: oesteban |
| 9 | +# @Last Modified time: 2014-06-17 10:19:23 |
| 10 | +""" |
| 11 | +Interfaces to perform image registrations and to apply the resulting |
| 12 | +displacement maps to images and points. |
| 13 | +
|
| 14 | +""" |
| 15 | + |
| 16 | +import os.path as op |
| 17 | +import re |
| 18 | + |
| 19 | +from ..base import (CommandLine, CommandLineInputSpec, isdefined, |
| 20 | + TraitedSpec, File, traits, InputMultiPath) |
| 21 | + |
| 22 | + |
| 23 | +from base import ElastixBaseInputSpec |
| 24 | + |
| 25 | +from ... import logging |
| 26 | +logger = logging.getLogger('interface') |
| 27 | + |
| 28 | + |
| 29 | +class RegistrationInputSpec(ElastixBaseInputSpec): |
| 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 | + parameters = InputMultiPath(File(exists=True), mandatory=True, argstr='-p %s...', |
| 36 | + desc='parameter file, elastix handles 1 or more -p') |
| 37 | + |
| 38 | + fixed_mask = File(exists=True, argstr='-fMask %s', desc='mask for fixed image') |
| 39 | + moving_mask = File(exists=True, argstr='-mMask %s', desc='mask for moving image') |
| 40 | + initial_transform = File(exists=True, argstr='-t0 %s', |
| 41 | + desc='parameter file for initial transform') |
| 42 | + |
| 43 | + |
| 44 | +class RegistrationOutputSpec(TraitedSpec): |
| 45 | + transform = InputMultiPath(File(exists=True), desc='output transform') |
| 46 | + warped_file = File(desc='input moving image warped to fixed image') |
| 47 | + warped_files = InputMultiPath(File(exists=False), |
| 48 | + desc=('input moving image warped to fixed image at each level')) |
| 49 | + warped_files_flags = traits.List(traits.Bool(False), |
| 50 | + desc='flag indicating if warped image was generated') |
| 51 | + |
| 52 | + |
| 53 | +class Registration(CommandLine): |
| 54 | + """Elastix nonlinear registration interface |
| 55 | +
|
| 56 | + Example |
| 57 | + ------- |
| 58 | +
|
| 59 | + >>> from nipype.interfaces.elastix import Registration |
| 60 | + >>> reg = Registration() |
| 61 | + >>> reg.inputs.fixed_image = 'fixed1.nii' |
| 62 | + >>> reg.inputs.moving_image = 'moving1.nii' |
| 63 | + >>> reg.inputs.parameters = ['elastix.txt'] |
| 64 | + >>> reg.cmdline |
| 65 | + 'elastix -f fixed1.nii -m moving1.nii -out ./ -p elastix.txt' |
| 66 | + """ |
| 67 | + |
| 68 | + _cmd = 'elastix' |
| 69 | + input_spec = RegistrationInputSpec |
| 70 | + output_spec = RegistrationOutputSpec |
| 71 | + |
| 72 | + def _list_outputs(self): |
| 73 | + outputs = self._outputs().get() |
| 74 | + |
| 75 | + out_dir = op.abspath(self.inputs.output_path) |
| 76 | + |
| 77 | + opts = [ 'WriteResultImage', 'ResultImageFormat' ] |
| 78 | + regex = re.compile(r'^\((\w+)\s(.+)\)$') |
| 79 | + |
| 80 | + outputs['transform'] = [] |
| 81 | + outputs['warped_files'] = [] |
| 82 | + outputs['warped_files_flags'] = [] |
| 83 | + |
| 84 | + for i,params in enumerate(self.inputs.parameters): |
| 85 | + config = {} |
| 86 | + |
| 87 | + with open(params, 'r') as f: |
| 88 | + for line in f.readlines(): |
| 89 | + line = line.strip() |
| 90 | + if not line.startswith('//') and line: |
| 91 | + m = regex.search(line) |
| 92 | + if m: |
| 93 | + value = self._cast(m.group(2).strip()) |
| 94 | + config[m.group(1).strip()] = value |
| 95 | + |
| 96 | + outputs['transform'].append(op.join(out_dir, |
| 97 | + 'TransformParameters.%01d.txt' % i )) |
| 98 | + |
| 99 | + warped_file = None |
| 100 | + if config['WriteResultImage']: |
| 101 | + warped_file = op.join(out_dir, |
| 102 | + 'result.%01d.%s' %(i,config['ResultImageFormat'])) |
| 103 | + |
| 104 | + outputs['warped_files'].append(warped_file) |
| 105 | + outputs['warped_files_flags'].append(config['WriteResultImage']) |
| 106 | + |
| 107 | + if outputs['warped_files_flags'][-1]: |
| 108 | + outputs['warped_file'] = outputs['warped_files'][-1] |
| 109 | + |
| 110 | + return outputs |
| 111 | + |
| 112 | + |
| 113 | + def _cast(self,val): |
| 114 | + if val.startswith('"') and val.endswith('"'): |
| 115 | + if val == '"true"': |
| 116 | + return True |
| 117 | + elif val == '"false"': |
| 118 | + return False |
| 119 | + else: |
| 120 | + return val[1:-1] |
| 121 | + |
| 122 | + try: |
| 123 | + return int(val) |
| 124 | + except ValueError: |
| 125 | + try: |
| 126 | + return float(val) |
| 127 | + except ValueError: |
| 128 | + return val |
| 129 | + |
| 130 | +class ApplyWarpInputSpec(ElastixBaseInputSpec): |
| 131 | + transform_file = File(exists=True, mandatory=True, argstr='-tp %s', |
| 132 | + desc='transform-parameter file, only 1') |
| 133 | + |
| 134 | + moving_image = File(exists=True, argstr='-in %s', mandatory=True, |
| 135 | + desc='input image to deform') |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | +class ApplyWarpOutputSpec(TraitedSpec): |
| 140 | + warped_file = File(desc='input moving image warped to fixed image') |
| 141 | + |
| 142 | +class ApplyWarp(CommandLine): |
| 143 | + """Use `transformix` to apply a transform on an input image. |
| 144 | + The transform is specified in the transform-parameter file. |
| 145 | +
|
| 146 | + Example:: |
| 147 | +
|
| 148 | + >>> from nipype.interfaces.elastix import ApplyWarp |
| 149 | + >>> reg = ApplyWarp() |
| 150 | + >>> reg.inputs.moving_image = 'moving1.nii' |
| 151 | + >>> reg.inputs.transform_file = 'TransformParameters.0.txt' |
| 152 | + >>> reg.cmdline |
| 153 | + 'transformix -in moving1.nii -out ./ -tp TransformParameters.0.txt' |
| 154 | + """ |
| 155 | + |
| 156 | + _cmd = 'transformix' |
| 157 | + input_spec = ApplyWarpInputSpec |
| 158 | + output_spec = ApplyWarpOutputSpec |
| 159 | + |
| 160 | + def _list_outputs(self): |
| 161 | + outputs = self._outputs().get() |
| 162 | + out_dir = op.abspath(self.inputs.output_path) |
| 163 | + outputs['warped_file'] = op.join(out_dir,'result.nii.gz') |
| 164 | + return outputs |
| 165 | + |
| 166 | + |
| 167 | +class AnalyzeWarpInputSpec(ElastixBaseInputSpec): |
| 168 | + transform_file = File(exists=True, mandatory=True, argstr='-tp %s', |
| 169 | + desc='transform-parameter file, only 1') |
| 170 | + |
| 171 | + |
| 172 | +class AnalyzeWarpOutputSpec(TraitedSpec): |
| 173 | + disp_field = File(exists=True, desc='displacements field') |
| 174 | + jacdet_map = File(exists=True, desc='det(Jacobian) map') |
| 175 | + jacmat_map = File(exists=True, desc='Jacobian matrix map') |
| 176 | + |
| 177 | +class AnalyzeWarp(CommandLine): |
| 178 | + """Use `transformix` to get details from the input transform (generate |
| 179 | + the corresponding deformation field, generate the determinant of the |
| 180 | + Jacobian map or the Jacobian map itself) |
| 181 | +
|
| 182 | + Example:: |
| 183 | +
|
| 184 | + >>> from nipype.interfaces.elastix import AnalyzeWarp |
| 185 | + >>> reg = AnalyzeWarp() |
| 186 | + >>> reg.inputs.transform_file = 'TransformParameters.0.txt' |
| 187 | + >>> reg.cmdline |
| 188 | + 'transformix -def all -jac all -jacmat all -out ./ -tp TransformParameters.0.txt' |
| 189 | + """ |
| 190 | + |
| 191 | + _cmd = 'transformix -def all -jac all -jacmat all' |
| 192 | + input_spec = AnalyzeWarpInputSpec |
| 193 | + output_spec = AnalyzeWarpOutputSpec |
| 194 | + |
| 195 | + def _list_outputs(self): |
| 196 | + outputs = self._outputs().get() |
| 197 | + out_dir = op.abspath(self.inputs.output_path) |
| 198 | + outputs['disp_field'] = op.join(out_dir,'deformationField.nii.gz') |
| 199 | + outputs['jacdet_map'] = op.join(out_dir,'spatialJacobian.nii.gz') |
| 200 | + outputs['jacmat_map'] = op.join(out_dir,'fullSpatialJacobian.nii.gz') |
| 201 | + return outputs |
| 202 | + |
| 203 | + |
| 204 | +class PointsWarpInputSpec(ElastixBaseInputSpec): |
| 205 | + points_file = File(exists=True, argstr='-def %s', mandatory=True, |
| 206 | + desc='input points (accepts .vtk triangular meshes).') |
| 207 | + transform_file = File(exists=True, mandatory=True, argstr='-tp %s', |
| 208 | + desc='transform-parameter file, only 1') |
| 209 | + |
| 210 | + |
| 211 | + |
| 212 | +class PointsWarpOutputSpec(TraitedSpec): |
| 213 | + warped_file = File(desc='input points displaced in fixed image domain') |
| 214 | + |
| 215 | +class PointsWarp(CommandLine): |
| 216 | + """Use `transformix` to apply a transform on an input point set. |
| 217 | + The transform is specified in the transform-parameter file. |
| 218 | +
|
| 219 | + Example:: |
| 220 | +
|
| 221 | + >>> from nipype.interfaces.elastix import PointsWarp |
| 222 | + >>> reg = PointsWarp() |
| 223 | + >>> reg.inputs.points_file = 'surf1.vtk' |
| 224 | + >>> reg.inputs.transform_file = 'TransformParameters.0.txt' |
| 225 | + >>> reg.cmdline |
| 226 | + 'transformix -out ./ -def surf1.vtk -tp TransformParameters.0.txt' |
| 227 | + """ |
| 228 | + |
| 229 | + _cmd = 'transformix' |
| 230 | + input_spec = PointsWarpInputSpec |
| 231 | + output_spec = PointsWarpOutputSpec |
| 232 | + |
| 233 | + def _list_outputs(self): |
| 234 | + outputs = self._outputs().get() |
| 235 | + out_dir = op.abspath(self.inputs.output_path) |
| 236 | + |
| 237 | + fname, ext = op.splitext(op.basename(self.inputs.points_file)) |
| 238 | + |
| 239 | + outputs['warped_file'] = op.join(out_dir,'outputpoints%s' % ext) |
| 240 | + return outputs |
0 commit comments