3
d!                 @   s  d Z ddljZddlZddlmZ ddlmZ ddlm	Z	m
Z
mZmZmZ ej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dS )"zo
Interfaces to perform image registrations and to apply the resulting
displacement maps to images and points.

    N   )logging   )ElastixBaseInputSpec   )CommandLineTraitedSpecFiletraitsInputMultiPathznipype.interfacec               @   sl   e Zd ZedddddZedddddZeeddddd	d
ZeddddZeddddZ	eddddZ
dS )RegistrationInputSpecTz-f %szfixed image)exists	mandatoryargstrdescz-m %szmoving image)r   z-p %s...z,parameter file, elastix handles 1 or more -p)r   r   r   z	-fMask %szmask for fixed image)r   r   r   z	-mMask %szmask for moving imagez-t0 %sz$parameter file for initial transformN)__name__
__module____qualname__r	   Zfixed_imagemoving_imager   
parametersZ
fixed_maskZmoving_maskZinitial_transform r   r   H/tmp/pip-build-7vycvbft/nipype/nipype/interfaces/elastix/registration.pyr      s   r   c               @   sN   e Zd ZeeddddZeddZeeddddZej	ej
dddZd	S )
RegistrationOutputSpecT)r   zoutput transform)r   z(input moving image warped to fixed imageFz6input moving image warped to fixed image at each levelz-flag indicating if warped image was generatedN)r   r   r   r   r	   	transformwarped_filewarped_filesr
   ListZBoolwarped_files_flagsr   r   r   r   r   &   s   
r   c               @   s,   e Zd ZdZdZeZeZdd Z	dd Z
dS )Registrationa  
    Elastix nonlinear registration interface

    Example
    -------

    >>> from nipype.interfaces.elastix import Registration
    >>> reg = Registration()
    >>> reg.inputs.fixed_image = 'fixed1.nii'
    >>> reg.inputs.moving_image = 'moving1.nii'
    >>> reg.inputs.parameters = ['elastix.txt']
    >>> reg.cmdline
    'elastix -f fixed1.nii -m moving1.nii -threads 1 -out ./ -p elastix.txt'


    Zelastixc             C   sP  | j  j }tj| jj}tjd}g |d< g |d< g |d< xt| jj	D ]\}}i }t
|dd}x\|j D ]P}|j }|jd rl|rl|j|}	|	rl| j|	jdj }
|
||	jdj < qlW W d Q R X |d jtj|d	|  d }|d
 r
tj|d||d f }|d j| |d j|d
  qJW |d d rL|d d |d< |S )Nz^\((\w+)\s(.+)\)$r   r   r   rz//r   r   zTransformParameters.%01d.txtZWriteResultImagezresult.%01d.%sZResultImageFormatr   r    )_outputsgetopabspathinputsoutput_pathrecompile	enumerater   open	readlinesstrip
startswithsearch_castgroupappendjoin)selfoutputsout_dirregexiparamsconfigflinemvaluer   r   r   r   _list_outputsH   s6    

 
zRegistration._list_outputsc             C   sx   |j dr8|jdr8|dkr dS |dkr,dS |dd S yt|S  tk
rr   yt|S  tk
rl   |S X Y nX d S )N"z"true"Tz"false"Fr   r    )r-   endswithint
ValueErrorfloat)r3   valr   r   r   r/   q   s    zRegistration._castN)r   r   r   __doc___cmdr   
input_specr   output_specr>   r/   r   r   r   r   r   2   s   )r   c               @   s,   e Zd ZedddddZedddddZdS )	ApplyWarpInputSpecTz-tp %sz transform-parameter file, only 1)r   r   r   r   z-in %szinput image to deform)r   r   r   r   N)r   r   r   r	   transform_filer   r   r   r   r   rI      s   rI   c               @   s   e Zd ZeddZdS )ApplyWarpOutputSpecz(input moving image warped to fixed image)r   N)r   r   r   r	   r   r   r   r   r   rK      s   rK   c               @   s$   e Zd ZdZdZeZeZdd Z	dS )	ApplyWarpa  
    Use ``transformix`` to apply a transform on an input image.
    The transform is specified in the transform-parameter file.

    Example
    -------

    >>> from nipype.interfaces.elastix import ApplyWarp
    >>> reg = ApplyWarp()
    >>> reg.inputs.moving_image = 'moving1.nii'
    >>> reg.inputs.transform_file = 'TransformParameters.0.txt'
    >>> reg.cmdline
    'transformix -in moving1.nii -threads 1 -out ./ -tp TransformParameters.0.txt'


    transformixc             C   s.   | j  j }tj| jj}tj|d|d< |S )Nzresult.nii.gzr   )r!   r"   r#   r$   r%   r&   r2   )r3   r4   r5   r   r   r   r>      s    zApplyWarp._list_outputsN)
