Skip to content

Commit 6877337

Browse files
authored
Merge pull request #376 from JuliaRobotics/maint/20Q2/seperateVarFacCopy
copyGraph separate variables factors
2 parents d708217 + b6a2b46 commit 6877337

File tree

6 files changed

+89
-62
lines changed

6 files changed

+89
-62
lines changed

.travis.yml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
- julia: 1.3
2525
env: IIF_TEST=true
2626
if: NOT branch =~ ^release.*$
27-
- arch: arm64
27+
# - arch: arm64
2828
- stage: "Documentation"
2929
julia: 1.3
3030
os: linux
@@ -33,15 +33,14 @@ jobs:
3333
- julia --project=docs/ docs/make.jl
3434
after_success: skip
3535
fast_finish: true
36+
allow_failures:
37+
- julia: nightly
38+
# - arch: arm64
39+
#- env: IIF_TEST=true
3640

3741
notifications:
3842
email: false
3943

40-
matrix:
41-
allow_failures:
42-
- julia: nightly
43-
- arch: arm64
44-
#- env: IIF_TEST=true
4544

4645
# Set the password for Neo4j to neo4j:test
4746
before_script:

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "DistributedFactorGraphs"
22
uuid = "b5cc3c7e-6572-11e9-2517-99fb8daf2f04"
3-
version = "0.7.2"
3+
version = "0.7.3"
44

55
[deps]
66
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

