3
dk                 @   sh  d Z ddlZddlmZmZmZmZ ddl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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 )-zThe regutils module provides classes for interfacing with the `niftyreg
<http://niftyreg.sourceforge.net>`_ utility command line tools.

The interfaces were written to work with niftyreg version 1.5.10
    N   )TraitedSpecFiletraits	isdefined   )get_custom_pathNiftyRegCommandNiftyRegCommandInputSpec   )split_filenamec               @   s   e Zd ZdZedddddZedddddZeddd	d
Zej	dddd*dddZ
edgddd+ddZej	dddddddZejdddZejdd dZejd!d"d#Zd$Zejd%ed#Zd&Zej	d'dd(ed#Zd)S ),RegResampleInputSpeczInput Spec for RegResample.Tz The input reference/target imagez-ref %s)existsdescargstr	mandatoryzThe input floating/source imagez-flo %szThe input transformation filez	-trans %s)r   r   r   resZblankz-%sr   zType of output)r   position
usedefaultr   flo_filez%sr   z,The output filename of the transformed image)name_sourcename_templater   r   r   NNLINCUBSINCzInterpolation typez	-inter %d)r   r   zPadding valuez-pad %fzResample Tensor Mapz-tensor z-voffzTurn off verbose output)r   r   zNPerform the resampling in two steps to resample an image to a lower resolutionz-psfzQMinimise the matrix metric (0) or the determinant (1) when estimating the PSF [0]r   z-psf_alg %dN)__name__
__module____qualname____doc__r   ref_filer   
trans_filer   Enumtypeout_file	inter_valFloatZpad_valBoolZtensor_flagZverbosity_off_flagr   Zpsf_flagZpsf_alg r*   r*   E/tmp/pip-build-7vycvbft/nipype/nipype/interfaces/niftyreg/regutils.pyr      sF   r   c               @   s   e Zd ZdZeddZdS )RegResampleOutputSpeczOutput Spec for RegResample.z,The output filename of the transformed image)r   N)r   r   r    r!   r   r&   r*   r*   r*   r+   r,   P   s   r,   c                   s:   e Zd ZdZedZeZeZ	 fddZ
dddZ  ZS )	RegResamplea  Interface for executable reg_resample from NiftyReg platform.

    Tool to resample floating image in the space of a defined reference image
    given a transformation parametrisation generated by reg_aladin, reg_f3d or
    reg_transform

    `Source code <https://cmiclab.cs.ucl.ac.uk/mmodat/niftyreg>`_

    Examples
    --------
    >>> from nipype.interfaces import niftyreg
    >>> node = niftyreg.RegResample()
    >>> node.inputs.ref_file = 'im1.nii'
    >>> node.inputs.flo_file = 'im2.nii'
    >>> node.inputs.trans_file = 'warpfield.nii'
    >>> node.inputs.inter_val = 'LIN'
    >>> node.inputs.omp_core_val = 4
    >>> node.cmdline
    'reg_resample -flo im2.nii -inter 1 -omp 4 -ref im1.nii -trans warpfield.nii -res im2_res.nii.gz'

    Zreg_resamplec                s<   |dkr$ddddd}|j ||  S tt| j|||S d S )Nr'   r   r   r      )r   r   r   r   )r   superr-   _format_arg)selfnamespecvaluer'   )	__class__r*   r+   r0   s   s    zRegResample._format_argNc             C   s,   t |\}}}| jj}tjj|dj||S )Nz{0}_{1}.nii.gz)r   inputsr%   ospathjoinformat)r1   r4   r2   r8   base_suffixr*   r*   r+   _overload_extensionz   s    zRegResample._overload_extension)N)r   r   r    r!   r   _cmdr   
