a
    «ˆcÞI  ã                   @   s8  d Z ddlZddl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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 ddlmZ edd� G dd„ dejƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deeƒZ G dd„ deƒZ!G dd„ deƒZ"e#dk�r4e $¡  dS )zZ
Integration tests of mlab with the null engine.

This also tests some numerics with VTK.
é    N)Úpatch)Úassert_allclose)ÚUnittestTools)Úpush_exception_handler)Úmlab)ÚEngine)Útvtk)Úengine_manager)Úregistry)Úget_example_dataT)Zreraise_exceptionsc                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚTestMlabNullEnginezU Stub mlab to isolate as well as possible from creation of a new
        figure.
    c                 C   s*   t tj ¡ ƒD ]}| ¡  t |¡ qd S ©N)Úlistr
   ÚenginesÚvaluesÚstopÚunregister_engine©ÚselfZengine© r   úe/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/mayavi/tests/test_mlab_integration.pyÚ_stop_unregister_all_engines#   s    z/TestMlabNullEngine._stop_unregister_all_enginesc                 C   s"   |   ¡  dtj_t ¡ }|| _d S )NÚtest)r   r   ÚoptionsÚbackendÚ
get_engineÚe©r   r   r   r   r   ÚsetUp*   s    zTestMlabNullEngine.setUpc                 C   sP   t  ¡ }|| ju}d t_| j ¡  t | j¡ |rL| ¡  t |¡ tdƒ‚d S )Nz"The NullEngine has been overridden)	r   r   r   r	   Úcurrent_enginer   r
   r   ÚAssertionError)r   r   Zengine_overriddenr   r   r   ÚtearDown0   s    


zTestMlabNullEngine.tearDownN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r!   r   r   r   r   r      s   r   c                   @   sp   e Zd Z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dd„ Zdd„ Zdd„ Zdd„ ZdS )ÚTestMlabNullEngineMiscz. Misc tests for mlab with the null engine
    c                 C   sŒ   t  d¡}d|d< tj |¡}tj |¡}| ¡ j ¡ j	\}}}t j
 |g d¢¡ t j
 |g d¢¡ t j
 |g d¢¡ |jd urˆtdƒ‚d S )N©é   r(   r(   é   )r)   r)   r)   )ç       @r*   ç      ø?ç      @r*   r*   )r*   r+   r*   r*   r,   r*   )r+   r*   r*   r*   r*   r,   z The NullEngine seems not to work)ÚnpÚzerosr   ÚpipelineÚscalar_fieldZcontourÚget_output_datasetZpointsZto_arrayÚTÚtestingZassert_almost_equalÚscener    )r   ÚaÚsrcÚfilterÚxÚyÚzr   r   r   Útest_contour_filterA   s    

z*TestMlabNullEngineMisc.test_contour_filterc                 C   s^   t j d¡\}}}tj |||¡}tjj|dd�}|  t|jƒd¡ |  	t
| ¡ tjƒ¡ d S )N©r(   éd   ZGaussianSplatter)r7   r)   )r-   Úrandomr   r/   Úscalar_scatterZuser_definedÚassertEqualÚlenÚoutputsÚ
assertTrueÚ
isinstancer1   r   Z	ImageData)r   r8   r9   r:   r6   Zdensityr   r   r   Útest_user_defined_filterS   s    ÿz/TestMlabNullEngineMisc.test_user_defined_filterc           	      C   s  t jjft jjft jjt jjft jft jft jff}tj	 	d¡}|D ]:}|d |ƒ}|dd … D ]}||ƒ}q`|  
t|dƒ¡ qDtj	 	d¡\}}}t jft jft jft jjft jjt jjft jjt jjft jjff}|D ]>}|d |||ƒ}|dd … D ]}||ƒ}qø|  
t|dƒ¡ qØd S )Nr'   r   r)   Úmlab_source)r   r/   r?   r0   Zimage_plane_widgetÚ	contour3dÚvolume_sliceZpoints3dr-   r>   rC   ÚhasattrÚmeshÚsurfÚquiver3dZvector_scatterZextract_vector_componentsZextract_vector_normÚarray2d_source)	r   Z	pipelinesÚdatar/   ÚobjÚfactoryr8   r9   r:   r   r   r   Útest_mlab_source]   s:    ú
þö
z'TestMlabNullEngineMisc.test_mlab_sourcec                 C   sD   g d¢g}t j d¡\}}}tj ||||¡}tjj|ddd�}d S )N)r   r)   é   r'   r=   ée   ©ÚvminÚvmax)r-   r>   r   r/   Ztriangular_mesh_sourceZsurface)r   Ztrisr8   r9   r:   rJ   rK   r   r   r   Útest_strange_vmin_vmax�   s    
z-TestMlabNullEngineMisc.test_strange_vmin_vmaxc                 C   s`   t  ¡ }tj |¡ t ¡ }|jd jd }ddlm	} |  
t||ƒ¡ |  t |j¡|¡ d S )Nr   )ÚVTKDataSource)ÚvtkZvtkPolyDatar   r/   Zadd_datasetr   ZscenesÚchildrenZmayavi.sources.vtk_data_sourcerX   rC   rD   r@   r   Zto_vtkrN   )r   Úpdr   r6   rX   r   r   r   Ú(test_add_dataset_works_with_vtk_datasets‡   s    z?TestMlabNullEngineMisc.test_add_dataset_works_with_vtk_datasetsc                 C   sò   t  ¡ }t  ¡ }|  |j|u ¡ t  ¡ }|  |j|u ¡ t  |¡ |  |j|u ¡ t  d¡}|  |j|u ¡ t  d¡}|  |j|u ¡ t  d¡ |  |j|u ¡ t  d¡}|  |j|u ¡ t  d¡}|  |j|u ¡ t  d¡ |  |j|u ¡ dS )z* Various tests for mlab.figure().
        r(   é   zTest 1zTest 2N)r   Úfigurer   rC   Úcurrent_scene)r   Úf1r   Úf2r   r   r   Útest_figure“   s&    






