Skip to content

Commit b5c7e01

Browse files
committed
most test pass
TODO getting subgraphs, Dot files
1 parent 4d626e3 commit b5c7e01

File tree

6 files changed

+76
-56
lines changed

6 files changed

+76
-56
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Graphs = "≥ 0.10.3"
2323
Reexport = "≥ 0.2"
2424
Requires = "≥ 0.5"
2525
julia = "0.7, 1"
26+
MetaGraph = "0.6.1+"
2627

2728
[extras]
2829
IncrementalInference = "904591bb-b899-562f-9e6f-b8df64c7d480"

src/DistributedFactorGraphs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function __init__()
5858
Rows are all factors, columns are all variables, and each cell contains either nothing or the symbol of the relating factor.
5959
The first column is the factor headings.
6060
"""
61-
function getAdjacencyMatrixDataFrame(dfg::GraphsDFG)::Main.DataFrames.DataFrame
61+
function getAdjacencyMatrixDataFrame(dfg::Union{GraphsDFG, LightGraphsDFG})::Main.DataFrames.DataFrame
6262
varLabels = sort(map(v->v.label, getVariables(dfg)))
6363
factLabels = sort(map(f->f.label, getFactors(dfg)))
6464
adjDf = DataFrames.DataFrame(:Factor => Union{Missing, Symbol}[])

src/GraphsDFG/services/GraphsDFG.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ end
304304
Checks if the graph is fully connected, returns true if so.
305305
"""
306306
function isFullyConnected(dfg::GraphsDFG)::Bool
307-
return length(connected_components(dfg.g)) == 1
307+
return length(Graphs.connected_components(dfg.g)) == 1
308308
end
309309

310310
#Alias

src/LightGraphsDFG/LightGraphsDFG.jl

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@ using MetaGraphs
33

44
# Imports
55
include("entities/LightGraphsDFG.jl")
6-
# include("services/LightGraphsDFG.jl")
6+
include("services/LightGraphsDFG.jl")
77

88
# Exports
99
export LightGraphsDFG
10-
# export exists
11-
# export getLabelDict, getDescription, setDescription, getInnerGraph, getAddHistory, getSolverParams, setSolverParams
10+
export exists
11+
export getLabelDict, getDescription, setDescription, getInnerGraph, getAddHistory, getSolverParams, setSolverParams
1212
#
13-
# export getAddHistory, getDescription, getLabelDict
14-
# export addVariable!, addFactor!
15-
# export ls, lsf, getVariables, getFactors, getVariableIds, getFactorIds
16-
# export getVariable, getFactor
17-
# export updateVariable!, updateFactor!
18-
# export deleteVariable!, deleteFactor!
19-
# export getAdjacencyMatrix
20-
# export getAdjacencyMatrixDataFrame
21-
# export getNeighbors
22-
# export getSubgraphAroundNode
23-
# export getSubgraph
24-
# export isFullyConnected, hasOrphans
25-
# export toDot, toDotFile
13+
export getAddHistory, getDescription, getLabelDict
14+
export addVariable!, addFactor!
15+
export ls, lsf, getVariables, getFactors, getVariableIds, getFactorIds
16+
export getVariable, getFactor
17+
export updateVariable!, updateFactor!
18+
export deleteVariable!, deleteFactor!
19+
export getAdjacencyMatrix
20+
export getAdjacencyMatrixDataFrame
21+
export getNeighbors
22+
export getSubgraphAroundNode
23+
export getSubgraph
24+
export isFullyConnected, hasOrphans
25+
export toDot, toDotFile

