Skip to content

Commit f83af79

Browse files
authored
getAdjacencyMatrixSparse integration (#86)
* getAdjacencyMatrixSparse - LigthDFG - LightGraphsDFG - SymbolDFG - GraphsDFG
1 parent 45c797d commit f83af79

File tree

11 files changed

+80
-46
lines changed

11 files changed

+80
-46
lines changed

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "DistributedFactorGraphs"
22
uuid = "b5cc3c7e-6572-11e9-2517-99fb8daf2f04"
3-
version = "0.3.2"
3+
version = "0.4.0"
44

55
[deps]
66
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
@@ -15,6 +15,7 @@ MetaGraphs = "626554b9-1ddb-594c-aa3c-2596fe9399a5"
1515
Neo4j = "d2adbeaf-5838-5367-8a2f-e46d570981db"
1616
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
1717
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
18+
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
1819

1920
[compat]
2021
Distributions = "≥ 0.18"

src/DistributedFactorGraphs.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ using Distributions
88
using Reexport
99
using JSON2
1010
using LinearAlgebra
11+
using SparseArrays
1112

1213
# Entities
1314
include("entities/AbstractTypes.jl")
@@ -38,6 +39,10 @@ export pack, unpack
3839
include("services/AbstractDFG.jl")
3940
include("services/DFGVariable.jl")
4041

42+
#TODO @sam I'm just doing it like this at the moment because I don't know a better way.
43+
include("services/interfaces.jl")
44+
export getAdjacencyMatrixSparse
45+
4146
# Include the Graphs.jl API.
4247
include("GraphsDFG/GraphsDFG.jl")
4348

src/GraphsDFG/services/GraphsDFG.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,21 @@ function getAdjacencyMatrix(dfg::GraphsDFG)::Matrix{Union{Nothing, Symbol}}
484484
return adjMat
485485
end
486486

487+
function getAdjacencyMatrixSparse(dfg::GraphsDFG)::Tuple{LightGraphs.SparseMatrixCSC, Vector{Symbol}, Vector{Symbol}}
488+
varLabels = map(v->v.label, getVariables(dfg))
489+
factLabels = map(f->f.label, getFactors(dfg))
490+
491+
vDict = Dict(varLabels .=> [1:length(varLabels)...])
492+
493+
adjMat = spzeros(Int, length(factLabels), length(varLabels))
494+
495+
for (fIndex, factLabel) in enumerate(factLabels)
496+
factVars = getNeighbors(dfg, getFactor(dfg, factLabel))
497+
map(vLabel -> adjMat[fIndex,vDict[vLabel]] = 1, factVars)
498+
end
499+
return adjMat, varLabels, factLabels
500+
end
501+
487502
"""
488503
$(SIGNATURES)
489504
Produces a dot-format of the graph for visualization.

src/LightDFG/LightDFG.jl

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ import ...DistributedFactorGraphs: setSolverParams,
3333
getNeighbors,
3434
getSubgraphAroundNode,
3535
getSubgraph,
36-
getAdjacencyMatrix
36+
getAdjacencyMatrix,
37+
getAdjacencyMatrixSparse
3738

3839
include("FactorGraphs/FactorGraphs.jl")
3940
using .FactorGraphs
@@ -46,21 +47,5 @@ include("services/LightDFG.jl")
4647
# Exports
4748
export LightDFG
4849

49-
export exists
50-
export getLabelDict, getDescription, setDescription, getInnerGraph, getAddHistory, getSolverParams, setSolverParams
51-
#
52-
export getAddHistory, getDescription, getLabelDict
53-
export addVariable!, addFactor!
54-
export ls, lsf, getVariables, getFactors, getVariableIds, getFactorIds
55-
export getVariable, getFactor
56-
export updateVariable!, updateFactor!
57-
export deleteVariable!, deleteFactor!
58-
export getAdjacencyMatrix
59-
export getAdjacencyMatrixDataFrame
60-
export getNeighbors
61-
export getSubgraphAroundNode
62-
export getSubgraph
63-
export isFullyConnected, hasOrphans
64-
export toDot, toDotFile
6550

6651
end

src/LightDFG/services/LightDFG.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,18 @@ function getAdjacencyMatrix(dfg::LightDFG)::Matrix{Union{Nothing, Symbol}}
466466
return adjMat
467467
end
468468

469+
function getAdjacencyMatrixSparse(dfg::LightDFG)::Tuple{LightGraphs.SparseMatrixCSC, Vector{Symbol}, Vector{Symbol}}
470+
varLabels = collect(keys(dfg.g.variables))
471+
factLabels = collect(keys(dfg.g.factors))
472+
varIndex = [dfg.g.labels[s] for s in varLabels]
473+
factIndex = [dfg.g.labels[s] for s in factLabels]
474+
475+
adj = adjacency_matrix(dfg.g)
476+
477+
adjvf = adj[factIndex, varIndex]
478+
return adjvf, varLabels, factLabels
479+
end
480+
469481
#=
470482
"""
471483
$(SIGNATURES)

src/LightGraphsDFG/services/LightGraphsDFG.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,8 @@ Rows are all factors, columns are all variables, and each cell contains either n
510510
The first row and first column are factor and variable headings respectively.
511511
"""
512512
function getAdjacencyMatrix(dfg::LightGraphsDFG)::Matrix{Union{Nothing, Symbol}}
513-
varLabels = sort(map(v->v.label, getVariables(dfg)))
514-
factLabels = sort(map(f->f.label, getFactors(dfg)))
513+
varLabels = map(v->v.label, getVariables(dfg))
514+
factLabels = map(f->f.label, getFactors(dfg))
515515
vDict = Dict(varLabels .=> [1:length(varLabels)...].+1)
516516

517517
adjMat = Matrix{Union{Nothing, Symbol}}(nothing, length(factLabels)+1, length(varLabels)+1)
@@ -525,7 +525,7 @@ function getAdjacencyMatrix(dfg::LightGraphsDFG)::Matrix{Union{Nothing, Symbol}}
525525
return adjMat
526526
end
527527

528-
function getAdjacencyMatrixSparse(dfg::LightGraphsDFG)
528+
function getAdjacencyMatrixSparse(dfg::LightGraphsDFG)::Tuple{LightGraphs.SparseMatrixCSC, Vector{Symbol}, Vector{Symbol}}
529529
adj = LightGraphs.adjacency_matrix(dfg.g)
530530
v_labels = getVariableIds(dfg)
531531
f_labels = getFactorIds(dfg)

src/SymbolDFG/SymbolDFG.jl

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
module SymbolDFGs
22

33
using LightGraphs
4+
using SparseArrays
45
using DocStringExtensions
56

7+
import ...DistributedFactorGraphs: AbstractDFG, DFGNode, AbstractParams, NoSolverParams, DFGVariable, DFGFactor
68
# import DFG functions to exstend
79
import ...DistributedFactorGraphs: setSolverParams,
810
getInnerGraph,
@@ -31,7 +33,8 @@ import ...DistributedFactorGraphs: setSolverParams,
3133
getNeighbors,
3234
getSubgraphAroundNode,
3335
getSubgraph,
34-
getAdjacencyMatrix
36+
getAdjacencyMatrix,
37+
getAdjacencyMatrixSparse
3538

3639
include("SymbolFactorGraphs/SymbolFactorGraphs.jl")
3740
using .SymbolFactorGraphs
@@ -44,21 +47,5 @@ include("services/SymbolDFG.jl")
4447
# Exports
4548
export SymbolDFG
4649

47-
export exists
48-
export getLabelDict, getDescription, setDescription, getInnerGraph, getAddHistory, getSolverParams, setSolverParams
49-
#
50-
export getAddHistory, getDescription, getLabelDict
51-
export addVariable!, addFactor!
52-
export ls, lsf, getVariables, getFactors, getVariableIds, getFactorIds
53-
export getVariable, getFactor
54-
export updateVariable!, updateFactor!
55-
export deleteVariable!, deleteFactor!
56-
export getAdjacencyMatrix
57-
export getAdjacencyMatrixDataFrame
58-
export getNeighbors
59-
export getSubgraphAroundNode
60-
export getSubgraph
61-
export isFullyConnected, hasOrphans
62-
export toDot, toDotFile
6350

6451
end

src/SymbolDFG/entities/SymbolDFG.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11

2-
import ...DistributedFactorGraphs: AbstractDFG, DFGNode, AbstractParams, NoSolverParams, DFGVariable, DFGFactor
3-
4-
52
mutable struct SymbolDFG{T <: AbstractParams, V <: DFGNode, F <:DFGNode} <: AbstractDFG
63
g::SymbolFactorGraph{V,F}
74
description::String

src/SymbolDFG/services/SymbolDFG.jl

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import ...DistributedFactorGraphs: DFGVariable, DFGFactor
21

32
# Accessors
43
getLabelDict(dfg::SymbolDFG) = dfg.labelDict
@@ -461,8 +460,8 @@ Rows are all factors, columns are all variables, and each cell contains either n
461460
The first row and first column are factor and variable headings respectively.
462461
"""
463462
function getAdjacencyMatrix(dfg::SymbolDFG)::Matrix{Union{Nothing, Symbol}}
464-
varLabels = sort(map(v->v.label, getVariables(dfg)))
465-
factLabels = sort(map(f->f.label, getFactors(dfg)))
463+
varLabels = map(v->v.label, getVariables(dfg))
464+
factLabels = map(f->f.label, getFactors(dfg))
466465
vDict = Dict(varLabels .=> [1:length(varLabels)...].+1)
467466

468467
adjMat = Matrix{Union{Nothing, Symbol}}(nothing, length(factLabels)+1, length(varLabels)+1)
@@ -476,6 +475,21 @@ function getAdjacencyMatrix(dfg::SymbolDFG)::Matrix{Union{Nothing, Symbol}}
476475
return adjMat
477476
end
478477

478+
479+
function getAdjacencyMatrixSparse(dfg::SymbolDFG)::Tuple{LightGraphs.SparseMatrixCSC, Vector{Symbol}, Vector{Symbol}}
480+
varLabels = collect(keys(dfg.g.variables))
481+
factLabels = collect(keys(dfg.g.factors))
482+
483+
vDict = Dict(varLabels .=> [1:length(varLabels)...])
484+
485+
adjMat = spzeros(Int, length(factLabels), length(varLabels))
486+
487+
for (fIndex, factLabel) in enumerate(factLabels)
488+
factVars = outneighbors(dfg.g, factLabel)
489+
map(vLabel -> adjMat[fIndex,vDict[vLabel]] = 1, factVars)
490+
end
491+
return adjMat, varLabels, factLabels
492+
end
479493
#=
480494
"""
481495
$(SIGNATURES)

src/services/interfaces.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
3+
"""
4+
$(SIGNATURES)
5+
Get an adjacency matrix for the DFG, returned as a tuple: adjmat::SparseMatrixCSC{Int}, var_labels::Vector{Symbol) fac_labels::Vector{Symbol).
6+
Rows are the factors, columns are the variables, with the corresponding labels in fac_labels,var_labels.
7+
"""
8+
getAdjacencyMatrixSparse(dfg::AbstractDFG) = error("getAdjacencyMatrixSparse not implemented for $(typeof(dfg))")

0 commit comments

Comments
 (0)