
    BVh(                         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 ddlmZ  G d d      Z G d	 d
ej                         Z G d d      Z G d de      Zy)z"Writes events to disk in a logdir.    N)_pywrap_events_writer)gfile)
tf_logging)compatc                   L    e Zd ZdZ	 	 ddZd Zd Zd Z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.

  This class is not thread-safe.
  Nc                 $   t        |      | _        t        j                  | j                         || _        || _        t        j                         | _        t               | _
        t               | _        t        j                  t        j                  t         j"                  j%                  | j                  d                  | _        |r.| j&                  j)                  t        j                  |             | j+                          d| _        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:

    *  `flush_secs`: How often, in seconds, to flush the added summaries
       and events to disk.
    *  `max_queue`: Maximum number of summaries or events pending to be
       written to disk before one of the 'add' calls block.

    Args:
      logdir: A string. Directory where event file will be written.
      max_queue: 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.
      filename_suffix: A string. Every event file's name is suffixed with
        `filename_suffix`.
    eventsFN)str_logdirr   MakeDirs
_max_queue_flush_secs	threadingEvent_flush_completeobject_flush_sentinel_close_sentinelr   EventsWriterr   as_bytesospathjoin
_ev_writerInitWithSuffix_initialize_closed)selflogdir	max_queue
flush_secsfilename_suffixs        b/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/summary/writer/event_file_writer.py__init__zEventFileWriter.__init__'   s    0 v;DL	NN4<< DO!D$??,D!8D!8D+88T\\8<=?DO
oo$$V___%EFDL    c                    t        | j                        | _        t        | j                  | j                  | j
                  | j                  | j                  | j                        | _	        | j                  j                          y)zInitializes or re-initializes the queue and writer thread.

    The EventsWriter itself does not need to be re-initialized explicitly,
    because it will auto-initialize itself if used after being closed.
    N)CloseableQueuer   _event_queue_EventLoggerThreadr   r   r   r   r   _workerstartr   s    r$   r   zEventFileWriter._initializeM   sd     't7D%d&7&7&*&6&68L8L&*&:&:&*&:&:<DL
 	LLr&   c                     | j                   S )z7Returns the directory where event file will be written.)r   r-   s    r$   
get_logdirzEventFileWriter.get_logdir[   s    <<r&   c                 L    | j                   r| j                          d| _         yy)zReopens the EventFileWriter.

    Can be called after `close()` to add more events in the same directory.
    The events will go into a new events file.

    Does nothing if the EventFileWriter was not closed.
    FN)r   r   r-   s    r$   reopenzEventFileWriter.reopen_   s#     ||
dl r&   c                 @    | j                   s| j                  |       yy)zYAdds an event to the event file.

    Args:
      event: An `Event` protocol buffer.
    N)r   _try_put)r   events     r$   	add_eventzEventFileWriter.add_eventk   s     <<
mmE r&   c                     	 | j                   j                  |       y# t        $ rF | j                          | j                  j
                  r| j                  j
                  \  }}}|dY yw xY w)zAttempts to enqueue an item to the event queue.

    If the queue is closed, this will close the EventFileWriter and reraise the
    exception that caused the queue closure, if one exists.

    Args:
      item: the item to enqueue
    N)r)   putQueueClosedError_internal_closer+   failure_exc_info)r   item_	exceptions       r$   r3   zEventFileWriter._try_putt   se    "
D! "
		&	&,,779aT! 
'"s    AA-,A-c                 @   | j                   s| j                  j                          | j                  | j                         | j                  j                          | j                  j                  r,| j                          | j                  j                  \  }}}|yy)z~Flushes the event file to disk.

    Call this method to make sure that all pending events have been written to
    disk.
    N)	r   r   clearr3   r   waitr+   r:   r9   )r   r<   r=   s      r$   flushzEventFileWriter.flush   s     <<   "
mmD(()
!		&	&,,779a 
' r&   c                     | j                   s<| j                          | j                  | j                         | j	                          yy)z~Flushes the event file to disk and close the file.

    Call this method when you do not need the summary writer anymore.
    N)r   rA   r3   r   r9   r-   s    r$   closezEventFileWriter.close   s7    
 <<
jjl
mmD(()
 r&   c                 z    d| _         | j                  j                          | j                  j	                          y )NT)r   r+   r   r   Closer-   s    r$   r9   zEventFileWriter._internal_close   s)    DLLLOOr&   )
   x   N)__name__
