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 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dS )z
The stats module provides higher-level interfaces to some of the operations
that can be performed with the niftyseg stats (seg_stats) command-line program.
    N   )TraitedSpecFiletraitsCommandLineInputSpec   )NiftySegCommand)get_custom_pathc               @   sF   e Zd ZdZeddddddZeddddd	Zd
Zej	ddedZ
dS )
StatsInputz$Input Spec for seg_stats interfaces.r   z%sTzimage to operate on)positionargstrexists	mandatorydescz-m %sz!statistics within the masked area)r   r   r   r   z8Only estimate statistics if voxel is larger than <float>z-t %f   )r   r   r   N)__name__
__module____qualname____doc__r   in_fileZ	mask_filer   r   FloatZlarger_voxel r   r   B/tmp/pip-build-7vycvbft/nipype/nipype/interfaces/niftyseg/stats.pyr
      s   r
   c               @   s   e Zd ZdZejddZdS )StatsOutputz%Output Spec for seg_stats interfaces.zOutput array from seg_stats)r   N)r   r   r   r   r   Arrayoutputr   r   r   r   r   !   s   r   c                   sD   e Zd ZdZedddZeZeZ	dd Z
 fddZd	d
 Z  ZS )StatsCommanda  
    Base Command Interface for seg_stats interfaces.

    The executable seg_stats enables the estimation of image statistics on
    continuous voxel intensities (average, standard deviation, min/max, robust
    range, percentiles, sum, probabilistic volume, entropy, etc) either over
    the full image or on a per slice basis (slice axis can be specified),
    statistics over voxel coordinates (location of max, min and centre of
    mass, bounding box, etc) and statistics over categorical images (e.g. per
    region volume, count, average, Dice scores, etc). These statistics are
    robust to the presence of NaNs, and can be constrained by a mask and/or
    thresholded at a certain level.
    Z	seg_statsZNIFTYSEGDIR)Zenv_dirc             C   s\   g }xH|j dD ]:}|jdr qt|dkr.qdd |j  D }|j| qW tj|j S )N
#r   c             S   s   g | ]}t |qS r   )float).0sr   r   r   
<listcomp>A   s    z.StatsCommand._parse_stdout.<locals>.<listcomp>)split
startswithlenappendnparrayZsqueeze)selfstdoutoutZstring_lineliner   r   r   _parse_stdout:   s    
zStatsCommand._parse_stdoutc                s"   t t| j|}| j|j| _|S )N)superr   _run_interfacer/   r,   r   )r+   ZruntimeZnew_runtime)	__class__r   r   r1   E   s    zStatsCommand._run_interfacec             C   s   | j  j }| j|d< |S )Nr   )output_specgetr   )r+   outputsr   r   r   _list_outputsJ   s    
zStatsCommand._list_outputs)r   r   r   r   r	   Z_cmdr
   
