3
d"<                 @   s   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dS )z
The reg module provides classes for interfacing with the `niftyreg
<http://niftyreg.sourceforge.net>`_ registration 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jdd	d
Z	ejddd
Z
dZejded
ZeddddZeddddZeddddZejddddZejddddZejddddZdZejedd Zd!Zejed"d Zd#Zejed$d Zd%Zejed&d Zejd'd(ddZejd)d*ddZejd+d,d Zejd-d.d Zejd/d0d Zejd1d2d Zejd3d4d Z ejd5d6d Z!ejd7d8d
Z"ed9gd:d;d<d=Z#ed9gd>d?d@d=Z$dAS )BRegAladinInputSpeczInput Spec for RegAladin.Tz The input reference/target imagez-ref %s)existsdescargstr	mandatoryzThe input floating/source imagez-flo %sz-noSymzTurn off symmetric registration)r   r   z-rigOnlyzDo only a rigid registrationz'Directly optimise the affine parametersz
-affDirectzThe input affine transformationz	-inaff %s)r   r   r   zThe input reference maskz	-rmask %szThe input floating maskz	-fmask %szMaximum number of iterationsz	-maxit %dr   )r   r   lowz%Number of resolution levels to createz-ln %dz&Number of resolution levels to performz-lp %dz/Amount of smoothing to apply to reference imagez	-smooR %f)r   r   z.Amount of smoothing to apply to floating imagez	-smooF %fz-Use nifti header to initialise transformationz-nacz=Use the masks centre of mass to initialise the transformationz-cogz!Percent of blocks that are activez-pv %dzPercent of inlier blocksz-pi %dz(Lower threshold value on reference imagez-refLowThr %fz(Upper threshold value on reference imagez-refUpThr %fz'Lower threshold value on floating imagez-floLowThr %fz'Upper threshold value on floating imagez-floUpThr %fzPlatform indexz	-platf %izDevice to use idz	-gpuid %iz-voffzTurn off verbose outputflo_filez
%s_aff.txtzThe output affine matrix filez-aff %s)name_sourcename_templater   r   z%s_res.nii.gzz%The affine transformed floating imagez-res %sN)%__name__
__module____qualname____doc__r   ref_filer   r   BoolZ
nosym_flagZrig_only_flagr   Zaff_direct_flagZin_aff_file
rmask_file
fmask_fileRange	maxit_valln_vallp_valFloatZ
smoo_r_valZ
smoo_f_valZnac_flagZcog_flagZv_valZi_valZref_low_valZ
ref_up_valZflo_low_valZ
flo_up_valZIntZplatform_valZ	gpuid_valverbosity_off_flagaff_fileres_file r&   r&   @/tmp/pip-build-7vycvbft/nipype/nipype/interfaces/niftyreg/reg.pyr      sr   



r   c               @   s4   e Zd ZdZeddZeddZdZej	edZ
dS )RegAladinOutputSpeczOutput Spec for RegAladin.zThe output affine file)r   zThe output transformed imagez+Output string in the format for reg_averageN)r   r   r   r   r   r$   r%   r   r   String
avg_outputr&   r&   r&   r'   r(   v   s
   

r(   c                   s0   e Zd ZdZedZeZeZ	 fddZ
  ZS )	RegAladina  Interface for executable reg_aladin from NiftyReg platform.

    Block Matching algorithm for symmetric global registration.
    Based on Modat et al., "Global image registration using
    asymmetric block-matching approach"
    J. Med. Img. 1(2) 024003, 2014, doi: 10.1117/1.JMI.1.2.024003

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

    Examples
    --------
    >>> from nipype.interfaces import niftyreg
    >>> node = niftyreg.RegAladin()
    >>> node.inputs.ref_file = 'im1.nii'
    >>> node.inputs.flo_file = 'im2.nii'
    >>> node.inputs.rmask_file = 'mask.nii'
    >>> node.inputs.omp_core_val = 4
    >>> node.cmdline
    'reg_aladin -aff im2_aff.txt -flo im2.nii -omp 4 -ref im1.nii -res im2_res.nii.gz -rmask mask.nii'

    Z
