3
Xd:.                 @   s   d Z ddlmZ ddlZddlm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dS )zANTS Legacy Interfaces

These interfaces are for programs that have been deprecated by ANTs, but
are preserved for backwards compatibility.
    )rangeN)glob   )ANTSCommandANTSCommandInputSpec   )TraitedSpecFiletraits	isdefinedOutputMultiPath   )split_filenamec               @   s   e Zd ZejdddddddZeddd	ddd
Zedddddd
Zej	dddZ
ej	dddZejejddddZej	dddZejdddddddZejdd d!d"d#d$d%d&d'dd(d)Zejd*d+dd,d)Zej	d-d.dZd/S )0antsIntroductionInputSpecr   r   z-d %dTzimage dimension (2 or 3)r   )argstr
usedefaultdescpositionz-r %sztemplate file to warp to)existsr   r   	mandatorycopyfilez-i %szinput image to warp to templateFz-f 1z;force script to proceed even if headers may be incompatible)r   r   z-lz}Applies inverse warp to the template labels to estimate label positions in target space (use for template-based segmentation)z-m %sxzmaximum number of iterations (must be list of integers in the form [J,K,L...]: J = coarsest resolution iterations, K = middle resolution interations, L = fine resolution iterations)r   sepr   z-n 1z-Applies bias field correction to moving imagePRCCMIMSQz-s %szType of similartiy metric used for registration (CC = cross correlation, MI = mutual information, PR = probability mapping, MSQ = mean square difference)GRELSYS2EXDDRIRAz-t %sa9  Type of transofmration model used for registration (EL = elastic transformation model, SY = SyN with time, arbitrary number of time points, S2 =  SyN with time optimized for 2 time points, GR = greedy SyN, EX = exponential, DD = diffeomorphic demons style exponential mapping, RI = purely rigid, RA = affine rigid)r   r   r   Zants_z-o %sz>Prefix that is prepended to all output files (default = ants_)z-q 1z%Perform a quality check of the resultN)__name__
__module____qualname__r
   Enum	dimensionr	   Zreference_imageZinput_imageBoolZforce_proceedZinverse_warp_template_labelsListIntmax_iterationsbias_field_correctionsimilarity_metrictransformation_modelStr
out_prefixZquality_check r3   r3   O/var/www/html/virt/lib/python3.6/site-packages/nipype/interfaces/ants/legacy.pyr      st   
r   c               @   sH   e Zd ZedddZedddZedddZedddZedddZdS )	antsIntroductionOutputSpecTzaffine (prefix_Affine.txt))r   r   zwarp field (prefix_Warp.nii)z+inverse warp field (prefix_InverseWarp.nii)z!input image (prefix_repaired.nii)z"output image (prefix_deformed.nii)N)	r%   r&   r'   r	   affine_transformation
warp_fieldinverse_warp_field
input_fileoutput_filer3   r3   r3   r4   r5   o   s   
r5   c               @   s$   e Zd ZdZdZeZeZdd Z	dS )antsIntroductiona  Uses ANTS to generate matrices to warp data from one space to another.

    Examples
    --------

    >>> from nipype.interfaces.ants.legacy import antsIntroduction
    >>> warp = antsIntroduction()
    >>> warp.inputs.reference_image = 'Template_6.nii'
    >>> warp.inputs.input_image = 'structural.nii'
    >>> warp.inputs.max_iterations = [30,90,20]
    >>> warp.cmdline
    'antsIntroduction.sh -d 3 -i structural.nii -m 30x90x20 -o ants_ -r Template_6.nii -t GR'

    zantsIntroduction.shc             C   s   | j  j }| jj}t| s.t|rj|dkrjtjjtj | jj	d |d< tjjtj | jj	d |d< tjjtj | jj	d |d< tjjtj | jj	d	 |d
< tjjtj | jj	d |d< |S )Nr#   r$   zWarp.nii.gzr7   zInverseWarp.nii.gzr8   z
Affine.txtr6   zrepaired.nii.gzr9   zdeformed.nii.gzr:   )r#   r$   )
_outputsgetinputsr0   r   ospathjoingetcwdr2   )selfoutputsZ
transmodelr3   r3   r4   _list_outputs   s    
zantsIntroduction._list_outputsN)
r%   r&   r'   __doc___cmdr   
input_specr5   output_specrE   r3   r3   r3   r4   r;   y   s
   r;   c               @   s   e Zd ZdS )GenWarpFieldsN)r%   r&   r'   r3   r3   r3   r4   rJ      s   rJ   c               @   s  e Zd ZejddddddddZejd	d