z"TestMlabNullEngineMisc.test_figurec                 C   sø   t  ¡ }|  |j¡ t  |¡ |  |j¡ t  d¡}|  |j¡ t  d¡ |  |j¡ t  d¡}|  |j¡ t  d¡ |  |j¡ t  ¡ }|  |j¡ t  ¡  |  |j¡ dd„ tdƒD ƒ}|D ]}|  |j¡ qÀt jdd� |D ]}|  |j¡ qâdS )	z) Various tests for mlab.close().
        i:  rb   c                 S   s   g | ]}t  ¡ ‘qS r   )r   r^   )Ú.0Úir   r   r   Ú
<listcomp>Æ   ó    z5TestMlabNullEngineMisc.test_close.<locals>.<listcomp>é   T)ÚallN)r   r^   rC   ZrunningÚcloseÚassertFalseÚrange)r   ÚfZfigsr   r   r   Ú
test_close¯   s,    




z!TestMlabNullEngineMisc.test_closec                 C   sÐ   d}t jt  |d ¡t  |d ¡d |d t  |d ¡ f }t j d|f¡\}}}t ||||¡}d}t jt  |d ¡t  |d ¡d |d t  |d ¡ f }t j d|f¡\}}}|jj||||d� dS )zº When reseting the triangular mesh (ie polydata), if we are
            not careful, we can create a segfault by passing triangules
            between points that do not exist.
        r=   r(   r)   iè  ©r8   r9   r:   Ú	trianglesN)r-   Zc_Zaranger>   r   Útriangular_meshrF   Úreset)r   Únro   r8   r9   r:   r6   r   r   r   Útest_triangular_mesh_resetÍ   s    $ÿ$ÿz1TestMlabNullEngineMisc.test_triangular_mesh_resetc                 C   sX   t j d¡}tj|dd�}tj|dd�}t ¡  |  |jjjd¡ |  |jjjd¡ dS )	zˆ Test that when an object with scalars hidden is created, it
            does not get a colorbar, unless no other is avalaible.
        )rg   rg   Z
gist_earth©Zcolormap©r   r   r   )ÚcolorFTN)	r-   r>   r   rK   Zcolorbarr@   Úmodule_managerÚscalar_lut_managerZshow_scalar_bar)r   r5   Ús1Ús2r   r   r   Útest_colorbarâ   s    
ÿ
ÿz$TestMlabNullEngineMisc.test_colorbarc                 C   sb   t j d¡\}}}tj |||¡}t d¡}| |¡ |  t	j
 |¡¡ t	j
 |¡r^t	 |¡ d S )Nr<   z.vtk)r-   r>   r   r/   r?   ÚtempfileÚmktempZsave_outputrC   ÚosÚpathÚexistsÚremove)r   r8   r9   r:   r6   Ztmpfnamer   r   r   Ú#test_source_can_save_output_to_fileñ   s    

