Skip to content

Commit 4a27abf

Browse files
committed
Draft
* Dot export not to be done and ignored in test * MetaGraphs requires master
1 parent 2928158 commit 4a27abf

File tree

3 files changed

+70
-39
lines changed

3 files changed

+70
-39
lines changed

src/LightGraphsDFG/services/LightGraphsDFG.jl

Lines changed: 64 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ function getVariable(dfg::LightGraphsDFG, variableId::Int64)::DFGVariable
141141
return get_prop(dfg.g, variableId, :variable)
142142
end
143143

144+
function getVariable(g::MetaGraph, variableId::Int64)::DFGVariable
145+
return get_prop(g, variableId, :variable)
146+
end
147+
144148
"""
145149
$(SIGNATURES)
146150
Get a DFGVariable from a DFG using its label.
@@ -166,6 +170,13 @@ function getFactor(dfg::LightGraphsDFG, factorId::Int64)::DFGFactor
166170
return get_prop(dfg.g, factorId, :factor)
167171
end
168172

173+
function getFactor(g::MetaGraph, factorId::Int64)::DFGFactor
174+
# if !(factorId in values(dfg.labelDict))
175+
# error("Factor ID '$(factorId)' does not exist in the factor graph")
176+
# end
177+
return get_prop(g, factorId, :factor)
178+
end
179+
169180
"""
170181
$(SIGNATURES)
171182
Get a DFGFactor from a DFG using its label.
@@ -422,43 +433,54 @@ function ls(dfg::LightGraphsDFG, label::Symbol)::Vector{Symbol} where T <: DFGNo
422433
return getNeighbors(dfg, label)
423434
end
424435

425-
function _copyIntoGraph!(sourceDFG::LightGraphsDFG, destDFG::LightGraphsDFG, variableFactorLabels::Vector{Symbol}, includeOrphanFactors::Bool=false)::Nothing
426-
# Split into variables and factors
427-
verts = map(id -> sourceDFG.g.vertices[sourceDFG.labelDict[id]], variableFactorLabels)
428-
sourceVariables = filter(n -> n.dfgNode isa DFGVariable, verts)
429-
sourceFactors = filter(n -> n.dfgNode isa DFGFactor, verts)
436+
function _copyIntoGraph!(sourceDFG::LightGraphsDFG, destDFG::LightGraphsDFG, ns::Vector{Int}, includeOrphanFactors::Bool=false)::Nothing
437+
# Split into variables and factors
438+
subgraph = sourceDFG.g[ns]
439+
sourceVariableIds = collect(filter_vertices(subgraph, :variable))
440+
sourceFactorIds = collect(filter_vertices(subgraph, :factor))
441+
# or copy out of sourceDFG.
442+
# sourceFactorIds = intersect(ns, collect(filter_vertices(sourceDFG.g, :factor)))
443+
444+
#get factor and variable nodes
445+
variables = map(vId->getVariable(subgraph, vId), sourceVariableIds)
446+
factors = map(fId->getFactor(subgraph, fId), sourceFactorIds)
447+
448+
# Now we have to add all variables first,
449+
for v in variables
450+
if !haskey(destDFG.labelDict, v.label)
451+
addVariable!(destDFG, deepcopy(v))
452+
end
453+
end
430454

431-
# Now we have to add all variables first,
432-
for variable in sourceVariables
433-
if !haskey(destDFG.labelDict, variable.dfgNode.label)
434-
addVariable!(destDFG, deepcopy(variable.dfgNode))
435-
end
436-
end
437455
# And then all factors to the destDFG.
438-
for factor in sourceFactors
439-
if !haskey(destDFG.labelDict, factor.dfgNode.label)
456+
for f in factors
457+
if !haskey(destDFG.labelDict, f.label)
440458
# Get the original factor variables (we need them to create it)
441-
variables = in_neighbors(factor, sourceDFG.g)
459+
# variables = in_neighbors(factor, sourceDFG.g)
460+
# variables = getNeighbors(sourceDFG, f)
461+
varIds = LightGraphs.neighbors(sourceDFG.g, sourceDFG.g[f.label,:label])
462+
variables = map(vId->getVariable(sourceDFG.g, vId), varIds)
463+
442464
# Find the labels and associated variables in our new subgraph
443465
factVariables = DFGVariable[]
444-
for variable in variables
445-
if haskey(destDFG.labelDict, variable.dfgNode.label)
446-
push!(factVariables, getVariable(destDFG, variable.dfgNode.label))
466+
for v in variables
467+
if haskey(destDFG.labelDict, v.label)
468+
push!(factVariables, getVariable(destDFG, v.label))
447469
#otherwise ignore
448470
end
449471
end
450472

