3
dG                 @   s@  d Z ddlmZmZmZmZmZ ddlmZm	Z	 ddlm
Z
mZ ddlZd	ZG d
d deZG dd deZG dd de
ZG dd deZG dd deZG dd deZG dd de
ZG dd deZG dd deZG dd de
ZG dd deZG d d! d!eZG d"d# d#e
ZG d$d% d%eZG d&d' d'eZG d(d) d)e
ZG d*d+ d+eZG d,d- d-eZG d.d/ d/e
Z G d0d1 d1eZ!G d2d3 d3eZ"G d4d5 d5e
Z#G d6d7 d7eeZ$G d8d9 d9eeZ%G d:d; d;eeZ&G d<d= d=eeZ'G d>d? d?eeZ(G d@dA dAeeZ)G dBdC dCee#Z*G dDdE dEee Z+dS )Fam  DTITK registration interfaces

DTI-TK developed by Gary Hui Zhang, gary.zhang@ucl.ac.uk
For additional help, visit http://dti-tk.sf.net

The high-dimensional tensor-based DTI registration algorithm

Zhang, H., Avants, B.B, Yushkevich, P.A., Woo, J.H., Wang, S., McCluskey, L.H.,
 Elman, L.B., Melhem, E.R., Gee, J.C., High-dimensional spatial normalization
 of diffusion tensor images improves the detection of white matter differences
 in amyotrophic lateral sclerosis, IEEE Transactions on Medical Imaging,
 26(11):1585-1597, November 2007. PMID: 18041273.

The original piecewise-affine tensor-based DTI registration algorithm at the
core of DTI-TK

Zhang, H., Yushkevich, P.A., Alexander, D.C., Gee, J.C., Deformable
 registration of diffusion tensor MR images with explicit orientation
 optimization, Medical Image Analysis, 10(5):764-785, October 2006. PMID:
 16899392.

   )TraitedSpecCommandLineInputSpectraitsFile	isdefined   )fname_presuffixsplit_filename   )CommandLineDtitkDTITKRenameMixin    NZrestructuredtextc               @   s   e Zd ZedddddddZedddddddZejd	d
dddddddd	Zej	dddddddZ
ejdddddddZeddddddZdS )RigidInputSpeczfixed tensor volumeTr   z%sF)descexists	mandatorypositionargstrcopyfilezmoving tensor volumer
   ZEDSZGDSZDDSZNMIr   zsimilarity metric)r   r   r   r   
usedefault   r   z%g %g %gz%dist between samp points (mm) (x,y,z))r   r   r   r   r   z%gzcost function toleranceg{Gz?)r   r   r   r   default_valuer   zInitialize w/DTITK-FORMATaffine   )r   r   r   r   r   N)r   r   r   )__name__
__module____qualname__r   
fixed_filemoving_filer   EnumZsimilarity_metricTupleZsampling_xyzFloatftolZinitialize_xfm r"   r"   F/tmp/pip-build-7vycvbft/nipype/nipype/interfaces/dtitk/registration.pyr   #   sX   r   c               @   s    e Zd ZeddZeddZdS )RigidOutputSpecT)r   N)r   r   r   r   out_fileout_file_xfmr"   r"   r"   r#   r$   X   s   
r$   c                   s4   e Zd ZdZeZeZdZ fddZ	dd Z
  ZS )Rigida  Performs rigid registration between two tensor volumes

    Example
    -------

    >>> from nipype.interfaces import dtitk
    >>> node = dtitk.Rigid()
    >>> node.inputs.fixed_file = 'im1.nii'
    >>> node.inputs.moving_file = 'im2.nii'
    >>> node.inputs.similarity_metric = 'EDS'
    >>> node.inputs.sampling_xyz = (4,4,4)
    >>> node.inputs.ftol = 0.01
    >>> node.cmdline
    'dti_rigid_reg im1.nii im2.nii EDS 4 4 4 0.01'
    >>> node.run() # doctest: +SKIP
    Zdti_rigid_regc                s(   t t| j|}d|jkr$| j| |S )Nz%.aff doesn't exist or can't be opened)superr'   _run_interfacestderrZraise_exception)selfZruntime)	__class__r"   r#   r)   x   s    

