3
d(                 @   s   d dl Z d dlmZ d dlmZmZmZmZmZ d dl	m
Z
mZ d dl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 )    N)Path)FileInputMultiPathTraitedSpectraits	isdefined)
NestedCellCell)
SPMCommand)SPMCommandInputSpec)split_filenamec               @   s   e Zd ZeedddddddZeeddddddZejdd	dd
dZ	ejdddddZ
ejdddddZejdddddZejdddddZdS )+ExtractAdditionalSurfaceParametersInputSpecT)existsZ	data_surfzLeft and central surfaces filesF)fielddesc	mandatorycopyfilezAll surface files)r   r   r   ZGIzExtract gyrification index (GI) based on absolute mean curvature. The method is described in Luders et al. Neuroimage, 29:1224-1230, 2006)r   Z
usedefaultr   gmvzExtract volumeareazExtract area surfaceZSDzaExtract sulcus depth based on euclidian distance between the central surface anf its convex hull.ZFDztExtract cortical complexity (fractal dimension) which is described in Yotter ar al. Neuroimage, 56(3): 961-973, 2011N)__name__
__module____qualname__r   r   left_central_surfacessurface_filesr   ZBoolgyrificationr   r   depthfractal_dimension r   r   A/tmp/pip-build-7vycvbft/nipype/nipype/interfaces/cat12/surface.pyr      s2   r   c               @   s   e Zd ZejeddddZejeddddZejeddddZejeddddZ	ejeddddZ
ejeddd	dZejeddd
dZejeddddZejeddddZejeddddZejeddddZejeddddZdS ),ExtractAdditionalSurfaceParametersOutputSpecT)r   z+Files of left Hemisphere extracted measures)r   z,Files of right Hemisphere extracted measureszGyrification of left Hemispherez Gyrification of right Hemispherez%Grey matter volume of left Hemispherez&Grey matter volume of right HemispherezArea of left HemispherezArea of right HemispherezDepth of left HemispherezDepth of right Hemispherez$Fractal Dimension of left Hemispherez%Fractal Dimension of right HemisphereN)r   r   r   r   Listr   Zlh_extracted_filesZrh_extracted_filesZlh_gyrificationZrh_gyrificationZlh_gmvZrh_gmvZlh_areaZrh_areaZlh_depthZrh_depthZlh_fractaldimensionZrh_fractaldimensionr   r   r   r   r   0   s(   r   c                   s<   e Zd ZdZeZeZ fddZdd Z	 fddZ
  ZS )"ExtractAdditionalSurfaceParametersav  
    Additional surface parameters can be extracted that can be used for statistical analysis, such as:

    * Central surfaces
    * Surface area
    * Surface GM volume
    * Gyrification Index
    * Sulcus depth
    * Toro's gyrification index
    * Shaer's local gyrification index
    * Laplacian gyrification indeces
    * Addicional surfaces
    * Measure normalization
    * Lazy processing

    http://www.neuro.uni-jena.de/cat12/CAT12-Manual.pdf#page=53

    Examples
    --------
    >>> # Set the left surface files, both will be processed
    >>> lh_path_central = 'lh.central.structural.gii'
    >>> # Put here all surface files generated by CAT12 Segment, this is only required if the this approach is putted in
    >>> surf_files = ['lh.sphere.reg.structural.gii', 'rh.sphere.reg.structural.gii', 'lh.sphere.structural.gii', 'rh.sphere.structural.gii', 'rh.central.structural.gii', 'lh.pbt.structural', 'rh.pbt.structural']
    >>> extract_additional_measures = ExtractAdditionalSurfaceParameters(left_central_surfaces=lh_path_central, surface_files=surf_files)
    >>> extract_additional_measures.run() # doctest: +SKIP

    c                s2   t  j}|r d|kr d| _d| _t jf | d S )Nz12.toolszcat.stools.surfextract)r
   version_jobtype_jobnamesuper__init__)selfinputs_local_version)	__class__r   r   r'   t   s
    z+ExtractAdditionalSurfaceParameters.__init__c             C   s  | j  j }| jjdf| jjdf| jjdf| jjdf| jjdfg}x| jjD ]}t	|\}}}|j
