3
d0I                 @   s  d dl jZddlmZmZmZmZmZm	Z	m
Z
mZ ddlmZ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G d*d+ d+eZ G d,d- d-eZ!G d.d/ d/eZ"dS )0    N   )CommandLineInputSpecCommandLinetraitsTraitedSpecFile	isdefined	UndefinedInputMultiObject   )MRTrix3BaseInputSpecMRTrix3Basec               @   sp   e Zd ZeddddddZeddddd	Zejejejejfd
ddZ	eddddddZ
edddddddZdS )DWIDenoiseInputSpecTz%sr   zinput DWI image)existsargstrposition	mandatorydescz-mask %sr   z
mask image)r   r   r   r   z-extent %d,%d,%dz>set the window size of the denoising filter. (default = 5,5,5))r   r   z	-noise %sz%s_noisein_filezthe output noise map)r   name_templatename_sourcekeep_extensionr   z%s_denoisedzthe output denoised DWI image)r   r   r   r   r   r   N)__name__
__module____qualname__r   r   maskr   TupleIntZextentnoiseout_file r"   r"   F/tmp/pip-build-7vycvbft/nipype/nipype/interfaces/mrtrix3/preprocess.pyr      s(   r   c               @   s$   e Zd ZedddZedddZdS )DWIDenoiseOutputSpeczthe output noise mapT)r   r   zthe output denoised DWI imageN)r   r   r   r   r    r!   r"   r"   r"   r#   r$   /   s   r$   c               @   s   e Zd ZdZdZeZeZdS )
DWIDenoisea  
    Denoise DWI data and estimate the noise level based on the optimal
    threshold for PCA.

    DWI data denoising and noise map estimation by exploiting data redundancy
    in the PCA domain using the prior knowledge that the eigenspectrum of
    random covariance matrices is described by the universal Marchenko Pastur
    distribution.

    Important note: image denoising must be performed as the first step of the
    image processing pipeline. The routine will fail if interpolation or
    smoothing has been applied to the data prior to denoising.

    Note that this function does not correct for non-Gaussian noise biases.

    For more information, see
    <https://mrtrix.readthedocs.io/en/latest/reference/commands/dwidenoise.html>

    Example
    -------

    >>> import nipype.interfaces.mrtrix3 as mrt
    >>> denoise = mrt.DWIDenoise()
    >>> denoise.inputs.in_file = 'dwi.mif'
    >>> denoise.inputs.mask = 'mask.mif'
    >>> denoise.inputs.noise = 'noise.mif'
    >>> denoise.cmdline                               # doctest: +ELLIPSIS
    'dwidenoise -mask mask.mif -noise noise.mif dwi.mif dwi_denoised.mif'
    >>> denoise.run()                                 # doctest: +SKIP
    Z
dwidenoiseN)	r   r   r   __doc___cmdr   
input_specr$   output_specr"   r"   r"   r#   r%   4   s   r%   c            	   @   s   e Zd ZeddddddZejddgddddd	d
dZej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dddZdS )MRDeGibbsInputSpecTz%sr   zinput DWI image)r   r   r   r   r   r   r   ,z-axes %sz]indicate the plane in which the data was acquired (axial = 0,1; coronal = 0,2; sagittal = 1,2)default_value
usedefaultsepZminlenmaxlenr   r      z-nshifts %dz1discretization of subpixel spacing (default = 20))r,   r-   r   r   z-minW %dzMleft border of window used for total variation (TV) computation (default = 1)   z-maxW %dzNright border of window used for total variation (TV) computation (default = 3)z%s_unrr   zthe output unringed DWI image)r   r   r   r   r   r   Nr   r   )r   r   r   r   r   r   ZListIntZaxesr   ZnshiftsZminWZmaxWr!   r"   r"   r"   r#   r*   Y   s@   r*   c               @   s   e Zd ZedddZdS )MRDeGibbsOutputSpeczthe output unringed DWI imageT)r   r   N)r   r   r   r   r!   r"   r"   r"   r#   r2      s   r2   c               @   s   e Zd ZdZdZeZeZdS )	MRDeGibbsa  
    Remove Gibbs ringing artifacts.

    This application attempts to remove Gibbs ringing artefacts from MRI images
    using the method of local subvoxel-shifts proposed by Kellner et al.

    This command is designed to run on data directly after it has been
    reconstructed by the scanner, before any interpolation of any kind has
    taken place. You should not run this command after any form of motion
    correction (e.g. not after dwipreproc). Similarly, if you intend running
    dwidenoise, you should run this command afterwards, since it has the
    potential to alter the noise structure, which would impact on dwidenoise's
    performance.

    Note that this method is designed to work on images acquired with full
    k-space coverage. Running this method on partial Fourier ('half-scan') data
    may lead to suboptimal and/or biased results, as noted in the original
    reference below. There is currently no means of dealing with this; users
    should exercise caution when using this method on partial Fourier data, and
    inspect its output for any obvious artefacts.

    For more information, see
    <https://mrtrix.readthedocs.io/en/latest/reference/commands/mrdegibbs.html>

    Example
    -------

    >>> import nipype.interfaces.mrtrix3 as mrt
    >>> unring = mrt.MRDeGibbs()
    >>> unring.inputs.in_file = 'dwi.mif'
    >>> unring.cmdline
    'mrdegibbs -axes 0,1 -maxW 3 -minW 1 -nshifts 20 dwi.mif dwi_unr.mif'
    >>> unring.run()                                 # doctest: +SKIP
    Z	mrdegibbsN)	r   r   r   r&   r'   r*   r(   r2   r)   r"   r"   r"   r#   r3      s   "r3   c            	   @   sx   e Zd ZeddddddZedddZejd	dd
