
    h                         S r SSKrSSKrSSKrSSKrSSKrSSKrSSKJr  SSK	J
r
Jr   " S S5      r " S S	5      r " S
 S\R                  5      rg)z"Writes events to disk in a logdir.    N   )	event_pb2)RecordWriterdirectory_checkc                   :    \ rS rSrSrS
S jrS rS rS rS r	Sr
g	)EventsWriter   z1Writes `Event` protocol buffers to an event file.c                    US-   [        [        R                  " 5       5      SS -   S-   [        R                  " 5       -   U-   U l        SU l        [        U R                  5      U l        [        R                  " 5       U l
        [        R                  " 5       U R                  l        SU R                  l        [        R                  " 5       U l        U R!                  U R                  5        g)zc
Events files have a name of the form
'/some/file/path/events.out.tfevents.[timestamp].[hostname]'
z.out.tfevents.N
   .r   zbrain.Event:2)strtimesocketgethostname
_file_name_num_outstanding_eventsr   _py_recordio_writerr   Event_event	wall_timefile_version	threadingLock_lockwrite_event)selffile_prefixfilename_suffixs      X/var/www/fran/franai/venv/lib/python3.13/site-packages/tensorboardX/event_file_writer.py__init__EventsWriter.__init__    s    
 &(883tyy{;KCR;PPSVV !#23'($#/#@ oo' $		#2 ^^%
%    c                     [        U[        R                  5      (       d  [        S[	        U5       35      eU R                  UR                  5       5      $ )zAppend "event" to the file.z,Expected an event_pb2.Event proto,  but got )
isinstancer   r   	TypeErrortype_write_serialized_eventSerializeToStringr   events     r   r   EventsWriter.write_event0   sO     %11 ((,U}6 7 7++E,C,C,EFFr"   c                     U R                      U =R                  S-  sl        U R                  R                  U5        S S S 5        g ! , (       d  f       g = f)Nr   )r   r   r   write)r   	event_strs     r   r'   $EventsWriter._write_serialized_event9   s8    ZZ((A-($$**95 ZZs   1A
Ac                     U R                      SU l        U R                  R                  5         SSS5        g! , (       d  f       g= f)zFlushes the event file to disk.r   NT)r   r   r   flushr   s    r   r1   EventsWriter.flush>   s:    ZZ+,D($$**,   Z s	   "8
Ac                     U R                  5       nU R                     U R                  R                  5         SSS5        U$ ! , (       d  f       U$ = f)zCall self.flush().N)r1   r   r   close)r   return_values     r   r5   EventsWriter.closeE   s?    zz|ZZ$$**,  Zs   A
A)r   r   r   r   r   N) )__name__
__module____qualname____firstlineno____doc__r    r   r'   r1   r5   __static_attributes__ r"   r   r   r      s    ;& G6
r"   r   c                   @    \ rS rSrSrSS jrS rS rS rS r	S r
S	rg
)EventFileWriterM   a  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                 |   Xl         [        U R                   5        [        R                  " U5      U l        [        [        R                  R                  U R                   S5      U5      U l	        X0l
        SU l        [        U R                  U R                  U5      U l        U R                  R                  5         g)aD  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.
eventsFN)_logdirr   multiprocessingQueue_event_queuer   ospathjoin
_ev_writer_flush_secs_closed_EventLoggerThread_workerstart)r   logdirmax_queue_size
flush_secsr   s        r   r    EventFileWriter.__init__U   s     %+11.A&rww||LL(($%46%)$*;*;T__*46 	r"   c                     U R                   $ )z7Returns the directory where event file will be written.)rE   r2   s    r   
get_logdirEventFileWriter.get_logdirp   s    ||r"   c                     U R                   (       aR  SU l         [        U R                  U R                  U R                  5      U l        U R
                  R                  5         gg)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 and a new write/flush worker
is created. Does nothing if the EventFileWriter was not closed.
FN)rN   rO   rH   rL   rM   rP   rQ   r2   s    r   reopenEventFileWriter.reopent   sL     << DL-!!4??D4D4DDL LL  r"   c                 ^    U R                   (       d  U R                  R                  U5        gg)zMAdds an event to the event file.

Args:
  event: An `Event` protocol buffer.
N)rN   rH   putr)   s     r   	add_eventEventFileWriter.add_event   s$     ||!!%( r"   c                 \    U R                   (       d  U R                  R                  5         gg)zrFlushes the event file to disk.

Call this method to make sure that all pending events have been written to
disk.
N)rN   rL   r1   r2   s    r   r1   EventFileWriter.flush   s      ||OO!!# r"   c                    U R                   (       dt  U R                  5         U R                  R                  5         U R                  R                  5         U R                  R                  5         SU l        SU l        SU l         gg)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.
NT)rN   r1   rP   stoprL   r5   rH   r2   s    r   r5   EventFileWriter.close   sa    
 ||JJLLLOO!!###% $DDLDL r"   )rN   rL   rH   rM   rE   rP   N)r   x   r8   )r9   r:   r;   r<   r=   r    rW   rZ   r^   r1   r5   r>   r?   r"   r   rA   rA   M   s%    6!)$ r"   rA   c                   *    \ rS rSrSrS rS rS rSrg)rO      zThread that logs events.c                     [         R                  R                  U 5        SU l        Xl        X l        X0l        SU l        SU l        [        5       U l
        g)zCreates an _EventLoggerThread.
Args:
  queue: A Queue from which to dequeue data.
  record_writer: An data writer. Used to log brain events for
   the visualizer.
  flush_secs: How often, in seconds, to flush the
    pending file to disk.
Tr   FN)r   Threadr    daemon_queue_record_writerrM   _next_flush_time_has_pending_dataobject_shutdown_signal)r   queuerecord_writerrT   s       r   r    _EventLoggerThread.__init__   sJ     	!!$'+% !!& &r"   c                 n    U R                   R                  U R                  5        U R                  5         g )N)rk   r]   rp   rK   r2   s    r   rc   _EventLoggerThread.stop   s!    --.		r"   c                 d    [         R                   " 5       nU R                  U-
  nS n US:  a  U R                  R                  SU5      nOU R                  R                  S5      n[	        U5      [	        U R
                  5      :X  a  g U R                  R                  U5        SU l        [         R                   " 5       nXR                  :  aE  U R                  (       a!  U R                  R                  5         SU l        XR                  -   U l        GM  ! [        R                   a     Nf = f)NTr   F)r   rm   rk   getr&   rp   rl   r   rn   rq   Emptyr1   rM   )r   nowqueue_wait_durationdatas       r   run_EventLoggerThread.run   s    
 ))+C"&"7"7#"=D&*;;??41DED;;??51D:d&;&;!<<##//5)-& ))+C***)) ''--/-2D*(+.>.>(>%5  ;; s   A D 
"D D/.D/)rM   rn   rm   rk   rl   rp   rj   N)	r9   r:   r;   r<   r=   r    rc   r|   r>   r?   r"   r   rO   rO      s    ")&?r"   rO   )r=   rF   rI   rq   r   r   r   protor   rr   r   r   r   rA   ri   rO   r?   r"   r   <module>r      sN    )  	      8- -`R  R j9?)) 9?r"   