Skip to content

Commit a9ca114

Browse files
committed
Start of importing/exporting
1 parent ddd4a99 commit a9ca114

File tree

8 files changed

+123
-15
lines changed

8 files changed

+123
-15
lines changed

src/CloudGraphsDFG/services/CloudGraphsDFG.jl

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,6 @@ export copySession!
44
# With great power comes great "Oh crap, I deleted everything..."
55
export clearSession!!, clearRobot!!, clearUser!!
66

7-
## Utility functions for getting type names and modules (from IncrementalInference)
8-
function _getmodule(t::T) where T
9-
T.name.module
10-
end
11-
function _getname(t::T) where T
12-
T.name.name
13-
end
14-
15-
# Simply for convenience - don't export
16-
const PackedFunctionNodeData{T} = GenericFunctionNodeData{T, <: AbstractString}
17-
PackedFunctionNodeData(x1, x2, x3, x4, x5::S, x6::T, x7::String="", x8::Vector{Int}=Int[]) where {T <: PackedInferenceType, S <: AbstractString} = GenericFunctionNodeData(x1, x2, x3, x4, x5, x6, x7, x8)
18-
const FunctionNodeData{T} = GenericFunctionNodeData{T, Symbol}
19-
FunctionNodeData(x1, x2, x3, x4, x5::Symbol, x6::T, x7::String="", x8::Vector{Int}=Int[]) where {T <: Union{FunctorInferenceType, ConvolutionObject}}= GenericFunctionNodeData{T, Symbol}(x1, x2, x3, x4, x5, x6, x7, x8)
20-
217
function _getNodeType(dfg::CloudGraphsDFG, nodeLabel::Symbol)::Symbol
228
dfg.useCache && haskey(dfg.variableDict, nodeLabel) && return :VARIABLE
239
dfg.useCache && haskey(dfg.factorDict, nodeLabel) && return :FACTOR
@@ -344,7 +330,7 @@ function getFactor(dfg::CloudGraphsDFG, factorId::Int64)::DFGFactor
344330
# Lastly, rebuild the metadata
345331
factor = dfg.rebuildFactorMetadata!(dfg, factor)
346332
# GUARANTEED never to bite us in the ass in the future...
347-
# ... TODO: refactor if changed: https://github.com/JuliaRobotics/IncrementalInference.jl/issues/350
333+
# ... TODO: refactor if changed: https://github.com/JuliaRobotics/IncrementalInference.jl/issues/350
348334
getData(factor).fncargvID = _variableOrderSymbols
349335

350336
# Add to cache

src/DistributedFactorGraphs.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ include("services/DFGVariable.jl")
4141
# Include the Graphs.jl API.
4242
include("GraphsDFG/GraphsDFG.jl")
4343

44+
# Include the FilesDFG API.
45+
include("FileDFG/FileDFG.jl")
46+
47+
export saveDFG, loadDFG
48+
4449
function __init__()
4550
@require DataFrames="a93c6f00-e57d-5684-b7b6-d8193f3e46c0" begin
4651
if isdefined(Main, :DataFrames)

src/FileDFG/FileDFG.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
# Entities
3+
include("entities/FileDFG.jl")
4+
5+
# Services
6+
include("services/FileDFG.jl")

src/FileDFG/entities/FileDFG.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
# mutable struct FileDFG
3+
# folderName::String
4+
# FileDFG(folderName::String)::FileDFG = new(foldername)
5+
# end

