3
d:                 @   s  d Z ddlZddlZddl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/d0 d0eZ G d1d2 d2eZ!G d3d4 d4eZ"G d5d6 d6eZ#G d7d8 d8eZ$G d9d: d:eZ%G d;d< d<eZ&G d=d> d>eZ'G d?d@ d@e'Z(G dAdB dBeZ)G dCdD dDe'Z*G dEdF dFeZ+G dGdH dHe'Z,G dIdJ dJeZ-G dKdL dLeZ.G dMdN dNeZ/G dOdP dPeZ0G dQdR dReZ1G dSdT dTeZ2G dUdV dVeZ3G dWdX dXeZ4G dYdZ dZeZ5dS )[z
The maths module provides higher-level interfaces to some of the operations
that can be performed with the fslmaths command-line program.
    N   )TraitedSpecFiletraitsInputMultiPath	isdefined   )
FSLCommandFSLCommandInputSpecc               @   sx   e Zd ZeddddddZeddddddZd	d
ddddgZejeddddZ	ejeddddZ
ejddddZdS )
MathsInputr   z%sTzimage to operate on)positionargstrexists	mandatorydesczimage to writeF)Zgenfiler   r   r   Z
hash_filesfloatcharintshortdoubleinputr   z-dt %sz3datatype to use for calculations (default is float))r   r   r   z-odt %sz4datatype to use for output (default uses input type)   z-nanz*change NaNs to zeros before doing anythingN)__name__
__module____qualname__r   in_fileout_file_dtypesr   EnumZinternal_datatypeoutput_datatypeBoolZ	nan2zeros r#   r#   =/tmp/pip-build-7vycvbft/nipype/nipype/interfaces/fsl/maths.pyr      s"   

r   c               @   s   e Zd ZeddZdS )MathsOutputz image written after calculations)r   N)r   r   r   r   r   r#   r#   r#   r$   r%   *   s   r%   c               @   s,   e Zd ZdZeZeZdZdd Z	dd Z
dS )MathsCommandZfslmathsZ_mathsc             C   sT   | j  j }| jj|d< t| jjs<| j| jj| jd|d< tj	j
|d |d< |S )Nr   )suffix)output_specgetinputsr   r   Z
_gen_fnamer   _suffixospathabspath)selfoutputsr#   r#   r$   _list_outputs6   s    zMathsCommand._list_outputsc             C   s   |dkr| j  d S d S )Nr   )r1   )r/   namer#   r#   r$   _gen_filename@   s    zMathsCommand._gen_filenameN)r   r   r   Z_cmdr   
input_specr%   r(   r+   r1   r3   r#   r#   r#   r$   r&   /   s   
r&   c               @   s2   e Zd ZddddddgZejeddd	d
dZdS )ChangeDataTypeInputr   r   r   r   r   r   r   z-odt %sTzoutput data type)r   r   r   r   Nr   )r   r   r   r   r   r    r!   r#   r#   r#   r$   r5   F   s   r5   c               @   s   e Zd ZdZeZdZdS )ChangeDataTypez0Use fslmaths to change the datatype of an image.Z_chdtN)r   r   r   __doc__r5   r4   r+   r#   r#   r#   r$   r6   N   s   r6   c               @   sL   e Zd ZejdddddZejddddd	Zejd
dZ	ejddgdZ
dS )ThresholdInputSpecT   z%szthreshold value)r   r   r   r   Zbelowabovez+zero-out either below or above thresh value)
usedefaultr   z6interpret thresh as percentage (0-100) of robust range)r   z,use nonzero voxels to calculate robust rangeuse_robust_range)r   requiresN)r   r   r   r   Floatthreshr    	directionr"   r<   use_nonzero_voxelsr#   r#   r#   r$   r8   U   s   r8   c                   s(   e Zd ZdZeZdZ fddZ  ZS )	ThresholdzCUse fslmaths to apply a threshold to an image in a variety of ways.Z_threshc                s   |dkrpd}| j }| j jdkr&|d7 }|d7 }t|jr`|jr`t|jrX|jrX|d7 }n|d7 }|d| 7 }|S tt| j|||S )	Nr?   -r:   uZthrPpz %.10f)r*   r@   r   r<   rA   superrB   _format_arg)r/   r2   specvalueargZ_si)	__class__r#   r$   rH   o   s    
zThreshold._format_arg)	r   r   r   r7   r8   r4   r+   rH   __classcell__r#   r#   )rL   r$   rB   i   s   rB   c            
   @   s&   e Zd Zejddddddddd	Zd
