Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions src/Loggers/LogText.jl
Original file line number Diff line number Diff line change
@@ -1,23 +1,46 @@
"""
log_text(logger, name, text, step)
log_text(logger::TBLogger, name::String, text::Any, step = nothing)

Logs text with name `name` at step `step`
- text: If text is a 2-D or 3-D `Array`, it will be rendered as a table or a list. Any other data will be represented as string
"""
function log_text(logger::TBLogger, name::String, text::Any; step=nothing)
function log_text(logger::TBLogger, name::String, text::Any; step = nothing)
summ = SummaryCollection()
push!(summ.value, text_summary(name, text))
write_event(logger.file, make_event(logger, summ, step=step))
end

function text_summary(name::String, text::Any)
text = markdown_repr(text)
textstringval = [Vector{UInt8}(text)]
texttensorshape = TensorShapeProto(dim = Vector([TensorShapeProto_Dim(size = 1)]))
#Create a string tensor
#shape of the tensor
dims = Array{TensorShapeProto_Dim, 1}()
if isa(text, AbstractArray)
for dim in size(text)
push!(dims, TensorShapeProto_Dim(size = dim))
end
else
push!(dims, TensorShapeProto_Dim(size = 1))
end
texttensorshape = TensorShapeProto(dim = Vector(dims))

#content of the tensor
textstringval = Vector{Array{UInt8, 1}}()
if isa(text, AbstractArray)
for string in text
string = markdown_repr(string)
push!(textstringval, Array{UInt8, 1}(string))
end
else
text = markdown_repr(text)
push!(textstringval, Array{UInt8, 1}(text))
end
#metadata for the text
textcontent = serialize_proto(TextPluginData(version = 0))
plugindata = SummaryMetadata_PluginData(plugin_name = "text", content = textcontent)
smd = SummaryMetadata(plugin_data = plugindata)
#create tensor
texttensor = TensorProto(dtype = _DataType.DT_STRING, string_val = textstringval, tensor_shape = texttensorshape)
return Summary_Value(tag = name, metadata = smd, tensor = texttensor)
Summary_Value(tag = name, metadata = smd, tensor = texttensor)
end

"""
Expand All @@ -39,4 +62,3 @@ function markdown_repr(x)
end
return repr(x)
end

2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ end
log_text(logger, "markdown", "**This** is the *power* of >>>markdown", step = step)
log_text(logger, "html", "<p> HTML is a programming language</p>", step = step)
log_text(logger, "docstring", """This should work too""", step = step)
log_text(logger, "Array", collect(1:10), step = step)
log_text(logger, "Matrix", rand(4, 4), step = step)
end

@testset "Text processing interface" begin
Expand Down