3
d                  @   s   d 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 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dd ZdS )a   The freesurfer module provides basic functions for interfacing with
freesurfer tools.

Currently these tools are supported:

     * Dicom2Nifti: using mri_convert
     * Resample: using mri_convert

Examples
--------
See the docstrings for the individual classes for 'working' examples.

    N   )LooseVersion)fname_presuffix   )CommandLine	DirectoryCommandLineInputSpec	isdefinedtraitsTraitedSpecFilePackageInfoZrestructuredtextc               @   sR   e Zd ZdZejdr*ejjejddZe	dd Z
edd Zedd	 Zd
S )InfozFreesurfer subject directory and version information.

    Examples
    --------

    >>> from nipype.interfaces.freesurfer import Info
    >>> Info.version()  # doctest: +SKIP
    >>> Info.subjectsdir()  # doctest: +SKIP

    ZFREESURFER_HOMEzbuild-stamp.txtc             C   s   | j  d S )Nr   )
splitlines)Zraw_info r   C/tmp/pip-build-7vycvbft/nipype/nipype/interfaces/freesurfer/base.pyparse_version2   s    zInfo.parse_versionc             C   s   | j  }|dkrtdS |j jd}yt|d d W n tk
rP   d}Y nX d|d  }|r|d d	krxd
| }q|d d dkr|d dd }qtdd |d D dkr|d }qtd| n(d	|kr|d d }n|j jdd }t|S )zcReturn a comparable version object

        If no version found, use LooseVersion('0.0.0')
        Nz0.0.0-       .r   devz	6.0.0-dev   r   vc             S   s   g | ]}|d krdqS )r   r   r   ).0valr   r   r   
<listcomp>O   s    z%Info.looseversion.<locals>.<listcomp>r   zUnknown version string: z-devz-vr   r   r   )versionr   rstripsplitint
ValueErrorlenRuntimeError)clsverZvinfoZgithashZvstrr   r   r   looseversion6   s*    


zInfo.looseversionc             C   s   | j  rtjd S dS )a&  Check the global SUBJECTS_DIR

        Parameters
        ----------

        subjects_dir :  string
            The system defined subjects directory

        Returns
        -------

        subject_dir : string
            Represents the current environment setting of SUBJECTS_DIR

        SUBJECTS_DIRN)r   osenviron)r&   r   r   r   subjectsdir\   s    
zInfo.subjectsdirN)__name__
__module____qualname____doc__r*   getenvpathjoinZversion_filestaticmethodr   classmethodr(   r,   r   r   r   r   r   #   s   

&r   c               @   s   e Zd ZedddZdS )FSTraitedSpecTzsubjects directory)existsdescN)r-   r.   r/   r   subjects_dirr   r   r   r   r6   r   s   r6   c                   s^   e Zd ZdZeZdZ fddZdd Ze	dd Z
 fd	d
ZdddZedd Z  ZS )	FSCommandzaGeneral support for FreeSurfer commands.

    Every FS command accepts 'subjects_dir' input.
    Nc                s\   t t| jf | | jj| jd | js2tj | _t	| jj
 rP| jrP| j| j_
| j  d S )Nr9   )superr:   __init__inputson_trait_change_subjects_dir_update_subjects_dirr   r,   r	   r9   )selfr=   )	__class__r   r   r<      s    

zFSCommand.__init__c             C   s"   | j jr| j jjd| j ji d S )Nr)   )r=   r9   r+   update)rA   r   r   r   r?      s    zFSCommand._subjects_dir_updatec             C   s
   || _ d S )N)r@   )r&   r9   r   r   r   set_default_subjects_dir   s    z"FSCommand.set_default_subjects_dirc                s.   d|kr|d | j _| j  tt| jf |S )Nr9   )r=   r9   r?   r;   r:   run)rA   r=   )rB   r   r   rE      s    zFSCommand.run_fsTc             C   sF   |dkr"d| j  }|d7 }t||dkr2tj }t||||d}|S )a  Define a generic mapping for a single outfile

        The filename is potentially autogenerated by suffixing inputs.infile

        Parameters
        ----------
        basename : string (required)
            filename to base the new filename on
        fname : string
            if not None, just use this fname
        cwd : string
            prefix paths with cwd, otherwise os.getcwd()
        suffix : string
            default suffix
        r   z,Unable to generate filename for command %s. zbasename is not set!N)suffixuse_extnewpath)cmdr#   r*   getcwdr   )rA   basenamefnamecwdrG   rH   msgr   r   r   