ddd }x|D ]\}}	|rtxdD ]}
|
d }|
d |	 }t|| sg ||< t|| sg ||< dj|
|	|g}|| jtjj|| || jtjj|| qW qtW qLW |S )Nr   r   r   r   Zfractaldimension.      rhlhZ_extracted_files_)r/   r0   )_outputsgetr)   r   r   r   r   r   r   r   splitr   joinappendospath)r(   outputsZnames_outputsfilenamepthbaseextZoriginal_filenameZextracted_parameterZparameter_nameZ
hemisphereZall_files_hemisphereZname_hemisphereZgenerated_filenamer   r   r   _list_outputs|   s2    




z0ExtractAdditionalSurfaceParameters._list_outputsc                s$   |dkrt |S tt| j|||S )Nr   )Cell2Strr&   r!   _format_arg)r(   optspecval)r+   r   r   rA      s    
z.ExtractAdditionalSurfaceParameters._format_arg)r   r   r   __doc__r   
input_specr   output_specr'   r?   rA   __classcell__r   r   )r+   r   r!   T   s   'r!   c               @   s~   e Zd ZeeddddddZeedddddddZeeddd	dddZeeddd
ddddZeeddddddZ	dS )'ExtractROIBasedSurfaceMeasuresInputSpecT)r   z;Surface data files. This variable should be a list with allF)r   r   r   Zrdataz&(Left) ROI Atlas. These are the ROI's )r   r   r   r   z'(Right) ROI Atlas. These are the ROI's cdataz(Left) Surface data files. z(Right) Surface data files.N)
r   r   r   r   r   r   lh_roi_atlasZrh_roi_atlaslh_surface_measureZrh_surface_measurer   r   r   r   rI      s6   rI   c               @   s    e Zd ZejeddddZdS )(ExtractROIBasedSurfaceMeasuresOutputSpecT)r   z+Files with the measures extracted for ROIs.)r   N)r   r   r   r   r    r   label_filesr   r   r   r   rM      s   rM   c                   s8   e Zd ZdZeZeZdd Z fddZ	dd Z
  ZS )ExtractROIBasedSurfaceMeasuresac  
    Extract ROI-based surface values
    While ROI-based values for VBM (volume) data are automatically saved in the ``label`` folder as XML file it is
    necessary to additionally extract these values for surface data (except for thickness which is automatically
    extracted during segmentation). This has to be done after preprocessing the data and creating cortical surfaces.

    You can extract ROI-based values for cortical thickness but also for any other surface parameter that was extracted
    using the Extract Additional Surface Parameters such as volume, area, depth, gyrification and fractal dimension.


     http://www.neuro.uni-jena.de/cat12/CAT12-Manual.pdf#page=53

     Examples
     --------
    >>> # Template surface files
    >>> lh_atlas = 'lh.aparc_a2009s.freesurfer.annot'
    >>> rh_atlas = 'rh.aparc_a2009s.freesurfer.annot'
    >>> surf_files = ['lh.sphere.reg.structural.gii', 'rh.sphere.reg.structural.gii', 'lh.sphere.structural.gii', 'rh.sphere.structural.gii', 'lh.central.structural.gii', 'rh.central.structural.gii', 'lh.pbt.structural', 'rh.pbt.structural']
    >>> lh_measure = 'lh.area.structural'
    >>> extract_additional_measures = ExtractROIBasedSurfaceMeasures(surface_files=surf_files, lh_surface_measure=lh_measure, lh_roi_atlas=lh_atlas, rh_roi_atlas=rh_atlas)
    >>> extract_additional_measures.run() # doctest: +SKIP


    c             K   s2   t  j}|r d|kr d| _d| _t j| f| d S )Nz12.r"   zcat.stools.surf2roi)r
   r#   r$   r%   r'   )r(   r)   r*   r   r   r   r'      s
    z'ExtractROIBasedSurfaceMeasures.__init__c                s4   |dkrt |S |dkr t|S tt| j|||S )NrL   rK   )r   r@   r&   rO   rA   )r(   rB   rC   rD   )r+   r   r   rA      s
    z*ExtractROIBasedSurfaceMeasures._format_argc             C   sB   | j  j }t| jjd \}}}dd t|jdD |d< |S )Nr   c             S   s   g | ]}|j  rt|qS r   )is_filestr).0labelr   r   r   
<listcomp>  s    z@ExtractROIBasedSurfaceMeasures._list_outputs.<locals>.<listcomp>zlabel/*rN   )r3   r4   r   r)   rL   r   glob)r(   r:   r<   r=   r>   r   r   r   r?     s    z,ExtractROIBasedSurfaceMeasures._list_outputs)r   r   r   rE   rI   rF   rM   rG   r'   rA   r?   rH   r   r   )r+   r   rO      s   rO   c               @   s   e Zd Zdd ZdS )r@   c             C   s   d| j   S )z-Convert input to appropriate format for cat12z{%s})Z	to_string)r(   r   r   r   __str__  s    zCell2Str.__str__N)r   r   r   rV   r   r   r   r   r@     s   r@   )r8   Zpathlibr   Znipype.interfaces.baser   r   r   r   r   Znipype.interfaces.cat12.baser   r	   Znipype.interfaces.spmr
   Znipype.interfaces.spm.baser   Znipype.utils.filemanipr   r   r   r!   rI   rM   rO   r@   r   r   r   r   <module>   s   %$W(8