Skip to content

Commit 829be8a

Browse files
authored
Improve show function (#236)
* Fix indentation problem * Add tests compact false plain/text
1 parent b905906 commit 829be8a

File tree

2 files changed

+37
-24
lines changed

2 files changed

+37
-24
lines changed

src/GNNGraphs/gnngraph.jl

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -267,30 +267,31 @@ function Base.show(io::IO, ::MIME"text/plain", g::GNNGraph)
267267
print(io, "GNNGraph($(g.num_nodes), $(g.num_edges)) with ")
268268
print_all_features(io, g.ndata, g.edata, g.gdata)
269269
print(io, " data")
270-
else # if the following block is indented the printing is ruined
271-
print(io, "GNNGraph:
272-
num_nodes: $(g.num_nodes)
273-
num_edges: $(g.num_edges)")
274-
g.num_graphs > 1 && print(io, "\n num_graphs = $(g.num_graphs)")
275-
if !isempty(g.ndata)
276-
print(io, "\n ndata:")
277-
for k in keys(g.ndata)
278-
print(io, "\n $k = $(shortsummary(g.ndata[k]))")
279-
end
280-
end
281-
if !isempty(g.edata)
282-
print(io, "\n edata:")
283-
for k in keys(g.edata)
284-
print(io, "\n $k = $(shortsummary(g.edata[k]))")
285-
end
286-
end
287-
if !isempty(g.gdata)
288-
print(io, "\n gdata:")
289-
for k in keys(g.gdata)
290-
print(io, "\n $k = $(shortsummary(g.gdata[k]))")
291-
end
292-
end
293-
end #else
270+
else
271+
print(
272+
io,
273+
"GNNGraph:\n num_nodes: $(g.num_nodes)\n num_edges: $(g.num_edges)"
274+
)
275+
g.num_graphs > 1 && print(io, "\n num_graphs: $(g.num_graphs)")
276+
if !isempty(g.ndata)
277+
print(io, "\n ndata:")
278+
for k in keys(g.ndata)
279+
print(io, "\n\t$k = $(shortsummary(g.ndata[k]))")
280+
end
281+
end
282+
if !isempty(g.edata)
283+
print(io, "\n edata:")
284+
for k in keys(g.edata)
285+
print(io, "\n\t$k = $(shortsummary(g.edata[k]))")
286+
end
287+
end
288+
if !isempty(g.gdata)
289+
print(io, "\n gdata:")
290+
for k in keys(g.gdata)
291+
print(io, "\n\t$k = $(shortsummary(g.gdata[k]))")
292+
end
293+
end
294+
end
294295
end
295296

296297
MLUtils.numobs(g::GNNGraph) = g.num_graphs

test/GNNGraphs/gnngraph.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,4 +363,16 @@
363363
@test sprint(show, MIME("text/plain"), rand_graph(10, 20, ndata=(a=rand(5, 10), b=rand(3, 10)), edata=rand(2, 20)); context=:compact => true) == "GNNGraph(10, 20) with (a: 5×10, b: 3×10), e: 2×20 data"
364364
@test sprint(show, MIME("text/plain"), rand_graph(10, 20, ndata=(a=rand(5,5, 10), b=rand(3,2, 10)), edata=rand(2, 20)); context=:compact => true) == "GNNGraph(10, 20) with (a: 5×5×10, b: 3×2×10), e: 2×20 data"
365365
end
366+
367+
@testset "show plain/text compact false" begin
368+
@test sprint(show, MIME("text/plain"), rand_graph(10, 20); context=:compact => false) == "GNNGraph:\n num_nodes: 10\n num_edges: 20"
369+
@test sprint(show, MIME("text/plain"), rand_graph(10, 20, ndata=rand(5, 10)); context=:compact => false) == "GNNGraph:\n num_nodes: 10\n num_edges: 20\n ndata:\n\tx = 5×10 Matrix{Float64}"
370+
@test sprint(show, MIME("text/plain"), rand_graph(10, 20, ndata=(a=rand(5, 10), b=rand(3, 10)), edata=rand(2, 20), gdata=(q=rand(1, 1), p=rand(3, 1))); context=:compact => false) == "GNNGraph:\n num_nodes: 10\n num_edges: 20\n ndata:\n\ta = 5×10 Matrix{Float64}\n\tb = 3×10 Matrix{Float64}\n edata:\n\te = 2×20 Matrix{Float64}\n gdata:\n\tq = 1×1 Matrix{Float64}\n\tp = 3×1 Matrix{Float64}"
371+
@test sprint(show, MIME("text/plain"), rand_graph(10, 20, ndata=(a=rand(5, 10),)); context=:compact => false) == "GNNGraph:\n num_nodes: 10\n num_edges: 20\n ndata:\n\ta = 5×10 Matrix{Float64}"
372+
@test sprint(show, MIME("text/plain"), rand_graph(10, 20, ndata=rand(5, 10), edata=rand(2, 20)); context=:compact => false) == "GNNGraph:\n num_nodes: 10\n num_edges: 20\n ndata:\n\tx = 5×10 Matrix{Float64}\n edata:\n\te = 2×20 Matrix{Float64}"
373+
@test sprint(show, MIME("text/plain"), rand_graph(10, 20, ndata=rand(5, 10), gdata=rand(1, 1)); context=:compact => false) == "GNNGraph:\n num_nodes: 10\n num_edges: 20\n ndata:\n\tx = 5×10 Matrix{Float64}\n gdata:\n\tu = 1×1 Matrix{Float64}"
374+
@test sprint(show, MIME("text/plain"), rand_graph(10, 20, ndata=rand(5, 10), edata=(e=rand(2, 20), f=rand(2, 20), h=rand(3, 20)), gdata=rand(1, 1)); context=:compact => false) == "GNNGraph:\n num_nodes: 10\n num_edges: 20\n ndata:\n\tx = 5×10 Matrix{Float64}\n edata:\n\te = 2×20 Matrix{Float64}\n\tf = 2×20 Matrix{Float64}\n\th = 3×20 Matrix{Float64}\n gdata:\n\tu = 1×1 Matrix{Float64}"
375+
@test sprint(show, MIME("text/plain"), rand_graph(10, 20, ndata=(a=rand(5, 10), b=rand(3, 10)), edata=rand(2, 20)); context=:compact => false) == "GNNGraph:\n num_nodes: 10\n num_edges: 20\n ndata:\n\ta = 5×10 Matrix{Float64}\n\tb = 3×10 Matrix{Float64}\n edata:\n\te = 2×20 Matrix{Float64}"
376+
@test sprint(show, MIME("text/plain"), rand_graph(10, 20, ndata=(a=rand(5, 5, 10), b=rand(3, 2, 10)), edata=rand(2, 20)); context=:compact => false) == "GNNGraph:\n num_nodes: 10\n num_edges: 20\n ndata:\n\ta = 5×5×10 Array{Float64, 3}\n\tb = 3×2×10 Array{Float64, 3}\n edata:\n\te = 2×20 Matrix{Float64}"
377+
end
366378
end

0 commit comments

Comments
 (0)