ddgdZejdddddgdZ	edddZ
eddddddddZdS )DWIBiasCorrectInputSpecTz%sr   zinput DWI image)r   r   r   r   r   z-mask %sz*input mask image for bias field estimation)r   r   Zantsz/use ANTS N4 to estimate the inhomogeneity fieldr   use_fsl)r   r   r   r   xorZfslz0use FSL FAST to estimate the inhomogeneity fielduse_antsz-bias %sz
bias fieldz%s_biascorrr   r   z#the output bias corrected DWI image)r   r   r   r   r   r   ZgenfileNr   r   )r   r   r   r   r   in_maskr   Boolr7   r5   biasr!   r"   r"   r"   r#   r4      s0   

r4   c               @   s$   e Zd ZedddZedddZdS )DWIBiasCorrectOutputSpeczthe output bias fieldT)r   r   z#the output bias corrected DWI imageN)r   r   r   r   r:   r!   r"   r"   r"   r#   r;      s   r;   c                   s,   e Zd ZdZdZeZeZ fddZ	  Z
S )DWIBiasCorrecta)  
    Perform B1 field inhomogeneity correction for a DWI volume series.

    For more information, see
    <https://mrtrix.readthedocs.io/en/latest/reference/scripts/dwibiascorrect.html>

    Example
    -------

    >>> import nipype.interfaces.mrtrix3 as mrt
    >>> bias_correct = mrt.DWIBiasCorrect()
    >>> bias_correct.inputs.in_file = 'dwi.mif'
    >>> bias_correct.inputs.use_ants = True
    >>> bias_correct.cmdline
    'dwibiascorrect ants dwi.mif dwi_biascorr.mif'
    >>> bias_correct.run()                             # doctest: +SKIP
    Zdwibiascorrectc                sH   |dkr8| j }|d k	r8|d dk s,|jdr8d|j S t j|||S )Nr7   r5   r   3z3.0_RC-)r7   r5   )version
startswithr   super_format_arg)selfnameZ
trait_specvaluever)	__class__r"   r#   rB      s
    zDWIBiasCorrect._format_arg)r   r   r   r&   r'   r4   r(   r;   r)   rB   __classcell__r"   r"   )rG   r#   r<      s
   r<   c            
   @   s   e Zd ZeddddddZeddddddd	Zejd
ddddddddZej	ddddZ
ejdddZed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d#d$dZed%ddd&gd'd(Zejed)dd*d+ed,dd-d+d.d/gd0d1Zd2S )3DWIPreprocInputSpecTz%sr   zinput DWI image)r   r   r   r   r   zpreproc.mifr   zoutput file after preprocessing)r   r   r   r-   r   nonepairallheaderz-rpe_%sr   a3  Specify acquisition phase-encoding design. "none" for no reversed phase-encoding image, "all" for all DWIs have opposing phase-encoding acquisition, "pair" for using a pair of b0 volumes for inhomogeneity field estimation only, and "header" for phase-encoding information can be found in the image header(s))r   r   r   r   z
-pe_dir %szSpecify the phase encoding direction of the input series, can be a signed axis number (e.g. -0, 1, +2), an axis designator (e.g. RL, PA, IS), or NIfTI axis codes (e.g. i-, j, k))r   r   r   z-readout_time %fz/Total readout time of input series (in seconds))r   r   z
-se_epi %szProvide an additional image series consisting of spin-echo EPI images, which is to be used exclusively by topup for estimating the inhomogeneity field (i.e. it will not form part of the output image series))r   r   r   z-align_seepizaAchieve alignment between the SE-EPI images used for inhomogeneity field estimation, and the DWIsz-eddy_options "%s"zDManually provide additional command-line options to the eddy commandz-topup_options "%s"zEManually provide additional command-line options to the topup commandz-export_grad_mrtrixz*export new gradient files in mrtrix formatz-export_grad_fslz#export gradient files in FSL formatzgrad.bexport_grad_mrtrixzname of new gradient file)r   r-   requiresr   z
grad.bvecsZbvecs)r-   r   z
grad.bvalsZbvalsz%s, %sexport_grad_fslz*Output (bvecs, bvals) gradients FSL format)r   rO   r   N)r   r   r   r   r   r!   r   EnumZrpe_optionsStrZpe_dirZFloatZro_timeZin_epir9   Zalign_seepiZeddy_optionsZtopup_optionsrN   rP   out_grad_mrtrixr   out_grad_fslr"   r"   r"   r#   rI      sl   

