Skip to content

Commit 63a96ca

Browse files
authored
Merge pull request #454 from JuliaRobotics/feature/20q2/customprint
Custom pretty-printing #448
2 parents 33e558a + 8f91eaf commit 63a96ca

File tree

7 files changed

+193
-81
lines changed

7 files changed

+193
-81
lines changed

src/Deprecated.jl

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ include("../attic/GraphsDFG/GraphsDFG.jl")
1414

1515
@deprecate loadDFG(source::String, iifModule::Module, dest::AbstractDFG) loadDFG!(dest, source)
1616

17-
18-
export buildSubgraphFromLabels!
19-
2017
# leave a bit longer
2118
export buildSubgraphFromLabels!
2219
function buildSubgraphFromLabels!(dfg::AbstractDFG,
@@ -32,37 +29,3 @@ function buildSubgraphFromLabels!(dfg::AbstractDFG,
3229
""")
3330

3431
end
35-
36-
## TODO: I think these are handy, so move to Factor and Variable
37-
Base.getproperty(x::DFGFactor,f::Symbol) = begin
38-
if f == :solvable
39-
getfield(x,:_dfgNodeParams).solvable
40-
else
41-
getfield(x,f)
42-
end
43-
end
44-
45-
Base.setproperty!(x::DFGFactor,f::Symbol, val) = begin
46-
if f == :solvable
47-
setfield!(x,f,val)
48-
getfield(x,:_dfgNodeParams).solvable = val
49-
else
50-
setfield!(x,f,val)
51-
end
52-
end
53-
54-
Base.getproperty(x::DFGVariable,f::Symbol) = begin
55-
if f == :solvable
56-
getfield(x,:_dfgNodeParams).solvable
57-
else
58-
getfield(x,f)
59-
end
60-
end
61-
62-
Base.setproperty!(x::DFGVariable,f::Symbol, val) = begin
63-
if f == :solvable
64-
getfield(x,:_dfgNodeParams).solvable = val
65-
else
66-
setfield!(x,f,val)
67-
end
68-
end

src/DistributedFactorGraphs.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,8 @@ export
235235
#TODO remove export in DFG v0.8.0
236236
# export ConvolutionObject
237237

238-
## needsahome.jl
239-
import Base: print
240-
export printFactor, printVariable, print
238+
## CustomPrinting.jl
239+
export printFactor, printVariable, printNode
241240

242241
##==============================================================================
243242
## Files Includes
@@ -277,8 +276,8 @@ include("FileDFG/FileDFG.jl")
277276

278277
include("CloudGraphsDFG/CloudGraphsDFG.jl")
279278

280-
# Needs a home.
281-
include("needsahome.jl")
279+
# Custom show and printing for variable factor etc.
280+
include("services/CustomPrinting.jl")
282281

283282
function __init__()
284283
@require GraphPlot = "a2cc645c-3eea-5389-862e-a155d0052231" begin

src/entities/DFGFactor.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,22 @@ DFGFactor(label::Symbol,
141141

142142

143143

144+
Base.getproperty(x::DFGFactor,f::Symbol) = begin
145+
if f == :solvable
146+
getfield(x,:_dfgNodeParams).solvable
147+
else
148+
getfield(x,f)
149+
end
150+
end
151+
152+
Base.setproperty!(x::DFGFactor,f::Symbol, val) = begin
153+
if f == :solvable
154+
setfield!(x,f,val)
155+
getfield(x,:_dfgNodeParams).solvable = val
156+
else
157+
setfield!(x,f,val)
158+
end
159+
end
144160
##------------------------------------------------------------------------------
145161
## DFGFactorSummary lv1
146162
##------------------------------------------------------------------------------

src/entities/DFGVariable.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,23 @@ DFGVariable(label::Symbol,
233233
solvable::Int=1) where {T <: InferenceVariable} =
234234
DFGVariable{T}(label, timestamp, tags, estimateDict, Dict{Symbol, VariableNodeData{T}}(:default=>solverData), smallData, bigData, DFGNodeParams(solvable))
235235

236+
Base.getproperty(x::DFGVariable,f::Symbol) = begin
237+
if f == :solvable
238+
getfield(x,:_dfgNodeParams).solvable
239+
else
240+
getfield(x,f)
241+
end
242+
end
243+
244+
Base.setproperty!(x::DFGVariable,f::Symbol, val) = begin
245+
if f == :solvable
246+
getfield(x,:_dfgNodeParams).solvable = val
247+
else
248+
setfield!(x,f,val)
249+
end
250+
end
251+
252+
236253
##------------------------------------------------------------------------------
237254
function Base.copy(o::DFGVariable)::DFGVariable
238255
return DFGVariable(o.label, getSofttype(o)(), tags=copy(o.tags), estimateDict=copy(o.estimateDict),

src/needsahome.jl

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/services/CustomPrinting.jl

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
##==============================================================================
2+
## Overloading show
3+
##==============================================================================
4+
# Base.show_default(io, v)
5+
Base.show(io::IO, ::MIME"text/plain", v::DFGVariable) = show(IOContext(io, :limit=>true, :compact=>true), v)
6+
7+
Base.show(io::IO, ::MIME"text/plain", f::DFGFactor) = show(IOContext(io, :limit=>true, :compact=>true), f)
8+
9+
#default for Atom/Juno
10+
Base.show(io::IO, ::MIME"application/prs.juno.inline", x::Union{DFGVariable, DFGFactor}) = x
11+
12+
13+
##==============================================================================
14+
## Printing Variables and Factors
15+
##==============================================================================
16+
17+
printVariable(vert::DFGVariable; kwargs...) = printVariable(stdout::IO, vert; kwargs...)
18+
19+
function printVariable(io::IO, vert::DFGVariable;
20+
short::Bool=false,
21+
compact::Bool=true,
22+
limit::Bool=true,
23+
skipfields::Vector{Symbol}=Symbol[],
24+
solveKeys::Vector{Symbol}=Symbol[])
25+
26+
ioc = IOContext(io, :limit=>limit, :compact=>compact)
27+
28+
if short
29+
printstyled(ioc, summary(vert),"\n", bold=true)
30+
vnd = getSolverData(vert)
31+
println(ioc, "label: $(vert.label)")
32+
println(ioc, "tags: $(getTags(vert))")
33+
println(ioc, "size marginal samples: $(size(vnd.val))")
34+
println(ioc, "kde bandwidths: $((vnd.bw)[:,1])")
35+
if 0 < length(getPPEDict(vert))
36+
println(ioc, "PPE.suggested: $(round.(getPPE(vert).suggested,digits=4))")
37+
else
38+
println(ioc, "No PPEs")
39+
end
40+
# println(ioc, "kde max: $(round.(getKDEMax(getKDE(vnd)),digits=4))")
41+
# println(ioc, "kde max: $(round.(getKDEMax(getKDE(vnd)),digits=4))")
42+
else
43+
44+
printstyled(ioc, summary(vert),"\n", bold=true, color=:blue)
45+
46+
:solver in skipfields && push!(skipfields, :solverDataDict)
47+
:ppe in skipfields && push!(skipfields, :ppeDict)
48+
49+
t = typeof(vert)
50+
fields = setdiff(fieldnames(t), skipfields)
51+
nf = nfields(vert)
52+
53+
for f in fields
54+
printstyled(ioc, f,":\n", color=:blue)
55+
show(ioc, getfield(vert, f))
56+
println(ioc)
57+
end
58+
end
59+
end
60+
61+
printFactor(vert::DFGFactor; kwargs...) = printFactor(stdout::IO, vert; kwargs...)
62+
function printFactor(io::IO, vert::DFGFactor;
63+
short::Bool=false,
64+
compact::Bool=true,
65+
limit::Bool=true,
66+
skipfields::Vector{Symbol}=Symbol[])
67+
68+
ioc = IOContext(io, :limit=>limit, :compact=>compact)
69+
70+
if short
71+
printstyled(ioc, summary(vert),"\n", bold=true)
72+
println(ioc, "label: ", vert.label)
73+
println(ioc, "timestamp: ", vert.timestamp)
74+
println(ioc, "tags: ", vert.tags)
75+
println(ioc, "solvable: ", vert.solvable)
76+
println(ioc, "VariableOrder: ", vert._variableOrderSymbols)
77+
else
78+
79+
printstyled(ioc, summary(vert),"\n", bold=true, color=:blue)
80+
81+
:solver in skipfields && push!(skipfields, :solverData)
82+
83+
t = typeof(vert)
84+
fields = setdiff(fieldnames(t), skipfields)
85+
nf = nfields(vert)
86+
87+
for f in fields
88+
printstyled(ioc, f,":\n", color=:blue)
89+
show(ioc, getfield(vert, f))
90+
println(ioc)
91+
end
92+
end
93+
end
94+
95+
96+
"""
97+
$SIGNATURES
98+
99+
Display and return to console the user factor identified by tag name.
100+
"""
101+
printFactor(dfg::AbstractDFG, sym::Symbol) = print(getFactor(dfg, sym))
102+
103+
"""
104+
$SIGNATURES
105+
106+
Display the content of `VariableNodeData` to console for a given factor graph and variable tag`::Symbol`.
107+
108+
Dev Notes
109+
- TODO split as two show macros between AMP and DFG
110+
"""
111+
printVariable(dfg::AbstractDFG, sym::Symbol) = print(getVariable(dfg, sym))
112+
113+
printNode(dfg::AbstractDFG, sym::Symbol) = isVariable(dfg,sym) ? printVariable(dfg, sym) : printFactor(dfg, sym)

test/interfaceTests.jl

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,54 @@ end
2222
global fac0, fac1, fac2 = DFGFactorSCA()
2323
end
2424

25+
2526
@testset "Variables and Factors CRUD and SET" begin
2627
VariablesandFactorsCRUD_SET!(fg1, var1, var2, var3, fac0, fac1, fac2)
2728
end
2829

2930

31+
@testset "Custom Printing" begin
32+
33+
iobuf = IOBuffer()
34+
# for now just test the interface and a bit of output
35+
@test printVariable(var1) == nothing
36+
@test printFactor(fac1) == nothing
37+
38+
@test printVariable(iobuf, var1, skipfields=[:timestamp, :solver, :ppe]) == nothing
39+
@test String(take!(iobuf)) == "DFGVariable{TestSofttype1}\nlabel:\n:a\ntags:\nSet([:VARIABLE, :POSE])\nsmallData:\nDict(\"small\"=>\"data\")\nbigData:\nDict{Symbol,AbstractBigDataEntry}()\n_dfgNodeParams:\nDFGNodeParams(0)\n"
40+
41+
@test printVariable(iobuf, var1, short=true) == nothing
42+
@test String(take!(iobuf)) == "DFGVariable{TestSofttype1}\nlabel: a\ntags: Set([:VARIABLE, :POSE])\nsize marginal samples: (1, 1)\nkde bandwidths: [0.0]\nNo PPEs\n"
43+
44+
45+
@test printFactor(iobuf, fac1, skipfields=[:timestamp, :solver]) == nothing
46+
@test occursin(r"DFGFactor.*\nlabel:\n:abf1", String(take!(iobuf)))
47+
48+
String(take!(iobuf)) == "DFGFactor{TestCCW{TestFunctorInferenceType1}}\nlabel:\n:abf1\ntags:\nSet([:tag1, :tag2])\nsolvable:\n0\n_dfgNodeParams:\nDFGNodeParams(1)\n_variableOrderSymbols:\n[:a, :b]\n"
49+
50+
@test printFactor(iobuf, fac1, short=true) == nothing
51+
@test occursin(r"DFGFactor.*\nlabel.*\ntimestamp.*\ntags.*\nsolvable", String(take!(iobuf)))
52+
53+
# s = String(take!(iobuf))
54+
55+
@test show(var1) == nothing
56+
@test show(fac1) == nothing
57+
58+
@test show(iobuf, MIME("text/plain"), var1) == nothing
59+
isapprox(length(take!(iobuf)), 452, atol=10)
60+
@test show(iobuf, MIME("text/plain"), fac1) == nothing
61+
isapprox(length(take!(iobuf)), 301, atol=10)
62+
63+
@test printVariable(fg1, :a) == nothing
64+
@test printFactor(fg1, :abf1) == nothing
65+
66+
@test printNode(fg1, :a) == nothing
67+
@test printNode(fg1, :abf1) == nothing
68+
69+
show(stdout, MIME("application/prs.juno.inline"), var1) == var1
70+
show(stdout, MIME("application/prs.juno.inline"), fac1) == fac1
71+
end
72+
3073
@testset "tags" begin
3174
tagsTestBlock!(fg1, var1, v1_tags)
3275
end

0 commit comments

Comments
 (0)