3
Yd[V                 @   s  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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d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 d0d1 d1eZ'G d2d3 d3eZ(dS )4    N   )logging)split_filename   )CommandLineInputSpecCommandLineBaseInterfacetraitsFileTraitedSpec	isdefinedznipype.interfacec               @   s^   e Zd ZeddddddZe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dZdS )$DWI2SphericalHarmonicsImageInputSpecTz%sr   zDiffusion-weighted images)existsargstr	mandatorypositiondesc   zOutput filename)genfiler   r   r   z-grad %szGradient encoding, supplied as a 4xN text file with each line is in the format [ X Y Z b ], where [ X Y Z ] describe the direction of the applied gradient, and b gives the b-value in units (1000 s/mm^2). See FSL2MRTrixz-lmax %szset the maximum harmonic order for the output series. By default, the program will use the highest possible lmax given the number of diffusion-weighted images.)r   r   z
-normaliser   z(normalise the DW signal to the b=0 image)r   r   r   N)__name__
__module____qualname__r
   in_fileout_filenameencoding_filer	   Floatmaximum_harmonic_orderBool	normalise r!   r!   R/var/www/html/virt/lib/python3.6/site-packages/nipype/interfaces/mrtrix/tensors.pyr      s$   r   c               @   s   e Zd ZedddZdS )%DWI2SphericalHarmonicsImageOutputSpecTzSpherical harmonics image)r   r   N)r   r   r   r
   spherical_harmonics_imager!   r!   r!   r"   r#   0   s   r#   c               @   s4   e Zd ZdZdZeZeZdd Z	dd Z
dd Zd	S )
DWI2SphericalHarmonicsImagea  
    Convert base diffusion-weighted images to their spherical harmonic representation.

    This program outputs the spherical harmonic decomposition for the set measured signal attenuations.
    The signal attenuations are calculated by identifying the b-zero images from the diffusion encoding supplied
    (i.e. those with zero as the b-value), and dividing the remaining signals by the mean b-zero signal intensity.
    The spherical harmonic decomposition is then calculated by least-squares linear fitting.
    Note that this program makes use of implied symmetries in the diffusion profile.

    First, the fact the signal attenuation profile is real implies that it has conjugate symmetry,
    i.e. Y(l,-m) = Y(l,m)* (where * denotes the complex conjugate). Second, the diffusion profile should be
    antipodally symmetric (i.e. S(x) = S(-x)), implying that all odd l components should be zero. Therefore,
    this program only computes the even elements.

    Note that the spherical harmonics equations used here differ slightly from those conventionally used,
    in that the (-1)^m factor has been omitted. This should be taken into account in all subsequent calculations.

    Each volume in the output image corresponds to a different spherical harmonic component, according to the following convention:

    * [0] Y(0,0)
    * [1] Im {Y(2,2)}
    * [2] Im {Y(2,1)}
    * [3] Y(2,0)
    * [4] Re {Y(2,1)}
    * [5] Re {Y(2,2)}
    * [6] Im {Y(4,4)}
    * [7] Im {Y(4,3)}

    Example
    -------

    >>> import nipype.interfaces.mrtrix as mrt
    >>> dwi2SH = mrt.DWI2SphericalHarmonicsImage()
    >>> dwi2SH.inputs.in_file = 'diffusion.nii'
    >>> dwi2SH.inputs.encoding_file = 'encoding.txt'
    >>> dwi2SH.run()                                    # doctest: +SKIP
    Zdwi2SHc             C   sN   | j  j }| jj|d< t|d s8tj| j |d< ntj|d |d< |S )Nr$   )output_specgetinputsr   r   opabspath_gen_outfilename)selfoutputsr!   r!   r"   _list_outputs_   s    z)DWI2SphericalHarmonicsImage._list_outputsc             C   s   |dkr| j  S d S d S )Nr   )r+   )r,   namer!   r!   r"   _gen_filenamej   s    z)DWI2SphericalHarmonicsImage._gen_filenamec             C   s   t | jj\}}}|d S )Nz_SH.mif)r   r(   r   )r,   _r/   r!   r!   r"   r+   p   s    z,DWI2SphericalHarmonicsImage._gen_outfilenameN)r   r   r   __doc___cmdr   
