Skip to content

Commit 7bb56da

Browse files
committed
improving code layout
1 parent f56c33e commit 7bb56da

File tree

8 files changed

+314
-290
lines changed

8 files changed

+314
-290
lines changed

src/CliqueTypes.jl

Lines changed: 61 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,59 @@
11
# Clique types
22

3-
##==============================================================================
4-
## BayesTreeNodeData
5-
##==============================================================================
3+
4+
5+
# this is a developmental type, will be standardized after conclusion of #1010
6+
# TODO resolve type instability
7+
const MsgRelativeType = Vector{NamedTuple{(:variables, :likelihood), Tuple{Vector{Symbol},DFG.AbstractRelative}}}
8+
9+
const MsgPriorType = Dict{Symbol, MsgPrior{<:ManifoldKernelDensity}}
10+
11+
12+
"""
13+
$TYPEDEF
14+
15+
Internal development types used during consolidation. Stores relative and prior information making up a joint likelihood
16+
message passed upward on the Bayes tree.
17+
"""
18+
mutable struct _MsgJointLikelihood
19+
relatives::IIF.MsgRelativeType
20+
priors::IIF.MsgPriorType
21+
end
22+
23+
24+
"""
25+
$(TYPEDEF)
26+
Belief message for message passing on the tree. This should be considered an incomplete joint probility.
27+
28+
Notes:
29+
- belief -> Dictionary of [`TreeBelief`](@ref)
30+
- variableOrder -> Ordered variable id list of the seperators in cliqueLikelihood
31+
- cliqueLikelihood -> marginal distribution (<: `SamplableBelief`) over clique seperators.
32+
- Older names include: productFactor, Fnew, MsgPrior, LikelihoodMessage
33+
34+
DevNotes:
35+
- Used by both nonparametric and parametric.
36+
- Objective for parametric case: `MvNormal(μ=[:x0;:x2;:l5], Σ=[+ * *; * + *; * * +])`.
37+
- Part of the consolidation effort, see #459.
38+
- Better conditioning for joint structure in the works using deconvolution, see #579, #635.
39+
- TODO confirm why <: Singleton.
40+
41+
$(TYPEDFIELDS)
42+
"""
43+
mutable struct LikelihoodMessage{T <: MessageType} <: AbstractPrior
44+
sender::NamedTuple{(:id,:step),Tuple{Int,Int}}
45+
status::CliqStatus
46+
belief::Dict{Symbol, TreeBelief} # will eventually be deprecated
47+
variableOrder::Vector{Symbol}
48+
cliqueLikelihood::Union{Nothing,SamplableBelief} # TODO drop the Union
49+
msgType::T
50+
hasPriors::Bool
51+
# this is different from belief[].inferdim, as the total available infer dims remaining during down msgs -- see #910
52+
childSolvDims::Dict{Int, Float64}
53+
# calc differential factors for joint in the child clique
54+
jointmsg::_MsgJointLikelihood
55+
# diffJoints::Vector{NamedTuple{(:variables, :likelihood), Tuple{Vector{Symbol},DFG.AbstractRelative}}}
56+
end
657

758

859
"""
@@ -26,6 +77,11 @@ end
2677
MessageBuffer() = MessageBuffer(Dict{Int, LikelihoodMessage}(), nothing, nothing, nothing)
2778

2879

80+
##==============================================================================
81+
## BayesTreeNodeData
82+
##==============================================================================
83+
84+
2985
"""
3086
$(TYPEDEF)
3187
@@ -65,94 +121,6 @@ end
65121

66122

67123