z:TestMlabNullEngineMisc.test_source_can_save_output_to_filec                 C   sL   t j tdƒ¡}t j |¡}|jjddd� t j |¡}t|j	j	j
dƒ d S )NzuGridEx.vtkTrR   )Zcell_clippingZcell_maximum)ç      ð?r*   ç        rƒ   r„   rƒ   )r   r/   Úopenr   Zextract_unstructured_gridr7   Z	trait_setZslice_unstructured_gridr   ÚactorZbounds)r   r6   ZegZsugr   r   r   Útest_slice_unstructured_grid  s
    z3TestMlabNullEngineMisc.test_slice_unstructured_gridc           
      C   s¸   t jddd…ddd…f \}}|d |d  }t  ||d  ¡}tj |¡}|jj}|jj	j
}|j |j ¡ ¡}d|j |¡_|j ¡  tjj|dd�}	|  t  |	 ¡ jj	j
|¡¡ d S )Nr   é
   y              Y@rR   çš™™™™™¹?rv   )Zpoint_scalars)r-   ÚmgridZarctanr   r/   rM   rF   ÚdatasetZ
point_dataÚscalarsrk   Z	add_arrayr2   ZravelZ	get_arrayÚnameÚupdateZset_active_attributerj   Úallcloser1   )
r   r8   r9   r:   Úwr6   r‹   Zscalar_rangeZarray_idrl   r   r   r   Útest_set_active_attribute  s    

ÿÿz0TestMlabNullEngineMisc.test_set_active_attributeN)r"   r#   r$   r%   r;   rE   rQ   rW   r\   rb   rm   rs   r{   r‚   r‡   r‘   r   r   r   r   r&   =   s   
$r&   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚTestSourceResetc                 C   sX   t j d¡\}}}t ||||||¡}|j}t j d¡\}}}|j||||||d� d S )Nr<   ©r(   rg   )r8   r9   r:   ÚuÚvr�   )r-   r>   r   rL   rF   rq   )r   r8   r9   r:   Úgr6   r   r   r   Ú(test_glyph_reset_twice_with_fewer_points(  s
    z8TestSourceReset.test_glyph_reset_twice_with_fewer_pointsc                 C   sp   t  ddd¡}t  |¡t  |¡ }}t ||||¡}t  ddd¡}t  |¡t  |¡ }}|jj||||d� d S )Nr   rˆ   r=   rg   ©r8   r9   r:   rŒ   )r-   ZlinspaceÚcosÚsinr   Zplot3drF   rq   )r   Útr8   r9   Úlr   r   r   Ú(test_lines_reset_twice_with_fewer_points2  s    z8TestSourceReset.test_lines_reset_twice_with_fewer_pointsc                 C   sn   t jddd…ddd…f \}}t |||| ¡}t jddd…ddd…f \}}|jj|||| || d� d S )Nr   r)   ù              $@y               @r˜   )r-   rŠ   r   rJ   rF   rq   )r   r8   r9   rK   r   r   r   Ú'test_mesh_reset_twice_with_fewer_points>  s    z7TestSourceReset.test_mesh_reset_twice_with_fewer_pointsc                 C   s€   t j d¡\}}}t j ddd¡}| d¡ t ||||¡}t j d¡\}}}t j ddd¡}| d	¡ |jj||||d
� d S )Nr<   r   r=   i,  )r=   r(   r“   rg   é   )rg   r(   rn   )r-   r>   ÚrandintÚresizer   rp   rF   rq   )r   r8   r9   r:   ÚcellsrK   r   r   r   Ú*test_trimesh_reset_twice_with_fewer_pointsH  s    

z:TestSourceReset.test_trimesh_reset_twice_with_fewer_pointsN)r"   r#   r$   r—   r�   rŸ   r¤   r   r   r   r   r’   '  s   

r’   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚTestMlabPipelinea/   Test the pipeline functions.
        For vtk versions greater than 5.10, widgets need
        a render window interactor to be set, otherwise an error is raised.
        As such this test checks for the current VTK version and setups a real
        engine for vtk > 5.10 and null engine otherwise.
    c                 C   s   t ƒ }| ¡  t |¡ d S r   )r   Ústartr   Z
