Skip to content

Commit d8a5695

Browse files
authored
Refactoring Factors: data -> state, observation and cache (#1127)
* Spike on refactoring Factors * defFactorType macro (#1077) * defFactorFunction macro * update defFactorType * Update DFGFactor.jl * Fix tests * towards consolidated FactorCompute constructor * Intermediate removing factor solverData * rm factor data * rename to AbstractFactorObservation and FactorOperationalMemory -> FactorSolverCache * fix test and factor solverData related issues * deprecate GenericFunctionNodeData and related * fix broken docs * fix defFactorType -> getManifold * update news and bump to v0.27 * fix formatting
1 parent 05ad44d commit d8a5695

21 files changed

+655
-518
lines changed

NEWS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ Listing news on any major breaking changes in DFG. For regular changes, see int
66
- Deprecate `updateFactor!` for `mergeFactor!`, note `merege` returns number of nodes updated/added.
77
- Rename BlobEntry to Blobentry, see #1123.
88
- Rename BlobStore to Blobstore, see #1124.
9+
- Refactor the Factor solver data structure, see #1127:
10+
- Deprecated GenericFunctionNodeData, PackedFunctionNodeData, FunctionNodeData, and all functions related factor.solverData.
11+
- Replaced by 3 seperete types: Observation, State, and Cache
12+
- This is used internally be the solver and should not affect the average user of DFG.
13+
- Rename FactorOperationalMemory -> FactorSolverCache
14+
- Rename AbstractFactor -> AbstractFactorObservation (keeping both around)
915

1016
# v0.26
1117
- Graph structure plotting now uses GraphMakie.jl instead of GraphPlot.jl. Update by replacing `using GraphPlot` with `using GraphMakie`.

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "DistributedFactorGraphs"
22
uuid = "b5cc3c7e-6572-11e9-2517-99fb8daf2f04"
3-
version = "0.26.0"
3+
version = "0.27.0"
44

55
[deps]
66
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
@@ -19,6 +19,7 @@ ManifoldsBase = "3362f125-f0bb-47a3-aa74-596ffd7ef2fb"
1919
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
2020
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
2121
ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca"
22+
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
2223
RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd"
2324
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
2425
SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce"
@@ -61,6 +62,7 @@ ManifoldsBase = "0.14, 0.15, 1"
6162
OrderedCollections = "1.4"
6263
Pkg = "1.4, 1.5"
6364
ProgressMeter = "1"
65+
Random = "1.10"
6466
RecursiveArrayTools = "2, 3"
6567
Reexport = "1"
6668
SHA = "0.7, 1"

src/Common.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
_getmodule(t::T) where {T} = T.name.module
44
_getname(t::T) where {T} = T.name.name
55

6-
function convertPackedType(t::Union{T, Type{T}}) where {T <: AbstractFactor}
6+
function convertPackedType(t::Union{T, Type{T}}) where {T <: AbstractFactorObservation}
77
return getfield(_getmodule(t), Symbol("Packed$(_getname(t))"))
88
end
9-
function convertStructType(::Type{PT}) where {PT <: AbstractPackedFactor}
9+
function convertStructType(::Type{PT}) where {PT <: AbstractPackedFactorObservation}
1010
# see #668 for expanded reasoning. PT may be ::UnionAll if the type is of template type.
1111
ptt = PT isa DataType ? PT.name.name : PT
1212
moduleName = PT isa DataType ? PT.name.module : Main

src/Deprecated.jl

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
## ================================================================================
22
## Deprecated in v0.27
33
##=================================================================================
4+
export AbstractFactor
5+
const AbstractFactor = AbstractFactorObservation
6+
7+
export AbstractPackedFactor
8+
const AbstractPackedFactor = AbstractPackedFactorObservation
9+
10+
export FactorOperationalMemory
11+
const FactorOperationalMemory = FactorSolverCache
412

513
@deprecate getNeighborhood(args...; kwargs...) listNeighborhood(args...; kwargs...)
614
@deprecate addBlob!(store::AbstractBlobstore, blobId::UUID, data, ::String) addBlob!(
@@ -73,6 +81,10 @@ function updateVariableSolverData!(
7381
fields::Vector{Symbol} = Symbol[];
7482
warn_if_absent::Bool = true,
7583
)
84+
Base.depwarn(
85+
"updateVariableSolverData! is deprecated, use mergeVariableState! or copytoVariableState! instead",
86+
:updateVariableSolverData!,
87+
)
7688
#This is basically just setSolverData
7789
var = getVariable(dfg, variablekey)
7890
warn_if_absent &&
@@ -190,6 +202,194 @@ function updateVariableSolverData!(
190202
end
191203
end
192204

205+
## factor refactor deprecations
206+
Base.@kwdef mutable struct GenericFunctionNodeData{
207+
T <: Union{
208+
<:AbstractPackedFactorObservation,
209+
<:AbstractFactorObservation,
210+
<:FactorSolverCache,
211+
},
212+
}
213+
eliminated::Bool = false
214+
potentialused::Bool = false
215+
edgeIDs::Vector{Int} = Int[]
216+
fnc::T
217+
multihypo::Vector{Float64} = Float64[] # TODO re-evaluate after refactoring w #477
218+
certainhypo::Vector{Int} = Int[]
219+
nullhypo::Float64 = 0.0
220+
solveInProgress::Int = 0
221+
inflation::Float64 = 0.0
222+
end
223+
224+
function FactorCompute(
225+
label::Symbol,
226+
timestamp::Union{DateTime, ZonedDateTime},
227+
nstime::Nanosecond,
228+
tags::Set{Symbol},
229+
solverData::GenericFunctionNodeData,
230+
solvable::Int,
231+
variableOrder::Union{Vector{Symbol}, Tuple};
232+
observation = getFactorType(solverData),
233+
state::FactorState = FactorState(),
234+
solvercache::Base.RefValue{<:FactorSolverCache} = Ref{FactorSolverCache}(),
235+
id::Union{UUID, Nothing} = nothing,
236+
smallData::Dict{Symbol, SmallDataTypes} = Dict{Symbol, SmallDataTypes}(),
237+
)
238+
error(
239+
"This constructor is deprecated, use FactorCompute(label, variableOrder, solverData; ...) instead",
240+
)
241+
return FactorCompute(
242+
id,
243+
label,
244+
tags,
245+
Tuple(variableOrder),
246+
timestamp,
247+
nstime,
248+
Ref(solverData),
249+
Ref(solvable),
250+
smallData,
251+
observation,
252+
state,
253+
solvercache,
254+
)
255+
end
256+
257+
export getSolverData, setSolverData!
258+
259+
function getSolverData(f::FactorCompute)
260+
return error(
261+
"getSolverData(f::FactorCompute) is obsolete, use getState, getObservation, or getCache instead",
262+
)
263+
end
264+
265+
function setSolverData!(f::FactorCompute, data::GenericFunctionNodeData)
266+
return error(
267+
"setSolverData!(f::FactorCompute, data::GenericFunctionNodeData) is obsolete, use setState!, or setCache! instead",
268+
)
269+
end
270+
271+
@deprecate unpackFactor(dfg::AbstractDFG, factor::FactorDFG; skipVersionCheck::Bool = false) unpackFactor(
272+
factor;
273+
skipVersionCheck,
274+
)
275+
276+
@deprecate rebuildFactorMetadata!(args...; kwargs...) rebuildFactorCache!(
277+
args...;
278+
kwargs...,
279+
)
280+
281+
export reconstFactorData
282+
function reconstFactorData end
283+
284+
function decodePackedType(
285+
dfg::AbstractDFG,
286+
varOrder::AbstractVector{Symbol},
287+
::Type{T},
288+
packeddata::GenericFunctionNodeData{PT},
289+
) where {T <: FactorSolverCache, PT}
290+
error("decodePackedType is obsolete")
291+
#
292+
# TODO, to solve IIF 1424
293+
# variables = map(lb->getVariable(dfg, lb), varOrder)
294+
295+
# Also look at parentmodule
296+
usrtyp = convertStructType(PT)
297+
fulltype = DFG.FunctionNodeData{T{usrtyp}}
298+
factordata = reconstFactorData(dfg, varOrder, fulltype, packeddata)
299+
return factordata
300+
end
301+
302+
export _packSolverData
303+
function _packSolverData(f::FactorCompute, fnctype::AbstractFactorObservation)
304+
#
305+
error("_packSolverData is deprecated, use seperate packing of observation #TODO")
306+
packtype = convertPackedType(fnctype)
307+
try
308+
packed = convert(PackedFunctionNodeData{packtype}, getSolverData(f)) #TODO getSolverData
309+
packedJson = packed
310+
return packedJson
311+
catch ex
312+
io = IOBuffer()
313+
showerror(io, ex, catch_backtrace())
314+
err = String(take!(io))
315+
msg = "Error while packing '$(f.label)' as '$fnctype', please check the unpacking/packing converters for this factor - \r\n$err"
316+
error(msg)
317+
end
318+
end
319+
320+
export GenericFunctionNodeData, PackedFunctionNodeData, FunctionNodeData
321+
322+
const PackedFunctionNodeData{T} =
323+
GenericFunctionNodeData{T} where {T <: AbstractPackedFactorObservation}
324+
function PackedFunctionNodeData(args...; kw...)
325+
error("PackedFunctionNodeData is obsolete")
326+
return PackedFunctionNodeData{typeof(args[4])}(args...; kw...)
327+
end
328+
329+
const FunctionNodeData{T} = GenericFunctionNodeData{
330+
T,
331+
} where {T <: Union{<:AbstractFactorObservation, <:FactorSolverCache}}
332+
FunctionNodeData(args...; kw...) = FunctionNodeData{typeof(args[4])}(args...; kw...)
333+
334+
# this is the GenericFunctionNodeData for packed types
335+
#TODO deprecate FactorData in favor of FactorState (with no more distinction between packed and compute)
336+
const FactorData = PackedFunctionNodeData{AbstractPackedFactorObservation}
337+
338+
function FactorCompute(
339+
label::Symbol,
340+
variableOrder::Union{Vector{Symbol}, Tuple},
341+
solverData::GenericFunctionNodeData;
342+
tags::Set{Symbol} = Set{Symbol}(),
343+
timestamp::Union{DateTime, ZonedDateTime} = now(localzone()),
344+
solvable::Int = 1,
345+
nstime::Nanosecond = Nanosecond(0),
346+
id::Union{UUID, Nothing} = nothing,
347+
smallData::Dict{Symbol, SmallDataTypes} = Dict{Symbol, SmallDataTypes}(),
348+
)
349+
Base.depwarn(
350+
"`FactorCompute` constructor with `GenericFunctionNodeData` is deprecated. observation, state, and solvercache should be provided explicitly.",
351+
:FactorCompute,
352+
)
353+
observation = getFactorType(solverData)
354+
state = FactorState(
355+
solverData.eliminated,
356+
solverData.potentialused,
357+
solverData.multihypo,
358+
solverData.certainhypo,
359+
solverData.nullhypo,
360+
solverData.solveInProgress,
361+
solverData.inflation,
362+
)
363+
364+
if solverData.fnc isa FactorSolverCache
365+
solvercache = solverData.fnc
366+
else
367+
solvercache = nothing
368+
end
369+
370+
return FactorCompute(
371+
label,
372+
Tuple(variableOrder),
373+
observation,
374+
state,
375+
solvercache;
376+
id,
377+
timestamp,
378+
nstime,
379+
tags,
380+
smallData,
381+
solvable,
382+
)
383+
end
384+
385+
# Deprecated check usefull? # packedFnc = fncStringToData(factor.fnctype, factor.data)
386+
# Deprecated check usefull? # decodeType = getFactorOperationalMemoryType(dfg)
387+
# Deprecated check usefull? # fullFactorData = decodePackedType(dfg, factor._variableOrderSymbols, decodeType, packedFnc)
388+
function fncStringToData(args...; kwargs...)
389+
@warn "fncStringToData is obsolete, called with" args kwargs
390+
return error("fncStringToData is obsolete.")
391+
end
392+
193393
## ================================================================================
194394
## Deprecated in v0.25
195395
##=================================================================================

src/DistributedFactorGraphs.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ using Base
1818
using Base64
1919
using DocStringExtensions
2020
using Dates
21+
using Random
2122
using TimeZones
2223
using Distributions
2324
using Reexport
@@ -42,7 +43,7 @@ using Tables
4243

4344
# used for @defVariable
4445
import ManifoldsBase
45-
import ManifoldsBase: AbstractManifold, manifold_dimension
46+
using ManifoldsBase: AbstractManifold, manifold_dimension
4647
export AbstractManifold, manifold_dimension
4748

4849
import RecursiveArrayTools: ArrayPartition
@@ -165,7 +166,8 @@ export InferenceVariable
165166
export getSolverDataDict, setSolverData!
166167
export getVariableType, getVariableTypeName
167168

168-
export getSolverData
169+
export getObservation
170+
export getState, getFactorState
169171

170172
export getVariableType
171173

@@ -246,10 +248,9 @@ export @format_str
246248
# Factors
247249
##------------------------------------------------------------------------------
248250
# Factor Data
249-
export GenericFunctionNodeData, PackedFunctionNodeData, FunctionNodeData
250-
export AbstractFactor, AbstractPackedFactor
251+
export AbstractFactorObservation, AbstractPackedFactorObservation
251252
export AbstractPrior, AbstractRelative, AbstractRelativeMinimize, AbstractManifoldMinimize
252-
export FactorOperationalMemory
253+
export FactorSolverCache
253254

254255
# accessors
255256
export getVariableOrder
@@ -261,7 +262,7 @@ export mergeVariableData!, mergeGraphVariableData!
261262
# Serialization type conversion
262263
export convertPackedType, convertStructType
263264

264-
export reconstFactorData
265+
export pack, unpack, packDistribution, unpackDistribution
265266

266267
##------------------------------------------------------------------------------
267268
## Other utility functions
@@ -285,7 +286,7 @@ export findClosestTimestamp, findVariableNearTimestamp
285286

286287
# Serialization
287288
export packVariable, unpackVariable, packFactor, unpackFactor
288-
export rebuildFactorMetadata!
289+
export rebuildFactorCache!
289290
export @defVariable
290291

291292
# File import and export
@@ -301,7 +302,6 @@ export compare,
301302
compareField,
302303
compareFields,
303304
compareAll,
304-
compareAllSpecial,
305305
compareVariable,
306306
compareFactor,
307307
compareAllVariables,

0 commit comments

Comments
 (0)