Skip to content

Commit beac9b6

Browse files
committed
Documentation
1 parent eebf7b4 commit beac9b6

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Serialization of Variables and Factors
2+
3+
If you are transferring variables and factors over a wire you need to serialize
4+
and deserialize variables and factors.
5+
6+
## Packing
7+
8+
Packing is done with the exposed functions `packVariable()::Dict{String, Any}` and
9+
`packFactor()::Dict{String, Any}`. You can then serialize this into a string or JSON
10+
as you would normally.
11+
12+
For example:
13+
```
14+
using DistributedFactorGraphs
15+
using IncrementalInference, RoME
16+
17+
# Make a variable and a factor:
18+
# Make a simple graph
19+
dfg = GraphsDFG{SolverParams}(params=SolverParams())
20+
# Add the first pose :x0
21+
x0 = addVariable!(dfg, :x0, Pose2)
22+
# Add at a fixed location PriorPose2 to pin :x0 to a starting location (10,10, pi/4)
23+
prior = addFactor!(dfg, [:x0], PriorPose2( MvNormal([10; 10; 1.0/8.0], Matrix(Diagonal([0.1;0.1;0.05].^2))) ) )
24+
25+
# Now serialize them:
26+
pVariable = packVariable(dfg, x0)
27+
pFactor = packFactor(dfg, prior)
28+
29+
# And we can deserialize them
30+
upVariable = unpackVariable(dfg, pVariable)
31+
# FYI: The graph is used in unpackFactor to find the variables that the factor links to.
32+
upFactor = unpackFactor(dfg, pFactor, IncrementalInference)
33+
# Note, you need to call IncrementalInference.rebuildFactorMetadata!(dfgLoadInto, factor)
34+
# to make it useable. Please add an issue if this poses a problem or causes issues.
35+
```
36+
37+
As a more complex example, we can use JSON2 to stringify the data and write it to a file as FileDFG does:
38+
39+
```
40+
using DistributedFactorGraphs
41+
using IncrementalInference, RoME
42+
43+
# Make a variable and a factor:
44+
# Make a simple graph
45+
dfg = GraphsDFG{SolverParams}(params=SolverParams())
46+
# Add the first pose :x0
47+
x0 = addVariable!(dfg, :x0, Pose2)
48+
# Add at a fixed location PriorPose2 to pin :x0 to a starting location (10,10, pi/4)
49+
prior = addFactor!(dfg, [:x0], PriorPose2( MvNormal([10; 10; 1.0/8.0], Matrix(Diagonal([0.1;0.1;0.05].^2))) ) )
50+
51+
# Slightly fancier example: We can use JSON2, we can serialize to a string
52+
varFolder = "/tmp"
53+
for v in getVariables(dfg)
54+
vPacked = packVariable(dfg, v)
55+
io = open("$varFolder/$(v.label).json", "w")
56+
JSON2.write(io, vPacked)
57+
close(io)
58+
end
59+
# Factors
60+
for f in getFactors(dfg)
61+
fPacked = packFactor(dfg, f)
62+
io = open("$folder/factors/$(f.label).json", "w")
63+
JSON2.write(io, fPacked)
64+
close(io)
65+
end
66+
67+
```

0 commit comments

Comments
 (0)