3
îÈ‘[RC  ã               @   s  d Z ddlZddlZddlZddlZddlmZmZm	Z	m
Z
mZ ddlmZmZmZ ddlmZmZmZ ddlmZ ddlmZ ddlmZ dd	lmZmZ dd
lmZ ddlmZ ddl m!Z! ddl"m#Z#m$Z$ ddl%m&Z& ddl'm(Z( dd„ Z)G dd„ deƒZ*dS )z™This source manages a VTK dataset given to it.  When this source is
pickled or persisted, it saves the data given to it in the form of a
gzipped string.
é    N)ÚInstanceÚListÚStrÚBoolÚInt)ÚViewÚGroupÚItem)Úgzip_stringÚgunzip_stringÚ	set_state)Útvtk)Ú	messenger)Ú	array2vtk)Úis_old_pipelineÚconfigure_input_data)ÚSource)Úhandle_children_state)ÚDEnum)ÚPipelineInfoÚget_tvtk_dataset_nameé   )Úhas_attributes)Úget_all_attributesc             C   s’   t jdd�}|j}t|| ƒ d|_|jƒ  |jdkr‚d|_tjdƒ\}}t	j
|ƒ t	j|ƒ ||_|jƒ  t|ƒjƒ }t	j|ƒ n|j}||_|S )zdGiven a dataset, convert the dataset to an ASCII string that can
    be stored for persistence.
    r   )Úwrite_to_output_stringr   z.vtk)r   ZDataSetWriterÚglobal_warning_displayr   ÚupdateZoutput_string_lengthr   ÚtempfileÚmkstempÚosÚcloseÚremoveÚ	file_nameÚwriteÚopenÚreadZoutput_string)ÚdataÚwÚwarnÚfhÚfnameÚsdata© r,   úa/home/yuhw/wc/wire-cell-python/venv/lib/python3.6/site-packages/mayavi/sources/vtk_data_source.pyÚwrite_dataset_to_string&   s"    


 
r.   c                   s¼  e Zd ZdZdZeejdd�Ze	dgdgdgd�Z
eddd	�Zed
dd	�Zeddd	�Zeddd	�Zeddd	�Zeddd	�Zeeedd�edd�edd�edd�edd�edd�edd�ƒƒZeeƒZeeƒZeeƒZeeƒZeeƒZeeƒZeejf dd�Ze dƒZ!e"dFƒZ#‡ fdd „Z$d!d"„ Z%‡ fd#d$„Z&d%d&„ Z'dGd(d)„Z(dHd*d+„Z)dId,d-„Z*d.d/„ Z+d0d1„ Z,d2d3„ Z-d4d5„ Z.d6d7„ Z/d8d9„ Z0d:d;„ Z1d<d=„ Z2d>d?„ Z3d@dA„ Z4dBdC„ Z5dJdDdE„Z6‡  Z7S )KÚVTKDataSourcea�  This source manages a VTK dataset given to it.  When this
    source is pickled or persisted, it saves the data given to it in
    the form of a gzipped string.

    Note that if the VTK dataset has changed internally and you need
    to notify the mayavi pipeline to flush the data just call the
    `modified` method of the VTK dataset and the mayavi pipeline will
    update automatically.
    r   F)Ú
