3
d                 @   sr   d Z ddlZddlmZ ddlmZmZmZmZm	Z	m
Z
mZ dd Ze dkZG d	d
 d
eZG dd deZdS )z!Interfaces to run MATLAB scripts.    N   )config   )CommandLineInputSpecInputMultiPath	isdefinedCommandLinetraitsFile	Directoryc              C   s*   dt jkr&ddlm}  | t jddS dS )z:Determine whether Matlab is installed and can be executed.ZNIPYPE_NO_MATLABr   )whichZ	MATLABCMDmatlabN)osenvironZnipype.utils.filemanipr   getenv)r    r   :/tmp/pip-build-7vycvbft/nipype/nipype/interfaces/matlab.pyget_matlab_command   s    
r   c               @   s   e Zd ZdZejdddd(dZejddd	d
gddZejddddddZ	ejddddddZ
edddZejddddZejddddZeddddZee ddZejddgdd dZejd!d"d#d$d%gd&ddZd'S ))MatlabInputSpecz)Basic expected inputs to Matlab interfacez-r "%s;exit"zm-code to runTr   )argstrdesc	mandatorypositionzuse MCR interface	nodesktopnosplashsingle_comp_thread)r   xornohashz
-nodesktopz)Switch off desktop mode on unix platforms)r   
usedefaultr   r   z	-nosplashzSwitch of splash screenz-logfile %szSave matlab output to log)r   r   z-singleCompThreadzforce single threaded operation)r   r   r   zRun m-code using m-file)r   r   z
pyscript.mzName of file to write m-code to)r   r   zPaths to add to matlabpath)r   zver,ztry,z!prescript to be added before codez
,catch ME,z/fprintf(2,'MATLAB code threw an exception:\n');zfprintf(2,'%s\n',ME.message);zsif length(ME.stack) ~= 0, fprintf(2,'File:%s\nName:%s\nLine:%d\n',ME.stack.file,ME.stack.name,ME.stack.line);, end;zend;zscript added after codeN)__name__
__module____qualname____doc__r	   StrscriptZBooluses_mcrr   r   r
   logfiler   mfilescript_filer   r   pathsList	prescript
postscriptr   r   r   r   r      sH   r   c                   sz   e Zd ZdZdZdZdZdZeZ	d fdd	Z
edd Zedd	 Zed
d Z fddZ fddZdd Z  ZS )MatlabCommandzInterface that runs matlab code

    >>> import nipype.interfaces.matlab as matlab
    >>> mlab = matlab.MatlabCommand(mfile=False)  # don't write script file
    >>> mlab.inputs.script = "which('who')"
    >>> out = mlab.run()  # doctest: +SKIP
    r   Nc                s   t t| jf | |r&t|r&|| _n| jr4| j| _| jrRt| jj rR| j| j_| j	rpt| jj
 rp| j	| j_
t| jj rt| jj rtjddrd| j_d| _dS )zXinitializes interface to matlab
        (default 'matlab -nodesktop -nosplash')
        Z	executionZsingle_thread_matlabT	allatonceN)superr.   __init__r   _cmd_default_matlab_cmd_default_mfileinputsr(   _default_pathsr*   r   r&   r   
getbooleanterminal_output)self
matlab_cmdr5   )	__class__r   r   r1   `   s    

zMatlabCommand.__init__c             C   s
   || _ dS )a&  Set the default MATLAB command line for MATLAB classes.

        This method is used to set values for all MATLAB
        subclasses.  However, setting this will not update the output
        type for any existing instances.  For these, assign the
        <instance>.inputs.matlab_cmd.
        N)r3   )clsr:   r   r   r   set_default_matlab_cmdy   s    	z$MatlabCommand.set_default_matlab_cmdc             C   s
   || _ dS )a'  Set the default MATLAB script file format for MATLAB classes.

        This method is used to set values for all MATLAB
        subclasses.  However, setting this will not update the output
        type for any existing instances.  For these, assign the
        <instance>.inputs.mfile.
        N)r4   )r<   r(   r   r   r   set_default_mfile   s    	zMatlabCommand.set_default_mfilec             C   s
   || _ dS )a  Set the default MATLAB paths for MATLAB classes.

        This method is used to set values for all MATLAB
        subclasses.  However, setting this will not update the output
        type for any existing instances.  For these, assign the
        <instance>.inputs.paths.
        N)r6   )r<   r*   r   r   r   set_default_paths   s    	zMatlabCommand.set_default_pathsc          	      sJ   d| _ tt| j|}ytjd W n   Y nX d|jkrF| j| |S )Nr/   z	stty sanezMATLAB code threw an exception)r8   r0   r.   _run_interfacer   systemstderrZraise_exception)r9   Zruntime)r;   r   r   r@      s    

zMatlabCommand._run_interfacec                s:   |dkr&|j }| jjrd}| j||S tt| j|||S )Nr%   z%s)r%   )r   r5   r&   _gen_matlab_commandr0   r.   _format_arg)r9   nameZ
trait_specvaluer   )r;   r   r   rD      s    zMatlabCommand._format_argc       
      C   s:  t j }| jjp| jj}g }t| jjr0| jj}| jj}| jj}|rR|j	dd n|j	dd x|D ]}|j
d|  qdW |sdjdd |jdD }dj|| dj| }|r"tt jj|| jjd	}|j| W d
Q R X | jjrdt jj|| jj }	nd|| jjjdd f }	ndj|jd}	||	 S )z>Generates commands and, if mfile specified, writes it to disk.r   z<fprintf(1,'Executing %s at %s:\n',mfilename(),datestr(now));z2fprintf(1,'Executing code at %s:\n',datestr(now));zaddpath('%s');
,c             S   s   g | ]}|j  jd s|qS )%)strip
startswith).0liner   r   r   
<listcomp>   s   z5MatlabCommand._gen_matlab_command.<locals>.<listcomp>
wtNz%szaddpath('%s');%s. )r   getcwdr5   r(   r&   r   r*   r,   r-   insertappendjoinsplitopenpathr)   write)
r9   r   Zscript_linescwdr(   r*   r,   r-   rX   r%   r   r   r   rC      s8    


z!MatlabCommand._gen_matlab_command)N)r    r!   r"   r#   r2   r3   r4   r6   r   Z
input_specr1   classmethodr=   r>   r?   r@   rD   rC   __classcell__r   r   )r;   r   r.   Q   s   r.   )r#   r   rQ   r   baser   r   r   r   r	   r
   r   r   Z	no_matlabr   r.   r   r   r   r   <module>   s   $
3