Skip to content

[ENH] Implement missing inputs/outputs in avscale #1563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CHANGES
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
Upcoming release 0.13
=====================

* ENH: Implement missing inputs/outputs in FSL AvScale
(https://github.com/nipy/nipype/pull/1563)


Release 0.12.1 (August 3, 2016)
==============================
===============================

* FIX: runtime profiling is optional and off by default (https://github.com/nipy/nipype/pull/1561)
* TST: circle CI tests run with docker (https://github.com/nipy/nipype/pull/1541)
* FIX: workflow export functions without import error (https://github.com/nipy/nipype/pull/1552)


Release 0.12.0 (July 12, 2016)
==============================

Expand Down
2 changes: 1 addition & 1 deletion nipype/interfaces/fsl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .model import (Level1Design, FEAT, FEATModel, FILMGLS, FEATRegister,
FLAMEO, ContrastMgr, MultipleRegressDesign, L2Model, SMM,
MELODIC, SmoothEstimate, Cluster, Randomise, GLM)
from .utils import (Smooth, Merge, ExtractROI, Split, ImageMaths, ImageMeants,
from .utils import (AvScale, Smooth, Merge, ExtractROI, Split, ImageMaths, ImageMeants,
ImageStats, FilterRegressor, Overlay, Slicer,
PlotTimeSeries, PlotMotionParams, ConvertXFM,
SwapDimensions, PowerSpectrum, Reorient2Std,
Expand Down
11 changes: 9 additions & 2 deletions nipype/interfaces/fsl/tests/test_auto_AvScale.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@


def test_AvScale_inputs():
input_map = dict(args=dict(argstr='%s',
input_map = dict(all_param=dict(argstr='--allparams',
),
args=dict(argstr='%s',
),
environ=dict(nohash=True,
usedefault=True,
Expand All @@ -13,9 +15,12 @@ def test_AvScale_inputs():
usedefault=True,
),
mat_file=dict(argstr='%s',
position=0,
position=-2,
),
output_type=dict(),
ref_file=dict(argstr='%s',
position=-1,
),
terminal_output=dict(nohash=True,
),
)
Expand All @@ -32,9 +37,11 @@ def test_AvScale_outputs():
determinant=dict(),
forward_half_transform=dict(),
left_right_orientation_preserved=dict(),
rot_angles=dict(),
rotation_translation_matrix=dict(),
scales=dict(),
skews=dict(),
translations=dict(),
)
outputs = AvScale.output_spec()

Expand Down
99 changes: 60 additions & 39 deletions nipype/interfaces/fsl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import os
import os.path as op
import re
from glob import glob
import warnings
import tempfile
Expand Down Expand Up @@ -608,25 +609,32 @@ def aggregate_outputs(self, runtime=None, needed_outputs=None):
return outputs


class AvScaleInputSpec(FSLCommandInputSpec):
mat_file = File(exists=True, argstr="%s",
desc='mat file to read', position=0)
class AvScaleInputSpec(CommandLineInputSpec):
all_param = traits.Bool(False, argstr='--allparams')
mat_file = File(exists=True, argstr='%s',
desc='mat file to read', position=-2)
ref_file = File(exists=True, argstr='%s', position=-1,
desc='reference file to get center of rotation')


class AvScaleOutputSpec(TraitedSpec):
rotation_translation_matrix = traits.Any(
desc='Rotation and Translation Matrix')
scales = traits.Any(desc='Scales (x,y,z)')
skews = traits.Any(desc='Skews')
average_scaling = traits.Any(desc='Average Scaling')
determinant = traits.Any(desc='Determinant')
forward_half_transform = traits.Any(desc='Forward Half Transform')
backward_half_transform = traits.Any(desc='Backwards Half Transform')
rotation_translation_matrix = traits.List(
traits.List(traits.Float), desc='Rotation and Translation Matrix')
scales = traits.List(traits.Float, desc='Scales (x,y,z)')
skews = traits.List(traits.Float, desc='Skews')
average_scaling = traits.Float(desc='Average Scaling')
determinant = traits.Float(desc='Determinant')
forward_half_transform = traits.List(
traits.List(traits.Float), desc='Forward Half Transform')
backward_half_transform = traits.List(
traits.List(traits.Float), desc='Backwards Half Transform')
left_right_orientation_preserved = traits.Bool(
desc='True if LR orientation preserved')
rot_angles = traits.List(traits.Float, desc='rotation angles')
translations = traits.List(traits.Float, desc='translations')


class AvScale(FSLCommand):
class AvScale(CommandLine):
"""Use FSL avscale command to extract info from mat file output of FLIRT

Examples
Expand All @@ -643,34 +651,47 @@ class AvScale(FSLCommand):

_cmd = 'avscale'

def _format_arg(self, name, trait_spec, value):
return super(AvScale, self)._format_arg(name, trait_spec, value)

def aggregate_outputs(self, runtime=None, needed_outputs=None):
outputs = self._outputs()

def lines_to_float(lines):
out = []
for line in lines:
values = line.split()
out.append([float(val) for val in values])
return out

out = runtime.stdout.split('\n')

outputs.rotation_translation_matrix = lines_to_float(out[1:5])
outputs.scales = lines_to_float([out[6].split(" = ")[1]])
outputs.skews = lines_to_float([out[8].split(" = ")[1]])
outputs.average_scaling = lines_to_float([out[10].split(" = ")[1]])
outputs.determinant = lines_to_float([out[12].split(" = ")[1]])
if out[13].split(": ")[1] == 'preserved':
outputs.left_right_orientation_preserved = True
else:
outputs.left_right_orientation_preserved = False
outputs.forward_half_transform = lines_to_float(out[16:20])
outputs.backward_half_transform = lines_to_float(out[22:-1])
def _run_interface(self, runtime):
runtime = super(AvScale, self)._run_interface(runtime)


expr = re.compile(
'Rotation\ &\ Translation\ Matrix:\n(?P<rot_tran_mat>[0-9\.\ \n-]+)[\s\n]*'
'(Rotation\ Angles\ \(x,y,z\)\ \[rads\]\ =\ (?P<rot_angles>[0-9\.\ -]+))?[\s\n]*'
'(Translations\ \(x,y,z\)\ \[mm\]\ =\ (?P<translations>[0-9\.\ -]+))?[\s\n]*'
'Scales\ \(x,y,z\)\ =\ (?P<scales>[0-9\.\ -]+)[\s\n]*'
'Skews\ \(xy,xz,yz\)\ =\ (?P<skews>[0-9\.\ -]+)[\s\n]*'
'Average\ scaling\ =\ (?P<avg_scaling>[0-9\.-]+)[\s\n]*'
'Determinant\ =\ (?P<determinant>[0-9\.-]+)[\s\n]*'
'Left-Right\ orientation:\ (?P<lr_orientation>[A-Za-z]+)[\s\n]*'
'Forward\ half\ transform\ =[\s]*\n'
'(?P<fwd_half_xfm>[0-9\.\ \n-]+)[\s\n]*'
'Backward\ half\ transform\ =[\s]*\n'
'(?P<bwd_half_xfm>[0-9\.\ \n-]+)[\s\n]*')
out = expr.search(runtime.stdout).groupdict()
outputs = {}
outputs['rotation_translation_matrix'] = [[
float(v) for v in r.strip().split(' ')] for r in out['rot_tran_mat'].strip().split('\n')]
outputs['scales'] = [float(s) for s in out['scales'].strip().split(' ')]
outputs['skews'] = [float(s) for s in out['skews'].strip().split(' ')]
outputs['average_scaling'] = float(out['avg_scaling'].strip())
outputs['determinant'] = float(out['determinant'].strip())
outputs['left_right_orientation_preserved'] = out['lr_orientation'].strip() == 'preserved'
outputs['forward_half_transform'] = [[
float(v) for v in r.strip().split(' ')] for r in out['fwd_half_xfm'].strip().split('\n')]
outputs['backward_half_transform'] = [[
float(v) for v in r.strip().split(' ')] for r in out['bwd_half_xfm'].strip().split('\n')]

if self.inputs.all_param:
outputs['rot_angles'] = [float(r) for r in out['rot_angles'].strip().split(' ')]
outputs['translations'] = [float(r) for r in out['translations'].strip().split(' ')]


setattr(self, '_results', outputs)
return runtime

return outputs
def _list_outputs(self):
return self._results


class OverlayInputSpec(FSLCommandInputSpec):
Expand Down