input_specr,   output_specr0   r>   __classcell__r*   r*   )r5   r+   r-   V   s   r-   c            	   @   s^   e Zd ZdZdZededdZedddddZej	d	d
ddddddZ
edgdddddZdS )RegJacobianInputSpeczInput Spec for RegJacobian.zBReference/target file (required if specifying CPP transformations.Tz-ref %s)r   r   r   z"The input non-rigid transformationz	-trans %s)r   r   r   r   ZjacZjacLZjacMz-%sr   zType of jacobian outcome)r   r   r   r   r#   z%sz)The output jacobian determinant file namer   )r   r   r   r   r   Nr   r   )r   r   r    r!   r   r   r"   r#   r   r$   r%   r&   r*   r*   r*   r+   rC      s,   rC   c               @   s   e Zd ZdZeddZdS )RegJacobianOutputSpeczOutput Spec for RegJacobian.zThe output file)r   N)r   r   r    r!   r   r&   r*   r*   r*   r+   rD      s   rD   c               @   s*   e Zd ZdZedZeZeZ	dddZ
dS )RegJacobianaM  Interface for executable reg_resample from NiftyReg platform.

    Tool to generate Jacobian determinant maps from transformation
    parametrisation generated by reg_f3d

    `Source code <https://cmiclab.cs.ucl.ac.uk/mmodat/niftyreg>`_

    Examples
    --------
    >>> from nipype.interfaces import niftyreg
    >>> node = niftyreg.RegJacobian()
    >>> node.inputs.ref_file = 'im1.nii'
    >>> node.inputs.trans_file = 'warpfield.nii'
    >>> node.inputs.omp_core_val = 4
    >>> node.cmdline
    'reg_jacobian -omp 4 -ref im1.nii -trans warpfield.nii -jac warpfield_jac.nii.gz'

    Zreg_jacobianNc             C   s,   t |\}}}| jj}tjj|dj||S )Nz{0}_{1}.nii.gz)r   r6   r%   r7   r8   r9   r:   )r1   r4   r2   r8   r;   r<   r=   r*   r*   r+   r>      s    zRegJacobian._overload_extension)N)r   r   r    r!   r   r?   rC   r@   rD   rA   r>   r*   r*   r*   r+   rE      s
   rE   c               @   sl  e Zd ZdZedddddZedgddd	d
ZejdddZ	ejdddZ
eddddZdZejeddZejdddZeddddZejejedddddZejejedddddZejejedddd dZejejedddd!dZejd"d#dZd$Zejejejejed%dZejejejejd&d'dZd(Zejejejejed)dZejd*d+d,d-d.d/dZd0S )1RegToolsInputSpeczInput Spec for RegTools.TzThe input image file pathz-in %s)r   r   r   r   in_filez%s_tools.nii.gzzThe output file namez-out %s)r   r   r   r   z-isozMake output image isotropic)r   r   z-nosclzSet scale, slope to 0 and 1z&Values outside the mask are set to NaNz-nan %s)r   r   r   z1Binarise the input image with the given thresholdz-thr %f)r   r   z-binzBinarise the input imagez'Compute the mean RMS between the imagesz-rms %s)r   z"Divide the input by image or valuez-div %sz$Multiply the input by image or valuez-mul %szAdd to the input image or valuez-add %sz-sub %sz%Downsample the image by a factor of 2z-downz2Smooth the input image using a cubic spline kernelz-smoS %f %f %fz(Change the resolution of the input imagez-chgres %f %f %fz.Smooth the input image using a Gaussian kernelz-smoG %f %f %fr   r   r   r   z5Interpolation order to use to warp the floating imagez
-interp %dN)r   r   r    r!   r   rG   r&   r   r)   Ziso_flagZ
noscl_flagZ	mask_filer   r(   Zthr_valZbin_flagZrms_valZEitherZdiv_valZmul_valZadd_valZsub_valZ	down_flagTupleZ	smo_s_valZchg_res_valZ	smo_g_valr$   r'   r*   r*   r*   r+   rF      st   
rF   c               @   s   e Zd ZdZedddZdS )RegToolsOutputSpeczOutput Spec for RegTools.zThe output fileT)r   r   N)r   r   r    r!   r   r&   r*   r*   r*   r+   rI   0  s   rI   c                   s4   e Zd ZdZedZeZeZ	dZ
 fddZ  ZS )RegToolsav  Interface for executable reg_tools from NiftyReg platform.

    Tool delivering various actions related to registration such as
    resampling the input image to a chosen resolution or remove the nan and
    inf in the input image by a specified value.

    `Source code <https://cmiclab.cs.ucl.ac.uk/mmodat/niftyreg>`_

    Examples
    --------
    >>> from nipype.interfaces import niftyreg
    >>> node = niftyreg.RegTools()
    >>> node.inputs.in_file = 'im1.nii'
    >>> node.inputs.mul_val = 4
    >>> node.inputs.omp_core_val = 4
    >>> node.cmdline
    'reg_tools -in im1.nii -mul 4.0 -omp 4 -out im1_tools.nii.gz'

    Z	reg_toolsZ_toolsc                s<   |dkr$ddddd}|j ||  S tt| j|||S d S )Nr'   r   r   r   r.   )r   r   r   r   )r   r/   rJ   r0   )r1   r2   r3   r4   r'   )r5   r*   r+   r0   Q  s    zRegTools._format_arg)r   r   r    r!   r   r?   rF   r@   rI   rA   _suffixr0   rB   r*   r*   )r5   r+   rJ   6  s   rJ   c               @   s   e Zd ZdZejedddddddd	d