r   r   r   rE   rF   rI   rG   rK   rH   r>   r   r   r   r   rL      s
   rL   c               @   sR   e Zd ZejddddddZejddddd	Zejddd
dd	ZeddddZ	dS )AnalyzeWarpInputSpecallTr   z-def %sz[transform all points from the input-image, which effectively generates a deformation field.)
usedefaultpositionr   r   z-jac %sz>generate an image with the determinant of the spatial Jacobian)rP   r   r   z
-jacmat %sz@generate an image with the spatial Jacobian matrix at each voxelz-in %sz input image to deform (not used))r   r   r   N)
r   r   r   r
   EnumZpointsZjacZjacmatr	   r   r   r   r   r   rN      s$   rN   c               @   s*   e Zd ZeddZeddZeddZdS )AnalyzeWarpOutputSpeczdisplacements field)r   zdet(Jacobian) mapzJacobian matrix mapN)r   r   r   r	   
disp_field
jacdet_map
jacmat_mapr   r   r   r   rS      s   

rS   c               @   s    e Zd ZdZeZeZdd ZdS )AnalyzeWarpa  
    Use transformix to get details from the input transform (generate
    the corresponding deformation field, generate the determinant of the
    Jacobian map or the Jacobian map itself)

    Example
    -------

    >>> from nipype.interfaces.elastix import AnalyzeWarp
    >>> reg = AnalyzeWarp()
    >>> reg.inputs.transform_file = 'TransformParameters.0.txt'
    >>> reg.cmdline
    'transformix -def all -jac all -jacmat all -threads 1 -out ./ -tp TransformParameters.0.txt'


    c             C   sN   | j  j }tj| jj}tj|d|d< tj|d|d< tj|d|d< |S )NzdeformationField.nii.gzrT   zspatialJacobian.nii.gzrU   zfullSpatialJacobian.nii.gzrV   )r!   r"   r#   r$   r%   r&   r2   )r3   r4   r5   r   r   r   r>      s    zAnalyzeWarp._list_outputsN)	r   r   r   rE   rN   rG   rS   rH   r>   r   r   r   r   rW      s   rW   c               @   s,   e Zd ZedddddZedddddZdS )	PointsWarpInputSpecTz-def %sz.input points (accepts .vtk triangular meshes).)r   r   r   r   z-tp %sz transform-parameter file, only 1)r   r   r   r   N)r   r   r   r	   points_filerJ   r   r   r   r   rX      s   rX   c               @   s   e Zd ZeddZdS )PointsWarpOutputSpecz,input points displaced in fixed image domain)r   N)r   r   r   r	   r   r   r   r   r   rZ      s   rZ   c               @   s$   e Zd ZdZdZeZeZdd Z	dS )
PointsWarpa  Use ``transformix`` to apply a transform on an input point set.
    The transform is specified in the transform-parameter file.

    Example
    -------

    >>> from nipype.interfaces.elastix import PointsWarp
    >>> reg = PointsWarp()
    >>> reg.inputs.points_file = 'surf1.vtk'
    >>> reg.inputs.transform_file = 'TransformParameters.0.txt'
    >>> reg.cmdline
    'transformix -threads 1 -out ./ -def surf1.vtk -tp TransformParameters.0.txt'


    rM   c             C   sJ   | j  j }tj| jj}tjtj| jj\}}tj	|d| |d< |S )Nzoutputpoints%sr   )
r!   r"   r#   r$   r%   r&   splitextbasenamerY   r2   )r3   r4   r5   fnameextr   r   r   r>     s
    zPointsWarp._list_outputsN)
r   r   r   rE   rF   rX   rG   rZ   rH   r>   r   r   r   r   r[     s
   r[   )rE   os.pathpathr#   r'    r   baser   r   r   r	   r
   r   	getLoggerZifloggerr   r   r   rI   rK   rL   rN   rS   rW   rX   rZ   r[   r   r   r   r   <module>	   s$   

Q