Skip to content

Commit 4bf95c6

Browse files
authored
Merge pull request #1342 from JuliaRobotics/21Q3/enh/passthrufinal
fix propagateBelief N, more testing, passThru fix
2 parents d2f14b8 + 1b77d59 commit 4bf95c6

File tree

11 files changed

+251
-38
lines changed

11 files changed

+251
-38
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ TimeZones = "f269a46b-ccf7-5d73-abea-4c690281aa53"
4343
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
4444

4545
[compat]
46-
ApproxManifoldProducts = "0.4.13"
46+
ApproxManifoldProducts = "0.4.15"
4747
BSON = "0.2, 0.3"
4848
Combinatorics = "1.0"
4949
DataStructures = "0.16, 0.17, 0.18"

src/FactorGraph.jl

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,18 @@ function setValKDE!(vnd::VariableNodeData,
221221
setinit::Bool=true,
222222
inferdim::Union{Float32, Float64, Int32, Int64}=0 ) where {M,B,L<:AbstractVector}
223223
#
224-
oldbel = getBelief(vnd)
224+
oldBel = getBelief(vnd)
225+
226+
# New infomation might be partial
227+
newBel = replace(oldBel, mkd)
228+
225229
# Set partial dims as Manifold points
226-
ptsArr = AMP.getPoints(mkd, false)
227-
oldPts = getVal(vnd)
228-
# get partial coord dims
229-
pvec = mkd._partial
230+
ptsArr = AMP.getPoints(newBel, false)
230231

231-
# also set the bandwidth
232-
bws = getBW(mkd)[:,1]
233-
bw_ = getBW
232+
# also get the bandwidth
233+
bws = getBandwidth(newBel, false)
234+
235+
# update values in graph
234236
setValKDE!(vnd,ptsArr,bws,setinit,inferdim )
235237
nothing
236238
end

src/Factors/PartialPrior.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44
$(TYPEDEF)
55
66
Partial prior belief (absolute data) on any variable, given `<:SamplableBelief` and which dimensions of the intended variable.
7+
8+
Notes
9+
- If using [`AMP.ManifoldKernelDensity`](@ref), don't double partial. Only define the partial in this `PartialPrior` container.
10+
- Future TBD, consider using `AMP.getManifoldPartial` for more general abstraction.
711
"""
812
struct PartialPrior{T <: SamplableBelief,P <: Tuple} <: AbstractPrior
913
Z::T
1014
partial::P
1115
end
1216

13-
function getSample(cf::CalcFactor{<:PartialPrior}, N::Int=1)
14-
ret = Vector{Vector{Float64}}(undef, N)
15-
for i in 1:N
16-
ret[i] = rand(cf.factor.Z,1)[:]
17-
end
18-
(ret, )
19-
end
17+
getManifold(pp::PartialPrior{<:PackedManifoldKernelDensity}) = pp.Z.manifold
18+
19+
getSample(cf::CalcFactor{<:PartialPrior}, N::Int=1) = (randToPoints(cf.factor.Z,N), )
2020

2121

2222
"""

