-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAbstractDFG.jl
More file actions
1607 lines (1392 loc) · 46.8 KB
/
AbstractDFG.jl
File metadata and controls
1607 lines (1392 loc) · 46.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
##==============================================================================
## AbstractDFG
##==============================================================================
##------------------------------------------------------------------------------
## Broadcasting
##------------------------------------------------------------------------------
# to allow stuff like `getFactorType.(dfg, [:x1x2f1;:x10l3f2])`
# https://docs.julialang.org/en/v1/manual/interfaces/#
Base.Broadcast.broadcastable(dfg::AbstractDFG) = Ref(dfg)
##==============================================================================
## Interface for an AbstractDFG
##==============================================================================
# TODO update to remove URS
# Standard recommended fields to implement for AbstractDFG
# - `description::String`
# - `userLabel::String`
# - `robotLabel::String`
# - `sessionLabel::String`
# - `userData::Dict{Symbol, String}`
# - `robotData::Dict{Symbol, String}`
# - `sessionData::Dict{Symbol, String}`
# - `solverParams::T<:AbstractParams`
# - `addHistory::Vector{Symbol}`
# - `blobStores::Dict{Symbol, AbstractBlobstore}`
# AbstractDFG Accessors
##------------------------------------------------------------------------------
## Getters
##------------------------------------------------------------------------------
"""
$(SIGNATURES)
Get the id of the node.
"""
getId(node) = node.id
"""
$(SIGNATURES)
Get the label of the node.
"""
getLabel(node) = node.label
"""
$SIGNATURES
Get the metadata of the node.
"""
getMetadata(node) = node.metadata
"""
$(SIGNATURES)
Convenience function to get all the metadata of a DFG
"""
function getDFGInfo(dfg::AbstractDFG)
return (
description = getDescription(dfg),
agentLabel = getAgentLabel(dfg),
graphLabel = getGraphLabel(dfg),
agentMetadata = getAgentMetadata(dfg),
graphMetadata = getGraphMetadata(dfg),
solverParams = getSolverParams(dfg),
)
end
"""
$(SIGNATURES)
"""
getAgent(dfg::AbstractDFG) = dfg.agent
"""
$(SIGNATURES)
"""
function getGraph end
"""
$(SIGNATURES)
"""
getDescription(dfg::AbstractDFG) = dfg.description
"""
$(SIGNATURES)
"""
getAgentLabel(dfg::AbstractDFG) = getLabel(getAgent(dfg))
"""
$(SIGNATURES)
"""
getGraphLabel(dfg::AbstractDFG) = getLabel(dfg)
"""
$(SIGNATURES)
"""
getAddHistory(dfg::AbstractDFG) = dfg.addHistory
"""
$(SIGNATURES)
"""
getSolverParams(dfg::AbstractDFG) = dfg.solverParams
"""
$(SIGNATURES)
Method must be overloaded by the user for Serialization to work. E.g. IncrementalInference uses `CommonConvWrapper <: FactorSolverCache`.
"""
function getFactorOperationalMemoryType(dummy)
return error(
"Please extend your workspace with function getFactorOperationalMemoryType(<:AbstractParams) for your usecase, e.g. IncrementalInference uses `CommonConvWrapper <: FactorSolverCache`",
)
end
function getFactorOperationalMemoryType(dfg::AbstractDFG)
return getFactorOperationalMemoryType(getSolverParams(dfg))
end
"""
$(SIGNATURES)
Method must be overloaded by the user for Serialization to work.
"""
function rebuildFactorCache!(
dfg::AbstractDFG{<:AbstractParams},
factor::AbstractDFGFactor,
neighbors = [],
)
@warn("rebuildFactorCache! is not implemented for $(typeof(dfg))")
return nothing
end
"""
$(SIGNATURES)
Function to get the type of the variables in the DFG.
"""
function getTypeDFGVariables end
"""
$(SIGNATURES)
Function to get the type of the factors in the DFG.
"""
function getTypeDFGFactors end
##------------------------------------------------------------------------------
## Setters
##------------------------------------------------------------------------------
"""
$SIGNATURES
Set the metadata of the node.
"""
function setMetadata!(node, metadata::Dict{Symbol, SmallDataTypes})
# with set old data should be removed, but care is taken to make sure its not the same object
node.metadata !== metadata && empty!(node.metadata)
return merge!(node.metadata, metadata)
end
"""
$(SIGNATURES)
"""
setDescription!(dfg::AbstractDFG, description::String) = dfg.description = description
"""
$(SIGNATURES)
"""
#NOTE a MethodError will be thrown if solverParams type does not mach the one in dfg
# TODO Is it ok or do we want any abstract solver paramters
function setSolverParams!(dfg::AbstractDFG, solverParams::AbstractParams)
return dfg.solverParams = solverParams
end
# Accessors and CRUD for user/robot/session Data
"""
$SIGNATURES
Get the metadata from the agent in the AbstractDFG.
"""
getAgentMetadata(dfg::AbstractDFG) = getMetadata(getAgent(dfg))
"""
$SIGNATURES
Set the metadata of the agent in the AbstractDFG.
"""
function setAgentMetadata!(dfg::AbstractDFG, data::Dict{Symbol, SmallDataTypes})
agent = getAgent(dfg)
return setMetadata!(agent, data)
end
"""
$SIGNATURES
Get the metadata from the factorgraph in the AbstractDFG.
"""
getGraphMetadata(dfg::AbstractDFG) = getMetadata(dfg)
"""
$SIGNATURES
Set the metadata of the factorgraph in the AbstractDFG.
"""
function setGraphMetadata!(dfg::AbstractDFG, data::Dict{Symbol, SmallDataTypes})
return setMetadata!(dfg, data)
end
##==============================================================================
## Agent/Graph Data CRUD
##==============================================================================
#TODO maybe only support get and set?
#NOTE with API standardization this should become something like:
getAgentMetadata(dfg::AbstractDFG, key::Symbol) = getAgentMetadata(dfg)[key]
getGraphMetadata(dfg::AbstractDFG, key::Symbol) = getGraphMetadata(dfg)[key]
function updateAgentMetadata!(dfg::AbstractDFG, pair::Pair{Symbol, String})
return push!(dfg.agent.metadata, pair)
end
function updateGraphMetadata!(dfg::AbstractDFG, pair::Pair{Symbol, String})
return push!(dfg.graphMetadata, pair)
end
function deleteAgentMetadata!(dfg::AbstractDFG, key::Symbol)
pop!(dfg.agent.metadata, key)
return 1
end
function deleteGraphMetadata!(dfg::AbstractDFG, key::Symbol)
pop!(dfg.graphMetadata, key)
return 1
end
emptyAgentMetadata!(dfg::AbstractDFG) = empty!(dfg.agent.metadata)
emptyGraphMetadata!(dfg::AbstractDFG) = empty!(dfg.graphMetadata)
#TODO add__Data!?
##==============================================================================
## Agent/Graph/Model Blob Entries CRUD
##==============================================================================
function getGraphBlobentry end
function getGraphBlobentries end
function addGraphBlobentry! end
function addGraphBlobentries! end
function mergeGraphBlobentry! end
function deleteGraphBlobentry! end
function getAgentBlobentry end
function getAgentBlobentries end
function addAgentBlobentry! end
function addAgentBlobentries! end
function mergeAgentBlobentry! end
function deleteAgentBlobentry! end
function getModelBlobentry end
function getModelBlobentries end
function addModelBlobentry! end
function addModelBlobentries! end
function updateModelBlobentry! end
function deleteModelBlobentry! end
function listGraphBlobentries end
function listAgentBlobentries end
function listModelBlobentries end
##==============================================================================
## AbstractBlobstore CRUD
##==============================================================================
# AbstractBlobstore should have label or overwrite getLabel
getBlobstores(dfg::AbstractDFG) = dfg.blobStores
getBlobstore(dfg::AbstractDFG, key::Symbol) = dfg.blobStores[key]
function addBlobstore!(dfg::AbstractDFG, bs::AbstractBlobstore)
return push!(dfg.blobStores, getLabel(bs) => bs)
end
function updateBlobstore!(dfg::AbstractDFG, bs::AbstractBlobstore)
return push!(dfg.blobStores, getLabel(bs) => bs)
end
function deleteBlobstore!(dfg::AbstractDFG, key::Symbol)
pop!(dfg.blobStores, key)
return 1
end
emptyBlobstore!(dfg::AbstractDFG) = empty!(dfg.blobStores)
listBlobstores(dfg::AbstractDFG) = collect(keys(dfg.blobStores))
##==============================================================================
## CRUD Interfaces
##==============================================================================
##------------------------------------------------------------------------------
## Variable And Factor CRUD
##------------------------------------------------------------------------------
"""
$(SIGNATURES)
True if the variable or factor exists in the graph.
"""
function exists(dfg::AbstractDFG, node::DFGNode)
return error("exists not implemented for $(typeof(dfg))")
end
function exists(dfg::AbstractDFG, label::Symbol)
return error("exists not implemented for $(typeof(dfg))")
end
"""
$(SIGNATURES)
Add a VariableCompute to a DFG.
"""
function addVariable!(
dfg::G,
variable::V,
) where {G <: AbstractDFG, V <: AbstractDFGVariable}
return error("addVariable! not implemented for $(typeof(dfg))")
end
"""
$(SIGNATURES)
Add a Vector{VariableCompute} to a DFG.
"""
function addVariables!(dfg::AbstractDFG, variables::Vector{<:AbstractDFGVariable})
return asyncmap(variables) do v
return addVariable!(dfg, v)
end
end
"""
$(SIGNATURES)
Add a FactorCompute to a DFG.
"""
function addFactor!(dfg::AbstractDFG, factor::F) where {F <: AbstractDFGFactor}
return error("addFactor! not implemented for $(typeof(dfg))(dfg, factor)")
end
"""
$(SIGNATURES)
Add a Vector{FactorCompute} to a DFG.
"""
function addFactors!(dfg::AbstractDFG, factors::Vector{<:AbstractDFGFactor})
return asyncmap(factors) do f
return addFactor!(dfg, f)
end
end
"""
$(SIGNATURES)
Get a VariableCompute from a DFG using its label.
"""
function getVariable(dfg::G, label::Union{Symbol, String}) where {G <: AbstractDFG}
return error("getVariable not implemented for $(typeof(dfg))")
end
"""
$(SIGNATURES)
Get a VariableSummary from a DFG.
"""
function getVariableSummary end
"""
$(SIGNATURES)
Get the variables from a DFG as a Vector{VariableSummary}.
"""
function getVariablesSummary end
"""
$(SIGNATURES)
Get a VariableSkeleton from a DFG.
"""
function getVariableSkeleton end
"""
$(SIGNATURES)
Get the variables from a DFG as a Vector{VariableSkeleton}.
"""
function getVariablesSkeleton end
"""
$(SIGNATURES)
Get a FactorCompute from a DFG using its label.
"""
function getFactor(dfg::G, label::Union{Symbol, String}) where {G <: AbstractDFG}
return error("getFactor not implemented for $(typeof(dfg))")
end
"""
$(SIGNATURES)
Get the skeleton factors from a DFG as a Vector{FactorSkeleton}.
"""
function getFactorsSkeleton end
function Base.getindex(dfg::AbstractDFG, lbl::Union{Symbol, String})
if isVariable(dfg, lbl)
getVariable(dfg, lbl)
elseif isFactor(dfg, lbl)
getFactor(dfg, lbl)
else
error("Cannot find $lbl in this $(typeof(dfg))")
end
end
"""
$(SIGNATURES)
Merge a variable into the DFG. If a variable with the same label exists, it will be overwritten;
otherwise, the variable will be added to the graph.
"""
function mergeVariable!(dfg::AbstractDFG, variable::AbstractDFGVariable)
return error("mergeVariable! not implemented for $(typeof(dfg))")
end
"""
$(SIGNATURES)
Merge a factor into the DFG. If a factor with the same label exists, it will be overwritten;
otherwise, the factor will be added to the graph.
"""
function mergeFactor!(dfg::AbstractDFG, factor::AbstractDFGFactor)
return error("mergeFactor! not implemented for $(typeof(dfg))")
end
"""
$(SIGNATURES)
Delete a VariableCompute from the DFG using its label.
"""
function deleteVariable!(dfg::AbstractDFG, label::Symbol)
return error("deleteVariable! not implemented for $(typeof(dfg))")
end
"""
$(SIGNATURES)
Delete a FactorCompute from the DFG using its label.
"""
function deleteFactor!(dfg::AbstractDFG, label::Symbol)
return error("deleteFactor not implemented for $(typeof(dfg))")
end
"""
$(SIGNATURES)
List the DFGVariables in the DFG.
Optionally specify a label regular expression to retrieves a subset of the variables.
Tags is a list of any tags that a node must have (at least one match).
"""
function getVariables(
dfg::G,
regexFilter::Union{Nothing, Regex} = nothing;
tags::Vector{Symbol} = Symbol[],
solvable::Int = 0,
) where {G <: AbstractDFG}
return error("getVariables not implemented for $(typeof(dfg))")
end
function getVariables(dfg::AbstractDFG, labels::Vector{Symbol})
return map(label -> getVariable(dfg, label), labels)
end
"""
$(SIGNATURES)
List the DFGFactors in the DFG.
Optionally specify a label regular expression to retrieves a subset of the factors.
"""
function getFactors(
dfg::G,
regexFilter::Union{Nothing, Regex} = nothing;
tags::Vector{Symbol} = Symbol[],
solvable::Int = 0,
) where {G <: AbstractDFG}
return error("getFactors not implemented for $(typeof(dfg))")
end
function getFactors(dfg::AbstractDFG, labels::Vector{Symbol})
return map(label -> getFactor(dfg, label), labels)
end
##------------------------------------------------------------------------------
## Checking Types
##------------------------------------------------------------------------------
"""
$SIGNATURES
Return whether `sym::Symbol` represents a variable vertex in the graph DFG.
Checks whether it both exists in the graph and is a variable.
(If you rather want a quick for type, just do node isa VariableCompute)
"""
function isVariable(dfg::G, sym::Symbol) where {G <: AbstractDFG}
return error("isVariable not implemented for $(typeof(dfg))")
end
"""
$SIGNATURES
Return whether `sym::Symbol` represents a factor vertex in the graph DFG.
Checks whether it both exists in the graph and is a factor.
(If you rather want a quicker for type, just do node isa FactorCompute)
"""
function isFactor(dfg::G, sym::Symbol) where {G <: AbstractDFG}
return error("isFactor not implemented for $(typeof(dfg))")
end
##------------------------------------------------------------------------------
## Neighbors
##------------------------------------------------------------------------------
"""
$(SIGNATURES)
Checks if the graph is fully connected, returns true if so.
"""
function isConnected(dfg::AbstractDFG)
return error("isConnected not implemented for $(typeof(dfg))")
end
"""
$(SIGNATURES)
Retrieve a list of labels of the immediate neighbors around a given variable or factor specified by its label.
"""
function listNeighbors(dfg::AbstractDFG, label::Symbol; solvable::Int = 0)
return error("listNeighbors not implemented for $(typeof(dfg))")
end
##------------------------------------------------------------------------------
## copy and duplication
##------------------------------------------------------------------------------
#TODO use copy functions currently in attic
"""
$(SIGNATURES)
Gets an empty and unique DFG derived from an existing DFG.
"""
function _getDuplicatedEmptyDFG(dfg::AbstractDFG)
return error("_getDuplicatedEmptyDFG not implemented for $(typeof(dfg))")
end
##------------------------------------------------------------------------------
## CRUD Aliases
##------------------------------------------------------------------------------
"""
$(SIGNATURES)
Get a VariableCompute with a specific solver key.
In memory types still return a reference, other types returns a variable with only solveKey.
"""
function getVariable(dfg::AbstractDFG, label::Symbol, solveKey::Symbol)
var = getVariable(dfg, label)
if isa(var, VariableCompute) && !haskey(var.solverDataDict, solveKey)
error("Solvekey '$solveKey' does not exists in the variable")
elseif !isa(var, VariableCompute)
@warn "getVariable(dfg, label, solveKey) only supported for type VariableCompute."
end
return var
end
"""
$(SIGNATURES)
Delete a referenced VariableCompute from the DFG.
Notes
- Returns `Tuple{AbstractDFGVariable, Vector{<:AbstractDFGFactor}}`
"""
function deleteVariable!(dfg::AbstractDFG, variable::AbstractDFGVariable)
return deleteVariable!(dfg, variable.label)
end
"""
$(SIGNATURES)
Delete the referened FactorCompute from the DFG.
"""
function deleteFactor!(
dfg::G,
factor::F;
suppressGetFactor::Bool = false,
) where {G <: AbstractDFG, F <: AbstractDFGFactor}
return deleteFactor!(dfg, factor.label; suppressGetFactor = suppressGetFactor)
end
# Alias - bit ridiculous but know it'll come up at some point. Does existential and type check.
function isVariable(dfg::G, node::N) where {G <: AbstractDFG, N <: DFGNode}
return isVariable(dfg, node.label)
end
# Alias - bit ridiculous but know it'll come up at some point. Does existential and type check.
function isFactor(dfg::G, node::N) where {G <: AbstractDFG, N <: DFGNode}
return isFactor(dfg, node.label)
end
##------------------------------------------------------------------------------
## Connectivity Alias
##------------------------------------------------------------------------------
function listNeighbors(dfg::AbstractDFG, node::DFGNode; solvable::Int = 0)
return listNeighbors(dfg, node.label; solvable)
end
##==============================================================================
## Listing and listing aliases
##==============================================================================
##------------------------------------------------------------------------------
## Overwrite in driver for performance
##------------------------------------------------------------------------------
"""
$(SIGNATURES)
Get a list of labels of the DFGVariables in the graph.
Optionally specify a label regular expression to retrieves a subset of the variables.
Tags is a list of any tags that a node must have (at least one match).
Notes
- Returns `::Vector{Symbol}`
Example
```julia
listVariables(dfg, r"l", tags=[:APRILTAG;])
```
See also: [`ls`](@ref)
"""
function listVariables(
dfg::AbstractDFG,
regexFilter::Union{Nothing, Regex} = nothing;
tags::Vector{Symbol} = Symbol[],
solvable::Int = 0,
)
#
vars = getVariables(dfg, regexFilter; tags = tags, solvable = solvable)
return map(v -> v.label, vars)::Vector{Symbol}
end
# to be consolidated, see #612
function listVariables(
dfg::AbstractDFG,
typeFilter::Type{<:InferenceVariable};
tags::Vector{Symbol} = Symbol[],
solvable::Int = 0,
)
#
retlist::Vector{Symbol} = ls(dfg, typeFilter)
if 0 < length(tags) || solvable != 0
return intersect(retlist, ls(dfg; tags = tags, solvable = solvable))
else
return retlist
end
end
"""
$(SIGNATURES)
Get a list of the labels of the DFGFactors in the DFG.
Optionally specify a label regular expression to retrieves a subset of the factors.
"""
function listFactors(
dfg::AbstractDFG,
regexFilter::Union{Nothing, Regex} = nothing;
tags::Vector{Symbol} = Symbol[],
solvable::Int = 0,
)
return map(
f -> f.label,
getFactors(dfg, regexFilter; tags = tags, solvable = solvable),
)::Vector{Symbol}
end
"""
$TYPEDSIGNATURES
List all the solvekeys used amongst all variables in the distributed factor graph object.
Related
[`listSolveKeys`](@ref), [`getSolverDataDict`](@ref), [`listVariables`](@ref)
"""
function listSolveKeys(
variable::VariableCompute,
filterSolveKeys::Union{Regex, Nothing} = nothing,
skeys = Set{Symbol}(),
)
#
for ky in keys(getSolverDataDict(variable))
push!(skeys, ky)
end
#filter the solveKey set with filterSolveKeys regex
!isnothing(filterSolveKeys) &&
return filter!(k -> occursin(filterSolveKeys, string(k)), skeys)
return skeys
end
function listSolveKeys(
dfg::AbstractDFG,
lbl::Symbol,
filterSolveKeys::Union{Regex, Nothing} = nothing,
skeys = Set{Symbol}(),
)
return listSolveKeys(getVariable(dfg, lbl), filterSolveKeys, skeys)
end
#
function listSolveKeys(
dfg::AbstractDFG,
filterVariables::Union{Type{<:InferenceVariable}, Regex, Nothing} = nothing;
filterSolveKeys::Union{Regex, Nothing} = nothing,
tags::Vector{Symbol} = Symbol[],
solvable::Int = 0,
)
#
skeys = Set{Symbol}()
varList = listVariables(dfg, filterVariables; tags = tags, solvable = solvable)
for vs in varList #, ky in keys(getSolverDataDict(getVariable(dfg, vs)))
listSolveKeys(dfg, vs, filterSolveKeys, skeys)
end
# done inside the loop
# #filter the solveKey set with filterSolveKeys regex
# !isnothing(filterSolveKeys) && return filter!(k -> occursin(filterSolveKeys, string(k)), skeys)
return skeys
end
const listSupersolves = listSolveKeys
##------------------------------------------------------------------------------
## Aliases and Other filtered lists
##------------------------------------------------------------------------------
## Aliases
##--------
"""
$(SIGNATURES)
List the DFGVariables in the DFG.
Optionally specify a label regular expression to retrieves a subset of the variables.
Tags is a list of any tags that a node must have (at least one match).
Notes:
- Returns `Vector{Symbol}`
"""
function ls(
dfg::G,
regexFilter::Union{Nothing, Regex} = nothing;
tags::Vector{Symbol} = Symbol[],
solvable::Int = 0,
) where {G <: AbstractDFG}
return listVariables(dfg, regexFilter; tags = tags, solvable = solvable)
end
#TODO tags kwarg
"""
$(SIGNATURES)
List the DFGFactors in the DFG.
Optionally specify a label regular expression to retrieves a subset of the factors.
Notes
- Return `Vector{Symbol}`
"""
function lsf(
dfg::G,
regexFilter::Union{Nothing, Regex} = nothing;
tags::Vector{Symbol} = Symbol[],
solvable::Int = 0,
) where {G <: AbstractDFG}
return listFactors(dfg, regexFilter; tags = tags, solvable = solvable)
end
"""
$(SIGNATURES)
Retrieve a list of labels of the immediate neighbors around a given variable or factor.
"""
function ls(dfg::G, node::T; solvable::Int = 0) where {G <: AbstractDFG, T <: DFGNode}
return listNeighbors(dfg, node; solvable = solvable)
end
function ls(dfg::G, label::Symbol; solvable::Int = 0) where {G <: AbstractDFG}
return listNeighbors(dfg, label; solvable = solvable)
end
function lsf(dfg::G, label::Symbol; solvable::Int = 0) where {G <: AbstractDFG}
return listNeighbors(dfg, label; solvable = solvable)
end
## list by types
##--------------
function ls(dfg::G, ::Type{T}) where {G <: AbstractDFG, T <: InferenceVariable}
xx = getVariables(dfg)
mask = getVariableType.(xx) .|> typeof .== T
vxx = view(xx, mask)
return map(x -> x.label, vxx)
end
function ls(dfg::G, ::Type{T}) where {G <: AbstractDFG, T <: AbstractFactorObservation}
xx = getFactors(dfg)
names = typeof.(getFactorType.(xx)) .|> nameof
vxx = view(xx, names .== Symbol(T))
return map(x -> x.label, vxx)
end
"""
$(SIGNATURES)
Lists the factors of a specific type in the factor graph.
Example, list all the Point2Point2 factors in the factor graph `dfg`:
lsf(dfg, Point2Point2)
Notes
- Return `Vector{Symbol}`
"""
function lsf(dfg::G, ::Type{T}) where {G <: AbstractDFG, T <: AbstractFactorObservation}
return ls(dfg, T)
end
"""
$(SIGNATURES)
Helper to return neighbors at distance 2 around a given node.
"""
function ls2(dfg::AbstractDFG, label::Symbol)
l2 = getNeighborhood(dfg, label, 2)
l1 = getNeighborhood(dfg, label, 1)
return setdiff(l2, l1)
end
ls2(dfg::AbstractDFG, v::AbstractDFGVariable) = ls(dfg, getLabel(v))
"""
$SIGNATURES
Return vector of prior factor symbol labels in factor graph `dfg`.
Notes:
- Returns `Vector{Symbol}`
"""
function lsfPriors(dfg::G) where {G <: AbstractDFG}
priors = Symbol[]
fcts = lsf(dfg)
for fc in fcts
if isPrior(dfg, fc)
push!(priors, fc)
end
end
return priors
end
#TODO is this repeated functionality?
"""
$(SIGNATURES)
Gives back all factor labels that fit the bill:
lsWho(dfg, :Pose3)
Notes
- Returns `Vector{Symbol}`
Dev Notes
- Cloud versions will benefit from less data transfer
- `ls(dfg::C, ::T) where {C <: CloudDFG, T <: ..}`
Related
ls, lsf, lsfPriors
"""
function lsWho(dfg::AbstractDFG, type::Symbol)
vars = getVariables(dfg)
labels = Symbol[]
for v in vars
varType = typeof(getVariableType(v)) |> nameof
varType == type && push!(labels, v.label)
end
return labels
end
## list types
##-----------
"""
$SIGNATURES
Return `Vector{Symbol}` of all unique variable types in factor graph.
"""
function lsTypes(dfg::AbstractDFG)
vars = getVariables(dfg)
alltypes = Set{Symbol}()
for v in vars
varType = typeof(getVariableType(v)) |> nameof
push!(alltypes, varType)
end
return collect(alltypes)
end
"""
$SIGNATURES
Return `::Dict{Symbol, Vector{Symbol}}` of all unique variable types with labels in a factor graph.
"""
function lsTypesDict(dfg::AbstractDFG)
vars = getVariables(dfg)
alltypes = Dict{Symbol, Vector{Symbol}}()
for v in vars
varType = typeof(getVariableType(v)) |> nameof
d = get!(alltypes, varType, Symbol[])
push!(d, v.label)
end
return alltypes
end
"""
$SIGNATURES
Return `Vector{Symbol}` of all unique factor types in factor graph.
"""
function lsfTypes(dfg::AbstractDFG)
facs = getFactors(dfg)
alltypes = Set{Symbol}()
for f in facs
facType = typeof(getFactorType(f)) |> nameof
push!(alltypes, facType)
end
return collect(alltypes)
end
"""
$SIGNATURES
Return `::Dict{Symbol, Vector{Symbol}}` of all unique factors types with labels in a factor graph.
"""
function lsfTypesDict(dfg::AbstractDFG)
facs = getFactors(dfg)
alltypes = Dict{Symbol, Vector{Symbol}}()
for f in facs
facType = typeof(getFactorType(f)) |> nameof
d = get!(alltypes, facType, Symbol[])
push!(d, f.label)
end
return alltypes
end
##------------------------------------------------------------------------------
## tags
##------------------------------------------------------------------------------
"""
$SIGNATURES
Determine if the variable or factor neighbors have the `tags:;Vector{Symbol}`, and `matchAll::Bool`.
"""
function hasTags(dfg::AbstractDFG, sym::Symbol, tags::Vector{Symbol}; matchAll::Bool = true)
#
alltags = listTags(dfg, sym)
return length(filter(x -> x in alltags, tags)) >= (matchAll ? length(tags) : 1)
end
"""
$SIGNATURES
Determine if the variable or factor neighbors have the `tags:;Vector{Symbol}`, and `matchAll::Bool`.
"""
function hasTagsNeighbors(
dfg::AbstractDFG,
sym::Symbol,
tags::Vector{Symbol};
matchAll::Bool = true,
)
#
# assume only variables or factors are neighbors
getNeiFnc = isVariable(dfg, sym) ? getFactor : getVariable
alltags = union((ls(dfg, sym) .|> x -> getTags(getNeiFnc(dfg, x)))...)
return length(filter(x -> x in alltags, tags)) >= (matchAll ? length(tags) : 1)
end
##==============================================================================
## Finding
##==============================================================================
# function findClosestTimestamp(setA::Vector{Tuple{DateTime,T}},
# setB::Vector{Tuple{DateTime,S}}) where {S,T}
"""
$SIGNATURES
Find and return the closest timestamp from two sets of Tuples. Also return the minimum delta-time (`::Millisecond`) and how many elements match from the two sets are separated by the minimum delta-time.
"""
function findClosestTimestamp(
setA::Vector{Tuple{ZonedDateTime, T}},
setB::Vector{Tuple{ZonedDateTime, S}},
) where {S, T}
#
# build matrix of delta times, ranges on rows x vars on columns
DT = Array{Millisecond, 2}(undef, length(setA), length(setB))
for i = 1:length(setA), j = 1:length(setB)
DT[i, j] = setB[j][1] - setA[i][1]
end
DT .= abs.(DT)
# absolute time differences
# DTi = (x->x.value).(DT) .|> abs
# find the smallest element
mdt = minimum(DT)
corrs = findall(x -> x == mdt, DT)
# return the closest timestamp, deltaT, number of correspondences
return corrs[1].I, mdt, length(corrs)
end
"""
$SIGNATURES
Find and return nearest variable labels per delta time. Function will filter on `regexFilter`, `tags`, and `solvable`.
Notes
- Returns `Vector{Tuple{Vector{Symbol}, Millisecond}}`
DevNotes:
- TODO `number` should allow returning more than one for k-nearest matches.
- Future versions likely will require some optimization around the internal `getVariable` call.
- Perhaps a dedicated/efficient `getVariableTimestamp` for all DFG flavors.
Related
ls, listVariables, findClosestTimestamp
"""
function findVariableNearTimestamp(
dfg::AbstractDFG,