Skip to content

Commit 50637f7

Browse files
committed
Fixing tar file saving, comparator for Graphs.jl, and the FileDFG tests
1 parent 3301d4f commit 50637f7

File tree

3 files changed

+72
-67
lines changed

3 files changed

+72
-67
lines changed

src/FileDFG/services/FileDFG.jl

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,25 @@ dfg = initfg()
1111
# ... Add stuff to graph using either IIF or DFG:
1212
v1 = addVariable!(dfg, :a, ContinuousScalar, labels = [:POSE], solvable=0)
1313
# Now save it:
14-
saveDFG(dfg, "/tmp/saveDFG")
14+
saveDFG(dfg, "/tmp/saveDFG.tar.gz")
1515
```
1616
"""
17-
function saveDFG(dfg::AbstractDFG, folder::String; compress::Symbol=:gzip)
17+
function saveDFG(dfg::AbstractDFG, folder::String)
18+
19+
# TODO: Deprecate the folder functionality in v0.6.1
20+
21+
# Clean up save path if a file is specified
22+
savepath = folder[end] == '/' ? folder[1:end-1] : folder
23+
savepath = splitext(splitext(savepath)[1])[1] # In case of .tar.gz
24+
1825
variables = getVariables(dfg)
1926
factors = getFactors(dfg)
20-
varFolder = "$folder/variables"
21-
factorFolder = "$folder/factors"
27+
varFolder = "$savepath/variables"
28+
factorFolder = "$savepath/factors"
2229
# Folder preparations
23-
if !isdir(folder)
24-
@info "Folder '$folder' doesn't exist, creating..."
25-
mkpath(folder)
30+
if !isdir(savepath)
31+
@info "Folder '$savepath' doesn't exist, creating..."
32+
mkpath(savepath)
2633
end
2734
!isdir(varFolder) && mkpath(varFolder)
2835
!isdir(factorFolder) && mkpath(factorFolder)
@@ -39,19 +46,16 @@ function saveDFG(dfg::AbstractDFG, folder::String; compress::Symbol=:gzip)
3946
# Factors
4047
for f in factors
4148
fPacked = packFactor(dfg, f)
42-
io = open("$folder/factors/$(f.label).json", "w")
49+
io = open("$factorFolder/$(f.label).json", "w")
4350
JSON2.write(io, fPacked)
4451
close(io)
4552
end
4653

47-
# compress newly saved folder, skip if not supported format
48-
!(compress in [:gzip]) && return
49-
savepath = folder[end] == '/' ? folder[1:end-1] : folder
5054
savedir = dirname(savepath)
51-
savename = splitpath(string(savepath))[end]
55+
savename = basename(string(savepath))
5256
@assert savename != ""
53-
# temporarily change working directory to get correct zipped path
54-
run( pipeline(`tar -zcf - -C $savedir $savename`, stdout="$savepath.tar.gz") )
57+
destfile = joinpath(savedir, savename*".tar.gz")
58+
run( pipeline(`tar -zcf - -C $savedir $savename`, stdout="$destfile"))
5559
Base.rm(joinpath(savedir,savename), recursive=true)
5660
end
5761

@@ -67,7 +71,6 @@ using DistributedFactorGraphs, IncrementalInference
6771
dfg = initfg()
6872
# Load the graph
6973
loadDFG("/tmp/savedgraph.tar.gz", IncrementalInference, dfg)
70-
loadDFG("/tmp/savedgraph", IncrementalInference, dfg) # alternative
7174
# Use the DFG as you do normally.
7275
ls(dfg)
7376
```

src/services/CompareUtils.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ const GeneratedCompareUnion = Union{MeanMaxPPE, VariableNodeData, DFGNodeParams,
2424
DFGFactor, DFGFactorSummary, SkeletonDFGFactor}
2525

2626
@generated function ==(x::T, y::T) where T <: GeneratedCompareUnion
27-
mapreduce(n -> :(x.$n == y.$n), (a,b)->:($a && $b), fieldnames(x))
27+
ignored = [:_internalId]
28+
mapreduce(n -> :(x.$n == y.$n), (a,b)->:($a && $b), setdiff(fieldnames(x), ignored))
2829
end
2930

3031

test/fileDFGTests.jl

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,59 @@
11
using DistributedFactorGraphs
22
using IncrementalInference
33
using Test
4-
dfg = LightDFG{SolverParams}()
54