zRigid._run_interfacec             C   s:   | j  j }| jj}t|ddd|d< t|dd|d< |S )Nz.affF)suffixuse_extr&   Z_aff)r-   r%   )output_specgetinputsr   r   )r+   outputsmovingr"   r"   r#   _list_outputs~   s
    zRigid._list_outputs)r   r   r   __doc__r   
input_specr$   r/   _cmdr)   r4   __classcell__r"   r"   )r,   r#   r'   ]   s   r'   c               @   s   e Zd ZdZdZdS )Affinea   Performs affine registration between two tensor volumes

    Example
    -------

    >>> from nipype.interfaces import dtitk
    >>> node = dtitk.Affine()
    >>> node.inputs.fixed_file = 'im1.nii'
    >>> node.inputs.moving_file = 'im2.nii'
    >>> node.inputs.similarity_metric = 'EDS'
    >>> node.inputs.sampling_xyz = (4,4,4)
    >>> node.inputs.ftol = 0.01
    >>> node.inputs.initialize_xfm = 'im_affine.aff'
    >>> node.cmdline
    'dti_affine_reg im1.nii im2.nii EDS 4 4 4 0.01 im_affine.aff'
    >>> node.run() # doctest: +SKIP
    Zdti_affine_regN)r   r   r   r5   r7   r"   r"   r"   r#   r9      s   r9   c               @   s   e Zd ZedddddZedddddd	Zed
