
    1Vh)                         d Z ddlZddlZddlZddlZddlZddlmZ ddlm	Z	 ddl
mZ  G d d      Z ed      Z G d d	      Z G d
 d      Z G d dej                         Zy)z"Writes events to disk in a logdir.    N)tf)	event_pb2)RecordWriterc                       e Zd Zd Zd Zy)AtomicCounterc                 D    || _         t        j                         | _        y N)_value	threadingLock_lock)selfinitial_values     \/home/dcms/DCMS/lib/python3.12/site-packages/tensorboard/summary/writer/event_file_writer.py__init__zAtomicCounter.__init__   s    #^^%
    c                     | j                   5  	 | j                  | xj                  dz  c_        cd d d        S # | xj                  dz  c_        w xY w# 1 sw Y   y xY w)N   )r   r
   r   s    r   getzAtomicCounter.get"   sO    ZZ 	!!{{q 		! 	! q 		! 	!s   A9AAAAN)__name__
__module____qualname__r   r    r   r   r   r      s    &!r   r   c                   2    e Zd ZdZ	 ddZd Zd Zd Zd Zy)	EventFileWritera&  Writes `Event` protocol buffers to an event file.

    The `EventFileWriter` class creates an event file in the specified
    directory, and asynchronously writes Event protocol buffers to the
    file. The Event file is encoded using the tfrecord format, which is
    similar to RecordIO.
    c           	         || _         t        j                  j                  j	                  |       t
        j                  j                  |dt        j                         t        j                         t        j                         t        j                         fz        |z   | _        t        j                  j                  j                  | j                  d      | _        t#        t%        | j                         ||      | _        t)        j*                  t        j                         dt)        j,                  d            }| j/                  |       | j1                          y)a  Creates a `EventFileWriter` and an event file to write to.

        On construction the summary writer creates a new event file in `logdir`.
        This event file will contain `Event` protocol buffers, which are written to
        disk via the add_event method.
        The other arguments to the constructor control the asynchronous writes to
        the event file:

        Args:
          logdir: A string. Directory where event file will be written.
          max_queue_size: Integer. Size of the queue for pending events and summaries.
          flush_secs: Number. How often, in seconds, to flush the
            pending events and summaries to disk.
        z"events.out.tfevents.%010d.%s.%s.%swbzbrain.Event:2z,tensorboard.summary.writer.event_file_writer)writer)	wall_timefile_versionsource_metadataN)_logdirr   iogfilemakedirsospathjointimesocketgethostnamegetpid_global_uidr   
_file_nameGFile_general_file_writer_AsyncWriterr   _async_writerr   EventSourceMetadata	add_eventflush)r   logdirmax_queue_size
flush_secsfilename_suffix_events         r   r   zEventFileWriter.__init__6   s    " 
V$GGLL4IIK&&(IIKOO%		 
 	 %'EEKK$5$5doot$L!)223^Z

 iik(%44E
 	v

r   c                     | j                   S )z7Returns the directory where event file will be written.)r#   r   s    r   
get_logdirzEventFileWriter.get_logdirf   s    ||r   c                     t        |t        j                        st        dt	        |      z        | j
                  j                  |j                                y)zeAdds an event to the event file.

        Args:
          event: An `Event` protocol buffer.
        z.Expected an event_pb2.Event proto,  but got %sN)
isinstancer   r4   	TypeErrortyper3   writeSerializeToString)r   events     r   r6   zEventFileWriter.add_eventj   sO     %1 $U,  	  !8!8!:;r   c                 8    | j                   j                          y)zFlushes the event file to disk.

        Call this method to make sure that all pending events have been
        written to disk.
        N)r3   r7   r   s    r   r7   zEventFileWriter.flushw   s     	  "r   c                 8    | j                   j                          y)zPerforms a final flush of the event file to disk, stops the
        write/flush worker and closes the file.

        Call this method when you do not need the summary writer
        anymore.
        N)r3   closer   s    r   rH   zEventFileWriter.close   s     	  "r   N)
   x    )	r   r   r   __doc__r   r>   r6   r7   rH   r   r   r   r   r   -   s(     JL.`<##r   r   c                   0    e Zd ZdZddZd Zd Zd Zd Zy)	r2   zWrites bytes to a file.c                    || _         d| _        t        j                  |      | _        t        | j                  | j                   |      | _        t        j                         | _	        | j                  j                          y)a@  Writes bytes to a file asynchronously. An instance of this class
        holds a queue to keep the incoming data temporarily. Data passed to the
        `write` function will be put to the queue and the function returns
        immediately. This class also maintains a thread to write data in the
        queue to disk. The first initialization parameter is an instance of
        `tensorboard.summary.record_writer` which computes the CRC checksum and
        then write the combined result to the disk. So we use an async approach
        to improve performance.

        Args:
            record_writer: A RecordWriter instance
            max_queue_size: Integer. Size of the queue for pending bytestrings.
            flush_secs: Number. How often, in seconds, to flush the
                pending bytestrings to disk.
        FN)_writer_closedqueueQueue_byte_queue_AsyncWriterThread_workerr   r   r   start)r   record_writerr9   r:   s       r   r   z_AsyncWriter.__init__   s`      % ;;~6)dllJ
 ^^%
