3
dO                 @   s   d dl jZd dlZd dl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 ddl	mZ ejdZd	d
 ZG dd deZG dd deZG dd de
ZdS )    N   )logging   )LibraryBaseInterfaceBaseInterfaceInputSpectraitsFileTraitedSpecInputMultiPathOutputMultiPath	isdefined   )have_cvznipype.interfacec             C   s   t j| d }t| }t|j }tj|||f}xt| D ]~\}}t j|}xN|jddD ]>\}	}
}y|| ||	 |
 d< W q\   tdj	|Y q\X q\W t j
||d d d d |f< q<W |S )Nr   T)dataZweightz(the graph edges do not have {} attribute)nxread_gpicklelennodesnpzeros	enumerateedgesKeyErrorformatZto_numpy_matrix)Zin_filesedge_keyfirstfilesr   Zmatrixidxnamegraphuvd r#   </tmp/pip-build-7vycvbft/nipype/nipype/interfaces/cmtk/nbs.pyntwks_to_matrices   s    
 r%   c               @   s   e Zd ZeedddddZeedddddZeddZej	d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
ZeddZeddZdS )NetworkBasedStatisticInputSpecT)existsz(Networks for the first group of subjects)	mandatorydescz)Networks for the second group of subjectszFAn optional network used to position the nodes for the output networks)r)   i  z!Number of permutations to perform)Z
usedefaultr)   r   zT-statistic thresholdleftrightZbothz(Can be one of "left", "right", or "both"Znumber_of_fiberszUsually "number_of_fibers, "fiber_length_mean", "fiber_length_std" for matrices made with CMTKSometimes "weight" or "value" for functional networks.z/Output network with edges identified by the NBSzFOutput network with p-values to weight the edges identified by the NBSN)__name__
__module____qualname__r
   r   	in_group1	in_group2node_position_networkr   ZIntnumber_of_permutationsZFloat	thresholdEnumt_tailStrr   Zout_nbs_networkZout_nbs_pval_networkr#   r#   r#   r$   r&   .   s4   
r&   c               @   s6   e Zd ZedddZedddZeeddddZdS )NetworkBasedStatisticOutputSpecTz/Output network with edges identified by the NBS)r'   r)   zFOutput network with p-values to weight the edges identified by the NBS)r'   )r)   N)r,   r-   r.   r   nbs_networknbs_pval_networkr   network_filesr#   r#   r#   r$   r7   S   s   
r7   c               @   s4   e Zd ZdZeZeZdZdd Z	dd Z
dd Zd	S )
NetworkBasedStatistica\  
    Calculates and outputs the average network given a set of input NetworkX gpickle files

    See Also
    --------
    For documentation of Network-based statistic parameters:
    https://github.com/LTS5/connectomeviewer/blob/master/cviewer/libs/pyconto/groupstatistics/nbs/_nbs.py

    Example
    -------
    >>> import nipype.interfaces.cmtk as cmtk
    >>> nbs = cmtk.NetworkBasedStatistic()
    >>> nbs.inputs.in_group1 = ['subj1.pck', 'subj2.pck'] # doctest: +SKIP
    >>> nbs.inputs.in_group2 = ['pat1.pck', 'pat2.pck'] # doctest: +SKIP
    >>> nbs.run()                 # doctest: +SKIP

    Zcviewerc             C   s  ddl m} | jj}| jj}| jj}| jj}|d t| d t| d | d }t| jj	|}t| jj
|}	|j||	|||\}
}}tjd tj|
 |j }x:t|
D ].\}}tj||d k\}}|
| |||f< qW tj|}tj|}tj|d	d
 }tj|dd
 }t| jjr,| jj}n| jj	d }tj|}tjd| x.|jddD ]\}}||j|< ||j|< q\W tjd| }tj| tj|| tjd| tjd| }tj| tj|| tjd| |S )Nr   )nbsz-thresh-z-k-z-tail-z.pckz	p-values:r   c             S   s   | d S )Nr   r#   )xr#   r#   r$   <lambda>   s    z6NetworkBasedStatistic._run_interface.<locals>.<lambda>c             S   s   | d S )Nr   r#   )r=   r#   r#   r$   r>      s    z4Populating node dictionaries with attributes from %sT)r   NBS_Result_z$Saving output NBS edge network as %sNBS_P_vals_z#Saving output p-value network as %s)Z$cviewer.libs.pyconto.groupstatisticsr<   inputsr3   r2   r5   r   strr%   r/   r0   Zcompute_nbsifloggerinfocopyr   r   wherer   Zfrom_numpy_matrixZrelabel_nodesr   r1   r   r   opabspathZwrite_gpickle)selfZruntimer<   THRESHKTAILr   detailsXYZPVALZADJ_ZpADJr   r=   yZnbsgraphZnbs_pval_graphZnode_ntwk_nameZnode_networknidndatapath	pval_pathr#   r#   r$   _run_interfacew   sJ    	(








z$NetworkBasedStatistic._run_interfacec       	      C   s   | j  j }| jj}| jj}| jj}| jj}|d t| d t| d | d }tj	d| }tj	d| }||d< ||d< ||g|d	< |S )
Nz-thresh-z-k-z-tail-z.pckr?   r@   r8   r9   r:   )
output_specgetrA   r3   r2   r5   r   rB   rG   rH   )	rI   outputsrJ   rK   rL   r   rM   rT   rU   r#   r#   r$   _list_outputs   s    	(z#NetworkBasedStatistic._list_outputsc             C   s   |d | S )N.r#   )rI   r   extr#   r#   r$   _gen_outfilename   s    z&NetworkBasedStatistic._gen_outfilenameN)r,   r-   r.   __doc__r&   Z
input_specr7   rW   Z_pkgrV   rZ   r]   r#   r#   r#   r$   r;   `   s   Ar;   )os.pathrT   rG   numpyr   Znetworkxr    r   baser   r   r   r   r	   r
   r   r   r   	getLoggerrC   r%   r&   r7   r;   r#   r#   r#   r$   <module>   s   
(

%