Skip to content

Commit 16113e1

Browse files
authored
Merge pull request #969 from JuliaRobotics/main/4Q20/general
improve testing and docs
2 parents be2ab1f + 560af2c commit 16113e1

File tree

2 files changed

+89
-51
lines changed

2 files changed

+89
-51
lines changed

src/FactorGraphTypes.jl

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ $(TYPEDEF)
1111
Solver parameters for the DistributedFactoGraph.
1212
1313
Dev Notes
14-
- TODO remove NothingUnion
14+
- # TODO remove NothingUnion
15+
- # TODO Upgrade to common @kwargs struct approach
1516
"""
1617
mutable struct SolverParams <: DFG.AbstractParams
1718
dimID::Int
@@ -110,26 +111,26 @@ function initfg(dfg::T=InMemDFGType(solverParams=SolverParams());
110111
sessionname="NA",
111112
robotname="",
112113
username="",
113-
cloudgraph=nothing)::T where T <: AbstractDFG
114+
cloudgraph=nothing) where T <: AbstractDFG
114115
#
115116
return dfg
116117
end
117118

118119

119120
#init an empty fg with a provided type and SolverParams
120-
function initfg(::Type{T}; solverParams=SolverParams(),
121-
sessionname="NA",
122-
robotname="",
123-
username="",
124-
cloudgraph=nothing)::AbstractDFG where T <: AbstractDFG
121+
function initfg(::Type{T};solverParams=SolverParams(),
122+
sessionname="NA",
123+
robotname="",
124+
username="",
125+
cloudgraph=nothing) where T <: AbstractDFG
125126
return T(solverParams=solverParams)
126127
end
127128

128-
function initfg(::Type{T}, solverParams::SolverParams;
129-
sessionname="NA",
130-
robotname="",
131-
username="",
132-
cloudgraph=nothing)::AbstractDFG where T <: AbstractDFG
129+
function initfg(::Type{T},solverParams::SolverParams;
130+
sessionname="NA",
131+
robotname="",
132+
username="",
133+
cloudgraph=nothing) where T <: AbstractDFG
133134
return T{SolverParams}(solverParams=solverParams)
134135
end
135136

@@ -188,15 +189,15 @@ mutable struct ConvPerThread
188189
ConvPerThread() = new()
189190
end
190191

191-
function ConvPerThread(X::Array{Float64,2},
192-
zDim::Int;
193-
factormetadata::FactorMetadata=FactorMetadata(),
194-
particleidx::Int=1,
195-
activehypo= 1:length(params),
196-
p=collect(1:size(X,1)),
197-
perturb=zeros(zDim),
198-
Y=zeros(size(X,1)),
199-
res=zeros(zDim) )
192+
function ConvPerThread( X::Array{Float64,2},
193+
zDim::Int;
194+
factormetadata::FactorMetadata=FactorMetadata(),
195+
particleidx::Int=1,
196+
activehypo= 1:length(params),
197+
p=collect(1:size(X,1)),
198+
perturb=zeros(zDim),
199+
Y=zeros(size(X,1)),
200+
res=zeros(zDim) )
200201
#
201202
cpt = ConvPerThread()
202203
cpt.thrid_ = 0
@@ -239,26 +240,26 @@ mutable struct CommonConvWrapper{T<:FunctorInferenceType} <: FactorOperationalMe
239240
end
240241

241242

242-
function CommonConvWrapper(fnc::T,
243-
X::Array{Float64,2},
244-
zDim::Int,
245-
params::Vector{Array{Float64,2}};
246-
factormetadata::FactorMetadata=FactorMetadata(),
247-
specialzDim::Bool=false,
248-
partial::Bool=false,
249-
hypotheses=nothing,
250-
certainhypo=nothing,
251-
activehypo= 1:length(params),
252-
nullhypo::Real=0,
253-
varidx::Int=1,
254-
measurement::Tuple=(zeros(0,1),),
255-
particleidx::Int=1,
256-
p=collect(1:size(X,1)),
257-
perturb=zeros(zDim),
258-
Y=zeros(size(X,1)),
259-
xDim=size(X,1),
260-
res=zeros(zDim),
261-
threadmodel=MultiThreaded ) where {T<:FunctorInferenceType}
243+
function CommonConvWrapper( fnc::T,
244+
X::Array{Float64,2},
245+
zDim::Int,
246+
params::Vector{Array{Float64,2}};
247+
factormetadata::FactorMetadata=FactorMetadata(),
248+
specialzDim::Bool=false,
249+
partial::Bool=false,
250+
hypotheses=nothing,
251+
certainhypo=nothing,
252+
activehypo= 1:length(params),
253+
nullhypo::Real=0,
254+
varidx::Int=1,
255+
measurement::Tuple=(zeros(0,1),),
256+
particleidx::Int=1,
257+
p=collect(1:size(X,1)),
258+
perturb=zeros(zDim),
259+
Y=zeros(size(X,1)),
260+
xDim=size(X,1),
261+
res=zeros(zDim),
262+
threadmodel=MultiThreaded ) where {T<:FunctorInferenceType}
262263
#
263264
ccw = CommonConvWrapper{T}()
264265

test/testMixturePrior.jl

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,40 @@ using Test
1111
# init graph
1212
fg = initfg()
1313
N = 100
14-
fg.solverParams.N = N;
14+
getSolverParams(fg).N = N;
1515

1616
# add first variable
1717
addVariable!(fg, :x0, ContinuousScalar)
1818

19+
# also test AliasingScalingSampler
20+
v = rand(50)
21+
v[20:29] .+= 5*rand(10)
22+
v ./= sum(v)
23+
bss = AliasingScalarSampler(collect(1:50), v)
24+
25+
1926
# add bi-modal mixture prior
20-
Prior0 = MixturePrior((a= Normal(-5.0,1.0), b=Uniform(0.0,1.0)), (0.5,0.5))
21-
Prior0 = MixturePrior((Normal(-5.0,1.0), Normal(0.0,1.0)), (0.5,0.5))
22-
Prior0 = MixturePrior([Normal(-5.0,1.0), Normal(0.0,1.0)], (0.5,0.5))
23-
Prior0 = MixturePrior((Normal(-5.0,1.0), Normal(0.0,1.0)), [0.5;0.5])
24-
Prior0 = MixturePrior([Normal(-5.0,1.0), Normal(0.0,1.0)], [0.5;0.5])
25-
Prior0 = MixturePrior([Normal(-5.0,1.0), Normal(0.0,1.0)], Categorical([0.5;0.5]))
26-
addFactor!(fg, [:x0], Prior0)
27+
Prior0 = Mixture(Prior,(a=Normal(-5.0,1.0), b=Uniform(0.0,1.0)), (0.5,0.5))
28+
Prior0 = Mixture(Prior,(Normal(-5.0,1.0), Normal(0.0,1.0)), (0.5,0.5))
29+
Prior0 = Mixture(Prior,[Normal(-5.0,1.0), Normal(0.0,1.0)], (0.5,0.5))
30+
Prior0 = Mixture(Prior,(Normal(-5.0,1.0), Normal(0.0,1.0)), [0.5;0.5])
31+
Prior0 = Mixture(Prior,[Normal(-5.0,1.0), Normal(0.0,1.0)], [0.5;0.5])
32+
Prior0 = Mixture(Prior,(Normal(-5.0,1.0), bss), Categorical([0.5;0.5]))
33+
f1 = addFactor!(fg, [:x0], Prior0)
34+
35+
# also test serialization of AliasingScalarSampler
36+
saveDFG("/tmp/test_fg_bss", fg)
2737

38+
39+
# check numerics
2840
smpls, lb = getSample(Prior0, N)
2941

3042
# should be a balance of particles
3143
@test sum(lb .== 1) - sum(lb .== 2) |> abs < 0.3*N
3244
@test sum(smpls .< -2.5) - sum(-2.5 .< smpls) |> abs < 0.3*N
3345

3446
# solve
35-
solveTree!(fg)
47+
solveTree!(fg);
3648

3749
marginalPts = getBelief(fg, :x0) |> getPoints
3850

@@ -43,8 +55,33 @@ marginalPts = getBelief(fg, :x0) |> getPoints
4355
end
4456

4557

58+
59+
@testset "Serialization of Mixture(Prior,..) including a AliasingScalarSampler" begin
60+
61+
62+
fg_ = loadDFG("/tmp/test_fg_bss")
63+
64+
N = getSolverParams(fg_).N
65+
66+
67+
solveTree!(fg_);
68+
69+
70+
marginalPts = getBelief(fg_, :x0) |> getPoints
71+
72+
# check solver solution consistent too
73+
@test sum(marginalPts .< -2.5) - sum(-2.5 .< marginalPts) |> abs < 0.25*N
74+
75+
76+
# cleanup
77+
Base.rm("/tmp/test_fg_bss.tar.gz")
78+
79+
end
80+
81+
4682
# using RoMEPlotting
47-
#
83+
# Gadfly.set_default_plot_size(35cm,20cm)
84+
4885
# # plot the results
4986
# plotKDE(fg, :x0)
5087

0 commit comments

Comments
 (0)