3
XdN[                 @   s  d Z ddlZddlmZmZ ddlmZmZmZmZm	Z	 ddl
m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dS )!z ANTS Apply Transforms interface
    N   )ANTSCommandANTSCommandInputSpec   )TraitedSpecFiletraits	isdefinedInputMultiObject   )split_filenamec               @   s   e Zd ZejdddddddZedddd	d
ZejdddddZ	eddgddZ
ejdddgdZejdddZejdddZejdddZeeddddddd Zejejd!d"Zd#S )$*WarpTimeSeriesImageMultiTransformInputSpec   r   z%dTzimage dimension (3 or 4)r   )argstr
usedefaultdescpositionz%szFimage to apply transformation to (generally a coregistered functional))r   	mandatorycopyfiler   Z_wtsimtzAPostfix that is prepended to all output files (default = _wtsimt))r   r   r   z-R %stightest_boxz0reference image space that you wish to warp INTO)r   xorr   z--tightest-bounding-boxzFcomputes tightest bounding box (overrided by reference_image if given)reference_image)r   r   r   z--reslice-by-headerzxUses orientation matrix and origin encoded in reference image file header. Not typically used with additional transforms)r   r   z--use-NNz"Use nearest neighbor interpolationz--use-Bsplinez$Use 3rd order B-Spline interpolation)existsz$transformation file(s) to be appliedF)r   r   r   r   a+  List of Affine transformations to invert.E.g.: [1,4,5] inverts the 1st, 4th, and 5th Affines found in transformation_series. Note that indexing starts with 1 and does not include warp fields. Affine transformations are distinguished from warp fields by the word "affine" included in their filenames.)r   N)__name__
__module____qualname__r   Enum	dimensionr   input_imageStrout_postfixr   Boolr   reslice_by_headeruse_nearestuse_bspliner
   transformation_seriesListIntinvert_affine r)   r)   S/var/www/html/virt/lib/python3.6/site-packages/nipype/interfaces/ants/resampling.pyr      sH   


r   c               @   s   e Zd ZedddZdS )+WarpTimeSeriesImageMultiTransformOutputSpecTzWarped image)r   r   N)r   r   r   r   output_imager)   r)   r)   r*   r+   K   s   r+   c                   sF   e Zd ZdZd ZeZeZ fddZ	dd Z
dgf fdd	Z  ZS )	!WarpTimeSeriesImageMultiTransforma+  Warps a time-series from one space to another

    Examples
    --------

    >>> from nipype.interfaces.ants import WarpTimeSeriesImageMultiTransform
    >>> wtsimt = WarpTimeSeriesImageMultiTransform()
    >>> wtsimt.inputs.input_image = 'resting.nii'
    >>> wtsimt.inputs.reference_image = 'ants_deformed.nii.gz'
    >>> wtsimt.inputs.transformation_series = ['ants_Warp.nii.gz','ants_Affine.txt']
    >>> wtsimt.cmdline
    'WarpTimeSeriesImageMultiTransform 4 resting.nii resting_wtsimt.nii -R ants_deformed.nii.gz ants_Warp.nii.gz ants_Affine.txt'

    >>> wtsimt = WarpTimeSeriesImageMultiTransform()
    >>> wtsimt.inputs.input_image = 'resting.nii'
    >>> wtsimt.inputs.reference_image = 'ants_deformed.nii.gz'
    >>> wtsimt.inputs.transformation_series = ['ants_Warp.nii.gz','ants_Affine.txt']
    >>> wtsimt.inputs.invert_affine = [1] # # this will invert the 1st Affine file: ants_Affine.txt
    >>> wtsimt.cmdline
    'WarpTimeSeriesImageMultiTransform 4 resting.nii resting_wtsimt.nii -R ants_deformed.nii.gz ants_Warp.nii.gz -i ants_Affine.txt'
    c                s   |dkr.t tjj| jj\}}}|| | S |dkrg }d}g }	xR|D ]J}
d|
krt| jjr|d7 }|| jjkr|dg7 }|	j| ||
g7 }qHW t| jjrt	| jjt	|	 }|rt
ddj|S tt| j|||S )	Nr    r%   r   ZAffiner   z-izqReview invert_affine, not all indexes from invert_affine were used, check the description for the full definition )r   ospathabspathinputsr   r	   r(   appendset
Exceptionsjoinsuperr-   _format_arg)selfoptspecval_nameextseriesaffine_counteraffine_inverttransformationdiff_inv)	__class__r)   r*   r8   l   s,    




