1
1
export summary_iterator
2
2
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
+
3
25
"""
4
26
read_event(f::IOStream) => Event
5
27
@@ -13,7 +35,9 @@ function read_event(f::IOStream)
13
35
14
36
# check
15
37
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
17
41
18
42
# read data
19
43
data_len = first (reinterpret (Int64, header))
@@ -42,12 +66,27 @@ struct TBEventFileCollectionIterator
42
66
43
67
purge:: Bool
44
68
end
45
- TBEventFileCollectionIterator (path; purge= true ) =
46
- TBEventFileCollectionIterator (path, sort (readdir (path)), purge)
47
69
48
70
TBEventFileCollectionIterator (logger:: TBLogger ; purge= true ) =
49
71
TBEventFileCollectionIterator (logdir (logger), purge= true )
50
72
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
+
51
90
function Base. iterate (it:: TBEventFileCollectionIterator , state= 1 )
52
91
state > length (it. files) && return nothing
53
92
fstream = open (joinpath (it. dir, it. files[state]))
0 commit comments