ddddZejdddddddZ	ej
dddddddZejdddddddZdS )DiffeoInputSpeczfixed tensor volumeTr   z%s)r   r   r   r   zmoving tensor volumer
   F)r   r   r   r   r   maskr   z!legacy parameter; always set to 1r   z%d)r   r   r   r   r      znumber of iterationsr   )r   r   r   r   r   gMb`?z&iteration for the optimization to stopr   z%gN)r   r   r   r   r   r   Z	mask_filer   r   legacyIntZn_itersr    r!   r"   r"   r"   r#   r:      s:   r:   c               @   s    e Zd ZeddZeddZdS )DiffeoOutputSpecT)r   N)r   r   r   r   r%   r&   r"   r"   r"   r#   r?      s   
r?   c               @   s$   e Zd ZdZeZeZdZdd Z	dS )Diffeoa  Performs diffeomorphic registration between two tensor volumes

    Example
    -------

    >>> from nipype.interfaces import dtitk
    >>> node = dtitk.Diffeo()
    >>> node.inputs.fixed_file = 'im1.nii'
    >>> node.inputs.moving_file = 'im2.nii'
    >>> node.inputs.mask_file = 'mask.nii'
    >>> node.inputs.legacy = 1
    >>> node.inputs.n_iters = 6
    >>> node.inputs.ftol = 0.002
    >>> node.cmdline
    'dti_diffeomorphic_reg im1.nii im2.nii mask.nii 1 6 0.002'
    >>> node.run() # doctest: +SKIP
    Zdti_diffeomorphic_regc             C   s8   | j  j }| jj}t|dd|d< t|dd|d< |S )Nz
_diffeo.df)r-   r&   Z_diffeor%   )r/   r0   r1   r   r   )r+   r2   r3   r"   r"   r#   r4      s
    zDiffeo._list_outputsN)
r   r   r   r5   r:   r6   r?   r/   r7   r4   r"   r"   r"   r#   r@      s
   r@   c               @   s:   e Zd ZedddddZedddddZedddd	Zd
S )ComposeXfmInputSpeczdiffeomorphic warp fileTz-df %s)r   r   r   r   zaffine transform filez-aff %szoutput pathz-out %s)r   r   ZgenfileN)r   r   r   r   in_dfZin_affr%   r"   r"   r"   r#   rA      s
   rA   c               @   s   e Zd ZeddZdS )ComposeXfmOutputSpecT)r   N)r   r   r   r   r%   r"   r"   r"   r#   rC      s   rC   c               @   s,   e Zd ZdZeZeZdZdd Z	dd Z
dS )
ComposeXfma  
     Combines diffeomorphic and affine transforms

    Example
    -------

    >>> from nipype.interfaces import dtitk
    >>> node = dtitk.ComposeXfm()
    >>> node.inputs.in_df = 'im_warp.df.nii'
    >>> node.inputs.in_aff= 'im_affine.aff'
    >>> node.cmdline
    'dfRightComposeAffine -aff im_affine.aff -df im_warp.df.nii -out
     im_warp_affdf.df.nii'
    >>> node.run() # doctest: +SKIP
    ZdfRightComposeAffinec             C   s:   | j  j }| jj}t|s&| jd}tjj||d< |S )Nr%   )	Z_outputsr0   r1   r%   r   _gen_filenameospathabspath)r+   r2   r%   r"   r"   r#   r4     s    
zComposeXfm._list_outputsc             C   sR   |dkrd S t | jj\}}}d}|jdr@|d7 }|d d }t||| ddS )Nr%   Z_affdfz.dfr   F)r-   r.   )r	   r1   rB   endswithr   )r+   namerG   baseextr-   r"   r"   r#   rE     s    
zComposeXfm._gen_filenameN)r   r   r   r5   rA   r6   rC   r/   r7   r4   rE   r"   r"   r"   r#   rD      s   rD   c               @   s   e Zd ZedddddZedddddd	Zedd
ddddgddZejddddddZ	ejdddddddZ
edddgddZejej ej ej fdd dgd!Zejej ej ej fd"dgd#d$Zejej fd% d&dgd'd$Zd(S ))AffSymTensor3DVolInputSpeczmoving tensor volumeTz-in %s)r   r   r   r   zoutput filenamez-out %sin_filez
%s_affxfmd)r   r   name_sourcename_templatekeep_extensionz	-trans %stargettranslationeulerdeformationzZtransform to apply: specify an input transformation file; parameters input will be ignored)r   r   xorr   LEIEIz
-interp %sz%Log Euclidean/Euclidean Interpolation)r   r   r   PPDNOFSz-reorient %sz_Reorientation strategy: preservation of principal direction, no reorientation, or finite strain)r   r   r   z
-target %s	transformzDoutput volume specification read from the target volume if specifiedztranslation (x,y,z) in mmz-translation %g %g %g)r   r   rW   z(theta, phi, psi) in degreesz-euler %g %g %g)r   rW   r   r<   z(xx,yy,zz,xy,yz,xz)z-deformation %g %g %g %g %g %gN)r   r   r   r   rO   r%   r]   r   r   interpolationreorientrS   r   r    rT   rU   rV   r"   r"   r"   r#   rN     s\   

rN   c               @   s   e Zd ZeddZdS )AffSymTensor3DVolOutputSpecT)r   N)r   r   r   r   r%   r"   r"   r"   r#   r`   W  s   r`   c               @   s   e Zd ZdZeZeZdZdS )AffSymTensor3DVola  
    Applies affine transform to a tensor volume

    Example
    -------

    >>> from nipype.interfaces import dtitk
    >>> node = dtitk.AffSymTensor3DVol()
    >>> node.inputs.in_file = 'im1.nii'
    >>> node.inputs.transform = 'im_affine.aff'
    >>> node.cmdline
    'affineSymTensor3DVolume -in im1.nii -interp LEI -out im1_affxfmd.nii
     -reorient PPD -trans im_affine.aff'
    >>> node.run() # doctest: +SKIP
    ZaffineSymTensor3DVolumeN)	r   r   r   r5   rN   r6   r`   r/   r7   r"   r"   r"   r#   ra   [  s   ra   c               @   s   e Zd ZedddddZedddddd	Zedd