src/LightGraphsDFG/services/LightGraphsDFG.jl

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ function addFactor!(dfg::LightGraphsDFG, variables::Vector{DFGVariable}, factor:
106106

107107
props = Dict(:factor=>factor)
108108

109-
retval = add_vertex!(dfg.g, :label, factor.label)
109+
retval = MetaGraphs.add_vertex!(dfg.g, :label, factor.label)
110110
retval && set_props!(dfg.g, nv(dfg.g), props)
111111

112112
# Add index
@@ -116,7 +116,7 @@ function addFactor!(dfg::LightGraphsDFG, variables::Vector{DFGVariable}, factor:
116116
# v = dfg.g.vertices[variable._internalId]
117117
# edge = Graphs.make_edge(dfg.g, v, f)
118118
# Graphs.add_edge!(dfg.g, edge)
119-
retval && add_edge!(dfg.g, dfg.g[variable.label,:label], dfg.g[factor.label,:label])
119+
retval && MetaGraphs.add_edge!(dfg.g, dfg.g[variable.label,:label], dfg.g[factor.label,:label])
120120
end
121121
# Track insertion
122122
# push!(dfg.addHistory, factor.label)
@@ -191,7 +191,7 @@ function updateVariable!(dfg::LightGraphsDFG, variable::DFGVariable)::DFGVariabl
191191
error("Variable label '$(variable.label)' does not exist in the factor graph")
192192
end
193193
# dfg.g.vertices[dfg.labelDict[variable.label]].dfgNode = variable
194-
set_props!(dfg.g, dfg.g[label,:label], :variable, variable)
194+
set_prop!(dfg.g, dfg.g[variable.label,:label], :variable, variable)
195195
return variable
196196
end
197197

@@ -204,7 +204,7 @@ function updateFactor!(dfg::LightGraphsDFG, factor::DFGFactor)::DFGFactor
204204
error("Factor label '$(factor.label)' does not exist in the factor graph")
205205
end
206206
# dfg.g.vertices[dfg.labelDict[factor.label]].dfgNode = factor
207-
set_props!(dfg.g, dfg.g[label,:label], :factor, factor)
207+
set_prop!(dfg.g, dfg.g[factor.label,:label], :factor, factor)
208208
return factor
209209
end
210210

@@ -242,7 +242,7 @@ function deleteFactor!(dfg::LightGraphsDFG, label::Symbol)::DFGFactor
242242
# factor = dfg.g.vertices[dfg.labelDict[label]].dfgNode
243243
# delete_vertex!(dfg.g.vertices[dfg.labelDict[label]], dfg.g)
244244
factor = get_prop(dfg.g, dfg.g[label,:label], :factor)
245-
rem_vertex!(dfg.g, dfg.g[label,:label])
245+
MetaGraphs.rem_vertex!(dfg.g, dfg.g[label,:label])
246246
delete!(dfg.labelDict, label)
247247
return factor
248248
end
@@ -345,7 +345,7 @@ end
345345
Checks if the graph is fully connected, returns true if so.
346346
"""
347347
function isFullyConnected(dfg::LightGraphsDFG)::Bool
348-
return length(connected_components(dfg.g)) == 1
348+
return length(LightGraphs.connected_components(dfg.g)) == 1
349349
end
350350

351351
#Alias
@@ -364,18 +364,20 @@ function getNeighbors(dfg::LightGraphsDFG, node::T; ready::Union{Nothing, Int}=n
364364
if !haskey(dfg.labelDict, node.label)
365365
error("Variable/factor with label '$(node.label)' does not exist in the factor graph")
366366
end
367-
vert = dfg.g.vertices[dfg.labelDict[node.label]]
368-
neighbors = in_neighbors(vert, dfg.g) #Don't use out_neighbors! It enforces directiveness even if we don't want it
367+
# vert = dfg.g.vertices[dfg.labelDict[node.label]]
368+
# neighbors = in_neighbors(vert, dfg.g) #Don't use out_neighbors! It enforces directiveness even if we don't want it
369+
neighbors = map(idx->get_prop(dfg.g, idx, :label), LightGraphs.neighbors(dfg.g, dfg.g[node.label,:label]))
369370
# Additional filtering
370-
neighbors = ready != nothing ? filter(v -> v.ready == ready, neighbors) : neighbors
371-
neighbors = backendset != nothing ? filter(v -> v.backendset == backendset, neighbors) : neighbors
371+
#TODO ready and backendset
372+
# neighbors = ready != nothing ? filter(v -> v.ready == ready, neighbors) : neighbors
373+
# neighbors = backendset != nothing ? filter(v -> v.backendset == backendset, neighbors) : neighbors
372374
# Variable sorting (order is important)
373375
if node isa DFGFactor
374-
order = intersect(node._variableOrderSymbols, map(v->v.dfgNode.label, neighbors))
376+
order = intersect(node._variableOrderSymbols, neighbors)#map(v->v.dfgNode.label, neighbors))
375377
return order
376378
end
377379

378-
return map(n -> n.dfgNode.label, neighbors)
380+
return neighbors#map(n -> n.dfgNode.label, neighbors)
379381
end
380382
"""
381383
$(SIGNATURES)
@@ -385,19 +387,23 @@ function getNeighbors(dfg::LightGraphsDFG, label::Symbol; ready::Union{Nothing,
385387
if !haskey(dfg.labelDict, label)
386388
error("Variable/factor with label '$(label)' does not exist in the factor graph")
387389
end
388-
vert = dfg.g.vertices[dfg.labelDict[label]]
389-
neighbors = in_neighbors(vert, dfg.g) #Don't use out_neighbors! It enforces directiveness even if we don't want it
390+
# vert = dfg.g.vertices[dfg.labelDict[label]]
391+
# neighbors = in_neighbors(vert, dfg.g) #Don't use out_neighbors! It enforces directiveness even if we don't want it
390392
# Additional filtering
391-
neighbors = ready != nothing ? filter(v -> v.ready == ready, neighbors) : neighbors
392-
neighbors = backendset != nothing ? filter(v -> v.backendset == backendset, neighbors) : neighbors
393+
# TODO ready and backendset
394+
# neighbors = ready != nothing ? filter(v -> v.ready == ready, neighbors) : neighbors
395+
# neighbors = backendset != nothing ? filter(v -> v.backendset == backendset, neighbors) : neighbors
393396
# Variable sorting when using a factor (function order is important)
394-
if vert.dfgNode isa DFGFactor
395-
vert.dfgNode._variableOrderSymbols
396-
order = intersect(vert.dfgNode._variableOrderSymbols, map(v->v.dfgNode.label, neighbors))
397-
return order
398-
end
397+
# if vert.dfgNode isa DFGFactor
398+
# vert.dfgNode._variableOrderSymbols
399+
# order = intersect(vert.dfgNode._variableOrderSymbols, map(v->v.dfgNode.label, neighbors))
400+
# return order
401+
# end
402+
# return map(n -> n.dfgNode.label, neighbors)
403+
404+
neighbors = map(idx->get_prop(dfg.g, idx, :label), LightGraphs.neighbors(dfg.g, dfg.g[label,:label]))
405+
return neighbors
399406

400-
return map(n -> n.dfgNode.label, neighbors)
401407
end
402408

403409
# Aliases
@@ -451,6 +457,8 @@ function _copyIntoGraph!(sourceDFG::LightGraphsDFG, destDFG::LightGraphsDFG, var
451457
return nothing
452458
end
453459

460+
461+
#TODO TODO TODO TODO TODO TODO TODO TODO
454462
"""
455463
$(SIGNATURES)
456464
Retrieve a deep subgraph copy around a given variable or factor.
@@ -525,16 +533,19 @@ function getAdjacencyMatrix(dfg::LightGraphsDFG)::Matrix{Union{Nothing, Symbol}}
525533
return adjMat
526534
end
527535

536+
528537
"""
529538
$(SIGNATURES)
530539
Produces a dot-format of the graph for visualization.
531540
"""
532541
function toDot(dfg::LightGraphsDFG)::String
533-
m = PipeBuffer()
534-
write(m,Graphs.to_dot(dfg.g))
535-
data = take!(m)
536-
close(m)
537-
return String(data)
542+
# m = PipeBuffer()
543+
# write(m,Graphs.to_dot(dfg.g))
544+
# data = take!(m)
545+
# close(m)
546+
# return String(data)
547+
@error "toDot(dfg::LightGraphsDFG) is not sopported yet"
548+
return nothing
538549
end
539550

540551
"""
@@ -548,12 +559,17 @@ Note
548559
- Based on graphviz.org
549560
"""
550561
function toDotFile(dfg::LightGraphsDFG, fileName::String="/tmp/dfg.dot")::Nothing
551-
open(fileName, "w") do fid
552-
write(fid,Graphs.to_dot(dfg.g))
553-
end
554-
return nothing
562+
# open(fileName, "w") do fid
563+
# write(fid,Graphs.to_dot(dfg.g))
564+
# end
565+
# return nothing
566+
#TODO
567+
@error "toDotFile(dfg::LightGraphsDFG,filename) is not sopported yet"
568+
return nothing
555569
end
556570

571+
572+
557573
# function __init__()
558574
# @require DataFrames="a93c6f00-e57d-5684-b7b6-d8193f3e46c0" begin
559575
# if isdefined(Main, :DataFrames)
@@ -580,7 +596,3 @@ end
580596
# end
581597
# end
582598
# end
583-
584-
585-
586-
#################################################################################

test/interfaceTests.jl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ map(n -> addFactor!(dfg, [verts[n], verts[n+1]], DFGFactor{Int, :Symbol}(Symbol(
114114
@test getNeighbors(dfg, :x1x2f1) == ls(dfg, :x1x2f1)
115115
end
116116

117+
#TODO Subgraphs for LightGraphsDFG
117118
@testset "Getting Subgraphs" begin
118119
# Subgraphs
119120
dfgSubgraph = getSubgraphAroundNode(dfg, verts[1], 2)
@@ -128,8 +129,14 @@ end
128129
end
129130

130131
@testset "Producing Dot Files" begin
131-
@test toDot(dfg) == "graph graphname {\n18 [\"label\"=\"x8x9f1\",\"shape\"=\"ellipse\",\"fillcolor\"=\"blue\",\"color\"=\"blue\"]\n2 [\"label\"=\"x2\",\"shape\"=\"box\",\"fillcolor\"=\"red\",\"color\"=\"red\"]\n2 -- 11\n2 -- 12\n16 [\"label\"=\"x6x7f1\",\"shape\"=\"ellipse\",\"fillcolor\"=\"blue\",\"color\"=\"blue\"]\n11 [\"label\"=\"x1x2f1\",\"shape\"=\"ellipse\",\"fillcolor\"=\"blue\",\"color\"=\"blue\"]\n7 [\"label\"=\"x7\",\"shape\"=\"box\",\"fillcolor\"=\"red\",\"color\"=\"red\"]\n7 -- 16\n7 -- 17\n9 [\"label\"=\"x9\",\"shape\"=\"box\",\"fillcolor\"=\"red\",\"color\"=\"red\"]\n9 -- 18\n9 -- 19\n10 [\"label\"=\"x10\",\"shape\"=\"box\",\"fillcolor\"=\"red\",\"color\"=\"red\"]\n10 -- 19\n19 [\"label\"=\"x9x10f1\",\"shape\"=\"ellipse\",\"fillcolor\"=\"blue\",\"color\"=\"blue\"]\n17 [\"label\"=\"x7x8f1\",\"shape\"=\"ellipse\",\"fillcolor\"=\"blue\",\"color\"=\"blue\"]\n8 [\"label\"=\"x8\",\"shape\"=\"box\",\"fillcolor\"=\"red\",\"color\"=\"red\"]\n8 -- 17\n8 -- 18\n6 [\"label\"=\"x6\",\"shape\"=\"box\",\"fillcolor\"=\"red\",\"color\"=\"red\"]\n6 -- 15\n6 -- 16\n4 [\"label\"=\"x4\",\"shape\"=\"box\",\"fillcolor\"=\"red\",\"color\"=\"red\"]\n4 -- 13\n4 -- 14\n3 [\"label\"=\"x3\",\"shape\"=\"box\",\"fillcolor\"=\"red\",\"color\"=\"red\"]\n3 -- 12\n3 -- 13\n5 [\"label\"=\"x5\",\"shape\"=\"box\",\"fillcolor\"=\"red\",\"color\"=\"red\"]\n5 -- 14\n5 -- 15\n13 [\"label\"=\"x3x4f1\",\"shape\"=\"ellipse\",\"fillcolor\"=\"blue\",\"color\"=\"blue\"]\n14 [\"label\"=\"x4x5f1\",\"shape\"=\"ellipse\",\"fillcolor\"=\"blue\",\"color\"=\"blue\"]\n15 [\"label\"=\"x5x6f1\",\"shape\"=\"ellipse\",\"fillcolor\"=\"blue\",\"color\"=\"blue\"]\n12 [\"label\"=\"x2x3f1\",\"shape\"=\"ellipse\",\"fillcolor\"=\"blue\",\"color\"=\"blue\"]\n1 [\"label\"=\"x1\",\"shape\"=\"box\",\"fillcolor\"=\"red\",\"color\"=\"red\"]\n1 -- 11\n}\n"
132+
if testDFGAPI == LightGraphsDFG
133+
@warn "Skipping LightGraphsDFG toDot functions"
134+
@test_skip 1==0#toDot(dfg)
135+
@test_skip 1==0#toDotFile(dfg, "something.dot")
136+
else
137+
@test toDot(dfg) == "graph graphname {\n18 [\"label\"=\"x8x9f1\",\"shape\"=\"ellipse\",\"fillcolor\"=\"blue\",\"color\"=\"blue\"]\n2 [\"label\"=\"x2\",\"shape\"=\"box\",\"fillcolor\"=\"red\",\"color\"=\"red\"]\n2 -- 11\n2 -- 12\n16 [\"label\"=\"x6x7f1\",\"shape\"=\"ellipse\",\"fillcolor\"=\"blue\",\"color\"=\"blue\"]\n11 [\"label\"=\"x1x2f1\",\"shape\"=\"ellipse\",\"fillcolor\"=\"blue\",\"color\"=\"blue\"]\n7 [\"label\"=\"x7\",\"shape\"=\"box\",\"fillcolor\"=\"red\",\"color\"=\"red\"]\n7 -- 16\n7 -- 17\n9 [\"label\"=\"x9\",\"shape\"=\"box\",\"fillcolor\"=\"red\",\"color\"=\"red\"]\n9 -- 18\n9 -- 19\n10 [\"label\"=\"x10\",\"shape\"=\"box\",\"fillcolor\"=\"red\",\"color\"=\"red\"]\n10 -- 19\n19 [\"label\"=\"x9x10f1\",\"shape\"=\"ellipse\",\"fillcolor\"=\"blue\",\"color\"=\"blue\"]\n17 [\"label\"=\"x7x8f1\",\"shape\"=\"ellipse\",\"fillcolor\"=\"blue\",\"color\"=\"blue\"]\n8 [\"label\"=\"x8\",\"shape\"=\"box\",\"fillcolor\"=\"red\",\"color\"=\"red\"]\n8 -- 17\n8 -- 18\n6 [\"label\"=\"x6\",\"shape\"=\"box\",\"fillcolor\"=\"red\",\"color\"=\"red\"]\n6 -- 15\n6 -- 16\n4 [\"label\"=\"x4\",\"shape\"=\"box\",\"fillcolor\"=\"red\",\"color\"=\"red\"]\n4 -- 13\n4 -- 14\n3 [\"label\"=\"x3\",\"shape\"=\"box\",\"fillcolor\"=\"red\",\"color\"=\"red\"]\n3 -- 12\n3 -- 13\n5 [\"label\"=\"x5\",\"shape\"=\"box\",\"fillcolor\"=\"red\",\"color\"=\"red\"]\n5 -- 14\n5 -- 15\n13 [\"label\"=\"x3x4f1\",\"shape\"=\"ellipse\",\"fillcolor\"=\"blue\",\"color\"=\"blue\"]\n14 [\"label\"=\"x4x5f1\",\"shape\"=\"ellipse\",\"fillcolor\"=\"blue\",\"color\"=\"blue\"]\n15 [\"label\"=\"x5x6f1\",\"shape\"=\"ellipse\",\"fillcolor\"=\"blue\",\"color\"=\"blue\"]\n12 [\"label\"=\"x2x3f1\",\"shape\"=\"ellipse\",\"fillcolor\"=\"blue\",\"color\"=\"blue\"]\n1 [\"label\"=\"x1\",\"shape\"=\"box\",\"fillcolor\"=\"red\",\"color\"=\"red\"]\n1 -- 11\n}\n"
132138

133-
@test toDotFile(dfg, "something.dot") == nothing
134-
Base.rm("something.dot")
139+
@test toDotFile(dfg, "something.dot") == nothing
140+
Base.rm("something.dot")
141+
end
135142
end

0 commit comments

Comments
 (0)