z-WarpTimeSeriesImageMultiTransform._format_argc             C   sP   | j  j }ttjj| jj\}}}tjjtj	 dj|| jj
|f|d< |S )N r,   )_outputsgetr   r/   r0   r1   r2   r   r6   getcwdr    )r9   outputsr=   r>   r?   r)   r)   r*   _list_outputs   s
     z/WarpTimeSeriesImageMultiTransform._list_outputsr   c                s0   t t| j|ddgd}d|jkr,| j| |S )Nr   r   )correct_return_codesz100 % complete)r7   r-   _run_interfacestdoutZraise_exception)r9   ZruntimerL   )rE   r)   r*   rM      s
    


z0WarpTimeSeriesImageMultiTransform._run_interface)r   r   r   __doc___cmdr   
input_specr+   output_specr8   rK   rM   __classcell__r)   r)   )rE   r*   r-   O   s   r-   c               @   s   e Zd ZejdddddddZeddd	dd
ZeddddddgdZedddddgdZ	eddgddZ
ejdddgdZejdddZejdddZejd d!dZeedd"dd#dd(d$Zejejd%d&Zd'S )) WarpImageMultiTransformInputSpecr   r   z%dTzimage dimension (2 or 3)r   )r   r   r   r   z%szFimage to apply transformation to (generally a coregistered functional))r   r   r   r   Fzname of the output warped imager    )genfile
hash_filesr   r   r   r   Z_wimtz?Postfix that is prepended to all output files (default = _wimt)r,   )r   rV   r   r   z-R %sr   z0reference image space that you wish to warp INTO)r   r   r   z--tightest-bounding-boxzFcomputes tightest bounding box (overrided by reference_image if given)r   )r   r   r   z--reslice-by-headerzxUses orientation matrix and origin encoded in reference image file header. Not typically used with additional transforms)r   r   z--use-NNz"Use nearest neighbor interpolationz--use-BSplinez$Use 3rd order B-Spline interpolation)r   z$transformation file(s) to be applied)r   r   r   r   a+  List of Affine transformations to invert.E.g.: [1,4,5] inverts the 1st, 4th, and 5th Affines found in transformation_series. Note that indexing starts with 1 and does not include warp fields. Affine transformations are distinguished from warp fields by the word "affine" included in their filenames.)r   N)r   r   r   r   r   r   r   r   r,   r    r   r!   r   r"   r#   r$   r
   r%   r&   r'   r(   r)   r)   r)   r*   rT      sX   




rT   c               @   s   e Zd ZedddZdS )!WarpImageMultiTransformOutputSpecTzWarped image)r   r   N)r   r   r   r   r,   r)   r)   r)   r*   rX      s   rX   c                   s<   e Zd ZdZd ZeZeZdd Z	 fddZ
dd Z  ZS )WarpImageMultiTransforma  Warps an image from one space to another

    Examples
    --------

    >>> from nipype.interfaces.ants import WarpImageMultiTransform
    >>> wimt = WarpImageMultiTransform()
    >>> wimt.inputs.input_image = 'structural.nii'
    >>> wimt.inputs.reference_image = 'ants_deformed.nii.gz'
    >>> wimt.inputs.transformation_series = ['ants_Warp.nii.gz','ants_Affine.txt']
    >>> wimt.cmdline
    'WarpImageMultiTransform 3 structural.nii structural_wimt.nii -R ants_deformed.nii.gz ants_Warp.nii.gz ants_Affine.txt'

    >>> wimt = WarpImageMultiTransform()
    >>> wimt.inputs.input_image = 'diffusion_weighted.nii'
    >>> wimt.inputs.reference_image = 'functional.nii'
    >>> wimt.inputs.transformation_series = ['func2anat_coreg_Affine.txt','func2anat_InverseWarp.nii.gz',     'dwi2anat_Warp.nii.gz','dwi2anat_coreg_Affine.txt']
    >>> wimt.inputs.invert_affine = [1]  # this will invert the 1st Affine file: 'func2anat_coreg_Affine.txt'
    >>> wimt.cmdline
    'WarpImageMultiTransform 3 diffusion_weighted.nii diffusion_weighted_wimt.nii -R functional.nii -i func2anat_coreg_Affine.txt func2anat_InverseWarp.nii.gz dwi2anat_Warp.nii.gz dwi2anat_coreg_Affine.txt'

    c             C   s:   |dkr6t tjj| jj\}}}dj|| jj|fS d S )Nr,   rF   )r   r/   r0   r1   r2   r   r6   r    )r9   r>   r=   r?   r)   r)   r*   _gen_filename  s    z%WarpImageMultiTransform._gen_filenamec       	         s   |dkrg }d}g }xV|D ]N}d|j  kr^t| jjr^|d7 }|| jjkr^|dg7 }|j| ||g7 }qW t| jjrt| jjt| }|rtddj|S tt	| j
