Skip to content

Commit 46b8970

Browse files
committed
wip
1 parent 0310ce6 commit 46b8970

File tree

7 files changed

+128
-48
lines changed

7 files changed

+128
-48
lines changed

src/ApproxConv.jl

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function prepareCommonConvWrapper!(ccwl::CommonConvWrapper{T},
8282
maxlen, sfidx, manis = prepareparamsarray!(ARR, Xi, solvefor, N)
8383
# should be selecting for the correct multihypothesis mode here with `gwp.params=ARR[??]`
8484
ccwl.params = ARR
85-
# get factor metadata -- TODO, populate
85+
# get factor metadata -- TODO, populate, also see #784
8686
fmd = FactorMetadata()
8787
# get variable node data
8888
vnds = Xi # (x->getSolverData(x)).(Xi)
@@ -187,7 +187,7 @@ function computeAcrossHypothesis!(ccwl::CommonConvWrapper{T},
187187
# @assert norm(ccwl.certainhypo - certainidx) < 1e-6
188188
for (hypoidx, vars) in activehypo
189189
count += 1
190-
if sfidx in certainidx || hypoidx in certainidx || hypoidx == sfidx
190+
if sfidx in certainidx && hypoidx != 0 || hypoidx in certainidx || hypoidx == sfidx
191191
# hypo case hypoidx, sfidx = $hypoidx, $sfidx
192192
for i in 1:Threads.nthreads() ccwl.cpt[i].activehypo = vars; end
193193
approxConvOnElements!(ccwl, allelements[count])
@@ -232,25 +232,6 @@ function computeAcrossHypothesis!(ccwl::CommonConvWrapper{T},
232232
end
233233

234234

235-
"""
236-
$(SIGNATURES)
237-
238-
Prepare data required for null hypothesis cases during convolution.
239-
"""
240-
function assembleNullHypothesis(ccwl::CommonConvWrapper{T},
241-
maxlen::Int,
242-
spreadfactor::Real=10 ) where {T}
243-
#
244-
@warn "this assembleNullHypothesis method has been updated for e.g. `addFactor!(; nullhypo=0.1)` instead."
245-
nhc = rand(ccwl.usrfnc!.nullhypothesis, maxlen) .- 1
246-
arr = ccwl.params[ccwl.varidx]
247-
ENT = generateNullhypoEntropy(arr, maxlen, spreadfactor)
248-
allelements = 1:maxlen
249-
return allelements, nhc, ENT
250-
end
251-
252-
253-
254235
"""
255236
$(SIGNATURES)
256237
@@ -282,7 +263,7 @@ function evalPotentialSpecific(Xi::Vector{DFGVariable},
282263
addOps, d1, d2, d3 = buildHybridManifoldCallbacks(manis)
283264

284265
# assemble how hypotheses should be computed
285-
_, allelements, activehypo, mhidx = assembleHypothesesElements!(ccwl.hypotheses, maxlen, sfidx, length(Xi), isinit )
266+
_, allelements, activehypo, mhidx = assembleHypothesesElements!(ccwl.hypotheses, maxlen, sfidx, length(Xi), isinit, ccwl.nullhypo )
286267
certainidx = ccwl.certainhypo
287268

288269
# perform the numeric solutions on the indicated elements

src/Deprecated.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,26 @@ function computeAcrossNullHypothesis!(ccwl::CommonConvWrapper{T},
4949
nothing
5050
end
5151

52+
53+
"""
54+
$(SIGNATURES)
55+
56+
Prepare data required for null hypothesis cases during convolution.
57+
"""
58+
function assembleNullHypothesis(ccwl::CommonConvWrapper{T},
59+
maxlen::Int,
60+
spreadfactor::Real=10 ) where {T}
61+
#
62+
@warn "this assembleNullHypothesis method has been deprecated for e.g. `addFactor!(; nullhypo=0.1)` instead."
63+
nhc = rand(ccwl.usrfnc!.nullhypothesis, maxlen) .- 1
64+
arr = ccwl.params[ccwl.varidx]
65+
ENT = generateNullhypoEntropy(arr, maxlen, spreadfactor)
66+
allelements = 1:maxlen
67+
return allelements, nhc, ENT
68+
end
69+
70+
71+
5272
##==============================================================================
5373
## Delete at end v0.13.x
5474
##==============================================================================

src/DispatchPackedConversions.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@ end
3232

3333
function convert(
3434
::Type{IncrementalInference.GenericFunctionNodeData{IncrementalInference.CommonConvWrapper{F}}},
35-
d::IncrementalInference.GenericFunctionNodeData{P} ) where {F <: FunctorInferenceType, P <: PackedInferenceType}
35+
packed::IncrementalInference.GenericFunctionNodeData{P} ) where {F <: FunctorInferenceType, P <: PackedInferenceType}
3636
#
3737
# TODO store threadmodel=MutliThreaded,SingleThreaded in persistence layer
38-
usrfnc = convert(F, d.fnc)
39-
mhcat, nh = parseusermultihypo(d.multihypo, d.nullhypo)
38+
usrfnc = convert(F, packed.fnc)
39+
mhcat, nh = parseusermultihypo(packed.multihypo, packed.nullhypo)
4040

4141
# TODO -- improve prepgenericconvolution for hypotheses and certainhypo field recovery when deserializing
4242
# reconstitute from stored data
4343
# FIXME, add threadmodel=threadmodel
4444
ccw = prepgenericconvolution(DFG.DFGVariable[], usrfnc, multihypo=mhcat, nullhypo=nh)
45-
ccw.certainhypo = d.certainhypo
45+
ccw.certainhypo = packed.certainhypo
4646

47-
ret = FunctionNodeData{CommonConvWrapper{typeof(usrfnc)}}(d.eliminated, d.potentialused, d.edgeIDs, ccw,
48-
d.multihypo, d.certainhypo, d.nullhypo, d.solveInProgress)
47+
ret = FunctionNodeData{CommonConvWrapper{typeof(usrfnc)}}(packed.eliminated, packed.potentialused, packed.edgeIDs, ccw,
48+
packed.multihypo, packed.certainhypo, packed.nullhypo, packed.solveInProgress)
4949
#
5050
return ret
5151
end

src/ExplicitDiscreteMarginalizations.jl

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,17 @@ function assembleHypothesesElements!(mh::Categorical,
195195

196196
return certainidx, allelements, activehypo, mhidx
197197
end
198+
199+
198200
function assembleHypothesesElements!(mh::Nothing,
199201
maxlen::Int,
200202
sfidx::Int,
201203
lenXi::Int,
202204
isinit::Vector{Bool}=ones(Bool, lenXi),
203205
nullhypo::Real=0 )
204206
#
207+
# FIXME, consolidate with the general multihypo case
208+
205209
# the default case where mh==nothing
206210
# equivalent to mh=[1;1;1] # assuming 3 variables
207211
# sfidx=1, allelements=allidx[nhidx.==0], activehypo=(0,[1;])
@@ -226,23 +230,24 @@ function assembleHypothesesElements!(mh::Nothing,
226230
nullarr = allidx[mhidx .== 0]
227231
# mhidx == 1 case is regular -- this will be all elements if nullhypo=0.0
228232
reguarr = allidx[mhidx .!= 0]
229-
for i in [0;certainidx]
230-
if i == 0
233+
pidxAll = [0;certainidx]
234+
for pidx in pidxAll
235+
if pidx == 0
231236
# elements that occur during nullhypo active
232237
push!(allelements, nullarr)
233-
push!(activehypo, (i,[sfidx;]))
234-
elseif i == 1
238+
push!(activehypo, (pidx,[sfidx;]))
239+
elseif pidx == 1
235240
# elements that occur during regular hypothesis true
236241
push!(allelements, reguarr)
237-
push!(activehypo, (i,[certainidx;]))
242+
push!(activehypo, (pidx,certainidx))
238243
else
239244
# all remaining collections are empty (part of multihypo support)
240245
push!(allelements, Int[])
241-
push!(activehypo, (i,Int[]))
246+
push!(activehypo, (pidx,Int[]))
242247
end
243248
end
244249

245-
250+
@show nullhypo, mhidx
246251
return certainidx, allelements, activehypo, mhidx
247252
end
248253

src/FactorGraph.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ function prepgenericconvolution(
581581
Xi::Vector{<:DFGVariable},
582582
usrfnc::T;
583583
multihypo::Union{Nothing, Distributions.Categorical}=nothing,
584-
nullhypo=0.0,
584+
nullhypo::Real=0.0,
585585
threadmodel=MultiThreaded ) where {T <: FunctorInferenceType}
586586
#
587587
ARR = Array{Array{Float64,2},1}()
@@ -599,6 +599,7 @@ function prepgenericconvolution(
599599
partial = sum(fldnms .== :partial) >= 1,
600600
hypotheses=multihypo,
601601
certainhypo=certainhypo,
602+
nullhypo=nullhypo,
602603
threadmodel=threadmodel
603604
)
604605
#

src/FactorGraphTypes.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ mutable struct CommonConvWrapper{T} <: FactorOperationalMemory where {T<:Functor
215215
# multi hypothesis settings
216216
hypotheses::Union{Nothing, Distributions.Categorical} # categorical to select which hypothesis is being considered during convolution operation
217217
certainhypo::Union{Nothing, Vector{Int}}
218+
nullhypo::Float64
218219
# values specific to one complete convolution operation
219220
params::Vector{Array{Float64,2}} # parameters passed to each hypothesis evaluation event on user function
220221
varidx::Int # which index is being solved for in params?
@@ -237,6 +238,7 @@ function CommonConvWrapper(fnc::T,
237238
hypotheses=nothing,
238239
certainhypo=nothing,
239240
activehypo= 1:length(params),
241+
nullhypo::Real=0,
240242
varidx::Int=1,
241243
measurement::Tuple=(zeros(0,1),),
242244
particleidx::Int=1,
@@ -256,6 +258,7 @@ function CommonConvWrapper(fnc::T,
256258
ccw.partial = partial
257259
ccw.hypotheses = hypotheses
258260
ccw.certainhypo=certainhypo
261+
ccw.nullhypo=nullhypo
259262
ccw.params = params
260263
ccw.varidx = varidx
261264
ccw.threadmodel = threadmodel

test/testExplicitMultihypo.jl

Lines changed: 82 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,107 @@ using Test
55

66
using IncrementalInference
77

8+
9+
@testset "test IncrementalInference.assembleHypothesesElements! with only nullhypothesis..." begin
10+
11+
# n2_1 == (certainidx, allelements, activehypo, mhidx)
12+
n2_1_gt1 = 1:2
13+
n2_1_gt2_ = (3,3,0)
14+
n2_1_gt3 = [(0,Int[1;]); (1,1:2); (2,Int[])]
15+
n2_1_gt4_ = 20
16+
n2_1 = IncrementalInference.assembleHypothesesElements!(nothing, 20, 1, 2, ones(Bool, 2), 0.5 )
17+
18+
@test sum([n2_1_gt1;] - [n2_1[1];]) == 0
19+
@test length(n2_1[2][1]) > n2_1_gt2_[1]
20+
@test length(n2_1[2][2]) > n2_1_gt2_[2]
21+
@test length(n2_1[2][3]) == n2_1_gt2_[3]
22+
@test length(n2_1[2][1]) + length(n2_1[2][2]) == n2_1_gt4_
23+
@test n2_1_gt3[1][1] == n2_1[3][1][1]
24+
@test n2_1_gt3[2][1] == n2_1[3][2][1]
25+
@test n2_1_gt3[3][1] == n2_1[3][3][1]
26+
@test sum(n2_1_gt3[1][2] .- n2_1[3][1][2]) == 0
27+
@test sum([n2_1_gt3[2][2];] .- [n2_1[3][2][2];]) == 0
28+
@test sum(n2_1_gt3[3][2] .- n2_1[3][3][2]) == 0
29+
@test sum(n2_1[4] .== 0) > n2_1_gt2_[1]
30+
@test sum(n2_1[4] .== 1) > n2_1_gt2_[1]
31+
@test sum( [1:n2_1_gt4_;][n2_1[4] .== 0] .== n2_1[2][1] ) == length(n2_1[2][1])
32+
@test sum( [1:n2_1_gt4_;][n2_1[4] .== 1] .== n2_1[2][2] ) == length(n2_1[2][2])
33+
@test length(n2_1[4]) == n2_1_gt4_
34+
35+
36+
37+
38+
# n2_1 == (certainidx, allelements, activehypo, mhidx)
39+
n2_1_gt1 = 1:2
40+
n2_1_gt2_ = (3,3,0)
41+
n2_1_gt3 = [(0,Int[2;]); (1,1:2); (2,Int[])]
42+
n2_1_gt4_ = 20
43+
n2_1 = IncrementalInference.assembleHypothesesElements!(nothing, 20, 2, 2, ones(Bool, 2), 0.5 )
44+
45+
@test sum([n2_1_gt1;] - [n2_1[1];]) == 0
46+
@test length(n2_1[2][1]) > n2_1_gt2_[1]
47+
@test length(n2_1[2][2]) > n2_1_gt2_[2]
48+
@test length(n2_1[2][3]) == n2_1_gt2_[3]
49+
@test length(n2_1[2][1]) + length(n2_1[2][2]) == n2_1_gt4_
50+
@test n2_1_gt3[1][1] == n2_1[3][1][1]
51+
@test n2_1_gt3[2][1] == n2_1[3][2][1]
52+
@test n2_1_gt3[3][1] == n2_1[3][3][1]
53+
@test sum(n2_1_gt3[1][2] .- n2_1[3][1][2]) == 0
54+
@test sum([n2_1_gt3[2][2];] .- [n2_1[3][2][2];]) == 0
55+
@test sum(n2_1_gt3[3][2] .- n2_1[3][3][2]) == 0
56+
@test sum(n2_1[4] .== 0) > n2_1_gt2_[1]
57+
@test sum(n2_1[4] .== 1) > n2_1_gt2_[1]
58+
@test sum( [1:n2_1_gt4_;][n2_1[4] .== 0] .== n2_1[2][1] ) == length(n2_1[2][1])
59+
@test sum( [1:n2_1_gt4_;][n2_1[4] .== 1] .== n2_1[2][2] ) == length(n2_1[2][2])
60+
@test length(n2_1[4]) == n2_1_gt4_
61+
62+
63+
64+
end
65+
66+
867
@testset "test IncrementalInference.assembleHypothesesElements! without multihypothesis..." begin
968

69+
# certainidx = 1 ## ??
70+
# sfidx=1, mhidx=0: ah = [1;]
71+
# sfidx=1, mhidx=1: ah = [1;2]
72+
73+
# s2_1 == (certainidx, allelements, activehypo, mhidx)
1074
s2_1_gt1 = 1:2
11-
s2_1_gt2 = (1:20,Int[])
12-
s2_1_gt3 = [(1,1:2); (2,Int[])]
13-
s2_1_gt4 = Int[]
75+
s2_1_gt2 = (Int[],1:20,Int[])
76+
s2_1_gt3 = [(0,Int[1;]); (1,1:2); (2,Int[])]
77+
s2_1_gt4 = ones(20)
1478
s2_1 = IncrementalInference.assembleHypothesesElements!(nothing, 20, 1, 2 )
1579
@test sum([s2_1_gt1;] .- [s2_1[1];]) == 0
16-
@test sum([s2_1_gt2[1];] .- [s2_1[2][1];]) == 0
17-
@test sum(s2_1_gt2[2] .- s2_1[2][2]) == 0
80+
@test sum( s2_1_gt2[1] .- s2_1[2][1]) == 0
81+
@test sum([s2_1_gt2[2];] .- [s2_1[2][2];]) == 0
82+
@test sum( s2_1_gt2[3] .- s2_1[2][3]) == 0
1883
@test s2_1_gt3[1][1] == s2_1[3][1][1]
19-
@test sum([s2_1_gt3[1][2];] .- [s2_1[3][1][2];]) == 0
84+
@test sum(s2_1_gt3[1][2] .- s2_1[3][1][2]) == 0
2085
@test s2_1_gt3[2][1] == s2_1[3][2][1]
21-
@test sum(s2_1_gt3[2][2] .- s2_1[3][2][2]) == 0
86+
@test sum([s2_1_gt3[2][2];] .- [s2_1[3][2][2];]) == 0
87+
@test s2_1_gt3[3][1] == s2_1[3][3][1]
88+
@test sum(s2_1_gt3[3][2] .- s2_1[3][3][2]) == 0
2289
@test sum(s2_1_gt4 .- s2_1[4]) == 0
2390

2491

2592
s2_2_gt1 = 1:2
26-
s2_2_gt2 = (1:20,Int[])
27-
s2_2_gt3 = [(1,1:2); (2,Int[])]
28-
s2_2_gt4 = Int[]
93+
s2_2_gt2 = (Int[],1:20,Int[])
94+
s2_2_gt3 = [(0,Int[2;]); (1,1:2); (2,Int[])]
95+
s2_2_gt4 = ones(20) # Int[]
2996

3097
s2_2 = IncrementalInference.assembleHypothesesElements!(nothing, 20, 2, 2 )
3198

3299
@test sum([s2_2_gt1;] .- [s2_2[1];]) == 0
33100
@test sum([s2_2_gt2[1];] .- [s2_2[2][1];]) == 0
34101
@test sum(s2_2_gt2[2] .- s2_2[2][2]) == 0
35-
@test s2_2_gt3[1][1] == s2_2[3][1][1]
102+
@test sum(s2_2_gt2[3] .- s2_2[2][3]) == 0
103+
@test s2_2_gt3[1][1] == s2_2[3][1][1]
36104
@test sum([s2_2_gt3[1][2];] .- [s2_2[3][1][2];]) == 0
37105
@test s2_2_gt3[2][1] == s2_2[3][2][1]
38-
@test sum(s2_2_gt3[2][2] .- s2_2[3][2][2]) == 0
106+
@test sum([s2_2_gt3[2][2];] .- [s2_2[3][2][2];]) == 0
107+
@test s2_2_gt3[3][1] == s2_2[3][3][1]
108+
@test sum([s2_2_gt3[3][2];] .- [s2_2[3][3][2];]) == 0
39109
@test sum(s2_2_gt4 .- s2_2[4]) == 0
40110

41111
end

0 commit comments

Comments
 (0)