3
d                 @   s,  d Z ddlZddljZddlZddlZddl	m
Z
mZ ddl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mZmZmZ dd	lmZmZm Z  dd
l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,G dd deZ-G d d! d!eZ.G d"d# d#eZ/G d$d% d%eZ0G d&d' d'eZ1G d(d) d)eZ2d*d+ Z3d,d- Z4G d.d/ d/eZ5G d0d1 d1eZ6G d2d3 d3eZ7d4d5 Z8d6d7 Z9d8d9 Z:d:d; Z;G d<d= d=eZ<G d>d? d?eZ=G d@dA dAeZ>G dBdC dCeZ?G dDdE dEeZ@G dFdG dGeZAG dHdI dIeeZBG dJdK dKeZCG dLdM dMeZDG dNdO dOeZEG dPdQ dQeZFG dRdS dSeZGdTdU ZHG dVdW dWeZIG dXdY dYeZJG dZd[ d[eZKG d\d] d]eZLG d^d_ d_eZMG d`da daeZNG dbdc dceZOG ddde deeZPG dfdg dgeZQG dhdi dieZRG djdk dkeZSG dldm dmeZTddndoZUddpdqZVddrdsZWG dtdu dueZXG dvdw dweZYG dxdy dyeZZG dzd{ d{ej[Z[G d|d} d}ej\Z\G d~d dej]Z]G dd de!j^Z^dS )zMiscellaneous algorithms.    N)floorceil   )logging   )metrics)
BaseInterfacetraitsTraitedSpecFileInputMultiPathOutputMultiPathBaseInterfaceInputSpec	isdefinedDynamicTraitedSpec	Undefined)fname_presuffixsplit_filenameensure_list)	confoundsznipype.interfacec               @   sb   e Zd ZeddddZejejejejdddZ	ej
dddd	dd
ZejdddZeddZdS )PickAtlasInputSpecTz(Location of the atlas that will be used.)existsdesc	mandatoryzcLabels of regions that will be included in the mask. Must be        compatible with the atlas used.)r   r   Zbothleftrightz7Restrict the mask to only one hemisphere: left or right)r   
usedefaultz;Defines how much the mask will be dilated (expanded in 3D).)r   r   zWhere to store the output mask.)r   N)__name__
__module____qualname__r   atlasr	   ZEitherIntListlabelsEnumhemidilation_sizeoutput_file r(   r(   8/tmp/pip-build-7vycvbft/nipype/nipype/algorithms/misc.pyr   #   s"   
r   c               @   s   e Zd ZedddZdS )PickAtlasOutputSpecTzoutput mask file)r   r   N)r   r   r   r   	mask_filer(   r(   r(   r)   r*   >   s   r*   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 )	PickAtlaszReturns ROI masks given an atlas and a list of labels. Supports dilation
    and left right masking (assuming the atlas is properly aligned).
    c             C   s   | j  }tj|| j  |S )N)_get_brodmann_areanbsave_gen_output_filename)selfruntimeZnimr(   r(   r)   _run_interfaceJ   s    zPickAtlas._run_interfacec             C   s:   t | jjs&t| jjdtj dd}ntjj| jj}|S )NZ_maskT)fnamesuffixnewpathZuse_ext)	r   inputsr'   r   r    osgetcwdpathrealpath)r1   outputr(   r(   r)   r0   P   s    
zPickAtlas._gen_output_filenamec             C   s<  t j| jj}tj|j}tj|j}t	| jj
ts@| jj
g}n| jj
}x|D ]}d|||k< qNW | jjdkrd|ttt|jd d d d d d d f< n<| jjdkrd|d ttt|jd d d d d d f< | jjdkr*ddlm} ||d| jj d d| jj d d| jj d f}t j||j|jS )Nr   r   r   r   r   )grey_dilation)r.   loadr7   r    np
asanyarraydataobjzerosshape
isinstancer#   listr%   intr   floatr   r&   Zscipy.ndimage.morphologyr=   Nifti1Imageaffineheader)r1   niiZorigdatanewdatar#   Zlabr=   r(   r(   r)   r-   \   s(    
20zPickAtlas._get_brodmann_areac             C   s   | j  j }| j |d< |S )Nr+   )_outputsgetr0   )r1   outputsr(   r(   r)   _list_outputsz   s    zPickAtlas._list_outputsN)r   r   r   __doc__r   
input_specr*   output_specr3   r0   r-   rP   r(   r(   r(   r)   r,   B   s   r,   c               @   s.   e Zd ZeedddddZejdddZdS )SimpleThresholdInputSpecT)r   zvolumes to be thresholded)r   r   zOvolumes to be thresholdedeverything below this value will be set        to zeroN)	r   r   r   r   r   volumesr	   Float	thresholdr(   r(   r(   r)   rT      s
   rT   c               @   s   e Zd ZeeddddZdS )SimpleThresholdOutputSpecT)r   zthresholded volumes)r   N)r   r   r   r   r   thresholded_volumesr(   r(   r(   r)   rX      s   rX   c               @   s(   e Zd ZdZeZeZdd Zdd Z	dS )SimpleThresholdz$Applies a threshold to input volumesc       
      C   s|   xv| j jD ]j}tj|}|j }|| j jk}tj|j}|| ||< tj	||j
