11export summary_iterator
22
3+ """
4+ is_valid_event(f::IOStream) => Bool
5+
6+ Returns true if the stream points to a valid TensorBoard event, false overwise.
7+ This is accomplished by checeking the crc checksum on the header (first 8
8+ bytes) of the event.
9+ """
10+ function is_valid_event (f:: IOStream )
11+ eof (f) && return false
12+
13+ header = read (f, 8 )
14+ length (header) != 8 && return false
15+
16+ crc_header = read (f, 4 )
17+ length (crc_header) != 4 && return false
18+
19+ # check
20+ crc_header_ck = reinterpret (UInt8, UInt32[masked_crc32c (header)])
21+ return crc_header == crc_header_ck
22+ end
23+
24+
325"""
426 read_event(f::IOStream) => Event
527
@@ -13,7 +35,9 @@ function read_event(f::IOStream)
1335
1436 # check
1537 crc_header_ck = reinterpret (UInt8, UInt32[masked_crc32c (header)])
16- @assert crc_header == crc_header_ck
38+ if crc_header != crc_header_ck
39+ error (" Invalid event checksum for stream" , f)
40+ end
1741
1842 # read data
1943 data_len = first (reinterpret (Int64, header))
@@ -42,12 +66,27 @@ struct TBEventFileCollectionIterator
4266
4367 purge:: Bool
4468end
45- TBEventFileCollectionIterator (path; purge= true ) =
46- TBEventFileCollectionIterator (path, sort (readdir (path)), purge)
4769
4870TBEventFileCollectionIterator (logger:: TBLogger ; purge= true ) =
4971 TBEventFileCollectionIterator (logdir (logger), purge= true )
5072
73+ function TBEventFileCollectionIterator (path; purge= true )
74+ fnames = sort (readdir (path))
75+ good_fnames = typeof (fnames)()
76+
77+ # Only consider files whose first event file would be valid.
78+ # So if there are other files in this folder, we ignore them.
79+ for fname in fnames
80+ open (joinpath (path,fname), " r" ) do f
81+ is_valid_event (f) && push! (good_fnames, fname)
82+ end
83+ end
84+
85+ @debug " Valid TensorBoard event files" good_fnames
86+
87+ TBEventFileCollectionIterator (path, good_fnames, purge)
88+ end
89+
5190function Base. iterate (it:: TBEventFileCollectionIterator , state= 1 )
5291 state > length (it. files) && return nothing
5392 fstream = open (joinpath (it. dir, it. files[state]))
0 commit comments