set_enginer   r   r   r   r   _  s    zTestMlabPipeline.setUpc                 C   s"   t tj ¡ ƒD ]}t |¡ qd S r   )r   r
   r   Úkeysr   r   r   r   r   r!   d  s    zTestMlabPipeline.tearDownc                 C   s  t jddd…ddd…ddd…f \}}}t  |d |d  |d  ¡}t ||||¡}t j d¡\}}}tj ||||¡}	t jj	|	t  |d |d  |d  ¡dd� t 
||||||¡}
tjj|
|||dd�\}}}t jj	||dd� t jj	||dd� t jj	||d	d� d
S )z Test probe_data
        r   r)   rž   rR   )r(   rˆ   r]   rR   )ÚdecimalZvectors)Útyper(   N)r-   rŠ   Úsqrtr   rG   r>   r/   Z
probe_datar3   Úassert_array_almost_equalÚflow)r   r8   r9   r:   ÚrZisoZx_Zy_Zz_Zr_r¬   Zu_Zv_Zw_r   r   r   Útest_probe_datah  s    ( ÿz TestMlabPipeline.test_probe_dataN)r"   r#   r$   r%   r   r!   r®   r   r   r   r   r¥   W  s   r¥   c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚTestMlabHelperFunctionsz: Test various behaviors of the mlab helper functions.
    c                 C   sj   t j d¡}t  |j¡\}}t |||¡}t |¡}tj|dd�}|||fD ]}|  |jjjj	d¡ qLd S )N©rˆ   rˆ   F)Z
auto_scaleçÍÌÌÌÌÌì?)
r-   r>   ÚindicesÚshaper   Úbarchartr@   ÚglyphZglyph_sourceZy_length)r   Úsr8   r9   Zbar1Zbar2Zbar3Úbarr   r   r   Útest_barchart�  s    
z%TestMlabHelperFunctions.test_barchartc                 C   s   t j d¡}t |¡}d S )Nr°   )r-   r>   r   Úimshow)r   r¶   rO   r   r   r   Útest_imshow�  s    z#TestMlabHelperFunctions.test_imshowc                 C   s    t jtj dd¡g d¢d� d S )Nrˆ   é   )éÿÿÿÿé   r¼   é   r   r   )Zextent)r   r¹   r-   r>   Zrand)r   r   r   r   Útest_imshow_extent’  s    z*TestMlabHelperFunctions.test_imshow_extentc                 C   sX   t j ddd¡}tj|dd�}|  |d¡� d|jj_W d   ƒ n1 sJ0    Y  d S )Nr   r½   )r=   r=   Zcoolrt   Zpipeline_changedZjet)	r-   r>   r¡   r   r¹   ZassertTraitChangesrw   rx   Zlut_mode)r   r5   r†   r   r   r   Útest_imshow_colormap•  s    z,TestMlabHelperFunctions.test_imshow_colormapc                 C   sj   t jddd…ddd…ddd…f \}}}|| d ||  || d  }tj|dd�}|  |jjd¡ d S )Néûÿÿÿrg   y              4@ç      à?r*   Zy_axes)Úplane_orientation)r-   Zogridr   rH   r@   ÚipwrÃ   )r   r8   r9   r:   rŒ   rÄ   r   r   r   Útest_volume_sliceŸ  s    ( z)TestMlabHelperFunctions.test_volume_sliceN)	r"   r#   r$   r%   r¸   rº   r¿   rÀ   rÅ   r   r   r   r   r¯   }  s   
r¯   c                   @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )ÚTestMlabModulesz Test the mlab modules.
    c           
   	   C   sè  t  d¡}d|d< tj |¡}d}tjj|dd|d�}t j d¡D ]}t  |j 	|¡|¡ qBdt j d¡ D ]}|  
|j |¡d¡ qldd	t j d¡  D ]}|  
|j |¡d
¡ qštj |¡}|jjd |jjd  }tjj|ddd�}|jjd |jjd  }	t j d¡D ]4}t j |j 	|| ¡|j 	dd|	 |  ¡¡ �qtj d| ¡}tj |¡}|jjd |jjd  }tjj|ddd�}|jjd |jjd  }	t j d¡D ]4}t j |j 	d| | ¡|j 	d|	|  ¡¡ �q®dS )z' Test the mlab volume factory.
        r'   r)   r   )r)   r‰   gj¼t“Ô?rÂ   r±   )rU   rV   rv   rˆ   r‰   gš™™™™™É?g      Ð?g      è?rT   rR   r+   N)r-   r.   r   r/   r0   Úvolumer>   r�   Z_ctfZ	get_colorr@   Z_otfÚ	get_valuerk   r3   r«   )