reg_aladinc                sB   t t| j }tjj|d }tjj| jj}d||f |d< |S )Nr$   z%s %sr*   )superr+   _list_outputsospathabspathinputsr   )selfoutputsZaffZflo)	__class__r&   r'   r-      s
    zRegAladin._list_outputs)r   r   r   r   r   _cmdr   
input_specr(   output_specr-   __classcell__r&   r&   )r4   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Zeddd	d
Zedddd
Zedddd
Z	dZ
eje
ddZdZ
eje
ddZej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ejddeje
ddZd Z
ejejddeje
d!dZd"Z
ejejddeje
d#dZd$Z
ejejddeje
d%dZejd&d'dZejd(d)dZejd*d+dZejd,d-dZejd.d/dZejd0d1dZd2Z
ejd3e
d4Z d5Z
ejd6e
d4Z!d7Z
ejde
d8d9Z"d7Z
ejde
d:d9Z#d;Z
ejejddejdde
d<dZ$d;Z
ejejddejdde
d=dZ%ejd>d?dZ&d@Z
ejejddeje
dAdZ'ejdBdCdZ(dDZ
ejde
dEd9Z)ejdFdGdZ*dHZ
ejde
dId9Z+ejdJdKdZ,ejdLdMdZ-ejdNdOdZ.ejdPdQdZ/ejddRdSdTZ0ejddUdVdTZ1ejddWdXdTZ2ejdYdZdZ3ejd[d\dZ4d]Z
ejde
d^d9Z5ejd_d`dZ6eddadbd
Z7dcZ
eje
dddZ8ejdedfdZ9ejdgdhd4Z:edigdjdkdldmZ;edigdndodpdmZ<dqS )rRegF3DInputSpeczInput Spec for RegF3D.Tz The input reference/target imagez-ref %s)r   r   r   r   zThe input floating/source imagez-flo %sz$The input affine transformation filez-aff %s)r   r   r   z!The input cpp transformation filez	-incpp %szReference image maskz	-rmask %sz*Smoothing kernel width for reference imagez	-smooR %f)r   r   z)Smoothing kernel width for floating imagez	-smooF %fz#Lower threshold for reference imagez
--rLwTh %fz#Upper threshold for reference imagez
--rUpTh %fz"Lower threshold for floating imagez
--fLwTh %fz"Upper threshold for floating imagez
--fUpTh %fz?Lower threshold for reference image at the specified time pointr   )r   z-rLwTh %d %fz?Upper threshold for reference image at the specified time pointz-rUpTh %d %fz>Lower threshold for floating image at the specified time pointz-fLwTh %d %fz>Upper threshold for floating image at the specified time pointz-fUpTh %d %fz#Final grid spacing along the x axesz-sx %fz#Final grid spacing along the y axesz-sy %fz#Final grid spacing along the z axesz-sz %fzBending energy valuez-be %fzLinear elasticity penalty termz-le %fz,Log of jacobian of deformation penalty valuez-jl %fzEDo not approximate the log of jacobian penalty at control points onlyz-noAppJL)r   r   z-use NMI even when other options are specifiedz--nmiz3Number of bins in the histogram for reference imagez--rbn %d)r   r   r   z--fbn %dzHNumber of bins in the histogram for reference image for given time pointz
-rbn %d %dz
-fbn %d %dz%SD of the Gaussian for computing LNCCz	--lncc %fz<SD of the Gaussian for computing LNCC for a given time pointz-lncc %d %fz!Use SSD as the similarity measurez--ssdz8Use SSD as the similarity measure for a given time pointz-ssd %dz+Use KL divergence as the similarity measurez--kldzBUse KL divergence as the similarity measure for a given time pointz-kld %dzUse additive NMIz-amczDon't optimise in x directionz-noxzDon't optimise in y directionz-noyzDon't optimise in z directionz-nozz	-maxit %dz&Maximum number of iterations per level)r   r   r   z-ln %dz%Number of resolution levels to createz-lp %dz&Number of resolution levels to performz'Do not use the multiresolution approachz-nopyzUse simple GD optimizationz-noConjz3Add perturbation steps after each optimization stepz-pert %dzUse velocity field integrationz-velzFloating image maskz	-fmask %sz.Kernel width for smoothing the metric gradientz-smoothGrad %fzPadding valuez-pad %fz-voffzTurn off verbose outputr   z%s_cpp.nii.gzzThe output CPP filez-cpp %s)r   r   r   r   z%s_res.nii.gzzThe output resampled imagez-res %sN)=r   r   r   r   r   r   r   r$   Z
incpp_filer   r   r   r"   Zref_smooth_valZflo_smooth_valZrlwth_thr_valZrupth_thr_valZflwth_thr_valZfupth_thr_valTupler   Zrlwth2_thr_valZrupth2_thr_valZflwth2_thr_valZfupth2_thr_valZsx_valZsy_valZsz_valZbe_valZle_valZjl_valr   Zno_app_jl_flagZnmi_flagZrbn_valZfbn_valZrbn2_valZfbn2_valZlncc_valZ	lncc2_valZssd_flagZ	ssd2_flagZkld_flagZ	kld2_flagZamc_flagZnox_flagZnoy_flagZnoz_flagr   r    r!   Z	nopy_flagZnoconj_flagZpert_valvel_flagr   Zsmooth_grad_valZpad_valr#   cpp_filer%   r&   r&   r&   r'   r9      s   







r9   c               @   sH   e Zd ZdZeddZeddZeddZeddZdZ	e
je	dZdS )	RegF3DOutputSpeczOutput Spec for RegF3D.zThe output CPP file)r   zThe output resampled imagezThe output inverse CPP filezThe output inverse res filez+Output string in the format for reg_averageN)r   r   r   r   r   r<   r%   invcpp_fileinvres_filer   r   r)   r*   r&   r&   r&   r'   r=   Y  s   



r=   c                   s<   e Zd ZdZedZeZeZ	e
dd Z fddZ  ZS )RegF3Da  Interface for executable reg_f3d from NiftyReg platform.

    Fast Free-Form Deformation (F3D) algorithm for non-rigid registration.
    Initially based on Modat et al., "Fast Free-Form Deformation using
    graphics processing units", CMPB, 2010

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

    Examples
    --------
    >>> from nipype.interfaces import niftyreg
    >>> node = niftyreg.RegF3D()
    >>> node.inputs.ref_file = 'im1.nii'
    >>> node.inputs.flo_file = 'im2.nii'
    >>> node.inputs.rmask_file = 'mask.nii'
    >>> node.inputs.omp_core_val = 4
    >>> node.cmdline
    'reg_f3d -cpp im2_cpp.nii.gz -flo im2.nii -omp 4 -ref im1.nii -res im2_res.nii.gz -rmask mask.nii'

    Zreg_f3dc             C   s   t | \}}}tjj||S )N)r   r.   r/   join)in_filednZbn_r&   r&   r'   _remove_extension  s    zRegF3D._remove_extensionc                s   t t| j }| jjdkrN| j|d }| j|d }d| |d< d| |d< | jjdkrt| jjrtj	j
|d }tj	j
| jj}d| jj||f |d< n0tj	j
|d }tj	j
| jj}d	||f |d< |S )
NTr%   r<   z%s_backward.nii.gzr?   r>   z%s %s %sr*   z%s %s)r,   r@   r-   r1   r;   rE   r   r$   r.   r/   r0   r   )r2   r3   Zres_nameZcpp_namer<   r   )r4   r&   r'   r-     s"    zRegF3D._list_outputs)r   r   r   r   r   r5   r9   r6   r=   r7   staticmethodrE   r-   r8   r&   r&   )r4   r'   r@   d  s   r@   )r   r.   baser   r   r   r   r   r	   r
   Zutils.filemanipr   r   r(   r+   r9   r=   r@   r&   r&   r&   r'   <module>	   s   e	& 5