input_specr#   r&   r.   r0   r+   r!   r!   r!   r"   r%   4   s   %r%   c               @   s   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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e
jdddZe
jdddZe
jdddZeddd'ddZe
jddd d!Zd"S )(*ConstrainedSphericalDeconvolutionInputSpecTz%sr   zdiffusion-weighted image)r   r   r   r   r   r   zdthe diffusion-weighted signal response function for a single fibre population (see EstimateResponse)r   zOutput filename)r   r   r   r   z-mask %szEonly perform computation within the specified binary brain mask image)r   r   r   r   z-grad %szGradient encoding, supplied as a 4xN text file with each line is in the format [ X Y Z b ], where [ X Y Z ] describe the direction of the applied gradient, and b gives the b-value in units (1000 s/mm^2). See FSL2MRTrixz
-filter %sza text file containing the filtering coefficients for each even harmonic order.the linear frequency filtering parameters used for the initial linear spherical deconvolution step (default = [ 1 1 1 0 0 ]).z
-lambda %szathe regularisation parameter lambda that controls the strength of the constraint (default = 1.0).)r   r   z-lmax %szset the maximum harmonic order for the output series. By default, the program will use the highest possible lmax given the number of diffusion-weighted images.z-threshold %szthe threshold below which the amplitude of the FOD is assumed to be zero, expressed as a fraction of the mean value of the initial FOD (default = 0.1)z	-niter %szIthe maximum number of iterations to perform for each voxel (default = 50)z-debugzDisplay debugging messages.z-directions %sza text file containing the [ el az ] pairs for the directions: Specify the directions over which to apply the non-negativity constraint (by default, the built-in 300 direction set is used)z
-normalisez(normalise the DW signal to the b=0 image)r   r   r   Nr   r   r   r   )r   r   r   r
   r   Zresponse_filer   
mask_imager   Zfilter_filer	   r   Zlambda_valueIntr   Zthreshold_valueZ
iterationsr   debugdirections_filer    r!   r!   r!   r"   r5   u   s`   r5   c               @   s   e Zd ZedddZdS )+ConstrainedSphericalDeconvolutionOutputSpecTzSpherical harmonics image)r   r   N)r   r   r   r
   r$   r!   r!   r!   r"   r;      s   r;   c               @   s4   e Zd ZdZdZeZeZdd Z	dd Z
dd Zd	S )
!ConstrainedSphericalDeconvolutionaH  
    Perform non-negativity constrained spherical deconvolution.

    Note that this program makes use of implied symmetries in the diffusion profile.
    First, the fact the signal attenuation profile is real implies that it has conjugate symmetry,
    i.e. Y(l,-m) = Y(l,m)* (where * denotes the complex conjugate). Second, the diffusion profile should be
    antipodally symmetric (i.e. S(x) = S(-x)), implying that all odd l components should be zero.
    Therefore, this program only computes the even elements. Note that the spherical harmonics equations used here
    differ slightly from those conventionally used, in that the (-1)^m factor has been omitted. This should be taken
    into account in all subsequent calculations. Each volume in the output image corresponds to a different spherical
    harmonic component, according to the following convention:

    * [0] Y(0,0)
    * [1] Im {Y(2,2)}
    * [2] Im {Y(2,1)}
    * [3] Y(2,0)
    * [4] Re {Y(2,1)}
    * [5] Re {Y(2,2)}
    * [6] Im {Y(4,4)}
    * [7] Im {Y(4,3)}

    Example
    -------

    >>> import nipype.interfaces.mrtrix as mrt
    >>> csdeconv = mrt.ConstrainedSphericalDeconvolution()
    >>> csdeconv.inputs.in_file = 'dwi.mif'
    >>> csdeconv.inputs.encoding_file = 'encoding.txt'
    >>> csdeconv.run()                                          # doctest: +SKIP
    Zcsdeconvc             C   sN   | j  j }| jj|d< t|d s8tj| j |d< ntj|d |d< |S )Nr$   )r&   r'   r(   r   r   r)   r*   r+   )r,   r-   r!   r!   r"   r.      s    z/ConstrainedSphericalDeconvolution._list_outputsc             C   s   |dkr| j  S d S d S )Nr   )r+   )r,   r/   r!   r!   r"   r0      s    z/ConstrainedSphericalDeconvolution._gen_filenamec             C   s   t | jj\}}}|d S )Nz_CSD.mif)r   r(   r   )r,   r1   r/   r!   r!   r"   r+      s    z2ConstrainedSphericalDeconvolution._gen_outfilenameN)r   r   r   r2   r3   r5   r4   r;   r&   r.   r0   r+   r!   r!   r!   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Ze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dddZejdddZdS )EstimateResponseForSHInputSpecTz%sr   zDiffusion-weighted images)r   r   r   r   r   r   zEonly perform computation within the specified binary brain mask image)r   r   r   r   r   r   zOutput filename)r   r   r   r   z-grad %szGradient encoding, supplied as a 4xN text file with each line is in the format [ X Y Z b ], where [ X Y Z ] describe the direction of the applied gradient, and b gives the b-value in units (1000 s/mm^2). See FSL2MRTrixz-lmax %szset the maximum harmonic order for the output series. By default, the program will use the highest possible lmax given the number of diffusion-weighted images.)r   r   z
