|
| 1 | + |
| 2 | +function compareField(Allc, Bllc, syms)::Bool |
| 3 | + return eval(:($Allc.$syms == $Bllc.$syms)) |
| 4 | +end |
| 5 | + |
| 6 | +""" |
| 7 | + $(SIGNATURES) |
| 8 | +
|
| 9 | +Compare the all fields of T that are not in `skip` for objects `Al` and `Bl`. |
| 10 | +
|
| 11 | +TODO > add to func_ref.md |
| 12 | +""" |
| 13 | +function compareFields(Al::T, |
| 14 | + Bl::T; |
| 15 | + show::Bool=true, |
| 16 | + skip::Vector{Symbol}=Symbol[] )::Bool where {T} |
| 17 | + for field in fieldnames(T) |
| 18 | + (field in skip) && continue |
| 19 | + tp = compareField(Al, Bl, field) |
| 20 | + show && @debug(" $tp : $field")==nothing |
| 21 | + !tp && return false |
| 22 | + end |
| 23 | + return true |
| 24 | +end |
| 25 | + |
| 26 | +function compareFields(Al::T, |
| 27 | + Bl::T; |
| 28 | + show::Bool=true, |
| 29 | + skip::Vector{Symbol}=Symbol[] )::Bool where {T <: Union{Number, AbstractString}} |
| 30 | + # |
| 31 | + return Al == Bl |
| 32 | +end |
| 33 | + |
| 34 | +function compareAll(Al::T, |
| 35 | + Bl::T; |
| 36 | + show::Bool=true, |
| 37 | + skip::Vector{Symbol}=Symbol[] )::Bool where {T <: Union{AbstractString,Symbol}} |
| 38 | + # |
| 39 | + return Al == Bl |
| 40 | +end |
| 41 | + |
| 42 | +function compareAll(Al::T, |
| 43 | + Bl::T; |
| 44 | + show::Bool=true, |
| 45 | + skip::Vector{Symbol}=Symbol[] )::Bool where {T <: Union{Array{<:Number}, Number}} |
| 46 | + # |
| 47 | + (length(Al) != length(Bl)) && return false |
| 48 | + return norm(Al - Bl) < 1e-6 |
| 49 | +end |
| 50 | + |
| 51 | +function compareAll(Al::T, |
| 52 | + Bl::T; |
| 53 | + show::Bool=true, |
| 54 | + skip::Vector{Symbol}=Symbol[] )::Bool where {T <: Array} |
| 55 | + # |
| 56 | + (length(Al) != length(Bl)) && return false |
| 57 | + for i in 1:length(Al) |
| 58 | + !compareAll(Al[i],Bl[i], show=false) && return false |
| 59 | + end |
| 60 | + return true |
| 61 | +end |
| 62 | + |
| 63 | + |
| 64 | +""" |
| 65 | + $(SIGNATURES) |
| 66 | +
|
| 67 | +Recursively compare the all fields of T that are not in `skip` for objects `Al` and `Bl`. |
| 68 | +
|
| 69 | +TODO > add to func_ref.md |
| 70 | +""" |
| 71 | +function compareAll(Al::T, |
| 72 | + Bl::T; |
| 73 | + show::Bool=true, |
| 74 | + skip::Vector{Symbol}=Symbol[] )::Bool where {T <: Tuple} |
| 75 | + # |
| 76 | + length(Al) != length(Bl) && return false |
| 77 | + for i in 1:length(Al) |
| 78 | + !compareAll(Al[i], Bl[i], show=show, skip=skip) && return false |
| 79 | + end |
| 80 | + return true |
| 81 | +end |
| 82 | + |
| 83 | +function compareAll(Al::T, |
| 84 | + Bl::T; |
| 85 | + show::Bool=true, |
| 86 | + skip::Vector{Symbol}=Symbol[] )::Bool where {T <: Dict} |
| 87 | + # |
| 88 | + (length(Al) != length(Bl)) && return false |
| 89 | + for (id, val) in Al |
| 90 | + (Symbol(id) in skip) && continue |
| 91 | + !compareAll(val, Bl[id], show=show, skip=skip) && return false |
| 92 | + end |
| 93 | + return true |
| 94 | +end |
| 95 | + |
| 96 | +function compareAll(Al::T, Bl::T; show::Bool=true, skip::Vector{Symbol}=Symbol[])::Bool where T |
| 97 | + @debug "Comparing types $T:" |
| 98 | + @debug " Al = $Al" |
| 99 | + @debug " Bl = $Bl" |
| 100 | + !compareFields(Al, Bl, show=show, skip=skip) && return false |
| 101 | + for field in fieldnames(T) |
| 102 | + field in skip && continue |
| 103 | + @debug(" Checking field: $field") |
| 104 | + Ad = eval(:($Al.$field)) |
| 105 | + Bd = eval(:($Bl.$field)) |
| 106 | + !compareAll(Ad, Bd, show=show, skip=skip) && return false |
| 107 | + end |
| 108 | + return true |
| 109 | +end |
| 110 | + |
| 111 | +""" |
| 112 | + $SIGNATURES |
| 113 | +
|
| 114 | +Compare that all fields are the same in a `::FactorGraph` variable. |
| 115 | +""" |
| 116 | +function compareVariable(A::DFGVariable, |
| 117 | + B::DFGVariable; |
| 118 | + skip::Vector{Symbol}=Symbol[], |
| 119 | + show::Bool=true, |
| 120 | + skipsamples::Bool=true )::Bool |
| 121 | + # |
| 122 | + skiplist = union([:attributes;:solverDataDict;:_internalId],skip) |
| 123 | + TP = compareAll(A, B, skip=skiplist, show=show) |
| 124 | + varskiplist = skipsamples ? [:val; :bw] : Symbol[] |
| 125 | + skiplist = union([:softtype;],varskiplist) |
| 126 | + union!(skiplist, skip) |
| 127 | + TP = TP && compareAll(A.solverDataDict, B.solverDataDict, skip=skiplist, show=show) |
| 128 | + |
| 129 | + Ad = getData(A) |
| 130 | + Bd = getData(B) |
| 131 | + |
| 132 | + # TP = TP && compareAll(A.attributes, B.attributes, skip=[:softtype;], show=show) |
| 133 | + varskiplist = union(varskiplist, [:softtype;:_internalId]) |
| 134 | + union!(varskiplist, skip) |
| 135 | + TP = TP && compareAll(Ad, Bd, skip=varskiplist, show=show) |
| 136 | + TP = TP && typeof(Ad.softtype) == typeof(Bd.softtype) |
| 137 | + TP = TP && compareAll(Ad.softtype, Bd.softtype, show=show, skip=skip) |
| 138 | + return TP |
| 139 | +end |
| 140 | + |
| 141 | +function compareAllSpecial(A::T1, |
| 142 | + B::T2; |
| 143 | + skip=Symbol[], |
| 144 | + show::Bool=true) where {T1 <: GenericFunctionNodeData, T2 <: GenericFunctionNodeData} |
| 145 | + if T1 != T2 |
| 146 | + return false |
| 147 | + else |
| 148 | + return compareAll(A, B, skip=skip, show=show) |
| 149 | + end |
| 150 | +end |
| 151 | + |
| 152 | +""" |
| 153 | + $SIGNATURES |
| 154 | +
|
| 155 | +Compare that all fields are the same in a `::FactorGraph` factor. |
| 156 | +""" |
| 157 | +function compareFactor(A::DFGFactor, |
| 158 | + B::DFGFactor; |
| 159 | + show::Bool=true, |
| 160 | + skip::Vector{Symbol}=Symbol[], |
| 161 | + skipsamples::Bool=true, |
| 162 | + skipcompute::Bool=true ) |
| 163 | + # |
| 164 | + @info show |
| 165 | + TP = compareAll(A, B, skip=union([:attributes;:data;:_variableOrderSymbols;:_internalId],skip), show=show) |
| 166 | + # TP = TP & compareAll(A.attributes, B.attributes, skip=[:data;], show=show) |
| 167 | + TP = TP & compareAllSpecial(getData(A), getData(B), skip=union([:fnc;:_internalId], skip), show=show) |
| 168 | + TP = TP & compareAllSpecial(getData(A).fnc, getData(B).fnc, skip=union([:cpt;:measurement;:params;:varidx;:threadmodel], skip), show=show) |
| 169 | + TP = TP & (skipsamples || compareAll(getData(A).fnc.measurement, getData(B).fnc.measurement, show=show, skip=skip)) |
| 170 | + TP = TP & (skipcompute || compareAll(getData(A).fnc.params, getData(B).fnc.params, show=show, skip=skip)) |
| 171 | + TP = TP & (skipcompute || compareAll(getData(A).fnc.varidx, getData(B).fnc.varidx, show=show, skip=skip)) |
| 172 | + |
| 173 | + return TP |
| 174 | +end |
| 175 | + # Ad = getData(A) |
| 176 | + # Bd = getData(B) |
| 177 | + # TP = compareAll(A, B, skip=[:attributes;:data], show=show) |
| 178 | + # TP &= compareAll(A.attributes, B.attributes, skip=[:data;], show=show) |
| 179 | + # TP &= compareAllSpecial(getData(A).fnc, getData(B).fnc, skip=[:cpt;], show=show) |
| 180 | + # TP &= compareAll(getData(A).fnc.cpt, getData(B).fnc.cpt, show=show) |
| 181 | + |
| 182 | + |
| 183 | +""" |
| 184 | + $SIGNATURES |
| 185 | +
|
| 186 | +Compare all variables in both `::FactorGraph`s A and B. |
| 187 | +
|
| 188 | +Notes |
| 189 | +- A and B should all the same variables and factors. |
| 190 | +
|
| 191 | +Related: |
| 192 | +
|
| 193 | +`compareFactorGraphs`, `compareSimilarVariables`, `compareVariable`, `ls` |
| 194 | +""" |
| 195 | +function compareAllVariables(fgA::G1, |
| 196 | + fgB::G2; |
| 197 | + skip::Vector{Symbol}=Symbol[], |
| 198 | + show::Bool=true, |
| 199 | + skipsamples::Bool=true )::Bool where {G1 <: AbstractDFG, G2 <: AbstractDFG} |
| 200 | + # get all the variables in A or B |
| 201 | + xlA = getVariableIds(fgA) |
| 202 | + xlB = getVariableIds(fgB) |
| 203 | + vars = union(xlA, xlB) |
| 204 | + |
| 205 | + # compare all variables exist in both A and B |
| 206 | + TP = length(xlA) == length(xlB) |
| 207 | + for xla in xlA |
| 208 | + TP &= xla in xlB |
| 209 | + end |
| 210 | + # slightly redundant, but repeating opposite direction anyway |
| 211 | + for xlb in xlB |
| 212 | + TP &= xlb in xlA |
| 213 | + end |
| 214 | + |
| 215 | + # compare each variable is the same in both A and B |
| 216 | + for var in vars |
| 217 | + TP = TP && compareVariable(DFG.getVariable(fgA, var), DFG.getVariable(fgB, var), skipsamples=skipsamples, skip=skip) |
| 218 | + end |
| 219 | + |
| 220 | + # return comparison result |
| 221 | + return TP |
| 222 | +end |
| 223 | + |
| 224 | +""" |
| 225 | + $SIGNATURES |
| 226 | +
|
| 227 | +Compare similar labels between `::FactorGraph`s A and B. |
| 228 | +
|
| 229 | +Notes |
| 230 | +- At least one variable label should exist in both A and B. |
| 231 | +
|
| 232 | +Related: |
| 233 | +
|
| 234 | +`compareFactorGraphs`, `compareAllVariables`, `compareSimilarFactors`, `compareVariable`, `ls`. |
| 235 | +""" |
| 236 | +function compareSimilarVariables(fgA::G1, |
| 237 | + fgB::G2; |
| 238 | + skip::Vector{Symbol}=Symbol[], |
| 239 | + show::Bool=true, |
| 240 | + skipsamples::Bool=true )::Bool where {G1 <: AbstractDFG, G2 <: AbstractDFG} |
| 241 | + # |
| 242 | + xlA = getVariableIds(fgA) |
| 243 | + xlB = getVariableIds(fgB) |
| 244 | + |
| 245 | + # find common variables |
| 246 | + xlAB = intersect(xlA, xlB) |
| 247 | + TP = length(xlAB) > 0 |
| 248 | + |
| 249 | + # compare the common set |
| 250 | + for var in xlAB |
| 251 | + @info var |
| 252 | + TP &= compareVariable(DFG.getVariable(fgA, var), DFG.getVariable(fgB, var), skipsamples=skipsamples, skip=skip) |
| 253 | + end |
| 254 | + |
| 255 | + # return comparison result |
| 256 | + return TP |
| 257 | +end |
| 258 | + |
| 259 | +""" |
| 260 | + $SIGNATURES |
| 261 | +
|
| 262 | +Compare similar factors between `::FactorGraph`s A and B. |
| 263 | +
|
| 264 | +Related: |
| 265 | +
|
| 266 | +`compareFactorGraphs`, `compareSimilarVariables`, `compareAllVariables`, `ls`. |
| 267 | +""" |
| 268 | +function compareSimilarFactors(fgA::G1, |
| 269 | + fgB::G2; |
| 270 | + skipsamples::Bool=true, |
| 271 | + skipcompute::Bool=true, |
| 272 | + show::Bool=true )::Bool where {G1 <: AbstractDFG, G2 <: AbstractDFG} |
| 273 | + # |
| 274 | + xlA = getFactorIds(fgA) |
| 275 | + xlB = getFactorIds(fgB) |
| 276 | + |
| 277 | + # find common variables |
| 278 | + xlAB = intersect(xlA, xlB) |
| 279 | + TP = length(xlAB) > 0 |
| 280 | + |
| 281 | + # compare the common set |
| 282 | + for var in xlAB |
| 283 | + TP = TP && compareFactor(DFG.getFactor(fgA, var), getFactor(fgB, var), skipsamples=skipsamples, skipcompute=skipcompute, show=show) |
| 284 | + end |
| 285 | + |
| 286 | + # return comparison result |
| 287 | + return TP |
| 288 | +end |
| 289 | + |
| 290 | +""" |
| 291 | + $SIGNATURES |
| 292 | +
|
| 293 | +Compare and return if two factor graph objects are the same, by comparing similar variables and factors. |
| 294 | +
|
| 295 | +Notes: |
| 296 | +- Default items to skip with `skipsamples`, `skipcompute`. |
| 297 | +- User defined fields to skip can be specified with `skip::Vector{Symbol}`. |
| 298 | +
|
| 299 | +Related: |
| 300 | +
|
| 301 | +`compareSimilarVariables`, `compareSimilarFactors`, `compareAllVariables`, `ls`. |
| 302 | +""" |
| 303 | +function compareFactorGraphs(fgA::G1, |
| 304 | + fgB::G2; |
| 305 | + skipsamples::Bool=true, |
| 306 | + skipcompute::Bool=true, |
| 307 | + skip::Vector{Symbol}=Symbol[], |
| 308 | + show::Bool=true )::Bool where {G1 <: AbstractDFG, G2 <: AbstractDFG} |
| 309 | + # |
| 310 | + skiplist = Symbol[:g;:bn;:IDs;:fIDs;:id;:nodeIDs;:factorIDs;:fifo;:solverParams] |
| 311 | + skiplist = union(skiplist, skip) |
| 312 | + @warn "compareFactorGraphs will skip comparisons on: $skiplist" |
| 313 | + |
| 314 | + TP = compareAll(fgA, fgB, skip=skiplist, show=show) |
| 315 | + TP = TP && compareSimilarVariables(fgA, fgB, skipsamples=skipsamples, show=show, skip=skiplist ) |
| 316 | + TP = TP && compareSimilarFactors(fgA, fgB, skipsamples=skipsamples, skipcompute=skipcompute, show=show ) |
| 317 | + TP = TP && compareAll(fgA.solverParams, fgB.solverParams, skip=skiplist) |
| 318 | + |
| 319 | + return TP |
| 320 | +end |
0 commit comments