|j}t|\}}	}tj||	d  q
W |S )Nz_thresholded.nii)r7   rU   r.   r>   	get_fdatarW   r?   rB   rC   rH   rI   rJ   r   r/   )
r1   r2   r4   imgdataZ
active_mapZthresholded_mapZnew_img_baser(   r(   r)   r3      s    
zSimpleThreshold._run_interfacec             C   sR   | j  j }g |d< x8| jjD ],}t|\}}}|d jtjj|d  qW |S )NrY   z_thresholded.nii)	rM   rN   r7   rU   r   appendr8   r:   abspath)r1   rO   r4   r^   r_   r(   r(   r)   rP      s    zSimpleThreshold._list_outputsN)
r   r   r   rQ   rT   rR   rX   rS   r3   rP   r(   r(   r(   r)   rZ      s
   rZ   c               @   s8   e Zd ZeedddddZejej	dd	dddZ
dS )
ModifyAffineInputSpecT)r   z.volumes which affine matrices will be modified)r   r      zNtransformation matrix that will be left multiplied by the        affine matrix)valuerC   r   r   N)rc   rc   )r   r   r   r   r   rU   r	   Arrayr?   Zeyetransformation_matrixr(   r(   r(   r)   rb      s   rb   c               @   s   e Zd ZeeddZdS )ModifyAffineOutputSpecT)existN)r   r   r   r   r   transformed_volumesr(   r(   r(   r)   rg      s   rg   c               @   s0   e Zd ZdZeZeZdd Zdd Z	dd Z
dS )	ModifyAffinezeLeft multiplies the affine matrix with a specified values. Saves the volume
    as a nifti file.
    c             C   s   t |\}}}tjj|d S )Nz_transformed.nii)r   r8   r:   ra   )r1   namer^   r_   r(   r(   r)   r0      s    z!ModifyAffine._gen_output_filenamec             C   sV   xP| j jD ]D}tj|}|j}tj| j j|}tjtj	|j
||j| j| q
W |S )N)r7   rU   r.   r>   rI   r?   dotrf   r/   rH   rA   rJ   r0   )r1   r2   r4   r\   rI   r(   r(   r)   r3      s    
zModifyAffine._run_interfacec             C   s>   | j  j }g |d< x$| jjD ]}|d j| j| qW |S )Nri   )rM   rN   r7   rU   r`   r0   )r1   rO   r4   r(   r(   r)   rP      s
    zModifyAffine._list_outputsN)r   r   r   rQ   rb   rR   rg   rS   r0   r3   rP   r(   r(   r(   r)   rj      s   rj   c               @   s4   e Zd ZeddddZeddddZejddZdS )CreateNiftiInputSpecTzANALYZE img file)r   r   r   zcorresponding ANALYZE hdr filezaffine transformation array)r   N)	r   r   r   r   	data_fileheader_filer	   re   rI   r(   r(   r(   r)   rm      s   rm   c               @   s   e Zd ZeddZdS )CreateNiftiOutputSpecT)r   N)r   r   r   r   
nifti_filer(   r(   r(   r)   rp      s   rp   c               @   s0   e Zd ZdZeZeZdd Zdd Z	dd Z
dS )	CreateNiftizCreates a nifti volumec             C   s"   t | jj\}}}tjj|d S )Nz.nii)r   r7   rn   r8   r:   ra   )r1   r^   r_   r(   r(   r)   _gen_output_file_name   s    z!CreateNifti._gen_output_file_namec             C   s   t | jjd}tjj|}W d Q R X t| jjr<| jj}nd }t | jjd}|j	|}W d Q R X tj
|||}tj|| j  |S )Nrb)openr7   ro   r.   ZAnalyzeHeaderZfrom_fileobjr   rI   rn   Zdata_from_fileobjrH   r/   rs   )r1   r2   Zhdr_filehdrrI   rn   r]   r\   r(   r(   r)   r3      s    
zCreateNifti._run_interfacec             C   s   | j  j }| j |d< |S )Nrq   )rM   rN   rs   )r1   rO   r(   r(   r)   rP     s    zCreateNifti._list_outputsN)r   r   r   rQ   rm   rR   rp   rS   rs   r3   rP   r(   r(   r(   r)   rr      s   rr   c               @   s   e Zd ZedddZdS )GunzipInputSpecT)r   r   N)r   r   r   r   in_filer(   r(   r(   r)   rw     s   rw   c               @   s   e Zd ZeddZdS )GunzipOutputSpecT)r   N)r   r   r   r   out_filer(   r(   r(   r)   ry     s   ry   c               @   s0   e Zd ZdZeZeZdd Zdd Z	dd Z
dS )	Gunzipa  Gunzip wrapper

    >>> from nipype.algorithms.misc import Gunzip
    >>> gunzip = Gunzip(in_file='tpms_msk.nii.gz')
    >>> res = gunzip.run()
    >>> res.outputs.out_file  # doctest: +ELLIPSIS
    '.../tpms_msk.nii'

    .. testcleanup::

    >>> os.unlink('tpms_msk.nii')
    c             C   sB   t | jj\}}}|dd  j dkr2|d d }tjj|| S )N   z.gzr}   )r   r7   rx   lowerr8   r:   ra   )r1   r^   r_   extr(   r(   r)   rs   +  s    zGunzip._gen_output_file_namec             C   sV   dd l }dd l}|j| jjd,}t| j d}|j|| W d Q R X W d Q R X |S )Nr   rt   wb)gzipshutilru   r7   rx   rs   copyfileobj)r1   r2   r   r   rx   rz   r(   r(   r)   r3   1  s     zGunzip._run_interfacec             C   s   | j  j }| j |d< |S )Nrz   )rM   rN   rs   )r1   rO   r(   r(   r)   rP   :  s    zGunzip._list_outputsN)r   r   r   rQ   rw   rR   ry   rS   rs   r3   rP   r(   r(   r(   r)   r{     s   	r{   c             C   sF   t  }x:| D ]2}ttj|\}}}tj||| }|j| qW |S )N)rE   r   opra   joinr`   )in_listr   Zout_listfilenamer:   rk   r^   Zout_namer(   r(   r)   
replaceext@  s    
r   c             C   sz   t j| }|rXtt j|dkrXt j|t j|d t j|d  df}tjt j| tj|d }t j	||dd |S )Nr   r   z.csv,)	delimiter)
r?   ZasarraylenrC   reshapeifloggerinfor   ra   savetxt)in_arrayrk   r   output_arrayZoutput_namer(   r(   r)   _matlab2csvI  s    
$r   c               @   s*   e Zd ZeddddZejddddZdS )Matlab2CSVInputSpecTzInput MATLAB .mat file)r   r   r   zgThe output of this interface is meant for R, so matrices will be        reshaped to vectors by default.)r   r   N)r   r   r   r   rx   r	   Boolreshape_matrixr(   r(   r(   r)   r   V  s
   r   c               @   s   e Zd ZeeddZdS )Matlab2CSVOutputSpeczFOutput CSV files for each variable saved in the input .mat        file)r   N)r   r   r   r   r   	csv_filesr(   r(   r(   r)   r   `  s   r   c               @   s(   e Zd ZdZeZeZdd Zdd Z	dS )
Matlab2CSVa  
    Save the components of a MATLAB .mat file as a text file with comma-separated values (CSVs).

    CSV files are easily loaded in R, for use in statistical processing.
    For further information, see cran.r-project.org/doc/manuals/R-data.pdf

    Example
    -------
    >>> from nipype.algorithms import misc
    >>> mat2csv = misc.Matlab2CSV()
    >>> mat2csv.inputs.in_file = 'cmatrix.mat'
    >>> mat2csv.run() # doctest: +SKIP

    c       	      C   sD  dd l j}|jtj| jj}t }xJt|j D ]:}|j	ds2t
|| d tjr`|j| q2tjd| q2W t|dkrtjdt| tj| x|D ].}tjd|t||  t|| || jj qW npt|dkr6t| jj\}}}|d }tjd|t||  tjd|| jj t|| || jj n
tjd	 |S )
Nr   __z;One of the keys in the input file, %s, is not a Numpy arrayr   z%i variables found:z#...Converting %s - type %s - to CSVz"Single variable found %s, type %s:z...Converting %s to CSV from %szNo values in the MATLAB file?!)scipy.ioioloadmatr   ra   r7   rx   rE   keys
startswithrD   r?   ndarrayr`   r   r   r   typer   r   r   error)	r1   r2   sioin_dictsaved_variableskeyvariabler^   rk   r(   r(   r)   r3   |  s:    





zMatlab2CSV._run_interfacec       	      C   s   dd l j}| j j }|jtj| jj}t	 }xJt	|j
 D ]:}|jds>t|| d tjrl|j| q>tjd| q>W t|dkrt|d|d< n<t|dkrt| jj\}}}tj|d |d< n
tjd |S )Nr   r   z;One of the keys in the input file, %s, is not a Numpy arrayr   z.csvr   zNo values in the MATLAB file?!)r   r   rS   rN   r   r   ra   r7   rx   rE   r   r   rD   r?   r   r`   r   r   r   r   r   )	r1   r   rO   r   r   r   r^   rk   r   r(   r(   r)   rP     s$    



zMatlab2CSV._list_outputsN)
r   r   r   rQ   r   rR   r   rS   r3   rP   r(   r(   r(   r)   r   i  s
   .r   c       	   #   C   s4  xt | D ]\}}ytj|dd}W n tk
r   ytj|ddd}W n tk
r   t|d}|j }W d Q R X |jd}t|}y tj|ddtt	d|d}W n4 tk
r   tj|ddtt	d|d d}Y nX Y nX Y nX |dkr|}qtj
||f}qW tj|}tjd tjtj| |S )	Nr   )r   r   )r   skiprowsr)r   r   Zusecolsr   zFinal output array shape:)	enumerater?   Zloadtxt
ValueErrorru   readlinesplitr   rE   rangeZdstacksqueezer   r   rC   )	r   idxrx   r   firstZheader_lineZheader_listZn_colsZ	out_arrayr(   r(   r)   
merge_csvs  s<    
(

r   c             C   s   dd l j}ddlm} t| dkrt }|j| }|jd}|d|d  }xjt| D ]D\}}||\}	}
}|j	|	|
}|j
|d}
|
j
dd}
|j|
 qVW n|| d \}	}
}|
g}|S )Nr   r   )r   r   / Z_subject_id_)os.pathr:   utils.filemanipr   r   rE   commonprefixrfindr   r   replacer`   )in_filesr   r   Z	out_namesr   Z	lastslash
commonpathZfileidxrx   r:   rk   r   r(   r(   r)   remove_identical_paths  s     


r   c             C   s   g }| r|j d t|dkrNx\tdt|d D ]}|j t|tf q2W n.x,td|d d D ]}|j t|tf qbW |r|j |df tj| |S )Nheadinga40r   r   )r   r   )r`   r   r   minstrrG   r   r   )ZrowheadingsrC   extraheadingBoolextraheadingtypelistr   r(   r(   r)   maketypelist  s    

r   c       	      C   s   g }|r|j d t|dkrrtjt||}xtdt|d D ]*}| d d |d f |t|< |j d qBW nFtjd|}x8tdt| d D ]"}| |d  |t|< |j d qW |r|j d dj|}||fS )Nz%sr   z%fr   )	r`   r   r?   rB   maxr   r   r   r   )	r   r   rowheadingsBoolrC   r   Zfmtlistr<   r   fmtr(   r(   r)   makefmtlist  s     