S )StdImageInputTXYZTz-%sstdr9   z$dimension to standard deviate across)r;   r   r   r   N)r   r   r   r   r    	dimensionr#   r#   r#   r$   rN      s   rN   c               @   s   e Zd ZdZeZdZdS )StdImagez\Use fslmaths to generate a standard deviation in an image across a given
    dimension.
    Z_stdN)r   r   r   r7   rN   r4   r+   r#   r#   r#   r$   rT      s   rT   c            
   @   s&   e Zd Zejddddddddd	Zd
S )MeanImageInputrO   rP   rQ   rR   Tz-%smeanr9   zdimension to mean across)r;   r   r   r   N)r   r   r   r   r    rS   r#   r#   r#   r$   rU      s   rU   c               @   s   e Zd ZdZeZdZdS )	MeanImagez?Use fslmaths to generate a mean image across a given dimension.Z_meanN)r   r   r   r7   rU   r4   r+   r#   r#   r#   r$   rV      s   rV   c            
   @   s&   e Zd Zejddddddddd	Zd
S )MaxImageInputrO   rP   rQ   rR   Tz-%smaxr9   zdimension to max across)r;   r   r   r   N)r   r   r   r   r    rS   r#   r#   r#   r$   rW      s   rW   c               @   s   e Zd ZdZeZdZdS )MaxImagear  Use fslmaths to generate a max image across a given dimension.

    Examples
    --------
    >>> from nipype.interfaces.fsl.maths import MaxImage
    >>> maxer = MaxImage()
    >>> maxer.inputs.in_file = "functional.nii"  # doctest: +SKIP
    >>> maxer.dimension = "T"
    >>> maxer.cmdline  # doctest: +SKIP
    'fslmaths functional.nii -Tmax functional_max.nii'

    Z_maxN)r   r   r   r7   rW   r4   r+   r#   r#   r#   r$   rX      s   rX   c            
   @   s:   e Zd Zejddddddddd	Zejd
dddddZdS )PercentileImageInputrO   rP   rQ   rR   Tz-%spercr9   zdimension to percentile across)r;   r   r   r   r   d   z%f   z5nth percentile (0-100) of FULL RANGE across dimension)lowhighr   r   r   N)r   r   r   r   r    rS   ZRangepercr#   r#   r#   r$   rY      s   rY   c               @   s   e Zd ZdZeZdZdS )PercentileImagea  Use fslmaths to generate a percentile image across a given dimension.

    Examples
    --------
    >>> from nipype.interfaces.fsl.maths import MaxImage
    >>> percer = PercentileImage()
    >>> percer.inputs.in_file = "functional.nii"  # doctest: +SKIP
    >>> percer.dimension = "T"
    >>> percer.perc = 90
    >>> percer.cmdline  # doctest: +SKIP
    'fslmaths functional.nii -Tperc 90 functional_perc.nii'

    Z_percN)r   r   r   r7   rY   r4   r+   r#   r#   r#   r$   r_      s   r_   c            
   @   s&   e Zd Zejddddddddd	Zd