allow_noneÚany)ÚdatasetsÚattribute_typesÚ
attributesÚ_point_scalars_listz"scalar point data attribute to use)Zvalues_nameÚdescÚ_point_vectors_listz#vectors point data attribute to useÚ_point_tensors_listz"tensor point data attribute to useÚ_cell_scalars_listz!scalar cell data attribute to useÚ_cell_vectors_listz"vectors cell data attribute to useÚ_cell_tensors_listz!tensor cell data attribute to useÚpoint_scalars_name)ÚnameÚpoint_vectors_nameÚpoint_tensors_nameÚcell_scalars_nameÚcell_vectors_nameÚcell_tensors_namer&   )Úargsr0   Tr   c                s¦   t t| ƒjƒ }xdD ]}|j|d ƒ qW x4dD ],}|jd
| d d ƒ |jd
| d d ƒ q.W | j}|d k	r¢t|ƒ}tjd dkr’t|j	dƒƒ}nt|ƒ}||d< |S )NÚ_assign_attributeÚ_firstÚ	_observerÚpoint_scalarsÚpoint_vectorsÚpoint_tensorsÚcell_scalarsÚcell_vectorsÚcell_tensorsÚ_Z_listÚ_namer   é   Úasciir&   )rD   rE   rF   )rG   rH   rI   rJ   rK   rL   )
Úsuperr/   Ú__get_pure_state__Úpopr&   r.   ÚsysÚversion_infor
   Úencode)ÚselfÚdr=   r&   r+   Úz)Ú	__class__r,   r-   rR   ›   s     
 z VTKDataSource.__get_pure_state__c             C   sœ   |j }|d k	rdtjd dkr,t|ƒjdƒ}nt|ƒ}tjd|d�}|j}d|_|jƒ  ||_|j	| _ t
| |ddgd� t| j|jƒ t
| |dgd	gd
� d S )Nr   rO   rP   r   )Zread_from_input_stringZinput_stringÚchildrenr&   )ÚignoreÚ*)Úfirstr\   )r&   rT   rU   r   Údecoder   ZDataSetReaderr   r   Úoutputr   r   r[   )rW   ÚstaterY   rX   Úrr(   r,   r,   r-   Ú__set_pure_state__®   s    z VTKDataSource.__set_pure_state__c                s$   | j r
dS | jƒ  tt| ƒjƒ  dS )zRThis is invoked when this object is added to the mayavi
        pipeline.
        N)ÚrunningÚ_update_datarQ   r/   Ústart)rW   )rZ   r,   r-   rf   Æ   s    zVTKDataSource.startc             C   s"   | j jƒ  t| j ƒr| jjƒ  dS )z§Invoke this to flush data changes downstream.  This is
        typically used when you change the data object and want the
        mayavi pipeline to refresh.
        N)r&   Úmodifiedr   rD   r   )rW   r,   r,   r-   r   Õ   s    

zVTKDataSource.updateÚpointc       	      C   sæ   t j|ƒ}t|jƒdks tdƒ‚t| jd| ƒ}t|jƒdkr˜|jd dksTtdƒ‚tjt	|ƒƒ}||_
|j|ƒ d	d
d	ddœ}d|||jd  f }n&tjt	|ƒƒ}||_
|j|ƒ d| }tt| |ƒ|g ƒ}t| |t|ƒƒ dS )aÜ  Add an attribute to the dataset to specified category ('point' or
        'cell').

        One may add a scalar, vector (3/4 components) or a tensor (9 components).

        Note that it is the user's responsibility to set the correct size of
        the arrays.

        Parameters
        ----------

        array: numpy array/list : array data to add.

        name: str: name of the array.

        category: 'point'/'cell': the category of the attribute data.

        rO   zOnly 2D arrays can be added.z%s_datar   é   é   é	   z4Only Nxm arrays where (m in [1,3,4,9]) are supportedÚscalarsÚvectorsÚtensors)r   ri   rj   rk   z_%s_%s_listz_%s_scalars_listN)r   ri   rj   rk   )ÚnpÚasarrayÚlenÚshapeÚAssertionErrorÚgetattrr&   r   Zto_tvtkr   r=   Z	add_arrayÚsetÚsetattrÚsorted)	rW   Úarrayr=   Úcategoryr&   ÚvaÚmappingÚ	attributeÚnamesr,   r,   r-   Úadd_attributeà   s$    