__module____qualname____doc__r%   r   r/   r1   r5   r3   rA   rC   r9    r&   r$   r   r      s=     7:#$L
"""r&   r   c                       e Zd ZdZd Zd Zy)r*   zThread that logs events.c                     t         j                  j                  | d       d| _        || _        || _        || _        d| _        || _        || _	        || _
        d| _        y)a?  Creates an _EventLoggerThread.

    Args:
      queue: A CloseableQueue from which to dequeue events. The queue will be
        closed just before the thread exits, whether due to `close_sentinel` or
        any exception raised in the writing loop.
      ev_writer: An event writer. Used to log brain events for
        the visualizer.
      flush_secs: How often, in seconds, to flush the
        pending file to disk.
      flush_complete: A threading.Event that will be set whenever a flush
        operation requested via `flush_sentinel` has been completed.
      flush_sentinel: A sentinel element in queue that tells this thread to
        flush the writer and mark the current flush operation complete.
      close_sentinel: A sentinel element in queue that tells this thread to
        terminate and close the queue.
    EventLoggerThread)nameTr   rL   N)r   Threadr%   daemon_queuer   r   _next_event_flush_timer   r   r   r:   )r   queue	ev_writerr"   flush_completeflush_sentinelclose_sentinels          r$   r%   z_EventLoggerThread.__init__   sc    & d)<=DKDKDO!D"#D)D)D)DDr&   c                    	 	 | j                   j                         }|| j                  u r6	 | j                  j	                          | j                   j                          y || j                  u r5| j                  j                          | j                  j	                          nl| j                  j                  |       t        j                         }|| j                  kD  r.| j                  j                          || j                  z   | _        # t        $ r5}t        j                  d|       t!        j"                         | _         d }~ww xY w# | j                  j	                          | j                   j                          w xY w)Nz'EventFileWriter writer thread error: %s)rS   getr   r   setrC   r   r   Flush
WriteEventtimerT   r   	Exceptionloggingerrorsysexc_infor:   )r   r4   nowes       r$   runz_EventLoggerThread.run   s+   !D(((
(  
kk) d***
//


!



"
"
$
//
$
$U
+		#4...OO!!#*-0@0@*@D'   mm=qA!llnd  
kks*   )D !B1D 	E0EEE 6F	N)rH   rI   rJ   rK   r%   rg   rL   r&   r$   r*   r*      s     @r&   r*   c                   *    e Zd ZdZddZd Zd Zd Zy)r(   zCStripped-down fork of the standard library Queue that is closeable.c                    || _         t        j                         | _        d| _        t        j                         | _        t        j                  | j                        | _	        t        j                  | j                        | _
        y)zCreate a queue object with a given maximum size.

    Args:
      maxsize: int size of queue. If <= 0, the queue size is infinite.
    FN)_maxsizecollectionsdequerS   r   r   Lock_mutex	Condition
_not_empty	_not_full)r   maxsizes     r$   r%   zCloseableQueue.__init__   s`     DM##%DKDL.."DK  ))$++6DO ((5DNr&   c                    | j                   5  | j                  s'| j                   j                          | j                  s'| j                  j                         }| j                  j                          |cddd       S # 1 sw Y   yxY w)zRemove and return an item from the queue.

    If the queue is empty, blocks until an item is available.

    Returns:
      an item from the queue
    N)rp   rS   r@   popleftrq   notifyr   r;   s     r$   r[   zCloseableQueue.get   se     
  [[  "d
nn  s   3B 5B  B	c                    | j                   5  | j                  r
t               | j                  dkD  rut	        | j
                        | j                  k(  rS| j                   j                          | j                  r
t               t	        | j
                        | j                  k(  rS| j
                  j                  |       | j                  j                          ddd       y# 1 sw Y   yxY w)aj  Put an item into the queue.

    If the queue is closed, fails immediately.

    If the queue is full, blocks until space is available or until the queue
    is closed by a call to close(), at which point this call fails.

    Args:
      item: an item to add to the queue

    Raises:
      QueueClosedError: if insertion failed because the queue is closed
    r   N)
rq   r   r8   rj   lenrS   r@   appendrp   ru   rv   s     r$   r7   zCloseableQueue.put
  s     
 		  		$++$--/
..


\\"$$ $++$--/ kk
oo	 	 	s   BC&(5C&&C/c                     | j                   5  d| _        | j                   j                          ddd       y# 1 sw Y   yxY w)zFCloses the queue, causing any pending or future `put()` calls to fail.TN)rq   r   
notify_allr-   s    r$   rC   zCloseableQueue.close#  s5    	 "dl
nn!" " "s	   "8AN)r   )rH   rI   rJ   rK   r%   r[   r7   rC   rL   r&   r$   r(   r(      s    K6$2"r&   r(   c                       e Zd ZdZy)r8   zCRaised when CloseableQueue.put() fails because the queue is closed.N)rH   rI   rJ   rK   rL   r&   r$   r8   r8   *  s    Kr&   r8   )rK   rk   os.pathr   rc   r   r_   tensorflow.python.clientr   tensorflow.python.platformr   r   ra   tensorflow.python.utilr   r   rQ   r*   r(   r`   r8   rL   r&   r$   <module>r      s`    )   
   : , < )F FR=)) =@A" A"HLy Lr&   