Skip to content

Commit 4516872

Browse files
committed
Adding normal adjacency, cleaning up tests
1 parent 4c70ec2 commit 4516872

File tree

3 files changed

+88
-65
lines changed

3 files changed

+88
-65
lines changed

REQUIRE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ LightGraphs 1.2.0
44
MetaGraphs 0.6.1
55
Requires 0.5.2
66
DataFrames
7+
JSON2

src/services/GraphsDFG.jl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export addFactor!
2020
export ls, lsf, getVariables, getFactors
2121
export getVariable, getFactor
2222
export updateVariable, updateFactor
23+
export getAdjacencyMatrix
2324
export getAdjacencyMatrixDataFrame
2425
export getNeighbors
2526
export getSubgraphAroundNode
@@ -270,8 +271,22 @@ function getSubGraph(dfg::GraphsDFG, variableFactorLabels::Vector{Symbol}, inclu
270271
_copyIntoGraph!(dfg, addToDFG, variableFactorLabels, includeOrphanFactors)
271272
return addToDFG
272273
end
273-
#
274274

275+
function getAdjacencyMatrix(dfg::GraphsDFG)::Matrix{Union{Nothing, Symbol}}
276+
varLabels = sort(map(v->v.label, getVariables(dfg)))
277+
factLabels = sort(map(f->f.label, getFactors(dfg)))
278+
vDict = Dict(varLabels .=> [1:length(varLabels)...].+1)
279+
280+
adjMat = Matrix{Union{Nothing, Symbol}}(nothing, length(factLabels)+1, length(varLabels)+1)
281+
# Set row/col headings
282+
adjMat[2:end, 1] = factLabels
283+
adjMat[1, 2:end] = varLabels
284+
for (fIndex, factLabel) in enumerate(factLabels)
285+
factVars = getNeighbors(dfg, getFactor(dfg, factLabel))
286+
map(vLabel -> adjMat[fIndex+1,vDict[vLabel]] = factLabel, factVars)
287+
end
288+
return adjMat
289+
end
275290

276291
function __init__()
277292
@require DataFrames="a93c6f00-e57d-5684-b7b6-d8193f3e46c0" begin

test/runtests.jl

Lines changed: 71 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,68 @@ using DistributedFactorGraphs.GraphsJlAPI
66
dfg = GraphsDFG()
77
v1 = DFGVariable(:a)
88
v2 = DFGVariable(:b)
9-
addVariable!(dfg, v1)
10-
@test_throws Exception addVariable!(dfg, v1)
11-
addVariable!(dfg, v2)
129
f1 = DFGFactor(:f1)
13-
addFactor!(dfg, f1, [v1, v2])
14-
@test_throws Exception addFactor!(dfg, DFGFactor("f2"), [v1, DFGVariable("Nope")])
10+
@testset "Creating Graphs" begin
11+
global dfg,v1,v2,f1
12+
addVariable!(dfg, v1)
13+
@test_throws Exception addVariable!(dfg, v1)
14+
addVariable!(dfg, v2)
15+
addFactor!(dfg, f1, [v1, v2])
16+
@test_throws Exception addFactor!(dfg, DFGFactor("f2"), [v1, DFGVariable("Nope")])
17+
end
1518

16-
@test length(ls(dfg)) == 2
17-
@test length(lsf(dfg)) == 1
18-
# Regexes
19-
@test ls(dfg, r"a") == [v1]
20-
@test lsf(dfg, r"f*") == [f1]
19+
@testset "Listing Nodes" begin
20+
global dfg,v1,v2,f1
21+
@test length(ls(dfg)) == 2
22+
@test length(lsf(dfg)) == 1
23+
# Regexes
24+
@test ls(dfg, r"a") == [v1]
25+
@test lsf(dfg, r"f*") == [f1]
26+
end
2127

2228
# Gets
23-
@test getVariable(dfg, v1.label) == v1
24-
@test getFactor(dfg, f1.label) == f1
25-
@test_throws Exception getVariable(dfg, :nope)
26-
@test_throws Exception getVariable(dfg, "nope")
27-
@test_throws Exception getFactor(dfg, :nope)
28-
@test_throws Exception getFactor(dfg, "nope")
29+
@testset "Gets and Sets" begin
30+
global dfg,v1,v2,f1
31+
@test getVariable(dfg, v1.label) == v1
32+
@test getFactor(dfg, f1.label) == f1
33+
@test_throws Exception getVariable(dfg, :nope)
34+
@test_throws Exception getVariable(dfg, "nope")
35+
@test_throws Exception getFactor(dfg, :nope)
36+
@test_throws Exception getFactor(dfg, "nope")
2937

30-
# Sets
31-
v1Prime = deepcopy(v1)
32-
@test updateVariable(dfg, v1Prime) != v1
33-
f1Prime = deepcopy(f1)
34-
@test updateFactor(dfg, f1Prime) != f1
38+
# Sets
39+
v1Prime = deepcopy(v1)
40+
@test updateVariable(dfg, v1Prime) != v1
41+
f1Prime = deepcopy(f1)
42+
@test updateFactor(dfg, f1Prime) != f1
43+
end
3544

3645
# Deletions
3746
# Not supported at present
38-
@warn "Deletions with Graph.jl is not supported at present"
47+
@testset "Deletions" begin
48+
@warn "Deletions with Graph.jl is not supported at present"
49+
end
3950

4051
# Connectivity test
41-
@test isFullyConnected(dfg) == true
42-
addVariable!(dfg, DFGVariable(:orphan))
43-
@test isFullyConnected(dfg) == false
52+
@testset "Connectivity Test" begin
53+
global dfg,v1,v2,f1
54+
@test isFullyConnected(dfg) == true
55+
addVariable!(dfg, DFGVariable(:orphan))
56+
@test isFullyConnected(dfg) == false
57+
end
4458

4559
# Adjacency matrices
46-
adjMat = getAdjacencyMatrixDataFrame(dfg)
47-
@test size(adjMat) == (4,4)
48-
49-
# Testing
50-
using Test
51-
using DataFrames
52-
using DistributedFactorGraphs
53-
using DistributedFactorGraphs.GraphsJlAPI
60+
@testset "Adjacency Matrices" begin
61+
global dfg,v1,v2,f1
62+
# Normal
63+
adjMat = getAdjacencyMatrix(dfg)
64+
@test size(adjMat) == (2,4)
65+
@test adjMat[1, :] == [nothing, :a, :b, :orphan]
66+
@test adjMat[2, :] == [:f1, :f1, :f1, nothing]
67+
# Dataframe
68+
adjDf = getAdjacencyMatrixDataFrame(dfg)
69+
@test size(adjDf) == (1,4)
70+
end
5471

5572
# Now make a complex graph for connectivity tests
5673
numNodes = 10
@@ -59,37 +76,27 @@ verts = map(n -> DFGVariable(Symbol("x$n")), 1:numNodes)
5976
map(v -> addVariable!(dfg, v), verts)
6077
map(n -> addFactor!(dfg, DFGFactor(Symbol("x$(n)x$(n+1)f1")), [verts[n], verts[n+1]]), 1:(numNodes-1))
6178
# map(n -> addFactor!(dfg, [verts[n], verts[n+2]], DFGFactor(Symbol("x$(n)x$(n+2)f2"))), 1:2:(numNodes-2))
62-
adjMat = getAdjacencyMatrixDataFrame(dfg)
6379

64-
# Get neighbors tests
65-
@test getNeighbors(dfg, verts[1]) == [:x1x2f1]
66-
neighbors = getNeighbors(dfg, getFactor(dfg, :x1x2f1))
67-
@test all([v in [:x1, :x2] for v in neighbors])
68-
# Testing aliases
69-
@test getNeighbors(dfg, getFactor(dfg, :x1x2f1)) == ls(dfg, getFactor(dfg, :x1x2f1))
70-
@test getNeighbors(dfg, :x1x2f1) == ls(dfg, :x1x2f1)
71-
72-
# Subgraphs
73-
dfgSubgraph = getSubgraphAroundNode(dfg, verts[1], 2)
74-
# Only returns x1 and x2
75-
@test all([v in [:x1, :x1x2f1, :x2] for v in map(n -> n.label, [ls(dfgSubgraph)..., lsf(dfgSubgraph)...])])
76-
# Test include orphan factorsVoid
77-
dfgSubgraph = getSubgraphAroundNode(dfg, verts[1], 1, true)
78-
@test all([v in [:x1, :x1x2f1] for v in map(n -> n.label, [ls(dfgSubgraph)..., lsf(dfgSubgraph)...])])
79-
# Test adding to the dfg
80-
dfgSubgraph = getSubgraphAroundNode(dfg, verts[1], 2, true, dfgSubgraph)
81-
@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}[]
80+
@testset "Getting Neighbors" begin
81+
global dfg,verts
82+
# Get neighbors tests
83+
@test getNeighbors(dfg, verts[1]) == [:x1x2f1]
84+
neighbors = getNeighbors(dfg, getFactor(dfg, :x1x2f1))
85+
@test all([v in [:x1, :x2] for v in neighbors])
86+
# Testing aliases
87+
@test getNeighbors(dfg, getFactor(dfg, :x1x2f1)) == ls(dfg, getFactor(dfg, :x1x2f1))
88+
@test getNeighbors(dfg, :x1x2f1) == ls(dfg, :x1x2f1)
8989
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)
90+
91+
@testset "Getting Subgraphs" begin
92+
# Subgraphs
93+
dfgSubgraph = getSubgraphAroundNode(dfg, verts[1], 2)
94+
# Only returns x1 and x2
95+
@test all([v in [:x1, :x1x2f1, :x2] for v in map(n -> n.label, [ls(dfgSubgraph)..., lsf(dfgSubgraph)...])])
96+
# Test include orphan factorsVoid
97+
dfgSubgraph = getSubgraphAroundNode(dfg, verts[1], 1, true)
98+
@test all([v in [:x1, :x1x2f1] for v in map(n -> n.label, [ls(dfgSubgraph)..., lsf(dfgSubgraph)...])])
99+
# Test adding to the dfg
100+
dfgSubgraph = getSubgraphAroundNode(dfg, verts[1], 2, true, dfgSubgraph)
101+
@test all([v in [:x1, :x1x2f1, :x2] for v in map(n -> n.label, [ls(dfgSubgraph)..., lsf(dfgSubgraph)...])])
95102
end

0 commit comments

Comments
 (0)