3
dh                 @   s   d Z ddlm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 dd	lmZ ejd
ZG dd deZG dd deZG dd de
Zdd Zdd Zdd ZdS )a  The fsl module provides classes for interfacing with the `FSL
<http://www.fmrib.ox.ac.uk/fsl/index.html>`_ command line tools.  This
was written to work with FSL version 4.1.4.

These are the base tools for working with FSL.
Preprocessing tools are found in fsl/preprocess.py
Model tools are found in fsl/model.py
DTI tools are found in fsl/dti.py

XXX Make this doc current!

Currently these tools are supported:

* BET v2.1: brain extraction
* FAST v4.1: segmentation and bias correction
* FLIRT v5.5: linear registration
* MCFLIRT: motion correction
* FNIRT v1.0: non-linear warp

Examples
--------
See the docstrings of the individual classes for examples.

    )globN   )logging)fname_presuffix   )traits	isdefinedCommandLineCommandLineInputSpecPackageInfo)BibTeXznipype.interfacec               @   sp   e Zd ZdZdddddZejdr:ejjejddd	Z	e
d
d Zedd Zedd Ze
dddZdS )Infoa;  
    Handle FSL ``output_type`` and version information.

    output type refers to the type of file fsl defaults to writing
    eg, NIFTI, NIFTI_GZ

    Examples
    --------

    >>> from nipype.interfaces.fsl import Info
    >>> Info.version()  # doctest: +SKIP
    >>> Info.output_type()  # doctest: +SKIP

    z.niiz.imgz.nii.gzz.img.gz)NIFTIZ
NIFTI_PAIRZNIFTI_GZZNIFTI_PAIR_GZFSLDIRetcZ
fslversionc             C   s   | j  d S )Nr   )
splitlines)Zraw_info r   </tmp/pip-build-7vycvbft/nipype/nipype/interfaces/fsl/base.pyparse_versionB   s    zInfo.parse_versionc             C   s4   y
| j | S  tk
r.   d|f}t|Y nX dS )aH  Get the file extension for the given output type.

        Parameters
        ----------
        output_type : {'NIFTI', 'NIFTI_GZ', 'NIFTI_PAIR', 'NIFTI_PAIR_GZ'}
            String specifying the output type.

        Returns
        -------
        extension : str
            The file extension for the output type.
        zInvalid FSLOUTPUTTYPE: N)ftypesKeyError)clsoutput_typemsgr   r   r   output_type_to_extF   s
    
zInfo.output_type_to_extc             C   s.   y
t jd S  tk
r(   tjd dS X dS )a@  Get the global FSL output file type FSLOUTPUTTYPE.

        This returns the value of the environment variable
        FSLOUTPUTTYPE.  An exception is raised if it is not defined.

        Returns
        -------
        fsl_ftype : string
            Represents the current environment setting of FSLOUTPUTTYPE
        FSLOUTPUTTYPEzJFSLOUTPUTTYPE environment variable is not set. Setting FSLOUTPUTTYPE=NIFTIr   N)osenvironr   IFLOGGERwarning)r   r   r   r   r   [   s    
zInfo.output_typeNc                sr   yt jd }W n tk
r*   tdY nX t jj|dd | dkrd fddtt jj dD S t jj | S )	zGrab an image from the standard location.

        Returns a list of standard images if called without arguments.

        Could be made more fancy to allow for more relocatabilityr   z!FSL environment variables not setdatastandardNc                s   g | ]}|j  d  dqS )/ )replace).0filename)stdpathr   r   
<listcomp>~   s   z'Info.standard_image.<locals>.<listcomp>z*nii*)r   r   r   	Exceptionpathjoinr   )Zimg_nameZfsldirr   )r'   r   standard_imagep   s    
zInfo.standard_image)N)__name__
__module____qualname____doc__r   r   getenvr*   r+   Zversion_filestaticmethodr   classmethodr   r   r,   r   r   r   r   r   (   s   
r   c               @   s*   e Zd ZdZejdeejj	 ddZ
dS )FSLCommandInputSpecz
    Base Input Specification for all FSL Commands

    All command support specifying FSLOUTPUTTYPE dynamically
    via output_type.

    Example
    -------
    fsl.ExtractRoi(tmin=42, tsize=1, output_type='NIFTI')
    r   zFSL output type)descN)r-   r.   r/   r0   r   Enumlistr   r   keysr   r   r   r   r   r4      s   