ddddgddZejddddddZ	edddgddZ
ejej ej ej fdddgdZejej ej ej fddgddZejej fd d dgd!dZd"S )#AffScalarVolInputSpeczmoving scalar volumeTz-in %s)r   r   r   r   zoutput filenamez-out %srO   z
%s_affxfmd)r   r   rP   rQ   rR   z	-trans %srS   rT   rU   rV   zZtransform to apply: specify an input transformation file; parameters input will be ignored)r   r   rW   r   	trilinearNNz
-interp %sz+trilinear or nearest neighbor interpolation)r   r   r   z
-target %sr]   zDoutput volume specification read from the target volume if specifiedztranslation (x,y,z) in mmz-translation %g %g %g)r   r   rW   z(theta, phi, psi) in degreesz-euler %g %g %g)r   rW   r   r<   z(xx,yy,zz,xy,yz,xz)z-deformation %g %g %g %g %g %gN)r   r   r   r   rO   r%   r]   r   r   r^   rS   r   r    rT   rU   rV   r"   r"   r"   r#   rb   q  sN   

rb   c               @   s   e Zd ZedddZdS )AffScalarVolOutputSpeczmoved volumeT)r   r   N)r   r   r   r   r%   r"   r"   r"   r#   re     s   re   c                   s,   e Zd ZdZeZeZdZ fddZ	  Z
S )AffScalarVola  
    Applies affine transform to a scalar volume

    Example
    -------

    >>> from nipype.interfaces import dtitk
    >>> node = dtitk.AffScalarVol()
    >>> node.inputs.in_file = 'im1.nii'
    >>> node.inputs.transform = 'im_affine.aff'
    >>> node.cmdline
    'affineScalarVolume -in im1.nii -interp 0 -out im1_affxfmd.nii -trans
     im_affine.aff'
    >>> node.run() # doctest: +SKIP
    ZaffineScalarVolumec                s*   |dkrddd| }t t| j|||S )Nr^   r   r
   )rc   rd   )r(   rf   _format_arg)r+   rK   specvalue)r,   r"   r#   rg     s    zAffScalarVol._format_arg)r   r   r   r5   rb   r6   re   r/   r7   rg   r8   r"   r"   )r,   r#   rf     s
   rf   c               @   s   e Zd ZedddddZedddddd	Zedd
dddZejddddZ	ej
ddddddZej
ddddddZedddgddZejej ej ej fddd gd!Zejej ej ej fd"d#Zej
d$d%d&d'd(Zd)S )*DiffeoSymTensor3DVolInputSpeczmoving tensor volumeTz-in %s)r   r   r   r   zoutput filenamez-out %srO   z%s_diffeoxfmd)r   r   rP   rQ   rR   z	-trans %sztransform to apply)r   r   r   r   ZFDz-df %s)r   r   rX   rY   z
-interp %sz%Log Euclidean/Euclidean Interpolation)r   r   r   rZ   r\   z-reorient %szLReorientation strategy: preservation of principal direction or finite strain)r   r   r   z
-target %s
voxel_sizezDoutput volume specification read from the target volume if specified)r   r   rW   r   z%xyz voxel size (superseded by target)z-vsize %g %g %grS   )r   r   rW   z-flip %d %d %d)r   backwardforwardz"use backward or forward resamplingz-type %s)r   r   N)r   r   r   r   rO   r%   r]   r   StrZdfr   r^   r_   rS   r   r    rk   r>   flipresampling_typer"   r"   r"   r#   rj     sP   
rj   c               @   s   e Zd ZeddZdS )DiffeoSymTensor3DVolOutputSpecT)r   N)r   r   r   r   r%   r"   r"   r"   r#   rq     s   rq   c                   s,   e Zd ZdZeZeZdZ fddZ	  Z
S )DiffeoSymTensor3DVola  
    Applies diffeomorphic transform to a tensor volume

    Example
    -------

    >>> from nipype.interfaces import dtitk
    >>> node = dtitk.DiffeoSymTensor3DVol()
    >>> node.inputs.in_file = 'im1.nii'
    >>> node.inputs.transform = 'im_warp.df.nii'
    >>> node.cmdline
    'deformationSymTensor3DVolume -df FD -in im1.nii -interp LEI -out
     im1_diffeoxfmd.nii -reorient PPD -trans im_warp.df.nii'
    >>> node.run() # doctest: +SKIP
    ZdeformationSymTensor3DVolumec                s*   |dkrddd| }t t| j|||S )Nrp   r   r
   )rm   rl   )r(   rr   rg   )r+   rK   rh   ri   )r,   r"   r#   rg     s    z DiffeoSymTensor3DVol._format_arg)r   r   r   r5   rj   r6   rq   r/   r7   rg   r8   r"   r"   )r,   r#   rr     s
   rr   c               @   s   e Zd ZedddddZedddddd	Zedd
