Skip to content

Commit

Permalink
Merge pull request #120 from JuliaRobotics/feature/summaryfunction
Browse files Browse the repository at this point in the history
Summary functions
  • Loading branch information
GearsAD authored Oct 1, 2019
2 parents 6a4278a + f38ffd7 commit 9afd7a3
Show file tree
Hide file tree
Showing 25 changed files with 686 additions and 364 deletions.
5 changes: 3 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ JSON2 = "2535ab7d-5cd8-5a07-80ac-9b1792aadce3"
LightGraphs = "093fc24a-ae57-5d10-9952-331d41423f4d"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MetaGraphs = "626554b9-1ddb-594c-aa3c-2596fe9399a5"
Neo4j = "d2adbeaf-5838-5367-8a2f-e46d570981db"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Expand All @@ -29,6 +28,8 @@ julia = "0.7, 1"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
GraphPlot = "a2cc645c-3eea-5389-862e-a155d0052231"
Neo4j = "d2adbeaf-5838-5367-8a2f-e46d570981db"

[targets]
test = ["Test"]
test = ["Test", "GraphPlot", "Neo4j"]
6 changes: 6 additions & 0 deletions docs/src/func_ref.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ getSubgraphAroundNode
getSubgraph
```

### Summaries
```@docs
getSummary
getSummaryGraph
```

### Visualization and Plotting
```@docs
toDot
Expand Down
19 changes: 9 additions & 10 deletions src/DistributedFactorGraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,18 @@ using LinearAlgebra
using SparseArrays

# Entities
include("entities/AbstractTypes.jl")
include("entities/AbstractDFG.jl")
include("entities/DFGFactor.jl")
include("entities/DFGVariable.jl")
include("entities/AbstractDFGSummary.jl")

export AbstractDFG
export AbstractParams, NoSolverParams
export DFGNode

export DFGFactor
export DFGNode, DFGVariable, DFGFactor
export InferenceType, PackedInferenceType, FunctorInferenceType, InferenceVariable, ConvolutionObject

export FunctorSingleton, FunctorPairwise, FunctorPairwiseMinimize

export DFGVariable
export label, timestamp, tags, estimates, estimate, solverData, getData, solverDataDict, internalId, smallData, bigData
export setSolverData
export label, data, id
export label, timestamp, tags, estimates, estimate, data, solverData, getData, solverDataDict, setSolverData, internalId, smallData, bigData
export DFGVariableSummary, DFGFactorSummary, AbstractDFGSummary

# Services/AbstractDFG Exports
export hasFactor, hasVariable, isInitialized, getFactorFunction, isVariable, isFactor
Expand All @@ -45,9 +40,13 @@ export getAdjacencyMatrixSparse
# File import and export
export saveDFG, loadDFG

# Summary functions
export getSummary, getSummaryGraph

# Common includes
include("services/AbstractDFG.jl")
include("services/DFGVariable.jl")
include("services/DFGFactor.jl")