rI   c               @   sH   e Zd ZedddZedddddZedddd	dZed
ddddZdS )DWIPreprocOutputSpecz%sz output preprocessed image series)r   r   zgrad.bTz,preprocessed gradient file in mrtrix3 format)r   r-   r   z
grad.bvecszexported fsl gradient bvec filez
grad.bvalszexported fsl gradient bval fileN)r   r   r   r   r!   rS   out_fsl_bvecout_fsl_bvalr"   r"   r"   r#   rU   <  s    rU   c               @   s$   e Zd ZdZdZeZeZdd Z	dS )
DWIPreproca~  
    Perform diffusion image pre-processing using FSL's eddy tool; including inhomogeneity distortion correction using FSL's topup tool if possible

    For more information, see
    <https://mrtrix.readthedocs.io/en/latest/reference/commands/dwifslpreproc.html>

    Example
    -------

    >>> import nipype.interfaces.mrtrix3 as mrt
    >>> preproc = mrt.DWIPreproc()
    >>> preproc.inputs.in_file = 'dwi.mif'
    >>> preproc.inputs.rpe_options = 'none'
    >>> preproc.inputs.out_file = "preproc.mif"
    >>> preproc.inputs.eddy_options = '--slm=linear --repol'     # linear second level model and replace outliers
    >>> preproc.inputs.export_grad_mrtrix = True    # export final gradient table in MRtrix format
    >>> preproc.inputs.ro_time = 0.165240   # 'TotalReadoutTime' in BIDS JSON metadata files
    >>> preproc.inputs.pe_dir = 'j'     # 'PhaseEncodingDirection' in BIDS JSON metadata files
    >>> preproc.cmdline
    'dwifslpreproc dwi.mif preproc.mif -rpe_none -eddy_options "--slm=linear --repol" -export_grad_mrtrix grad.b -pe_dir j -readout_time 0.165240'
    >>> preproc.run()                             # doctest: +SKIP
    Zdwifslpreprocc             C   sp   | j  j }tj| jj|d< | jjr8tj| jj|d< | jjrltj| jj	d |d< tj| jj	d |d< |S )Nr!   rS   r   rV   r   rW   )
r)   getopabspathinputsr!   rN   rS   rP   rT   )rC   outputsr"   r"   r#   _list_outputsn  s    zDWIPreproc._list_outputsN)
r   r   r   r&   r'   rI   r(   rU   r)   r^   r"   r"   r"   r#   rX   R  s
   rX   c            
   @   s   e Zd Zejddddddddd	ZeddddddZedd ddZ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eejddddZdS )$ResponseSDInputSpecZmsmt_5ttZ
dhollanderZtournierZtaxz%sr   Tz,response estimation algorithm (multi-tissue))r   r   r   r      zinput DWI image)r   r   r   r   r      zinput 5tt image)r   r   r   zwm.txtr1   zoutput WM response text file)r   r   r-   r   r   zoutput GM response text filezoutput CSF response text filez-mask %szprovide initial mask image)r   r   r   z-lmax %sr+   ztmaximum harmonic degree of response function - single value for single-shell response, list for multi-shell response)r   r.   r   Nr   r   )r   r   r   r   rQ   	algorithmr   r   Zmtt_filewm_filegm_filecsf_filer8   r
   r   Zmax_shr"   r"   r"   r#   r_   z  s4   r_   c               @   s0   e Zd ZedddZedddZedddZdS )ResponseSDOutputSpecz%szoutput WM response text file)r   r   zoutput GM response text filezoutput CSF response text fileN)r   r   r   r   rf   rg   rh   r"   r"   r"   r#   ri     s   ri   c               @   s$   e Zd ZdZdZeZeZdd Z	dS )