-normalisez(normalise the DW signal to the b=0 imagez-quietz7Do not display information messages or progress status.z-debugzDisplay debugging messages.Nr6   r   r   )r   r   r   r
   r   r7   r   r   r	   r8   r   r   r    quietr9   r!   r!   r!   r"   r=      s6   

r=   c               @   s   e Zd ZedddZdS )EstimateResponseForSHOutputSpecTzSpherical harmonics image)r   r   N)r   r   r   r
   responser!   r!   r!   r"   r?     s   r?   c               @   s4   e Zd ZdZdZeZeZdd Z	dd Z
dd Zd	S )
EstimateResponseForSHa  
    Estimates the fibre response function for use in spherical deconvolution.

    Example
    -------

    >>> import nipype.interfaces.mrtrix as mrt
    >>> estresp = mrt.EstimateResponseForSH()
    >>> estresp.inputs.in_file = 'dwi.mif'
    >>> estresp.inputs.mask_image = 'dwi_WMProb.mif'
    >>> estresp.inputs.encoding_file = 'encoding.txt'
    >>> estresp.run()                                   # doctest: +SKIP
    Zestimate_responsec             C   sN   | j  j }| jj|d< t|d s8tj| j |d< ntj|d |d< |S )Nr@   )r&   r'   r(   r   r   r)   r*   r+   )r,   r-   r!   r!   r"   r.   /  s    z#EstimateResponseForSH._list_outputsc             C   s   |dkr| j  S d S d S )Nr   )r+   )r,   r/   r!   r!   r"   r0   8  s    z#EstimateResponseForSH._gen_filenamec             C   s   t | jj\}}}|d S )Nz_ER.txt)r   r(   r   )r,   r1   r/   r!   r!   r"   r+   >  s    z&EstimateResponseForSH._gen_outfilenameN)r   r   r   r2   r3   r=   r4   r?   r&   r.   r0   r+   r!   r!   r!   r"   rA     s   	rA   c             C   s.  t j| }t j|}t j|d t j|d kr:t j|}|rf|dd d f  |dd d f< tjd |r|dd d f  |dd d f< tjd |r|dd d f  |dd d f< tjd tjt j| tjt j| t jt j||f}t| \}}	}t|\}}
}|	d |
 d }t j|| |S )	Nr   r   z&Inverting b-vectors in the x directionz&Inverting b-vectors in the y directionr   z&Inverting b-vectors in the z directionr1   z.txt)	npZloadtxtshapeZ	transposeifloggerinfoZvstackr   Zsavetxt)	bvec_file	bval_fileinvert_xinvert_yinvert_zZbvecsZbvalsencodingr1   bvecbvalout_encoding_filer!   r!   r"   concat_filesC  s*    