# Include the Graphs.jl API.
include("GraphsDFG/GraphsDFG.jl")
Expand Down
60 changes: 1 addition & 59 deletions src/GraphsDFG/services/GraphsDFG.jl
Original file line number Diff line number Diff line change
Expand Up @@ -366,68 +366,10 @@ function getSubgraphAroundNode(dfg::GraphsDFG{P}, node::T, distance::Int64=1, in
return addToDFG
end

"""
$(SIGNATURES)
Get an adjacency matrix for the DFG, returned as a Matrix{Union{Nothing, Symbol}}.
Rows are all factors, columns are all variables, and each cell contains either nothing or the symbol of the relating factor.
The first row and first column are factor and variable headings respectively.
"""
function getAdjacencyMatrix(dfg::GraphsDFG)::Matrix{Union{Nothing, Symbol}}
varLabels = sort(map(v->v.label, getVariables(dfg)))
factLabels = sort(map(f->f.label, getFactors(dfg)))
vDict = Dict(varLabels .=> [1:length(varLabels)...].+1)

adjMat = Matrix{Union{Nothing, Symbol}}(nothing, length(factLabels)+1, length(varLabels)+1)
# Set row/col headings
adjMat[2:end, 1] = factLabels
adjMat[1, 2:end] = varLabels
for (fIndex, factLabel) in enumerate(factLabels)
factVars = getNeighbors(dfg, getFactor(dfg, factLabel))
map(vLabel -> adjMat[fIndex+1,vDict[vLabel]] = factLabel, factVars)
end
return adjMat
end

function getAdjacencyMatrixSparse(dfg::GraphsDFG)::Tuple{LightGraphs.SparseMatrixCSC, Vector{Symbol}, Vector{Symbol}}
varLabels = map(v->v.label, getVariables(dfg))
factLabels = map(f->f.label, getFactors(dfg))

vDict = Dict(varLabels .=> [1:length(varLabels)...])

adjMat = spzeros(Int, length(factLabels), length(varLabels))

for (fIndex, factLabel) in enumerate(factLabels)
factVars = getNeighbors(dfg, getFactor(dfg, factLabel))
map(vLabel -> adjMat[fIndex,vDict[vLabel]] = 1, factVars)
end
return adjMat, varLabels, factLabels
end

"""
$(SIGNATURES)
Produces a dot-format of the graph for visualization.
"""
function toDot(dfg::GraphsDFG)::String
m = PipeBuffer()
write(m,Graphs.to_dot(dfg.g))
data = take!(m)
close(m)
return String(data)
end

"""
$(SIGNATURES)
Produces a dot file of the graph for visualization.
Download XDot to see the data
Note
- Default location "/tmp/dfg.dot" -- MIGHT BE REMOVED
- Can be viewed with the `xdot` system application.
- Based on graphviz.org
"""
function toDotFile(dfg::GraphsDFG, fileName::String="/tmp/dfg.dot")::Nothing
open(fileName, "w") do fid
write(fid,Graphs.to_dot(dfg.g))
end
return nothing
return Graphs.to_dot(dfg.g)
end
17 changes: 10 additions & 7 deletions src/LightDFG/FactorGraphs/FactorGraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,22 @@ export
filter_vertices,
reverse

import DistributedFactorGraphs: DFGNode
const AbstractNodeType = DFGNode
# import DistributedFactorGraphs: DFGNode
# const AbstractNodeType = DFGNode
import DistributedFactorGraphs: AbstractDFGVariable, AbstractDFGFactor
const AbstractVariableType = AbstractDFGVariable
const AbstractFactorType = AbstractDFGFactor

include("BiMaps.jl")

struct FactorGraph{T <: Integer,V <: AbstractNodeType, F <: AbstractNodeType} <: AbstractGraph{T}
struct FactorGraph{T <: Integer,V <: AbstractVariableType, F <: AbstractFactorType} <: AbstractGraph{T}
graph::SimpleGraph{T}
labels::BiDictMap{T}
variables::Dict{Symbol,V}
factors::Dict{Symbol,F}
end

function FactorGraph{T, V, F}(nv::Int=100, nf::Int=100) where {T <: Integer, V <: AbstractNodeType, F <: AbstractNodeType}
function FactorGraph{T, V, F}(nv::Int=100, nf::Int=100) where {T <: Integer, V <: AbstractVariableType, F <: AbstractFactorType}
fadjlist = Vector{Vector{T}}()
sizehint!(fadjlist, nv + nf)
g = SimpleGraph{T}(0, fadjlist)
Expand All @@ -55,10 +58,10 @@ function FactorGraph{T, V, F}(nv::Int=100, nf::Int=100) where {T <: Integer, V <
return FactorGraph{T, V, F}(g, labels, variables, factors)
end

# fg = FactorGraph{Int, AbstractNodeType, AbstractNodeType}()
# fg = FactorGraph{Int, AbstractVariableType, AbstractFactorType}()

FactorGraph() = FactorGraph{Int, AbstractNodeType, AbstractNodeType}()
FactorGraph{V,F}() where {V <: AbstractNodeType, F <: AbstractNodeType} = FactorGraph{Int, V, F}()
FactorGraph() = FactorGraph{Int, AbstractVariableType, AbstractFactorType}()
FactorGraph{V,F}() where {V <: AbstractVariableType, F <: AbstractFactorType} = FactorGraph{Int, V, F}()


function show(io::IO, g::FactorGraph)
Expand Down
4 changes: 2 additions & 2 deletions src/LightDFG/LightDFG.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module LightDFGs
using LightGraphs
using DocStringExtensions

import ...DistributedFactorGraphs: AbstractDFG, DFGNode, AbstractParams, NoSolverParams, DFGVariable, DFGFactor
import ...DistributedFactorGraphs: AbstractDFG, DFGNode, AbstractDFGVariable, AbstractDFGFactor, AbstractDFGSummary, AbstractParams, NoSolverParams, DFGVariable, DFGFactor

# import DFG functions to exstend
# import DFG functions to extend
import ...DistributedFactorGraphs: setSolverParams,
getInnerGraph,
getFactor,
Expand Down
32 changes: 25 additions & 7 deletions src/LightDFG/entities/LightDFG.jl
Original file line number Diff line number Diff line change
@@ -1,31 +1,49 @@

mutable struct LightDFG{T <: AbstractParams, V <: DFGNode, F <:DFGNode} <: AbstractDFG
"""
$(SIGNATURES)
An in-memory DistributedFactorGraph based on LightGraphs.jl with parameters:
- T: Solver parameters (defaults to `NoSolverParams()`)
- V: Variable type
- F: Factor type
"""
mutable struct LightDFG{T <: AbstractParams, V <: AbstractDFGVariable, F <:AbstractDFGFactor} <: AbstractDFG
g::FactorGraph{Int, V, F}
description::String
userId::String
robotId::String
sessionId::String
#NOTE does not exist
# nodeCounter::Int64
#NOTE does not exist
# labelDict::Dict{Symbol, Int64}
addHistory::Vector{Symbol} #TODO: Discuss more - is this an audit trail?
solverParams::T # Solver parameters
end

#TODO? do we not want props such as userId, robotId, sessionId, etc...
"""
$(SIGNATURES)
Create an in-memory LightDFG with the following parameters:
- T: Solver parameters (defaults to `NoSolverParams()`)
- V: Variable type
- F: Factor type
"""
function LightDFG{T,V,F}(g::FactorGraph{Int,V,F}=FactorGraph{Int,V,F}();
description::String="LightGraphs.jl implementation",
userId::String="User ID",
robotId::String="Robot ID",
sessionId::String="Session ID",
params::T=NoSolverParams()) where {T <: AbstractParams, V <:DFGNode, F<:DFGNode}
params::T=NoSolverParams()) where {T <: AbstractParams, V <:AbstractDFGVariable, F<:AbstractDFGFactor}

LightDFG{T,V,F}(g, description, userId, robotId, sessionId, Symbol[], params)
end

# LightDFG{T}(; kwargs...) where T <: AbstractParams = LightDFG{T,DFGVariable,DFGFactor}(;kwargs...)
"""
$(SIGNATURES)
Create an in-memory LightDFG with the following parameters:
- T: Solver parameters (defaults to `NoSolverParams()`)
- V: Variable type
- F: Factor type
"""
LightDFG{T}(g::FactorGraph{Int,DFGVariable,DFGFactor}=FactorGraph{Int,DFGVariable,DFGFactor}(); kwargs...) where T <: AbstractParams = LightDFG{T,DFGVariable,DFGFactor}(g; kwargs...)

Base.propertynames(x::LightDFG, private::Bool=false) =
Expand Down
Loading

0 comments on commit 9afd7a3

Please sign in to comment.