S )MaxnImageInputrO   rP   rQ   rR   Tz-%smaxnr9   zdimension to index max across)r;   r   r   r   N)r   r   r   r   r    rS   r#   r#   r#   r$   r`      s   r`   c               @   s   e Zd ZdZeZdZdS )	MaxnImagezUUse fslmaths to generate an image of index of max across
    a given dimension.

    Z_maxnN)r   r   r   r7   r`   r4   r+   r#   r#   r#   r$   ra     s   ra   c            
   @   s&   e Zd Zejddddddddd	Zd
S )MinImageInputrO   rP   rQ   rR   Tz-%sminr9   zdimension to min across)r;   r   r   r   N)r   r   r   r   r    rS   r#   r#   r#   r$   rb     s   rb   c               @   s   e Zd ZdZeZdZdS )MinImagezBUse fslmaths to generate a minimum image across a given dimension.Z_minN)r   r   r   r7   rb   r4   r+   r#   r#   r#   r$   rc     s   rc   c            
   @   s&   e Zd Zejddddddddd	Zd
S )MedianImageInputrO   rP   rQ   rR   Tz	-%smedianr9   zdimension to median across)r;   r   r   r   N)r   r   r   r   r    rS   r#   r#   r#   r$   rd   !  s   rd   c               @   s   e Zd ZdZeZdZdS )MedianImagezAUse fslmaths to generate a median image across a given dimension.Z_medianN)r   r   r   r7   rd   r4   r+   r#   r#   r#   r$   re   /  s   re   c            
   @   s&   e Zd Zejddddddddd	Zd
S )AR1ImageInputrO   rP   rQ   rR   Tz-%sar1r9   z)dimension to find AR(1) coefficientacross)r;   r   r   r   N)r   r   r   r   r    rS   r#   r#   r#   r$   rf   6  s   rf   c               @   s   e Zd ZdZeZdZdS )AR1ImagezUse fslmaths to generate an AR1 coefficient image across a
    given dimension. (Should use -odt float and probably demean first)

    Z_ar1N)r   r   r   r7   rf   r4   r+   r#   r#   r#   r$   rg   D  s   rg   c               @   s8   e Zd ZejddgddddZejddgddddZd	S )
IsotropicSmoothInputTsigmar9   z-s %.5fzfwhm of smoothing kernel [mm])r   xorr   r   r   fwhmzsigma of smoothing kernel [mm]N)r   r   r   r   r>   rk   ri   r#   r#   r#   r$   rh   N  s   rh   c                   s(   e Zd ZdZeZdZ fddZ  ZS )IsotropicSmoothzAUse fslmaths to spatially smooth an image with a gaussian kernel.Z_smoothc                sB   |dkr.t |tjdtjd  }|j| S tt| j|||S )Nrk      r   )r   npsqrtlogr   rG   rl   rH   )r/   r2   rI   rJ   ri   )rL   r#   r$   rH   f  s    
zIsotropicSmooth._format_arg)	r   r   r   r7   rh   r4   r+   rH   rM   r#   r#   )rL   r$   rl   `  s   rl   c               @   s   e Zd ZeddddddZdS )ApplyMaskInputTz-mas %sr9   z binary image defining mask space)r   r   r   r   r   N)r   r   r   r   Z	mask_filer#   r#   r#   r$   rq   m  s   rq   c               @   s   e Zd ZdZeZdZdS )	ApplyMaskz5Use fslmaths to apply a binary mask to another image.Z_maskedN)r   r   r   r7   rq   r4   r+   r#   r#   r#   r$   rr   x  s   rr   c               @   sR   e Zd Zejddddddddd	d
d
ZejdddgddZeddddgddZ	dS )KernelInputZ3DZ2DZboxZboxvgaussZspherefilez
-kernel %sr9   zkernel shape to use)r   r   r   z%.4fr[   kernel_filezDkernel size - voxels for box/boxv, mm for sphere, mm sigma for gauss)r   r   rj   r   Tz%skernel_sizezuse external file for kernel)r   r   r   rj   r   N)
r   r   r   r   r    Zkernel_shaper>   rw   r   rv   r#   r#   r#   r$   rs     s,   rs   c            	   @   s$   e Zd ZejddddddddZd	S )
DilateInputmeanmodalmaxz-dil%s   Tz*filtering operation to perfoem in dilation)r   r   r   r   N)r   r   r   r   r    	operationr#   r#   r#   r$   rx     s   rx   c                   s(   e Zd ZdZeZdZ fddZ  ZS )DilateImagez7Use fslmaths to perform a spatial dilation of an image.Z_dilc                s4   |dkr |j tdddd|  S tt| j|||S )Nr}   MDF)ry   rz   r{   )r   dictrG   r~   rH   )r/   r2   rI   rJ   )rL   r#   r$   rH     s    zDilateImage._format_arg)	r   r   r   r7   rx   r4   r+   rH   rM   r#   r#   )rL   r$   r~     s   r~   c               @   s    e Zd ZejddddddZdS )
ErodeInputz%sr|   TFz:if true, minimum filter rather than erosion by zeroing-out)r   r   r;   default_valuer   N)r   r   r   r   r"   minimum_filterr#   r#   r#   r$   r     s   r   c                   s(   e Zd ZdZeZdZ fddZ  ZS )
ErodeImagez6Use fslmaths to perform a spatial erosion of an image.Z_eroc                s(   |dkr|rdS dS t t| j|||S )Nr   z-eroFz-ero)rG   r   rH   )r/   r2   rI   rJ   )rL   r#   r$   rH     s
    zErodeImage._format_arg)	r   r   r   r7   r   r4   r+   rH   rM   r#   r#   )rL   r$   r     s   r   c            	   @   s$   e Zd ZejddddddddZd	S )
