Skip to content

Commit aaa6fd0

Browse files
authored
Merge pull request #14 from JuliaRobotics/feature/dfgintegration
Feature/dfgintegration for IncrementalInference
2 parents fcd5247 + 7da9519 commit aaa6fd0

File tree

9 files changed

+251
-30
lines changed

9 files changed

+251
-30
lines changed

Project.toml

Lines changed: 1 addition & 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.0.0"
3+
version = "0.0.1"
44

55
[deps]
66
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"

src/DistributedFactorGraphs.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,18 @@ export DFGNode
1616
export DFGFactor
1717
export DFGVariable
1818
export label, timestamp, tags, estimates, estimate, solverData, solverDataDict, id, smallData, bigData
19+
export setSolverData
1920
export label, data, id
2021

22+
# Solver (IIF) Exports
23+
export VariableNodeData, PackedVariableNodeData
24+
export GenericFunctionNodeData#, FunctionNodeData
25+
export getSerializationModule, setSerializationModule!
26+
export pack, unpack
27+
2128
# Include the Graphs.jl API.
2229
include("services/GraphsDFG.jl")
30+
include("services/AbstractDFG.jl")
31+
include("services/DFGVariable.jl")
2332

2433
end

src/entities/DFGFactor.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,18 @@ Fundamental structure for a DFG factor.
2222
"""
2323
mutable struct DFGFactor{T, S} <: DFGNode
2424
label::Symbol
25+
tags::Vector{Symbol}
2526
data::GenericFunctionNodeData{T, S}
27+
ready::Int
28+
backendset::Int
2629
_internalId::Int64
27-
DFGFactor{T, S}(label::Symbol) where {T, S} = new{T, S}(label, GenericFunctionNodeData{T, S}(), 0)
28-
DFGFactor{T, S}(label::Symbol, _internalId::Int64) where {T, S} = new{T, S}(label, GenericFunctionNodeData{T, S}(), _internalId)
30+
DFGFactor{T, S}(label::Symbol) where {T, S} = new{T, S}(label, Symbol[], GenericFunctionNodeData{T, S}(), 0, 0, 0)
31+
DFGFactor{T, S}(label::Symbol, _internalId::Int64) where {T, S} = new{T, S}(label, Symbol[], GenericFunctionNodeData{T, S}(), 0, 0, _internalId)
2932
end
3033

34+
# const FunctionNodeData{T} = GenericFunctionNodeData{T, Symbol}
35+
# FunctionNodeData(x1, x2, x3, x4, x5::Symbol, x6::T, x7::String="", x8::Vector{Int}=Int[]) where {T <: Union{FunctorInferenceType, ConvolutionObject}}= GenericFunctionNodeData{T, Symbol}(x1, x2, x3, x4, x5, x6, x7, x8)
36+
3137
label(f::F) where F <: DFGFactor = f.label
3238
data(f::F) where F <: DFGFactor = f.data
3339
id(f::F) where F <: DFGFactor = f._internalId

src/entities/DFGVariable.jl

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ $(TYPEDEF)
44
mutable struct VariableNodeData
55
val::Array{Float64,2}
66
bw::Array{Float64,2}
7-
BayesNetOutVertIDs::Array{Int,1}
7+
BayesNetOutVertIDs::Array{Symbol,1}
88
dimIDs::Array{Int,1} # Likely deprecate
99
dims::Int
1010
eliminated::Bool
11-
BayesNetVertID::Int
12-
separator::Array{Int,1}
11+
BayesNetVertID::Union{Nothing, Symbol}
12+
separator::Array{Symbol,1}
1313
groundtruth::Union{Nothing, Dict{ Tuple{Symbol, Vector{Float64}} } } # not packed yet
1414
softtype
1515
initialized::Bool
16+
partialinit::Bool
1617
ismargin::Bool
1718
dontmargin::Bool
1819
VariableNodeData() = new()
@@ -22,18 +23,57 @@ mutable struct VariableNodeData
2223
# end
2324
VariableNodeData(x1::Array{Float64,2},
2425
x2::Array{Float64,2},
25-
x3::Vector{Int},
26+
x3::Vector{Symbol},
2627
x4::Vector{Int},
2728
x5::Int,
2829
x6::Bool,
29-
x7::Int,
30-
x8::Vector{Int},
30+
x7::Union{Nothing, Symbol},
31+
x8::Vector{Symbol},
3132
x9::Union{Nothing, Dict{ Tuple{Symbol, Vector{Float64}} } },
3233
x10,
3334
x11::Bool,
3435
x12::Bool,
35-
x13::Bool) =
36-
new(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13)
36+
x13::Bool,
37+
x14::Bool) =
38+
new(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14)
39+
end
40+
41+
"""
42+
$(TYPEDEF)
43+
"""
44+
mutable struct PackedVariableNodeData
45+
vecval::Array{Float64,1}
46+
dimval::Int
47+
vecbw::Array{Float64,1}
48+
dimbw::Int
49+
BayesNetOutVertIDs::Array{Int,1}
50+
dimIDs::Array{Int,1}
51+
dims::Int
52+
eliminated::Bool
53+
BayesNetVertID::Int
54+
separator::Array{Int,1}
55+
# groundtruth::NothingUnion{ Dict{ Tuple{Symbol, Vector{Float64}} } }
56+
softtype::String
57+
initialized::Bool
58+
partialinit::Bool
59+
ismargin::Bool
60+
dontmargin::Bool
61+
PackedVariableNodeData() = new()
62+
PackedVariableNodeData(x5::Vector{Float64},
63+
x6::Int,
64+
x7::Vector{Float64},
65+
x8::Int,
66+
x9::Vector{Int},
67+
x10::Vector{Int},
68+
x11::Int,
69+
x12::Bool,
70+
x13::Int,
71+
x14::Vector{Int},
72+
x15::String,
73+
x16::Bool,
74+
x17::Bool,
75+
x18::Bool,
76+
x19::Bool ) = new(x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19)
3777
end
3878

3979
struct VariableEstimate
@@ -54,9 +94,11 @@ mutable struct DFGVariable <: DFGNode
5494
solverDataDict::Dict{Symbol, VariableNodeData}
5595
smallData::Any
5696
bigData::Any
97+
ready::Int
98+
backendset::Int
5799
_internalId::Int64
58-
DFGVariable(label::Symbol, _internalId::Int64) = new(label, now(), Symbol[], Dict{Symbol, VariableEstimate}(), Dict{Symbol, VariableNodeData}(:default => VariableNodeData()), nothing, nothing, _internalId)
59-
DFGVariable(label::Symbol) = new(label, now(), Symbol[], Dict{Symbol, VariableEstimate}(), Dict{Symbol, VariableNodeData}(:default => VariableNodeData()), nothing, nothing, 0)
100+
DFGVariable(label::Symbol, _internalId::Int64) = new(label, now(), Symbol[], Dict{Symbol, VariableEstimate}(), Dict{Symbol, VariableNodeData}(:default => VariableNodeData()), nothing, nothing, 0, 0, _internalId)
101+
DFGVariable(label::Symbol) = new(label, now(), Symbol[], Dict{Symbol, VariableEstimate}(), Dict{Symbol, VariableNodeData}(:default => VariableNodeData()), nothing, nothing, 0, 0, 0)
60102
end
61103

62104
# Accessors
@@ -67,6 +109,7 @@ estimates(v::DFGVariable) = v.estimateDict
67109
estimate(v::DFGVariable, key::Symbol=:default) = haskey(v.estimateDict, key) ? v.estimateDict[key] : nothing
68110
#solverData(v::DFGVariable) = haskey(v.solverDataDict, :default) ? v.solverDataDict[:default] : nothing
69111
solverData(v::DFGVariable, key::Symbol=:default) = haskey(v.solverDataDict, key) ? v.solverDataDict[key] : nothing
112+
setSolverData(v::DFGVariable, data::VariableNodeData, key::Symbol=:default) = v.solverDataDict[key] = data
70113
solverDataDict(v::DFGVariable) = v.solverDataDict
71114
id(v::DFGVariable) = v._internalId
72115
# Todo: Complete this.

src/services/AbstractDFG.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
"""
3+
$(SIGNATURES)
4+
5+
De-serialization of IncrementalInference objects require discovery of foreign types.
6+
7+
Example:
8+
9+
Template to tunnel types from a user module:
10+
```julia
11+
# or more generic solution -- will always try Main if available
12+
IIF.setSerializationNamespace!(Main)
13+
14+
# or a specific package such as RoME if you import all variable and factor types into a specific module.
15+
using RoME
16+
IIF.setSerializationNamespace!(RoME)
17+
```
18+
"""
19+
function setSerializationModule!(dfg::G, mod::Module)::Nothing where G <: AbstractDFG
20+
@warn "Setting serialization module from AbstractDFG - override this in the '$(typeof(dfg)) structure! This is being ignored."
21+
end
22+
23+
function getSerializationModule(dfg::G)::Module where G <: AbstractDFG
24+
@warn "Retrieving serialization module from AbstractDFG - override this in the '$(typeof(dfg)) structure! This is returning Main"
25+
return Main
26+
end

src/services/DFGFactor.jl

Whitespace-only changes.

src/services/DFGVariable.jl

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using Base
2+
import Base.==
3+
4+
function pack(dfg::Union{G, Nothing}, d::VariableNodeData)::PackedVariableNodeData where G <: AbstractDFG
5+
@debug "Dispatching conversion variable -> packed variable for type $(string(d.softtype))"
6+
return PackedVariableNodeData(d.val[:],size(d.val,1),
7+
d.bw[:], size(d.bw,1),
8+
d.BayesNetOutVertIDs,
9+
d.dimIDs, d.dims, d.eliminated,
10+
d.BayesNetVertID, d.separator,
11+
string(d.softtype), d.initialized, d.partialinit, d.ismargin, d.dontmargin)
12+
end
13+
14+
function unpack(dfg::Union{G, Nothing}, d::PackedVariableNodeData)::VariableNodeData where G <: AbstractDFG
15+
r3 = d.dimval
16+
c3 = r3 > 0 ? floor(Int,length(d.vecval)/r3) : 0
17+
M3 = reshape(d.vecval,r3,c3)
18+
19+
r4 = d.dimbw
20+
c4 = r4 > 0 ? floor(Int,length(d.vecbw)/r4) : 0
21+
M4 = reshape(d.vecbw,r4,c4)
22+
23+
# TODO -- allow out of module type allocation (future feature, not currently in use)
24+
@debug "Dispatching conversion packed variable -> variable for type $(string(d.softtype))"
25+
st = nothing #IncrementalInference.ContinuousMultivariate # eval(parse(d.softtype))
26+
mainmod = getSerializationModule(dfg)
27+
mainmod == nothing && error("Serialization module is null - please call setSerializationNamespace!(\"Main\" => Main) in your main program.")
28+
try
29+
unpackedTypeName = split(d.softtype, "(")[1]
30+
unpackedTypeName = split(unpackedTypeName, '.')[end]
31+
@debug "DECODING Softtype = $unpackedTypeName"
32+
st = getfield(mainmod, Symbol(unpackedTypeName))()
33+
catch ex
34+
@error "Unable to deserialize soft type $(d.softtype)"
35+
io = IOBuffer()
36+
showerror(io, ex, catch_backtrace())
37+
err = String(take!(io))
38+
@error err
39+
end
40+
@debug "Net conversion result: $st"
41+
42+
return VariableNodeData(M3,M4, d.BayesNetOutVertIDs,
43+
d.dimIDs, d.dims, d.eliminated, d.BayesNetVertID, d.separator,
44+
nothing, st, d.initialized, d.partialinit, d.ismargin, d.dontmargin )
45+
end
46+
47+
function compare(a::VariableNodeData,b::VariableNodeData)
48+
TP = true
49+
TP = TP && a.val == b.val
50+
TP = TP && a.bw == b.bw
51+
TP = TP && a.BayesNetOutVertIDs == b.BayesNetOutVertIDs
52+
TP = TP && a.dimIDs == b.dimIDs
53+
TP = TP && a.dims == b.dims
54+
TP = TP && a.eliminated == b.eliminated
55+
TP = TP && a.BayesNetVertID == b.BayesNetVertID
56+
TP = TP && a.separator == b.separator
57+
TP = TP && a.partialinit == b.partialinit
58+
TP = TP && a.ismargin == b.ismargin
59+
TP = TP && a.softtype == b.softtype
60+
return TP
61+
end
62+
63+
function ==(a::VariableNodeData,b::VariableNodeData, nt::Symbol=:var)
64+
return DistributedFactorGraphs.compare(a,b)
65+
end

0 commit comments

Comments
 (0)