3
Yd++                 @   sJ  d Z ddl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 dd	lmZ d
dlmZmZmZmZ G dd de
jZG dd de
jZG dd de
jZdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Z G d$d% d%e
jZ!G d&d' d'e
jZ"G d(d) d)e
j#Z$d*d+ Z%ej&j'd,d-d.gd/d0 Z(d1d2 Z)dS )3z"Tests for the engine utils module
    N)deepcopy   )engine   )base)utility)config   )clean_working_directorywrite_workflow_provload_resultfileformat_nodec               @   s   e Zd ZejdddZdS )	InputSpecT)existscopyfileN)__name__
__module____qualname__nibFilein_file r   r   Y/var/www/html/virt/lib/python3.6/site-packages/nipype/pipeline/engine/tests/test_utils.pyr      s   r   c               @   s    e Zd ZejjejjddZdS )
OutputSpecoutputs)descN)r   r   r   r   traitsListZIntoutput1r   r   r   r   r      s   r   c               @   s$   e Zd ZeZeZdd Zdd ZdS )UtilsTestInterfacec             C   s
   d|_ |S )Nr   )
returncode)selfruntimer   r   r   _run_interface"   s    z!UtilsTestInterface._run_interfacec             C   s   | j  j }dg|d< |S )N   r   )Z_outputsget)r!   r   r   r   r   _list_outputs&   s    
z UtilsTestInterface._list_outputsN)	r   r   r   r   
input_specr   output_specr#   r&   r   r   r   r   r      s   r   c       	      C   sL  dd }t jd| jd}t jtjddgdd| jd}dd	d
ddgf|_d	d
ddg|j_t jtj	 d| jd}|j
|d|d
df|d |j
|d|d t jtjddgdd| jd}d
dddg|j_|j
|d|d t jtj	 d| jd}|j
|d|d
df|d |j
|d|d |j }|j| t jt|}t|j dksHtd S )Nc             S   s   dd l }|j| | | j S )Nr   )Znumpyarraytolist)Zarg1Zarg2Zarg3npr   r   r   test_function-   s    z0test_identitynode_removal.<locals>.test_functionZtestidentity)namebase_dirab)fieldssrcr   r$   r	   r   selectorZinlistindexcdZpasserr   outZ	selector2   r9   )peWorkflowstrpathNodeniuIdentityInterface	iterablesinputsr/   ZSelectconnectr5   Z_create_flat_graphZ_set_needed_outputsZgenerate_expanded_graphr   lennodesAssertionError)	tmpdirr,   wfZn1Zn2Zn3Zn4fgegr   r   r   test_identitynode_removal,   s(    
rJ   c             C   s  G dd dt j}G dd dt j}| }| }ddddd	d
g}g }x,|D ]$}| j|}|jd |j|j qJW |d dd |_|d |_|d |_dg}	t	j
  tjj|d stt	j
  t	jddd t|| j||	tt	j}