dddZeje	dddddd6dZ
ejdddddddZejdddZejdddddZejddgddZejejdddd Zejd!d"dZejd#d$dZejd%d&d'd(d)d*dZejd+d,d-d.d/d0d1dd2d	Zejd3d4Zd5S )7buildtemplateparallelInputSpecr   r      z-d %dTzimage dimension (2, 3 or 4)r   )r   r   r   r   Z	antsTMPL_z-o %szBPrefix that is prepended to all output files (default = antsTMPL_))r   r   r   )r   z(list of images to generate template fromz%s)r   r   r   r   r   z-c %dz\control for parallel processing (0 = serial, 1 = use PBS, 2 = use PEXEC, 3 = use Apple XGridz-g %fz@smaller magnitude results in more cautious steps (default = .25))r   r   z-i %dz#iterations of template constructionz-j %dparallelizationzERequires parallelization = 2 (PEXEC). Sets number of cpu cores to use)r   requiresr   z-m %sr   zmaximum number of iterations (must be list of integers in the form [J,K,L...]: J = coarsest resolution iterations, K = middle resolution interations, L = fine resolution iterations)r   r   r   z-n 1z-Applies bias field correction to moving imagez-r 1zSregisters inputs before creating template (useful if no initial template available)r   r   r   r   z-s %szType of similartiy metric used for registration (CC = cross correlation, MI = mutual information, PR = probability mapping, MSQ = mean square difference)r   r   r   r    r!   r"   z-t %sa  Type of transofmration model used for registration (EL = elastic transformation model, SY = SyN with time, arbitrary number of time points, S2 =  SyN with time optimized for 2 time points, GR = greedy SyN, EX = exponential, DD = diffeomorphic demons style exponential mappingzeuses first volume as target of all inputs. When not used, an unbiased average image is used to start.)r   N)r%   r&   r'   r
   r(   r)   r1   r2   r+   r	   in_filesrM   ZFloatZgradient_step_sizer,   Ziteration_limit	num_coresr-   r*   r.   Zrigid_body_registrationr/   r0   use_first_as_targetr3   r3   r3   r4   rK      s   
rK   c               @   s<   e Zd ZedddZeeddddZeeddddZdS )	buildtemplateparallelOutputSpecTzfinal ANTS template)r   r   )r   z,Templates from different stages of iteration)r   z~Outputs for each input image. Includes warp field, inverse warp, Affine, original image (repaired) and warped image (deformed)N)r%   r&   r'   r	   final_template_filer   template_filessubject_outfilesr3   r3   r3   r4   rS      s   rS   c                   s4   e Zd ZdZdZeZeZ fddZ	dd Z
  ZS )buildtemplateparallela  Generate a optimal average template

    .. warning::

      This can take a VERY long time to complete

    Examples
    --------

    >>> from nipype.interfaces.ants.legacy import buildtemplateparallel
    >>> tmpl = buildtemplateparallel()
    >>> tmpl.inputs.in_files = ['T1.nii', 'structural.nii']
    >>> tmpl.inputs.max_iterations = [30, 90, 20]
    >>> tmpl.cmdline
    'buildtemplateparallel.sh -d 3 -i 4 -m 30x90x20 -o antsTMPL_ -c 0 -t GR T1.nii structural.nii'

    zbuildtemplateparallel.shc                sj   |dkr$| j jdkr dt| S dS |dkrV| j jr:d}nd}|djdd	 |D  S tt| j|||S )
NrQ   r   z-j  rP   z-z  c             s   s   | ]
}|V  qd S )Nr3   ).0namer3   r3   r4   	<genexpr>Q  s    z4buildtemplateparallel._format_arg.<locals>.<genexpr>)r>   rM   strrR   rA   superrW   _format_arg)rC   optspecvalstart)	__class__r3   r4   r_   F  s    z!buildtemplateparallel._format_argc          	   C   s(  | j  j }g |d< xttttjjdD ]}tjjd| jj	|| jj
f }tj|tjjd| jj	|| jj
|f  d| jj	|| jj
|f }|d jtjj| tjjd| jj
 |d< q.W g |d< xX| jjD ]L}t|\}}}ttjjd| jj
|f }x|D ]}|d j| qW qW |S )	NrU   z*iteration*z!%s_iteration_%d/%stemplate.nii.gzz%%s_iteration_%d/%stemplate_i%d.nii.gzz%stemplate.nii.gzrT   rV   z%s%s*)r<   r=   r   lenr   r?   r@   realpathr>   r0   r2   renameappendrP   r   )rC   rD   itempfile_filename_baser3   r3   r4   rE   T  s4    
z#buildtemplateparallel._list_outputs)r%   r&   r'   rF   rG   rK   rH   rS   rI   r_   rE   __classcell__r3   r3   )rd   r4   rW   /  s   rW   )rF   builtinsr   r?   r   rn   r   r   r   r	   r
   r   r   Zutils.filemanipr   r   r5   r;   rJ   rK   rS   rW   r3   r3   r3   r4   <module>   s   ]
4o