r   c               @   sv   e Zd ZeedddddZeddddZejej	dd	Z
ejej	d
d	Zej	ddddZej	dd	Zej	dd	ZdS )MergeCSVFilesInputSpecT)r   z'Input comma-separated value (CSV) files)r   r   z
merged.csvz#Output filename for merged CSV file)r   r   zList of column headings to save in merged CSV file        (must be equal to number of input files). If left undefined, these        will be pulled from the input filenames.)r   zlList of row headings to save in merged CSV file        (must be equal to number of rows in the input files).labelz1Column heading for the row headings         addedz'New heading to add for the added field.zcNew field to add to each row. This is useful for saving the        group or subject ID in the file.N)r   r   r   r   r   r   rz   r	   r"   Strcolumn_headingsrow_headingsrow_heading_titleextra_column_headingextra_fieldr(   r(   r(   r)   r   #  s&   r   c               @   s   e Zd ZeddZdS )MergeCSVFilesOutputSpecz#Output CSV file containing columns )r   N)r   r   r   r   csv_filer(   r(   r(   r)   r   D  s   r   c               @   s(   e Zd ZdZeZeZdd Zdd Z	dS )MergeCSVFilesa  
    Merge several CSV files into a single CSV file.

    This interface is designed to facilitate data loading in the R environment.
    If provided, it will also incorporate column heading names into the
    resulting CSV file.
    CSV files are easily loaded in R, for use in statistical processing.
    For further information, see cran.r-project.org/doc/manuals/R-data.pdf

    Example
    -------
    >>> from nipype.algorithms import misc
    >>> mat2csv = misc.MergeCSVFiles()
    >>> mat2csv.inputs.in_files = ['degree.mat','clustering.mat']
    >>> mat2csv.inputs.column_headings = ['degree','clustering']
    >>> mat2csv.run() # doctest: +SKIP

    c             C   s  d}d}d}t | jjr,tjd | jj}ntjd t| jj}t | jjrt | jjrp| jj}tjd| nd}tjd |j	| d}t
