Skip to content

Commit 64c17b5

Browse files
committed
More filter standardization and cleanup
1 parent 51906c2 commit 64c17b5

11 files changed

Lines changed: 289 additions & 270 deletions

File tree

src/Deprecated.jl

Lines changed: 78 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -55,69 +55,31 @@ function mergeGraphVariableData!(args...)
5555
)
5656
end
5757

58-
#NOTE List types funcction do not fit verb noun and will be deprecated.
59-
# should return types
60-
6158
# """
62-
# $SIGNATURES
59+
# $(SIGNATURES)
60+
# Gives back all factor labels that fit the bill:
61+
# lsWho(dfg, :Pose3)
6362

64-
# Return `Vector{Symbol}` of all unique variable types in factor graph.
65-
# """
66-
function lsTypes(dfg::AbstractDFG)
67-
vars = getVariables(dfg)
68-
alltypes = Set{Symbol}()
69-
for v in vars
70-
varType = Symbol(typeof(getVariableType(v)))
71-
push!(alltypes, varType)
72-
end
73-
return collect(alltypes)
74-
end
63+
# Notes
64+
# - Returns `Vector{Symbol}`
7565

76-
# """
77-
# $SIGNATURES
66+
# Dev Notes
67+
# - Cloud versions will benefit from less data transfer
68+
# - `ls(dfg::C, ::T) where {C <: CloudDFG, T <: ..}`
69+
70+
# Related
7871

79-
# Return `::Dict{Symbol, Vector{Symbol}}` of all unique variable types with labels in a factor graph.
72+
# ls, lsf, lsfPriors
8073
# """
81-
function lsTypesDict(dfg::AbstractDFG)
74+
function lsWho(dfg::AbstractDFG, type::Symbol)
75+
Base.depwarn("lsWho(dfg, type) is deprecated, use ls(dfg, type) instead.", :lsWho)
8276
vars = getVariables(dfg)
83-
alltypes = Dict{Symbol, Vector{Symbol}}()
77+
labels = Symbol[]
8478
for v in vars
85-
varType = Symbol(typeof(getVariableType(v)))
86-
d = get!(alltypes, varType, Symbol[])
87-
push!(d, v.label)
88-
end
89-
return alltypes
90-
end
91-
92-
# """
93-
# $SIGNATURES
94-
95-
# Return `Vector{Symbol}` of all unique factor types in factor graph.
96-
# """
97-
function lsfTypes(dfg::AbstractDFG)
98-
facs = getFactors(dfg)
99-
alltypes = Set{Symbol}()
100-
for f in facs
101-
facType = typeof(getFactorType(f)) |> nameof
102-
push!(alltypes, facType)
103-
end
104-
return collect(alltypes)
105-
end
106-
107-
# """
108-
# $SIGNATURES
109-
110-
# Return `::Dict{Symbol, Vector{Symbol}}` of all unique factors types with labels in a factor graph.
111-
# """
112-
function lsfTypesDict(dfg::AbstractDFG)
113-
facs = getFactors(dfg)
114-
alltypes = Dict{Symbol, Vector{Symbol}}()
115-
for f in facs
116-
facType = typeof(getFactorType(f)) |> nameof
117-
d = get!(alltypes, facType, Symbol[])
118-
push!(d, f.label)
79+
varType = typeof(getVariableType(v)) |> nameof
80+
varType == type && push!(labels, v.label)
11981
end
120-
return alltypes
82+
return labels
12183
end
12284

12385
# solvekey is deprecated and sync!/copyto! is the better verb.
@@ -175,6 +137,67 @@ end
175137
export DFGVariable
176138
const DFGVariable = VariableCompute
177139

