Skip to content

Commit 0cccdd4

Browse files
dehannAffie
andauthored
small refactor on loading different types (#1043)
* small refactor on loading different types * run julia formatter --------- Co-authored-by: Johannes Terblanche <[email protected]>
1 parent 8b75810 commit 0cccdd4

File tree

2 files changed

+18
-37
lines changed

2 files changed

+18
-37
lines changed

src/FileDFG/services/FileDFG.jl

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -166,53 +166,34 @@ function loadDFG!(
166166
varFiles = sort(readdir(varFolder; sort = false); lt = natural_lt)
167167
factorFiles = sort(readdir(factorFolder; sort = false); lt = natural_lt)
168168

169+
packedvars = @showprogress 1 "loading variables" map(varFiles) do varFile
170+
jstr = read("$varFolder/$varFile", String)
171+
return JSON3.read(jstr, PackedVariable)
172+
end
173+
# FIXME, why is this treated different from VariableSkeleton, VariableSummary?
174+
# FIXME, still creates type instability on `variables` as either `::Variable` or `::DFGVariable`
169175
if isa(dfgLoadInto, GraphsDFG) && GraphsDFGs._variablestype(dfgLoadInto) == Variable
170-
variables = @showprogress 1 "loading variables" map(varFiles) do varFile
171-
jstr = read("$varFolder/$varFile", String)
172-
return JSON3.read(jstr, PackedVariable)
173-
end
176+
variables = packedvars
174177
else
175-
variables = DFGVariable[]
176-
@showprogress 1 "loading variables" for varFile in varFiles
177-
jstr = read("$varFolder/$varFile", String)
178-
try
179-
packedData = JSON3.read(jstr, PackedVariable)
180-
push!(variables, unpackVariable(packedData))
181-
catch ex
182-
@error(
183-
"JSON3 is having trouble reading $varFolder/$varFile into a PackedVariable"
184-
)
185-
@show jstr
186-
throw(ex)
187-
end
188-
end
178+
variables = unpackVariable.(packedvars)
189179
end
190180

191181
@info "Loaded $(length(variables)) variables"#- $(map(v->v.label, variables))"
192182
@info "Inserting variables into graph..."
193183
# Adding variables
194184
map(v -> addVariable!(dfgLoadInto, v), variables)
195185

186+
packedfacts = @showprogress 1 "loading factors" map(factorFiles) do factorFile
187+
jstr = read("$factorFolder/$factorFile", String)
188+
return JSON3.read(jstr, PackedFactor)
189+
end
190+
# FIXME, still creates type instability on `variables` as either `::Factor` or `::DFGFactor{<:}`
196191
if isa(dfgLoadInto, GraphsDFG) && GraphsDFGs._factorstype(dfgLoadInto) == PackedFactor
197-
factors = @showprogress 1 "loading factors" map(factorFiles) do factorFile
198-
jstr = read("$factorFolder/$factorFile", String)
199-
return JSON3.read(jstr, PackedFactor)
200-
end
192+
factors = packedfacts
201193
else
202-
@showprogress 1 "loading factors" for factorFile in factorFiles
203-
jstr = read("$factorFolder/$factorFile", String)
204-
try
205-
packedData = JSON3.read(jstr, PackedFactor)
206-
push!(factors, unpackFactor(dfgLoadInto, packedData))
207-
catch ex
208-
@error(
209-
"JSON3 is having trouble reading $factorFolder/$factorFile into a PackedFactor"
210-
)
211-
@show jstr
212-
throw(ex)
213-
end
214-
end
194+
factors = unpackFactor.(dfgLoadInto, packedfacts)
215195
end
196+
216197
@info "Loaded $(length(factors)) factors"# - $(map(f->f.label, factors))"
217198
@info "Inserting factors into graph..."
218199
# # Adding factors

test/iifInterfaceTests.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ end
153153
@test !isPrior(dfg, :abf1) # f1 is not a prior
154154
@test lsfPriors(dfg) == []
155155
#FIXME don't know what it is supposed to do
156-
@test 0 < length( lsfTypes(dfg) )
156+
@test 0 < length(lsfTypes(dfg))
157157

158158
@test ls(dfg, LinearRelative) == [:abf1]
159159
@test lsf(dfg, LinearRelative) == [:abf1]
@@ -163,7 +163,7 @@ end
163163
@test getVariableType(dfg, :a) isa Position{1}
164164

165165
#TODO what is lsTypes supposed to return?
166-
@test 0 < length( lsTypes(dfg) )
166+
@test 0 < length(lsTypes(dfg))
167167

168168
@test issetequal(ls(dfg, Position{1}), [:a, :b])
169169
@test issetequal(lsWho(dfg, :Position), [:a, :b])

0 commit comments

Comments
 (0)