ddgddZdZejedddddddd	d
ddgedZ	dZeddddd	d
dgdgedZ
dZedddddd
dgdgedZdZedddddd	dgdgedZdZedddddd	d
gdgedZdZejeddd!ddddgedZedddddZd S )"RegAverageInputSpeczInput Spec for RegAverage.T)existr   z-avg %s avg_lts_filesavg_ref_filedemean1_ref_filedemean2_ref_filedemean3_ref_file
warp_filesz*Averaging of images/affine transformations)r   r   sepxorr   z(Robust average of affine transformationsz-avg_lts %s	avg_fileszAll input images are resampled into the space of <reference image> and averaged. A cubic spline interpolation scheme is used for resamplingz-avg_tran %s)r   r   rV   requiresr   zZAverage images and demean average image that have affine transformations to a common spacez-demean1 %sz]Average images and demean average image that have non-rigid transformations to a common spacez-demean2 %szhAverage images and demean average image that have linear and non-rigid transformations to a common spacez-demean3 %szMtransformation files and floating image pairs/triplets to the reference spacez%sr   zOutput file name)genfiler   r   r   Nr   )r   r   r    r!   r   Listr   rW   r   rO   rP   rQ   rR   rS   rT   r&   r*   r*   r*   r+   rL   Y  s   rL   c               @   s   e Zd ZdZeddZdS )RegAverageOutputSpeczOutput Spec for RegAverage.zOutput file name)r   N)r   r   r    r!   r   r&   r*   r*   r*   r+   r[     s   r[   c                   sH   e Zd ZdZedZeZeZ	dZ
dd Zdd Ze fdd	Z  ZS )

RegAverageaj  Interface for executable reg_average from NiftyReg platform.

    Compute average matrix or image from a list of matrices or image.
    The tool can be use to resample images given input transformation
    parametrisation as well as to demean transformations in Euclidean or
    log-Euclidean space.

    This interface is different than the others in the way that the options
    will be written in a command file that is given as a parameter.

    `Source code <https://cmiclab.cs.ucl.ac.uk/mmodat/niftyreg>`_

    Examples
    --------

    >>> from nipype.interfaces import niftyreg
    >>> node = niftyreg.RegAverage()
    >>> one_file = 'im1.nii'
    >>> two_file = 'im2.nii'
    >>> three_file = 'im3.nii'
    >>> node.inputs.avg_files = [one_file, two_file, three_file]
    >>> node.cmdline  # doctest: +ELLIPSIS
    'reg_average --cmd_file .../reg_average_cmd'
    Zreg_averageZavg_outc             C   sr   |dkrnt | jjr$| j| jddS t | jjr^t| jjd \}}}|d