r   rŒ   r6   rv   ZvolÚvalueZvol1Zrange1Zvol2Zrange2r   r   r   Útest_volumeª  s<    
þþzTestMlabModules.test_volumec                 C   sZ   t j d¡}tj |¡}t ddd¡ tjddddd� t d¡ |  ttjddd	¡ d
S )z Test the text module.
        r'   r‰   r±   Úfoor(   )r:   r   r+   r   N)	r-   r>   r   r/   r0   ÚtextÚtitleÚassertRaisesÚ
ValueError©r   rN   r6   r   r   r   Ú	test_textÓ  s    
zTestMlabModules.test_textc                 C   s8   t j d¡}tj |¡}tjdddddddddd	�	}d
S )z! Test the text3d module.
        r'   r   rË   rÂ   rR   Fru   )éZ   r   r   )ZopacityZscaleZorient_to_camerarv   ZorientationN)r-   r>   r   r/   r0   Ztext3d)r   rN   r6   r›   r   r   r   Útest_text3dà  s    ÷zTestMlabModules.test_text3dc                 C   s@   t j d¡}tj |¡}tj |¡ tj |¡ tj |¡ dS )z%Test the contour_grid_plane.
        )rˆ   rˆ   rˆ   N)r-   r>   r   r/   r0   ÚoutlineZ
grid_planeZcontour_grid_planerÐ   r   r   r   Útest_contour_grid_planeñ  s
    z'TestMlabModules.test_contour_grid_planec                 C   sV   t  t j d¡¡}t |¡}|  |jjjd¡ |d7 }|j 	¡  |  |jjjd¡ dS )zTest the barchart function.)r(   r(   Zscale_by_vector_componentsr)   N)
r-   Úabsr>   r   r´   r@   rµ   Z
scale_moderF   rŽ   )r   r¶   Úbr   r   r   r¸   ú  s    

ÿ

ÿzTestMlabModules.test_barchartc                 C   s.   t  ¡ }t  |¡}t|jjg d¢ddd� d S )N)ç      ø¿r+   rØ   r+   g      à¿rÂ   r   r‰   )ZrtolZatol)r   Ztest_plot3dZaxesr   Úranges)r   r¶   r5   r   r   r   Ú	test_axes  s
    
ÿzTestMlabModules.test_axesN)
r"   r#   r$   r%   rÊ   rÑ   rÓ   rÕ   r¸   rÚ   r   r   r   r   rÆ   ¦  s   )	rÆ   c                   @   s   e Zd Zdd„ ZdS )ÚTestMlabAnimatec                 C   s°   t jdd�dd„ ƒ}| j ¡  ddlm} |ƒ | jj_| jjjj}ddl	m
} td|ƒ�  |ƒ }|j ¡  W d   ƒ n1 s~0    Y  |j ¡  |  |jjd	¡ |j ¡  d S )
NF)Zuic                  s   s   t dƒD ]
} d V  qd S )Nrg   )rk   )rd   r   r   r   Úanim  s    z>TestMlabAnimate.test_animate_sets_up_movie_maker.<locals>.animr   )Úmake_mock_scene)Ú	NoUITimerzmayavi.tools.animator.Timerr]   )r   Zanimater   Z	new_sceneZmayavi.tests.test_file_timesteprÝ   r_   r4   Zmovie_makerZmayavi.core.file_data_sourcerÞ   r   ZtimerZStartZanimation_startZassert_called_once_withr@   Zanimation_stepZ
call_countZanimation_stop)r   rÜ   rÝ   ÚmmrÞ   r5   r   r   r   Ú test_animate_sets_up_movie_maker  s    


(
z0TestMlabAnimate.test_animate_sets_up_movie_makerN)r"   r#   r$   rà   r   r   r   r   rÛ     s   rÛ   Ú__main__)%r%   r~   r|   ZunittestZunittest.mockr   Únumpyr-   Znumpy.testingr   Ztraits.testing.unittest_toolsr   Z
traits.apir   rY   Zmayavir   Zmayavi.core.enginer   Ztvtk.apir   Zmayavi.tools.engine_managerr	   Zmayavi.core.registryr
   Zmayavi.tests.commonr   ZTestCaser   r&   r’   r¥   r¯   rÆ   rÛ   r"   Úmainr   r   r   r   Ú<module>   s4   
 k0&)j