| jjd	krtjd
 t | jjrtjd dj| jjd}|djtj| d }d}n"tjd ddjtj| d }tjd tj| t| jj}t| jj\}	}
}|dksLd}tj|
| }t|d}|j| W d Q R X tj|}t||||}t|||||\}}|r| jj}g }x$|D ]}d| d }|j	| qW tj|dd}||d< t | jjr\g }t
|d	kr|d }nd	}x"td|D ]}|j	| jj q.W tjt
| |||< tj| tj| t|d}tj|||dd W d Q R X |S )NFr   z#Column headings have been provided:z:Column headings not provided! Pulled from input filenames:z!Extra column heading provided: %sr   z2Extra column heading was not defined. Using "type"Tr   zOnly one file input!z>Row headings have been provided. Adding "labels"column header.z"{p}",")pz","z"
z$Row headings have not been provided."zFinal Headings:z.csvwz|S40)dtyper   r   ar   )r   ) r   r7   r   r   r   r   r   r   r   r`   r   warningr   formatr   r   	itertoolschainr   r   rz   r   ra   ru   writer?   rC   r   r   arrayr   r   )r1   r2   r   r   r   ZheadingsprefixZcsv_headingsr   r^   rk   r   rz   Zfile_handlerC   r   r   r<   Zrow_heading_listZrow_heading_list_with_quotesZrow_headingZrow_heading_with_quotesr   ZextrafieldlistZmxr   r(   r(   r)   r3   _  sz    














zMergeCSVFiles._run_interfacec             C   sD   | j  j }t| jj\}}}|dks*d}tj|| }||d< |S )Nz.csvr   )rS   rN   r   r7   rz   r   ra   )r1   rO   r^   rk   r   rz   r(   r(   r)   rP     s    zMergeCSVFiles._list_outputsN)
r   r   r   rQ   r   rR   r   rS   r3   rP   r(   r(   r(   r)   r   H  s
   Ur   c               @   s@   e Zd ZeddddZeddddZejddZejd	dZ	d
S )AddCSVColumnInputSpecTz'Input comma-separated value (CSV) files)r   r   r   zextra_heading.csvz#Output filename for merged CSV file)r   r   z'New heading to add for the added field.)r   zcNew field to add to each row. This is useful for saving the        group or subject ID in the file.N)
r   r   r   r   rx   rz   r	   r   r   r   r(   r(   r(   r)   r     s   r   c               @   s   e Zd ZeddZdS )AddCSVColumnOutputSpecz#Output CSV file containing columns )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d Zdd Z	dS )AddCSVColumnag  
    Short interface to add an extra column and field to a text file.

    Example
    -------
    >>> from nipype.algorithms import misc
    >>> addcol = misc.AddCSVColumn()
    >>> addcol.inputs.in_file = 'degree.csv'
    >>> addcol.inputs.extra_column_heading = 'group'
    >>> addcol.inputs.extra_field = 'male'
    >>> addcol.run() # doctest: +SKIP

    c             C   s   t | jjd}t| jj\}}}|dks,d}tj|| }t |d}|j }|jdd}|d | jj	 d }|j
| x6|D ].}	|	jdd}
|
d | jj d }
|j
|
 q|W |j  |j  |S )	Nr   z.csvr   
r   z,"z"
r   )ru   r7   rx   r   rz   r   ra   r   r   r   r   r   close)r1   r2   rx   r^   rk   r   rz   	firstlineZnew_firstlinelinenew_liner(   r(   r)   r3     s"    


zAddCSVColumn._run_interfacec             C   sD   | j  j }t| jj\}}}|dks*d}tj|| }||d< |S )Nz.csvr   )rS   rN   r   r7   rz   r   ra   )r1   rO   r^   rk   r   rz   r(   r(   r)   rP     s    zAddCSVColumn._list_outputsN)
r   r   r   rQ   r   rR   r   rS   r3   rP   r(   r(   r(   r)   r     s
   r   c                   s:   e Zd ZedddZejeji ddZ fddZ	  Z
S )AddCSVRowInputSpecTz'Input comma-separated value (CSV) files)r   r   )rd   r   c                s\   || j  kr2t|s&tt| j|| || j|< n&|| jkrF|| j|< tt| j|| d S )N)copyable_trait_namesr   superr   __setattr__rM   )r1   r   rd   )	__class__r(   r)   r     s    

zAddCSVRowInputSpec.__setattr__)r   r   r   r   rx   r	   DictAnyrM   r   __classcell__r(   r(   )r   r)   r      s   r   c               @   s   e Zd ZeddZdS )AddCSVRowOutputSpecz Output CSV file containing rows )r   N)r   r   r   r   r   r(   r(   r(   r)   r     s   r   c                   sN   e Zd ZdZeZeZd fdd	Zdd Z	dd	 Z
 fd
dZdd Z  ZS )	AddCSVRowa3  
    Simple interface to add an extra row to a CSV file.

    .. note:: Requires `pandas <http://pandas.pydata.org/>`_

    .. warning:: Multi-platform thread-safe execution is possible with
        `lockfile <https://pythonhosted.org/lockfile/lockfile.html>`_. Please
        recall that (1) this module is alpha software; and (2) it should be
        installed for thread-safe writing.
        If lockfile is not installed, then the interface is not thread-safe.


    Example
    -------
    >>> from nipype.algorithms import misc
    >>> addrow = misc.AddCSVRow()
    >>> addrow.inputs.in_file = 'scores.csv'
    >>> addrow.inputs.si = 0.74
    >>> addrow.inputs.di = 0.93
    >>> addrow.inputs.subject_id = 'S400'
    >>> addrow.inputs.list_of_values = [ 0.4, 0.7, 0.3 ]
    >>> addrow.run() # doctest: +SKIP

    NTc                s   t t| jf | i }|| _d| _d | _|r^x0|D ](}| jj|tj	 t