|||S )Nr%   r   Zaffiner   z-izqReview invert_affine, not all indexes from invert_affine were used, check the description for the full definitionr.   )lowerr	   r2   r(   r3   r4   r5   r6   r7   rY   r8   )	r9   r:   r;   r<   r@   rA   rB   rC   rD   )rE   r)   r*   r8     s&    




z#WarpImageMultiTransform._format_argc             C   sH   | j  j }t| jjr.tjj| jj|d< ntjj| jd|d< |S )Nr,   )	rG   rH   r	   r2   r,   r/   r0   r1   rZ   )r9   rJ   r)   r)   r*   rK   '  s    z%WarpImageMultiTransform._list_outputs)r   r   r   rO   rP   rT   rQ   rX   rR   rZ   r8   rK   rS   r)   r)   )rE   r*   rY      s   rY   c               @   s  e Z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ddZej	dddddZ
ej	ddddZedddddZejddddddddd d!dd"Zejejej ejej ej Zeejedd#d$d!dd%d&Zeej Zejd'd(dd"Zejdd)gd*d+Zejd,ddd-d.Zd/S )0ApplyTransformsInputSpecr   r   r   z--dimensionality %dzThis option forces the image to be treated as a specified-dimensional image. If not specified, antsWarp tries to infer the dimensionality from the input image.)r   r   r   r   z--input-image-type %dz[Option specifying the input image type of scalar (default), vector, tensor, or time series.z
--input %sTzFimage to apply transformation to (generally a coregistered functional))r   r   r   r   z--output %szoutput file nameF)r   r   rU   rV   Z_transz?Postfix that is appended to all output files (default = _trans))r   r   z--reference-image %sz0reference image space that you wish to warp INTOZLinearZNearestNeighborZCosineWindowedSincZWelchWindowedSincZHammingWindowedSincZLanczosWindowedSinc
MultiLabelGaussianBSplinez%s)r   r   )r   identityzstransform files: will be applied in reverse order. For example, the last specified transform will be applied first.)r   r   r   g        z--default-value %gr,   z;output a composite warp file instead of a transformed image)requiresr   z
--float %dz-Use float instead of double for computations.)r   default_valuer   r   N)r   r   r   r   r   r   Zinput_image_typer   r   r   r,   r    r   interpolationZEitherTupler'   ZFloatinterpolation_parametersr
   