zVTKDataSource.add_attributec             C   sB   | j ||ƒ}t| jd| ƒ}|j|ƒ t| d||f ƒ}|j|ƒS )ztRemove an attribute by its name and optional category (point and
        cell).  Returns the removed array.
        z%s_dataz_%s_%s_list)Ú_find_array_listrt   r&   Zremove_arrayr!   )rW   r=   ry   Útyper&   Ú	attr_listr,   r,   r-   Úremove_attribute  s
    
zVTKDataSource.remove_attributec       	      C   sj   | j ||ƒ}t| jd| ƒ}|j|ƒ}||_d||f }t| |ƒ}|j|ƒ |j|ƒ t| |t|ƒƒ dS )z?Rename a particular attribute from `name1` to `name2`.
        z%s_dataz_%s_%s_listN)	r   rt   r&   Z	get_arrayr=   r!   Úappendrv   rw   )	rW   Zname1Zname2ry   r€   r&   Úarrr|   r�   r,   r,   r-   Úrename_attribute  s    



zVTKDataSource.rename_attributec             C   s¸   t | jƒr6| j}| j||ƒ | jƒ  |jƒ  |g| _ntjƒ }|j	| jƒ |g| _d| _
t| jd ƒg| j_|d k	r€|j| jƒ |jdtjƒ| _tj|ƒ}tj|d| jƒ | jƒ | _d S )NTr   ÚModifiedEvent)r   r&   rD   r   re   r   Úoutputsr   ZTrivialProducerZ
set_outputÚdata_changedr   Úoutput_infor2   Zremove_observerÚ_observer_idZadd_observerr   ÚsendÚto_vtkÚconnectÚ_fire_data_changedÚ	_get_namer=   )rW   ÚoldÚnewÚaaÚtpZnew_vtkr,   r,   r-   Ú_data_changed!  s&    



zVTKDataSource._data_changedc             G   s
   d| _ dS )z%Simply fire the `data_changed` event.TN)rˆ   )rW   rC   r,   r,   r-   rŽ   ?  s    z VTKDataSource._fire_data_changedc             C   sö   |d krd S | j }t|ƒdkrLt||d ƒ}t|d| ƒ}|d ƒ d| _d S | j}d }|dkrf|j}n|dkrt|j}t|d| ƒ}||ƒ |j||jƒ |jƒ d ƒ |dkrä|j	d	ƒrät||d ƒj
}	|	j}
tƒ rä|	j|_|	j|j_|jƒ  d| _d S )
Nr   Ú_datazset_active_%sTrh   ÚcellÚ_DATArl   ÚvtkImageData)r&   rq   rt   rˆ   rD   Ú
point_dataZ	cell_dataÚassignÚupperÚis_arl   Úranger   Ú	data_typeÚscalar_typer`   r   )rW   rž   Z	attr_typeÚvalueÚdatasetrX   Úmethodr’   r&   Úsrb   r,   r,   r-   Ú_set_data_nameC  s4    
zVTKDataSource._set_data_namec             C   s   | j dd|ƒ d S )Nrl   rh   )r¤   )rW   r    r,   r,   r-   Ú_point_scalars_name_changede  s    z)VTKDataSource._point_scalars_name_changedc             C   s   | j dd|ƒ d S )Nrm   rh   )r¤   )rW   r    r,   r,   r-   Ú_point_vectors_name_changedh  s    z)VTKDataSource._point_vectors_name_changedc             C   s   | j dd|ƒ d S )Nrn   rh   )r¤   )rW   r    r,   r,   r-   Ú_point_tensors_name_changedk  s    z)VTKDataSource._point_tensors_name_changedc             C   s   | j dd|ƒ d S )Nrl   r–   )r¤   )rW   r    r,   r,   r-   Ú_cell_scalars_name_changedn  s    z(VTKDataSource._cell_scalars_name_changedc             C   s   | j dd|ƒ d S )Nrm   r–   )r¤   )rW   r    r,   r,   r-   Ú_cell_vectors_name_changedq  s    z(VTKDataSource._cell_vectors_name_changedc             C   s   | j dd|ƒ d S )Nrn   r–   )r¤   )rW   r    r,   r,   r-   Ú_cell_tensors_name_changedt  s    z(VTKDataSource._cell_tensors_name_changedc             C   s–   | j d krd S t| j ƒ\}}| j j}|j}| j jdƒr`|d k	r`|j}tƒ r`|j| jj	_
|j| j _
dd„ }|| |dƒ || |dƒ | jrŒd| _d| _d S )Nr˜   c       
      S   sè   dddg}| j }t| jd| ƒ}xÂ|D ]º}t|| ƒ}|jdƒ t| d||f |ƒ t|ƒdkr&t| d||f ƒ}| jrŒt|ƒd	krŒ|d	 }t|d