| jj|< t
||< q2W | jjf ddi| |r~d| _d S )NFZtrait_change_notifyT)r   r   __init__Z	_infields
_have_lock_lockr7   Z	add_traitr	   r   r   rM   Z	trait_setZ_always_run)r1   ZinfieldsZ	force_runkwargsZundefined_traitsr   )r   r(   r)   r   0  s    
zAddCSVRow.__init__c             C   s  ydd l }W n, tk
r8 } ztd|W Y d d }~X nX yddlm} d| _W n( tk
rx   ddlm} |d Y nX i }xnt| jj	j
 D ]Z\}}|dkr|| jj krqt|trx.t|D ]\}	}
|
|d||	f < qW q|||< qW |j|g}| jr|d	| jj | _| jj  tj| jjrR|j| jjdd
}|j||gdd}t| jjd}|j| W d Q R X | jr| jj  |S )Nr   zBThis interface requires pandas (http://pandas.pydata.org/) to run.)SoftFileLockT)warnzdPython module filelock was not found: AddCSVRow will not be thread-safe in multi-processor executionZtrait_addedz%s_%dz%s.lock)Z	index_col)Zignore_indexr   )ZpandasImportErrorZfilelockr   r   warningsr   rE   r7   rM   itemsr   rD   r   Z	DataFramerx   r   acquirer   r   Zread_csvconcatru   Zto_csvrelease)r1   r2   pder   r   Z
input_dictr   valivZdfZformerdffr(   r(   r)   r3   A  sB    



zAddCSVRow._run_interfacec             C   s   | j  j }| jj|d< |S )Nr   )rS   rN   r7   rx   )r1   rO   r(   r(   r)   rP   w  s    zAddCSVRow._list_outputsc                s   | j tt| j S )N)_add_output_traitsr   r   rM   )r1   )r   r(   r)   rM   |  s    zAddCSVRow._outputsc             C   s   |S )Nr(   )r1   r_   r(   r(   r)   r    s    zAddCSVRow._add_output_traits)NT)r   r   r   rQ   r   rR   r   rS   r   r3   rP   rM   r  r   r(   r(   )r   r)   r     s   6r   c               @   s(   e Zd ZeddddZejdddZdS )#CalculateNormalizedMomentsInputSpecTzXText file with timeseries in columns and timepoints in rows,        whitespace separated)r   r   r   zPDefine which moment should be calculated, 3 for skewness, 4 for        kurtosis.)r   r   N)r   r   r   r   timeseries_filer	   r!   momentr(   r(   r(   r)   r    s   r  c               @   s   e Zd Zejej ddZdS )$CalculateNormalizedMomentsOutputSpecZMoments)r   N)r   r   r   r	   r"   rV   Zmomentsr(   r(   r(   r)   r    s   r  c               @   s(   e Zd ZdZeZeZdd Zdd Z	dS )CalculateNormalizedMomentsa  
    Calculates moments of timeseries.

    Example
    -------
    >>> from nipype.algorithms import misc
    >>> skew = misc.CalculateNormalizedMoments()
    >>> skew.inputs.moment = 3
    >>> skew.inputs.timeseries_file = 'timeseries.txt'
    >>> skew.run() # doctest: +SKIP

    c             C   s   t | jj| jj| _|S )N)calc_momentsr7   r  r  _moments)r1   r2   r(   r(   r)   r3     s    z)CalculateNormalizedMoments._run_interfacec             C   s   | j  j }| j|d< |S )NZskewness)rS   rN   r
  )r1   rO   r(   r(   r)   rP     s    
z(CalculateNormalizedMoments._list_outputsN)
r   r   r   rQ   r  rR   r  rS   r3   rP   r(   r(   r(   r)   r    s
   r  c             C   sV   ddl j}tj| }|j|ddd}|j||dd}|dk}tj|d|||d   S )zReturns nth moment (3 for skewness, 4 for kurtosis) of timeseries
    (list of values; one per timeseries).

    Keyword arguments:
    timeseries_file -- text file with white space separated timepoints in rows

    r   Nr   )axisg       @)Zscipy.statsstatsr?   Z
genfromtxtr  where)r  r  r  Z
timeseriesm2Zm3Zzeror(   r(   r)   r	    s    

r	  c               @   sh   e Zd ZeddddZedddZejddddZej	d	d
ddddZ
ej	d	dddddZeddZdS )AddNoiseInputSpecTz-input image that will be corrupted with noise)r   r   r   zBinput mask, voxels outside this mask will be considered background)r   r   g      $@zdesired output SNR in dB)r   r   normalricianzdesired noise distribution)r   r   r   rayleighz@desired noise distribution, currently only normal is implementedzdesired output filename)r   N)r   r   r   r   rx   in_maskr	   rV   snrr$   distbg_distrz   r(   r(   r(   r)   r    s*   r  c               @   s   e Zd ZedddZdS )AddNoiseOutputSpecTzcorrupted image)r   r   N)r   r   r   r   rz   r(   r(   r(   r)   r    s   r  c               @   s:   e Zd ZdZeZeZdd Zdd Z	dd Z
dddZdS )AddNoisea(  
    Corrupts with noise the input image.


    Example
    -------
    >>> from nipype.algorithms.misc import AddNoise
    >>> noise = AddNoise()
    >>> noise.inputs.in_file = 'T1.nii'
    >>> noise.inputs.in_mask = 'mask.nii'
    >>> noise.snr = 30.0
    >>> noise.run() # doctest: +SKIP

    c             C   s   t j| jj}|j }| jj}t| jjrBtj	t j| jjj
}n
tj|}| j|||| jj| jjd}t j||j|j}|j| j  |S )N)masksnr_dbr  r  )r.   r>   r7   rx   r[   r  r   r  r?   r@   rA   	ones_like	gen_noiser  r  rH   rI   rJ   to_filenamer0   )r1   r2   Zin_imageZin_datar  r  resultZres_imr(   r(   r)   r3     s    
zAddNoise._run_interfacec             C   sF   t | jjs:t| jj\}}}tjjd|| jj|f }n| jj}|S )Nz%s_SNR%03.2f%s)	r   r7   rz   r   rx   r8   r:   ra   r  )r1   r^   r_   r   rz   r(   r(   r)   r0     s
    zAddNoise._gen_output_filenamec             C   s   | j  j }| j |d< |S )Nrz   )rS   rN   r0   )r1   rO   r(   r(   r)   rP     s    zAddNoise._list_outputsN      $@r  c             C   s  ddl m} |tjd|d }|dkr4tj|}nDd||dk< d||dk < |j|jk rxtjtj|g|jd  dd}||dk j	d}|dkr
