
    1Vh+                         d 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 dd	lmZ d
Z G d de
j"                        Zy)z&TensorBoard 3D mesh visualizer plugin.    N)wrappers)	http_util)provider)base_plugin)metadata)plugin_data_pb2)plugin_utild   c                      e Zd ZdZej
                  Zd Zd Zd Z	d Z
ej                  j                  d        Zd Zd Zd	 Zd
 Zd Zd ZddZej                  j                  d        Zej                  j                  d        Zy)
MeshPluginz0A plugin that serves 3D visualization of meshes.c                     |j                   | _        |j                  xs i j                  | j                  t
              | _        t        j                  dd      | _	        y)zInstantiates a MeshPlugin via TensorBoard core.

        Args:
          context: A base_plugin.TBContext instance. A magic container that
            TensorBoard uses to make objects available to the plugin.
        meshr   )	data_kindlatest_known_versionN)
data_provider_data_providersampling_hintsgetplugin_name_DEFAULT_DOWNSAMPLING_downsample_tor	   _MetadataVersionChecker_version_checker)selfcontexts     T/home/dcms/DCMS/lib/python3.12/site-packages/tensorboard/plugins/mesh/mesh_plugin.py__init__zMeshPlugin.__init__#   sW     &33&55;@@3
 !, C C!"!
    c           	          | j                   j                  ||t        j                  t	        j
                  |g|g            }||   |   j                  }t        j                  |      S )z4Gets the `MeshPluginData` proto for an instance tag.runstagsexperiment_idr   run_tag_filter)r   list_tensorsr   PLUGIN_NAMEr   RunTagFilterplugin_contentparse_plugin_metadata)r   ctx
experimentruninstance_tagresultscontents          r   _instance_tag_metadataz!MeshPlugin._instance_tag_metadata3   sj    %%22$ ,,#00U,	 3 
 #,|,;;--g66r   c                 >    | j                  ||||      j                  S )z2Gets the user-facing tag name for an instance tag.)r1   name)r   r+   r,   r-   r.   s        r   _tagzMeshPlugin._tag@   s#    **S,

$	r   c                 P   | j                   j                  ||t        j                  t	        j
                  |g            }|j                  |i       j                         D cg c]3  \  }}|t        j                  |j                        j                  k(  r|5 c}}S c c}}w )z2Gets the instance tag names for a user-facing tag.)r!   r#   )r   r&   r   r'   r   r(   r   itemsr*   r)   r3   )r   r+   r,   r-   tagindexr.   tss           r   _instance_tagszMeshPlugin._instance_tagsF   s    ##00$ ,,#00se<	 1 
 ',iiR&8&>&>&@
"rh44R5F5FGLLL 
 	
 
s   &8B"c                    t        j                  |j                        }t        j                  |j                        }| j                  j                  ||t        j                        }t               }|j                         D ]  \  }}t               ||<   |j                         D ]  \  }}	t        j                  |	j                        }
| j                  j                  |
j                  ||      sM| j                  ||||      }| j!                  ||||      }d|j"                  d   i||   |<     t%        j&                  ||d      S )aa  A route (HTTP handler) that returns a response with tags.

        Args:
          request: The werkzeug.Request object.

        Returns:
          A response that contains a JSON object. The keys of the object
          are all the runs. Each run is mapped to a (potentially empty)
          list of all tags that are relevant to this plugin.
        )r$   r   samplesr   application/json)r	   r   environr$   r   r&   r   r'   dictr6   r*   r)   r   okversionr4   r1   shaper   Respond)r   requestr+   r,   all_runsresponser-   r"   r.   	metadatummdr7   metas                r   _serve_tagszMeshPlugin._serve_tagsT   s9    !!'//2 ..w?
&&33$ ,, 4 
 6!) 	@IC FHSM+/::< @'i33I4L4LM,,//

CNiiZlC22S,
 '0A%?c"@	@   (4FGGr   c                 J    | j                   | j                  | j                  dS )zGets all routes offered by the plugin.

        This method is called by TensorBoard when retrieving all the
        routes offered by the plugin.

        Returns:
          A dictionary mapping URL path to route that handles it.
        )z/tagsz/meshesz/data)rJ   _serve_mesh_metadata_serve_mesh_datar   s    r   get_plugin_appszMeshPlugin.get_plugin_apps}   s)     %%00**
 	
r   c                      y)NF rN   s    r   	is_activezMeshPlugin.is_active   s    r   c                 .    t        j                  d      S )Nzmesh-dashboard)element_name)r   FrontendMetadatarN   s    r   frontend_metadatazMeshPlugin.frontend_metadata   s    ++9IJJr   c                 <    |j                   |   j                         S )z0Returns a single sample from a batch of samples.)numpytolist)r   tensor_datumsamples      r   _get_samplezMeshPlugin._get_sample   s    !!&)0022r   c                 N    |j                   |j                  |||t        |      dS )aL  Converts a TensorDatum into a JSON-compatible response.

        Args:
          event: TensorDatum object containing data in proto format.
          content_type: enum plugin_data_pb2.MeshPluginData.ContentType value,
            representing content type in TensorDatum.
          components: Bitmask representing all parts (vertices, colors, etc.) that
            belong to the summary.
          data_shape: list of dimensions sizes of the tensor.
          config: rendering scene configuration as dictionary.

        Returns:
          Dictionary of transformed metadata.
        )	wall_timestepcontent_type