rO   c               @   sd   e Z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
edd
dZdS )FSL2MRTrixInputSpecTz"FSL b-vectors file (3xN text file))r   r   r   z!FSL b-values file (1xN text file)Fz&Inverts the b-vectors along the x-axis)Z
usedefaultr   z&Inverts the b-vectors along the y-axisz&Inverts the b-vectors along the z-axiszOutput encoding filename)r   r   N)r   r   r   r
   rF   rG   r	   r   rH   rI   rJ   rN   r!   r!   r!   r"   rP   [  s   rP   c               @   s   e Zd ZeddZdS )FSL2MRTrixOutputSpeczThe gradient encoding, supplied as a 4xN text file with each line is in the format [ X Y Z b ], where [ X Y Z ] describe the direction of the applied gradientand b gives the b-value in units (1000 s/mm^2).)r   N)r   r   r   r
   r   r!   r!   r!   r"   rQ   n  s   rQ   c               @   s8   e Zd ZdZeZeZdd Zdd Z	dd Z
dd	 Zd
S )
FSL2MRTrixaN  
    Converts separate b-values and b-vectors from text files (FSL style) into a
    4xN text file in which each line is in the format [ X Y Z b ], where [ X Y Z ]
    describe the direction of the applied gradient, and b gives the
    b-value in units (1000 s/mm^2).

    Example
    -------

    >>> import nipype.interfaces.mrtrix as mrt
    >>> fsl2mrtrix = mrt.FSL2MRTrix()
    >>> fsl2mrtrix.inputs.bvec_file = 'bvecs'
    >>> fsl2mrtrix.inputs.bval_file = 'bvals'
    >>> fsl2mrtrix.inputs.invert_y = True
    >>> fsl2mrtrix.run()                                # doctest: +SKIP
    c             C   s(   t | jj| jj| jj| jj| jj}|S )N)rO   r(   rF   rG   rH   rI   rJ   )r,   ZruntimerK   r!   r!   r"   _run_interface  s    
zFSL2MRTrix._run_interfacec             C   s$   | j  j }tj| jd|d< |S )NrN   r   )r&   r'   r)   r*   r0   )r,   r-   r!   r!   r"   r.     s    zFSL2MRTrix._list_outputsc             C   s   |dkr| j  S d S d S )NrN   )r+   )r,   r/   r!   r!   r"   r0     s    zFSL2MRTrix._gen_filenamec             C   s4   t | jj\}}}t | jj\}}}|d | d S )Nr1   z.txt)r   r(   rF   rG   )r,   r1   rL   rM   r!   r!   r"   r+     s    zFSL2MRTrix._gen_outfilenameN)r   r   r   r2   rP   r4   rQ   r&   rS   r.   r0   r+   r!   r!   r!   r"   rR   u  s   
rR   c               @   sz   e Zd Zejd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gddddddZdS )GenerateDirectionsInputSpecTz%sr   z%the number of directions to generate.)r   r   r   r   z	-power %sz0specify exponent to use for repulsion power law.)r   r   z	-niter %sz4specify the maximum number of iterations to perform.z-infozDisplay information messages.z-quietz7do not display information messages or progress status.z-debugzDisplay debugging messages.num_dirszdirections_%d.txtFr   z=the text file to write the directions to, as [ az el ] pairs.)name_sourcename_templater   
hash_filesr   r   Nr   r   )r   r   r   r	   r8   rU   r   powerZniterr   display_infoquiet_displaydisplay_debugr
   out_filer!   r!   r!   r"   rT     s(   


rT   c               @   s   e Zd ZedddZdS )GenerateDirectionsOutputSpecTzdirections file)r   r   N)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 )GenerateDirectionsa3  
    generate a set of directions evenly distributed over a hemisphere.

    Example
    -------

    >>> import nipype.interfaces.mrtrix as mrt
    >>> gendir = mrt.GenerateDirections()
    >>> gendir.inputs.num_dirs = 300
    >>> gendir.run()                                          # doctest: +SKIP
    ZgendirN)	r   r   r   r2   r3   rT   r4   r^   r&   r!   r!   r!   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Zeddd	d