_gen_fname   s    
zFSCommand._gen_fnamec             C   s   t j }|tdkr|jS d S )Nz0.0.0)r   r(   r   vstring)rA   r'   r   r   r   r      s    zFSCommand.version)NNrF   T)r-   r.   r/   r0   r6   
input_specr@   r<   r?   r5   rD   rE   rP   propertyr   __classcell__r   r   )rB   r   r:   v   s   	
r:   c               @   s   e Zd ZdZedd ZdS )FSSurfaceCommandaj  Support for FreeSurfer surface-related functions.
    For some functions, if the output file is not specified starting with 'lh.'
    or 'rh.', FreeSurfer prepends the prefix from the input file to the output
    filename. Output out_file must be adjusted to accommodate this. By
    including the full path in the filename, we can also avoid this behavior.
    c             C   sj   t jj|\}}|dkr\t jj| \}} d}| dd |kr\|dd |kr\| dd | }t jj||S )ao  Based on MRIsBuildFileName in freesurfer/utils/mrisurf.c

        If no path information is provided for out_name, use path and
        hemisphere (if also unspecified) from in_file to determine the path
        of the associated file.
        Use in_file prefix to indicate hemisphere for out_name, rather than
        inspecting the surface data structure.
        r   lh.rh.Nr   )rV   rW   )r*   r2   r!   r3   )in_fileZout_namer2   baseZhemisr   r   r   _associated_file   s    
 z!FSSurfaceCommand._associated_fileN)r-   r.   r/   r0   r4   rZ   r   r   r   r   rU      s   rU   c               @   s    e Zd ZdZdZdZdd ZdS )FSScriptCommandz?Support for Freesurfer script commands with log terminal_outputfileFc             C   s    | j  j }tjjd|d< |S )Nzoutput.nipypelog_file)Z_outputsgetr*   r2   abspath)rA   outputsr   r   r   _list_outputs   s    zFSScriptCommand._list_outputsN)r-   r.   r/   r0   Z_terminal_outputZ_always_runra   r   r   r   r   r[      s   r[   c               @   s   e Zd ZedddddZdS )FSScriptOutputSpeczoutput.nipypeTzThe output log)Z
usedefaultr7   r8   N)r-   r.   r/   r   r]   r   r   r   r   rb      s   rb   c               @   s   e Zd ZejddZdS )FSTraitedSpecOpenMPz"allows for specifying more threads)r8   N)r-   r.   r/   r
   ZIntnum_threadsr   r   r   r   rc      s   rc   c                   s<   e Zd ZdZeZdZ fddZdd Z fddZ	  Z
S )	FSCommandOpenMPzSupport for FS commands that utilize OpenMP

    Sets the environment variable 'OMP_NUM_THREADS' to the number
    of threads specified by the input num_threads.
    Nc                s|   t t| jf | | jj| jd | jsNtjj	dd | _| jsNtjj	dd | _t
| jj rp| jrpt| j| j_| j  d S )Nrd   OMP_NUM_THREADSZNSLOTS)r;   re   r<   r=   r>   _num_threads_update_num_threadsr*   r+   r^   r	   rd   r"   )rA   r=   )rB   r   r   r<      s    zFSCommandOpenMP.__init__c             C   s&   | j jr"| j jjdt| j ji d S )Nrf   )r=   rd   r+   rC   str)rA   r   r   r   rg      s    z#FSCommandOpenMP._num_threads_updatec                s.   d|kr|d | j _| j  tt| jf |S )Nrd   )r=   rd   rg   r;   re   rE   )rA   r=   )rB   r   r   rE     s    zFSCommandOpenMP.run)r-   r.   r/   r0   rc   rR   rh   r<   rg   rE   rT   r   r   )rB   r   re      s   re   c               C   s   t j dkrdS dS dS )zzChecks if FreeSurfer is NOT installed
    used with skipif to skip tests that will
    fail if FreeSurfer is not installedNTF)r   r   r   r   r   r   no_freesurfer  s    rj   )r0   r*   r   r   Zutils.filemanipr   rY   r   r   r   r	   r
   r   r   r   Z__docformat__r   r6   r:   rU   r[   rb   rc   re   rj   r   r   r   r   <module>   s   (OA#