68-
function BayesTreeNodeData(;status::CliqStatus=NULL,
69-
frontalIDs=Symbol[],
70-
separatorIDs=Symbol[],
71-
inmsgIDs=Symbol[],
72-
potIDs=Symbol[],
73-
potentials=Symbol[],
74-
partialpotential=Bool[],
75-
dwnPotentials=Symbol[],
76-
dwnPartialPotential=Bool[],
77-
cliqAssocMat=Array{Bool}(undef, 0,0),
78-
cliqMsgMat=Array{Bool}(undef, 0,0),
79-
directvarIDs=Int[],
80-
directFrtlMsgIDs=Int[],
81-
msgskipIDs=Int[],
82-
itervarIDs=Int[],
83-
directPriorMsgIDs=Int[],
84-
debug=nothing,
85-
debugDwn=nothing,
86-
allmarginalized=false,
87-
initialized=:NULL,
88-
upsolved=false,
89-
downsolved=false,
90-
isCliqReused=false,
91-
messages = MessageBuffer()
92-
)
93-
btnd = BayesTreeNodeData(status,
94-
frontalIDs,
95-
separatorIDs,
96-
inmsgIDs,
97-
potIDs,
98-
potentials,
99-
partialpotential,
100-
dwnPotentials,
101-
dwnPartialPotential,
102-
cliqAssocMat,
103-
cliqMsgMat,
104-
directvarIDs,
105-
directFrtlMsgIDs,
106-
msgskipIDs,
107-
itervarIDs,
108-
directPriorMsgIDs,
109-
debug,
110-
debugDwn,
111-
allmarginalized,
112-
initialized,
113-
upsolved,
114-
downsolved,
115-
isCliqReused,
116-
messages )
117-
#
118-
return btnd
119-
end
120-
#
121-
122-
123-
function compare( c1::BayesTreeNodeData,
124-
c2::BayesTreeNodeData;
125-
skip::Vector{Symbol}=[] )
126-
#
127-
TP = true
128-
129-
TP = TP && c1.frontalIDs == c2.frontalIDs
130-
TP = TP && c1.separatorIDs == c2.separatorIDs
131-
TP = TP && c1.inmsgIDs == c2.inmsgIDs
132-
TP = TP && c1.potIDs == c2.potIDs
133-
TP = TP && c1.potentials == c2.potentials
134-
TP = TP && c1.partialpotential == c2.partialpotential
135-
TP = TP && c1.dwnPotentials == c2.dwnPotentials
136-
TP = TP && c1.dwnPartialPotential == c2.dwnPartialPotential
137-
TP = TP && c1.cliqAssocMat == c2.cliqAssocMat
138-
TP = TP && c1.cliqMsgMat == c2.cliqMsgMat
139-
TP = TP && c1.directvarIDs == c2.directvarIDs
140-
TP = TP && c1.directFrtlMsgIDs == c2.directFrtlMsgIDs
141-
TP = TP && c1.msgskipIDs == c2.msgskipIDs
142-
TP = TP && c1.itervarIDs == c2.itervarIDs
143-
TP = TP && c1.directPriorMsgIDs == c2.directPriorMsgIDs
144-
TP = TP && c1.debug == c2.debug
145-
TP = TP && c1.debugDwn == c2.debugDwn
146-
TP = TP && c1.allmarginalized == c2.allmarginalized
147-
TP = TP && c1.initialized == c2.initialized
148-
TP = TP && c1.upsolved == c2.upsolved
149-
TP = TP && c1.downsolved == c2.downsolved
150-
TP = TP && c1.isCliqReused == c2.isCliqReused
151-
152-
return TP
153-
end
154-
155-
156124
## Packed types for serialization
157125

158126

@@ -175,61 +143,13 @@ mutable struct PackedBayesTreeNodeData
175143
end
176144

177145

