Skip to content

Commit 866808a

Browse files
authored
Merge pull request #997 from JuliaRobotics/23Q1/maint/packvar_updates
Packed[Variable/Factor] as a DFGType and nstime as string
2 parents 2fb816f + 206c6c0 commit 866808a

13 files changed

+112
-20
lines changed

docs/src/variable_factor_serialization.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ prior = addFactor!(dfg, [:x0], PriorPose2( MvNormal([10; 10; 1.0/8.0], Matrix(Di
2626

2727
# Now serialize them:
2828
pVariable = packVariable(dfg, x0)
29-
pFactor = packFactor(dfg, prior)
29+
pFactor = packFactor(prior)
3030

3131
# And we can deserialize them
3232
upVariable = unpackVariable(dfg, pVariable)
@@ -60,7 +60,7 @@ for v in getVariables(dfg)
6060
end
6161
# Factors
6262
for f in getFactors(dfg)
63-
fPacked = packFactor(dfg, f)
63+
fPacked = packFactor(f)
6464
io = open("$folder/factors/$(f.label).json", "w")
6565
JSON3.write(io, fPacked)
6666
close(io)

src/DataBlobs/services/HelpersDataWrapEntryBlob.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ addData!(
156156
kwargs...
157157
)
158158

159+
#FIXME id used wrong
159160
function addData!(
160161
dfg::AbstractDFG,
161162
blobstore::AbstractBlobStore,

src/Deprecated.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
## ================================================================================
33
## Remove in v0.21
44
##=================================================================================
5-
5+
@deprecate packFactor(dfg::AbstractDFG, f::DFGFactor) packFactor(f::DFGFactor)
66

77
# #TODO check this one
88
# function addData!(

src/DistributedFactorGraphs.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ export getPPEDict, getVariablePPEDict, getVariablePPE
173173

174174
# CRUD & SET
175175
export getPPE,
176+
getPPEs,
176177
getVariablePPE,
177178
addPPE!,
178179
updatePPE!,

src/entities/DFGFactor.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ FunctionNodeData(args...; kw...) = FunctionNodeData{typeof(args[4])}(args...; kw
8383
# | DFGFactor | X | X | X | X | X |
8484

8585
# Packed Factor
86-
Base.@kwdef struct PackedFactor
86+
Base.@kwdef struct PackedFactor <: AbstractDFGFactor
8787
id::Union{UUID, Nothing} = nothing
8888
label::Symbol
8989
tags::Vector{Symbol}
9090
_variableOrderSymbols::Vector{Symbol}
9191
timestamp::ZonedDateTime
92-
nstime::Int
92+
nstime::String
9393
fnctype::String
9494
solvable::Int
9595
data::String
@@ -280,8 +280,8 @@ StructTypes.omitempties(::Type{SkeletonDFGFactor}) = (:id,)
280280
##==============================================================================
281281
## Define factor levels
282282
##==============================================================================
283-
const FactorDataLevel0 = Union{DFGFactor, DFGFactorSummary, SkeletonDFGFactor}
284-
const FactorDataLevel1 = Union{DFGFactor, DFGFactorSummary}
283+
const FactorDataLevel0 = Union{DFGFactor, DFGFactorSummary, PackedFactor, SkeletonDFGFactor}
284+
const FactorDataLevel1 = Union{DFGFactor, DFGFactorSummary, PackedFactor}
285285
const FactorDataLevel2 = Union{DFGFactor}
286286

287287
##==============================================================================

src/entities/DFGVariable.jl

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,12 @@ getEstimateFields(::MeanMaxPPE) = [:suggested, :max, :mean]
178178
##==============================================================================
179179

180180
# Packed Variable
181-
Base.@kwdef struct PackedVariable
181+
Base.@kwdef struct PackedVariable <: AbstractDFGVariable
182182
id::Union{UUID, Nothing} = nothing
183183
label::Symbol
184184
tags::Vector{Symbol} = Symbol[]
185185
timestamp::ZonedDateTime = now(tz"UTC")
186-
nstime::Int = 0
186+
nstime::String = "0"
187187
ppes::Vector{MeanMaxPPE} = MeanMaxPPE[]
188188
blobEntries::Vector{BlobEntry} = BlobEntry[]
189189
variableType::String
@@ -193,6 +193,33 @@ Base.@kwdef struct PackedVariable
193193
solverData::Vector{PackedVariableNodeData} = PackedVariableNodeData[]
194194
end
195195

196+
#IIF like contruction helper for packed variable
197+
function PackedVariable(
198+
label::Symbol,
199+
variableType::String;
200+
tags::Vector{Symbol} = Symbol[],
201+
timestamp::ZonedDateTime = now(tz"UTC"),
202+
solvable::Int = 1,
203+
nanosecondtime::Int64 = 0,
204+
smalldata::Dict{Symbol, SmallDataTypes} = Dict{Symbol, SmallDataTypes}(),
205+
kwargs...
206+
)
207+
union!(tags, [:VARIABLE])
208+
209+
pacvar = PackedVariable(;
210+
label,
211+
variableType,
212+
nstime = string(nanosecondtime),
213+
solvable,
214+
tags,
215+
metadata = base64encode(JSON3.write(smalldata)),
216+
timestamp,
217+
kwargs...
218+
)
219+
220+
return pacvar
221+
end
222+
196223
StructTypes.StructType(::Type{PackedVariable}) = StructTypes.UnorderedStruct()
197224
StructTypes.idproperty(::Type{PackedVariable}) = :id
198225
StructTypes.omitempties(::Type{PackedVariable}) = (:id,)
@@ -265,7 +292,7 @@ DFGVariable(label::Symbol,
265292
solverDataDict::Dict{Symbol, VariableNodeData{T,P}}=Dict{Symbol, VariableNodeData{T,getPointType(T)}}(),
266293
kw...) where {T <: InferenceVariable, P} = DFGVariable(label, T; solverDataDict=solverDataDict, kw...)
267294
#
268-
@deprecate DFGVariable(label::Symbol, T_::Type{<:InferenceVariable},w...; timestamp::DateTime=now(),kw...) DFGVariable(label, T_, w...; timestamp=ZonedDateTime(timestamp), kw...)
295+
# @deprecate DFGVariable(label::Symbol, T_::Type{<:InferenceVariable},w...; timestamp::DateTime=now(),kw...) DFGVariable(label, T_, w...; timestamp=ZonedDateTime(timestamp), kw...)
269296
#
270297

271298

@@ -397,8 +424,8 @@ StructTypes.omitempties(::Type{SkeletonDFGVariable}) = (:id,)
397424
##==============================================================================
398425
# Define variable levels
399426
##==============================================================================
400-
const VariableDataLevel0 = Union{DFGVariable, DFGVariableSummary, SkeletonDFGVariable}
401-
const VariableDataLevel1 = Union{DFGVariable, DFGVariableSummary}
427+
const VariableDataLevel0 = Union{DFGVariable, DFGVariableSummary, PackedVariable, SkeletonDFGVariable}
428+
const VariableDataLevel1 = Union{DFGVariable, DFGVariableSummary, PackedVariable}
402429
const VariableDataLevel2 = Union{DFGVariable}
403430

404431
##==============================================================================

src/services/AbstractDFG.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,7 @@ function copyGraph!(destDFG::AbstractDFG,
952952
# And then all factors to the destDFG.
953953
for factor in sourceFactors
954954
# Get the original factor variables (we need them to create it)
955-
sourceFactorVariableIds = getNeighbors(sourceDFG, factor)
955+
sourceFactorVariableIds = collect(factor._variableOrderSymbols)
956956
# Find the labels and associated variables in our new subgraph
957957
factVariableIds = Symbol[]
958958
for variable in sourceFactorVariableIds

src/services/DFGFactor.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ setTimestamp(f::DFGFactor, ts::ZonedDateTime) = DFGFactor(f.label, ts, f.nstime,
8686
setTimestamp(f::DFGFactorSummary, ts::ZonedDateTime) = DFGFactorSummary(f.id, f.label, f.tags, f._variableOrderSymbols, ts)
8787
setTimestamp(f::DFGFactorSummary, ts::DateTime) = DFGFactorSummary(f, ZonedDateTime(ts, localzone()))
8888

89+
function setTimestamp(v::PackedFactor, timestamp::ZonedDateTime)
90+
return PackedFactor(;(key => getproperty(v, key) for key in fieldnames(PackedFactor))..., timestamp)
91+
end
8992

9093
##------------------------------------------------------------------------------
9194
## solvable

src/services/DFGVariable.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@ function setTimestamp(v::DFGVariableSummary, ts::ZonedDateTime; verbose::Bool=tr
344344
return DFGVariableSummary(v.id, v.label, ts, v.tags, v.ppeDict, v.variableTypeName, v.dataDict)
345345
end
346346

347+
function setTimestamp(v::PackedVariable, timestamp::ZonedDateTime; verbose::Bool=true)
348+
return PackedVariable(;(key => getproperty(v, key) for key in fieldnames(PackedVariable))..., timestamp)
349+
end
347350

348351
##------------------------------------------------------------------------------
349352
## solvable
@@ -386,6 +389,13 @@ function getPPE(vari::VariableDataLevel1, solveKey::Symbol=:default)
386389
# return haskey(ppeDict, solveKey) ? ppeDict[solveKey] : nothing
387390
end
388391

392+
"""
393+
$SIGNATURES
394+
395+
Get all the parametric point estimate (PPE) for a variable in the factor graph.
396+
"""
397+
function getPPEs end
398+
389399
# afew more aliases on PPE, brought back from deprecated DF
390400

391401
"""

src/services/Serialization.jl

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ export _packSolverData
66
const TYPEKEY = "_type"
77

88
## Version checking
9-
# FIXME return VersionNumber
9+
#NOTE fixed really bad function but kept similar as fallback #TODO upgrade to use pkgversion(m::Module)
1010
function _getDFGVersion()
11-
if haskey(Pkg.dependencies(), Base.UUID("b5cc3c7e-6572-11e9-2517-99fb8daf2f04"))
12-
return string(Pkg.dependencies()[Base.UUID("b5cc3c7e-6572-11e9-2517-99fb8daf2f04")].version) |> VersionNumber
11+
12+
pkgorigin = get(Base.pkgorigins, Base.PkgId(DistributedFactorGraphs), nothing)
13+
if !isnothing(pkgorigin)
14+
return pkgorigin.version
15+
end
16+
dep = get(Pkg.dependencies(), Base.UUID("b5cc3c7e-6572-11e9-2517-99fb8daf2f04"), nothing)
17+
if !isnothing(dep)
18+
return dep.version
1319
else
1420
# This is arguably slower, but needed for Travis.
1521
return Pkg.TOML.parse(read(joinpath(dirname(pathof(@__MODULE__)), "..", "Project.toml"), String))["version"] |> VersionNumber
@@ -18,7 +24,7 @@ end
1824

1925
function _versionCheck(node::Union{<:PackedVariable, <:PackedFactor})
2026
if VersionNumber(node._version) < _getDFGVersion()
21-
@warn "This data was serialized using DFG $(props["_version"]) but you have $(_getDFGVersion()) installed, there may be deserialization issues." maxlog=10
27+
@warn "This data was serialized using DFG $(node._version) but you have $(_getDFGVersion()) installed, there may be deserialization issues." maxlog=10
2228
end
2329
end
2430

@@ -151,7 +157,7 @@ function packVariable(v::AbstractDFGVariable; includePPEs::Bool=true, includeSol
151157
id=v.id,
152158
label = v.label,
153159
timestamp = v.timestamp,
154-
nstime = v.nstime.value,
160+
nstime = string(v.nstime.value),
155161
tags = collect(v.tags), # Symbol.()
156162
ppes = collect(values(v.ppeDict)),
157163
solverData = packVariableNodeData.(collect(values(v.solverDataDict))),
@@ -216,15 +222,15 @@ function _packSolverData(
216222
end
217223

218224
# returns PackedFactor
219-
function packFactor(dfg::AbstractDFG, f::DFGFactor)
225+
function packFactor(f::DFGFactor)
220226
fnctype = getSolverData(f).fnc.usrfnc!
221227
return PackedFactor(;
222228
id = f.id,
223229
label = f.label,
224230
tags = collect(f.tags),
225231
_variableOrderSymbols = f._variableOrderSymbols,
226232
timestamp = f.timestamp,
227-
nstime = f.nstime.value,
233+
nstime = string(f.nstime.value),
228234
fnctype = String(_getname(fnctype)),
229235
solvable = getSolvable(f),
230236
metadata = base64encode(JSON3.write(f.smallData)),

0 commit comments

Comments
 (0)