kr^| j| j|dS | j| jddS d S )Nr&   z.txt)extr   .nii.nii.gz.hdr.img.img.gz)r^   r_   r`   ra   rb   )r   r6   rO   
_gen_fnamerK   rW   r   )r1   r2   r<   Z_extr*   r*   r+   _gen_filename  s    zRegAverage._gen_filenamec             C   s8   | j  j }t| jjr&| jj|d< n| jd|d< |S )Nr&   )rA   getr   r6   r&   rd   )r1   outputsr*   r*   r+   _list_outputs  s
    zRegAverage._list_outputsc          
      sL   t t| j}tjjtj d}t|d}|j| W dQ R X d| j	|f S )z2Rewrite the cmdline to write options in text_file.reg_average_cmdwNz%s --cmd_file %s)
r/   r\   cmdliner7   r8   r9   getcwdopenwritecmd)r1   argvrh   f)r5   r*   r+   rj     s
    zRegAverage.cmdline)r   r   r    r!   r   r?   rL   r@   r[   rA   rK   rd   rg   propertyrj   rB   r*   r*   )r5   r+   r\     s   
r\   c               @   s~  e Zd ZdZedddddZedddd	d
gdZeddd<dddddddddddg
dZeddd=dddddddddddg
dZeddd>dddddddddddg
dZ	eddd?d!ddddddddddg
d"gd#Z
edd$d@d!d%Zd&Zedd'dAeddddddddddg
d(gd#Zd&Zedd$dBedgd)Zedd*dCd+ddddddddddg
dZejedd,edd,d-d.dDddddddddddg
d/Zedd0dEd1ddddddddddg
dZd2ZejejejejejejejejejejejejejedFd3ddddddddddg
d4Zd5Zedd6dGeddddddddddg
dZd7Zejedd,edd,edd,d8dHeddddddddddg
d4ZeddId$d9d:Zd;S )JRegTransformInputSpeczInput Spec for RegTransform.Tz The input reference/target imagez-ref %sr   )r   r   r   r   z'The input second reference/target imagez-ref2 %sr   	ref1_file)r   r   r   r   rX   z-def %sr   z-Compute deformation field from transformation
disp_input
flow_input
comp_inputupd_s_form_inputinv_aff_inputinv_nrr_input
half_inputmake_aff_inputaff_2_rig_inputflirt_2_nr_input)r   r   r   r   rV   z-disp %sz.Compute displacement field from transformation	def_inputz-flow %sz"Compute flow field from spline SVFz-comp %sr   zcompose two transformationscomp_input2)r   r   r   r   rV   rX   z%s)r   r   r   r   z-Update s-form using the affine transformationz-updSform %supd_s_form_input2)r   r   r   r   rX   z
-invAff %szInvert an affine transformation)r   z"Invert a non-linear transformationz-invNrr %s %s)r   r   r   rV   z-half %sz$Half way to the input transformationz,-makeAff %f %f %f %f %f %f %f %f %f %f %f %fz$Make an affine transformation matrix)r   r   r   rV   z6Extract the rigid component from affine transformationz-aff2rig %szGConvert a FLIRT affine transformation to niftyreg affine transformationz-flirtAff2NR %s %s %sztransformation file to write)rY   r   r   r   Nr   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r    r!   r   rs   Z	ref2_filer~   rt   ru   rv   r   r   rw   r   rx   r   rH   ry   rz   Z
argstr_tmpr(   r{   r|   r}   r&   r*   r*   r*   r+   rr     s  











rr   c               @   s   e Zd ZdZeddZdS )RegTransformOutputSpeczOutput Spec for RegTransform.z*Output File (transformation in any format))r   N)r   r   r    r!   r   r&   r*   r*   r*   r+   r     s   r   c               @   s<   e Zd ZdZedZeZeZ	dZ
dd Zdd Zdd	 Zd
S )RegTransformaK  Interface for executable reg_transform from NiftyReg platform.

    Tools to convert transformation parametrisation from one type to another
    as well as to compose, inverse or half transformations.

    `Source code <https://cmiclab.cs.ucl.ac.uk/mmodat/niftyreg>`_

    Examples
    --------
    >>> from nipype.interfaces import niftyreg
    >>> node = niftyreg.RegTransform()
    >>> node.inputs.def_input = 'warpfield.nii'
    >>> node.inputs.omp_core_val = 4
    >>> node.cmdline  # doctest: +ELLIPSIS
    'reg_transform -omp 4 -def warpfield.nii .../warpfield_trans.nii.gz'

    Zreg_transformZ_transc             C   s   | j j| j j| j j| j j| j j| j j| j j| j j| j j	| j j
