Skip to content

Commit 5e104eb

Browse files
authored
Standardize get dfg variable variable/factor types (#1080)
* standardize get dfg variable variable/factor types * implement getFactors(dfg, labels) --------- Co-authored-by: Johannes Terblanche <[email protected]>
1 parent e88ef99 commit 5e104eb

File tree

6 files changed

+32
-12
lines changed

6 files changed

+32
-12
lines changed

src/Common.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ end
2424
splitbynum(x::AbstractString) = split(x, r"(?<=\D)(?=\d)|(?<=\d)(?=\D)")
2525
#parse to Int
2626
function numstringtonum(arr::Vector{<:AbstractString})
27-
return [(n = tryparse(Int, e)) != nothing ? n : e for e in arr]
27+
return [(n = tryparse(Int, e)) !== nothing ? n : e for e in arr]
2828
end
2929
#natural less than
3030
function natural_lt(x::T, y::T) where {T <: AbstractString}

src/FileDFG/services/FileDFG.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ function loadDFG!(
174174
end
175175
# FIXME, why is this treated different from VariableSkeleton, VariableSummary?
176176
# FIXME, still creates type instability on `variables` as either `::Variable` or `::DFGVariable`
177-
if isa(dfgLoadInto, GraphsDFG) && GraphsDFGs._variablestype(dfgLoadInto) == Variable
177+
if isa(dfgLoadInto, GraphsDFG) && getTypeDFGVariables(dfgLoadInto) == Variable
178178
variables = packedvars
179179
else
180180
variables = unpackVariable.(packedvars)
@@ -190,7 +190,7 @@ function loadDFG!(
190190
return JSON3.read(jstr, PackedFactor)
191191
end
192192
# FIXME, still creates type instability on `variables` as either `::Factor` or `::DFGFactor{<:}`
193-
if isa(dfgLoadInto, GraphsDFG) && GraphsDFGs._factorstype(dfgLoadInto) == PackedFactor
193+
if isa(dfgLoadInto, GraphsDFG) && getTypeDFGFactors(dfgLoadInto) == PackedFactor
194194
factors = packedfacts
195195
else
196196
factors = unpackFactor.(dfgLoadInto, packedfacts)
@@ -201,7 +201,7 @@ function loadDFG!(
201201
# # Adding factors
202202
map(f -> addFactor!(dfgLoadInto, f), factors)
203203

204-
if isa(dfgLoadInto, GraphsDFG) && GraphsDFGs._factorstype(dfgLoadInto) != PackedFactor
204+
if isa(dfgLoadInto, GraphsDFG) && getTypeDFGFactors(dfgLoadInto) != PackedFactor
205205
# Finally, rebuild the CCW's for the factors to completely reinflate them
206206
# NOTE CREATES A NEW DFGFactor IF CCW TYPE CHANGES
207207
@info "Rebuilding CCW's for the factors..."

src/GraphsDFG/GraphsDFG.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ import ...DistributedFactorGraphs:
5252
getSessionBlobEntry,
5353
getSessionBlobEntries,
5454
addSessionBlobEntry!,
55-
addSessionBlobEntries!
55+
addSessionBlobEntries!,
56+
getTypeDFGVariables,
57+
getTypeDFGFactors
5658

5759
include("FactorGraphs/FactorGraphs.jl")
5860
using .FactorGraphs

src/GraphsDFG/services/GraphsDFGSerialization.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ function StructTypes.subtypes(::Type{PackedGraphsDFG})
2929
return NamedTuple(map(s -> nameof(s) => PackedGraphsDFG{s}, subs))
3030
end
3131

32-
_variablestype(fg::GraphsDFG{<:AbstractParams, T, <:AbstractDFGFactor}) where {T} = T
33-
_factorstype(fg::GraphsDFG{<:AbstractParams, <:AbstractDFGVariable, T}) where {T} = T
32+
getTypeDFGVariables(fg::GraphsDFG{<:AbstractParams, T, <:AbstractDFGFactor}) where {T} = T
33+
getTypeDFGFactors(fg::GraphsDFG{<:AbstractParams, <:AbstractDFGVariable, T}) where {T} = T
3434

3535
##
3636
"""
@@ -52,8 +52,8 @@ function packDFGMetadata(fg::GraphsDFG)
5252

5353
props = (k => getproperty(fg, k) for k in commonfields)
5454
return PackedGraphsDFG(;
55-
typePackedVariable = _variablestype(fg) == Variable,
56-
typePackedFactor = _factorstype(fg) == PackedFactor,
55+
typePackedVariable = getTypeDFGVariables(fg) == Variable,
56+
typePackedFactor = getTypeDFGFactors(fg) == PackedFactor,
5757
blobStores,
5858
props...,
5959
)

src/services/AbstractDFG.jl

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 @@ function rebuildFactorMetadata!(
101101
return error("rebuildFactorMetadata! is not implemented for $(typeof(dfg))")
102102
end
103103

104+
"""
105+
$(SIGNATURES)
106+
Function to get the type of the variables in the DFG.
107+
"""
108+
function getTypeDFGVariables end
109+
110+
"""
111+
$(SIGNATURES)
112+
Function to get the type of the factors in the DFG.
113+
"""
114+
function getTypeDFGFactors end
115+
104116
##------------------------------------------------------------------------------
105117
## Setters
106118
##------------------------------------------------------------------------------
@@ -373,6 +385,10 @@ function getFactors(
373385
return error("getFactors not implemented for $(typeof(dfg))")
374386
end
375387

388+
function getFactors(dfg::AbstractDFG, labels::Vector{Symbol})
389+
return map(label -> getFactor(dfg, label), labels)
390+
end
391+
376392
##------------------------------------------------------------------------------
377393
## Checking Types
378394
##------------------------------------------------------------------------------
@@ -1079,9 +1095,8 @@ function copyGraph!(
10791095
showprogress::Bool = verbose,
10801096
)
10811097
# Split into variables and factors
1082-
sourceVariables = map(vId -> getVariable(sourceDFG, vId), variableLabels)
1083-
sourceFactors = map(fId -> getFactor(sourceDFG, fId), factorLabels)
1084-
1098+
sourceVariables = getVariables(sourceDFG, variableLabels)
1099+
sourceFactors = getFactors(sourceDFG, factorLabels)
10851100
# Now we have to add all variables first,
10861101
@showprogress desc = "copy variables" enabled = showprogress for variable in
10871102
sourceVariables

src/services/Serialization.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,10 @@ function unpackVariable(variable::PackedVariable; skipVersionCheck::Bool = false
245245
)
246246
end
247247

248+
DFGVariable(v::DFGVariable) = v
248249
DFGVariable(v::Variable) = unpackVariable(v)
250+
Variable(v::Variable) = v
251+
Variable(v::DFGVariable) = packVariable(v)
249252

250253
##==============================================================================
251254
## Factor Packing and unpacking

0 commit comments

Comments
 (0)