Skip to content

Refactoring Factors: data -> state, observation and cache #1127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

# v0.26
- Graph structure plotting now uses GraphMakie.jl instead of GraphPlot.jl. Update by replacing `using GraphPlot` with `using GraphMakie`.
Expand Down
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "DistributedFactorGraphs"
uuid = "b5cc3c7e-6572-11e9-2517-99fb8daf2f04"
version = "0.26.0"
version = "0.27.0"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand All @@ -19,6 +19,7 @@ ManifoldsBase = "3362f125-f0bb-47a3-aa74-596ffd7ef2fb"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce"
Expand Down Expand Up @@ -61,6 +62,7 @@ ManifoldsBase = "0.14, 0.15, 1"
OrderedCollections = "1.4"
Pkg = "1.4, 1.5"
ProgressMeter = "1"
Random = "1.10"
RecursiveArrayTools = "2, 3"
Reexport = "1"
SHA = "0.7, 1"
Expand Down
4 changes: 2 additions & 2 deletions src/Common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
_getmodule(t::T) where {T} = T.name.module
_getname(t::T) where {T} = T.name.name

function convertPackedType(t::Union{T, Type{T}}) where {T <: AbstractFactor}
function convertPackedType(t::Union{T, Type{T}}) where {T <: AbstractFactorObservation}
return getfield(_getmodule(t), Symbol("Packed$(_getname(t))"))
end
function convertStructType(::Type{PT}) where {PT <: AbstractPackedFactor}
function convertStructType(::Type{PT}) where {PT <: AbstractPackedFactorObservation}
# see #668 for expanded reasoning. PT may be ::UnionAll if the type is of template type.
ptt = PT isa DataType ? PT.name.name : PT
moduleName = PT isa DataType ? PT.name.module : Main
Expand Down
200 changes: 200 additions & 0 deletions src/Deprecated.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
## ================================================================================
## Deprecated in v0.27
##=================================================================================
export AbstractFactor
const AbstractFactor = AbstractFactorObservation

export AbstractPackedFactor
const AbstractPackedFactor = AbstractPackedFactorObservation

export FactorOperationalMemory
const FactorOperationalMemory = FactorSolverCache

@deprecate getNeighborhood(args...; kwargs...) listNeighborhood(args...; kwargs...)
@deprecate addBlob!(store::AbstractBlobstore, blobId::UUID, data, ::String) addBlob!(
Expand Down Expand Up @@ -73,6 +81,10 @@
fields::Vector{Symbol} = Symbol[];
warn_if_absent::Bool = true,
)
Base.depwarn(
"updateVariableSolverData! is deprecated, use mergeVariableState! or copytoVariableState! instead",
:updateVariableSolverData!,
)
#This is basically just setSolverData
var = getVariable(dfg, variablekey)
warn_if_absent &&
Expand Down Expand Up @@ -190,6 +202,194 @@
end
end

## factor refactor deprecations
Base.@kwdef mutable struct GenericFunctionNodeData{
T <: Union{
<:AbstractPackedFactorObservation,
<:AbstractFactorObservation,
<:FactorSolverCache,
},
}
eliminated::Bool = false
potentialused::Bool = false
edgeIDs::Vector{Int} = Int[]
fnc::T
multihypo::Vector{Float64} = Float64[] # TODO re-evaluate after refactoring w #477
certainhypo::Vector{Int} = Int[]
nullhypo::Float64 = 0.0
solveInProgress::Int = 0
inflation::Float64 = 0.0
end

function FactorCompute(

Check warning on line 224 in src/Deprecated.jl

View check run for this annotation

Codecov / codecov/patch

src/Deprecated.jl#L224

Added line #L224 was not covered by tests
label::Symbol,
timestamp::Union{DateTime, ZonedDateTime},
nstime::Nanosecond,
tags::Set{Symbol},
solverData::GenericFunctionNodeData,
solvable::Int,
variableOrder::Union{Vector{Symbol}, Tuple};
observation = getFactorType(solverData),
state::FactorState = FactorState(),
solvercache::Base.RefValue{<:FactorSolverCache} = Ref{FactorSolverCache}(),
id::Union{UUID, Nothing} = nothing,
smallData::Dict{Symbol, SmallDataTypes} = Dict{Symbol, SmallDataTypes}(),
)
error(

Check warning on line 238 in src/Deprecated.jl

View check run for this annotation

Codecov / codecov/patch

src/Deprecated.jl#L238

Added line #L238 was not covered by tests
"This constructor is deprecated, use FactorCompute(label, variableOrder, solverData; ...) instead",
)
return FactorCompute(

Check warning on line 241 in src/Deprecated.jl

View check run for this annotation

Codecov / codecov/patch

src/Deprecated.jl#L241

Added line #L241 was not covered by tests
id,
label,
tags,
Tuple(variableOrder),
timestamp,
nstime,
Ref(solverData),
Ref(solvable),
smallData,
observation,
state,
solvercache,
)
end

export getSolverData, setSolverData!

