-
Notifications
You must be signed in to change notification settings - Fork 21
/
TreeDebugTools.jl
802 lines (647 loc) · 24.4 KB
/
TreeDebugTools.jl
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
#
export repeatCSMStep!
export attachCSM!
export filterHistAllToArray, cliqHistFilterTransitions, printCliqSummary
export printHistoryLine, printHistoryLane, printCliqHistorySummary
export printCSMHistoryLogical, printCSMHistorySequential
"""
$SIGNATURES
Get the main factor graph stored in a history object from a particular step in CSM.
Related
getCliqSubgraphFromHistory, printCliqHistorySummary, printCliqSummary
"""
getGraphFromHistory(hist::Vector{<:Tuple}, step::Int) = hist[step][4].dfg
"""
$SIGNATURES
Get the cliq sub graph fragment stored in a history object from a particular step in CSM.
Related
getGraphFromHistory, printCliqHistorySummary, printCliqSummary
"""
getCliqSubgraphFromHistory(hist::Vector{<:Tuple}, step::Int) = hist[step][4].cliqSubFg
getCliqSubgraphFromHistory(tree::AbstractBayesTree, hists::Dict{Symbol, Tuple}, frnt::Symbol, step::Int) = getCliqSubgraphFromHistory(hists[frnt], step)
function printCliqSummary(dfg::G,
tree::AbstractBayesTree,
frs::Symbol,
logger=ConsoleLogger() ) where G <: AbstractDFG
#
printCliqSummary(dfg, getClique(tree, frs), logger)
end
"""
$(SIGNATURES)
Calculate a fresh (single step) approximation to the variable `sym` in clique `cliq` as though during the upward message passing. The full inference algorithm may repeatedly calculate successive apprimxations to the variables based on the structure of the clique, factors, and incoming messages.
Which clique to be used is defined by frontal variable symbols (`cliq` in this case) -- see `getClique(...)` for more details. The `sym` symbol indicates which symbol of this clique to be calculated. **Note** that the `sym` variable must appear in the clique where `cliq` is a frontal variable.
"""
function treeProductUp(fg::AbstractDFG,
tree::AbstractBayesTree,
cliq::Symbol,
sym::Symbol;
N::Int=100,
dbg::Bool=false )
#
cliq = getClique(tree, cliq)
cliqdata = getCliqueData(cliq)
# get all the incoming (upward) messages from the tree cliques
# convert incoming messages to Int indexed format (semi-legacy format)
# upmsgssym = LikelihoodMessage[]
# for cl in childCliqs(tree, cliq)
# msgdict = getUpMsgs(cl)
# dict = Dict{Symbol, TreeBelief}()
# for (dsy, btd) in msgdict.belief
# vari = getVariable(fg, dsy)
# # manis = getVariableType(vari).manifolds
# dict[dsy] = TreeBelief(btd.val, btd.bw, btd.inferdim, getVariableType(vari))
# end
# push!( upmsgssym, LikelihoodMessage(beliefDict=dict) )
# end
upmsgssym = fetchMsgsUpChildren(tree, cliq, TreeBelief)
# perform the actual computation
potprod = nothing
pGM, fulldim = predictbelief(fg, sym, :, N=N, dbg=dbg )
# manis = getVariableType(getVariable(fg, sym)) |> getManifolds
# pGM, potprod, fulldim = cliqGibbs( fg, cliq, sym, upmsgssym, N, dbg, manis )
return pGM, potprod
end
"""
$(SIGNATURES)
Calculate a fresh---single step---approximation to the variable `sym` in clique `cliq` as though during the downward message passing. The full inference algorithm may repeatedly calculate successive apprimxations to the variable based on the structure of variables, factors, and incoming messages to this clique.
Which clique to be used is defined by frontal variable symbols (`cliq` in this case) -- see `getClique(...)` for more details. The `sym` symbol indicates which symbol of this clique to be calculated. **Note** that the `sym` variable must appear in the clique where `cliq` is a frontal variable.
"""
function treeProductDwn(fg::G,
tree::AbstractBayesTree,
cliq::Symbol,
sym::Symbol;
N::Int=100,
dbg::Bool=false ) where G <: AbstractDFG
#
@warn "treeProductDwn might not be working properly at this time. (post DFG v0.6 upgrade maintenance required)"
cliq = getClique(tree, cliq)
cliqdata = getCliqueData(cliq)
# get the local variable id::Int identifier
# vertid = fg.labelDict[sym]
# get all the incoming (upward) messages from the tree cliques
# convert incoming messages to Int indexed format (semi-legacy format)
cl = parentCliq(tree, cliq)
msgdict = getDwnMsgs(cl[1])
dict = Dict{Int, TreeBelief}()
for (dsy, btd) in msgdict
dict[fg.IDs[dsy]] = TreeBelief(btd.val, btd.bw, btd.inferdim, getVariableType(getVariable(fg,sym)) )
end
dwnmsgssym = LikelihoodMessage[LikelihoodMessage(dict);]
# perform the actual computation
potprod = nothing
pGM, fulldim = predictbelief(fg, sym, :, N=N, dbg=dbg)
# pGM, potprod, fulldim = cliqGibbs( fg, cliq, sym, dwnmsgssym, N, dbg ) #vertid
return pGM, potprod, sym, dwnmsgssym
end
"""
$SIGNATURES
Print one specific line of a clique state machine history.
Related:
[`printCliqHistorySequential`](@ref), [`printCliqHistorySummary`](@ref)
"""
function printHistoryLine(fid,
hi::CSMHistoryTuple,
cliqid::AbstractString="",
seq::Int=0)
#
# global sequence number
first = clampBufferString("$seq", 5)
# 5.13
first *= clampBufferString("$cliqid.$(string(hi[2]))", 6)
# time
first *= clampBufferString(string(split(string(hi[1]), 'T')[end]), 14)
# next function
nextfn = split(split(string(hi[3]),'.')[end], '_')[1]
first *= clampBufferString(nextfn*" ", 20, 18)
first *= " | "
first *= clampBufferString(string(getCliqueStatus(hi[4].cliq))*" ", 9, 7)
# parent status
first *= " P "
downRxMessage = getMessageBuffer(hi[4].cliq).downRx
toadd = if !isnothing(downRxMessage)
#TODO maybe don't use tree here
"$(getParent(hi[4].tree, hi[4].cliq)[1].id):$(downRxMessage.status)"
else
" ----"
end
first *= clampBufferString(toadd*" ", 9, 7)
# children status
first = first*"C "
upRxMessages = getMessageBuffer(hi[4].cliq).upRx
# all_child_status = map((k,msg) -> (k,msg.status), pairs(upRxMessages))
if length(upRxMessages) > 0
for (k,msg) in upRxMessages
toadd = string(k)*":"*string(msg.status)*" "
first *= clampBufferString(toadd*" ", 8, 7)
end
else
first *= clampBufferString(" ----", 8)
end
# sibling status # TODO JT removed but kept for future if needed
# first *= "|S| "
# if 0 < length(hi[4].parentCliq)
# frt = (hi[4].parentCliq[1] |> getFrontals)[1]
# childs = getChildren(hi[4].tree, frt)
# # remove current clique to leave only siblings
# filter!(x->x.index!=hi[4].cliq.id.value, childs)
# for ch in childs
# first = first*"$(ch.index)"*string(getCliqueStatus(ch))*" "
# end
# end
println(fid, first)
end
"""
$SIGNATURES
Print a short summary of state machine history for a clique solve.
Related:
getTreeAllFrontalSyms, animateCliqStateMachines, printHistoryLine, printCliqHistorySequential
"""
function printCliqHistorySummary( fid,
hist::Vector{CSMHistoryTuple},
cliqid::AbstractString="")
if length(hist) == 0
@warn "printCliqHistorySummary -- No CSM history found."
end
for hi in hist
printHistoryLine(fid,hi,cliqid)
end
nothing
end
function printCliqHistorySummary( hist::Vector{CSMHistoryTuple},
cliqid::AbstractString="")
#
printCliqHistorySummary(stdout, hist, cliqid)
end
function printCliqHistorySummary( hists::Dict{Int,Vector{CSMHistoryTuple}},
tree::AbstractBayesTree,
sym::Symbol )
#
hist = hists[getClique(tree, sym).id]
printCliqHistorySummary(stdout, hist, string(getClique(tree, sym).id))
end
# TODO maybe Base. already has something like this Union{UnitRange, AbstractVector, etc.}
const CSMRangesT{T} = Union{T,UnitRange{T},<:AbstractVector{T} }
const CSMRanges = CSMRangesT{Int}
# old
# const CSMTupleRangesT{T} = Union{Tuple{T,T},Tuple{T,UnitRange{T}},Tuple{T,AbstractVector{T}},Tuple{UnitRange{T},T},Tuple{UnitRange{T},UnitRange{T}},Tuple{AbstractVector{T},T},Tuple{AbstractVector{T},AbstractVector{T}},Tuple{AbstractVector{T},UnitRange{T}} }
"""
$SIGNATURES
Print a sequential summary lines of clique state machine histories in hists::Dict.
Notes
- Slices are allowed, see examples.
Example
```julia
printCSMHistorySequential(hists)
printCSMHistorySequential(hists, 2=>46)
printCSMHistorySequential(hists, 1=>11:15)
printCSMHistorySequential(hists, [1,4,6]=>11:15)
printCSMHistorySequential(hists, [2=>45:52; 1=>10:15])
```
DevNotes
- TODO perhaps move some of this functionality upstream to FSM
- TODO upgrade to default `whichstep = :=>:` -- i.e.
- add dispatch for `(:) |> typeof == Colon`,
- `5:6=>:`.
- TODO also add a elements between `Tuple{<:CSMRanges, Pair{<:CSMRanges,<:CSMRanges}}` option
- ([1;3], 1=>5:7) which will print all steps from CSM 1 and 3, which occur between 1=>5 and 1=>7.
- TODO maybe also `Dict(5=>5:8, 8=>20:25)`, or `Dict(2:5=>[1;3], 10=>1:5)`.
Related:
printHistoryLine, printCliqHistory
"""
function printCSMHistorySequential( hists::Dict{Int,Vector{CSMHistoryTuple}},
whichsteps::Union{Nothing,Vector{<:Pair{<:CSMRanges,<:CSMRanges}}}=nothing,
fid=stdout )
#
# vectorize all histories in single Array
allhists = Vector{CSMHistoryTuple}()
alltimes = Vector{DateTime}()
allcliqids = Vector{Int}()
for (cid,hist) in hists, hi in hist
push!(allhists, hi)
push!(alltimes, hi[1])
push!(allcliqids, cid)
end
# sort array by timestamp element
pm = sortperm(alltimes)
allhists_ = allhists[pm]
alltimes_ = alltimes[pm]
allcliqids_ = allcliqids[pm]
# print each line of the sorted array with correct cliqid marker
for idx in 1:length(alltimes)
hiln = allhists_[idx]
# show only one line if whichstep is not nothing
inSliceList = whichsteps === nothing
if !inSliceList
for whichstep in whichsteps
inSliceList && break
inSliceList = inSliceList || (allcliqids_[idx] in whichstep[1] && hiln[2] in whichstep[2])
end
end
if inSliceList
printHistoryLine(fid,hiln,string(allcliqids_[idx]), idx)
end
end
nothing
end
function printCSMHistorySequential(hists::Dict{Int,Vector{CSMHistoryTuple}},
whichstep::Pair{<:CSMRanges,<:CSMRanges},
fid=stdout)
#
printCSMHistorySequential(hists,[whichstep;], fid)
end
function printCSMHistorySequential(hists::Dict{Int,Vector{CSMHistoryTuple}},
fid::AbstractString)
#
@info "printCliqHistorySequential -- assuming file request, writing history to $fid"
file = open(fid, "w")
printCSMHistorySequential(hists, nothing, file)
close(file)
nothing
end
"""
$SIGNATURES
Print one line of lanes summarizing all clique state machine histories.
Notes
- hiVec is vector of all cliques (i.e. lanes) to print as one LINE into `fid`
- contains `::Tuple{Int,..}` with global counter (not the default CSM counter)
- Vector of `CSMHistoryTuple`
Related:
printCliqHistoryLogical, printCliqHistoryLine
"""
function printHistoryLane(fid,
linecounter::Union{Int,String},
hiVec::Vector{<:Union{NamedTuple,Tuple}},
seqLookup::NothingUnion{Dict{Pair{Int,Int},Int}}=nothing )
#
## build a string
line = clampBufferString("$linecounter",4)
for counter in 1:length(hiVec)
# lane marker
line *= "| "
if !isassigned(hiVec, counter)
line *= clampBufferString("", 19)
continue
end
hi = hiVec[counter]
# global counter
useCount = seqLookup !== nothing ? seqLookup[(hi[4].cliq.id.value=>hi[2])] : hi[2]
line *= clampBufferString("$(useCount)",4)
# next function
nextfn = split(string(hi[3]),'.')[end]
line *= clampBufferString(nextfn, 10, 9)
# clique status
st = hi[4] isa String ? hi[4] : string(getCliqueStatus(hi[4].cliq))
line *= clampBufferString(st, 5, 4)
end
## print the string
println(fid, line)
end
"""
$SIGNATURES
Print history in swimming lanes, side by side with global sequence counter.
Examples
```julia
printCSMHistoryLogical(hist)
printCSMHistoryLogical(hists, order=[4;3], printLines=2:5)
# or to a IOStream, file, network, etc
fid = open(joinLogPath(fg, "CSMHistCustom.txt"),"w")
printCSMHistoryLogical(hist, fid)
close(fid)
```
DevNotes
- `order` should be flexible like `Sequential` and `<:CSMRanges`.
"""
function printCSMHistoryLogical(hists::Dict{Int,Vector{CSMHistoryTuple}},
fid=stdout;
order::AbstractVector{Int}=sort(collect(keys(hists))),
printLines=1:99999999 )
#
# vectorize all histories in single Array
allhists = Vector{CSMHistoryTuple}()
alltimes = Vector{DateTime}()
allcliqids = Vector{Int}()
# "lanes" (i.e. individual cliques)
numLanes = length(order)
# "lines" (i.e. CSM steps)
maxLines = [0;0]
for (cid,hist) in hists
# find max number of lines to print later
maxLines[2] = length(hist)
maxLines[1] = maximum(maxLines)
for hi in hist
push!(allhists, hi)
push!(alltimes, hi[1])
push!(allcliqids, cid)
end
end
maxLines[1] = minimum([maxLines[1];printLines[end]])
# sort array by timestamp element
pm = sortperm(alltimes)
allhists_ = allhists[pm]
alltimes_ = alltimes[pm]
allcliqids_ = allcliqids[pm]
# first get the global sequence (invert order dict as bridge table)
seqLookup = Dict{Pair{Int,Int},Int}()
for idx in 1:length(alltimes)
hiln = allhists_[idx]
seqLookup[(hiln[4].cliq.id.value => hiln[2])] = idx
end
# print the column titles
titles = Vector{Tuple{String, Int, String, String}}()
for ord in order
csym = 0 < length(hists[ord]) ? getFrontals(hists[ord][1][4].cliq)[1] |> string : ""
csym = clampBufferString("$csym", 9)
push!(titles, ("",ord,csym,clampBufferString("", 10)) )
end
printHistoryLane(fid, "", titles)
print(fid,"----")
for i in 1:numLanes
print(fid,"+--------------------")
end
println(fid,"")
glbSeqCt = 0 # Ref{Int}(0)
## repeat for the maximum number of "lines" (i.e. CSM steps)
for idx in printLines[1]:maxLines[1]
## build each line as vector of "lanes" (i.e. individual cliques)
allLanes = Vector{CSMHistoryTuple}(undef, numLanes)
laIdx = 0
for laId in order
laIdx += 1
# if history data exists for this line (idx) and lane (laId), then build a new lane Tuple
if idx <= length(hists[laId])
# FIXME, swat first element with global counter (not local as stored in hists)
# @show hists[laId][idx][3]
allLanes[laIdx] = hists[laId][idx]
end
end
#$cliqid.$(string(hi[2]))
# glbSeqCt += 1
printHistoryLane(fid, idx, allLanes, seqLookup)
end
end
# print each line of the sorted array with correct cliqid marker
"""
$SIGNATURES
Repeat a solver state machine step -- useful for debugging.
Notes
- use in combination with `solveTree!(fg, recordcliqs=[:0; :x7; ...])` -- i.e. by clique frontals as identifier
- to record everything, one can do: `recordcliqs=ls(fg)`.
- `duplicate` avoids changing history or prime data in `hists`.
- Replaces old API `sandboxCliqResolveStep`
- Consider using this in combination with tools like [Revise.jl](https://github.com/timholy/Revise.jl)
- On by default in VSCode.
- Internally sets `csmc.enableLogging=false`
Example
```julia
using IncrementalInference
# generate a factor graph
fg = generateCanonicalFG_Kaess()
# solve and record everything
smtasks = Task[]
tree, _, = solveTree!(fg, smtasks=smtasks, recordcliqs=ls(fg));
# draw Bayes tree with graphviz and xdot installed
drawTree(tree, show=true)
# fetch histories
hists = fetchCliqHistoryAll!(smtasks);
# check a new csmc before step 2
csmc_ = repeatCSMStep!(hists, 1, 1)
# For use with VSCode debugging
@enter repeatCSMStep!(hists, 1, 1)
# or perhaps test a longer chain of changes
hists_ = deepcopy(hists)
repeatCSMStep!(hists_, 1, 4, duplicate=false)
repeatCSMStep!(hists_, 1, 5, duplicate=false)
repeatCSMStep!(hists_, 1, 6, duplicate=false)
```
Related
[`solveTree!`](@ref), [`fetchCliqHistoryAll`](@ref), [`printCSMHistoryLogical`](@ref), [`printCSMHistorySequential`](@ref), cliqHistFilterTransitions
"""
function repeatCSMStep!(hists::Dict{Int,<:AbstractVector{CSMHistoryTuple}},
csmid::Int,
step::Int;
duplicate::Bool=true,
enableLogging::Bool=false )
#
# the function at steo
fnc_ = hists[csmid][step].f
# the data before step
csmc_ = (duplicate ? x->deepcopy(x) : x->x)( hists[csmid][step].csmc )
csmc_.enableLogging = enableLogging
# run the step
fnc_(csmc_)
end
"""
$SIGNATURES
Reattach a CSM's data container after the deepcopy used from recordcliq.
MIGHT BE OBSOLETE
"""
function attachCSM!(csmc::CliqStateMachineContainer,
dfg::AbstractDFG,
tree::MetaBayesTree;
logger = SimpleLogger(stdout))
#
# csmc = csmc__
@error("attachCSM! has been updated without testing and might not work as you intended.")
csmc.dfg = dfg
csmc.tree = tree
csmc.logger = logger # TODO option to reopen and append to previous logger file
@info "attaching csmc and dropping any contents from csmc's previously held (copied) message channels."
cid = csmc.cliq.id.value
# pids = csmc.parentCliq .|> x->x.id
# cids = csmc.childCliqs .|> x->x.id
csmc.cliq = tree.cliques[cid]
# csmc.parentCliq = pids .|> x->getindex(tree.cliques, x)
# csmc.childCliqs = cids .|> x->getindex(tree.cliques, x)
return csmc
end
"""
$SIGNATURES
Draw many images in '/tmp/?/csm_%d.png' representing time synchronized state machine
events for cliques `cliqsyms::Vector{Symbol}`.
Notes
- State history must have previously been recorded (stored in tree cliques).
Related
printCliqHistorySummary
"""
function animateCliqStateMachines(tree::AbstractBayesTree,
cliqsyms::Vector{Symbol},
hists::Dict{Symbol, Tuple};
frames::Int=100 )
#
error("`animateCliqStateMachines` is outdated")
startT = Dates.now()
stopT = Dates.now()
# get start and stop times across all cliques
first = true
for sym in cliqsyms
hist = hists[sym] #getCliqSolveHistory(tree, sym)
if hist[1][1] < startT
startT = hist[1][1]
end
if first
stopT = hist[end][1]
first = false
end
if stopT < hist[end][1]
stopT= hist[end][1]
end
end
# export all figures
folders = String[]
for sym in cliqsyms
hist = hists[sym] #getCliqSolveHistory(tree, sym)
# hist = getCliqSolveHistory(tree, sym)
retval = animateStateMachineHistoryByTime(hist, frames=frames, folder="caesar/animatecsm/cliq$sym", title="$sym", startT=startT, stopT=stopT, rmfirst=false)
push!(folders, "cliq$sym")
end
return folders
end
"""
$SIGNATURES
Return state machine transition steps from history such that the `nextfnc::Function`.
Related:
printCliqHistorySummary, filterHistAllToArray, sandboxCliqResolveStep
"""
function cliqHistFilterTransitions(hist::Vector{CSMHistoryTuple}, nextfnc::Function)
ret = Vector{CSMHistoryTuple}()
for hi in hist
if hi[3] == nextfnc
push!(ret, hi)
end
end
return ret
end
"""
$SIGNATURES
Return state machine transition steps from all cliq histories with transition `nextfnc::Function`.
Related:
printCliqHistorySummary, cliqHistFilterTransitions, sandboxCliqResolveStep
"""
function filterHistAllToArray(tree::AbstractBayesTree, hists::Dict{Symbol, Tuple}, frontals::Vector{Symbol}, nextfnc::Function)
error("filterHistAllToArray needs to be updated for new CSM")
ret = Vector{CSMHistoryTuple}()
for sym in frontals
hist = hists[sym] # getCliqSolveHistory(tree, sym)
fih = cliqHistFilterTransitions(hist, nextfnc)
for fi in fih
push!(ret, fi)
end
end
return ret
end
"""
$SIGNATURES
Animate multiple clique state machines on the same graphviz visualization. Renders according to
linear time for all provided histories.
Example:
```julia
using Caesar
# build a factor graph
fg = initfg()
# addVariable!(...)
# addFactor!(...)
# ...
fsy = getTreeAllFrontalSyms(fg, tree) # for later use
# perform inference to find the factor graph marginal posterior estimates
tree, smt, hist = solveTree!(fg, recordcliqs=fsy)
# generate frames in standard location /tmp/caesar/csmCompound/
# requires: sudo apt-get install graphviz
csmAnimate(fg, tree, fsy, frames=500)
# to render and show from default location (might require)
# sudo apt-get install ffmpeg vlc
# .ogv [Totem Ubuntu default]
Base.rm("/tmp/caesar/csmCompound/out.ogv")
run(`ffmpeg -r 10 -i /tmp/caesar/csmCompound/csm_%d.png -c:v libtheora -vf fps=25 -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -q 10 /tmp/caesar/csmCompound/out.ogv`)
run(`totem /tmp/caesar/csmCompound/out.ogv`)
# h.264 [VLC not default]
Base.rm("/tmp/caesar/csmCompound/out.mp4")
run(`ffmpeg -r 10 -i /tmp/caesar/csmCompound/csm_%d.png -c:v libx264 -vf fps=25 -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" /tmp/caesar/csmCompound/out.mp4`)
run(`vlc /tmp/caesar/csmCompound/out.mp4`)
```
"""
function animateCSM(tree::AbstractBayesTree,
autohist::Dict{Int, T};
frames::Int=100,
interval::Int=2,
dpi::Int=100,
rmfirst::Bool=true,
folderpath::AbstractString="/tmp/caesar/csmCompound/",
fsmColors::Dict{Symbol,String}=Dict{Symbol,String}(),
defaultColor::AbstractString="red" ) where T <: AbstractVector
#
easyNames = Dict{Symbol, Int}()
hists = Dict{Symbol, Vector{Tuple{DateTime,Int64,Function,CliqStateMachineContainer}}}()
for (id, hist) in autohist
frtl = getFrontals(tree.cliques[id])
hists[frtl[1]] = Tuple.(hist)
easyNames[frtl[1]] = id
end
startT = Dates.now()
stopT = Dates.now()
# get start and stop times across all cliques
first = true
for (csym, hist) in hists
# global startT, stopT
@show csym
if hist[1][1] < startT
startT = hist[1][1]
end
if first
stopT = hist[end][1]
first = false
end
if stopT < hist[end][1]
stopT = hist[end][1]
end
end
# export all figures
if rmfirst
@warn "Removing $folderpath in preparation for new frames."
Base.rm("$folderpath", recursive=true, force=true)
end
function csmTreeAni(hl::Tuple, frame::Int, folderpath::AbstractString)
drawTree(hl[4].tree, show=false, filepath=joinpath(folderpath, "tree_$frame.png"), dpi=dpi)
nothing
end
function autocolor_cb(hi::Tuple, csym::Symbol, aniT::DateTime)
retc = getCliqueDrawColor(hi[4].cliq)
return (retc === nothing ? "gray" : retc)
end
# animateStateMachineHistoryByTimeCompound(hists, startT, stopT, folder="caesar/csmCompound", frames=frames)
animateStateMachineHistoryIntervalCompound( hists, easyNames=easyNames,
folderpath=folderpath,
interval=interval, dpi=dpi,
draw_more_cb=csmTreeAni,
fsmColors=fsmColors,
defaultColor=defaultColor,
autocolor_cb=autocolor_cb )
end
"""
$SIGNATURES
Convenience function to assign and make video of CSM state machine for `cliqs`.
Notes
- Probably several teething issues still (lower priority).
- Use `assignhist` if solver params async was true, or errored.
Related
csmAnimate, printCliqHistorySummary
"""
function makeCsmMovie(fg::AbstractDFG,
tree::AbstractBayesTree,
cliqs=ls(fg);
assignhist=nothing,
show::Bool=true,
filename::AbstractString="/tmp/caesar/csmCompound/out.ogv",
frames::Int=1000 )
#
if assignhist != nothing
assignTreeHistory!(tree, assignhist)
end
csmAnimate(fg, tree, cliqs, frames=frames)
# Base.rm("/tmp/caesar/csmCompound/out.ogv")
run(`ffmpeg -r 10 -i /tmp/caesar/csmCompound/csm_%d.png -c:v libtheora -vf fps=25 -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -q 10 $filename`)
if show
@async run(`totem $filename`)
end
filename
end
#