SpatialFilterInputry   ZmedianZmeanuz-f%sr|   Tzoperation to filter with)r   r   r   r   N)r   r   r   r   r    r}   r#   r#   r#   r$   r     s   r   c               @   s   e Zd ZdZeZdZdS )SpatialFilterz*Use fslmaths to spatially filter an image._filtN)r   r   r   r7   r   r4   r+   r#   r#   r#   r$   r     s   r   c               @   sL   e Zd Zejddddddddd	d
ddddddddddddddddddZdS )UnaryMathsInputexprp   sincostanasinacosatanZsqrro   ZrecipabsbinZbinvZfillhZfillh26indexZedgenanZnanmZrandZrandnrangez-%sr9   Tzoperation to perform)r   r   r   r   N)r   r   r   r   r    r}   r#   r#   r#   r$   r     s8   r   c                   s$   e Zd ZdZeZ fddZ  ZS )
UnaryMathszHUse fslmaths to perorm a variety of mathematical operations on an image.c                s   d| j j | _tt| j S )N_)r*   r}   r+   rG   r   r1   )r/   )rL   r#   r$   r1   
  s    zUnaryMaths._list_outputs)r   r   r   r7   r   r4   r1   rM   r#   r#   )rL   r$   r     s   r   c               @   sX   e Zd Zejddddddddd	d
ddZedddddgddZejddddgddZ	dS )BinaryMathsInputaddsubmuldivremr{   minTz-%sr9   zoperation to perform)r   r   r   r   z%sr[   operand_valuez&second image to perform operation with)r   r   r   r   rj   r   z%.8foperand_filezvalue to perform operation with)r   r   r   rj   r   N)
r   r   r   r   r    r}   r   r   r>   r   r#   r#   r#   r$   r     s2   r   c               @   s   e Zd ZdZeZdS )BinaryMathszbUse fslmaths to perform mathematical operations using a second image or
    a numeric value.

    N)r   r   r   r7   r   r4   r#   r#   r#   r$   r   /  s   r   c               @   s2   e Zd ZejdddddZeedddddZd	S )
MultiImageMathsInputr9   z%sTz0python formatted string of operations to perform)r   r   r   r   )r   z)list of file names to plug into op string)r   r   N)	r   r   r   r   String	op_stringr   r   operand_filesr#   r#   r#   r$   r   8  s   r   c                   s$   e Zd ZdZeZ fddZ  ZS )MultiImageMathsa  Use fslmaths to perform a sequence of mathematical operations.

    Examples
    --------
    >>> from nipype.interfaces.fsl import MultiImageMaths
    >>> maths = MultiImageMaths()
    >>> maths.inputs.in_file = "functional.nii"
    >>> maths.inputs.op_string = "-add %s -mul -1 -div %s"
    >>> maths.inputs.operand_files = ["functional2.nii", "functional3.nii"]
    >>> maths.inputs.out_file = "functional4.nii"
    >>> maths.cmdline
    'fslmaths functional.nii -add functional2.nii -mul -1 -div functional3.nii functional4.nii'

    c                s,   |dkr|t | jj S tt| j|||S )Nr   )tupler*   r   rG   r   rH   )r/   r2   rI   rJ   )rL   r#   r$   rH   Y  s    zMultiImageMaths._format_arg)r   r   r   r7   r   r4   rH   rM   r#   r#   )rL   r$   r   G  s   r   c               @   s4   e Zd ZejddddddZejddddd	dZd
S )TemporalFilterInputr   z%.6fr[   Tz!lowpass filter sigma (in volumes))r   r   r;   r   z
-bptf %.6fr9   z"highpass filter sigma (in volumes)Nr   r   )r   r   r   r   r>   Zlowpass_sigmaZhighpass_sigmar#   r#   r#   r$   r   _  s   r   c               @   s   e Zd ZdZeZdZdS )TemporalFilterzYUse fslmaths to apply a low, high, or bandpass temporal filter to a
    timeseries.

    r   N)r   r   r   r7   r   r4   r+   r#   r#   r#   r$   r   q  s   r   )6r7   r,   numpyrn   baser   r   r   r   r   r	   r
   r   r%   r&   r5   r6   r8   rB   rN   rT   rU   rV   rW   rX   rY   r_   r`   ra   rb   rc   rd   re   rf   rg   rh   rl   rq   rr   rs   rx   r~   r   r   r   r   r   r   r   r   r   r   r   r   r#   r#   r#   r$   <module>   s\   	

!
 	