Skip to content

Commit b88f04c

Browse files
committed
change to hyporecipe and better names
1 parent 2ff7bc5 commit b88f04c

File tree

6 files changed

+86
-48
lines changed

6 files changed

+86
-48
lines changed

src/DeconvUtils.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ function approxDeconv(fcto::DFGFactor,
6767
# build a lambda that incorporates the multihypo selections
6868
# set these first
6969
# ccw.cpt[].activehypo / .p / .params # params should already be set from construction
70-
certainidx, allelements, activehypo, mhidx = assembleHypothesesElements!(nothing, N, 0, length(varsyms))
70+
hyporecipe = _prepareHypoRecipe!(nothing, N, 0, length(varsyms))
71+
# Juila 1.7 allows destructure assign `(;a,b) = namedtype`
72+
# certainidx, allelements, activehypo, mhidx =
7173
# only doing the current active hypo
72-
@assert activehypo[2][1] == 1 "deconv was expecting hypothesis nr == (1, 1:d)"
74+
@assert hyporecipe.activehypo[2][1] == 1 "deconv was expecting hypothesis nr == (1, 1:d)"
7375

7476
islen1 = zDim == 1
7577

@@ -80,7 +82,7 @@ function approxDeconv(fcto::DFGFactor,
8082
targeti_ = makeTarget(idx)
8183

8284
# TODO must first resolve hypothesis selection before unrolling them -- deferred #1096
83-
cpt_.activehypo = activehypo[2][2]
85+
cpt_.activehypo = hyporecipe.activehypo[2][2]
8486

8587
onehypo!, _ = _buildCalcFactorLambdaSample( ccw,
8688
idx,

src/ExplicitDiscreteMarginalizations.jl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ allelements=
6363
[3, 4, 5, ...]
6464
activehypo=
6565
3-element Array{Any,1}:
66-
(0, [2]) # nullhypo -- forced afterward, likely to be deprecated in this case
66+
(0, [2]) # nullhypo -- forced afterward, might be deprecated if better solution is found
6767
(1, [1, 2]) # unroll hypo lambda for X,La
6868
(2, [1, 2]) # unroll hypo lambda for X,La
6969
(3, [2, 3]) # would be (but cannot) unroll hypo La,Lb # almost our nullhypo # wont build a lambda
@@ -84,9 +84,10 @@ Notes:
8484
- Issue 427, race condition during initialization since n-ary variables not resolvable without other init.
8585
8686
DevNotes
87-
- Improved implementations should implicitly induce the same behaviour through summation (integration) when marginalizing any number of discrete variables.
87+
- FIXME convert into some kind of `HypoRecipe` struct, to improve readibility and code maintainability
8888
- TODO add nullhypo cases to returning result
8989
- FIXME make type-stable `activehypo` and others
90+
- Improved implementations should implicitly induce the same behaviour through summation (integration) when marginalizing any number of discrete variables.
9091
9192
```
9293
# `allelements` example BearingRange [:x1, 0.5:l1a, 0.5:l1b]
@@ -135,7 +136,7 @@ sfidx=3, allelements=allidx[nhidx.==0], activehypo=(0,[3;])
135136
136137
TODO still need to compensate multihypo case for user nullhypo addition.
137138
"""
138-
function assembleHypothesesElements!( mh::Categorical,
139+
function _prepareHypoRecipe!( mh::Categorical,
139140
maxlen::Int,
140141
sfidx::Int,
141142
lenXi::Int,
@@ -220,11 +221,12 @@ function assembleHypothesesElements!( mh::Categorical,
220221
# #
221222
# end
222223

223-
return certainidx, allelements, activehypo, mhidx
224+
# hyporecipe::NamedTuple
225+
return (;certainidx, allelements, activehypo, mhidx)
224226
end
225227

226228

227-
function assembleHypothesesElements!( mh::Nothing,
229+
function _prepareHypoRecipe!( mh::Nothing,
228230
maxlen::Int,
229231
sfidx::Int,
230232
lenXi::Int,
@@ -274,7 +276,8 @@ function assembleHypothesesElements!( mh::Nothing,
274276
end
275277
end
276278

277-
return certainidx, allelements, activehypo, mhidx
279+
# return hyporecipe::NamedTuple
280+
return (;certainidx, allelements, activehypo, mhidx)
278281
end
279282

280283

src/NumericalCalculations.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ function _buildCalcFactorMixture( ccwl::CommonConvWrapper{Mixture{N_,F,S,T}},
155155
length(measurement_), measurement_, varParams)
156156
end
157157

158+
159+
158160
"""
159161
$SIGNATURES
160162
Internal function to build lambda pre-objective function for finding factor residuals.

src/services/ApproxConv.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ end
168168
function calcProposalBelief(dfg::AbstractDFG,
169169
fct::DFGFactor{<:CommonConvWrapper{<:PartialPriorPassThrough}},
170170
target::Symbol,
171-
measurement::Tuple=(zeros(0,0),);
171+
measurement::Tuple=(Vector{Vector{Float64}}(),);
172172
N::Int=length(measurement[1]),
173173
solveKey::Symbol=:default,
174174
dbg::Bool=false )

src/services/EvalFactor.jl

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,30 @@ end
230230

231231

232232

233+
# function _calcIPCRelative(activeVars::AbstractVector{<:DFGVariable},
234+
# sfidx_active::Integer,
235+
# meas_pts...;
236+
# )
237+
# #
238+
# ipc = if ccwl._gradients === nothing
239+
# ones(getDimension(activeVars[sfidx_active]))
240+
# else
241+
# ipc_ = []
242+
# # get infoPerCoord from all variables
243+
# for (vid,var) in enumerate(activeVars)
244+
# # set all other variables infoPerCoord values
245+
# getLabel(var) != getLabel(activeVars[sfidx_active]) ? nothing : continue
246+
# push!(ipc_, vid=>ones(getDimension(var)))
247+
# end
248+
# # update the gradients at current point estimates
249+
# meas_pts =
250+
# ccwl._gradients(meas_pts)
251+
# # do perturbation check
252+
# allipc = calcPerturbationFromVariable(ccwl._gradients, ipc_)
253+
# allipc[sfidx_masked]
254+
# end
255+
256+
# end
233257

234258
"""
235259
$(SIGNATURES)
@@ -265,30 +289,36 @@ function evalPotentialSpecific( Xi::AbstractVector{<:DFGVariable},
265289
isinit = map(x->isInitialized(x), Xi)
266290

267291
# assemble how hypotheses should be computed
268-
# TODO convert to HypothesisRecipeElements result
269-
_, allelements, activehypo, mhidx = assembleHypothesesElements!(ccwl.hypotheses, maxlen, sfidx, length(Xi), isinit, ccwl.nullhypo )
270-
certainidx = ccwl.certainhypo
292+
hyporecipe = _prepareHypoRecipe!(ccwl.hypotheses, maxlen, sfidx, length(Xi), isinit, ccwl.nullhypo )
271293

272294
# get manifold add operations
273295
# TODO, make better use of dispatch, see JuliaRobotics/RoME.jl#244
274296
# addOps, d1, d2, d3 = buildHybridManifoldCallbacks(manis)
275297
mani = getManifold(getVariableType(Xi[sfidx]))
276-
298+
277299
# perform the numeric solutions on the indicated elements
278300
# FIXME consider repeat solve as workaround for inflation off-zero
279-
computeAcrossHypothesis!( ccwl, allelements, activehypo, certainidx,
301+
# FIXME figure out why certainidx is not used from the hyporecipe
302+
certainidx = ccwl.certainhypo
303+
@assert certainidx == hyporecipe.certainidx "expected hyporecipe.certainidx to be the same as cached in ccw"
304+
computeAcrossHypothesis!( ccwl, hyporecipe.allelements, hyporecipe.activehypo, certainidx,
280305
sfidx, maxlen, mani, spreadNH=spreadNH,
281306
inflateCycles=inflateCycles, skipSolve=skipSolve,
282307
_slack=_slack )
283308
#
284-
# do info per coord
285-
ipc = if ccwl._gradients === nothing
286-
ones(getDimension(Xi[sfidx]))
287-
else
288-
ipc_ = ones(getDimension(Xi[sfidx]))
289-
# calcPerturbationFromVariable(ccwl._gradients, 2, ipc_) # TODO, WIP
290-
ipc_ # TODO, WIP
291-
end
309+
# ## do info per coord
310+
# @assert hyporecipe.activehypo[1][1] === 0 "expected 0-hypo case in hyporecipe.activehypo, to get variable hypo mask for relative partial propagation calculations."
311+
# # select only the active variables in case of multihypo
312+
# activeids = hyporecipe.activehypo[hyporecipe.mhidx[smpid]+1][2]
313+
# # solvefor index without the fractional variables
314+
# active_mask = (s->x in activeids).(1:length(Xi))
315+
# sfidx_active = sum(active_mask[1:sfidx])
316+
# # build a view to the decision variable memory
317+
# # activeVars = Xi[active_mask]
318+
# smpid = 1 # assume gradients are just done for the first sample values
319+
# meas_pts = tuple((_getindextuple(ccwl.measurement, smpid))..., (getindex.(varParams, smpid))...)
320+
# ipc = _calcIPCRelative(activeVars, )
321+
ipc = ones(getDimension(Xi[sfidx]))
292322

293323
# return the found points, and info per coord
294324
return ccwl.params[ccwl.varidx], ipc
@@ -331,12 +361,13 @@ function evalPotentialSpecific( Xi::AbstractVector{<:DFGVariable},
331361

332362
# Check which variables have been initialized, TODO not sure why forcing to Bool vs BitVector
333363
isinit::Vector{Bool} = Xi .|> isInitialized .|> Bool
334-
_, _, _, mhidx = assembleHypothesesElements!(ccwl.hypotheses, nn, sfidx, length(Xi), isinit, ccwl.nullhypo )
364+
hyporecipe = _prepareHypoRecipe!(ccwl.hypotheses, nn, sfidx, length(Xi), isinit, ccwl.nullhypo )
365+
335366
# get solvefor manifolds, FIXME ON FIRE, upgrade to new Manifolds.jl
336367
mani = getManifold(Xi[sfidx])
337368
# two cases on how to use the measurement
338-
nhmask = mhidx .== 0
339-
ahmask = mhidx .== 1
369+
nhmask = hyporecipe.mhidx .== 0
370+
ahmask = hyporecipe.mhidx .== 1
340371
# generate nullhypo samples
341372
# inject lots of entropy in nullhypo case
342373
# make spread (1σ) equal to mean distance of other fractionals

test/testExplicitMultihypo.jl

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ using Test
55
using IncrementalInference
66

77

8-
@testset "test IncrementalInference.assembleHypothesesElements! with only nullhypothesis..." begin
8+
@testset "test IncrementalInference._prepareHypoRecipe! with only nullhypothesis..." begin
99

1010
##
1111

@@ -14,7 +14,7 @@ n2_1_gt1 = 1:2
1414
n2_1_gt2_ = (3,3,0)
1515
n2_1_gt3 = [(0,Int[1;]); (1,1:2); (2,Int[])]
1616
n2_1_gt4_ = 20
17-
n2_1 = IncrementalInference.assembleHypothesesElements!(nothing, 20, 1, 2, ones(Bool, 2), 0.5 )
17+
n2_1 = IncrementalInference._prepareHypoRecipe!(nothing, 20, 1, 2, ones(Bool, 2), 0.5 )
1818

1919
@test sum([n2_1_gt1;] - [n2_1[1];]) == 0
2020
@test length(n2_1[2][1]) > n2_1_gt2_[1]
@@ -40,7 +40,7 @@ n2_1_gt1 = 1:2
4040
n2_1_gt2_ = (3,3,0)
4141
n2_1_gt3 = [(0,Int[2;]); (1,1:2); (2,Int[])]
4242
n2_1_gt4_ = 20
43-
n2_1 = IncrementalInference.assembleHypothesesElements!(nothing, 20, 2, 2, ones(Bool, 2), 0.5 )
43+
n2_1 = IncrementalInference._prepareHypoRecipe!(nothing, 20, 2, 2, ones(Bool, 2), 0.5 )
4444

4545
@test sum([n2_1_gt1;] - [n2_1[1];]) == 0
4646
@test length(n2_1[2][1]) > n2_1_gt2_[1]
@@ -64,7 +64,7 @@ n2_1 = IncrementalInference.assembleHypothesesElements!(nothing, 20, 2, 2, ones(
6464
end
6565

6666

67-
@testset "test IncrementalInference.assembleHypothesesElements! without multihypothesis..." begin
67+
@testset "test IncrementalInference._prepareHypoRecipe! without multihypothesis..." begin
6868

6969
##
7070

@@ -77,7 +77,7 @@ s2_1_gt1 = 1:2
7777
s2_1_gt2 = (Int[],1:20,Int[])
7878
s2_1_gt3 = [(0,Int[1;]); (1,1:2); (2,Int[])]
7979
s2_1_gt4 = ones(20)
80-
s2_1 = IncrementalInference.assembleHypothesesElements!(nothing, 20, 1, 2 )
80+
s2_1 = IncrementalInference._prepareHypoRecipe!(nothing, 20, 1, 2 )
8181
@test sum([s2_1_gt1;] .- [s2_1[1];]) == 0
8282
@test sum( s2_1_gt2[1] .- s2_1[2][1]) == 0
8383
@test sum([s2_1_gt2[2];] .- [s2_1[2][2];]) == 0
@@ -96,7 +96,7 @@ s2_2_gt2 = (Int[],1:20,Int[])
9696
s2_2_gt3 = [(0,Int[2;]); (1,1:2); (2,Int[])]
9797
s2_2_gt4 = ones(20) # Int[]
9898

99-
s2_2 = IncrementalInference.assembleHypothesesElements!(nothing, 20, 2, 2 )
99+
s2_2 = IncrementalInference._prepareHypoRecipe!(nothing, 20, 2, 2 )
100100

101101
@test sum([s2_2_gt1;] .- [s2_2[1];]) == 0
102102
@test sum([s2_2_gt2[1];] .- [s2_2[2][1];]) == 0
@@ -116,7 +116,7 @@ end
116116

117117

118118

119-
@testset "assembleHypothesesElements! with bi-modality (certain variable)" begin
119+
@testset "_prepareHypoRecipe! with bi-modality (certain variable)" begin
120120

121121
##
122122

@@ -129,7 +129,7 @@ s3_1_gt2 = (0,3,3,40)
129129
s3_1_gt3 = [(1,Int[]); (2,Int[1;2]); (3,Int[1;3])]
130130
s3_1_gt4 = 40
131131

132-
s3_1 = IncrementalInference.assembleHypothesesElements!(Categorical([0.0;0.5;0.5]), 40, 1, 3)
132+
s3_1 = IncrementalInference._prepareHypoRecipe!(Categorical([0.0;0.5;0.5]), 40, 1, 3)
133133

134134
@test sum([s3_1_gt1;] - [s3_1[1];]) == 0
135135
@test sum([s3_1_gt2[1];] .- [s3_1[2][1];]) == 0
@@ -155,7 +155,7 @@ s3_1 = IncrementalInference.assembleHypothesesElements!(Categorical([0.0;0.5;0.5
155155
end
156156

157157

158-
@testset "assembleHypothesesElements! with bi-modality (fractional variable 1/2)" begin
158+
@testset "_prepareHypoRecipe! with bi-modality (fractional variable 1/2)" begin
159159

160160
##
161161

@@ -168,7 +168,7 @@ s3_2_gt2 = (0,3,3,40)
168168
s3_2_gt3 = [(0, Int[2]); (1,Int[1;2]); (2,Int[1;2]); (3,Int[2;3])]
169169
s3_2_gt4 = 40
170170

171-
s3_2 = IncrementalInference.assembleHypothesesElements!(Categorical([0.0;0.5;0.5]), 40, 2, 3 )
171+
s3_2 = IncrementalInference._prepareHypoRecipe!(Categorical([0.0;0.5;0.5]), 40, 2, 3 )
172172

173173
@test sum(s3_2_gt1 - s3_2[1]) == 0
174174
@test sum(s3_2_gt2[1] .- s3_2[2][2]) == 0
@@ -197,7 +197,7 @@ s3_2 = IncrementalInference.assembleHypothesesElements!(Categorical([0.0;0.5;0.5
197197

198198
end
199199

200-
@testset "assembleHypothesesElements! with bi-modality (fractional variable 2/2)" begin
200+
@testset "_prepareHypoRecipe! with bi-modality (fractional variable 2/2)" begin
201201

202202
##
203203

@@ -210,7 +210,7 @@ s3_3_gt2 = (0,3,3,40)
210210
s3_3_gt3 = [(0, Int[3]); (1,Int[1;3]); (2,Int[2;3]); (3,Int[1;3])]
211211
s3_3_gt4 = 40
212212

213-
s3_3 = IncrementalInference.assembleHypothesesElements!(Categorical([0.0;0.5;0.5]), 40, 3, 3 )
213+
s3_3 = IncrementalInference._prepareHypoRecipe!(Categorical([0.0;0.5;0.5]), 40, 3, 3 )
214214

215215
@test sum(s3_3_gt1 - s3_3[1]) == 0
216216
@test sum(s3_3_gt2[1] .- s3_3[2][2]) == 0
@@ -241,7 +241,7 @@ end
241241

242242

243243

244-
# @testset "test IncrementalInference.assembleHypothesesElements! with bi-modality backwards permutation..." begin
244+
# @testset "test IncrementalInference._prepareHypoRecipe! with bi-modality backwards permutation..." begin
245245

246246
# certainidx = 1
247247
# sfidx=1, mhidx=1: ah = []
@@ -252,7 +252,7 @@ end
252252
# s3_1_gt3 = [(1,Int[]); (2,Int[1;2]); (3,Int[1;3])]
253253
# s3_1_gt4 = 20
254254
#
255-
# s3_1 = IncrementalInference.assembleHypothesesElements!(Categorical([0.5;0.5;0.0]), 20, 1, 3)
255+
# s3_1 = IncrementalInference._prepareHypoRecipe!(Categorical([0.5;0.5;0.0]), 20, 1, 3)
256256

257257
# @test sum(s3_1_gt1 - s3_1[1]) == 0
258258
# @test sum(s3_1_gt2[1] .- s3_1[2][1]) == 0
@@ -278,7 +278,7 @@ end
278278
# end
279279

280280

281-
@testset "assembleHypothesesElements! with tri-modality (certain variable)" begin
281+
@testset "_prepareHypoRecipe! with tri-modality (certain variable)" begin
282282

283283
##
284284

@@ -288,7 +288,7 @@ s4_1_gt2 = (0,3,3,3,N)
288288
s4_1_gt3 = [(1,Int[]); (2,Int[1;2]); (3,Int[1;3]); (4,Int[1;4])]
289289
s4_1_gt4 = N
290290

291-
s4_1 = IncrementalInference.assembleHypothesesElements!(Categorical([0.0;0.33;0.33;0.34]), N, 1, 4 )
291+
s4_1 = IncrementalInference._prepareHypoRecipe!(Categorical([0.0;0.33;0.33;0.34]), N, 1, 4 )
292292

293293
@test sum(s4_1_gt1 - s4_1[1]) == 0
294294
@test sum(s4_1_gt2[1] .- s4_1[2][1]) == 0
@@ -321,7 +321,7 @@ s4_1 = IncrementalInference.assembleHypothesesElements!(Categorical([0.0;0.33;0.
321321
end
322322

323323

324-
@testset "assembleHypothesesElements! with tri-modality (fractional variable 1/3)" begin
324+
@testset "_prepareHypoRecipe! with tri-modality (fractional variable 1/3)" begin
325325

326326
## solve for fractional variable in trinary case
327327

@@ -331,7 +331,7 @@ s4_2_gt2 = (0,3,3,3,N)
331331
s4_2_gt3 = [(0,Int[2]); (1,Int[1;2]); (2,Int[1;2]); (3,Int[2;3;4]); (4,Int[2;3;4])]
332332
s4_2_gt4 = N
333333

334-
s4_2 = IncrementalInference.assembleHypothesesElements!(Categorical([0.0;0.33;0.33;0.34]), N, 2, 4 )
334+
s4_2 = IncrementalInference._prepareHypoRecipe!(Categorical([0.0;0.33;0.33;0.34]), N, 2, 4 )
335335

336336
@test sum(s4_2_gt1 - s4_2[1]) == 0
337337
@test length(s4_2[2][1]) > 0.5*s4_2_gt2[2]
@@ -368,7 +368,7 @@ end
368368

369369

370370

371-
@testset "assembleHypothesesElements! with tri-modality (fractional variable 2/3)" begin
371+
@testset "_prepareHypoRecipe! with tri-modality (fractional variable 2/3)" begin
372372

373373
##
374374

@@ -378,7 +378,7 @@ s4_3_gt2 = (0,3,3,3,N)
378378
s4_3_gt3 = [(0,Int[3]); (1,Int[1;3]); (2,Int[2;3;4]); (3,Int[1;3]); (4,Int[2;3;4])]
379379
s4_3_gt4 = N
380380

381-
s4_3 = IncrementalInference.assembleHypothesesElements!(Categorical([0.0;0.33;0.33;0.34]), N, 3, 4 )
381+
s4_3 = IncrementalInference._prepareHypoRecipe!(Categorical([0.0;0.33;0.33;0.34]), N, 3, 4 )
382382

383383
@test sum(s4_3_gt1 - s4_3[1]) == 0
384384
@test length(s4_3[2][1]) > 0.5*s4_3_gt2[2]
@@ -413,7 +413,7 @@ s4_3 = IncrementalInference.assembleHypothesesElements!(Categorical([0.0;0.33;0.
413413

414414
end
415415

416-
@testset "assembleHypothesesElements! with tri-modality (fractional variable 3/3)" begin
416+
@testset "_prepareHypoRecipe! with tri-modality (fractional variable 3/3)" begin
417417

418418
##
419419

@@ -423,7 +423,7 @@ s4_4_gt2 = (0,3,3,3,N)
423423
s4_4_gt3 = [(0,Int[4]); (1,Int[1;4]); (2,Int[2;3;4]); (3,Int[2;3;4]); (4,Int[1;4])]
424424
s4_4_gt4 = N
425425

426-
s4_4 = IncrementalInference.assembleHypothesesElements!(Categorical([0.0;0.33;0.33;0.34]), N, 4, 4 )
426+
s4_4 = IncrementalInference._prepareHypoRecipe!(Categorical([0.0;0.33;0.33;0.34]), N, 4, 4 )
427427

428428
@test sum(s4_4_gt1 - s4_4[1]) == 0
429429
@test length(s4_4[2][1]) > 0.5*s4_4_gt2[2]

0 commit comments

Comments
 (0)