src/FileDFG/services/FileDFG.jl

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
function _packVariable(dfg::G, v::DFGVariable)::Dict{String, Any} where G <: AbstractDFG
3+
props = Dict{String, Any}()
4+
props["label"] = string(v.label)
5+
props["timestamp"] = string(v.timestamp)
6+
props["tags"] = JSON2.write(v.tags)
7+
props["estimateDict"] = JSON2.write(v.estimateDict)
8+
props["solverDataDict"] = JSON2.write(Dict(keys(v.solverDataDict) .=> map(vnd -> pack(dfg, vnd), values(v.solverDataDict))))
9+
props["smallData"] = JSON2.write(v.smallData)
10+
props["ready"] = v.ready
11+
props["backendset"] = v.backendset
12+
return props
13+
end
14+
15+
function _packFactor(dfg::G, f::DFGFactor)::Dict{String, Any} where G <: AbstractDFG
16+
# Construct the properties to save
17+
props = Dict{String, Any}()
18+
props["label"] = string(f.label)
19+
props["tags"] = JSON2.write(f.tags)
20+
# Pack the node data
21+
fnctype = f.data.fnc.usrfnc!
22+
packtype = getfield(_getmodule(fnctype), Symbol("Packed$(_getname(fnctype))"))
23+
packed = convert(PackedFunctionNodeData{packtype}, f.data)
24+
props["data"] = JSON2.write(packed)
25+
# Include the type
26+
props["fnctype"] = String(_getname(fnctype))
27+
props["_variableOrderSymbols"] = JSON2.write(f._variableOrderSymbols)
28+
props["backendset"] = f.backendset
29+
props["ready"] = f.ready
30+
31+
return props
32+
end
33+
34+
function saveDFG(dfg::G, folder::String) where G <: AbstractDFG
35+
variables = getVariables(dfg)
36+
factors = getFactors(dfg)
37+
varFolder = "$folder/variables"
38+
factorFolder = "$folder/factors"
39+
# Folder preparations
40+
if !isdir(folder)
41+
@info "Folder '$folder' doesn't exist, creating..."
42+
mkpath(folder)
43+
end
44+
!isdir(varFolder) && mkpath(varFolder)
45+
!isdir(factorFolder) && mkpath(factorFolder)
46+
# Clearing out the folders
47+
map(f -> rm("$varFolder/$f"), readdir(varFolder))
48+
map(f -> rm("$factorFolder/$f"), readdir(factorFolder))
49+
# Variables
50+
for v in variables
51+
vPacked = _packVariable(dfg, v)
52+
io = open("$varFolder/$(v.label).json", "w")
53+
JSON2.write(io, vPacked)
54+
close(io)
55+
end
56+
# Factors
57+
for f in factors
58+
fPacked = _packFactor(dfg, f)
59+
io = open("$folder/factors/$(f.label).json", "w")
60+
JSON2.write(io, fPacked)
61+
close(io)
62+
end
63+
end
64+
65+
function loadDFG(folderName::String, dfgLoadInto::G=GraphsDFG{NoSolverParams}()) where G <: AbstractDFG
66+
return dfgLoadInto
67+
end

src/entities/DFGFactor.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,9 @@ end
5050
label(f::F) where F <: DFGFactor = f.label
5151
data(f::F) where F <: DFGFactor = f.data
5252
id(f::F) where F <: DFGFactor = f._internalId
53+
54+
# Simply for convenience - don't export
55+
const PackedFunctionNodeData{T} = GenericFunctionNodeData{T, <: AbstractString}
56+
PackedFunctionNodeData(x1, x2, x3, x4, x5::S, x6::T, x7::String="", x8::Vector{Int}=Int[]) where {T <: PackedInferenceType, S <: AbstractString} = GenericFunctionNodeData(x1, x2, x3, x4, x5, x6, x7, x8)
57+
const FunctionNodeData{T} = GenericFunctionNodeData{T, Symbol}
58+
FunctionNodeData(x1, x2, x3, x4, x5::Symbol, x6::T, x7::String="", x8::Vector{Int}=Int[]) where {T <: Union{FunctorInferenceType, ConvolutionObject}}= GenericFunctionNodeData{T, Symbol}(x1, x2, x3, x4, x5, x6, x7, x8)

src/services/AbstractDFG.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,11 @@ function _copyIntoGraph!(sourceDFG::G, destDFG::H, variableFactorLabels::Vector{
6262
end
6363
return nothing
6464
end
65+
66+
## Utility functions for getting type names and modules (from IncrementalInference)
67+
function _getmodule(t::T) where T
68+
T.name.module
69+
end
70+
function _getname(t::T) where T
71+
T.name.name
72+
end

test/FileDFG.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Revise
2+
using Test
3+
using DistributedFactorGraphs
4+
using IncrementalInference, RoME
5+
6+
# Make a simple graph
7+
dfg = GraphsDFG{SolverParams}(params=SolverParams())
8+
9+
# Add the first pose :x0
10+
x0 = addVariable!(dfg, :x0, Pose2)
11+
IncrementalInference.compareVariable(x0, getVariable(dfg, :x0))
12+
13+
# Add at a fixed location PriorPose2 to pin :x0 to a starting location (10,10, pi/4)
14+
prior = addFactor!(dfg, [:x0], PriorPose2( MvNormal([10; 10; 1.0/8.0], Matrix(Diagonal([0.1;0.1;0.05].^2))) ) )
15+
16+
# Drive around in a hexagon
17+
for i in 0:5
18+
psym = Symbol("x$i")
19+
nsym = Symbol("x$(i+1)")
20+
addVariable!(dfg, nsym, Pose2)
21+
pp = Pose2Pose2(MvNormal([10.0;0;pi/3], Matrix(Diagonal([0.1;0.1;0.1].^2))))
22+
addFactor!(dfg, [psym;nsym], pp )
23+
end
24+
25+
saveDFG(dfg "/tmp/fileDFG")

0 commit comments

Comments
 (0)