transformsr!   invert_transform_flagsrb   print_out_composite_warp_filefloatr)   r)   r)   r*   r\   2  sz   r\   c               @   s   e Zd ZedddZdS )ApplyTransformsOutputSpecTzWarped image)r   r   N)r   r   r   r   r,   r)   r)   r)   r*   rj     s   rj   c                   sL   e Zd ZdZdZeZeZdd Z	dd Z
dd Z fd	d
Zdd Z  ZS )ApplyTransformsa
  ApplyTransforms, applied to an input image, transforms it according to a
    reference image and a transform (or a set of transforms).

    Examples
    --------

    >>> from nipype.interfaces.ants import ApplyTransforms
    >>> at = ApplyTransforms()
    >>> at.inputs.input_image = 'moving1.nii'
    >>> at.inputs.reference_image = 'fixed1.nii'
    >>> at.inputs.transforms = 'identity'
    >>> at.cmdline
    'antsApplyTransforms --default-value 0 --float 0 --input moving1.nii --interpolation Linear --output moving1_trans.nii --reference-image fixed1.nii --transform identity'

    >>> at = ApplyTransforms()
    >>> at.inputs.dimension = 3
    >>> at.inputs.input_image = 'moving1.nii'
    >>> at.inputs.reference_image = 'fixed1.nii'
    >>> at.inputs.output_image = 'deformed_moving1.nii'
    >>> at.inputs.interpolation = 'Linear'
    >>> at.inputs.default_value = 0
    >>> at.inputs.transforms = ['ants_Warp.nii.gz', 'trans.mat']
    >>> at.inputs.invert_transform_flags = [False, True]
    >>> at.cmdline
    'antsApplyTransforms --default-value 0 --dimensionality 3 --float 0 --input moving1.nii --interpolation Linear --output deformed_moving1.nii --reference-image fixed1.nii --transform ants_Warp.nii.gz --transform [ trans.mat, 1 ]'

    >>> at1 = ApplyTransforms()
    >>> at1.inputs.dimension = 3
    >>> at1.inputs.input_image = 'moving1.nii'
    >>> at1.inputs.reference_image = 'fixed1.nii'
    >>> at1.inputs.output_image = 'deformed_moving1.nii'
    >>> at1.inputs.interpolation = 'BSpline'
    >>> at1.inputs.interpolation_parameters = (5,)
    >>> at1.inputs.default_value = 0
    >>> at1.inputs.transforms = ['ants_Warp.nii.gz', 'trans.mat']
    >>> at1.inputs.invert_transform_flags = [False, False]
    >>> at1.cmdline
    'antsApplyTransforms --default-value 0 --dimensionality 3 --float 0 --input moving1.nii --interpolation BSpline[ 5 ] --output deformed_moving1.nii --reference-image fixed1.nii --transform ants_Warp.nii.gz --transform trans.mat'

    Identity transforms may be used as part of a chain:

    >>> at2 = ApplyTransforms()
    >>> at2.inputs.dimension = 3
    >>> at2.inputs.input_image = 'moving1.nii'
    >>> at2.inputs.reference_image = 'fixed1.nii'
    >>> at2.inputs.output_image = 'deformed_moving1.nii'
    >>> at2.inputs.interpolation = 'BSpline'
    >>> at2.inputs.interpolation_parameters = (5,)
    >>> at2.inputs.default_value = 0
    >>> at2.inputs.transforms = ['identity', 'ants_Warp.nii.gz', 'trans.mat']
    >>> at2.cmdline
    'antsApplyTransforms --default-value 0 --dimensionality 3 --float 0 --input moving1.nii --interpolation BSpline[ 5 ] --output deformed_moving1.nii --reference-image fixed1.nii --transform identity --transform ants_Warp.nii.gz --transform trans.mat'
    ZantsApplyTransformsc             C   sB   |dkr>| j j}t|s:t| j j\}}}|| j j | }|S d S )Nr,   )r2   r,   r	   r   r   r    )r9   r>   outputr=   r?   r)   r)   r*   rZ     s    zApplyTransforms._gen_filenamec             C   s   g }| j j}t|s(dgt| j j }nt| j jt|krDtdxBt| j j|D ]0\}}|rt|jd| d qT|jd|  qTW dj|S )NFzcERROR: The invert_transform_flags list must have the same number of entries as the transforms list.z--transform [ z, 1 ]z--transform r.   )	r2   rg   r	   lenrf   
ValueErrorzipr3   r6   )r9   retvalZinvert_flagsZ	transforminvertr)   r)   r*   _get_transform_filenames  s    z(ApplyTransforms._get_transform_filenamesc             C   s8   t | jjr&d| jdt| jjf S d| jd S d S )Nz--output [ %s, %d ]r,   z--output %s)r	   r2   rh   rZ   int)r9   r)   r)   r*   _get_output_warped_filename  s
    z+ApplyTransforms._get_output_warped_filenamec                s   |dkr| j  S |dkr | j S |dkrp| jjdkrdt| jjrdd| jjdjd	d
 | jjD f S d| jj S tt| j	|||S )Nr,   rf   rc   r_   r]   r^   z--interpolation %s[ %s ]z, c             S   s   g | ]}t |qS r)   )str).0paramr)   r)   r*   