tjj|d st|
j|d kstt	jddd t|| j||	tt	j}
tjj|d sJttjj|d s`ttjj|d svttjj|d  st|
jt jkstt|
jdkstt	j
  d S )Nc               @   s"   e Zd ZejjejZej ZdS )z0test_clean_working_directory.<locals>.OutputSpecN)	r   r   r   r   r   r   r   filesothersr   r   r   r   r   O   s   r   c               @   s   e Zd Zej ZdS )z/test_clean_working_directory.<locals>.InputSpecN)r   r   r   r   r   infiler   r   r   r   r   S   s   r   zfile.hdrzfile.imgz	file.BRIKz	file.HEADz_0x1234.jsonzfoo.txtdummyr   r	      r$   rK   	executionZremove_unnecessary_outputsFTr   r9   )r   TraitedSpecjoinwriteappendr<   rK   rL   rM   r   Zset_default_configospathr   rE   setr
   r   	_sections	UndefinedrC   )rF   r   r   r   rA   	filenamesoutfilesfilenameoutfileZneeded_outputsr7   r   r   r   test_clean_working_directoryN   sJ    




r^   c             C   s|   dd }t j| d}t jtjdgdg|ddd}ddgf|_t jtjdgdg|ddd}ddgf|_|j|d|d |S )	z*Creates a workflow for the following testsc             S   s   | S )Nr   )fwhmr   r   r   r_      s    zcreate_wf.<locals>.fwhm)r-   r_   )input_namesoutput_namesfunctionprocr   Zproc2)r:   r;   r=   r>   Functionr@   rB   )r-   r_   pipeprocessZprocess2r   r   r   	create_wf   s    rg   c             C   sP   t jdd}| j|_|jdd tdD  |jdd}t|j dksLt	d S )	Nmeta)r-   c             S   s   g | ]}t d | qS )zwf%d)rg   ).0ir   r   r   
<listcomp>   s    z4test_multi_disconnected_iterable.<locals>.<listcomp>   Linear)plugin<   )
r:   r;   r<   r.   	add_nodesrangerunrC   rD   rE   )rF   metawfrI   r   r   r    test_multi_disconnected_iterable   s
    rt   c             C   s|   t jdd}| j|_|jdd tdD  |jdd}| jdj}t||d	d
}t	|j
dksdtt	|j dksxtd S )Nrh   )r-   c             S   s   g | ]}t d | qS )zwf%d)rg   )ri   rj   r   r   r   rk      s    z#test_provenance.<locals>.<listcomp>r$   rm   )rn   Zworkflow_provenance_testall)formatr	      )r:   r;   r<   r.   rp   rq   rr   rR   r   rC   ZbundlesrE   Zget_records)rF   rs   rI   Z	prov_baseZpsgr   r   r   test_provenance   s    rx   c             C   s   | d S )Nr$   r   )valuer   r   r   
dummy_func   s    rz   c          	   C   s   t j }tjtjdgdgtddgdd}dd tdD |j_	t
tj|_d	|jd
 d< | j|_tjtjj |j  W dQ R X t j| dS )z3Test mapnode crash when stop_on_first_crash is TrueWRONG	newstring)r`   ra   rb   myfunc)	iterfieldr-   c             S   s   g | ]}d j |qS )zstring{})rv   )ri   rj   r   r   r   rk      s    z&test_mapnode_crash.<locals>.<listcomp>r   TrP   Zstop_on_first_crashN)rU   getcwdr:   MapNoder>   rd   rz   rq   rA   r{   r   r   rX   r<   r.   pytestraisesrD   ZNodeExecutionErrorrr   chdir)rF   cwdnoder   r   r   test_mapnode_crash   s    r   c          	   C   st   t j }tjtjdgdgtddgdd}dd tdD |j_	| j
|_tjt |j  W d	Q R X t j| d	S )
z4Test mapnode crash when stop_on_first_crash is Falser{   r|   )r`   ra   rb   r}   )r~   r-   c             S   s   g | ]}d j |qS )zstring{})rv   )ri   rj   r   r   r   rk      s    z'test_mapnode_crash2.<locals>.<listcomp>r   N)rU   r   r:   r   r>   rd   rz   rq   rA   r{   r<   r.   r   r   	Exceptionrr   r   )rF   r   r   r   r   r   test_mapnode_crash2   s    r   c          
   C   s   | j   tjtjdgdgtddgdd}dd tdD |j_tj	d	}|j
|g | j|_tj |jd
 d< tjt |jdd W dQ R X dS )z9Test mapnode crash when mapnode is embedded in a workflowr{   r|   )r`   ra   rb   r}   )r~   r-   c             S   s   g | ]}d j |qS )zstring{})rv   )ri   rj   r   r   r   rk      s    z'test_mapnode_crash3.<locals>.<listcomp>r   ZtestmapnodecrashrP   Zcrashdump_dirrm   )rn   N)r   r:   r   r>   rd   rz   rq   rA   r{   r;   rp   r<   r.   rU   r   r   r   r   RuntimeErrorrr   )rF   r   rG   r   r   r   test_mapnode_crash3   s    