140+
export listSolveKeys, listSupersolves
141+
# """
142+
# $TYPEDSIGNATURES
143+
# List all the solvekeys used amongst all variables in the distributed factor graph object.
144+
145+
# Related
146+
147+
# [`listSolveKeys`](@ref), [`getSolverDataDict`](@ref), [`listVariables`](@ref)
148+
# """
149+
function listSolveKeys(
150+
variable::VariableCompute,
151+
filterSolveKeys::Union{Regex, Nothing} = nothing,
152+
skeys = Set{Symbol}(),
153+
)
154+
Base.depwarn(
155+
"listSolveKeys is deprecated, use listVariableStates instead.",
156+
:listSolveKeys,
157+
)
158+
#
159+
for ky in keys(getSolverDataDict(variable))
160+
push!(skeys, ky)
161+
end
162+
163+
#filter the solveKey set with filterSolveKeys regex
164+
!isnothing(filterSolveKeys) &&
165+
return filter!(k -> occursin(filterSolveKeys, string(k)), skeys)
166+
return skeys
167+
end
168+
169+
function listSolveKeys(
170+
dfg::AbstractDFG,
171+
lbl::Symbol,
172+
filterSolveKeys::Union{Regex, Nothing} = nothing,
173+
skeys = Set{Symbol}(),
174+
)
175+
return listSolveKeys(getVariable(dfg, lbl), filterSolveKeys, skeys)
176+
end
177+
#
178+
179+
function listSolveKeys(
180+
dfg::AbstractDFG,
181+
filterVariables::Union{Type{<:VariableStateType}, Regex, Nothing} = nothing;
182+
filterSolveKeys::Union{Regex, Nothing} = nothing,
183+
tags::Vector{Symbol} = Symbol[],
184+
solvable::Int = 0,
185+
)
186+
#
187+
skeys = Set{Symbol}()
188+
varList = listVariables(dfg, filterVariables; tags = tags, solvable = solvable)
189+
for vs in varList #, ky in keys(getSolverDataDict(getVariable(dfg, vs)))
190+
listSolveKeys(dfg, vs, filterSolveKeys, skeys)
191+
end
192+
193+
# done inside the loop
194+
# #filter the solveKey set with filterSolveKeys regex
195+
# !isnothing(filterSolveKeys) && return filter!(k -> occursin(filterSolveKeys, string(k)), skeys)
196+
197+
return skeys
198+
end
199+
const listSupersolves = listSolveKeys
200+
178201
## ================================================================================
179202
## Deprecated in v0.27
180203
##=================================================================================

src/DistributedFactorGraphs.jl

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ using RecursiveArrayTools: ArrayPartition
4949
export ArrayPartition
5050
using StaticArrays
5151

52-
import Base: getindex
53-
5452
using InteractiveUtils: subtypes
5553

5654
##==============================================================================
@@ -198,8 +196,6 @@ export exists,
198196
deleteFactor!,
199197
listVariables,
200198
listFactors,
201-
listSolveKeys,
202-
listSupersolves,
203199
getVariables,
204200
getFactors,
205201
isVariable,
@@ -341,7 +337,6 @@ export natural_lt, sortDFG
341337

342338
## List
343339
export ls, lsf, ls2
344-
export lsWho
345340
export isPrior, lsfPriors
346341
export hasTags, hasTagsNeighbors
347342

@@ -355,7 +350,8 @@ export @defVariable
355350

356351
# File import and export
357352
export saveDFG, loadDFG!, loadDFG
358-
export toDot, toDotFile
353+
354+
# export toDot, toDotFile
359355

360356
# shortest path
361357
export findShortestPathDijkstra

src/GraphsDFG/FactorGraphs/BiMaps.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# import Base: getindex, setindex!, firstindex, lastindex, iterate, keys, isempty
21
struct BiDictMap{T <: Integer}
32
int_sym::Dict{T, Symbol}
43
sym_int::Dict{Symbol, T}

src/GraphsDFG/FactorGraphs/FactorGraphs.jl

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ using Graphs
33

44
using OrderedCollections
55

