Skip to content

Commit 47f1527

Browse files
authored
Merge pull request #212 from JuliaRobotics/twig/4Q19/getSubGraphAroundNode_filter
Filtering getSubgraphAroundNode with solvable field
2 parents b683df9 + b4556a0 commit 47f1527

File tree

7 files changed

+26
-9
lines changed

7 files changed

+26
-9
lines changed

src/CloudGraphsDFG/services/CloudGraphsDFG.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,12 +663,12 @@ Optionally provide a distance to specify the number of edges should be followed.
663663
Optionally provide an existing subgraph addToDFG, the extracted nodes will be copied into this graph. By default a new subgraph will be created.
664664
Note: By default orphaned factors (where the subgraph does not contain all the related variables) are not returned. Set includeOrphanFactors to return the orphans irrespective of whether the subgraph contains all the variables.
665665
"""
666-
function getSubgraphAroundNode(dfg::CloudGraphsDFG, node::DFGNode, distance::Int64=1, includeOrphanFactors::Bool=false, addToDFG::AbstractDFG=_getDuplicatedEmptyDFG(dfg))::AbstractDFG
666+
function getSubgraphAroundNode(dfg::CloudGraphsDFG, node::DFGNode, distance::Int64=1, includeOrphanFactors::Bool=false, addToDFG::AbstractDFG=_getDuplicatedEmptyDFG(dfg); solvable::Int=0)::AbstractDFG
667667
distance < 1 && error("getSubgraphAroundNode() only works for distance > 0")
668668

669669
# Thank you Neo4j for 0..* awesomeness!!
670670
neighborList = _getLabelsFromCyphonQuery(dfg.neo4jInstance,
671-
"(n:$(dfg.userId):$(dfg.robotId):$(dfg.sessionId):$(node.label))-[FACTORGRAPH*0..$distance]-(node:$(dfg.userId):$(dfg.robotId):$(dfg.sessionId)) WHERE (n:VARIABLE OR n:FACTOR OR node:VARIABLE OR node:FACTOR) and not (node:SESSION)")
671+
"(n:$(dfg.userId):$(dfg.robotId):$(dfg.sessionId):$(node.label))-[FACTORGRAPH*0..$distance]-(node:$(dfg.userId):$(dfg.robotId):$(dfg.sessionId)) WHERE (n:VARIABLE OR n:FACTOR OR node:VARIABLE OR node:FACTOR) and not (node:SESSION) and (node.solvable >= $solvable)")
672672

673673
# Copy the section of graph we want
674674
_copyIntoGraph!(dfg, addToDFG, neighborList, includeOrphanFactors)

src/GraphsDFG/services/GraphsDFG.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ Optionally provide a distance to specify the number of edges should be followed.
329329
Optionally provide an existing subgraph addToDFG, the extracted nodes will be copied into this graph. By default a new subgraph will be created.
330330
Note: By default orphaned factors (where the subgraph does not contain all the related variables) are not returned. Set includeOrphanFactors to return the orphans irrespective of whether the subgraph contains all the variables.
331331
"""
332-
function getSubgraphAroundNode(dfg::GraphsDFG{P}, node::T, distance::Int64=1, includeOrphanFactors::Bool=false, addToDFG::GraphsDFG=GraphsDFG{P}())::GraphsDFG where {P <: AbstractParams, T <: DFGNode}
332+
function getSubgraphAroundNode(dfg::GraphsDFG{P}, node::T, distance::Int64=1, includeOrphanFactors::Bool=false, addToDFG::GraphsDFG=GraphsDFG{P}(); solvable::Int=0)::GraphsDFG where {P <: AbstractParams, T <: DFGNode}
333333
if !haskey(dfg.labelDict, node.label)
334334
error("Variable/factor with label '$(node.label)' does not exist in the factor graph")
335335
end
@@ -343,7 +343,7 @@ function getSubgraphAroundNode(dfg::GraphsDFG{P}, node::T, distance::Int64=1, in
343343
for (key, node) in curList
344344
neighbors = in_neighbors(node, dfg.g) #Don't use out_neighbors! It enforces directiveness even if we don't want it
345345
for neighbor in neighbors
346-
if !haskey(neighborList, neighbor.dfgNode.label)
346+
if !haskey(neighborList, neighbor.dfgNode.label) && (isSolvable(neighbor.dfgNode) >= solvable)
347347
push!(neighborList, neighbor.dfgNode.label => neighbor)
348348
push!(newNeighbors, neighbor.dfgNode.label => neighbor)
349349
end

src/LightDFG/services/LightDFG.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,13 +395,15 @@ Optionally provide a distance to specify the number of edges should be followed.
395395
Optionally provide an existing subgraph addToDFG, the extracted nodes will be copied into this graph. By default a new subgraph will be created.
396396
Note: By default orphaned factors (where the subgraph does not contain all the related variables) are not returned. Set includeOrphanFactors to return the orphans irrespective of whether the subgraph contains all the variables.
397397
"""
398-
function getSubgraphAroundNode(dfg::LightDFG{P,V,F}, node::DFGNode, distance::Int64=1, includeOrphanFactors::Bool=false, addToDFG::LightDFG=LightDFG{P,V,F}())::LightDFG where {P <: AbstractParams, V <: AbstractDFGVariable, F <: AbstractDFGFactor}
398+
function getSubgraphAroundNode(dfg::LightDFG{P,V,F}, node::DFGNode, distance::Int64=1, includeOrphanFactors::Bool=false, addToDFG::LightDFG=LightDFG{P,V,F}(); solvable::Int=0)::LightDFG where {P <: AbstractParams, V <: AbstractDFGVariable, F <: AbstractDFGFactor}
399399
if !exists(dfg,node.label)
400400
error("Variable/factor with label '$(node.label)' does not exist in the factor graph")
401401
end
402402

403403
# Get a list of all unique neighbors inside 'distance'
404404
ns = neighborhood(dfg.g, dfg.g.labels[node.label], distance)
405+
# Always return the center node, skip that if we're filtering.
406+
solvable != 0 && (filter!(id -> _isSolvable(dfg, dfg.g.labels[id], solvable) || dfg.g.labels[id] == node.label, ns))
405407

406408
# Copy the section of graph we want
407409
_copyIntoGraph!(dfg, addToDFG, ns, includeOrphanFactors)

src/SymbolDFG/services/SymbolDFG.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ Optionally provide a distance to specify the number of edges should be followed.
373373
Optionally provide an existing subgraph addToDFG, the extracted nodes will be copied into this graph. By default a new subgraph will be created.
374374
Note: By default orphaned factors (where the subgraph does not contain all the related variables) are not returned. Set includeOrphanFactors to return the orphans irrespective of whether the subgraph contains all the variables.
375375
"""
376-
function getSubgraphAroundNode(dfg::SymbolDFG, node::DFGNode, distance::Int64=1, includeOrphanFactors::Bool=false, addToDFG::SymbolDFG=SymbolDFG{AbstractParams}())::SymbolDFG
376+
function getSubgraphAroundNode(dfg::SymbolDFG, node::DFGNode, distance::Int64=1, includeOrphanFactors::Bool=false, addToDFG::SymbolDFG=SymbolDFG{AbstractParams}(); solvable::Int=0)::SymbolDFG
377377
if !exists(dfg,node.label)
378378
error("Variable/factor with label '$(node.label)' does not exist in the factor graph")
379379
end
@@ -389,7 +389,7 @@ function getSubgraphAroundNode(dfg::SymbolDFG, node::DFGNode, distance::Int64=1,
389389
for cl in curList
390390
neighbors = outneighbors(dfg.g, cl)
391391
for neighbor in neighbors
392-
if !(neighbor in neighborList)
392+
if !(neighbor in neighborList) && _isSolvable(dfg, neighbor, solvable)
393393
push!(neighborList, neighbor)
394394
push!(newNeighbors, neighbor)
395395
end

src/services/AbstractDFG.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,9 @@ Retrieve a deep subgraph copy around a given variable or factor.
367367
Optionally provide a distance to specify the number of edges should be followed.
368368
Optionally provide an existing subgraph addToDFG, the extracted nodes will be copied into this graph. By default a new subgraph will be created.
369369
Note: By default orphaned factors (where the subgraph does not contain all the related variables) are not returned. Set includeOrphanFactors to return the orphans irrespective of whether the subgraph contains all the variables.
370+
Note: Always returns the node at the center, but filters around it if solvable is set.
370371
"""
371-
function getSubgraphAroundNode(dfg::G, node::T, distance::Int64=1, includeOrphanFactors::Bool=false, addToDFG::H=_getDuplicatedEmptyDFG(dfg))::G where {G <: AbstractDFG, H <: AbstractDFG, P <: AbstractParams, T <: DFGNode}
372+
function getSubgraphAroundNode(dfg::G, node::T, distance::Int64=1, includeOrphanFactors::Bool=false, addToDFG::H=_getDuplicatedEmptyDFG(dfg); solvable::Int=0)::G where {G <: AbstractDFG, H <: AbstractDFG, P <: AbstractParams, T <: DFGNode}
372373
error("getSubgraphAroundNode not implemented for $(typeof(dfg))")
373374
end
374375

test/iifInterfaceTests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,13 @@ end
405405
# Only returns x1 and x2
406406
@test symdiff([:x1, :x1x2f1, :x2], [ls(dfgSubgraph)..., lsf(dfgSubgraph)...]) == []
407407

408+
# DFG issue #201 Test include orphan factors with filtering - should only return x7 with solvable=1
409+
dfgSubgraph = getSubgraphAroundNode(dfg, getFactor(dfg, :x7x8f1), 1, true, solvable=0)
410+
@test symdiff([:x7, :x8, :x7x8f1], [ls(dfgSubgraph)..., lsf(dfgSubgraph)...]) == []
411+
# Filter - always returns the node you start at but filters around that.
412+
dfgSubgraph = getSubgraphAroundNode(dfg, getFactor(dfg, :x7x8f1), 1, true, solvable=1)
413+
@test symdiff([:x7x8f1, :x7], [ls(dfgSubgraph)..., lsf(dfgSubgraph)...]) == []
414+
408415
# DFG issue #95 - confirming that getSubgraphAroundNode retains order
409416
# REF: https://github.com/JuliaRobotics/DistributedFactorGraphs.jl/issues/95
410417
for fId in getVariableIds(dfg)

test/interfaceTests.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ end
346346
dfgSubgraph = getSubgraphAroundNode(dfg, verts[1], 2)
347347
# Only returns x1 and x2
348348
@test symdiff([:x1, :x1x2f1, :x2], [ls(dfgSubgraph)..., lsf(dfgSubgraph)...]) == []
349-
# Test include orphan factorsVoid
349+
# Test include orphan factors
350350
dfgSubgraph = getSubgraphAroundNode(dfg, verts[1], 1, true)
351351
@test symdiff([:x1, :x1x2f1], [ls(dfgSubgraph)..., lsf(dfgSubgraph)...]) == []
352352
# Test adding to the dfg
@@ -357,6 +357,13 @@ end
357357
# Only returns x1 and x2
358358
@test symdiff([:x1, :x1x2f1, :x2], [ls(dfgSubgraph)..., lsf(dfgSubgraph)...]) == []
359359

360+
# DFG issue #201 Test include orphan factors with filtering - should only return x7 with solvable=1
361+
dfgSubgraph = getSubgraphAroundNode(dfg, getFactor(dfg, :x7x8f1), 1, true, solvable=0)
362+
@test symdiff([:x7, :x8, :x7x8f1], [ls(dfgSubgraph)..., lsf(dfgSubgraph)...]) == []
363+
# Filter - always returns the node you start at but filters around that.
364+
dfgSubgraph = getSubgraphAroundNode(dfg, getFactor(dfg, :x7x8f1), 1, true, solvable=1)
365+
@test symdiff([:x7x8f1, :x7], [ls(dfgSubgraph)..., lsf(dfgSubgraph)...]) == []
366+
360367
# DFG issue #95 - confirming that getSubgraphAroundNode retains order
361368
# REF: https://github.com/JuliaRobotics/DistributedFactorGraphs.jl/issues/95
362369
for fId in getVariableIds(dfg)

0 commit comments

Comments
 (0)