451473
# Only if we have all of them should we add it (otherwise strange things may happen on evaluation)
452474
if includeOrphanFactors || length(factVariables) == length(variables)
453-
addFactor!(destDFG, factVariables, deepcopy(factor.dfgNode))
475+
addFactor!(destDFG, factVariables, deepcopy(f))
454476
end
455477
end
456478
end
479+
@show "Using ME!"
457480
return nothing
458481
end
459482

460483

461-
#TODO TODO TODO TODO TODO TODO TODO TODO
462484
"""
463485
$(SIGNATURES)
464486
Retrieve a deep subgraph copy around a given variable or factor.
@@ -472,25 +494,29 @@ function getSubgraphAroundNode(dfg::LightGraphsDFG{P}, node::T, distance::Int64=
472494
end
473495

474496
# Build a list of all unique neighbors inside 'distance'
475-
neighborList = Dict{Symbol, Any}()
476-
push!(neighborList, node.label => dfg.g.vertices[dfg.labelDict[node.label]])
477-
curList = Dict{Symbol, Any}(node.label => dfg.g.vertices[dfg.labelDict[node.label]])
478-
for dist in 1:distance
479-
newNeighbors = Dict{Symbol, Any}()
480-
for (key, node) in curList
481-
neighbors = in_neighbors(node, dfg.g) #Don't use out_neighbors! It enforces directiveness even if we don't want it
482-
for neighbor in neighbors
483-
if !haskey(neighborList, neighbor.dfgNode.label)
484-
push!(neighborList, neighbor.dfgNode.label => neighbor)
485-
push!(newNeighbors, neighbor.dfgNode.label => neighbor)
486-
end
487-
end
488-
end
489-
curList = newNeighbors
490-
end
491-
492-
# Copy the section of graph we want
493-
_copyIntoGraph!(dfg, addToDFG, collect(keys(neighborList)), includeOrphanFactors)
497+
# neighborList = Dict{Symbol, Any}()
498+
# push!(neighborList, node.label => dfg.g.vertices[dfg.labelDict[node.label]])
499+
# curList = Dict{Symbol, Any}(node.label => dfg.g.vertices[dfg.labelDict[node.label]])
500+
# for dist in 1:distance
501+
# newNeighbors = Dict{Symbol, Any}()
502+
# for (key, node) in curList
503+
# neighbors = in_neighbors(node, dfg.g) #Don't use out_neighbors! It enforces directiveness even if we don't want it
504+
# for neighbor in neighbors
505+
# if !haskey(neighborList, neighbor.dfgNode.label)
506+
# push!(neighborList, neighbor.dfgNode.label => neighbor)
507+
# push!(newNeighbors, neighbor.dfgNode.label => neighbor)
508+
# end
509+
# end
510+
# end
511+
# curList = newNeighbors
512+
# end
513+
# Copy the section of graph we want
514+
# _copyIntoGraph!(dfg, addToDFG, collect(keys(neighborList)), includeOrphanFactors)
515+
516+
ns = neighborhood(dfg.g, dfg.g[node.label,:label], distance)
517+
# _copyIntoGraph!(dfg, addToDFG, map(n->get_prop(dfg.g, n, :label), ns), includeOrphanFactors)
518+
#TODO overwrite copyIntoGraph for speed with Int
519+
_copyIntoGraph!(dfg, addToDFG, ns, includeOrphanFactors)
494520
return addToDFG
495521
end
496522

test/interfaceTests.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ map(n -> addFactor!(dfg, [verts[n], verts[n+1]], DFGFactor{Int, :Symbol}(Symbol(
114114
@test getNeighbors(dfg, :x1x2f1) == ls(dfg, :x1x2f1)
115115
end
116116

117-
#TODO Subgraphs for LightGraphsDFG
118117
@testset "Getting Subgraphs" begin
119118
# Subgraphs
120119
dfgSubgraph = getSubgraphAroundNode(dfg, verts[1], 2)

test/runtests.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ using Test
22
using DataFrames
33
using DistributedFactorGraphs
44

5+
#FIXME REMOVE when MetaGraphs tags new release
6+
using Pkg
7+
MG_master = PackageSpec(name="MetaGraphs", rev="master")
8+
Pkg.add(MG_master)
9+
#____ REMOVE when MetaGraphs tags new release
10+
511
# Test each interface
612
apis = [GraphsDFG, LightGraphsDFG]
713
# apis = [graphsDFG, cgDFG]

0 commit comments

Comments
 (0)