Skip to content

Commit 9650893

Browse files
authored
Merge pull request #1333 from JuliaRobotics/21Q3/api/calcfactresid
swap arg order calcFactorRe.. and _evalFactorTemp...
2 parents f46feb3 + 69464d8 commit 9650893

File tree

8 files changed

+28
-26
lines changed

8 files changed

+28
-26
lines changed

src/ApproxConv.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,9 @@ function evalPotentialSpecific( Xi::AbstractVector{<:DFGVariable},
409409
ipc = if ccwl._gradients === nothing
410410
ones(getDimension(Xi[sfidx]))
411411
else
412-
# calcPerturbationFromVariable(ccwl._gradients, 2, ipc_)
413-
ones(getDimension(Xi[sfidx]))
412+
ipc_ = ones(getDimension(Xi[sfidx]))
413+
# calcPerturbationFromVariable(ccwl._gradients, 2, ipc_) # TODO, WIP
414+
ipc_ # TODO, WIP
414415
end
415416

416417
# return the found points, and info per coord
@@ -597,7 +598,7 @@ Notes
597598
598599
Example
599600
```julia
600-
B = _evalFactorTemporary!(EuclidDistance, ([10;],), 2, [(ContinuousScalar,[0]); (ContinuousScalar,[9.5;])] )
601+
B = _evalFactorTemporary!(EuclidDistance, (ContinuousScalar, ContinuousScalar), 2, ([10;],), ([0.],[9.5]) )
601602
# should return `B = 10`
602603
```
603604
@@ -606,9 +607,9 @@ Related
606607
[`evalFactor`](@ref), [`calcFactorResidual`](@ref), [`testFactorResidualBinary`](@ref)
607608
"""
608609
function _evalFactorTemporary!( fct::AbstractFactor,
610+
varTypes::Tuple,
609611
sfidx::Int, # solve for index, assuming variable order for fct
610612
meas_single::Tuple,
611-
varTypes::Tuple,
612613
pts::Tuple;
613614
tfg::AbstractDFG=initfg(),
614615
solveKey::Symbol=:default,

src/CalcFactor.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,16 @@ DevNotes
103103
104104
Example
105105
```julia
106-
residual = calcFactorResidualTemporary(Pose2Pose2(...), (z_i,), (RoME.Pose2, x1), (RoME.Pose2, x2))
106+
residual = calcFactorResidualTemporary(Pose2Pose2(...), (RoME.Pose2,RoME.Pose2), (z_i,), (x1, x2))
107107
```
108108
109109
Related
110110
111111
[`calcFactorResidual`](@ref), [`CalcResidual`](@ref), [`_evalFactorTemporary!`](@ref), [`approxConv`](@ref), [`_buildGraphByFactorAndTypes!`](@ref)
112112
"""
113113
function calcFactorResidualTemporary( fct::AbstractRelative,
114-
measurement::Tuple,
115114
varTypes::Tuple,
115+
measurement::Tuple,
116116
pts::Tuple;
117117
tfg::AbstractDFG = initfg(),
118118
_blockRecursion::Bool=false )

src/Deprecated.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ end
5959
# return msst_
6060
# end
6161

62-
@deprecate testFactorResidualBinary(fct, T1::InstanceType{InferenceVariable}, T2::InstanceType{InferenceVariable}, param1, param2, meas::Tuple) calcFactorResidualTemporary(fct, meas, (T1,param1), (T2,param2))
62+
@deprecate testFactorResidualBinary(fct, T1::InstanceType{InferenceVariable}, T2::InstanceType{InferenceVariable}, param1, param2, meas::Tuple) calcFactorResidualTemporary(fct, (T1,T2), meas, (param1,param2))
6363

6464
# """
6565
# $SIGNATURES

src/FactorGraph.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -680,10 +680,10 @@ function prepgenericconvolution(Xi::Vector{<:DFGVariable},
680680
gradients = nothing
681681
# prepare new cached gradient lambdas (attempt)
682682
try
683-
# this try block definitely fails on deserialization, due to empty DFGVariable[] vector here:
684683
# https://github.com/JuliaRobotics/IncrementalInference.jl/blob/db7ff84225cc848c325e57b5fb9d0d85cb6c79b8/src/DispatchPackedConversions.jl#L46
685684
# also https://github.com/JuliaRobotics/DistributedFactorGraphs.jl/issues/590#issuecomment-891450762
686-
if (!_blockRecursion) && usrfnc isa AbstractRelative
685+
# FIXME, suppressing nested gradient propagation on GenericMarginals for the time being, see #1010
686+
if (!_blockRecursion) && usrfnc isa AbstractRelative && !(usrfnc isa GenericMarginal)
687687
# take first value from each measurement-tuple-element
688688
measurement_ = map(x->x[1], meas_single)
689689
# compensate if no info available during deserialization

src/services/FactorGradients.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function _prepFactorGradientLambdas(fct::Union{<:AbstractRelativeMinimize,<:Abst
1313
tfg::AbstractDFG = initfg(),
1414
_blockRecursion::Bool=true,
1515
# gradients relative to coords requires
16-
slack_resid = calcFactorResidualTemporary(fct, measurement, varTypes, pts, tfg=tfg, _blockRecursion=_blockRecursion),
16+
slack_resid = calcFactorResidualTemporary(fct, varTypes, measurement, pts, tfg=tfg, _blockRecursion=_blockRecursion),
1717
# numerical diff perturbation size
1818
h::Real=1e-4 )
1919
#
@@ -29,7 +29,7 @@ function _prepFactorGradientLambdas(fct::Union{<:AbstractRelativeMinimize,<:Abst
2929
T_pth_s_i = (s,i) -> AMP.makePointFromCoords(M[s], coord_h(s,i), pts[s]) # exp(M,..., hat(M,...) )
3030
tup_pt_s_i_h = (s,i) -> tuple(pts[1:(s-1)]..., T_pth_s_i(s,i), pts[(s+1):end]...)
3131
# build a residual calculation specifically considering graph factor selections `s`, e.g. for binary `s ∈ {1,2}`.
32-
f_dsi_h = (d,s,i) -> IIF._evalFactorTemporary!(fct, d, measurement, varTypes, tup_pt_s_i_h(s,i); tfg=tfg, newFactor=false, currNumber=0, _slack=slack_resid )
32+
f_dsi_h = (d,s,i) -> IIF._evalFactorTemporary!(fct, varTypes, d, measurement, tup_pt_s_i_h(s,i); tfg=tfg, newFactor=false, currNumber=0, _slack=slack_resid )
3333
# standard calculus derivative definition (in coordinate space)
3434
Δf_dsi = (d,s,i, crd=coord_(d)) -> (f_dsi_h(d,s,i)[1] - crd)./h
3535
# jacobian block per s, for each i
@@ -129,7 +129,7 @@ function (fgc::FactorGradientsCached!)(meas_pts...)
129129
measurement = tuple(meas_pts[1:lenm]...)
130130
pts = tuple(meas_pts[(1+lenm):end]...)
131131
varTypes = tuple(getVariableType.(getVariable.(fgc._tfg, getVariableOrder(fgc.dfgfct)))...)
132-
new_slack = calcFactorResidualTemporary(fct, measurement, varTypes, pts; tfg=fgc._tfg)
132+
new_slack = calcFactorResidualTemporary(fct, varTypes, measurement, pts; tfg=fgc._tfg)
133133
# TODO make sure slack_residual is properly wired up with all the lambda functions as expected
134134
_setFGCSlack!(fgc, new_slack)
135135
# _setPointsMani!(fgc.slack_residual, new_slack)

test/testApproxConv.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ ccw = IIF._getCCW(fg, :x0x1f1)
2323

2424
@test IIF._getZDim(ccw) == 3
2525

26-
res = calcFactorResidualTemporary(lr3, ([0;0;0.5],),
26+
res = calcFactorResidualTemporary(lr3,
2727
(ContinuousEuclid{3},ContinuousEuclid{3}),
28-
(zeros(3), [0;0;1.0]) )
28+
([0;0;0.5],),
29+
(zeros(3), [0;0;1.0]) )
2930
#
3031

3132
@test (sum(abs.(res)) - 0.5) < 1e-10

test/testGradientUtils.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ dfg, _dfgfct = IIF._buildGraphByFactorAndTypes!(fct, varTypes, varPts)
2020

2121
## test the evaluation of factor without
2222

23-
B = IIF._evalFactorTemporary!(EuclidDistance(Normal(10,1)), 2, ([10;],), varTypes, varPts );
23+
B = IIF._evalFactorTemporary!(EuclidDistance(Normal(10,1)), varTypes, 2, ([10;],), varPts );
2424

2525
@test_broken B isa Vector{Vector{Float64}}
2626
@test isapprox( B[1], [10.0;], atol=1e-6)
@@ -39,25 +39,25 @@ pts = ([0;],[9.5;])
3939

4040
##
4141

42-
slack_resid = calcFactorResidualTemporary(fct, measurement, varTypes, pts)
42+
slack_resid = calcFactorResidualTemporary(fct, varTypes, measurement, pts)
4343

4444
##
4545

46-
coord_1 = IIF._evalFactorTemporary!(fct, 1, measurement, varTypes, pts, _slack=slack_resid )
46+
coord_1 = IIF._evalFactorTemporary!(fct, varTypes, 1, measurement, pts, _slack=slack_resid )
4747
@test length(coord_1) == 1
4848
@test isapprox( coord_1[1], [0.0], atol=1e-6)
4949

50-
coord_2 = IIF._evalFactorTemporary!(fct, 2, measurement, varTypes, pts, _slack=slack_resid )
50+
coord_2 = IIF._evalFactorTemporary!(fct, varTypes, 2, measurement, pts, _slack=slack_resid )
5151
@test length(coord_2) == 1
5252
@test isapprox( coord_2[1], [9.5], atol=1e-6)
5353

5454
##
5555

56-
coord_1 = IIF._evalFactorTemporary!(fct, 1, measurement, varTypes, pts )
56+
coord_1 = IIF._evalFactorTemporary!(fct, varTypes, 1, measurement, pts )
5757
@test length(coord_1) == 1
5858
@test isapprox( coord_1[1], [-0.5], atol=1e-6)
5959

60-
coord_2 = IIF._evalFactorTemporary!(fct, 2, measurement, varTypes, pts )
60+
coord_2 = IIF._evalFactorTemporary!(fct, varTypes, 2, measurement, pts )
6161
@test length(coord_2) == 1
6262
@test isapprox( coord_2[1], [10.0], atol=1e-6)
6363

@@ -89,7 +89,7 @@ _fg,_ = IIF._buildGraphByFactorAndTypes!(fct, varTypes, pts);
8989

9090
_fg = initfg()
9191

92-
slack_resid = calcFactorResidualTemporary(fct, measurement, varTypes, pts, tfg=_fg)
92+
slack_resid = calcFactorResidualTemporary(fct, varTypes, measurement, pts, tfg=_fg)
9393

9494
@test length(getVal(_fg[:x1])) == 1
9595
@test length(getVal(_fg[:x1])[1]) == 2
@@ -100,7 +100,7 @@ slack_resid = calcFactorResidualTemporary(fct, measurement, varTypes, pts, tfg=_
100100
## Manually provide a common temp graph and force no factor and same variables via keywords
101101

102102
tfg,_ = IIF._buildGraphByFactorAndTypes!(fct, varTypes, pts);
103-
coord_1 = IIF._evalFactorTemporary!(fct, 1, measurement, varTypes, pts, _slack=slack_resid, tfg=tfg, newFactor=false, currNumber=0 )
103+
coord_1 = IIF._evalFactorTemporary!(fct, varTypes, 1, measurement, pts, _slack=slack_resid, tfg=tfg, newFactor=false, currNumber=0 )
104104

105105
##
106106

@@ -109,23 +109,23 @@ coord_1 = IIF._evalFactorTemporary!(fct, 1, measurement, varTypes, pts, _slack=s
109109

110110
@test isapprox( coord_1[1], [0;0.0], atol=1e-6)
111111

112-
coord_2 = IIF._evalFactorTemporary!(fct, 2, measurement, varTypes, pts, _slack=slack_resid )
112+
coord_2 = IIF._evalFactorTemporary!(fct, varTypes, 2, measurement, pts, _slack=slack_resid )
113113
@test length(coord_2) == 1
114114
@test length(coord_2[1]) == 2
115115

116116
@test isapprox( coord_2[1], [9.5; 0], atol=1e-6)
117117

118118
## Repeat the same test but allow _evalFactorTemporary to self construct internal temporary graph
119119

120-
coord_1 = IIF._evalFactorTemporary!(fct, 1, measurement, varTypes, pts )
120+
coord_1 = IIF._evalFactorTemporary!(fct, varTypes, 1, measurement, pts )
121121

122122
##
123123
@test length(coord_1) == 1
124124
@test length(coord_1[1]) == 2
125125

126126
@test isapprox( coord_1[1], [-0.5;0], atol=1e-6)
127127

128-
coord_2 = IIF._evalFactorTemporary!(fct, 2, measurement, varTypes, pts )
128+
coord_2 = IIF._evalFactorTemporary!(fct, varTypes, 2, measurement, pts )
129129
@test length(coord_2) == 1
130130
@test length(coord_2[1]) == 2
131131

test/testpartialconstraint.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ val2_ = getVal(v1)
284284
val_, = predictbelief(fg, v2, [f3;f4], N=N)
285285
@cast val[i,j] := val_[j][i]
286286
# plotKDE(kde!(val),levels=3)
287-
@test_broken norm(Statistics.mean(val,dims=2)[1] .- [-20.0]) < 0.01
287+
@test norm(Statistics.mean(val,dims=2)[1] .- [-20.0]) < 1
288288
@test_broken norm(Statistics.mean(val,dims=2)[2] .- [10.0]) < 0.01
289289
@test (Statistics.std(val,dims=2)[1] .- 1.0) < 3.0
290290
@test (Statistics.std(val,dims=2)[2] .- 1.0) < 3.0

0 commit comments

Comments
 (0)