ZejdddZ	ej
ej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ejdddZeddddd#ddgdZd S )$FindShPeaksInputSpecTz%sr   z#the input image of SH coefficients.)r   r   r   r   r   r   z:the set of directions to use as seeds for the peak findingz	-peaks %szZthe program will try to find the peaks that most closely match those in the image provided)r   r   r   z-num %sz-the number of peaks to extract (default is 3))r   r   z-direction %s a!  phi theta.  the direction of a peak to estimate. The algorithm will attempt to find the same number of peaks as have been specified using this option  phi: the azimuthal angle of the direction (in degrees). theta: the elevation angle of the direction (in degrees, from the vertical z-axis))r   sepminlenmaxlenr   z-threshold %szBonly peak amplitudes greater than the threshold will be consideredz-infozDisplay information messages.z-quietz7do not display information messages or progress status.z-debugzDisplay debugging messages.z%s_peak_dirs.mifFr   zithe output image. Each volume corresponds to the x, y & z component of each peak direction vector in turnr   )rW   keep_extensionr   rX   r   r   rV   Nr6   r   r   )r   r   r   r
   r   r:   peaks_imager	   r8   	num_peaksListr   peak_directionsZpeak_thresholdr   rZ   r[   r\   r]   r!   r!   r!   r"   r`     sP   

r`   c               @   s   e Zd ZedddZdS )FindShPeaksOutputSpecTzPeak directions image)r   r   N)r   r   r   r
   r]   r!   r!   r!   r"   rj     s   rj   c               @   s   e Zd ZdZdZeZeZdS )FindShPeaksa  
    identify the orientations of the N largest peaks of a SH profile

    Example
    -------

    >>> import nipype.interfaces.mrtrix as mrt
    >>> shpeaks = mrt.FindShPeaks()
    >>> shpeaks.inputs.in_file = 'csd.mif'
    >>> shpeaks.inputs.directions_file = 'dirs.txt'
    >>> shpeaks.inputs.num_peaks = 2
    >>> shpeaks.run()                                          # doctest: +SKIP
    Zfind_SH_peaksN)	r   r   r   r2   r3   r`   r4   rj   r&   r!   r!   r!   r"   rk     s   rk   c            	   @   s   e Zd ZeddddddZeddddZejd	d
dZej	ej
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edddddddgdZdS )Directions2AmplitudeInputSpecTz%sr   zothe input directions image. Each volume corresponds to the x, y & z component of each direction vector in turn.)r   r   r   r   r   z	-peaks %szZthe program will try to find the peaks that most closely match those in the image provided)r   r   r   z-num %sz-the number of peaks to extract (default is 3))r   r   z-direction %sra   a!  phi theta.  the direction of a peak to estimate. The algorithm will attempt to find the same number of peaks as have been specified using this option  phi: the azimuthal angle of the direction (in degrees). theta: the elevation angle of the direction (in degrees, from the vertical z-axis))r   rb   rc   rd   r   z-infozDisplay information messages.z-quietz7do not display information messages or progress status.z-debugzDisplay debugging messages.z%s_amplitudes.mifFr   zthe output amplitudes imager   )rW   re   r   rX   r   r   rV   Nr   r   )r   r   r   r
   r   rf   r	   r8   rg   rh   r   ri   r   rZ   r[   r\   r]   r!   r!   r!   r"   rl   $  s>   

rl   c               @   s   e Zd ZedddZdS )Directions2AmplitudeOutputSpecTzamplitudes image)r   r   N)r   r   r   r
   r]   r!   r!   r!   r"   rm   M  s   rm   c               @   s   e Zd ZdZdZeZeZdS )Directions2Amplitudea6  
    convert directions image to amplitudes

    Example
    -------

    >>> import nipype.interfaces.mrtrix as mrt
    >>> amplitudes = mrt.Directions2Amplitude()
    >>> amplitudes.inputs.in_file = 'peak_directions.mif'
    >>> amplitudes.run()                                          # doctest: +SKIP
    Zdir2ampN)	r   r   r   r2   r3   rl   r4   rm   r&   r!   r!   r!   r"   rn   Q  s   rn   ))os.pathpathr)   ZnumpyrB    r   Zutils.filemanipr   baser   r   r   r	   r
   r   r   	getLoggerrD   r   r#   r%   r5   r;   r<   r=   r?   rA   rO   rP   rQ   rR   rT   r^   r_   r`   rj   rk   rl   rm   rn   r!   r!   r!   r"   <module>   s6   
$

AA:$'04)