Skip to content

Commit

Permalink
rename set_step to set_step! and increment_step to increment_step!. R…
Browse files Browse the repository at this point in the history
…emove old (and unused) methods. Define a reset! method for TBLogger
  • Loading branch information
PhilipVinc committed Apr 30, 2019
1 parent fae4efd commit e8a8662
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 50 deletions.
68 changes: 34 additions & 34 deletions src/TBLogger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,23 +124,18 @@ end

# Accessors
"""
logdir(lg::Logger) -> String
logdir(lg::TBLogger) -> String
Returns the directory to which Logger `lg` is writing data.
Returns the directory where `lg` is writing events data.
"""
logdir(lg::TBLogger) = lg.logdir

"""
get_file(lg::Logger) -> IOS
get_file(lg::TBLogger) -> IOS
Returns the main `file` IOStream object of Logger `lg`.
"""
get_file(lg::TBLogger) = lg.file
function add_log_file(lg::TBLogger, path::String)
file = open(path, "w")
lg.all_files[path] = file
return file
end

"""
get_file(lg, tags::String...) -> IOS
Expand All @@ -149,50 +144,55 @@ Returns the `file` IOStream object of Logger `lg` writing to the tag
`tags1/tags2.../tagsN`.
"""
function get_file(lg::TBLogger, tags::String...)
key = joinpath(logdir(lg), tags...)
if key lg.all_files
key = joinpath(tags...)
if key keys(lg.all_files)
return lg.all_files[key]
else
return add_log_file(lg, key)
return add_eventfile(lg, key)
end
end

"""
set_step(lg, iter::Int)
set_step!(lg, step) -> Int
Sets the iteration counter in the logger to `iter`. This counter is used by the
Sets the iteration counter in the logger to `step`. This counter is used by the
logger when no value is passed by the user.
"""
set_step(lg::TBLogger, iter::Int) = lg.global_step = iter

increment_step(lg::TBLogger, iter::Int) = lg.global_step += iter
set_step!(lg::TBLogger, step) = lg.global_step = step

"""
step(lg)
increment_step!(lg, Δ_Step) -> Int
Returns the internal iteration counter of the logger. When no step keyword
is provided to the loggers, it will use this value.
Increments the step counter in the logger by `Δ_Step` and returns the new value.
"""
step(lg::TBLogger) = lg.global_step


# Additional things
increment_step!(lg::TBLogger, Δ_Step) = lg.global_step += Δ_Step

"""
set_tb_logdir(logdir, overwrite=false)
Start a new log in the given directory
step(lg) -> Int
Returns the internal step counter of the logger.
"""
function set_tb_logdir(logdir, overwrite=false)
default_logging_session[] = Logger(logdir, overwrite=overwrite)
end
step(lg::TBLogger) = lg.global_step

"""
reset_tb_logs()
Reset the current log, deleteing all information
reset!(lg)
Reset the TBLogger `lg`, deleting everything in it's log directory.
"""
function reset_tb_logs()
logdir = default_logging_session[].logdir
default_logging_session[] = Logger(logdir, overwrite=true)
function reset!(lg::TBLogger)
# close open streams
for k=keys(lg.all_files)
close(lg.all_files[k])
delete!(lg.all_files, k)
end

# Overwrite the logdirectoy and create a new base event file
init_logdir(logdir(lg), tb_overwrite)
fname = add_eventfile(lg, "")
lg.file = get_file(lg, fname)
set_step!(lg, 0)

return lg
end


Expand Down Expand Up @@ -229,6 +229,6 @@ function handle_message(lg::TBLogger, level, message, _module, group, id, file,
push!(summ.value, summary_impl(name, val))
end
end
iter = increment_step(lg, i_step)
iter = increment_step!(lg, i_step)
write_event(lg.file, make_event(lg, summ, step=iter))
end
3 changes: 2 additions & 1 deletion src/TensorBoardLogger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import Base.CoreLogging:
export log_histogram, log_value, log_vector, log_text
export scalar_summary, histogram_summary, text_summary, make_event
export TBLogger
export set_tb_logdir, reset_tb_logs, default_logging_session
export reset!, set_step!, increment_step!


# Protobuffer definitions for tensorboard
include("protojl/tensorflow.jl")
Expand Down
17 changes: 2 additions & 15 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,8 @@ using TensorBoardLogger, Logging
using TensorBoardLogger: preprocess, summary_impl
using Test

@testset "Initialization" begin
isdir("test_logs/") && rm("test_logs", force=true, recursive=true)

# Check tb_append
TBLogger("test_logs/run")
@test isdir("test_logs/run")
TBLogger("test_logs/run") #tb_append by default
@test isdir("test_logs/run_1")
TBLogger("test_logs/run", tb_increment)
@test isdir("test_logs/run_2")

# check tb_overwrite
close(open("test_logs/run_2/testfile", "w"))
TBLogger("test_logs/run_2", tb_overwrite)
@test !isfile("test_logs/run_2/testfile")
@testset "TBLogger" begin
include("test_TBLogger.jl")
end

@testset "Scalar Value Logger" begin
Expand Down
77 changes: 77 additions & 0 deletions test/test_TBLogger.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using TensorBoardLogger, Logging
using TensorBoardLogger: preprocess, summary_impl
using Test

@testset "Initialization" begin
isdir("test_logs/") && rm("test_logs", force=true, recursive=true)

# Check tb_append
TBLogger("test_logs/run")
@test isdir("test_logs/run")
TBLogger("test_logs/run") #tb_append by default
@test isdir("test_logs/run_1")
TBLogger("test_logs/run", tb_increment)
@test isdir("test_logs/run_2")

# check tb_overwrite
close(open("test_logs/run_2/testfile", "w"))
TBLogger("test_logs/run_2", tb_overwrite)
@test !isfile("test_logs/run_2/testfile")
end

@testset "create log directory" begin
import TensorBoardLogger.init_logdir
rm("test_logs/", recursive=true, force=true)

# Check tb_append
init_logdir("test_logs/run")
@test isdir("test_logs/run")
init_logdir("test_logs/run") #tb_append by default
@test isdir("test_logs/run_1")
init_logdir("test_logs/run", tb_increment)
@test isdir("test_logs/run_2")

# check tb_overwrite
close(open("test_logs/run_2/testfile", "w"))
init_logdir("test_logs/run_2", tb_overwrite)
@test !isfile("test_logs/run_2/testfile")
end

@testset "create event file" begin
import TensorBoardLogger.init_logdir, TensorBoardLogger.create_eventfile
rm("test_logs/", recursive=true, force=true)
logdir = init_logdir("test_logs/run")

fname, file = create_eventfile(logdir)
@test isfile(joinpath(logdir, fname))

fname, file = create_eventfile(logdir, prepend="test/")
@test isfile(joinpath(logdir, fname))
end

@testset "stepping" begin
import TensorBoardLogger.step

tbl = TBLogger("test_logs/run", tb_overwrite)
@test step(tbl) == 0

tbl = TBLogger("test_logs/run", purge_step=12)
@test step(tbl) == 12

@test increment_step!(tbl, 1) == 13
@test step(tbl) == 13

@test set_step!(tbl, 1) == 1
@test step(tbl) == 1
end

@testset "resetting" begin
tbl = TBLogger("test_logs/run", tb_overwrite)
TensorBoardLogger.add_eventfile(tbl, "pp")
set_step!(tbl, 100)

reset!(tbl)
@test TensorBoardLogger.step(tbl) == 0
@test length(tbl.all_files) == 1

end

0 comments on commit e8a8662

Please sign in to comment.