src/GraphProductOperations.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function propagateBelief( dfg::AbstractDFG,
1919
factors::AbstractVector{<:DFGFactor};
2020
solveKey::Symbol=:default,
2121
dens = Vector{ManifoldKernelDensity}(),
22-
N::Int=maximum([length(getPoints(getBelief(destvar, solveKey))); getSolverParams(dfg).N]),
22+
N::Int=getSolverParams(dfg).N, #maximum([length(getPoints(getBelief(destvar, solveKey))); getSolverParams(dfg).N]),
2323
needFreshMeasurements::Bool=true,
2424
dbg::Bool=false,
2525
logger=ConsoleLogger() )
@@ -45,7 +45,7 @@ function propagateBelief( dfg::AbstractDFG,
4545
# @info "BUILDING MKD" varType M
4646

4747
# take the product
48-
mkd = AMP.manifoldProduct(dens, M, Niter=1, oldPoints=oldpts)
48+
mkd = AMP.manifoldProduct(dens, M, Niter=1, oldPoints=oldpts, N=N)
4949

5050
# @info "GOT" mkd.manifold
5151

@@ -95,7 +95,7 @@ Return: product belief, full proposals, partial dimension proposals, labels
9595
function localProduct(dfg::AbstractDFG,
9696
sym::Symbol;
9797
solveKey::Symbol=:default,
98-
N::Int=maximum([length(getPoints(getBelief(dfg, sym, solveKey))); getSolverParams(dfg).N]),
98+
N::Int=getSolverParams(dfg).N, #maximum([length(getPoints(getBelief(dfg, sym, solveKey))); getSolverParams(dfg).N]),
9999
dbg::Bool=false,
100100
logger=ConsoleLogger() )
101101
#

src/IncrementalInference.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,8 @@ export *,
395395
reshapeVec2Mat,
396396
accumulateFactorChain
397397

398+
# more optional exports
399+
export HeatmapDensityRegular
398400

399401

400402
export buildCliqSubgraph_StateMachine

src/services/ApproxConv.jl

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,23 @@ function calcProposalBelief(dfg::AbstractDFG,
265265
#
266266

267267
# density passed through directly from PartialPriorPassThrough.Z
268-
proposal = getFactorType(fct).Z.densityFnc
268+
fctFnc = getFactorType(fct)
269+
proposal = fctFnc.Z.densityFnc
270+
271+
# in case of partial, place the proposal into larger marginal/partial MKD
272+
proposal_ = if isPartial(fctFnc)
273+
# oldbel = getBelief(dfg, target, solveKey)
274+
varType = getVariableType(dfg, target)
275+
M = getManifold(varType)
276+
u0 = getPointIdentity(varType)
277+
# replace(oldbel, proposal)
278+
antimarginal(M,u0,proposal,Int[fctFnc.partial...])
279+
else
280+
proposal
281+
end
269282

270283
# return the proposal belief and inferdim, NOTE likely to be changed
271-
return proposal
284+
return proposal_
272285
end
273286

274287
"""
@@ -287,7 +300,7 @@ function proposalbeliefs!(dfg::AbstractDFG,
287300
# partials::Dict{Any, Vector{ManifoldKernelDensity}}, # TODO change this structure
288301
measurement::Tuple=(Vector{Vector{Float64}}(),);
289302
solveKey::Symbol=:default,
290-
N::Int=maximum([length(getPoints(getBelief(dfg, destlbl, solveKey))); getSolverParams(dfg).N]),
303+
N::Int=getSolverParams(dfg).N, #maximum([length(getPoints(getBelief(dfg, destlbl, solveKey))); getSolverParams(dfg).N]),
291304
dbg::Bool=false )
292305
#
293306

@@ -302,7 +315,7 @@ function proposalbeliefs!(dfg::AbstractDFG,
302315
inferd = getFactorSolvableDim(dfg, fct, destlbl, solveKey)
303316
# convolve or passthrough to get a new proposal
304317
propBel_ = calcProposalBelief(dfg, fct, destlbl, measurement, N=N, dbg=dbg, solveKey=solveKey)
305-
# @show propBel_.manifold
318+
# @show propBel_.manifold, isPartial(propBel_)
306319
# partial density
307320
propBel = if isPartial(ccwl)
308321
pardims = _getDimensionsPartial(ccwl)

src/services/GraphInit.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ function doautoinit!( dfg::AbstractDFG,
8989
xi::DFGVariable;
9090
solveKey::Symbol=:default,
9191
singles::Bool=true,
92-
N::Int=maximum([length(getPoints(getBelief(xi, solveKey))); getSolverParams(dfg).N]),
92+
N::Int=getSolverParams(dfg).N, #maximum([length(getPoints(getBelief(xi, solveKey))); getSolverParams(dfg).N]),
9393
logger=ConsoleLogger() )
9494
#
9595
didinit = false
@@ -122,9 +122,11 @@ function doautoinit!( dfg::AbstractDFG,
122122
@info "do init of $vsym"
123123
end
124124
# FIXME ensure a product of only partial densities and returned pts are put to proper dimensions
125-
bel,inferdim = propagateBelief(dfg, getVariable(dfg,vsym), getFactor.(dfg,useinitfct), solveKey=solveKey, logger=logger)
126-
# @info "MANIFOLD IS" bel.manifold string(getPoints(bel, false)[1])
127-
setValKDE!(xi, bel, true, inferdim, solveKey=solveKey) # getPoints(bel, false)
125+
bel,inferdim = propagateBelief(dfg, getVariable(dfg,vsym), getFactor.(dfg,useinitfct), solveKey=solveKey, logger=logger, N=N)
126+
# while the propagate step might allow large point counts, the graph should stay restricted to N
127+
bel_ = Npts(bel) == getSolverParams(dfg).N ? bel : resample(bel, getSolverParams(dfg).N)
128+
# @info "MANIFOLD IS" bel.manifold isPartial(bel) string(bel._partial) string(getPoints(bel, false)[1])
129+
setValKDE!(xi, bel_, true, inferdim, solveKey=solveKey) # getPoints(bel, false)
128130
# Update the estimates (longer DFG function used so cloud is also updated)
129131
setVariablePosteriorEstimates!(dfg, xi.label, solveKey)
130132
# Update the data in the event that it's not local

src/services/HeatmapSampler.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
@info "Including Interpolations related functions in IncrementalInference."
44

5-
# using Interpolations
5+
using .Interpolations
66

77
# only export on Requires.jl
88
export HeatmapDensityRegular
@@ -72,7 +72,7 @@ function HeatmapDensityRegular( data::AbstractMatrix{<:Real},
7272
# constuct a pre-density from which to draw intermediate samples
7373
density_ = fitKDE(support_, weights_, domain...; bw_factor=bw_factor)
7474
pts_preIS, = sample(density_, N)
75-
@show size(pts_preIS)
75+
# @show size(pts_preIS)
7676

7777
@cast vec_preIS[j][i] := pts_preIS[i,j]
7878

test/testBasicGraphs.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,4 @@ end
355355

356356

357357

358-
359-
360358
#

test/testPartialPrior.jl

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using Test
44
using IncrementalInference
5+
using Manifolds
56

67
##
78

@@ -20,17 +21,20 @@ getSample(cfo::CalcFactor{<:PartialDim2}, N::Int=1) = ([rand(cfo.factor.Z, 1)[:]
2021
##
2122

2223
@testset "Test partial dimensions on prior are correct" begin
23-
2424
##
2525

2626
fg = initfg()
2727

28-
addVariable!(fg, :x0, ContinuousEuclid{2})
28+
v0 = addVariable!(fg, :x0, ContinuousEuclid{2})
2929

3030
f0 = addFactor!(fg, [:x0], PartialDim2(Normal()))
3131

3232
@test IIF._getDimensionsPartial(f0) == [2]
3333

34+
bel, infd = propagateBelief(fg, v0, [f0;])
35+
36+
@test isPartial(bel)
37+
3438
##
3539

3640
dens = Vector{ManifoldKernelDensity}()
@@ -44,5 +48,35 @@ predictbelief(fg, :x0, [:x0f1;])
4448
@test true
4549

4650
##
51+
end
52+
53+
54+
55+
@testset "test propagateBelief returning a partial" begin
56+
##
57+
58+
fg = initfg()
59+
60+
v0 = addVariable!(fg, :x0, ContinuousEuclid{2})
4761

48-
end
62+
pts = [randn(1) for _ in 1:1000];
63+
mkd = manikde!(TranslationGroup(1), pts, bw=[0.1;])
64+
pp = PartialPrior(mkd, (2,))
65+
f0 = addFactor!(fg, [:x0;], pp, graphinit=false)
66+
67+
##
68+
69+
bel, infd = propagateBelief(fg, v0, [f0;])
70+
@test isPartial(bel)
71+
72+
##
73+
74+
doautoinit!(fg, :x0)
75+
76+
# check the number of points in the graph value store
77+
@show getSolverParams(fg).N
78+
@test length(getPoints(getBelief(fg, :x0))) == getSolverParams(fg).N
79+
@info "PassThrough factors currently work different and will pass the full N=1000 count through to the graph."
80+
81+
##
82+
end

0 commit comments

Comments
 (0)