| j j| j jg}g }xH|D ]@}t|rV|j| t|\}}}|dks|dks|dkrV|S qVW t|r|d S d S )Nz.niiz.nii.gzz.hdrr   )r6   r~   rt   ru   rv   r   rw   rx   ry   rz   r{   r|   r}   r   appendr   len)r1   r6   entriesentryr<   r]   r*   r*   r+   _find_input:  s,    


zRegTransform._find_inputc             C   s  |dkrt | jjr(| jd| jddS t | jjrt | jjrt| jj\}}}t| jj\}}}|dkst|dkr| j|| jddS | j|| j|dS t | jjr| j| jjd
 | jddS | j	 }t|\}}}|dk r| j|| j|dS | j|| jddS d S )Nr&   Zmatrixz.txt)r=   r]   .nii.nii.gz.hdr.img.img.gzr   )r   r   r   r   r   )r   r   r   r   r   )r   r   r   r   r   )
r   r6   r{   rc   rK   rv   r   r   r}   r   )r1   r2   r<   Zbn1Zext1Zext2Zinput_to_user]   r*   r*   r+   rd   T  s.    

    
zRegTransform._gen_filenamec             C   s8   | j  j }t| jjr&| jj|d< n| jd|d< |S )Nr&   )rA   re   r   r6   r&   rd   )r1   rf   r*   r*   r+   rg   u  s
    zRegTransform._list_outputsN)r   r   r    r!   r   r?   rr   r@   r   rA   rK   r   rd   rg   r*   r*   r*   r+   r   "  s   !r   c            	   @   sZ   e Zd ZdZedddddZedddddZejdd	d
dddddZ	edgddddZ
dS )RegMeasureInputSpeczInput Spec for RegMeasure.Tz The input reference/target imagez-ref %s)r   r   r   r   zThe input floating/source imagez-flo %sZnccZlnccZnmiZssdz-%sz Measure of similarity to compute)r   r   r   r   z%sz-out %sz+The output text file containing the measure)r   r   r   r   N)r   r   r    r!   r   r"   r   r   r$   measure_typer&   r*   r*   r*   r+   r     s0   r   c               @   s   e Zd ZdZeddZdS )RegMeasureOutputSpeczOutput Spec for RegMeasure.z+The output text file containing the measure)r   N)r   r   r    r!   r   r&   r*   r*   r*   r+   r     s   r   c               @   s*   e Zd ZdZedZeZeZ	dddZ
dS )
RegMeasureaA  Interface for executable reg_measure from NiftyReg platform.

    Given two input images, compute the specified measure(s) of similarity

    `Source code <https://cmiclab.cs.ucl.ac.uk/mmodat/niftyreg>`_

    Examples
    --------
    >>> from nipype.interfaces import niftyreg
    >>> node = niftyreg.RegMeasure()
    >>> node.inputs.ref_file = 'im1.nii'
    >>> node.inputs.flo_file = 'im2.nii'
    >>> node.inputs.measure_type = 'lncc'
    >>> node.inputs.omp_core_val = 4
    >>> node.cmdline
    'reg_measure -flo im2.nii -lncc -omp 4 -out im2_lncc.txt -ref im1.nii'

    Zreg_measureNc             C   s,   t |\}}}| jj}tjj|dj||S )Nz{0}_{1}.txt)r   r6   r   r7   r8   r9   r:   )r1   r4   r2   r8   r;   r<   r=   r*   r*   r+   r>     s    zRegMeasure._overload_extension)N)r   r   r    r!   r   r?   r   r@   r   rA   r>   r*   r*   r*   r+   r     s
   r   )r!   r7   r;   r   r   r   r   r   r	   r
   Zutils.filemanipr   r   r,   r-   rC   rD   rE   rF   rI   rJ   rL   r[   r\   rr   r   r   r   r   r   r*   r*   r*   r+   <module>   s0   @*l#w?  	^"