||j
  }||j | }	tjj|j|	d	}
tj|dko|d
kr tjj|j|	d	}||dk |
|dk< ||
 }nv|dkrx|j
 | }	tjj|j|	d	}tjj|j|	d	}||d }||d }tj|| d |d  }ntd|S )z
        Generates a copy of an image with a certain amount of
        added gaussian noise (rayleigh for background in mask)
        r   )sqrtg      $@Nr   r|   rc   r  )sizeZscaler  r  g       @r   z2Only normal and rician distributions are supported)mathr   r?   powerr  ndimZrollaxisr   rC   r   Zmeanvarrandomr  anyr  NotImplementedError)r1   imager  r  r  r  r   r  signalZsigma_nZnoiseZbg_noiseZim_noiseZn_1Zn_2Zstde_1Zstde_2r(   r(   r)   r    s6     


zAddNoise.gen_noise)Nr  r  r  )r   r   r   rQ   r  rR   r  rS   r3   r0   rP   r  r(   r(   r(   r)   r    s   	r  c               @   s*   e Zd ZeeddddZedddZdS )#NormalizeProbabilityMapSetInputSpecTzThe tpms to be normalized)r   r   r   z-Masked voxels must sum up 1.0, 0.0 otherwise.)r   r   N)r   r   r   r   r   r   r  r(   r(   r(   r)   r,  G  s   r,  c               @   s   e Zd ZeeddddZdS )$NormalizeProbabilityMapSetOutputSpecT)r   znormalized maps)r   N)r   r   r   r   r   	out_filesr(   r(   r(   r)   r-  N  s   r-  c               @   s(   e Zd ZdZeZeZdd Zdd Z	dS )NormalizeProbabilityMapSeta-  
    Returns the input tissue probability maps (tpms, aka volume fractions).

    The tissue probability maps are normalized to sum up 1.0 at each voxel within the mask.

    .. note:: Please recall this is not a spatial normalization algorithm


    Example
    -------
    >>> from nipype.algorithms import misc
    >>> normalize = misc.NormalizeProbabilityMapSet()
    >>> normalize.inputs.in_files = [ 'tpm_00.nii.gz', 'tpm_01.nii.gz', 'tpm_02.nii.gz' ]
    >>> normalize.inputs.in_mask = 'tpms_msk.nii.gz'
    >>> normalize.run() # doctest: +SKIP

    c             C   s,   d }t | jjr| jj}t| jj|| _|S )N)r   r7   r  normalize_tpmsr   _out_filenames)r1   r2   r  r(   r(   r)   r3   i  s
    z)NormalizeProbabilityMapSet._run_interfacec             C   s   | j  j }| j|d< |S )Nr.  )rS   rN   r1  )r1   rO   r(   r(   r)   rP   r  s    
z(NormalizeProbabilityMapSet._list_outputsN)
r   r   r   rQ   r,  rR   r-  rS   r3   rP   r(   r(   r(   r)   r/  R  s
   	r/  c               @   s>   e Zd ZeddddZedddZejejejejddZ	dS )	SplitROIsInputSpecTzfile to be splitted)r   r   r   zonly process files inside mask)r   r   zdesired ROI size)r   N)
r   r   r   r   rx   r  r	   Tupler!   roi_sizer(   r(   r(   r)   r2  x  s   r2  c               @   sB   e Zd ZeeddddZeeddddZeeddddZdS )SplitROIsOutputSpecT)r   zthe resulting ROIs)r   za mask indicating valid valuesz!arrays keeping original locationsN)r   r   r   r   r   r.  	out_masks	out_indexr(   r(   r(   r)   r5  ~  s
   r5  c               @   s(   e Zd ZdZeZeZdd Zdd Z	dS )	SplitROIsa]  
    Splits a 3D image in small chunks to enable parallel processing.

    ROIs keep time series structure in 4D images.

    Example
    -------
    >>> from nipype.algorithms import misc
    >>> rois = misc.SplitROIs()
    >>> rois.inputs.in_file = 'diffusion.nii'
    >>> rois.inputs.in_mask = 'mask.nii'
    >>> rois.run() # doctest: +SKIP

    c             C   st   d }d }i | _ t| jjr"| jj}t| jjr6| jj}t| jj||}|d | j d< |d | j d< |d | j d< |S )Nr   r.  r   r6  r   r7  )	_outnamesr   r7   r  r4  