dddZedddgddZej	ej
 ej
 ej
 fdddgdZej	ej ej ej fddZejdddddZejdddddd Zd!S )"DiffeoScalarVolInputSpeczmoving scalar volumeTz-in %s)r   r   r   r   zoutput filenamez-out %srO   z%s_diffeoxfmd)r   r   rP   rQ   rR   z	-trans %sztransform to apply)r   r   r   r   z
-target %srk   zDoutput volume specification read from the target volume if specified)r   r   rW   r   z%xyz voxel size (superseded by target)z-vsize %g %g %grS   )r   r   rW   z-flip %d %d %d)r   rl   rm   z"use backward or forward resamplingz-type %s)r   r   rc   rd   ztrilinear, or nearest neighborz
-interp %s)r   r   r   N)r   r   r   r   rO   r%   r]   rS   r   r   r    rk   r>   ro   r   rp   r^   r"   r"   r"   r#   rs     sB   
rs   c               @   s   e Zd ZedddZdS )DiffeoScalarVolOutputSpeczmoved volumeT)r   r   N)r   r   r   r   r%   r"   r"   r"   r#   rt   D  s   rt   c                   s,   e Zd ZdZeZeZdZ fddZ	  Z
S )DiffeoScalarVola  
    Applies diffeomorphic transform to a scalar volume

    Example
    -------

    >>> from nipype.interfaces import dtitk
    >>> node = dtitk.DiffeoScalarVol()
    >>> node.inputs.in_file = 'im1.nii'
    >>> node.inputs.transform = 'im_warp.df.nii'
    >>> node.cmdline
    'deformationScalarVolume -in im1.nii -interp 0 -out im1_diffeoxfmd.nii
     -trans im_warp.df.nii'
    >>> node.run() # doctest: +SKIP
    ZdeformationScalarVolumec                sB   |dkrddd| }n|dkr.ddd| }t t| j|||S )Nrp   r   r
   )rm   rl   r^   )rc   rd   )r(   ru   rg   )r+   rK   rh   ri   )r,   r"   r#   rg   ]  s
    zDiffeoScalarVol._format_arg)r   r   r   r5   rs   r6   rt   r/   r7   rg   r8   r"   r"   )r,   r#   ru   H  s
   ru   c               @   s   e Zd ZdS )	RigidTaskN)r   r   r   r"   r"   r"   r#   rv   e  s   rv   c               @   s   e Zd ZdS )
AffineTaskN)r   r   r   r"   r"   r"   r#   rw   i  s   rw   c               @   s   e Zd ZdS )
DiffeoTaskN)r   r   r   r"   r"   r"   r#   rx   m  s   rx   c               @   s   e Zd ZdS )ComposeXfmTaskN)r   r   r   r"   r"   r"   r#   ry   q  s   ry   c               @   s   e Zd ZdS )affScalarVolTaskN)r   r   r   r"   r"   r"   r#   rz   u  s   rz   c               @   s   e Zd ZdS )affSymTensor3DVolTaskN)r   r   r   r"   r"   r"   r#   r{   y  s   r{   c               @   s   e Zd ZdS )diffeoScalarVolTaskN)r   r   r   r"   r"   r"   r#   r|   }  s   r|   c               @   s   e Zd ZdS )diffeoSymTensor3DVolTaskN)r   r   r   r"   r"   r"   r#   r}     s   r}   ),r5   rL   r   r   r   r   r   Zutils.filemanipr   r	   r   r   rF   Z__docformat__r   r$   r'   r9   r:   r?   r@   rA   rC   rD   rN   r`   ra   rb   re   rf   rj   rq   rr   rs   rt   ru   rv   rw   rx   ry   rz   r{   r|   r}   r"   r"   r"   r#   <module>   sF   5)$
(=36,