function getSolverData(f::FactorCompute)
return error(

Check warning on line 260 in src/Deprecated.jl

View check run for this annotation

Codecov / codecov/patch

src/Deprecated.jl#L259-L260

Added lines #L259 - L260 were not covered by tests
"getSolverData(f::FactorCompute) is obsolete, use getState, getObservation, or getCache instead",
)
end

function setSolverData!(f::FactorCompute, data::GenericFunctionNodeData)
return error(

Check warning on line 266 in src/Deprecated.jl

View check run for this annotation

Codecov / codecov/patch

src/Deprecated.jl#L265-L266

Added lines #L265 - L266 were not covered by tests
"setSolverData!(f::FactorCompute, data::GenericFunctionNodeData) is obsolete, use setState!, or setCache! instead",
)
end

@deprecate unpackFactor(dfg::AbstractDFG, factor::FactorDFG; skipVersionCheck::Bool = false) unpackFactor(
factor;
skipVersionCheck,
)

@deprecate rebuildFactorMetadata!(args...; kwargs...) rebuildFactorCache!(
args...;
kwargs...,
)

export reconstFactorData
function reconstFactorData end

function decodePackedType(

Check warning on line 284 in src/Deprecated.jl

View check run for this annotation

Codecov / codecov/patch

src/Deprecated.jl#L284

Added line #L284 was not covered by tests
dfg::AbstractDFG,
varOrder::AbstractVector{Symbol},
::Type{T},
packeddata::GenericFunctionNodeData{PT},
) where {T <: FactorSolverCache, PT}
error("decodePackedType is obsolete")

Check warning on line 290 in src/Deprecated.jl

View check run for this annotation

Codecov / codecov/patch

src/Deprecated.jl#L290

Added line #L290 was not covered by tests
#
# TODO, to solve IIF 1424
# variables = map(lb->getVariable(dfg, lb), varOrder)

# Also look at parentmodule
usrtyp = convertStructType(PT)
fulltype = DFG.FunctionNodeData{T{usrtyp}}
factordata = reconstFactorData(dfg, varOrder, fulltype, packeddata)
return factordata

Check warning on line 299 in src/Deprecated.jl

View check run for this annotation

Codecov / codecov/patch

src/Deprecated.jl#L296-L299

Added lines #L296 - L299 were not covered by tests
end

export _packSolverData
function _packSolverData(f::FactorCompute, fnctype::AbstractFactorObservation)

Check warning on line 303 in src/Deprecated.jl

View check run for this annotation

Codecov / codecov/patch

src/Deprecated.jl#L303

Added line #L303 was not covered by tests
#
error("_packSolverData is deprecated, use seperate packing of observation #TODO")
packtype = convertPackedType(fnctype)
try
packed = convert(PackedFunctionNodeData{packtype}, getSolverData(f)) #TODO getSolverData
packedJson = packed
return packedJson

Check warning on line 310 in src/Deprecated.jl

View check run for this annotation

Codecov / codecov/patch

src/Deprecated.jl#L305-L310

Added lines #L305 - L310 were not covered by tests
catch ex
io = IOBuffer()
showerror(io, ex, catch_backtrace())
err = String(take!(io))
msg = "Error while packing '$(f.label)' as '$fnctype', please check the unpacking/packing converters for this factor - \r\n$err"
error(msg)

Check warning on line 316 in src/Deprecated.jl

View check run for this annotation

Codecov / codecov/patch

src/Deprecated.jl#L312-L316

Added lines #L312 - L316 were not covered by tests
end
end

export GenericFunctionNodeData, PackedFunctionNodeData, FunctionNodeData

const PackedFunctionNodeData{T} =
GenericFunctionNodeData{T} where {T <: AbstractPackedFactorObservation}
function PackedFunctionNodeData(args...; kw...)
error("PackedFunctionNodeData is obsolete")
return PackedFunctionNodeData{typeof(args[4])}(args...; kw...)

Check warning on line 326 in src/Deprecated.jl

View check run for this annotation

Codecov / codecov/patch

src/Deprecated.jl#L324-L326

Added lines #L324 - L326 were not covered by tests
end

const FunctionNodeData{T} = GenericFunctionNodeData{
T,
} where {T <: Union{<:AbstractFactorObservation, <:FactorSolverCache}}
FunctionNodeData(args...; kw...) = FunctionNodeData{typeof(args[4])}(args...; kw...)

Check warning on line 332 in src/Deprecated.jl

View check run for this annotation

Codecov / codecov/patch

src/Deprecated.jl#L332

Added line #L332 was not covered by tests

# this is the GenericFunctionNodeData for packed types
#TODO deprecate FactorData in favor of FactorState (with no more distinction between packed and compute)
const FactorData = PackedFunctionNodeData{AbstractPackedFactorObservation}

