Skip to content

Commit 4c70ec2

Browse files
committed
Adjacency matrices
1 parent a32e854 commit 4c70ec2

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

src/services/GraphsDFG.jl

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,22 @@ function getNeighbors(dfg::GraphsDFG, node::T)::Vector{Symbol} where T <: DFGNo
180180
neighbors = in_neighbors(vert, dfg.g) #Don't use out_neighbors! It enforces directiveness even if we don't want it
181181
return map(n -> n.dfgNode.label, neighbors)
182182
end
183+
function getNeighbors(dfg::GraphsDFG, label::Symbol)::Vector{Symbol} where T <: DFGNode
184+
if !haskey(dfg.labelDict, label)
185+
error("Variable/factor with label '$(label)' does not exist in the factor graph")
186+
end
187+
vert = dfg.g.vertices[dfg.labelDict[label]]
188+
neighbors = in_neighbors(vert, dfg.g) #Don't use out_neighbors! It enforces directiveness even if we don't want it
189+
return map(n -> n.dfgNode.label, neighbors)
190+
end
183191

184-
# Alias
192+
# Aliases
185193
function ls(dfg::GraphsDFG, node::T)::Vector{Symbol} where T <: DFGNode
186194
return getNeighbors(dfg, node)
187195
end
196+
function ls(dfg::GraphsDFG, label::Symbol)::Vector{Symbol} where T <: DFGNode
197+
return getNeighbors(dfg, label)
198+
end
188199

189200
function _copyIntoGraph!(sourceDFG::GraphsDFG, destDFG::GraphsDFG, variableFactorLabels::Vector{Symbol}, includeOrphanFactors::Bool=false)::Nothing
190201
# Split into variables and factors
@@ -267,9 +278,18 @@ function __init__()
267278
if isdefined(Main, :DataFrames)
268279
export getAdjacencyMatrixDataFrame
269280
function getAdjacencyMatrixDataFrame(dfg::GraphsDFG)::Main.DataFrames.DataFrame
270-
colNames = map(n -> n.dfgNode.label, vertices(dfg.g))
271-
adjMat = adjacency_matrix(dfg.g)
272-
return Main.DataFrames.DataFrame(adjMat, colNames)
281+
varLabels = sort(map(v->v.label, getVariables(dfg)))
282+
factLabels = sort(map(f->f.label, getFactors(dfg)))
283+
adjDf = DataFrames.DataFrame(:Factor => Union{Missing, Symbol}[])
284+
for varLabel in varLabels
285+
adjDf[varLabel] = Union{Missing, Symbol}[]
286+
end
287+
for (i, factLabel) in enumerate(factLabels)
288+
push!(adjDf, [factLabel, DataFrames.missings(length(varLabels))...])
289+
factVars = getNeighbors(dfg, getFactor(dfg, factLabel))
290+
map(vLabel -> adjDf[vLabel][i] = factLabel, factVars)
291+
end
292+
return adjDf
273293
end
274294
end
275295
end

test/runtests.jl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ adjMat = getAdjacencyMatrixDataFrame(dfg)
6565
@test getNeighbors(dfg, verts[1]) == [:x1x2f1]
6666
neighbors = getNeighbors(dfg, getFactor(dfg, :x1x2f1))
6767
@test all([v in [:x1, :x2] for v in neighbors])
68-
# Testing alias
68+
# Testing aliases
6969
@test getNeighbors(dfg, getFactor(dfg, :x1x2f1)) == ls(dfg, getFactor(dfg, :x1x2f1))
70+
@test getNeighbors(dfg, :x1x2f1) == ls(dfg, :x1x2f1)
7071

7172
# Subgraphs
7273
dfgSubgraph = getSubgraphAroundNode(dfg, verts[1], 2)
@@ -78,3 +79,17 @@ dfgSubgraph = getSubgraphAroundNode(dfg, verts[1], 1, true)
7879
# Test adding to the dfg
7980
dfgSubgraph = getSubgraphAroundNode(dfg, verts[1], 2, true, dfgSubgraph)
8081
@test all([v in [:x1, :x1x2f1, :x2] for v in map(n -> n.label, [ls(dfgSubgraph)..., lsf(dfgSubgraph)...])])
82+
83+
# Adjacency matrix
84+
varLabels = sort(map(v->v.label, getVariables(dfg)))
85+
factLabels = sort(map(f->f.label, getFactors(dfg)))
86+
adjDf = DataFrame(:Factor => Union{Missing, Symbol}[])
87+
for varLabel in varLabels
88+
adjDf[varLabel] = Union{Missing, Symbol}[]
89+
end
90+
for (i, factLabel) in enumerate(factLabels)
91+
global adjDf
92+
push!(adjDf, [factLabel, missings(length(varLabels))...])
93+
factVars = getNeighbors(dfg, getFactor(dfg, factLabel))
94+
map(vLabel -> adjDf[vLabel][i] = factLabel, factVars)
95+
end

0 commit comments

Comments
 (0)