src/DFGPlots/DFGPlots.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ More information at [GraphPlot.jl](https://github.com/JuliaGraphs/GraphPlot.jl)
8888
function dfgplot(dfg::AbstractDFG, p::DFGPlotProps = DFGPlotProps())
8989
# TODO implement convert functions
9090
ldfg = LightDFG{NoSolverParams}()
91-
copyGraph!(ldfg, dfg, union(listVariables(dfg), listFactors(dfg)), copyGraphMetadata=false)
91+
copyGraph!(ldfg, dfg, listVariables(dfg), listFactors(dfg), copyGraphMetadata=false)
9292
dfgplot(ldfg, p)
9393
end
9494

src/LightDFG/services/LightDFG.jl

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -265,33 +265,31 @@ function getNeighbors(dfg::LightDFG, label::Symbol; solvable::Int=0)::Vector{Sym
265265

266266
end
267267

268-
269-
# TODO copy LightDFG to LightDFG overwrite
270-
# function copyGraph!(destDFG::LightDFG,
271-
# sourceDFG::LightDFG,
272-
# variableFactorLabels::Vector{Symbol};
273-
# copyGraphMetadata::Bool=false,
274-
# overwriteDest::Bool=false,
275-
# deepcopyNodes::Bool=false,
276-
# verbose::Bool = true)
277-
278-
function buildSubgraph(::Type{G}, dfg::LightDFG, variableFactorLabels::Vector{Symbol}, distance::Int=0; solvable::Int=0, kwargs...) where G <: AbstractDFG
279-
268+
function getNeighborhood(dfg::LightDFG, variableFactorLabels::Vector{Symbol}, distance::Int; solvable::Int=0)::Vector{Symbol}
280269
# find neighbors at distance to add
281270
nbhood = Int[]
282271

283272
for l in variableFactorLabels
284273
union!(nbhood, neighborhood(dfg.g, dfg.g.labels[l], distance))
285274
end
286275

287-
solvable != 0 && (filter!(id -> _isSolvable(dfg, dfg.g.labels[id], solvable), nbhood))
288-
289276
allvarfacs = [dfg.g.labels[id] for id in nbhood]
290-
# Copy the section of graph we want
291-
destDFG = deepcopyGraph(G, dfg, allvarfacs; kwargs...)
292-
return destDFG
277+
278+
solvable != 0 && filter!(nlbl -> (getSolvable(dfg, nlbl) >= solvable), allvarfacs)
279+
280+
return allvarfacs
281+
293282
end
294283

284+
# TODO copy LightDFG to LightDFG overwrite
285+
# function copyGraph!(destDFG::LightDFG,
286+
# sourceDFG::LightDFG,
287+
# variableFactorLabels::Vector{Symbol};
288+
# copyGraphMetadata::Bool=false,
289+
# overwriteDest::Bool=false,
290+
# deepcopyNodes::Bool=false,
291+
# verbose::Bool = true)
292+
295293

296294
# Biadjacency Matrix https://en.wikipedia.org/wiki/Adjacency_matrix#Of_a_bipartite_graph
297295
function getBiadjacencyMatrix(dfg::LightDFG; solvable::Int=0)::NamedTuple{(:B, :varLabels, :facLabels),Tuple{LightGraphs.SparseMatrixCSC,Vector{Symbol}, Vector{Symbol}}}

src/services/AbstractDFG.jl

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -799,19 +799,15 @@ Dev Note:
799799
"""
800800
function copyGraph!(destDFG::AbstractDFG,
801801
sourceDFG::AbstractDFG,
802-
variableFactorLabels::Vector{Symbol};
802+
variableLabels::Vector{Symbol},
803+
factorLabels::Vector{Symbol};
803804
copyGraphMetadata::Bool=false,
804805
overwriteDest::Bool=false,
805806
deepcopyNodes::Bool=false,
806807
verbose::Bool = true)
807808
# Split into variables and factors
808-
sourceVariables = map(vId->getVariable(sourceDFG, vId), intersect(listVariables(sourceDFG), variableFactorLabels))
809-
sourceFactors = map(fId->getFactor(sourceDFG, fId), intersect(listFactors(sourceDFG), variableFactorLabels))
810-
if length(sourceVariables) + length(sourceFactors) != length(variableFactorLabels)
811-
remv = setdiff(map(v->v.label, sourceVariables), variableFactorLabels)
812-
remf = setdiff(map(f->f.label, sourceFactors), variableFactorLabels)
813-
error("Cannot copy because cannot find the following nodes in the source graph: $remv, $remf")
814-
end
809+
sourceVariables = map(vId->getVariable(sourceDFG, vId), variableLabels)
810+
sourceFactors = map(fId->getFactor(sourceDFG, fId), factorLabels)
815811

816812
# Now we have to add all variables first,
817813
for variable in sourceVariables
@@ -861,18 +857,20 @@ end
861857

862858
function deepcopyGraph!(destDFG::AbstractDFG,
863859
sourceDFG::AbstractDFG,
864-
variableFactorLabels::Vector{Symbol} = union(ls(sourceDFG), lsf(sourceDFG));
860+
variableLabels::Vector{Symbol} = ls(sourceDFG),
861+
factorLabels::Vector{Symbol} = lsf(sourceDFG);
865862
kwargs...)
866-
copyGraph!(destDFG, sourceDFG, variableFactorLabels; deepcopyNodes=true, kwargs...)
863+
copyGraph!(destDFG, sourceDFG, variableLabels, factorLabels; deepcopyNodes=true, kwargs...)
867864
end
868865

869866

870-
function deepcopyGraph( ::Type{T},
871-
sourceDFG::AbstractDFG,
872-
variableFactorLabels::Vector{Symbol} = union(ls(sourceDFG), lsf(sourceDFG));
873-
kwargs...) where T <: AbstractDFG
867+
function deepcopyGraph(::Type{T},
868+
sourceDFG::AbstractDFG,
869+
variableLabels::Vector{Symbol} = ls(sourceDFG),
870+
factorLabels::Vector{Symbol} = lsf(sourceDFG);
871+
kwargs...) where T <: AbstractDFG
874872
destDFG = T(getDFGInfo(sourceDFG)...)
875-
copyGraph!(destDFG, sourceDFG, variableFactorLabels; deepcopyNodes=true, kwargs...)
873+
copyGraph!(destDFG, sourceDFG, variableLabels, factorLabels; deepcopyNodes=true, kwargs...)
876874
return destDFG
877875
end
878876

@@ -946,8 +944,10 @@ function buildSubgraph(::Type{G},
946944
#build up the neighborhood from variableFactorLabels
947945
allvarfacs = getNeighborhood(dfg, variableFactorLabels, distance; solvable=solvable)
948946

947+
variableLabels = intersect(listVariables(dfg), allvarfacs)
948+
factorLabels = intersect(listFactors(dfg), allvarfacs)
949949
# Copy the section of graph we want
950-
destDFG = deepcopyGraph(G, dfg, allvarfacs; kwargs...)
950+
destDFG = deepcopyGraph(G, dfg, variableLabels, factorLabels; kwargs...)
951951
return destDFG
952952
end
953953

@@ -965,15 +965,19 @@ Related:
965965
"""
966966
function mergeGraph!(destDFG::AbstractDFG,
967967
sourceDFG::AbstractDFG,
968-
variableFactorLabels::Vector{Symbol} = union(ls(sourceDFG), lsf(sourceDFG)),
968+
variableLabels::Vector{Symbol} = ls(sourceDFG),
969+
factorLabels::Vector{Symbol} = lsf(sourceDFG),
969970
distance::Int = 0;
970971
solvable::Int = 0,
971972
kwargs...)
972973

973974
# find neighbors at distance to add
974-
allvarfacs = getNeighborhood(sourceDFG, variableFactorLabels, distance; solvable=solvable)
975+
allvarfacs = getNeighborhood(sourceDFG, union(variableLabels, factorLabels), distance; solvable=solvable)
976+
977+
sourceVariables = intersect(listVariables(sourceDFG), allvarfacs)
978+
sourceFactors = intersect(listFactors(sourceDFG), allvarfacs)
975979

976-
copyGraph!(destDFG, sourceDFG, allvarfacs; deepcopyNodes=true, overwriteDest=true, kwargs...)
980+
copyGraph!(destDFG, sourceDFG, sourceVariables, sourceFactors; deepcopyNodes=true, overwriteDest=true, kwargs...)
977981

978982
return destDFG
979983
end
@@ -1082,7 +1086,7 @@ Produces a dot-format of the graph for visualization.
10821086
function toDot(dfg::AbstractDFG)::String
10831087
#TODO implement convert
10841088
graphsdfg = GraphsDFG{NoSolverParams}()
1085-
copyGraph!(graphsdfg, dfg, union(listVariables(dfg), listFactors(dfg)))
1089+
copyGraph!(graphsdfg, dfg, listVariables(dfg), listFactors(dfg))
10861090
# Calls down to GraphsDFG.toDot
10871091
return toDot(graphsdfg)
10881092
end
@@ -1103,7 +1107,7 @@ function toDotFile(dfg::AbstractDFG, fileName::String="/tmp/dfg.dot")::Nothing
11031107
graphsdfg = dfg
11041108
else
11051109
graphsdfg = GraphsDFG{NoSolverParams}()
1106-
copyGraph!(graphsdfg, dfg, union(listVariables(dfg), listFactors(dfg)))
1110+
copyGraph!(graphsdfg, dfg, listVariables(dfg), listFactors(dfg))
11071111
end
11081112

11091113
open(fileName, "w") do fid

test/testBlocks.jl

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,10 @@ function VariablesandFactorsCRUD_SET!(fg, v1, v2, v3, f0, f1, f2)
362362
@test_throws ErrorException getVariable(fg, :c)
363363
@test_throws ErrorException getFactor(fg, :bcf1)
364364

365+
#test issue #375
366+
@test_skip @test_throws ErrorException getVariable(fg, :abf1)
367+
@test_skip @test_throws ErrorException getFactor(fg, :a)
368+
365369
# Existence
366370
@test exists(fg, :a)
367371
@test !exists(fg, :c)
@@ -1005,7 +1009,7 @@ end
10051009

10061010
function BuildingSubgraphs(testDFGAPI; VARTYPE=DFGVariable, FACTYPE=DFGFactor)
10071011

1008-
# "Getting Subgraphs"
1012+
# "Getting Subgraphs"
10091013
dfg, verts, facs = connectivityTestGraph(testDFGAPI, VARTYPE=VARTYPE, FACTYPE=FACTYPE)
10101014
# Subgraphs
10111015
dfgSubgraph = buildSubgraph(testDFGAPI, dfg, [verts[1].label], 2)
@@ -1132,41 +1136,63 @@ function CopyFunctionsTest(testDFGAPI; kwargs...)
11321136
varlbls = ls(dfg)
11331137
faclbls = lsf(dfg)
11341138

1135-
dcdfg = DFG.deepcopyGraph(LightDFG, dfg)
1139+
dcdfg = deepcopyGraph(LightDFG, dfg)
11361140

11371141
@test issetequal(ls(dcdfg), varlbls)
11381142
@test issetequal(lsf(dcdfg), faclbls)
11391143

1140-
lbls = [:x2, :x3, :x2x3f1]
1141-
dcdfg_part = DFG.deepcopyGraph(LightDFG, dfg, lbls)
1144+
vlbls = [:x2, :x3]
1145+
flbls = [:x2x3f1]
1146+
dcdfg_part = deepcopyGraph(LightDFG, dfg, vlbls, flbls)
11421147

1143-
@test issetequal(union(ls(dcdfg_part), lsf(dcdfg_part)), lbls)
1148+
@test issetequal(ls(dcdfg_part), vlbls)
1149+
@test issetequal(lsf(dcdfg_part), flbls)
11441150

11451151
# deepcopy subgraph ignoring orphans
1146-
@test_logs (:warn, r"orphan") dcdfg_part = DFG.deepcopyGraph(LightDFG, dfg, union(lbls, [:x1x2f1]))
1147-
@test issetequal(union(ls(dcdfg_part), lsf(dcdfg_part)), lbls)
1152+
@test_logs (:warn, r"orphan") dcdfg_part = deepcopyGraph(LightDFG, dfg, vlbls, union(flbls, [:x1x2f1]))
1153+
@test issetequal(ls(dcdfg_part), vlbls)
1154+
@test issetequal(lsf(dcdfg_part), flbls)
11481155

11491156
# deepcopy subgraph with 2 parts
1150-
lbls = [:x2, :x3, :x2x3f1, :x5, :x6, :x5x6f1, :x10]
1151-
dcdfg_part = DFG.deepcopyGraph(LightDFG, dfg, lbls)
1152-
@test issetequal(union(ls(dcdfg_part), lsf(dcdfg_part)), lbls)
1157+
vlbls = [:x2, :x3, :x5, :x6, :x10]
1158+
flbls = [:x2x3f1, :x5x6f1]
1159+
dcdfg_part = deepcopyGraph(LightDFG, dfg, vlbls, flbls)
1160+
@test issetequal(ls(dcdfg_part), vlbls)
1161+
@test issetequal(lsf(dcdfg_part), flbls)
11531162
@test !isFullyConnected(dcdfg_part)
11541163
# dfgplot(dcdfg_part)
11551164

1165+
1166+
vlbls = [:x2, :x3]
1167+
dcdfg_part = deepcopyGraph(LightDFG, dfg, vlbls; verbose=false)
1168+
@test issetequal(ls(dcdfg_part), vlbls)
1169+
@test issetequal(lsf(dcdfg_part), [:x2x3f1])
1170+
11561171
# not found errors
1157-
@test_throws ErrorException DFG.deepcopyGraph(LightDFG, dfg, [:x0, :a])
1172+
@test_throws ErrorException deepcopyGraph(LightDFG, dfg, [:x1, :a])
1173+
@test_throws ErrorException deepcopyGraph(LightDFG, dfg, [:x1], [:f1])
11581174

11591175
# already exists errors
1160-
dcdfg_part = DFG.deepcopyGraph(LightDFG, dfg, [:x1, :x2, :x3, :x1x2f1, :x2x3f1])
1161-
@test_throws ErrorException DFG.deepcopyGraph!(dcdfg_part, dfg, [:x4, :x2, :x3, :x1x2f1, :x2x3f1])
1162-
@test_throws ErrorException DFG.deepcopyGraph!(dcdfg_part, dfg, [:x1x2f1])
1176+
dcdfg_part = deepcopyGraph(LightDFG, dfg, [:x1, :x2, :x3], [:x1x2f1, :x2x3f1])
1177+
@test_throws ErrorException deepcopyGraph!(dcdfg_part, dfg, [:x4, :x2, :x3], [:x1x2f1, :x2x3f1])
1178+
@test_skip @test_throws ErrorException deepcopyGraph!(dcdfg_part, dfg, [:x1x2f1])
11631179

11641180
# same but overwrite destination
1165-
DFG.deepcopyGraph!(dcdfg_part, dfg, [:x4, :x2, :x3, :x1x2f1, :x2x3f1]; overwriteDest = true)
1181+
deepcopyGraph!(dcdfg_part, dfg, [:x4, :x2, :x3], [:x1x2f1, :x2x3f1]; overwriteDest = true)
1182+
1183+
deepcopyGraph!(dcdfg_part, dfg, Symbol[], [:x1x2f1]; overwriteDest=true)
11661184

1167-
DFG.deepcopyGraph!(dcdfg_part, dfg, [:x1x2f1]; overwriteDest=true)
1185+
vlbls1 = [:x1, :x2, :x3]
1186+
vlbls2 = [:x4, :x5, :x6]
1187+
dcdfg_part1 = deepcopyGraph(LightDFG, dfg, vlbls1)
1188+
dcdfg_part2 = deepcopyGraph(GraphsDFG, dfg, vlbls2)
11681189

1190+
mergedGraph = testDFGAPI()
1191+
mergeGraph!(mergedGraph, dcdfg_part1)
1192+
mergeGraph!(mergedGraph, dcdfg_part2)
11691193

1194+
@test issetequal(ls(mergedGraph), union(vlbls1, vlbls2))
1195+
@test issetequal(lsf(mergedGraph), union(lsf(dcdfg_part1), lsf(dcdfg_part2)))
11701196
# convert to...
11711197
# condfg = convert(GraphsDFG, dfg)
11721198
# @test condfg isa GraphsDFG

0 commit comments

Comments
 (0)