input_specr   r3   r/   r1   r6   __classcell__r   r   )r2   r   r   '   s   r   c               @   sD   e Zd 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ZdS )UnaryStatsInputz*Input Spec for seg_stats unary operations.rRar#   vZvlZvpnr)   enexXcBZxvoxZxdimz-%s   Tam  Operation to perform:

    * r - The range <min max> of all voxels.
    * R - The robust range (assuming 2% outliers on both sides) of all voxels
    * a - Average of all voxels
    * s - Standard deviation of all voxels
    * v - Volume of all voxels above 0 (<# voxels> * <volume per voxel>)
    * vl - Volume of each integer label (<# voxels per label> x <volume per voxel>)
    * vp - Volume of all probabilsitic voxels (sum(<in>) x <volume per voxel>)
    * n - Count of all voxels above 0 (<# voxels>)
    * np - Sum of all fuzzy voxels (sum(<in>))
    * e - Entropy of all voxels
    * ne - Normalized entropy of all voxels
    * x - Location (i j k x y z) of the smallest value in the image
    * X - Location (i j k x y z) of the largest value in the image
    * c - Location (i j k x y z) of the centre of mass of the object
    * B - Bounding box of all nonzero voxels [ xmin xsize ymin ysize zmin zsize ]
    * xvox - Output the number of voxels in the x direction.
      Replace x with y/z for other directions.
    * xdim - Output the voxel dimention in the x direction.
      Replace x with y/z for other directions.

)r   r   r   r   N)r   r   r   r   r   Enum	operationr   r   r   r   r9   P   s.   r9   c               @   s   e Zd ZdZeZdS )
UnaryStatsa  Unary statistical operations.

    See Also
    --------
    `Source code <http://cmictig.cs.ucl.ac.uk/wiki/index.php/NiftySeg>`__ --
    `Documentation <http://cmictig.cs.ucl.ac.uk/wiki/index.php/NiftySeg_documentation>`__

    Examples
    --------
    >>> import copy
    >>> from nipype.interfaces import niftyseg
    >>> unary = niftyseg.UnaryStats()
    >>> unary.inputs.in_file = 'im1.nii'

    >>> # Test v operation
    >>> unary_v = copy.deepcopy(unary)
    >>> unary_v.inputs.operation = 'v'
    >>> unary_v.cmdline
    'seg_stats im1.nii -v'
    >>> unary_v.run()  # doctest: +SKIP

    >>> # Test vl operation
    >>> unary_vl = copy.deepcopy(unary)
    >>> unary_vl.inputs.operation = 'vl'
    >>> unary_vl.cmdline
    'seg_stats im1.nii -vl'
    >>> unary_vl.run()  # doctest: +SKIP

    >>> # Test x operation
    >>> unary_x = copy.deepcopy(unary)
    >>> unary_x.inputs.operation = 'x'
    >>> unary_x.cmdline
    'seg_stats im1.nii -x'
    >>> unary_x.run()  # doctest: +SKIP

    N)r   r   r   r   r9   r7   r   r   r   r   rH      s   $rH   c               @   sb   e Zd ZdZejddd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 )BinaryStatsInputz+Input Spec for seg_stats Binary operations.psassZsvpaldZnccZnmiZVlZNlTz-%srE   a  Operation to perform:

    * p - <float> - The <float>th percentile of all voxels intensity (float=[0,100])
    * sa - <ax> - Average of all voxels
    * ss - <ax> - Standard deviation of all voxels
    * svp - <ax> - Volume of all probabilsitic voxels (sum(<in>) x <volume per voxel>)
    * al - <in2> - Average value in <in> for each label in <in2>
    * d - <in2> - Calculate the Dice score between all classes in <in> and <in2>
    * ncc - <in2> - Normalized cross correlation between <in> and <in2>
    * nmi - <in2> - Normalized Mutual Information between <in> and <in2>
    * Vl - <csv> - Volume of each integer label <in>. Save to <csv>file.
    * Nl - <csv> - Count of each label <in>. Save to <csv> file.

)r   r   r   r   z%s   operand_valuez&second image to perform operation with)r   r   r   r   xorr   z%.8foperand_filezvalue to perform operation with)r   r   r   rQ   r   N)r   r   r   r   r   rF   rG   r   rR   r   rP   r   r   r   r   rI      s:   rI   c               @   s   e Zd ZdZeZdS )BinaryStatsa  Binary statistical operations.

    See Also
    --------
    `Source code <http://cmictig.cs.ucl.ac.uk/wiki/index.php/NiftySeg>`__ --
    `Documentation <http://cmictig.cs.ucl.ac.uk/wiki/index.php/NiftySeg_documentation>`__

    Examples
    --------
    >>> import copy
    >>> from nipype.interfaces import niftyseg
    >>> binary = niftyseg.BinaryStats()
    >>> binary.inputs.in_file = 'im1.nii'
    >>> # Test sa operation
    >>> binary_sa = copy.deepcopy(binary)
    >>> binary_sa.inputs.operation = 'sa'
    >>> binary_sa.inputs.operand_value = 2.0
    >>> binary_sa.cmdline
    'seg_stats im1.nii -sa 2.00000000'
    >>> binary_sa.run()  # doctest: +SKIP
    >>> # Test ncc operation
    >>> binary_ncc = copy.deepcopy(binary)
    >>> binary_ncc.inputs.operation = 'ncc'
    >>> binary_ncc.inputs.operand_file = 'im2.nii'
    >>> binary_ncc.cmdline
    'seg_stats im1.nii -ncc im2.nii'
    >>> binary_ncc.run()  # doctest: +SKIP
    >>> # Test Nl operation
    >>> binary_nl = copy.deepcopy(binary)
    >>> binary_nl.inputs.operation = 'Nl'
    >>> binary_nl.inputs.operand_file = 'output.csv'
    >>> binary_nl.cmdline
    'seg_stats im1.nii -Nl output.csv'
    >>> binary_nl.run()  # doctest: +SKIP

    N)r   r   r   r   rI   r7   r   r   r   r   rS      s   $rS   )r   numpyr)   baser   r   r   r   r   Zniftyreg.baser	   r
   r   r   r9   rH   rI   rS   r   r   r   r   <module>   s   )3)4