Skip to content

Commit e4a81d4

Browse files
committed
buildSubgraphFromLabels!
1 parent 258cc40 commit e4a81d4

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

src/DistributedFactorGraphs.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ include("CloudGraphsDFG/CloudGraphsDFG.jl")
100100
const InMemoryDFGTypes = Union{GraphsDFG, LightDFG}
101101
export InMemoryDFGTypes
102102

103+
# Needs a home.
104+
include("needsahome.jl")
105+
103106
function __init__()
104107
@require GraphPlot = "a2cc645c-3eea-5389-862e-a155d0052231" begin
105108
@info "Including Plots"

src/needsahome.jl

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
export buildSubgraphFromLabels!
2+
3+
4+
"""
5+
$SIGNATURES
6+
Construct a new factor graph object as a subgraph of `dfg <: AbstractDFG` based on the
7+
variable labels `syms::Vector{Symbols}`.
8+
9+
SamC: Can we not just use _copyIntoGraph! for this? Looks like a small refactor to make it work.
10+
Will paste in as-is for now and we can figure it out as we go.
11+
12+
Notes
13+
- Slighly messy internals, but gets the job done -- some room for performance improvement.
14+
Related
15+
getVariableIds
16+
"""
17+
function buildSubgraphFromLabels!(dfg::G,
18+
syms::Vector{Symbol},
19+
destType::Type{<:AbstractDFG}=GraphsDFG; #Which one? I don't have a strong opinion on this.
20+
solvable::Int=0)::G where G <: AbstractDFG
21+
22+
# data structure for cliq sub graph
23+
if G <: InMemoryDFGTypes
24+
#Same type
25+
cliqSubFg = G(params=getSolverParams(dfg))
26+
else
27+
#Default
28+
cliqSubFg = destType{typeof(getSolverParms(dfg))}(params=getSolverParams(dfg))
29+
end
30+
31+
# add a little too many variables (since we need the factors)
32+
for sym in syms
33+
getSubgraphAroundNode(dfg, getVariable(dfg, sym), 2, false, cliqSubFg, solvable=solvable)
34+
end
35+
36+
# remove excessive variables that were copied by neighbors distance 2
37+
currVars = getVariableIds(cliqSubFg)
38+
toDelVars = setdiff(currVars, syms)
39+
for dv in toDelVars
40+
# delete any neighboring factors first
41+
for fc in lsf(cliqSubFg, dv)
42+
deleteFactor!(cliqSubFg, fc)
43+
end
44+
45+
# and the variable itself
46+
deleteVariable!(cliqSubFg, dv)
47+
end
48+
49+
return cliqSubFg
50+
end

test/needsahomeTests.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using DistributedFactorGraphs
2+
using Test
3+
4+
@testset "Needs-a-home tests" begin
5+
dfg = GraphsDFG{NoSolverParams}(params=NoSolverParams())
6+
struct TestInferenceVariable1 <: InferenceVariable end
7+
struct TestInferenceVariable2 <: InferenceVariable end
8+
9+
v1 = DFGVariable(:a, TestInferenceVariable1())
10+
v2 = DFGVariable(:b, TestInferenceVariable1())
11+
f1 = DFGFactor{Int, :Symbol}(:f1)
12+
addVariable!(dfg, v1)
13+
addVariable!(dfg, v2)
14+
addFactor!(dfg, [v1, v2], f1)
15+
16+
newdfg = buildSubgraphFromLabels!(dfg, [:b])
17+
@test ls(newdfg) == [:b]
18+
# Okay it looks like this function only accepts variables, is that right?
19+
@test_throws Exception newdfg = buildSubgraphFromLabels!(dfg, [:b, :f1])
20+
newdfg = buildSubgraphFromLabels!(dfg, [:b, :a])
21+
@test symdiff(ls(newdfg), [:b, :a]) == []
22+
23+
end

test/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ end
4444
include("DataStoreTests.jl")
4545
end
4646

47+
@testset "Needs-a-Home Tests" begin
48+
include("needsahomeTests.jl")
49+
end
4750

4851
@testset "LightDFG subtype tests" begin
4952
for type in [(var=DFGVariableSummary, fac=DFGFactorSummary), (var=SkeletonDFGVariable,fac=SkeletonDFGFactor)]

0 commit comments

Comments
 (0)