<listcomp>  s    z/ApplyTransforms._format_arg.<locals>.<listcomp>z--interpolation %s)r_   r]   r^   )
rt   rr   r2   rc   r	   re   r6   r7   rk   r8   )r9   r:   r;   r<   )rE   r)   r*   r8     s       zApplyTransforms._format_argc             C   s&   | j  j }tjj| jd|d< |S )Nr,   )rG   rH   r/   r0   r1   rZ   )r9   rJ   r)   r)   r*   rK   
  s    zApplyTransforms._list_outputs)r   r   r   rO   rP   r\   rQ   rj   rR   rZ   rr   rt   r8   rK   rS   r)   r)   )rE   r*   rk     s   =		rk   c               @   sp   e Zd ZejddddddZeddd	dd
ZejdddgdddZ	ej
eddddddZej
ej ddZdS ) ApplyTransformsToPointsInputSpecr   r   r   z--dimensionality %dzThis option forces the image to be treated as a specified-dimensional image. If not specified, antsWarp tries to infer the dimensionality from the input image.)r   r   z
--input %sTa  Currently, the only input supported is a csv file with columns including x,y (2D), x,y,z (3D) or x,y,z,t,label (4D) column headers. The points should be defined in physical space. If in doubt how to convert coordinates from your files to the space required by antsApplyTransformsToPoints try creating/drawing a simple label volume with only one voxel set to 1 and all others set to 0. Write down the voxel coordinates. Then use ImageMaths LabelStats to find out what coordinates for this voxel antsApplyTransformsToPoints is expecting.)r   r   r   r   z--output %szName of the output CSV file
input_fileFz%s_transformed.csv)r   r   Zname_sourcerV   Zname_template)r   z%sz-transforms that will be applied to the points)r   r   r   z1list indicating if a transform should be reversed)r   N)r   r   r   r   r   r   r   rz   r   output_filer&   rf   r!   rg   r)   r)   r)   r*   ry     s0   
ry   c               @   s   e Zd ZedddZdS )!ApplyTransformsToPointsOutputSpecTz%csv file with transformed coordinates)r   r   N)r   r   r   r   r{   r)   r)   r)   r*   r|   ?  s   r|   c                   s4   e Zd ZdZdZeZeZdd Z	 fddZ
  ZS )ApplyTransformsToPointsa  ApplyTransformsToPoints, applied to an CSV file, transforms coordinates
    using provided transform (or a set of transforms).

    Examples
    --------

    >>> from nipype.interfaces.ants import ApplyTransforms
    >>> at = ApplyTransformsToPoints()
    >>> at.inputs.dimension = 3
    >>> at.inputs.input_file = 'moving.csv'
    >>> at.inputs.transforms = ['trans.mat', 'ants_Warp.nii.gz']
    >>> at.inputs.invert_transform_flags = [False, False]
    >>> at.cmdline
    'antsApplyTransformsToPoints --dimensionality 3 --input moving.csv --output moving_transformed.csv --transform [ trans.mat, 0 ] --transform [ ants_Warp.nii.gz, 0 ]'


    ZantsApplyTransformsToPointsc             C   s   g }xt t| jjD ]x}t| jjrxt| jjt| jjkrn| jj| rNdnd}|jd| jj| |f  qtdq|jd| jj|   qW dj|S )Nr   r   z--transform [ %s, %d ]z_ERROR: The useInverse list must have the same number of entries as the transformsFileName list.z--transform %sr.   )	rangerm   r2   rf   r	   rg   r3   	Exceptionr6   )r9   rp   iiZinvert_coder)   r)   r*   rr   [  s    z0ApplyTransformsToPoints._get_transform_filenamesc                s$   |dkr| j  S tt| j|||S )Nrf   )rr   r7   r}   r8   )r9   r:   r;   r<   )rE   r)   r*   r8   r  s    z#ApplyTransformsToPoints._format_arg)r   r   r   rO   rP   ry   rQ   r|   rR   rr   r8   rS   r)   r)   )rE   r*   r}   C  s   r}   )rO   r/   baser   r   r   r   r   r	   r
   Zutils.filemanipr   r   r+   r-   rT   rX   rY   r\   rj   rk   ry   r|   r}   r)   r)   r)   r*   <module>   s"   @KIKX /