Skip to content

Commit 04240cc

Browse files
authored
Refactor comparison funcitons (#278)
* Move compare functions to one folder * generic compare == overloads * run iifCompareTests
1 parent eca9b7c commit 04240cc

File tree

10 files changed

+387
-112
lines changed

10 files changed

+387
-112
lines changed

src/Deprecated.jl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import Base: propertynames, getproperty
66
# this hides all the propertynames and makes it hard to work with.
77
# Base.propertynames(x::VariableDataLevel1, private::Bool=false) = private ? (:estimateDict, :ppeDict) : (:ppeDict,)
88

9+
## REMOVE in 0.6.1 #TODO look what should already be removed
10+
911
Base.getproperty(x::DFGVariable,f::Symbol) = begin
1012
if f == :estimateDict
1113
@warn "estimateDict is deprecated, use ppeDict instead"
@@ -47,7 +49,12 @@ Base.getproperty(x::DFGFactor,f::Symbol) = begin
4749
elseif f == :_internalId
4850
getfield(x,:_dfgNodeParams)._internalId
4951
elseif f == :data
50-
@warn "data field is deprecated, use solverData or getSolverData"
52+
53+
if !(@isdefined getFactorDataWarnOnce)
54+
@warn "get: data field is deprecated, use getSolverData. Further warnings are suppressed"
55+
global getFactorDataWarnOnce = true
56+
end
57+
5158
getfield(x, :solverData)
5259
else
5360
getfield(x,f)
@@ -61,7 +68,12 @@ Base.setproperty!(x::DFGFactor,f::Symbol, val) = begin
6168
elseif f == :_internalId
6269
getfield(x,:_dfgNodeParams)._internalId = val
6370
elseif f == :data
64-
@warn "data field is deprecated, use ...TODO?"
71+
72+
if !(@isdefined setFactorDataWarnOnce)
73+
@warn "set: data field is deprecated, use ...TODO? Further warnings are suppressed"
74+
global setFactorDataWarnOnce = true
75+
end
76+
6577
setfield!(x,:solverData, val)
6678
else
6779
setfield!(x,f,val)

src/services/CompareUtils.jl

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,40 @@
1+
## @generated compare
2+
# Reference https://github.com/JuliaLang/julia/issues/4648
3+
4+
import Base.==
5+
6+
#=
7+
TODO <:InferenceVariable: please someone confirm this, I'm not fully up to date why softtype was created.
8+
For now abstract `InferenceVariable`s are considered equal if they are the same type (except for labels, that I feel are deprecated)
9+
If your implentation has aditional properties such as `DynPose2` with `ut::Int64` (microsecond time) or support different manifolds
10+
implement compare if needed. softtype sounds a bit if it can be a trait, or in the future, rather a normal julia type.
11+
=#
12+
==(a::InferenceVariable,b::InferenceVariable) = typeof(a) == typeof(b)
13+
14+
==(a::ConvolutionObject, b::ConvolutionObject) = typeof(a) == typeof(b)
15+
16+
==(a::FunctorInferenceType, b::FunctorInferenceType) = typeof(a) == typeof(b)
17+
18+
# TestCCW1{TestFunctorInferenceType1}
19+
20+
# Generate compares automatically for all in this union
21+
const GeneratedCompareUnion = Union{MeanMaxPPE, VariableNodeData, DFGNodeParams,
22+
DFGVariable, DFGVariableSummary, SkeletonDFGVariable,
23+
GenericFunctionNodeData,
24+
DFGFactor, DFGFactorSummary, SkeletonDFGFactor}
25+
26+
@generated function ==(x::T, y::T) where T <: GeneratedCompareUnion
27+
mapreduce(n -> :(x.$n == y.$n), (a,b)->:($a && $b), fieldnames(x))
28+
end
29+
30+
31+
##
132

233
function compareField(Allc, Bllc, syms)::Bool
3-
(!isdefined(Allc, syms) && !isdefined(Bllc, syms)) && return true
4-
!isdefined(Allc, syms) && return false
5-
!isdefined(Bllc, syms) && return false
6-
return eval(:($Allc.$syms == $Bllc.$syms))
34+
(!isdefined(Allc, syms) && !isdefined(Bllc, syms)) && return true
35+
!isdefined(Allc, syms) && return false
36+
!isdefined(Bllc, syms) && return false
37+
return eval(:($Allc.$syms == $Bllc.$syms))
738
end
839

940
"""
@@ -114,6 +145,25 @@ function compareAll(Al::T, Bl::T; show::Bool=true, skip::Vector{Symbol}=Symbol[]
114145
return true
115146
end
116147

148+
#Compare VariableNodeData
149+
function compare(a::VariableNodeData, b::VariableNodeData)
150+
a.val != b.val && @debug("val is not equal")==nothing && return false
151+
a.bw != b.bw && @debug("bw is not equal")==nothing && return false
152+
a.BayesNetOutVertIDs != b.BayesNetOutVertIDs && @debug("BayesNetOutVertIDs is not equal")==nothing && return false
153+
a.dimIDs != b.dimIDs && @debug("dimIDs is not equal")==nothing && return false
154+
a.dims != b.dims && @debug("dims is not equal")==nothing && return false
155+
a.eliminated != b.eliminated && @debug("eliminated is not equal")==nothing && return false
156+
a.BayesNetVertID != b.BayesNetVertID && @debug("BayesNetVertID is not equal")==nothing && return false
157+
a.separator != b.separator && @debug("separator is not equal")==nothing && return false
158+
a.initialized != b.initialized && @debug("initialized is not equal")==nothing && return false
159+
abs(a.inferdim - b.inferdim) > 1e-14 && @debug("inferdim is not equal")==nothing && return false
160+
a.ismargin != b.ismargin && @debug("ismargin is not equal")==nothing && return false
161+
a.dontmargin != b.dontmargin && @debug("dontmargin is not equal")==nothing && return false
162+
a.solveInProgress != b.solveInProgress && @debug("solveInProgress is not equal")==nothing && return false
163+
typeof(a.softtype) != typeof(b.softtype) && @debug("softtype is not equal")==nothing && return false
164+
return true
165+
end
166+
117167
"""
118168
$SIGNATURES
119169
@@ -155,6 +205,20 @@ function compareAllSpecial(A::T1,
155205
end
156206
end
157207

208+
209+
# Compare FunctionNodeData
210+
function compare(a::GenericFunctionNodeData{T1,S},b::GenericFunctionNodeData{T2,S}) where {T1, T2, S}
211+
# TODO -- beef up this comparison to include the gwp
212+
TP = true
213+
TP = TP && a.fncargvID == b.fncargvID
214+
TP = TP && a.eliminated == b.eliminated
215+
TP = TP && a.potentialused == b.potentialused
216+
TP = TP && a.edgeIDs == b.edgeIDs
217+
TP = TP && a.frommodule == b.frommodule
218+
# TP = TP && typeof(a.fnc) == typeof(b.fnc)
219+
return TP
220+
end
221+
158222
"""
159223
$SIGNATURES
160224
@@ -167,7 +231,7 @@ function compareFactor(A::DFGFactor,
167231
skipsamples::Bool=true,
168232
skipcompute::Bool=true )
169233
#
170-
TP = compareAll(A, B, skip=union([:attributes;:data;:_variableOrderSymbols;:_internalId],skip), show=show)
234+
TP = compareAll(A, B, skip=union([:attributes;:solverData;:_variableOrderSymbols;:_internalId],skip), show=show)
171235
# TP = TP & compareAll(A.attributes, B.attributes, skip=[:data;], show=show)
172236
TP = TP & compareAllSpecial(getSolverData(A), getSolverData(B), skip=union([:fnc;:_internalId], skip), show=show)
173237
TP = TP & compareAllSpecial(getSolverData(A).fnc, getSolverData(B).fnc, skip=union([:cpt;:measurement;:params;:varidx;:threadmodel], skip), show=show)
@@ -323,3 +387,16 @@ function compareFactorGraphs(fgA::G1,
323387

324388
return TP
325389
end
390+
391+
392+
# """
393+
# ==(x::T, y::T) where T <: AbstractPointParametricEst
394+
# Equality check for AbstractPointParametricEst.
395+
# """
396+
# @generated function ==(x::T, y::T) where T <: AbstractPointParametricEst
397+
# mapreduce(n -> :(x.$n == y.$n), (a,b)->:($a && $b), fieldnames(x))
398+
# end
399+
#
400+
# @generated function Base.:(==)(x::T, y::T) where T <: Union{DFGFactorSummary, DFGVariableSummary, SkeletonDFGVariable, SkeletonDFGFactor}
401+
# mapreduce(n -> :(x.$n == y.$n), (a,b)->:($a && $b), fieldnames(x))
402+
# end

src/services/DFGFactor.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,18 @@ function unpackFactor(dfg::G, packedProps::Dict{String, Any}, iifModule)::DFGFac
7373
return factor
7474
end
7575

76-
# Compare FunctionNodeData
77-
function compare(a::GenericFunctionNodeData{T1,S},b::GenericFunctionNodeData{T2,S}) where {T1, T2, S}
78-
# TODO -- beef up this comparison to include the gwp
79-
TP = true
80-
TP = TP && a.fncargvID == b.fncargvID
81-
TP = TP && a.eliminated == b.eliminated
82-
TP = TP && a.potentialused == b.potentialused
83-
TP = TP && a.edgeIDs == b.edgeIDs
84-
TP = TP && a.frommodule == b.frommodule
85-
# TP = TP && typeof(a.fnc) == typeof(b.fnc)
86-
return TP
87-
end
76+
# # Compare FunctionNodeData
77+
# function compare(a::GenericFunctionNodeData{T1,S},b::GenericFunctionNodeData{T2,S}) where {T1, T2, S}
78+
# # TODO -- beef up this comparison to include the gwp
79+
# TP = true
80+
# TP = TP && a.fncargvID == b.fncargvID
81+
# TP = TP && a.eliminated == b.eliminated
82+
# TP = TP && a.potentialused == b.potentialused
83+
# TP = TP && a.edgeIDs == b.edgeIDs
84+
# TP = TP && a.frommodule == b.frommodule
85+
# # TP = TP && typeof(a.fnc) == typeof(b.fnc)
86+
# return TP
87+
# end
8888

8989
#FIXME
9090
# """

src/services/DFGVariable.jl

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -101,23 +101,23 @@ function unpack(dfg::G, d::PackedVariableNodeData)::VariableNodeData where G <:
101101
st, d.initialized, d.inferdim, d.ismargin, d.dontmargin, d.solveInProgress, d.solvedCount)
102102
end
103103

104-
function compare(a::VariableNodeData, b::VariableNodeData)
105-
a.val != b.val && @debug("val is not equal")==nothing && return false
106-
a.bw != b.bw && @debug("bw is not equal")==nothing && return false
107-
a.BayesNetOutVertIDs != b.BayesNetOutVertIDs && @debug("BayesNetOutVertIDs is not equal")==nothing && return false
108-
a.dimIDs != b.dimIDs && @debug("dimIDs is not equal")==nothing && return false
109-
a.dims != b.dims && @debug("dims is not equal")==nothing && return false
110-
a.eliminated != b.eliminated && @debug("eliminated is not equal")==nothing && return false
111-
a.BayesNetVertID != b.BayesNetVertID && @debug("BayesNetVertID is not equal")==nothing && return false
112-
a.separator != b.separator && @debug("separator is not equal")==nothing && return false
113-
a.initialized != b.initialized && @debug("initialized is not equal")==nothing && return false
114-
abs(a.inferdim - b.inferdim) > 1e-14 && @debug("inferdim is not equal")==nothing && return false
115-
a.ismargin != b.ismargin && @debug("ismargin is not equal")==nothing && return false
116-
a.dontmargin != b.dontmargin && @debug("dontmargin is not equal")==nothing && return false
117-
a.solveInProgress != b.solveInProgress && @debug("solveInProgress is not equal")==nothing && return false
118-
typeof(a.softtype) != typeof(b.softtype) && @debug("softtype is not equal")==nothing && return false
119-
return true
120-
end
104+
# function compare(a::VariableNodeData, b::VariableNodeData)
105+
# a.val != b.val && @debug("val is not equal")==nothing && return false
106+
# a.bw != b.bw && @debug("bw is not equal")==nothing && return false
107+
# a.BayesNetOutVertIDs != b.BayesNetOutVertIDs && @debug("BayesNetOutVertIDs is not equal")==nothing && return false
108+
# a.dimIDs != b.dimIDs && @debug("dimIDs is not equal")==nothing && return false
109+
# a.dims != b.dims && @debug("dims is not equal")==nothing && return false
110+
# a.eliminated != b.eliminated && @debug("eliminated is not equal")==nothing && return false
111+
# a.BayesNetVertID != b.BayesNetVertID && @debug("BayesNetVertID is not equal")==nothing && return false
112+
# a.separator != b.separator && @debug("separator is not equal")==nothing && return false
113+
# a.initialized != b.initialized && @debug("initialized is not equal")==nothing && return false
114+
# abs(a.inferdim - b.inferdim) > 1e-14 && @debug("inferdim is not equal")==nothing && return false
115+
# a.ismargin != b.ismargin && @debug("ismargin is not equal")==nothing && return false
116+
# a.dontmargin != b.dontmargin && @debug("dontmargin is not equal")==nothing && return false
117+
# a.solveInProgress != b.solveInProgress && @debug("solveInProgress is not equal")==nothing && return false
118+
# typeof(a.softtype) != typeof(b.softtype) && @debug("softtype is not equal")==nothing && return false
119+
# return true
120+
# end
121121

122122
#FIXME
123123
# """
@@ -128,17 +128,17 @@ end
128128
# return DistributedFactorGraphs.compare(a,b)
129129
# end
130130

131-
"""
132-
==(x::T, y::T) where T <: AbstractPointParametricEst
133-
Equality check for AbstractPointParametricEst.
134-
"""
135-
@generated function ==(x::T, y::T) where T <: AbstractPointParametricEst
136-
mapreduce(n -> :(x.$n == y.$n), (a,b)->:($a && $b), fieldnames(x))
137-
end
138-
139-
@generated function Base.:(==)(x::T, y::T) where T <: Union{DFGFactorSummary, DFGVariableSummary, SkeletonDFGVariable, SkeletonDFGFactor}
140-
mapreduce(n -> :(x.$n == y.$n), (a,b)->:($a && $b), fieldnames(x))
141-
end
131+
# """
132+
# ==(x::T, y::T) where T <: AbstractPointParametricEst
133+
# Equality check for AbstractPointParametricEst.
134+
# """
135+
# @generated function ==(x::T, y::T) where T <: AbstractPointParametricEst
136+
# mapreduce(n -> :(x.$n == y.$n), (a,b)->:($a && $b), fieldnames(x))
137+
# end
138+
#
139+
# @generated function Base.:(==)(x::T, y::T) where T <: Union{DFGFactorSummary, DFGVariableSummary, SkeletonDFGVariable, SkeletonDFGFactor}
140+
# mapreduce(n -> :(x.$n == y.$n), (a,b)->:($a && $b), fieldnames(x))
141+
# end
142142

143143
#FIXME
144144
# """

test/compareTests.jl

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using Test
2+
using DistributedFactorGraphs
3+
using Dates
4+
5+
# VariableNodeData
6+
vnd1 = VariableNodeData(TestSofttype1())
7+
vnd2 = deepcopy(vnd1)
8+
vnd3 = VariableNodeData(TestSofttype2())
9+
10+
@test vnd1 == vnd2
11+
vnd2.val = zeros(1,1)
12+
@test vnd1 == vnd2
13+
vnd2.val[1] = 0.1
14+
@test !(vnd1 == vnd2)
15+
@test !(vnd1 == vnd3)
16+
17+
# MeanMaxPPE
18+
ppe1 = MeanMaxPPE(:default, [1.], [2.], [3.])
19+
ppe2 = deepcopy(ppe1)
20+
ppe3 = MeanMaxPPE(:default, [2.], [3.], [4.])
21+
22+
@test ppe1 == ppe2
23+
ppe2.max[1] = 0.1
24+
@test !(ppe1 == ppe2)
25+
@test !(ppe1 == ppe3)
26+
27+
28+
# DFGVariable
29+
v1 = DFGVariable(:x1, TestSofttype1())
30+
v2 = deepcopy(v1)
31+
v3 = DFGVariable(:x2, TestSofttype2())
32+
33+
@test v1 == v2
34+
v2.solvable = 0
35+
@test !(v1 == v2)
36+
@test !(v1 == v3)
37+
@test !(DFGVariable(:x1, TestSofttype1()) == DFGVariable(:x1, TestSofttype2()))
38+
39+
# GenericFunctionNodeData
40+
gfnd1 = GenericFunctionNodeData([:a,:b], true, true, [1,2], :symbol, sin)
41+
gfnd2 = deepcopy(gfnd1)
42+
gfnd3 = GenericFunctionNodeData([:a,:b], true, true, [1,2], :symbol, cos)
43+
44+
@test gfnd1 == gfnd2
45+
gfnd2.fncargvID = [:a, :b]
46+
@test gfnd1 == gfnd2
47+
gfnd2.fncargvID = [:b, :a]
48+
@test !(gfnd1 == gfnd2)
49+
@test !(gfnd1 == gfnd3)
50+
51+
# DFGFactor
52+
f1 = DFGFactor(:f1, [:a, :b], gfnd1)
53+
f2 = deepcopy(f1)
54+
f3 = DFGFactor(:f1, [:b, :a], gfnd1)
55+
56+
@test f1 == f2
57+
f2.solverData = gfnd1
58+
@test f1 == f2
59+
f2.solverData = gfnd2
60+
@test !(f1 == f2)
61+
@test !(f1 == f3)

test/fileDFGTests.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ dfg = LightDFG{SolverParams}()
4444
@test issetequal(ls(dfg), ls(retDFG))
4545
@test issetequal(lsf(dfg), lsf(retDFG))
4646
for var in ls(dfg)
47-
@test_broken getVariable(dfg, var) == getVariable(retDFG, var)
47+
@test getVariable(dfg, var) == getVariable(retDFG, var)
4848
end
4949
for fact in lsf(dfg)
50-
@test_broken getFactor(dfg, fact) == getFactor(retDFG, fact)
50+
@test getFactor(dfg, fact) == getFactor(retDFG, fact)
5151
end
5252

5353
@test length(getBigDataEntries(getVariable(retDFG, :x1))) == 1

0 commit comments

Comments
 (0)