6-
import Base: eltype, show, ==, Pair, Tuple, copy, length, size, issubset, zero, getindex
7-
# import Random:
8-
# randstring, seed!
6+
import Base
97

108
import Graphs:
119
AbstractGraph,
@@ -39,15 +37,7 @@ import Graphs.SimpleGraphs:
3937
AbstractSimpleGraph, SimpleGraph, SimpleDiGraph, SimpleEdge, fadj, badj
4038

4139
export FactorGraph
42-
# addVariable!,
43-
# addFactor!,
44-
# GraphsBayesGraph,
45-
# filter_edges,
46-
# filter_vertices,
47-
# reverse
48-
49-
# import DistributedFactorGraphs: AbstractGraphNode
50-
# const AbstractNodeType = AbstractGraphNode
40+
5141
using DistributedFactorGraphs: AbstractGraphNode
5242
import DistributedFactorGraphs: AbstractGraphVariable, AbstractGraphFactor
5343
const AbstractVariableType = AbstractGraphVariable
@@ -83,15 +73,15 @@ end
8373
FactorGraph() = FactorGraph{Int, AbstractVariableType, AbstractFactorType}()
8474
# FactorGraph{V,F}() where {V <: AbstractVariableType, F <: AbstractFactorType} = FactorGraph{Int, V, F}()
8575

86-
function show(io::IO, ::MIME"text/plain", g::FactorGraph)
76+
function Base.show(io::IO, ::MIME"text/plain", g::FactorGraph)
8777
dir = is_directed(g) ? "directed" : "undirected"
8878
return print(io, "{$(nv(g)), $(ne(g))} $dir $(eltype(g)) $(typeof(g))")
8979
end
9080

9181
@inline fadj(g::FactorGraph, x...) = fadj(g.graph, x...)
9282
@inline badj(g::FactorGraph, x...) = badj(g.graph, x...)
9383

94-
eltype(g::FactorGraph) = eltype(g.graph)
84+
Base.eltype(g::FactorGraph) = eltype(g.graph)
9585
edgetype(g::FactorGraph) = edgetype(g.graph)
9686
nv(g::FactorGraph) = nv(g.graph)
9787
vertices(g::FactorGraph) = vertices(g.graph)
@@ -109,7 +99,7 @@ is_directed(::Type{FactorGraph}) = false
10999
is_directed(::Type{FactorGraph{T, V, F}}) where {T, V, F} = false
110100
is_directed(g::FactorGraph) = false
111101

112-
zero(g::FactorGraph{T, V, F}) where {T, V, F} = FactorGraph{T, V, F}(0, 0)
102+
Base.zero(g::FactorGraph{T, V, F}) where {T, V, F} = FactorGraph{T, V, F}(0, 0)
113103

114104
# TODO issubset(g::T, h::T) where T <: FactorGraph = issubset(g.graph, h.graph)
115105