ResponseSDa  
    Estimate response function(s) for spherical deconvolution using the specified algorithm.

    Example
    -------

    >>> import nipype.interfaces.mrtrix3 as mrt
    >>> resp = mrt.ResponseSD()
    >>> resp.inputs.in_file = 'dwi.mif'
    >>> resp.inputs.algorithm = 'tournier'
    >>> resp.inputs.grad_fsl = ('bvecs', 'bvals')
    >>> resp.cmdline                               # doctest: +ELLIPSIS
    'dwi2response tournier -fslgrad bvecs bvals dwi.mif wm.txt'
    >>> resp.run()                                 # doctest: +SKIP

    # We can also pass in multiple harmonic degrees in the case of multi-shell
    >>> resp.inputs.max_sh = [6,8,10]
    >>> resp.cmdline
    'dwi2response tournier -fslgrad bvecs bvals -lmax 6,8,10 dwi.mif wm.txt'
    Zdwi2responsec             C   s^   | j  j }tj| jj|d< | jjtkr<tj| jj|d< | jjtkrZtj| jj|d< |S )Nrf   rg   rh   )	r)   rY   rZ   r[   r\   rf   rg   r	   rh   )rC   r]   r"   r"   r#   r^     s    zResponseSD._list_outputsN)
r   r   r   r&   r'   r_   r(   ri   r)   r^   r"   r"   r"   r#   rj     s
   rj   c               @   s2   e Zd ZeddddddZeddddddd	Zd
S )ACTPrepareFSLInputSpecTz%sr   zinput anatomical image)r   r   r   r   r   zact_5tt.mifr   zoutput file after processing)r   r   r   r-   r   Nr   r   )r   r   r   r   r   r!   r"   r"   r"   r#   rk     s   rk   c               @   s   e Zd ZedddZdS )ACTPrepareFSLOutputSpecTzthe output response file)r   r   N)r   r   r   r   r!   r"   r"   r"   r#   rl     s   rl   c               @   s$   e Zd ZdZdZeZeZdd Z	dS )ACTPrepareFSLa  
    Generate anatomical information necessary for Anatomically
    Constrained Tractography (ACT).

    Example
    -------

    >>> import nipype.interfaces.mrtrix3 as mrt
    >>> prep = mrt.ACTPrepareFSL()
    >>> prep.inputs.in_file = 'T1.nii.gz'
    >>> prep.cmdline                               # doctest: +ELLIPSIS
    'act_anat_prepare_fsl T1.nii.gz act_5tt.mif'
    >>> prep.run()                                 # doctest: +SKIP
    Zact_anat_prepare_fslc             C   s"   | j  j }tj| jj|d< |S )Nr!   )r)   rY   rZ   r[   r\   r!   )rC   r]   r"   r"   r#   r^     s    zACTPrepareFSL._list_outputsN)
r   r   r   r&   r'   rk   r(   rl   r)   r^   r"   r"   r"   r#   rm     s
   rm   c               @   sT   e Zd ZeddddddZeddddddZedddd	d
ZedddddddZdS )ReplaceFSwithFIRSTInputSpecTz%sra   zinput anatomical image)r   r   r   r   r   r1   zinput T1 imager   zconnectome configuration file)r   r   r   r   zaparc+first.mifr   zoutput file after processing)r   r   r   r-   r   Nrc   rd   r   r   )r   r   r   r   r   Zin_t1wZ	in_configr!   r"   r"   r"   r#   rn     s"   rn   c               @   s   e Zd ZedddZdS )ReplaceFSwithFIRSTOutputSpecTzthe output response file)r   r   N)r   r   r   r   r!   r"   r"   r"   r#   ro     s   ro   c               @   s$   e Zd ZdZdZeZeZdd Z	dS )ReplaceFSwithFIRSTaY  
    Replace deep gray matter structures segmented with FSL FIRST in a
    FreeSurfer parcellation.

    Example
    -------

    >>> import nipype.interfaces.mrtrix3 as mrt
    >>> prep = mrt.ReplaceFSwithFIRST()
    >>> prep.inputs.in_file = 'aparc+aseg.nii'
    >>> prep.inputs.in_t1w = 'T1.nii.gz'
    >>> prep.inputs.in_config = 'mrtrix3_labelconfig.txt'
    >>> prep.cmdline                               # doctest: +ELLIPSIS
    'fs_parc_replace_sgm_first aparc+aseg.nii T1.nii.gz mrtrix3_labelconfig.txt aparc+first.mif'
    >>> prep.run()                                 # doctest: +SKIP
    Zfs_parc_replace_sgm_firstc             C   s"   | j  j }tj| jj|d< |S )Nr!   )r)   rY   rZ   r[   r\   r!   )rC   r]   r"   r"   r#   r^   -  s    z ReplaceFSwithFIRST._list_outputsN)
r   r   r   r&   r'   rn   r(   ro   r)   r^   r"   r"   r"   r#   rp     s
   rp   )#os.pathpathrZ   baser   r   r   r   r   r   r	   r
   r   r   r   r$   r%   r*   r2   r3   r4   r;   r<   rI   rU   rX   r_   ri   rj   rk   rl   rm   rn   ro   rp   r"   r"   r"   r#   <module>   s.   
(
%,) F($$