Skip to content

Commit 483bb2e

Browse files
authored
Ignore non-event files in deserialized folder (#89)
* Ignore non-event files in deserialized folder * add test for #88 * bump version * fix header
1 parent 95c2a6f commit 483bb2e

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "TensorBoardLogger"
22
uuid = "899adc3e-224a-11e9-021f-63837185c80f"
33
authors = ["Filippo Vicentini <filippovicentini@gmail.com>"]
4-
version = "0.1.13"
4+
version = "0.1.14"
55

66
[deps]
77
CRC32c = "8bf52ea8-c179-5cab-976a-9e18b702a9bc"

src/Deserialization/deserialization.jl

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
export 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
4468
end
45-
TBEventFileCollectionIterator(path; purge=true) =
46-
TBEventFileCollectionIterator(path, sort(readdir(path)), purge)
4769

4870
TBEventFileCollectionIterator(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+
5190
function Base.iterate(it::TBEventFileCollectionIterator, state=1)
5291
state > length(it.files) && return nothing
5392
fstream = open(joinpath(it.dir, it.files[state]))

test/deserialization.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ ENV["GKSwstype"] = "100"
1717
end
1818
end
1919

20+
# Issue #88: ignore non -tensorboard files
21+
dir = TensorBoardLogger.logdir(logger)
22+
f = open(joinpath(dir, "mockdata"), "w")
23+
write(f, "baddta")
24+
close(f)
25+
2026
tgs = TensorBoardLogger.tags(logger)
2127
@test "test/val" tgs
2228
@test "test/b" tgs
@@ -29,4 +35,5 @@ ENV["GKSwstype"] = "100"
2935
@test all(hist["test/val"].values .== (1-0.5*im).* is)
3036
@test all(hist["test/b"].values == is .* 2)
3137
@test all([v == mri for v=hist["test2/mri"].values])
38+
3239
end

0 commit comments

Comments
 (0)