3
d%                 @   s   d Z ddlZddlZddlm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 d
Zyed W n ek
r   dZY nX G dd deZG dd de
ZG dd de	ZdS )z
Interfaces to functionality from nitime for time-series analysis of fmri data

- nitime.analysis.CoherenceAnalyzer: Coherence/y
- nitime.fmri.io:
- nitime.viz.drawmatrix_channels

    N   )package_check)fname_presuffix   )NitimeBaseInterface   )TraitedSpecFile	Undefinedtraits	isdefinedBaseInterfaceInputSpecTZnitimeFc               @   s   e Zd ZdZeddddZejddZej	d	dZ
ejd
ddddZejdddddZejddgdddddZeddZeddZejdddddZdS )CoherenceAnalyzerInputSpecin_filein_TSzWcsv file with ROIs on the columns and time-points on the rows. ROI names at the top rowTTR)descexistsrequiresz:The TR used to collect the data in your csv file <in_file>)r   za nitime TimeSeries object    @   zThis is the size of the window used for the spectral estimation. Use values between 32 and the number of samples in your time-series.(Defaults to 64.))lowvalue
usedefaultr   r   zMThe number of samples which overlapbetween subsequent windows.(Defaults to 0)g{Gz?g333333?r   z\The range of frequencies overwhich the analysis will average.[low,high] (Default [0.02,0.15])r   r   Zminlenmaxlenr   zaFile to write outputs (coherence,time-delay) with file-names: ``file_name_{coherence,timedelay}``zFile to write output figures (coherence,time-delay) with file-names:
``file_name_{coherence,timedelay}``. Possible formats: .png,.svg,.pdf,.jpg,...matrixnetworkzThe type of plot to generate, where 'matrix' denotes a matrix image and'network' denotes a graph representation. Default: 'matrix')r   r   N)r   r   )r   )__name__
__module____qualname__Z_xor_inputsr	   r   r   ZFloatr   ZAnyr   ZRangeNFFT	n_overlapListfrequency_rangeoutput_csv_fileoutput_figure_fileEnumfigure_type r(   r(   C/tmp/pip-build-7vycvbft/nipype/nipype/interfaces/nitime/analysis.pyr   "   sB   r   c               @   sL   e Zd ZejddZejddZeddZeddZ	eddZ
eddZdS )CoherenceAnalyzerOutputSpecz.The pairwise coherence values between the ROIs)r   z6The pairwise time delays between the ROIs (in seconds)z4A csv file containing the pairwise  coherence valuesz4A csv file containing the pairwise time delay valuesz$Figure representing coherence valuesN)r   r   r   r   Arraycoherence_arraytimedelay_arrayr	   coherence_csvtimedelay_csvcoherence_figtimedelay_figr(   r(   r(   r)   r*   p   s   


r*   c               @   sH   e Zd ZdZeZeZdd Zdd Z	dd Z
dd	 Zd
d Zdd ZdS )CoherenceAnalyzerz4Wraps nitime.analysis.CoherenceAnalyzer: Coherence/yc             C   sf   t | jjj }|d j s$tdt | jjj jddjdjd}t	j
| jjdddj}||fS )aC  
        Read from csv in_file and return an array and ROI names

        The input file should have a first row containing the names of the
        ROIs (strings)

        the rest of the data will be read in and transposed so that the rows
        (TRs) will becomes the second (and last) dimension of the array

        r   zFFirst row of in_file should contain ROI names as strings of characters" 
,)Zskiprows	delimiter)openinputsr   readlineisalpha
ValueErrorreplacestripsplitnpZloadtxtT)selfZ	first_row	roi_namesdatar(   r(   r)   	_read_csv   s    $zCoherenceAnalyzer._read_csvc             C   s:   ddl m} | j \}}||| jjdd}t|d|_|S )zBRead data from the in_file and generate a nitime TimeSeries objectr   )
TimeSeriess)rD   Zsampling_intervalZ	time_unit)ROIs)Znitime.timeseriesrF   rE   r9   r   dictmetadata)rB   rF   rD   rC   TSr(   r(   r)   _csv2ts   s
    zCoherenceAnalyzer._csv2tsc             C   s   dd l j}| jj\}}| jjtkr,| j }n| jj}d|jkrVdd t|j	D | _
n|jd | _
|j|td| jj| jjdd}tj|j| jjd k|j| jjd k  d }tj|jd d d d |f d	| _tj|jd d d d |f d
| _|S )Nr   rH   c             S   s   g | ]\}}d | qS )zroi_%dr(   ).0x_r(   r(   r)   
<listcomp>   s    z4CoherenceAnalyzer._run_interface.<locals>.<listcomp>Zwelch)Zthis_methodr    r!   )methodr   rR   )Znitime.analysisZanalysisr9   r#   r   r
   rL   rJ   	enumeraterD   rH   r2   rI   r    r!   r@   whereZfrequenciesZmean	coherencedelay)rB   ZruntimeZntaZlbZubrK   AZfreq_idxr(   r(   r)   _run_interface   s*    


""z CoherenceAnalyzer._run_interfacec             C   s   | j  j }| j|d< | j|d< t| jjrft| drf| j  t	| jjdd|d< t	| jjdd|d< t| jj
rt| dr| j  t	| jj
dd|d	< t	| jj
dd|d
< |S )Nr,   r-   rU   
_coherence)suffixr.   _delayr/   r0   r1   )output_specgetrU   rV   r   r9   r$   hasattr_make_output_filesr   r%   _make_output_figures)rB   outputsr(   r(   r)   _list_outputs   s     

zCoherenceAnalyzer._list_outputsc             C   s   xt | j| jgddgD ]}tj d }tj||d dd tt| j	j
d|d  dd	}|jddj| j d
  x.t | jt|D ]\}}|jd||f  qW |j  qW dS )z0
        Generate the output csv files.
        rU   rV   r   r   r6   )r7   z_%s)rZ   zw+r5   z%s,%sN)ziprU   rV   tempfilemkstempr@   Zsavetxtr8   r   r9   r$   writejoinrH   close)rB   thisZtmp_fZfidrliner(   r(   r)   r_      s    z$CoherenceAnalyzer._make_output_filesc             C   s   ddl j}| jjdkrl|j| j| jdd}|jt| jj	dd |j| j
| jdd}|jt| jj	dd nP|j| j| jd}|jt| jj	dd |j| j
| jd}|jt| jj	dd dS )	zu
        Generate the desired figure and save the files according to
        self.inputs.output_figure_file

        r   Nr   )channel_namesZcolor_anchorrY   )rZ   r[   )rl   )Z
nitime.vizvizr9   r'   Zdrawmatrix_channelsrU   rH   Zsavefigr   r%   rV   Zdrawgraph_channels)rB   rm   Zfig_cohZfig_dtr(   r(   r)   r`     s     
z&CoherenceAnalyzer._make_output_figuresN)r   r   r   __doc__r   Z
input_specr*   r\   rE   rL   rX   rb   r_   r`   r(   r(   r(   r)   r2      s   )%r2   )rn   numpyr@   rd   Z
utils.miscr   Zutils.filemanipr   baser   r   r	   r
   r   r   r   Zhave_nitimeImportErrorr   r*   r2   r(   r(   r(   r)   <module>   s    	
N