r   c               @   s   e Zd Zejj ZdS )StrPathConfuserInputSpecN)r   r   r   r   r   Stringin_strr   r   r   r   r      s   r   c               @   sj   e Zd ZejjejejjZejj	ejjejddZ
ejj ZejjejjZejj ZejddZdS )StrPathConfuserOutputSpecT)r   N)r   r   r   r   r   Tupler   r   	out_tupleDictout_dict_pathZ
DictStrStrout_dict_strr   out_listout_strout_pathr   r   r   r   r      s   

r   c               @   s   e Zd ZeZeZdd ZdS )StrPathConfuserc             C   s   t jjt jj| jjd }t|dj  | jj| jd< || jd< || jjf| jd< | jj|i| jd< | jj| jji| jd< | jjgd | jd	< |S )
N_pathwr   r   r   r   r   r	   r   )	rU   rV   abspathbasenamerA   r   opencloseZ_results)r!   r"   r   r   r   r   r#      s    
zStrPathConfuser._run_interfaceN)r   r   r   r   r'   r   r(   r#   r   r   r   r   r      s   r   c             C   s   | j   tjtdddd}tddj  |j j}|j}|dksFt	|j
}tjj|s\t	|j||fksnt	|j||ikst	|j||ikst	|j|gd kst	dS )a  
    There was a bug in which, if the current working directory contained a file with the name
    of an output String, the string would get transformed into a path, and generally wreak havoc.
    This attempts to replicate that condition, using an object with strings and paths in various
    trait configurations, to ensure that the guards added resolve the issue.
    Please see https://github.com/nipy/nipype/issues/2944 for more details.
    2)r   spc)r-   r   r	   N)r   r:   r=   r   r   r   rr   r   r   rE   r   rU   rV   isabsr   r   r   r   )rF   r   r   r   r   r   r   r   test_modify_paths_bug  s    
r   use_relativeTFc       
      C   s  ddl m}m} | j  tjdd}tjdd| tjt	dddd}| j
d	j|_|j }t| jd	jdjd
j}|jj |jj kst|j|jkst|jj |jj kst|| jd	j| jdj || jd	j |rt| jdjdjd
j}|jj |jj kst|j|jks0t|jj |jj ksJt|jjjdd}	|jj|	kslt|jjd |	kst|jjd |	kstn4tjtj" t| jdjdjd
j W dQ R X tjdd| dS )z8Test minimally the save/load functions for result files.r   )copytreermtreerP   Zuse_relative_pathsr   )r   r   )r-   r   zresult_spc.pklznode2z/node/z/node2/N)shutilr   r   r   r   
getbooleanrW   r:   r=   r   mkdirr<   r.   rr   r   rR   r"   ZdictcopyrE   rA   r   r%   r   replacer   r   r   r   r   Z
TraitError)
rF   r   r   r   Zold_use_relativer   resultZloaded_resultZloaded_result2newpathr   r   r   test_save_load_resultfile!  s8    $r   c              C   sX   t jtjddgddd} t| }dt ji}tdj|| |d jj| jjksTt	d S )Nr/   r0   )r1   r   )r-   r=   
)
r:   r=   r>   r?   r   execrR   Z	interface_fieldsrE   )r   Z
serializedZ	workspacer   r   r   test_format_nodeQ  s
    
r   )*__doc__rU   copyr   r    r   r:   Z
interfacesr   r   r   r>   r   utilsr
   r   r   r   rQ   r   r   ZBaseInterfacer   rJ   r^   rg   rt   rx   rz   r   r   r   r   r   ZSimpleInterfacer   r   markZparametrizer   r   r   r   r   r   <module>   s4   "2	0