componentsconfig
data_shape)r^   r_   list)r   eventr`   ra   rc   rb   s         r   _get_tensor_metadatazMeshPlugin._get_tensor_metadata   s-    $ JJ($z*
 	
r   c                 *    | j                  ||      }|S )z6Convert a TensorDatum into a JSON-compatible response.)r\   )r   re   r[   datas       r   _get_tensor_datazMeshPlugin._get_tensor_data   s    v.r   Nc           
         t        j                  |j                        }t        j                  |j                        }|j                  j                  d      }|j                  j                  d      }g }| j                  ||||      D ]  }| j                  j                  ||t        j                  t        j                  |g|g      | j                        |   |   }	| j                  ||||      }
||	D cg c]  }|
|f c}z  } |$|D cg c]  }|d   j                  |k(  s| }}|S t!        |d       }|S c c}w c c}w )z0Collects list of tensor events based on request.r-   r7   r    )r$   r   r%   
downsample   c                      | d   j                   S )Nrl   )r_   )tensor_datas    r   <lambda>z3MeshPlugin._collect_tensor_events.<locals>.<lambda>   s    {1~7J7J r   )key)r	   r   r>   r$   argsr   r:   r   read_tensorsr   r'   r   r(   r   r1   r_   sorted)r   rD   r_   r+   r,   r-   r7   tensor_eventsr.   tensorsrI   tensorre   s                r   _collect_tensor_eventsz!MeshPlugin._collect_tensor_events   sm   !!'//2 ..w?
llu%llu% //ZcJ 	DL))66($00'44l^   .. 7    !G ..ZlD 7CtVnCCM	D #0E!HMMT4IM  	 ##JM  Ds   <EE	,E	c                    t        |j                  j                  dd            }| j                  ||      }|j                  j                  d      }	 t        j
                  j                  j                  |      }t        |j                  j                  dd            }|D cg c]&  \  }}|j                  |k(  r| j                  ||      ( }}}t        j
                  j                  t        j                   t        j
                  j"                  t        j$                  t        j
                  j&                  t        j(                  i|   }	t        j*                  ||		      }|j-                  d
      j/                         }t        j                  ||d      S # t        $ r t        j                  |ddd      cY S w xY wc c}}w )aU  A route that returns data for particular summary of specified type.

        Data can represent vertices coordinates, vertices indices in faces,
        vertices colors and so on. Each mesh may have different combination of
        abovementioned data and each type/part of mesh summary must be served as
        separate roundtrip to the server.

        Args:
          request: werkzeug.Request containing content_type as a name of enum
            plugin_data_pb2.MeshPluginData.ContentType.

        Returns:
          werkzeug.Response either float32 or int32 data in binary format.
        r_   g        r`   zBad content_typez
text/plaini  r[   r   )dtypearraybuffer)floatrq   r   rw   r   MeshPluginDataContentTypeValue
ValueErrorr   rC   intr`   ri   VERTEXnpfloat32FACEint32COLORuint8arrayreshapetobytes)
r   rD   r_   rt   r`   r[   rI   rv   rF   np_types
             r   rM   zMeshPlugin._serve_mesh_data   s     W\\%%fc2333GTB||''7	*99EEKKL W\\%%h23 !.
f  L0 !!&&1
 
 **112::**//**00"((
 	 88HG4##B'//1  (MBB-  	$$+\3 	
s   )F '+G!GGc                     | j                  |      }|D cg c]B  \  }}| j                  ||j                  |j                  |j                  |j
                        D }}}t        j                  ||d      S c c}}w )a]  A route that returns the mesh metadata associated with a tag.

        Metadata consists of wall time, type of elements in tensor, scene
        configuration and so on.

        Args:
          request: The werkzeug.Request object.

        Returns:
          A JSON list of mesh data associated with the run and tag
          combination.
        r=   )rw   rf   r`   ra   rB   json_configr   rC   )r   rD   rt   rI   rv   rF   s         r   rL   zMeshPlugin._serve_mesh_metadata	  s     33G< !.	
 f %%!!

  	
 	
   (4FGG	
s   AA8)N)__name__
__module____qualname____doc__r   r'   r   r   r1   r4   r:   r   RequestapplicationrJ   rO   rR   rV   r\   rf   ri   rw   rM   rL   rQ   r   r   r   r      s    :&&K
 7
 !!&H "&HP
"K3
4
!F !!,C ",C\ !!H "Hr   r   )r   rX   r   werkzeugr   tensorboard.backendr   tensorboard.datar   tensorboard.pluginsr   tensorboard.plugins.meshr   r   tensorboardr	   r   TBPluginr   rQ   r   r   <module>r      s>    -   ) % + - 4 # FH%% FHr   