65
@testset "FileDFG Tests" begin
7-
8-
if typeof(dfg) <: CloudGraphsDFG
9-
@warn "TEST: Nuking all data for user '$(dfg.userId)', robot '$(dfg.robotId)'!"
10-
clearRobot!!(dfg)
11-
end
12-
13-
# Same graph as iifInterfaceTests.jl
14-
numNodes = 10
15-
16-
#change ready and solvable for x7,x8 for improved tests on x7x8f1
17-
verts = map(n -> addVariable!(dfg, Symbol("x$n"), ContinuousScalar, labels = [:POSE]), 1:numNodes)
18-
#TODO fix this to use accessors
19-
verts[7].solvable = 1
20-
verts[8].solvable = 0
21-
getSolverData(verts[8]).solveInProgress = 1
22-
setSolvedCount!(verts[1], 5)
23-
#call update to set it on cloud
24-
updateVariable!(dfg, verts[7])
25-
updateVariable!(dfg, verts[8])
26-
27-
# Add some bigData to x1, x2
28-
addBigDataEntry!(verts[1], GeneralBigDataEntry(:testing, :testing; mimeType="application/nuthin!"))
29-
addBigDataEntry!(verts[2], FileBigDataEntry(:testing2, "/dev/null"))
30-
#call update to set it on cloud
31-
updateVariable!(dfg, verts[1])
32-
updateVariable!(dfg, verts[2])
33-
34-
facts = map(n -> addFactor!(dfg, [verts[n], verts[n+1]], LinearConditional(Normal(50.0,2.0))), 1:(numNodes-1))
35-
36-
# Save and load the graph to test.
37-
saveFolder = "/tmp/fileDFG"
38-
saveDFG(dfg, saveFolder)
39-
40-
copyDfg = DistributedFactorGraphs._getDuplicatedEmptyDFG(dfg)
41-
@info "Going to load $saveFolder"
42-
retDFG = loadDFG(saveFolder*".tar.gz", Main, copyDfg, loaddir="/tmp")
43-
44-
@test issetequal(ls(dfg), ls(retDFG))
45-
@test issetequal(lsf(dfg), lsf(retDFG))
46-
for var in ls(dfg)
47-
@test getVariable(dfg, var) == getVariable(retDFG, var)
6+
for filename in ["/tmp/fileDFG", "/tmp/FileDFGExtension.tar.gz"]
7+
global dfg
8+
dfg = DistributedFactorGraphs._getDuplicatedEmptyDFG(dfg)
9+
10+
if typeof(dfg) <: CloudGraphsDFG
11+
@warn "TEST: Nuking all data for user '$(dfg.userId)', robot '$(dfg.robotId)'!"
12+
clearRobot!!(dfg)
13+
end
14+
15+
# Same graph as iifInterfaceTests.jl
16+
numNodes = 5
17+
18+
#change ready and solvable for x7,x8 for improved tests on x7x8f1
19+
verts = map(n -> addVariable!(dfg, Symbol("x$n"), ContinuousScalar, labels = [:POSE]), 1:numNodes)
20+
#TODO fix this to use accessors
21+
verts[3].solvable = 1
22+
verts[4].solvable = 0
23+
getSolverData(verts[4]).solveInProgress = 1
24+
#call update to set it on cloud
25+
updateVariable!(dfg, verts[3])
26+
updateVariable!(dfg, verts[4])
27+
28+
setSolvedCount!(verts[1], 5)
29+
# Add some bigData to x1, x2
30+
addBigDataEntry!(verts[1], GeneralBigDataEntry(:testing, :testing; mimeType="application/nuthin!"))
31+
addBigDataEntry!(verts[2], FileBigDataEntry(:testing2, "/dev/null"))
32+
#call update to set it on cloud
33+
updateVariable!(dfg, verts[1])
34+
updateVariable!(dfg, verts[2])
35+
36+
facts = map(n -> addFactor!(dfg, [verts[n], verts[n+1]], LinearConditional(Normal(50.0,2.0))), 1:(numNodes-1))
37+
38+
# Save and load the graph to test.
39+
saveDFG(dfg, filename)
40+
41+
copyDfg = DistributedFactorGraphs._getDuplicatedEmptyDFG(dfg)
42+
@info "Going to load $filename"
43+
retDFG = loadDFG(filename, Main, copyDfg, loaddir="/tmp")
44+
45+
@test issetequal(ls(dfg), ls(retDFG))
46+
@test issetequal(lsf(dfg), lsf(retDFG))
47+
for var in ls(dfg)
48+
@test getVariable(dfg, var) == getVariable(retDFG, var)
49+
end
50+
for fact in lsf(dfg)
51+
@test getFactor(dfg, fact) == getFactor(retDFG, fact)
52+
end
53+
54+
@test length(getBigDataEntries(getVariable(retDFG, :x1))) == 1
55+
@test typeof(getBigDataEntry(getVariable(retDFG, :x1),:testing)) == GeneralBigDataEntry
56+
@test length(getBigDataEntries(getVariable(retDFG, :x2))) == 1
57+
@test typeof(getBigDataEntry(getVariable(retDFG, :x2),:testing2)) == FileBigDataEntry
4858
end
49-
for fact in lsf(dfg)
50-
@test getFactor(dfg, fact) == getFactor(retDFG, fact)
51-
end
52-
53-
@test length(getBigDataEntries(getVariable(retDFG, :x1))) == 1
54-
@test typeof(getBigDataEntry(getVariable(retDFG, :x1),:testing)) == GeneralBigDataEntry
55-
@test length(getBigDataEntries(getVariable(retDFG, :x2))) == 1
56-
@test typeof(getBigDataEntry(getVariable(retDFG, :x2),:testing2)) == FileBigDataEntry
57-
5859
end

0 commit comments

Comments
 (0)