22
22
23
23
import os
24
24
import os .path as op
25
+ import re
25
26
from glob import glob
26
27
import warnings
27
28
import tempfile
@@ -608,25 +609,32 @@ def aggregate_outputs(self, runtime=None, needed_outputs=None):
608
609
return outputs
609
610
610
611
611
- class AvScaleInputSpec (FSLCommandInputSpec ):
612
- mat_file = File (exists = True , argstr = "%s" ,
613
- desc = 'mat file to read' , position = 0 )
612
+ class AvScaleInputSpec (CommandLineInputSpec ):
613
+ all_param = traits .Bool (False , argstr = '--allparams' )
614
+ mat_file = File (exists = True , argstr = '%s' ,
615
+ desc = 'mat file to read' , position = - 2 )
616
+ ref_file = File (exists = True , argstr = '%s' , position = - 1 ,
617
+ desc = 'reference file to get center of rotation' )
614
618
615
619
616
620
class AvScaleOutputSpec (TraitedSpec ):
617
- rotation_translation_matrix = traits .Any (
618
- desc = 'Rotation and Translation Matrix' )
619
- scales = traits .Any (desc = 'Scales (x,y,z)' )
620
- skews = traits .Any (desc = 'Skews' )
621
- average_scaling = traits .Any (desc = 'Average Scaling' )
622
- determinant = traits .Any (desc = 'Determinant' )
623
- forward_half_transform = traits .Any (desc = 'Forward Half Transform' )
624
- backward_half_transform = traits .Any (desc = 'Backwards Half Transform' )
621
+ rotation_translation_matrix = traits .List (
622
+ traits .List (traits .Float ), desc = 'Rotation and Translation Matrix' )
623
+ scales = traits .List (traits .Float , desc = 'Scales (x,y,z)' )
624
+ skews = traits .List (traits .Float , desc = 'Skews' )
625
+ average_scaling = traits .Float (desc = 'Average Scaling' )
626
+ determinant = traits .Float (desc = 'Determinant' )
627
+ forward_half_transform = traits .List (
628
+ traits .List (traits .Float ), desc = 'Forward Half Transform' )
629
+ backward_half_transform = traits .List (
630
+ traits .List (traits .Float ), desc = 'Backwards Half Transform' )
625
631
left_right_orientation_preserved = traits .Bool (
626
632
desc = 'True if LR orientation preserved' )
633
+ rot_angles = traits .List (traits .Float , desc = 'rotation angles' )
634
+ translations = traits .List (traits .Float , desc = 'translations' )
627
635
628
636
629
- class AvScale (FSLCommand ):
637
+ class AvScale (CommandLine ):
630
638
"""Use FSL avscale command to extract info from mat file output of FLIRT
631
639
632
640
Examples
@@ -643,34 +651,47 @@ class AvScale(FSLCommand):
643
651
644
652
_cmd = 'avscale'
645
653
646
- def _format_arg (self , name , trait_spec , value ):
647
- return super (AvScale , self )._format_arg (name , trait_spec , value )
648
-
649
- def aggregate_outputs (self , runtime = None , needed_outputs = None ):
650
- outputs = self ._outputs ()
651
-
652
- def lines_to_float (lines ):
653
- out = []
654
- for line in lines :
655
- values = line .split ()
656
- out .append ([float (val ) for val in values ])
657
- return out
658
-
659
- out = runtime .stdout .split ('\n ' )
660
-
661
- outputs .rotation_translation_matrix = lines_to_float (out [1 :5 ])
662
- outputs .scales = lines_to_float ([out [6 ].split (" = " )[1 ]])
663
- outputs .skews = lines_to_float ([out [8 ].split (" = " )[1 ]])
664
- outputs .average_scaling = lines_to_float ([out [10 ].split (" = " )[1 ]])
665
- outputs .determinant = lines_to_float ([out [12 ].split (" = " )[1 ]])
666
- if out [13 ].split (": " )[1 ] == 'preserved' :
667
- outputs .left_right_orientation_preserved = True
668
- else :
669
- outputs .left_right_orientation_preserved = False
670
- outputs .forward_half_transform = lines_to_float (out [16 :20 ])
671
- outputs .backward_half_transform = lines_to_float (out [22 :- 1 ])
654
+ def _run_interface (self , runtime ):
655
+ runtime = super (AvScale , self )._run_interface (runtime )
656
+
657
+
658
+ expr = re .compile (
659
+ 'Rotation\ &\ Translation\ Matrix:\n (?P<rot_tran_mat>[0-9\.\ \n -]+)[\s\n ]*'
660
+ '(Rotation\ Angles\ \(x,y,z\)\ \[rads\]\ =\ (?P<rot_angles>[0-9\.\ -]+))?[\s\n ]*'
661
+ '(Translations\ \(x,y,z\)\ \[mm\]\ =\ (?P<translations>[0-9\.\ -]+))?[\s\n ]*'
662
+ 'Scales\ \(x,y,z\)\ =\ (?P<scales>[0-9\.\ -]+)[\s\n ]*'
663
+ 'Skews\ \(xy,xz,yz\)\ =\ (?P<skews>[0-9\.\ -]+)[\s\n ]*'
664
+ 'Average\ scaling\ =\ (?P<avg_scaling>[0-9\.-]+)[\s\n ]*'
665
+ 'Determinant\ =\ (?P<determinant>[0-9\.-]+)[\s\n ]*'
666
+ 'Left-Right\ orientation:\ (?P<lr_orientation>[A-Za-z]+)[\s\n ]*'
667
+ 'Forward\ half\ transform\ =[\s]*\n '
668
+ '(?P<fwd_half_xfm>[0-9\.\ \n -]+)[\s\n ]*'
669
+ 'Backward\ half\ transform\ =[\s]*\n '
670
+ '(?P<bwd_half_xfm>[0-9\.\ \n -]+)[\s\n ]*' )
671
+ out = expr .search (runtime .stdout ).groupdict ()
672
+ outputs = {}
673
+ outputs ['rotation_translation_matrix' ] = [[
674
+ float (v ) for v in r .strip ().split (' ' )] for r in out ['rot_tran_mat' ].strip ().split ('\n ' )]
675
+ outputs ['scales' ] = [float (s ) for s in out ['scales' ].strip ().split (' ' )]
676
+ outputs ['skews' ] = [float (s ) for s in out ['skews' ].strip ().split (' ' )]
677
+ outputs ['average_scaling' ] = float (out ['avg_scaling' ].strip ())
678
+ outputs ['determinant' ] = float (out ['determinant' ].strip ())
679
+ outputs ['left_right_orientation_preserved' ] = out ['lr_orientation' ].strip () == 'preserved'
680
+ outputs ['forward_half_transform' ] = [[
681
+ float (v ) for v in r .strip ().split (' ' )] for r in out ['fwd_half_xfm' ].strip ().split ('\n ' )]
682
+ outputs ['backward_half_transform' ] = [[
683
+ float (v ) for v in r .strip ().split (' ' )] for r in out ['bwd_half_xfm' ].strip ().split ('\n ' )]
684
+
685
+ if self .inputs .all_param :
686
+ outputs ['rot_angles' ] = [float (r ) for r in out ['rot_angles' ].strip ().split (' ' )]
687
+ outputs ['translations' ] = [float (r ) for r in out ['translations' ].strip ().split (' ' )]
688
+
689
+
690
+ setattr (self , '_results' , outputs )
691
+ return runtime
672
692
673
- return outputs
693
+ def _list_outputs (self ):
694
+ return self ._results
674
695
675
696
676
697
class OverlayInputSpec (FSLCommandInputSpec ):
0 commit comments