-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFileDFG.jl
More file actions
204 lines (172 loc) · 6.69 KB
/
FileDFG.jl
File metadata and controls
204 lines (172 loc) · 6.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"""
$(SIGNATURES)
Save a DFG to a folder. Will create/overwrite folder if it exists.
DevNotes:
- TODO remove `compress` kwarg.
# Example
```julia
using DistributedFactorGraphs, IncrementalInference
# Create a DFG - can make one directly, e.g. GraphsDFG{NoSolverParams}() or use IIF:
dfg = initfg()
# ... Add stuff to graph using either IIF or DFG:
v1 = addVariable!(dfg, :a, ContinuousScalar, tags = [:POSE], solvable=0)
# Now save it:
saveDFG(dfg, "/tmp/saveDFG.tar.gz")
```
"""
function saveDFG(folder::AbstractString, dfg::AbstractDFG)
# TODO: Deprecate the folder functionality
# Clean up save path if a file is specified
savepath = folder[end] == '/' ? folder[1:(end - 1)] : folder
savepath = splitext(splitext(savepath)[1])[1] # In case of .tar.gz
variables = getVariables(dfg)
factors = getFactors(dfg)
varFolder = "$savepath/variables"
factorFolder = "$savepath/factors"
# Folder preparations
if !isdir(savepath)
@debug "Folder '$savepath' doesn't exist, creating..."
mkpath(savepath)
end
!isdir(varFolder) && mkpath(varFolder)
!isdir(factorFolder) && mkpath(factorFolder)
# Clearing out the folders
map(f -> rm("$varFolder/$f"), readdir(varFolder))
map(f -> rm("$factorFolder/$f"), readdir(factorFolder))
# Variables
@showprogress desc = "saving variables" for v in variables
# vPacked = packVariable(v)
JSON.json("$varFolder/$(v.label).json", v; style = DFGJSONStyle())
end
# Factors
@showprogress desc = "saving factors" for f in factors
JSON.json("$factorFolder/$(f.label).json", f; style = DFGJSONStyle())
end
#GraphsDFG nodes
@assert isa(dfg, GraphsDFG) "only metadata for GraphsDFG are supported"
p = Progress(4; desc = "Saving DFG Nodes")
JSON.json("$savepath/graphroot.json", dfg.graph; style = DFGJSONStyle())
next!(p)
JSON.json("$savepath/agent.json", dfg.agent; style = DFGJSONStyle())
next!(p)
JSON.json("$savepath/solverparams.json", dfg.solverParams; style = DFGJSONStyle())
next!(p)
JSON.json("$savepath/blobstores.json", dfg.blobStores; style = DFGJSONStyle())
next!(p)
savedir = dirname(savepath) # is this a path of just local name? #344 -- workaround with unique names
savename = basename(string(savepath))
@assert savename != ""
destfile = joinpath(savedir, savename * ".tar.gz")
#create Tarbal using Tar.jl #351
tar_gz = open(destfile; write = true)
tar = CodecZlib.GzipCompressorStream(tar_gz)
Tar.create(joinpath(savedir, savename), tar)
close(tar)
#not compressed version
# Tar.create(joinpath(savedir,savename), destfile)
return Base.rm(joinpath(savedir, savename); recursive = true)
end
# support both argument orders, #581
saveDFG(dfg::AbstractDFG, folder::AbstractString) = saveDFG(folder, dfg)
#TODO loadDFG(dst::AbstractString) to load an equivalent dfg, but defined in IIF
"""
$(SIGNATURES)
Load a DFG from a saved folder.
# Example
```julia
using DistributedFactorGraphs, IncrementalInference
# Create a DFG - can make one directly, e.g. GraphsDFG{NoSolverParams}() or use IIF:
dfg = initfg()
# Load the graph
loadDFG!(dfg, "/tmp/savedgraph.tar.gz")
# Use the DFG as you do normally.
ls(dfg)
```
See also: [`loadDFG`](@ref), [`saveDFG`](@ref)
"""
function loadDFG!(
dfgLoadInto::AbstractDFG{V, F},
file::AbstractString;
) where {V <: AbstractGraphVariable, F <: AbstractGraphFactor}
# add if doesn't have .tar.gz extension
if !contains(basename(file), ".tar.gz")
file *= ".tar.gz"
end
# check the file actually exists
@assert isfile(file) "cannot find file $file"
# only extract the json files needed for the variables and factors
tar_gz = open(file)
tar = CodecZlib.GzipDecompressorStream(tar_gz)
dfgnodenames = r"^(factors|variables)"
loaddir = Tar.extract(hdr -> contains(hdr.path, dfgnodenames), tar)
close(tar)
# extract the factor graph from fileDFG folder
variablefiles = readdir(joinpath(loaddir, "variables"); sort = false, join = true)
# type instability on `variables` as either `::Vector{Variable}` or `::Vector{VariableCompute{<:}}` (vector of abstract)
variables = @showprogress dt=1 desc = "loading variables" asyncmap(
variablefiles,
) do file
v = JSON.parsefile(file, V; style = DFGJSONStyle())
return addVariable!(dfgLoadInto, v)
end
@debug "Loaded $(length(variables)) variables"
factorfiles = readdir(joinpath(loaddir, "factors"); sort = false, join = true)
factors = @showprogress dt=1 desc = "loading factors" asyncmap(factorfiles) do file
f = JSON.parsefile(file, F; style = DFGJSONStyle())
return addFactor!(dfgLoadInto, f)
end
@debug "Loaded $(length(factors)) factors"
if isa(dfgLoadInto, GraphsDFG) && getTypeDFGFactors(dfgLoadInto) <: FactorDFG
# Finally, rebuild the CCW's for the factors to completely reinflate them
@showprogress dt=1 desc = "Rebuilding factor solver cache" for factor in factors
rebuildFactorCache!(dfgLoadInto, factor)
end
end
Base.rm(loaddir; recursive = true, force = true)
return dfgLoadInto
end
"""
$SIGNATURES
Convenience graph loader into a default `LocalDFG`.
See also: [`loadDFG!`](@ref), [`saveDFG`](@ref)
"""
function loadDFG(file::AbstractString)
# add if doesn't have .tar.gz extension
if !contains(basename(file), ".tar.gz")
file *= ".tar.gz"
end
# check the file actually exists
@assert isfile(file) "cannot find file $file"
# only extract the json files needed to rebuild DFG object
tar_gz = open(file)
tar = CodecZlib.GzipDecompressorStream(tar_gz)
dfgnodenames = r"^(agent\.json|blobstores\.json|graphroot\.json|solverparams\.json)$"
loaddir = Tar.extract(hdr -> contains(hdr.path, dfgnodenames), tar)
close(tar)
progess = Progress(4; desc = "Loading DFG Nodes")
agent = JSON.parsefile(joinpath(loaddir, "agent.json"), Agent; style = DFGJSONStyle())
next!(progess)
graph = JSON.parsefile(
joinpath(loaddir, "graphroot.json"),
Graphroot;
style = DFGJSONStyle(),
)
next!(progess)
solverParams = JSON.parsefile(
joinpath(loaddir, "solverparams.json"),
AbstractDFGParams;
style = DFGJSONStyle(),
)
next!(progess)
blobStores = JSON.parsefile(
joinpath(loaddir, "blobstores.json"),
Dict{Symbol, AbstractBlobstore};
style = DFGJSONStyle(),
)
next!(progess)
dfg = GraphsDFG(; agent, graph, solverParams, blobStores)
@debug "DFG.loadDFG is deleting a temp folder created during unzip, $loaddir"
# cleanup temporary folder
Base.rm(loaddir; recursive = true, force = true)
return loadDFG!(dfg, file)
end