split_roisrx   )r1   r2   r  roisizeresr(   r(   r)   r3     s    zSplitROIs._run_interfacec             C   s4   | j  j }x"t| jj D ]\}}|||< qW |S )N)rS   rN   rE   r9  r   )r1   rO   kr  r(   r(   r)   rP     s    zSplitROIs._list_outputsN)
r   r   r   rQ   r2  rR   r5  rS   r3   rP   r(   r(   r(   r)   r8    s
   r8  c               @   s>   e Zd ZeeddddZeedddddZedddZd	S )
MergeROIsInputSpecTzfiles to be re-merged)r   r   r   )r   r   z array keeping original locations)r   zreference file)r   r   N)r   r   r   r   r   r   in_indexin_referencer(   r(   r(   r)   r>    s
   r>  c               @   s   e Zd ZedddZdS )MergeROIsOutputSpecTzthe recomposed file)r   r   N)r   r   r   r   merged_filer(   r(   r(   r)   rA    s   rA  c               @   s(   e Zd ZdZeZeZdd Zdd Z	dS )	MergeROIsa  
    Splits a 3D image in small chunks to enable parallel processing.

    ROIs keep time series structure in 4D images.

    Example
    -------
    >>> from nipype.algorithms import misc
    >>> rois = misc.MergeROIs()
    >>> rois.inputs.in_files = ['roi%02d.nii' % i for i in range(1, 6)]
    >>> rois.inputs.in_reference = 'mask.nii'
    >>> rois.inputs.in_index = ['roi%02d_idx.npz' % i for i in range(1, 6)]
    >>> rois.run() # doctest: +SKIP

    c             C   s"   t | jj| jj| jj}|| _|S )N)
merge_roisr7   r   r?  r@  _merged)r1   r2   r<  r(   r(   r)   r3     s    zMergeROIs._run_interfacec             C   s   | j  j }| j|d< |S )NrB  )rS   rN   rE  )r1   rO   r(   r(   r)   rP     s    
zMergeROIs._list_outputsN)
r   r   r   rQ   r>  rR   rA  rS   r3   rP   r(   r(   r(   r)   rC    s
   rC  c                s,  ddl  ddlddlj}j| j } |dkr4g }t|t| krxdt| D ]X\}}|j|j	|\}}|dkr|j|\}}|| }|j
d|||f }	||	g7 }qNW  fdd| D }
t| dkr*|
d jjd}d	||d
k< |
d jj }|jj  j j||
d j||d  |d S jfdd|
D }d
||d
k < j|dd}j|
d j}d||dk< |dk	rj j|j}d||dk< d||dk< jj|d}xlt|D ]`\}}	jj|| d}|| }|
| jj }|jd  j j|jj|
| j||	 qW |S )z
    Returns the input tissue probability maps (tpms, aka volume fractions)
    normalized to sum up 1.0 at each voxel within the mask.
    r   Nz.gzz%s_norm_%02d%sc                s   g | ]} j |qS r(   )r>   ).0Zfim)r.   r(   r)   
<listcomp>  s    z"normalize_tpms.<locals>.<listcomp>r   )r   g      ?g        c                s   g | ]}|j d  jdqS )Z	unchanged)Zcachingr   )r[   float32)rF  im)r?   r(   r)   rG    s    )r  rH  )nibabelnumpyr   r:   Z
atleast_1dtolistr   r   splitextbasenamera   r[   rH  rJ   copyset_data_dtyper/   rH   rI   stacksumonesrC   r@   r>   rA   maZmasked_equalastype)r   r  r.  r   r   Zfinnamer4   ZfextZfext2rz   imgsZimg_datarv   weightsZmskr]   Zprobmapr(   )r.   r?   r)   r0    sR    
 

&r0  c             C   s  ddl }ddl}ddlm}m} ddlj}|dkr6d}|j| }|j}	|	dd }
|	d }|d |d  |d  }|d |d |d |f}|dk	r|j	|j|j
}d||dk< d||dk < n
|j|
}|jdj|j}|j|}|j|}t||t| }|j	|j
j|jdf}|j|j|dd}|jd }|jd	}|j|j||jd
ddj| g }g }g }xHt|D ]:}|| }|d | }d}||kr|| }|}|||df }|jd| }|j|d  |j||d || f |dkrx|j||jt|t|f|jd
f}|j|f|jd
}d|t| d< |jd}|j|j|ddj| |j| n
|j| |jd| }|j|j|ddj| |j| qzW |||fS )z9
    Splits an image in ROIs for parallel processing
    r   N)r   r   
   r   r|   r   )r  zonesmask.nii.gz)r   .zroi%010d_idxz.npzzpartialmask.nii.gzzroi%010d.nii.gz)rX  rX  r   r"  r"  r"  r"  )rJ  rK  r#  r   r   r   r:   r>   rC   r@   rA   rS  r   rU  Zuint8ZnonzerorR  rF   rG   r!  r   Ztakera   rH   r  r   r`   ZsavezZvstackrB   rH  )rx   r  Zroishaper.   r?   r   r   r   rI  ZimshapeZdshapeZnvolsr;  Z	droishapeZnzelsZelsZnroisr]   Z
roidefnamer.  Zout_maskZout_idxsr   r   lastfillZdroiinameZ
partialmskZpartnamer4   r(   r(   r)   r:     sn    








"

r:  c                sP  ddl  ddl}ddlj}ddl}|dkr4|jd}|dkrB|j}|j|d dkry4tj	d| |j
d|g|jdd	 |j|d }W n   Y nX  j|}|j}	|jj }
|j}~|d |d  |d
  } j| d }t|jdkr|jd }nd}|d |d |d
 |f}|
j| |
jdd |dk r|j||f|d}xt| |D ]\}}|j|}|j|d }|j j|jjd|}t|}|f}y|d|df ||df< W n*   tdt|jt|jf   Y nX qRW  j|j||	|
j| nF|
j|dd  g }xLt|D ]@}|jd| } j|j|dd |	|
j| |j | q&W xt| |D ]\}}|j|}|j|d }xt!|D ]|\}}|j j|jjd} j|jd|f jd}t|}|f}|d| ||<  j|j|dd |	|
j| qW qvW  fdd|D } j"|}|j| |S )zE
    Re-builds an image resulting from a parallelized processing
    r   Nzmerged.nii.gzr   z.gzzuncompress %sZgunzipT)stdoutshellr   rc   mmseci,  )r   Zarr_0.zBConsistency between indexes and chunks was lost: data=%s, chunk=%sr|   zvol%06d.niic                s   g | ]} j |qS r(   )r>   )rF  rI  )r.   r(   r)   rG    s    zmerge_rois.<locals>.<listcomp>r"  r"  r"  r"  )#rJ  rK  r   r:   