r4   c                   sn   e Zd ZdZeZdZeddgdgZ fddZ	dd	 Z
ed
d Zedd ZdddZdddZ  ZS )
FSLCommandzBase support for FSL commands.Nz@article{JenkinsonBeckmannBehrensWoolrichSmith2012,author={M. Jenkinson, C.F. Beckmann, T.E. Behrens, M.W. Woolrich, and S.M. Smith},title={FSL},journal={NeuroImage},volume={62},pages={782-790},year={2012},}implementation)entrytagsc                sZ   t t| jf | | jj| jd | jd kr6tj | _t	| jjsN| j| j_n| j  d S )Nr   )
superr9   __init__inputsZon_trait_change_output_update_output_typer   r   r   )selfr?   )	__class__r   r   r>      s    

zFSLCommand.__init__c             C   s$   | j j| _| j jjd| j ji d S )Nr   )r?   r   rA   r   update)rB   r   r   r   r@      s    
zFSLCommand._output_updatec             C   s"   |t jkr|| _ntd| dS )a*  Set the default output type for FSL classes.

        This method is used to set the default output type for all fSL
        subclasses.  However, setting this will not update the output
        type for any existing instances.  For these, assign the
        <instance>.inputs.output_type.
        zInvalid FSL output_type: %sN)r   r   rA   AttributeError)r   r   r   r   r   set_default_output_type   s    

z"FSLCommand.set_default_output_typec             C   s   t j S )N)r   version)rB   r   r   r   rG      s    zFSLCommand.versionTc             C   s   |dkr"d| j  }|d7 }t||dkr2tj }|dkrHtj| jj}|rd|r`dj||f}n|}|dkrpd}t	||d|d}|S )a  Generate a filename based on the given parameters.

        The filename will take the form: cwd/basename<suffix><ext>.
        If change_ext is True, it will use the extentions specified in
        <instance>intputs.output_type.

        Parameters
        ----------
        basename : str
            Filename to base the new filename on.
        cwd : str
            Path to prefix to the new filename. (default is os.getcwd())
        suffix : str
            Suffix to add to the `basename`.  (defaults is '' )
        change_ext : bool
            Flag to change the filename extension to the FSL output type.
            (default True)

        Returns
        -------
        fname : str
            New filename based on given parameters.

        r#   z,Unable to generate filename for command %s. zbasename is not set!NF)suffixZuse_extnewpath)
cmd
ValueErrorr   getcwdr   r   r?   r   r+   r   )rB   basenamecwdrH   Z
change_extextr   fnamer   r   r   
_gen_fname   s     
zFSLCommand._gen_fnamec             C   s   |t j| jj S )N)r   r   r?   r   )rB   valuenamer   r   r   _overload_extension   s    zFSLCommand._overload_extension)NNTN)N)r-   r.   r/   r0   r4   Z
input_specrA   r   Z_referencesr>   r@   r3   rF   propertyrG   rQ   rT   __classcell__r   r   )rC   r   r9      s   

,r9   c              C   s   t j } | rdS dS d S )Nr      )r   rG   )verr   r   r   	check_fsl   s    rY   c               C   s   t j dkrdS dS dS )zlChecks if FSL is NOT installed
    used with skipif to skip tests that will
    fail if FSL is not installedNTF)r   rG   r   r   r   r   no_fsl  s    rZ   c               C   s&   dt jko"t jjt jjt jd  S )z#check if fsl_course data is presentZFSL_COURSE_DATA)r   r   r*   isdirabspathr   r   r   r   no_fsl_course_data  s    
r]   )r0   r   r   r#   r   Zutils.filemanipr   baser   r   r	   r
   r   Zexternal.duer   	getLoggerr   r   r4   r9   rY   rZ   r]   r   r   r   r   <module>   s   
\j