function FactorCompute(

Check warning on line 338 in src/Deprecated.jl

View check run for this annotation

Codecov / codecov/patch

src/Deprecated.jl#L338

Added line #L338 was not covered by tests
label::Symbol,
variableOrder::Union{Vector{Symbol}, Tuple},
solverData::GenericFunctionNodeData;
tags::Set{Symbol} = Set{Symbol}(),
timestamp::Union{DateTime, ZonedDateTime} = now(localzone()),
solvable::Int = 1,
nstime::Nanosecond = Nanosecond(0),
id::Union{UUID, Nothing} = nothing,
smallData::Dict{Symbol, SmallDataTypes} = Dict{Symbol, SmallDataTypes}(),
)
Base.depwarn(

Check warning on line 349 in src/Deprecated.jl

View check run for this annotation

Codecov / codecov/patch

src/Deprecated.jl#L349

Added line #L349 was not covered by tests
"`FactorCompute` constructor with `GenericFunctionNodeData` is deprecated. observation, state, and solvercache should be provided explicitly.",
:FactorCompute,
)
observation = getFactorType(solverData)
state = FactorState(

Check warning on line 354 in src/Deprecated.jl

View check run for this annotation

Codecov / codecov/patch

src/Deprecated.jl#L353-L354

Added lines #L353 - L354 were not covered by tests
solverData.eliminated,
solverData.potentialused,
solverData.multihypo,
solverData.certainhypo,
solverData.nullhypo,
solverData.solveInProgress,
solverData.inflation,
)

if solverData.fnc isa FactorSolverCache
solvercache = solverData.fnc

Check warning on line 365 in src/Deprecated.jl

View check run for this annotation

Codecov / codecov/patch

src/Deprecated.jl#L364-L365

Added lines #L364 - L365 were not covered by tests
else
solvercache = nothing

Check warning on line 367 in src/Deprecated.jl

View check run for this annotation

Codecov / codecov/patch

src/Deprecated.jl#L367

Added line #L367 was not covered by tests
end

return FactorCompute(

Check warning on line 370 in src/Deprecated.jl

View check run for this annotation

Codecov / codecov/patch

src/Deprecated.jl#L370

Added line #L370 was not covered by tests
label,
Tuple(variableOrder),
observation,
state,
solvercache;
id,
timestamp,
nstime,
tags,
smallData,
solvable,
)
end

# Deprecated check usefull? # packedFnc = fncStringToData(factor.fnctype, factor.data)
# Deprecated check usefull? # decodeType = getFactorOperationalMemoryType(dfg)
# Deprecated check usefull? # fullFactorData = decodePackedType(dfg, factor._variableOrderSymbols, decodeType, packedFnc)
function fncStringToData(args...; kwargs...)
@warn "fncStringToData is obsolete, called with" args kwargs
return error("fncStringToData is obsolete.")

Check warning on line 390 in src/Deprecated.jl

View check run for this annotation

Codecov / codecov/patch

src/Deprecated.jl#L388-L390

Added lines #L388 - L390 were not covered by tests
end

## ================================================================================
## Deprecated in v0.25
##=================================================================================
Expand Down
16 changes: 8 additions & 8 deletions src/DistributedFactorGraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ using Base
using Base64
using DocStringExtensions
using Dates
using Random
using TimeZones
using Distributions
using Reexport
Expand All @@ -42,7 +43,7 @@ using Tables

# used for @defVariable
import ManifoldsBase
import ManifoldsBase: AbstractManifold, manifold_dimension
using ManifoldsBase: AbstractManifold, manifold_dimension
export AbstractManifold, manifold_dimension

import RecursiveArrayTools: ArrayPartition
Expand Down Expand Up @@ -165,7 +166,8 @@ export InferenceVariable
export getSolverDataDict, setSolverData!
export getVariableType, getVariableTypeName

export getSolverData
export getObservation
export getState, getFactorState

export getVariableType

Expand Down Expand Up @@ -246,10 +248,9 @@ export @format_str
# Factors
##------------------------------------------------------------------------------
# Factor Data
export GenericFunctionNodeData, PackedFunctionNodeData, FunctionNodeData
export AbstractFactor, AbstractPackedFactor
export AbstractFactorObservation, AbstractPackedFactorObservation
export AbstractPrior, AbstractRelative, AbstractRelativeMinimize, AbstractManifoldMinimize
export FactorOperationalMemory
export FactorSolverCache

# accessors
export getVariableOrder
Expand All @@ -261,7 +262,7 @@ export mergeVariableData!, mergeGraphVariableData!
# Serialization type conversion
export convertPackedType, convertStructType

export reconstFactorData
export pack, unpack, packDistribution, unpackDistribution

##------------------------------------------------------------------------------
## Other utility functions
Expand All @@ -285,7 +286,7 @@ export findClosestTimestamp, findVariableNearTimestamp

# Serialization
export packVariable, unpackVariable, packFactor, unpackFactor
export rebuildFactorMetadata!
export rebuildFactorCache!
export @defVariable

# File import and export
Expand All @@ -301,7 +302,6 @@ export compare,
compareField,
compareFields,
compareAll,
compareAllSpecial,
compareVariable,
compareFactor,
compareAllVariables,
Expand Down
Loading
Loading