subprocessra   rH  rM  r   r   
check_callPIPEr>   rI   rJ   rO  rC   r   rP  Zset_xyzt_unitsrB   zipr   r@   rA   r   printr   rH   r  Zset_data_shaper   r`   r   Zconcat_images)r   Zin_idxsZin_refr   rz   r?   r   sprefZaffrv   ZrshZnpixZfcimgZndirsZnewshaper]   cnamer[  r  ZidxscdataZnelsZidatarK   dr4   rV  Zallimr(   )r.   r)   rD  n  s    






"
.

rD  c               @   s:   e Zd ZeeddddZejddZej	ddddZ
d	S )
CalculateMedianInputSpecTz)One or more realigned Nifti 4D timeseries)r   r   r   z&Filename prefix to store median images)r   Fz&Calculate a median file for each Nifti)r   r   N)r   r   r   r   r   r   r	   r   median_filer   median_per_filer(   r(   r(   r)   rj    s   
rj  c               @   s   e Zd ZeeddddZdS )CalculateMedianOutputSpecT)r   zOne or more median images)r   N)r   r   r   r   r   median_filesr(   r(   r(   r)   rm    s   rm  c                   sL   e Zd ZdZeZeZ fddZdddZ	dd Z
d	d
 ZdddZ  ZS )CalculateMediana  
    Computes an average of the median across one or more 4D Nifti timeseries

    Example
    -------
    >>> from nipype.algorithms.misc import CalculateMedian
    >>> mean = CalculateMedian()
    >>> mean.inputs.in_files = 'functional.nii'
    >>> mean.run() # doctest: +SKIP

    c                s   t t| j|| g | _d S )N)r   ro  r   _median_files)r1   argsr   )r   r(   r)   r     s    zCalculateMedian.__init__Nc       	      C   s   |r| j j| }n$t| j jtr.| j jd }n| j j}tjtj|\}}|dkrhtj|\}}|| }|d krt|}|jdr|dd  }| j jr| j j}ndj	||}|r|t
|7 }tjdj	||S )Nr   z.gz.r   z{}_{}z{}.{})r7   r   rD   rE   r   rM  rN  r   rk  r   r   ra   )	r1   r5   r   r   rx   r4   Zin_extZin_ext2outnamer(   r(   r)   
_gen_fname  s&    

zCalculateMedian._gen_fnamec             C   s   d }g | _ xntt| jjD ]Z\}}tj|}tj|j	 dd}| jj
r`| j j| j||| q|d krn|}q||7 }qW | jj
s| j j| j||| |S )Nr|   )r  )rp  r   r   r7   r   r.   r>   r?   medianr[   rl  r`   _write_nifti)r1   r2   totalr   r4   r\   r]   r(   r(   r)   r3     s    
zCalculateMedian._run_interfacec             C   s   | j  j }| j|d< |S )Nrn  )rM   rN   rp  )r1   rO   r(   r(   r)   rP     s    
zCalculateMedian._list_outputsru  c             C   s\   | j jr*tj||j|j}| j||d}n$tj||d  |j|j}| j|}|j| |S )N)r   r   )r7   rl  r.   rH   rI   rJ   rt  r  )r1   r\   r]   r   r5   Z
median_imgr   r(   r(   r)   rv    s    

zCalculateMedian._write_nifti)NN)ru  )r   r   r   rQ   rj  rR   rm  rS   r   rt  r3   rP   rv  r   r(   r(   )r   r)   ro    s   
ro  c                   s    e Zd ZdZ fddZ  ZS )DistancezCalculates distance between two volumes.

    .. deprecated:: 0.10.0
       Use :py:class:`nipype.algorithms.metrics.Distance` instead.
    c                s$   t tj| jf | tjdt d S )Nz^This interface has been deprecated since 0.10.0, please use nipype.algorithms.metrics.Distance)r   namrx  r   r   r   DeprecationWarning)r1   r7   )r   r(   r)   r   1  s    zDistance.__init__)r   r   r   rQ   r   r   r(   r(   )r   r)   rx  *  s   rx  c                   s    e Zd ZdZ fddZ  ZS )OverlapzCalculates various overlap measures between two maps.

    .. deprecated:: 0.10.0
       Use :py:class:`nipype.algorithms.metrics.Overlap` instead.
    c                s$   t tj| jf | tjdt d S )Nz]This interface has been deprecated since 0.10.0, please use nipype.algorithms.metrics.Overlap)r   ry  r{  r   r   r   rz  )r1   r7   )r   r(   r)   r   C  s    zOverlap.__init__)r   r   r   rQ   r   r   r(   r(   )r   r)   r{  <  s   r{  c                   s    e Zd ZdZ fddZ  ZS )FuzzyOverlapzCalculates various overlap measures between two maps, using a fuzzy
    definition.

    .. deprecated:: 0.10.0
       Use :py:class:`nipype.algorithms.metrics.FuzzyOverlap` instead.
    c                s$   t tj| jf | tjdt d S )NzbThis interface has been deprecated since 0.10.0, please use nipype.algorithms.metrics.FuzzyOverlap)r   ry  r|  r   r   r   rz  )r1   r7   )r   r(   r)   r   V  s    zFuzzyOverlap.__init__)r   r   r   rQ   r   r   r(   r(   )r   r)   r|  N  s   r|  c                   s    e Zd ZdZ fddZ  ZS )TSNRz`
    .. deprecated:: 0.12.1
       Use :py:class:`nipype.algorithms.confounds.TSNR` instead
    c                s$   t tj| jf | tjdt d S )NzWThis interface has been moved since 0.12.0, please use nipype.algorithms.confounds.TSNR)r   r   r}  r   r   r   UserWarning)r1   r7   )r   r(   r)   r   g  s    zTSNR.__init__)r   r   r   rQ   r   r   r(   r(   )r   r)   r}  a  s   r}  )NN)NN)NN)_rQ   r8   r   r:   r   rJ  r.   rK  r?   r#  r   r   r   r   r   r   r   ry  Zinterfaces.baser   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   	getLoggerr   r   r*   r,   rT   rX   rZ   rb   rg   rj   rm   rp   rr   rw   ry   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r	  r  r  r  r,  r-  r/  r2  r5  r8  r>  rA  rC  r0  r:  rD  rj  rm  ro  rx  r{  r|  r}  r(   r(   r(   r)   <module>   s   
0
>!"!&	
	\%!v0pd&
*
!
?
N
ZP