Skip to content

Commit 315fad0

Browse files
committed
Custom pretty-printing #448
1 parent f7c47e7 commit 315fad0

File tree

6 files changed

+150
-79
lines changed

6 files changed

+150
-79
lines changed

src/Deprecated.jl

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -59,38 +59,3 @@ function buildSubgraphFromLabels!(dfg::AbstractDFG,
5959

6060
return subfg
6161
end
62-
63-
64-
## TODO: I think these are handy, so move to Factor and Variable
65-
Base.getproperty(x::DFGFactor,f::Symbol) = begin
66-
if f == :solvable
67-
getfield(x,:_dfgNodeParams).solvable
68-
else
69-
getfield(x,f)
70-
end
71-
end
72-
73-
Base.setproperty!(x::DFGFactor,f::Symbol, val) = begin
74-
if f == :solvable
75-
setfield!(x,f,val)
76-
getfield(x,:_dfgNodeParams).solvable = val
77-
else
78-
setfield!(x,f,val)
79-
end
80-
end
81-
82-
Base.getproperty(x::DFGVariable,f::Symbol) = begin
83-
if f == :solvable
84-
getfield(x,:_dfgNodeParams).solvable
85-
else
86-
getfield(x,f)
87-
end
88-
end
89-
90-
Base.setproperty!(x::DFGVariable,f::Symbol, val) = begin
91-
if f == :solvable
92-
getfield(x,:_dfgNodeParams).solvable = val
93-
else
94-
setfield!(x,f,val)
95-
end
96-
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
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)

0 commit comments

Comments
 (0)