r   c                     | j                   5  | j                          | j                  rt        d      | j                  j                  |       | j                          ddd       y# 1 sw Y   yxY w)z4Enqueue the given bytes to be written asychronously.Writer is closedN)r   _check_worker_statusrP   IOErrorrS   put)r   
bytestrings     r   rC   z_AsyncWriter.write   sa    ZZ 	( %%'||011  , %%'	( 	( 	(s   AA))A2c                    | j                   5  | j                          | j                  rt        d      | j                  j                          | j                  j                          | j                          ddd       y# 1 sw Y   yxY w)zWrite all the enqueued bytestring before this flush call to disk.

        Block until all the above bytestring are written.
        rY   N)r   rZ   rP   r[   rS   r)   rO   r7   r   s    r   r7   z_AsyncWriter.flush   so    
 ZZ 		(%%'||011!!#LL  %%'		( 		( 		(s   A,BBc                 "   | j                   sw| j                  5  | j                   sUd| _         | j                  j                          | j                  j                          | j                  j                          ddd       yy# 1 sw Y   yxY w)z@Closes the underlying writer, flushing any pending writes first.TN)rP   r   rU   stoprO   r7   rH   r   s    r   rH   z_AsyncWriter.close   sm    || )||#'DLLL%%'LL&&(LL&&() ) ) )s   A"BBc                 8    | j                   j                  }||y)zzMakes sure the worker thread is still running and raises exception
        thrown in the worker thread otherwise.
        N)rU   	exception)r   rb   s     r   rZ   z!_AsyncWriter._check_worker_status   s#     LL**	 O !r   N)   rJ   )	r   r   r   rL   r   rC   r7   rH   rZ   r   r   r   r2   r2      s    !2(( )r   r2   c                   (    e Zd ZdZd Zd Zd Zd Zy)rT   z;Thread that processes asynchronous writes for _AsyncWriter.c                     t         j                  j                  |        d| _        d| _        || _        || _        || _        d| _        d| _	        t               | _        y)a  Creates an _AsyncWriterThread.

        Args:
          queue: A Queue from which to dequeue data.
          record_writer: An instance of record_writer writer.
          flush_secs: How often, in seconds, to flush the
            pending file to disk.
        TNr   F)r   Threadr   daemonrb   _queue_record_writer_flush_secs_next_flush_time_has_pending_dataobject_shutdown_signal)r   rQ   rW   r:   s       r   r   z_AsyncWriterThread.__init__   sW     	!!$'+% !!& &r   c                 n    | j                   j                  | j                         | j                          y r	   )rh   r\   rn   r)   r   s    r   r`   z_AsyncWriterThread.stop   s!    --.		r   c                     	 | j                          y # t        $ r]}|| _        	 	 | j                  j	                  d       | j                  j                          6# t        j                  $ r Y  w xY wd }~ww xY w)NF)_run	Exceptionrb   rh   r   	task_donerQ   Empty)r   exs     r   runz_AsyncWriterThread.run   sp    	IIK 	DN KKOOE*KK))+  ;; 	s1    	A9A47AA1.A40A11A44A9c                    	 t        j                          }| j                  |z
  }d }	 |dkD  r| j                  j                  d|      }n| j                  j                  d      }|| j                  u r	 |r| j                  j                          y y | j                  j                  |       d| _        |r| j                  j                          	 t        j                          }|| j                  kD  rA| j                  r!| j                  j                          d| _        || j                  z   | _        5# t        j                  $ r Y w xY w# |r| j                  j                          w w xY w)NTr   F)r*   rk   rh   r   rn   rs   ri   rC   rl   rQ   rt   r7   rj   )r   nowqueue_wait_durationdatas       r   rq   z_AsyncWriterThread._run  sC   
 ))+C"&"7"7#"=D,&*;;??41DED;;??51D4000 KK))+  ##))$/)-& KK))+))+CT***)) ''--/-2D*(+d.>.>(>%;  ;;  KK))+ s*   AD7 "D7 7E
E EE E/N)r   r   r   rL   r   r`   rv   rq   r   r   r   rT   rT      s    E)( "?r   rT   )rL   r'   rQ   r+   r   r*   tensorboard.compatr   tensorboard.compat.protor   (tensorboard.summary.writer.record_writerr   r   r.   r   r2   rf   rT   r   r   r   <module>r~      sh    ) 
     ! . A
! 
! AY# Y#xK K\M?)) M?r   