178-
179-
function convert(::Type{PackedBayesTreeNodeData}, btnd::BayesTreeNodeData)
180-
return PackedBayesTreeNodeData(
181-
btnd.frontalIDs,
182-
btnd.separatorIDs,
183-
btnd.inmsgIDs,
184-
btnd.potIDs,
185-
btnd.potentials,
186-
btnd.partialpotential,
187-
btnd.dwnPotentials,
188-
btnd.dwnPartialPotential,
189-
btnd.cliqAssocMat,
190-
btnd.cliqMsgMat,
191-
btnd.directvarIDs,
192-
btnd.directFrtlMsgIDs,
193-
btnd.msgskipIDs,
194-
btnd.itervarIDs,
195-
btnd.directPriorMsgIDs )
196-
end
197-
198-
199-
function convert(::Type{BayesTreeNodeData}, pbtnd::PackedBayesTreeNodeData)
200-
btnd = BayesTreeNodeData()
201-
btnd.frontalIDs = pbtnd.frontalIDs
202-
btnd.separatorIDs = pbtnd.separatorIDs
203-
btnd.inmsgIDs = pbtnd.inmsgIDs
204-
btnd.potIDs = pbtnd.potIDs
205-
btnd.potentials = pbtnd.potentials
206-
btnd.partialpotential = pbtnd.partialpotential
207-
btnd.dwnPotentials = pbtnd.dwnPotentials
208-
btnd.dwnPartialPotential = pbtnd.dwnPartialPotential
209-
btnd.cliqAssocMat = pbtnd.cliqAssocMat
210-
btnd.cliqMsgMat = pbtnd.cliqMsgMat
211-
btnd.directvarIDs = pbtnd.directvarIDs
212-
btnd.directFrtlMsgIDs = pbtnd.directFrtlMsgIDs
213-
btnd.msgskipIDs = pbtnd.msgskipIDs
214-
btnd.itervarIDs = pbtnd.itervarIDs
215-
btnd.directPriorMsgIDs = pbtnd.directPriorMsgIDs
216-
return btnd
217-
end
218-
146+
## Full Clique Types
219147

220148

221-
##==============================================================================
222-
## Cliques
223-
## TreeClique
224-
##==============================================================================
225149
struct CliqueId{T}
226150
value::T
227151
end
228152

229-
Base.getindex(cId::CliqueId) = cId.value
230-
Base.show(io::IO, ::MIME"text/plain", x::CliqueId) = print(io, x.value)
231-
Base.show(io::IO, x::CliqueId) = print(io, x.value)
232-
233153
"""
234154
$(TYPEDEF)
235155
Structure to store clique data
@@ -246,34 +166,5 @@ mutable struct TreeClique
246166
#solveInProgress #on a clique level a "solve in progress" might be very handy
247167
end
248168

249-
getId(c::TreeClique) = c.id
250-
251-
function Base.getproperty(x::TreeClique,f::Symbol)
252-
if f == :index
253-
Base.depwarn("`TreeCliqe` field `index` is deprecated, use `id`", :getproperty)
254-
f = :id
255-
end
256-
getfield(x,f)
257-
end
258-
259-
function Base.setproperty!(x::TreeClique, f::Symbol, val)
260-
if f == :index
261-
Base.depwarn("`TreeCliqe` field `index` is deprecated, use `id`", :setproperty!)
262-
f = :id
263-
end
264-
return setfield!(x, f, convert(fieldtype(typeof(x), f), val))
265-
end
266-
267-
TreeClique(i::Int) = TreeClique(CliqueId(i), BayesTreeNodeData(), Dict{String,Any}())
268-
TreeClique(id::CliqueId) = TreeClique(id, BayesTreeNodeData(), Dict{String,Any}())
269-
270-
271-
DFG.getLabel(cliq::TreeClique) = cliq.attributes["label"]
272-
function setLabel!(cliq::TreeClique, lbl::String)
273-
cliq.attributes["label"] = lbl
274-
lbl
275-
end
276-
277-
278169

279-
## end Cliques
170+
#

src/Factors/MsgLikelihoods.jl

Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,5 @@
11

2-
"""
3-
$(TYPEDEF)
42