src/GraphsDFG/services/GraphsDFG.jl

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ function getFactors(
258258
filterDFG!(factors, labelFilter, (String getLabel))
259259
filterDFG!(factors, solvableFilter, getSolvable)
260260
filterDFG!(factors, tagsFilter, getTags)
261-
filterDFG!(factors, typeFilter, getFactorType)
261+
filterDFG!(factors, typeFilter, typeof getFactorType)
262262
return factors
263263
end
264264

@@ -304,17 +304,27 @@ function isConnected(dfg::GraphsDFG)
304304
# return length(Graphs.connected_components(dfg.g)) == 1
305305
end
306306

307+
_isSolvable(dfg::GraphsDFG, label::Symbol, ready::Nothing) = true
308+
307309
function _isSolvable(dfg::GraphsDFG, label::Symbol, ready::Int)
308310
haskey(dfg.g.variables, label) && (return dfg.g.variables[label].solvable >= ready)
309311
haskey(dfg.g.factors, label) && (return dfg.g.factors[label].solvable >= ready)
310312
throw(LabelNotFoundError(label))
311313
end
312314

313-
function listNeighbors(dfg::GraphsDFG, node::AbstractGraphNode; solvable::Int = 0)
315+
function listNeighbors(
316+
dfg::GraphsDFG,
317+
node::AbstractGraphNode;
318+
solvable::Union{Nothing, Int} = nothing,
319+
)
314320
return listNeighbors(dfg, node.label; solvable)
315321
end
316322

317-
function listNeighbors(dfg::GraphsDFG, label::Symbol; solvable::Int = 0)
323+
function listNeighbors(
324+
dfg::GraphsDFG,
325+
label::Symbol;
326+
solvable::Union{Nothing, Int} = nothing,
327+
)
318328
if !(hasVariable(dfg, label) || hasFactor(dfg, label))
319329
throw(LabelNotFoundError(label))
320330
end
@@ -337,7 +347,7 @@ function listNeighborhood(
337347
dfg::GraphsDFG,
338348
variableFactorLabels::Vector{Symbol},
339349
distance::Int;
340-
solvable::Int = 0,
350+
solvable::Union{Nothing, Int} = nothing,
341351
)
342352
# find neighbors at distance to add
343353
nbhood = Int[]
@@ -348,7 +358,8 @@ function listNeighborhood(
348358

349359
allvarfacs = [dfg.g.labels[id] for id in nbhood]
350360

351-
solvable != 0 && filter!(nlbl -> (getSolvable(dfg, nlbl) >= solvable), allvarfacs)
361+
!isnothing(solvable) &&
362+
filter!(nlbl -> (getSolvable(dfg, nlbl) >= solvable), allvarfacs)
352363

353364
return allvarfacs
354365
end
@@ -365,9 +376,9 @@ end
365376
# Biadjacency Matrix https://en.wikipedia.org/wiki/Adjacency_matrix#Of_a_bipartite_graph
366377
function getBiadjacencyMatrix(
367378
dfg::GraphsDFG;
368-
solvable::Int = 0,
369-
varLabels = listVariables(dfg; solvable = solvable),
370-
factLabels = listFactors(dfg; solvable = solvable),
379+
solvable::Union{Nothing, Int} = nothing,
380+
varLabels = listVariables(dfg; solvable),
381+
factLabels = listFactors(dfg; solvable),
371382
)
372383
varIndex = [dfg.g.labels[s] for s in varLabels]
373384
factIndex = [dfg.g.labels[s] for s in factLabels]
@@ -443,7 +454,7 @@ function findShortestPathDijkstra(
443454
tagsFactors::Vector{Symbol} = Symbol[],
444455
typeVariables::Union{Nothing, <:AbstractVector} = nothing,
445456
typeFactors::Union{Nothing, <:AbstractVector} = nothing,
446-
solvable::Int = 0,
457+
solvable::Union{Nothing, Int} = nothing,
447458
initialized::Union{Nothing, Bool} = nothing,
448459
)
449460
#
@@ -458,14 +469,14 @@ function findShortestPathDijkstra(
458469

459470
#
460471
duplicate =
461-
regexVariables !== nothing ||
462-
regexFactors !== nothing ||
463-
0 < length(tagsVariables) ||
464-
0 < length(tagsFactors) ||
465-
typeVariables !== nothing ||
466-
typeFactors !== nothing ||
467-
initialized !== nothing ||
468-
solvable != 0
472+
!isnothing(regexVariables) ||
473+
!isnothing(regexFactors) ||
474+
!isempty(tagsVariables) ||
475+
!isempty(tagsFactors) ||
476+
!isnothing(typeVariables) ||
477+
!isnothing(typeFactors) ||
478+
!isnothing(initialized) ||
479+
!isnothing(solvable)
469480
#
470481
dfg_ = if duplicate
471482
# use copy if filter is being applied

0 commit comments

Comments
 (0)