| ƒ|ƒ |j||j	ƒ |j	ƒ d ƒ |j
ƒ  d||f |ddi}	| jf |	Ž q&W dS )zÅGiven the object, the dict of the attributes from the
            `get_all_attributes` function and the data type
            (point/cell) data this will setup the object and the data.
            rl   rm   rn   z%s_dataÚ z_%s_%s_listr   z
%s_%s_namer   zset_active_%sr—   Útrait_change_notifyFN)rD   rt   r&   rw   rƒ   rv   rq   rE   rš   r›   r   Ú	trait_set)
Úobjr4   Zd_typeÚattrsr’   r&   ÚattrÚvaluesÚdefaultÚkwr,   r,   r-   Ú_setup_data_traits‡  s$    


z6VTKDataSource._update_data.<locals>._setup_data_traitsrh   r–   FT)r&   r   r™   rl   rœ   r�   r   rž   rD   r`   rŸ   rE   rˆ   )rW   Zpnt_attrZ	cell_attrÚpdrl   rb   r´   r,   r,   r-   re   w  s     

zVTKDataSource._update_datac             C   s6   d}| j dk	r | j jj}d| }d| jkr2|d7 }|S )z/ Gets the name to display on the tree.
        zVTK Data (uninitialized)NzVTK Data (%s)z[Hidden]z	 [Hidden])r&   rZ   Ú__name__r=   )rW   ÚretÚtypr,   r,   r-   r�   ¦  s    


zVTKDataSource._get_namec             C   sN   dddg}x.|D ]&}d||f }t | |ƒ}||kr|S qW td||f ƒ‚dS )zrReturn information on which kind of attribute contains the
        specified named array in a particular category.rl   rm   rn   z_%s_%s_listz)No %s array named %s available in datasetN)rt   ÚKeyError)rW   r=   ry   Útypesr€   r°   r}   r,   r,   r-   r   ±  s    


zVTKDataSource._find_array_listéÿÿÿÿ)rh   )rh   )rh   )rh   )8r¶   Ú
__module__Ú__qualname__Ú__doc__Ú__version__r   r   ÚDataSetr&   r   r‰   r   r<   r>   r?   r@   rA   rB   r   r   r	   Úviewr   r   r5   r7   r8   r9   r:   r;   ZAssignAttributerD   r   rE   r   rŠ   rR   rc   rf   r   r~   r‚   r…   r”   rŽ   r¤   r¥   r¦   r§   r¨   r©   rª   re   r�   r   Ú__classcell__r,   r,   )rZ   r-   r/   D   sl   


'


"/r/   )+r¾   rT   r   r   Únumpyro   Ú
traits.apir   r   r   r   r   Útraitsui.apir   r   r	   Ú"apptools.persistence.state_picklerr
   r   r   Útvtk.apir   r   Ztvtk.array_handlerr   Ztvtk.commonr   r   Zmayavi.core.sourcer   Úmayavi.core.commonr   Zmayavi.core.trait_defsr   Úmayavi.core.pipeline_infor   r   Úutilsr   Zvtk_xml_file_readerr   r.   r/   r,   r,   r,   r-   Ú<module>   s&   