5-
Message prior on all dimensions of a variable node in the factor graph.
6-
7-
Notes
8-
- Only temporary existance during CSM operations.
9-
"""
10-
struct MsgPrior{T <: SamplableBelief} <: AbstractPrior
11-
Z::T
12-
inferdim::Float64
13-
end
14-
15-
# MsgPrior{T}() where {T} = new{T}()
16-
# MsgPrior{T}(z::T, infd::R) where {T <: SamplableBelief, R <: Real} = new{T}(z, infd)
17-
# function MsgPrior(z::T, infd::R) where {T <: SamplableBelief, R <: Real}
18-
# MsgPrior{T}(z, infd)
19-
# end
20-
21-
function getSample(cf::CalcFactor{<:MsgPrior}, N::Int=1)
22-
ret = [rand(cf.factor.Z,1) for _ in 1:N]
23-
(ret, )
24-
end
25-
26-
# MKD already returns a vector of points
27-
function getSample(cf::CalcFactor{<:MsgPrior{<:ManifoldKernelDensity}}, N::Int=1)
28-
ret = rand(cf.factor.Z,N)
29-
(ret, )
30-
end
31-
32-
getManifold(mp::MsgPrior{<:ManifoldKernelDensity}) = mp.Z.manifold
33-
34-
# this is a developmental type, will be standardized after conclusion of #1010
35-
# TODO resolve type instability
36-
const MsgRelativeType = Vector{NamedTuple{(:variables, :likelihood), Tuple{Vector{Symbol},DFG.AbstractRelative}}}
37-
38-
const MsgPriorType = Dict{Symbol, MsgPrior{<:ManifoldKernelDensity}}
39-
40-
41-
(cfo::CalcFactor{<:MsgPrior})(z, x1) = z .- x1
42-
43-
44-
45-
struct PackedMsgPrior <: PackedInferenceType
46-
Z::String
47-
inferdim::Float64
48-
end
49-
50-
function convert(::Type{PackedMsgPrior}, d::MsgPrior)
51-
PackedMsgPrior(convert(PackedSamplableBelief, d.Z), d.inferdim)
52-
end
53-
function convert(::Type{<:MsgPrior}, d::PackedMsgPrior)
54-
MsgPrior(convert(SamplableBelief, d.Z), d.inferdim)
55-
end
56-
57-
58-
59-
60-
61-
"""
62-
$TYPEDEF
63-
64-
Internal development types used during consolidation. Stores relative and prior information making up a joint likelihood
65-
message passed upward on the Bayes tree.
66-
"""
67-
mutable struct _MsgJointLikelihood
68-
relatives::IIF.MsgRelativeType
69-
priors::IIF.MsgPriorType
70-
end
713

724
_MsgJointLikelihood(;relatives::MsgRelativeType=MsgRelativeType(),priors::MsgPriorType=MsgPriorType() ) = _MsgJointLikelihood(relatives, priors)
735

@@ -91,39 +23,6 @@ end
9123
Base.show(io::IO, ::MIME"text/plain", x::_MsgJointLikelihood) = show(io, x)
9224

9325

94-
"""
95-
$(TYPEDEF)
96-
Belief message for message passing on the tree. This should be considered an incomplete joint probility.
97-
98-
Notes:
99-
- belief -> Dictionary of [`TreeBelief`](@ref)
100-
- variableOrder -> Ordered variable id list of the seperators in cliqueLikelihood
101-
- cliqueLikelihood -> marginal distribution (<: `SamplableBelief`) over clique seperators.
102-
- Older names include: productFactor, Fnew, MsgPrior, LikelihoodMessage
103-
104-
DevNotes:
105-
- Used by both nonparametric and parametric.
106-
- Objective for parametric case: `MvNormal(μ=[:x0;:x2;:l5], Σ=[+ * *; * + *; * * +])`.
107-
- Part of the consolidation effort, see #459.
108-
- Better conditioning for joint structure in the works using deconvolution, see #579, #635.
109-
- TODO confirm why <: Singleton.
110-
111-
$(TYPEDFIELDS)
112-
"""
113-
mutable struct LikelihoodMessage{T <: MessageType} <: AbstractPrior
114-
sender::NamedTuple{(:id,:step),Tuple{Int,Int}}
115-
status::CliqStatus
116-
belief::Dict{Symbol, TreeBelief} # will eventually be deprecated
117-
variableOrder::Vector{Symbol}
118-
cliqueLikelihood::Union{Nothing,SamplableBelief} # TODO drop the Union
119-
msgType::T
120-
hasPriors::Bool
121-
# this is different from belief[].inferdim, as the total available infer dims remaining during down msgs -- see #910
122-
childSolvDims::Dict{Int, Float64}
123-
# calc differential factors for joint in the child clique
124-
jointmsg::_MsgJointLikelihood
125-
# diffJoints::Vector{NamedTuple{(:variables, :likelihood), Tuple{Vector{Symbol},DFG.AbstractRelative}}}
126-
end
12726

12827

12928
LikelihoodMessage(; sender::NamedTuple{(:id,:step),Tuple{Int,Int}}=(;id=0, step=0),

0 commit comments

Comments
 (0)