Skip to content

Commit 3bb9d09

Browse files
committed
dfgplot with parameters
1 parent 3cc06db commit 3bb9d09

File tree

3 files changed

+57
-17
lines changed

3 files changed

+57
-17
lines changed

src/DFGPlots/DFGPlots.jl

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,58 @@ end
2424
DFGPlotProps() = DFGPlotProps( (var=colorant"seagreen", fac=colorant"cyan3"),
2525
(var=1.0, fac=0.3),
2626
(var=:box, fac=:elipse),
27-
spring_layout,
27+
spectral_layout,
2828
true)
2929

3030

31-
function dfgplot(dfg::MetaGraphsDFG)
31+
function dfgplot(dfg::LightDFG, p::DFGPlotProps = DFGPlotProps())
3232

33-
nodesize = [has_prop(dfg.g,i,:factor) ? 0.3 : 1.0 for i=vertices(dfg.g)]
34-
nodelabel = [has_prop(dfg.g,i,:factor) ? "" : string(get_prop(dfg.g,i,:label)) for i=vertices(dfg.g)]
35-
nodefillc = [has_prop(dfg.g,i,:factor) ? colorant"seagreen" : colorant"cyan3" for i=vertices(dfg.g)]
33+
nodetypes = [haskey(dfg.g.variables, s) for s in dfg.g.labels]
3634

37-
gplot(dfg.g, nodelabel=nodelabel, nodesize=nodesize, nodefillc=nodefillc, layout=spectral_layout)
35+
nodesize = [isVar ? p.nodesize.var : p.nodesize.fac for isVar in nodetypes]
36+
# nodelabel = [isVar ? string(get_prop(dfg.g,i,:label)) : "" for (i,isVar) in enumerate(nodetypes)]
37+
if p.drawlabels
38+
nodelabel = [nodetypes[i] ? string(s) : "" for (i,s) in enumerate(dfg.g.labels)]
39+
else
40+
nodelabel = nothing
41+
end
42+
43+
nodefillc = [isVar ? p.nodefillc.var : p.nodefillc.fac for isVar in nodetypes]
44+
45+
gplot(dfg.g, nodelabel=nodelabel, nodesize=nodesize, nodefillc=nodefillc, layout=p.layout)
3846

3947
end
4048

41-
function gplot(dfg::MetaGraphsDFG; keyargs...)
42-
gplot(dfg.g; keyargs...)
49+
function dfgplot(dfg::MetaGraphsDFG, p::DFGPlotProps = DFGPlotProps())
50+
51+
nodesize = [has_prop(dfg.g,i,:factor) ? p.nodesize.fac : p.nodesize.var for i=vertices(dfg.g)]
52+
if p.drawlabels
53+
nodelabel = [has_prop(dfg.g,i,:factor) ? "" : string(get_prop(dfg.g,i,:label)) for i=vertices(dfg.g)]
54+
else
55+
nodelabel = nothing
56+
end
57+
nodefillc = [has_prop(dfg.g,i,:factor) ? p.nodefillc.fac : p.nodefillc.var for i=vertices(dfg.g)]
58+
59+
gplot(dfg.g, nodelabel=nodelabel, nodesize=nodesize, nodefillc=nodefillc, layout=p.layout)
60+
4361
end
4462

45-
function dfgplot(dfg::AbstractDFG)
63+
function dfgplot(dfg::AbstractDFG, p::DFGPlotProps = DFGPlotProps())
4664
# TODO implement convert functions
4765
@warn "TODO Implement convert"
4866
ldfg = MetaGraphsDFG{AbstractParams}()
4967
DistributedFactorGraphs._copyIntoGraph!(dfg, ldfg, union(getVariableIds(dfg), getFactorIds(dfg)), true)
5068

51-
nodesize = [has_prop(ldfg.g,i,:factor) ? 0.3 : 1.0 for i=vertices(ldfg.g)]
52-
nodelabel = [has_prop(ldfg.g,i,:factor) ? "" : string(get_prop(ldfg.g,i,:label)) for i=vertices(ldfg.g)]
53-
nodefillc = [has_prop(ldfg.g,i,:factor) ? colorant"seagreen" : colorant"cyan3" for i=vertices(ldfg.g)]
69+
dfgplot(ldfg, p)
5470

55-
gplot(ldfg.g, nodelabel=nodelabel, nodesize=nodesize, nodefillc=nodefillc, layout=spectral_layout)
71+
end
72+
73+
function gplot(dfg::MetaGraphsDFG; keyargs...)
74+
gplot(dfg.g; keyargs...)
75+
end
5676

77+
function gplot(dfg::LightDFG; keyargs...)
78+
gplot(dfg.g; keyargs...)
5779
end
5880

5981
end

src/LightDFG/FactorGraphs/BiMaps.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ end
4545
Base.haskey(b::BiDictMap, s::Symbol) = haskey(b.sym_int, s)
4646
Base.haskey(b::BiDictMap, i::Int) = haskey(b.int_sym, i)
4747

48-
48+
Base.length(b::BiDictMap) = length(b.int_sym)
4949
Base.firstindex(v::BiDictMap) = 1
5050
Base.lastindex(v::BiDictMap) = length(v.int_sym)
5151
Base.iterate(v::BiDictMap, i=1) = (length(v.int_sym) < i ? nothing : (v.int_sym[i], i + 1))

src/LightDFG/services/LightDFG.jl

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,19 @@ Related
217217
218218
ls
219219
"""
220-
getVariableIds(dfg::LightDFG, regexFilter::Union{Nothing, Regex}=nothing; tags::Vector{Symbol}=Symbol[])::Vector{Symbol} = map(v -> v.label, getVariables(dfg, regexFilter, tags=tags))
220+
221+
function getVariableIds(dfg::LightDFG, regexFilter::Union{Nothing, Regex}=nothing; tags::Vector{Symbol}=Symbol[])::Vector{Symbol}
222+
223+
# variables = map(v -> v.dfgNode, filter(n -> n.dfgNode isa DFGVariable, vertices(dfg.g)))
224+
if length(tags) > 0
225+
return map(v -> v.label, getVariables(dfg, regexFilter, tags=tags))
226+
else
227+
variables = collect(keys(dfg.g.variables))
228+
regexFilter != nothing && (variables = filter(v -> occursin(regexFilter, String(v)), variables))
229+
return variables
230+
end
231+
end
232+
221233

222234
# Alias
223235
"""
@@ -246,8 +258,14 @@ end
246258
Get a list of the IDs of the DFGFactors in the DFG.
247259
Optionally specify a label regular expression to retrieves a subset of the factors.
248260
"""
249-
getFactorIds(dfg::LightDFG, regexFilter::Union{Nothing, Regex}=nothing)::Vector{Symbol} = map(f -> f.label, getFactors(dfg, regexFilter))
250-
261+
function getFactorIds(dfg::LightDFG, regexFilter::Union{Nothing, Regex}=nothing)::Vector{Symbol}
262+
# factors = map(v -> v.dfgNode, filter(n -> n.dfgNode isa DFGFactor, vertices(dfg.g)))
263+
factors = collect(keys(dfg.g.factors))
264+
if regexFilter != nothing
265+
factors = filter(f -> occursin(regexFilter, String(f)), factors)
266+
end
267+
return factors
268+
end
251269
"""
252270
$(SIGNATURES)
253271
List the DFGFactors in the DFG.

0 commit comments

Comments
 (0)