3
d                 @   s   d Z ddlZddlZddlZddlmZmZm	Z	m
Z
mZ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e
ZdS )zXNilearn is a Python library for fast and easy statistical learning on NeuroImaging data.    N   )traitsTraitedSpecLibraryBaseInterfaceSimpleInterfaceBaseInterfaceInputSpecFileInputMultiPathc               @   s   e Zd ZdZdS )NilearnBaseInterfaceZnilearnN)__name__
__module____qualname__Z_pkg r   r   ;/tmp/pip-build-7vycvbft/nipype/nipype/interfaces/nilearn.pyr
      s   r
   c               @   s|   e Zd ZeddddZeedddddZejdddZ	eddd	d
dZ
ejddddZejd	dddZejd	dddZdS )SignalExtractionInputSpecTz4-D fMRI nii file)exists	mandatorydesc)r   z{a 3-D label image, with 0 denoting background, or a list of 3-D probability maps (one per label) or the equivalent 4D file.)r   r   zHuman-readable labels for each segment in the label file, in order. The length of class_labels must be equal to the number of segments (background excluded). This list corresponds to the class labels in label_file in ascending orderzsignals.tsvFz9The name of the file to output to. signals.tsv by default)
usedefaultr   r   a  By default (True), returns simple time series calculated from each region independently (e.g., for noise regression). If False, returns unique signals for each region, discarding shared variance (e.g., for connectivity. Only has effect with 4D probability maps.)r   r   zIf True, include an extra column labeled "GlobalSignal", with values calculated from the entire brain (instead of just regions).z*If True, perform detrending using nilearn.N)r   r   r   r   in_filer	   label_filesr   Listclass_labelsout_fileZBoolincl_shared_varianceinclude_globaldetrendr   r   r   r   r      s.   r   c               @   s   e Zd ZedddZdS )SignalExtractionOutputSpecTztsv file containing the computed signals, with as many columns as there are labels and as many rows as there are timepoints in in_file, plus a header row with values from class_labels)r   r   N)r   r   r   r   r   r   r   r   r   r   H   s   r   c               @   s0   e Zd ZdZeZeZdd Zdd Z	dd Z
dS )	SignalExtractiona  
    Extracts signals over tissue classes or brain regions

    >>> seinterface = SignalExtraction()
    >>> seinterface.inputs.in_file = 'functional.nii'
    >>> seinterface.inputs.label_files = 'segmentation0.nii.gz'
    >>> seinterface.inputs.out_file = 'means.tsv'
    >>> segments = ['CSF', 'GrayMatter', 'WhiteMatter']
    >>> seinterface.inputs.class_labels = segments
    >>> seinterface.inputs.detrend = True
    >>> seinterface.inputs.include_global = True
    c             C   s   | j  }g }x |D ]}|j|j| jj qW tj|}tj| jj|j	t
f}tjj|j| jj| jd< tj| jd |ddd |S )Nr   s   %s	)fmt	delimiter)_process_inputsappendZfit_transforminputsr   npZhstackZvstackr   astypestrospathjoincwdr   Z_resultsZsavetxt)selfZruntimemaskersZsignalsmaskerZregion_signalsoutputr   r   r   _run_interfacec   s    

zSignalExtraction._run_interfacec       
      C   s  ddl j}ddlj}|j| jj}g }tj|j	dkrTtj|j	}|j
|j| nT|jd }| jjrx@|j|D ] }|j
|j| j|j	|j qrW n|j
|j| tjt||stdj| jj|t| jj|krtdj| jj|| jj| jjrb|j	jdd}tj|jtjdd}| j||j}|j|| jjd}|jd| | jjjdd	 x|D ]}	|	j| jjd qhW |S )
zvalidate and  process inputs into useful form.
        Returns a list of nilearn maskers and the list of corresponding label
        names.r   N      z9The label files {} contain invalid value {}. Check input.z]The length of class_labels {} does not match the number of regions {} found in label_files {})Zaxis)r   ZGlobalSignal) Znilearn.input_dataZ
input_dataZnilearn.imageimageZconcat_imgsr$   r   r%   ZamaxZdataobjr#   ZNiftiLabelsMaskershaper   Ziter_imgZNiftiMapsMasker_4daffineiscloseint
ValueErrorformatlenr   r   sumZrintr&   Zclipr   insertZ
set_params)
r,   nlZnliZ
label_datar-   Zn_labelsimgZglobal_label_dataZglobal_maskerr.   r   r   r   r"   r   s@    




z SignalExtraction._process_inputsc             C   s&   t j|ddddddtjf |S )zjtakes a 3-dimensional numpy array and an affine,
        returns the equivalent 4th dimensional nifti fileN)nbZNifti1Imager%   Znewaxis)r,   arrayr6   r   r   r   r5      s    zSignalExtraction._4dN)r   r   r   __doc__r   Z
input_specr   Zoutput_specr0   r"   r5   r   r   r   r   r   R   s   :r   )rB   r(   numpyr%   Znibabelr@   Zinterfaces.baser   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   <module>   s   $/
