-
Notifications
You must be signed in to change notification settings - Fork 21
/
FactorGraph01.jl
1044 lines (919 loc) · 32.7 KB
/
FactorGraph01.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
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
reshapeVec2Mat(vec::Vector, rows::Int) = reshape(vec, rows, round(Int,length(vec)/rows))
# function reshapeVec2Mat(vec::Vector, rows::Int)
# M = reshape(vec, rows, round(Int,length(vec)/rows))
# return ndims(M) < 2 ? (M')' : M
# end
# get vertex from factor graph according to label symbol "x1"
getVert(fgl::FactorGraph, lbl::Symbol; api::DataLayerAPI=dlapi, nt::Symbol=:var) = api.getvertex(fgl, lbl, nt=nt)
getVert(fgl::FactorGraph, id::Int; api::DataLayerAPI=dlapi) = api.getvertex(fgl, id)
# TODO -- upgrade to dedicated memory location in Graphs.jl
# see JuliaArchive/Graphs.jl#233
getData(v::Graphs.ExVertex) = v.attributes["data"]
# Convenience functions
getData(fgl::FactorGraph, lbl::Symbol; api::DataLayerAPI=dlapi) = getData(getVert(fgl, lbl, api=api))
getData(fgl::FactorGraph, id::Int; api::DataLayerAPI=dlapi) = getData(getVert(fgl, id, api=api))
function setData!(v::Graphs.ExVertex, data)
v.attributes["data"] = data
nothing
end
function getVal(v::Graphs.ExVertex)
return getData(v).val
end
function getVal(v::Graphs.ExVertex, idx::Int)
return getData(v).val[:,idx]
end
# Convenience function to get values for given variable label
function getVal(fgl::FactorGraph, lbl::Symbol; api::DataLayerAPI=dlapi)
#getVal(dlapi.getvertex(fgl, lbl))
getVal(getVert(fgl, lbl, api=api))
end
function getVal(fgl::FactorGraph, exvertid::Int; api::DataLayerAPI=dlapi)
# getVal(dlapi.getvertex(fgl, exvertid))
getVal(getVert(fgl, exvertid, api=api))
end
function getNumPts(v::Graphs.ExVertex)
return size(getData(v).val,2)
end
function getfnctype(data)
if typeof(data).name.name == :VariableNodeData
return VariableNodeData
end
data.fnc.usrfnc!
end
function getfnctype(vertl::Graphs.ExVertex)
data = getData(vertl)
getfnctype(data)
end
function getfnctype(fgl::FactorGraph, exvertid::Int; api::DataLayerAPI=dlapi)
#
# data = getData(fgl, exvertid, api=api)
# data.fnc.usrfnc!
getfnctype(getVert(fgl, exvertid, api=api))
end
# setVal! assumes you will update values to database separate, this used for local graph mods only
function setVal!(v::Graphs.ExVertex, val::Array{Float64,2})
getData(v).val = val
# v.attributes["data"].val = val
nothing
end
function getBWVal(v::Graphs.ExVertex)
return getData(v).bw
end
function setBW!(v::Graphs.ExVertex, bw::Array{Float64,2})
getData(v).bw = bw
# v.attributes["data"].bw = bw
nothing
end
function setVal!(v::Graphs.ExVertex, val::Array{Float64,2}, bw::Array{Float64,2})
setVal!(v,val)
setBW!(v,bw)
nothing
end
function setVal!(v::Graphs.ExVertex, val::Array{Float64,2}, bw::Vector{Float64})
setVal!(v,val,reshape(bw,length(bw),1)) #(bw')')
nothing
end
function setVal!(fg::FactorGraph, sym::Symbol, val::Array{Float64,2}; api::DataLayerAPI=localapi)
setVal!(api.getvertex(fg, sym), val)
end
function setValKDE!(v::Graphs.ExVertex, val::Array{Float64,2})
p = kde!(val)
setVal!(v,val,getBW(p)[:,1]) # TODO -- this can be little faster
nothing
end
function setValKDE!(v::Graphs.ExVertex, em::EasyMessage)
setVal!(v, em.pts, em.bws ) # getBW(p)[:,1]
nothing
end
function setValKDE!(v::Graphs.ExVertex, p::BallTreeDensity)
pts = getPoints(p)
setVal!(v, pts, getBW(p)[:,1]) # BUG ...al!(., val, . ) ## TODO -- this can be little faster
nothing
end
setVal!(v::Graphs.ExVertex, em::EasyMessage) = setValKDE!(v, em)
setVal!(v::Graphs.ExVertex, p::BallTreeDensity) = setValKDE!(v, p)
function kde!(em::EasyMessage)
return kde!(em.pts,em.bws)
end
# TODO -- there should be a better way, without retrieving full vertex
getOutNeighbors(fgl::FactorGraph, v::ExVertex; api::DataLayerAPI=dlapi, needdata::Bool=false, ready::Int=1,backendset::Int=1 ) = api.outneighbors(fgl, v, needdata=needdata, ready=ready, backendset=backendset )
getOutNeighbors(fgl::FactorGraph, vertid::Int; api::DataLayerAPI=dlapi, needdata::Bool=false, ready::Int=1,backendset::Int=1 ) = api.outneighbors(fgl, api.getvertex(fgl,vertid), needdata=needdata, ready=ready, backendset=backendset )
function updateFullVert!(fgl::FactorGraph, exvert::ExVertex;
api::DataLayerAPI=IncrementalInference.dlapi,
updateMAPest::Bool=false )
#
warn("use of updateFullVert! should be clarified for local or remote operations.")
api.updatevertex!(fgl, exvert, updateMAPest=updateMAPest)
end
function setDefaultNodeData!(v::Graphs.ExVertex,
initval::Array{Float64,2},
stdev::Array{Float64,2},
dodims::Int,
N::Int,
dims::Int;
gt=nothing, initialized::Bool=true,
softtype=nothing)
# TODO review and refactor this function, exists as legacy from pre-v0.3.0
# this should be the only function allocating memory for the node points (unless number of points are changed)
data = nothing
if initialized
if size(initval,2) < N && size(initval, 1) == dims
warn("setDefaultNodeData! -- deprecated use of stdev.")
p = kde!(initval,diag(stdev));
pN = resample(p,N)
elseif size(initval,2) < N && size(initval, 1) != dims
println("Node value memory allocated but not initialized")
pN = kde!(randn(dims, N));
else
pN = kde!(initval)
end
# dims = size(initval,1) # rows indicate dimensions
sp = Int[0;] #round.(Int,linspace(dodims,dodims+dims-1,dims))
gbw = getBW(pN)[:,1]
gbw2 = Array{Float64}(length(gbw),1)
gbw2[:,1] = gbw[:]
pNpts = getPoints(pN)
data = VariableNodeData(initval, stdev, pNpts,
gbw2, Int[], sp,
dims, false, 0, Int[], gt, softtype, true) #initialized
else
sp = round.(Int,linspace(dodims,dodims+dims-1,dims))
data = VariableNodeData(initval, stdev, zeros(dims, N),
zeros(dims,1), Int[], sp,
dims, false, 0, Int[], gt, softtype, false) #initialized
end
#
setData!(v, data)
# v.attributes["data"] = data
# what about dedicated user variable metadata
# p.factormetadata
nothing
end
"""
$(SIGNATURES)
Initialize a new Graphs.ExVertex which will be added to some factor graph.
"""
function addNewVarVertInGraph!(fgl::FactorGraph,
vert::Graphs.ExVertex,
id::Int,
lbl::Symbol,
ready::Int,
smalldata )
#
vert.attributes = Graphs.AttributeDict() #fg.v[fg.id]
vert.attributes["label"] = string(lbl) #fg.v[fg.id]
fgl.IDs[lbl] = id
# used for cloudgraph solving
vert.attributes["ready"] = ready
vert.attributes["backendset"] = 0
# store user metadata
vert.attributes["smalldata"] = smalldata
nothing
end
"""
$(SIGNATURES)
Add a node (variable) to a graph. Use this over the other dispatches.
"""
function addNode!(fg::FactorGraph,
lbl::Symbol,
softtype::T;
N::Int=100,
autoinit::Bool=true, # does init need to be separate from ready? TODO
ready::Int=1,
labels::Vector{<:AbstractString}=String[],
api::DataLayerAPI=dlapi,
uid::Int=-1,
smalldata="" ) where {T <:InferenceVariable}
#
currid = fg.id+1
if uid==-1
fg.id=currid
else
currid = uid
end
# dims = dims != -1 ? dims : st.dims
lblstr = string(lbl)
vert = ExVertex(currid,lblstr)
addNewVarVertInGraph!(fg, vert, currid, lbl, ready, smalldata)
# dlapi.setupvertgraph!(fg, vert, currid, lbl) #fg.v[currid]
# dodims = fg.dimID+1
setDefaultNodeData!(vert, zeros(softtype.dims,N), zeros(0,0), 0, N, softtype.dims, initialized=!autoinit, softtype=softtype) # dodims
vnlbls = union(string.(labels), softtype.labels, String["VARIABLE";])
push!(vnlbls, fg.sessionname)
api.addvertex!(fg, vert, labels=vnlbls)
# fg.dimID+=dims # DONE -- drop this, rows indicate dimensions, move to last dimension
push!(fg.nodeIDs, currid)
vert
end
"""
$(SIGNATURES)
Add a node (variable) to a graph. Use this over the other dispatches.
"""
function addNode!(fg::FactorGraph,
lbl::Symbol,
softtype::Type{<:InferenceVariable};
N::Int=100,
autoinit::Bool=true,
ready::Int=1,
labels::Vector{<:AbstractString}=String[],
api::DataLayerAPI=dlapi,
uid::Int=-1,
# dims::Int=-1,
smalldata="" )
#
addNode!(fg,
lbl,
softtype();
N=N,
autoinit=autoinit,
ready=ready,
labels=labels,
api=api,
uid=uid,
smalldata=smalldata )
end
function getVal(vA::Array{Graphs.ExVertex,1})
warn("getVal(::Vector{ExVertex}) is obsolete, use getVal.(ExVertex) instead.")
len = length(vA)
vals = Array{Array{Float64,2},1}()
cols = Array{Int,1}()
push!(cols,0)
rows = Array{Int,1}()
for v in vA
push!(vals, getVal(v))
c = size(vals[end],2)
r = size(vals[end],1)
push!(cols, floor(Int,c))
push!(rows, floor(Int,r))
end
cols = cumsum(cols)
sc = cols[end]
rw = floor(Int,rows[1])
val = Array{Float64,2}(rw, sc)
for i in 1:(len-1)
val[:,(cols[i]+1):cols[i+1]] = vals[i]
end
val[:,(cols[len]+1):cols[len+1]] = vals[len] # and the last one
return val
end
"""
$(SIGNATURES)
Prepare the particle arrays `ARR` to be used for approximate convolution. This function ensures that ARR has te same dimensions among all the parameters. Function returns with ARR[sfidx] pointing at newly allocated deepcopy of the existing values in getVal(Xi[.index==solvefor]). Return values `sfidx` is the element in ARR where `Xi.index==solvefor` and `maxlen` is length of all (possibly resampled) `ARR` contained particles. Note `Xi` is order sensitive.
"""
function prepareparamsarray!(ARR::Array{Array{Float64,2},1},
Xi::Vector{Graphs.ExVertex},
N::Int,
solvefor::Int )
#
LEN = Int[]
maxlen = N
count = 0
sfidx = 0
for xi in Xi
push!(ARR, getVal(xi))
len = size(ARR[end], 2)
push!(LEN, len)
if len > maxlen
maxlen = len
end
count += 1
if xi.index == solvefor
sfidx = count #xi.index
end
end
SAMP=LEN.<maxlen
for i in 1:count
if SAMP[i]
ARR[i] = KernelDensityEstimate.sample(getKDE(Xi[i]), maxlen)[1]
end
end
# TODO --rather define reusable memory for the proposal
# we are generating a proposal distribution, not direct replacement for existing memory and hence the deepcopy.
if sfidx > 0 ARR[sfidx] = deepcopy(ARR[sfidx]) end
return maxlen, sfidx
end
function parseusermultihypo(multihypo::Void)
verts = Symbol[]
mh = nothing
return mh
end
function parseusermultihypo(multihypo::Union{Tuple,Vector{Float64}})
mh = nothing
if multihypo != nothing
multihypo2 = Float64[multihypo...]
# verts = Symbol.(multihypo[1,:])
for i in 1:length(multihypo)
if multihypo[i] > 0.999999
multihypo2[i] = 0.0
end
end
mh = Categorical(Float64[multihypo2...] )
end
return mh
end
function prepgenericconvolution(
Xi::Vector{Graphs.ExVertex},
usrfnc::T;
multihypo::Union{Void, Distributions.Categorical}=nothing,
threadmodel=MultiThreaded ) where {T <: FunctorInferenceType}
# multiverts::Vector{Symbol}=Symbol[]
#
ARR = Array{Array{Float64,2},1}()
maxlen, sfidx = prepareparamsarray!(ARR, Xi, 0, 0)
fldnms = fieldnames(usrfnc)
zdim = typeof(usrfnc) != GenericMarginal ? size(getSample(usrfnc, 2)[1],1) : 0
ccw = CommonConvWrapper(
usrfnc,
zeros(1,0),
zdim,
ARR,
specialzDim = sum(fldnms .== :zDim) >= 1,
partial = sum(fldnms .== :partial) >= 1,
hypotheses=multihypo,
threadmodel=threadmodel
)
#
for i in 1:Threads.nthreads()
ccw.cpt[i].factormetadata.variableuserdata = []
ccw.cpt[i].factormetadata.solvefor = :null
for xi in Xi
push!(ccw.cpt[i].factormetadata.variableuserdata, getData(xi).softtype)
end
end
return ccw
end
function setDefaultFactorNode!(
fgl::FactorGraph,
vert::Graphs.ExVertex,
Xi::Vector{Graphs.ExVertex},
usrfnc::T;
multihypo::Union{Void,Tuple,Vector{Float64}}=nothing,
threadmodel=MultiThreaded) where {T <: Union{FunctorInferenceType, InferenceType}}
#
ftyp = typeof(usrfnc) # maybe this can be T
# @show "setDefaultFactorNode!", usrfnc, ftyp, T
mhcat = parseusermultihypo(multihypo)
# gwpf = prepgenericwrapper(Xi, usrfnc, getSample, multihypo=mhcat)
ccw = prepgenericconvolution(Xi, usrfnc, multihypo=mhcat, threadmodel=threadmodel)
m = Symbol(ftyp.name.module)
# experimental wip
data_ccw = FunctionNodeData{CommonConvWrapper{T}}(Int[], false, false, Int[], m, ccw)
vert.attributes["data"] = data_ccw
# existing interface
# data = FunctionNodeData{GenericWrapParam{T}}(Int[], false, false, Int[], m, gwpf)
# vert.attributes["data"] = data
nothing
end
function addNewFncVertInGraph!(fgl::FactorGraph, vert::Graphs.ExVertex, id::Int, lbl::Symbol, ready::Int)
vert.attributes = Graphs.AttributeDict() #fg.v[fg.id]
vert.attributes["label"] = lbl #fg.v[fg.id]
# fgl.f[id] = vert # -- not sure if this is required, using fg.g.vertices
fgl.fIDs[lbl] = id # fg.id,
# used for cloudgraph solving
vert.attributes["ready"] = ready
vert.attributes["backendset"] = 0
# for graphviz drawing
vert.attributes["shape"] = "point"
vert.attributes["width"] = 0.2
nothing
end
addNewFncVertInGraph!{T <: AbstractString}(fgl::FactorGraph, vert::Graphs.ExVertex, id::Int, lbl::T, ready::Int) =
addNewFncVertInGraph!(fgl,vert, id, Symbol(lbl), ready)
function isInitialized(vert::Graphs.ExVertex)::Bool
return getData(vert).initialized
end
function isInitialized(fgl::FactorGraph, vsym::Symbol)::Bool
# TODO, make cloudgraphs work and make faster by avoiding all the getVerts
isInitialized(getVert(fgl, vsym))
end
"""
$(SIGNATURES)
initialize destination variable nodes based on this factor in factor graph, fg, generally called
during addFactor!. Destination factor is first (singletons) or second (dim 2 pairwise) variable vertex in Xi.
"""
function doautoinit!(fgl::FactorGraph,
Xi::Vector{Graphs.ExVertex};
api::DataLayerAPI=dlapi,
singles::Bool=true,
N::Int=100)
# Mighty inefficient function, since we only need very select fields nearby from a few neighboring nodes
# do double depth search for variable nodes
# TODO this should maybe stay localapi only...
for xi in Xi
if !isInitialized(xi)
vsym = Symbol(xi.label)
neinodes = ls(fgl, vsym)
if (length(neinodes) > 1 || singles) # && !isInitialized(xi)
# Which of the factors can be used for initialization
useinitfct = Symbol[]
# println("Consider all pairwise factors connected to $vsym...")
for xifct in neinodes #potntlfcts
xfneivarnodes = lsf(fgl, xifct)
for vsym2 in xfneivarnodes
# println("find all variables that are initialized for $vsym2")
vert2 = getVert(fgl, vsym2)
if (isInitialized(vert2) && sum(useinitfct .== xifct) == 0 ) || length(xfneivarnodes) == 1
# OR singleton TODO get faster version of isInitialized for database version
# println("adding $xifct to init factors list")
push!(useinitfct, xifct)
end
end
end
# println("Consider all singleton (unary) factors to $vsym...")
# calculate the predicted belief over $vsym
pts = predictbelief(fgl, vsym, useinitfct, api=api)
setValKDE!(xi, pts)
getData(xi).initialized = true
api.updatevertex!(fgl, xi, updateMAPest=false)
end
end
end
nothing
end
"""
$(SIGNATURES)
initialize destination variable nodes based on this factor in factor graph, fg, generally called
during addFactor!. Destination factor is first (singletons) or second (dim 2 pairwise) variable vertex in Xi.
"""
function doautoinit!(fgl::FactorGraph,
xsyms::Vector{Symbol};
api::DataLayerAPI=dlapi,
singles::Bool=true,
N::Int=100)
#
verts = getVert.(fgl, xsyms, api=api)
doautoinit!(fgl, verts, api=api, singles=singles, N=N)
end
function doautoinit!(fgl::FactorGraph,
xsym::Symbol;
api::DataLayerAPI=dlapi,
singles::Bool=true,
N::Int=100)
#
doautoinit!(fgl, [getVert(fgl, xsym, api=api);], api=api, singles=singles, N=N)
end
function ensureAllInitialized!(fgl::FactorGraph; api::DataLayerAPI=dlapi)
xx, xl = ls(fgl)
allvarnodes = union(xx, xl)
for vsym in allvarnodes
if !isInitialized(fgl, vsym)
println("$vsym is not initialized, and will do so now...")
doautoinit!(fgl, Graphs.ExVertex[getVert(fgl, vsym, api=api);], api=api, singles=true)
end
end
nothing
end
function assembleFactorName(fgl::FactorGraph, Xi::Vector{Graphs.ExVertex}; maxparallel::Int=50)
namestring = ""
for vert in Xi #f.Xi
namestring = string(namestring,vert.attributes["label"])
end
for i in 1:maxparallel
tempnm = string(namestring, "f$i")
if !haskey(fgl.fIDs, Symbol(tempnm))
namestring = tempnm
break
end
i != maxparallel ? nothing : error("Cannot currently add more than $(maxparallel) factors in parallel, please open an issue if this is too restrictive.")
end
return namestring
end
"""
$(SIGNATURES)
Add factor with user defined type <: FunctorInferenceType to the factor graph object. Define whether the automatic initialization of variables should be performed. Use order sensitive `multihypo` keyword argument to define if any variables are related to data association uncertainty.
"""
function addFactor!(fgl::FactorGraph,
Xi::Vector{Graphs.ExVertex},
usrfnc::R;
multihypo::Union{Void,Tuple,Vector{Float64}}=nothing,
ready::Int=1,
api::DataLayerAPI=dlapi,
labels::Vector{T}=String[],
uid::Int=-1,
autoinit::Bool=true,
threadmodel=MultiThreaded ) where
{R <: Union{FunctorInferenceType, InferenceType},
T <: AbstractString}
#
currid = fgl.id+1
if uid==-1
fgl.id=currid
else
currid = uid
end
namestring = assembleFactorName(fgl, Xi)
# fgl.id+=1
newvert = ExVertex(currid,namestring)
addNewFncVertInGraph!(fgl, newvert, currid, namestring, ready)
setDefaultFactorNode!(fgl, newvert, Xi, deepcopy(usrfnc), multihypo=multihypo, threadmodel=threadmodel)
push!(fgl.factorIDs,currid)
for vert in Xi
push!(getData(newvert).fncargvID, vert.index)
# push!(newvert.attributes["data"].fncargvID, vert.index)
end
fnlbls = deepcopy(labels)
fnlbls = union(fnlbls, String["FACTOR";])
push!(fnlbls, fgl.sessionname)
# TODO -- multiple accesses to DB with this method, must refactor!
newvert = api.addvertex!(fgl, newvert, labels=fnlbls) # used to be two be three lines up ##fgl.g
for vert in Xi
api.makeaddedge!(fgl, vert, newvert)
end
# TODO change this operation to update a conditioning variable
if autoinit
doautoinit!(fgl, Xi, api=api, singles=false)
end
return newvert
end
"""
$(SIGNATURES)
Add factor with user defined type <: FunctorInferenceType to the factor graph object. Define whether the automatic initialization of variables should be performed. Use order sensitive `multihypo` keyword argument to define if any variables are related to data association uncertainty.
"""
function addFactor!(
fgl::FactorGraph,
xisyms::Vector{Symbol},
usrfnc::R;
multihypo::Union{Void,Tuple,Vector{Float64}}=nothing,
ready::Int=1,
api::DataLayerAPI=dlapi,
labels::Vector{T}=String[],
uid::Int=-1,
autoinit::Bool=true,
threadmodel=MultiThreaded ) where
{R <: Union{FunctorInferenceType, InferenceType},
T <: AbstractString}
#
verts = Vector{Graphs.ExVertex}()
for xi in xisyms
push!( verts, api.getvertex(fgl,xi) )
end
addFactor!(fgl, verts, usrfnc, multihypo=multihypo, ready=ready, api=api, labels=labels, uid=uid, autoinit=autoinit, threadmodel=threadmodel )
end
function prtslperr(s)
println(s)
sleep(0.1)
error(s)
end
# for computing the Bayes Net-----------------------------------------------------
function getEliminationOrder(fg::FactorGraph; ordering::Symbol=:qr)
s = fg.nodeIDs
lens = length(s)
sf = fg.factorIDs
lensf = length(sf)
adjm, dictpermu = adjacency_matrix(fg.g,returnpermutation=true)
permuteds = Vector{Int}(lens)
permutedsf = Vector{Int}(lensf)
for j in 1:length(dictpermu)
semap = 0
for i in 1:lens
if dictpermu[j] == s[i]
permuteds[i] = j#dictpermu[j]
semap += 1
if semap >= 2 break; end
end
end
for i in 1:lensf
if dictpermu[j] == sf[i]
permutedsf[i] = j#dictpermu[j]
semap += 1
if semap >= 2 break; end
end
end
end
A=convert(Array{Int},adjm[permutedsf,permuteds]) # TODO -- order seems brittle
p = Int[]
if ordering==:chol
p = cholfact(A'A,:U,Val{true})[:p] #,pivot=true
elseif ordering==:qr
q,r,p = qr(A,Val{true})
else
prtslperr("getEliminationOrder -- cannot do the requested ordering $(ordering)")
end
# we need the IDs associated with the Graphs.jl and our Type fg
return dictpermu[permuteds[p]] # fg.nodeIDs[p]
end
# lets create all the vertices first and then deal with the elimination variables thereafter
function addBayesNetVerts!(fg::FactorGraph, elimOrder::Array{Int,1})
for p in elimOrder
vert = getVert(fg, p, api=localapi)
@show vert.label, getData(vert).BayesNetVertID
if getData(vert).BayesNetVertID == 0
fg.bnid+=1
vert.attributes["data"].BayesNetVertID = p
localapi.updatevertex!(fg, vert)
else
println("addBayesNetVerts -- something is very wrong, should not have a Bayes net vertex")
end
end
end
function addConditional!(fg::FactorGraph, vertID::Int, lbl, Si)
bnv = getVert(fg, vertID, api=localapi) #fg.v[vertID]
bnvd = getData(bnv) # bnv.attributes["data"]
bnvd.separator = Si
for s in Si
push!(bnvd.BayesNetOutVertIDs, s)
end
localapi.updatevertex!(fg, bnv)
nothing
end
function addChainRuleMarginal!(fg::FactorGraph, Si)
lbls = String[]
genmarg = GenericMarginal()
Xi = Graphs.ExVertex[]
for s in Si
push!(Xi, getVert(fg, s, api=localapi))
end
println("adding marginal to")
for x in Xi @show x.index end
addFactor!(fg, Xi, genmarg, api=localapi, autoinit=false)
nothing
end
# TODO -- Cannot have any CloudGraph calls at this level, must refactor
function rmVarFromMarg(fgl::FactorGraph, fromvert::Graphs.ExVertex, gm::Array{Graphs.ExVertex,1})
for m in gm
# get all out edges
# get neighbors
for n in localapi.outneighbors(fgl, m)
if n.index == fromvert.index
alleids = m.attributes["data"].edgeIDs
i = 0
for id in alleids
i+=1
edge = localapi.getedge(fgl, id)
if edge != nothing # hack to avoid dictionary use case
if edge.SourceVertex.exVertexId == m.index || edge.DestVertex.exVertexId == m.index
warn("removing edge $(edge.neo4jEdgeId), between $(m.index) and $(n.index)")
localapi.deleteedge!(fgl, edge)
m.attributes["data"].edgeIDs = alleids[[collect(1:(i-1));collect((i+1):length(alleids))]]
localapi.updatevertex!(fgl, m)
end
end
end
end
end
# if 0 edges, delete the marginal
if length(localapi.outneighbors(fgl, m)) <= 1
warn("removing vertex id=$(m.index)")
localapi.deletevertex!(fgl,m)
end
end
nothing
end
function buildBayesNet!(fg::FactorGraph, p::Array{Int,1})
addBayesNetVerts!(fg, p)
for v in p
println()
println("Eliminating $(v)")
println("===============")
println()
# which variable are we eliminating
# all factors adjacent to this variable
fi = Int[]
Si = Int[]
gm = ExVertex[]
# TODO -- optimize outneighbor calls like this
vert = localapi.getvertex(fg,v)
for fct in localapi.outneighbors(fg, vert)
if (getData(fct).eliminated != true)
push!(fi, fct.index)
for sepNode in localapi.outneighbors(fg, fct)
if sepNode.index != v && length(findin(sepNode.index,Si)) == 0
push!(Si,sepNode.index)
end
end
getData(fct).eliminated = true #fct.attributes["data"].eliminated = true
localapi.updatevertex!(fg, fct) # TODO -- this might be a premature statement
end
if typeof(getData(fct).fnc) == GenericMarginal
push!(gm, fct)
end
end
if v != p[end]
addConditional!(fg, v, "", Si)
# not yet inserting the new prior p(Si) back into the factor graph
end
tuv = localapi.getvertex(fg, v) # TODO -- This may well through away valuable data
tuv.attributes["data"].eliminated = true # fg.v[v].
localapi.updatevertex!(fg, tuv)
# TODO -- remove links from current vertex to any marginals
rmVarFromMarg(fg, vert, gm)
#add marginal on remaining variables... ? f(xyz) = f(x | yz) f(yz)
# new function between all Si
addChainRuleMarginal!(fg, Si)
end
nothing
end
# some plotting functions on the factor graph
function stackVertXY(fg::FactorGraph, lbl::String)
v = dlapi.getvertex(fg,lbl)
vals = getVal(v)
X=vec(vals[1,:])
Y=vec(vals[2,:])
return X,Y
end
function getKDE(v::Graphs.ExVertex)
return kde!(getVal(v), getBWVal(v)[:,1])
end
function getVertKDE(v::Graphs.ExVertex)
return getKDE(v)
end
function getVertKDE(fgl::FactorGraph, id::Int; api::DataLayerAPI=dlapi)
v = api.getvertex(fgl,id)
return getKDE(v)
end
function getVertKDE(fgl::FactorGraph, lbl::Symbol; api::DataLayerAPI=dlapi)
v = api.getvertex(fgl,lbl)
return getKDE(v)
end
function drawCopyFG(fgl::FactorGraph)
fgd = deepcopy(fgl)
for (sym,vid) in fgd.IDs
delete!(fgd.g.vertices[vid].attributes,"data")
!haskey(fgd.g.vertices[vid].attributes,"frtend") ? nothing : delete!(fgd.g.vertices[vid].attributes,"frtend")
end
for (sym,vid) in fgd.fIDs
delete!(fgd.g.vertices[vid].attributes,"data")
!haskey(fgd.g.vertices[vid].attributes,"frtend") ? nothing : delete!(fgd.g.vertices[vid].attributes,"frtend")
end
return fgd
end
function writeGraphPdf(fgl::FactorGraph;
pdfreader::Union{Void, String}="evince",
filename::AS="/tmp/fg.pdf" ) where {AS <: AbstractString}
#
fgd = drawCopyFG(fgl)
println("Writing factor graph file")
dotfile = split(filename, ".pdf")[1]*".dot"
fid = open(dotfile,"w")
write(fid,Graphs.to_dot(fgd.g))
close(fid)
run(`dot $(dotfile) -Tpdf -o $(filename)`)
try
pdfreader != nothing ? (@async run(`$(pdfreader) $(filename)`)) : nothing
catch e
warn("not able to show $(filename) with pdfreader=$(pdfreader). Exception e=$(e)")
end
nothing
end
function expandEdgeListNeigh!(fgl::FactorGraph,
vertdict::Dict{Int,Graphs.ExVertex},
edgedict::Dict{Int,Graphs.Edge{Graphs.ExVertex}})
#asfd
for vert in vertdict
for newedge in out_edges(vert[2],fgl.g)
if !haskey(edgedict, newedge.index)
edgedict[newedge.index] = newedge
end
end
end
nothing
end
# dictionary of unique vertices from edgelist
function expandVertexList!(fgl::FactorGraph,
edgedict::Dict{Int,Graphs.Edge{Graphs.ExVertex}},
vertdict::Dict{Int,Graphs.ExVertex})
# go through all source and target nodes
for edge in edgedict
if !haskey(vertdict, edge[2].source.index)
vertdict[edge[2].source.index] = edge[2].source
end
if !haskey(vertdict, edge[2].target.index)
vertdict[edge[2].target.index] = edge[2].target
end
end
nothing
end
function edgelist2edgedict(edgelist::Array{Graphs.Edge{Graphs.ExVertex},1})
edgedict = Dict{Int,Graphs.Edge{Graphs.ExVertex}}()
for edge in edgelist
edgedict[edge.index] = edge
end
return edgedict
end
# TODO -- convert to use add_vertex! instead, since edges type must be made also
function addVerticesSubgraph(fgl::FactorGraph,
fgseg::FactorGraph,
vertdict::Dict{Int,Graphs.ExVertex})
for vert in vertdict
fgseg.g.vertices[vert[1]] = vert[2]
if haskey(fgl.v,vert[1])
fgseg.g.vertices[vert[1]] = vert[2]
fgseg.IDs[Symbol(vert[2].label)] = vert[1]
# add edges going in opposite direction
elr = Graphs.out_edges(vert[2], fgl.g)
len = length(elr)
keeprm = trues(len)
j = 0
for i in 1:len
if !haskey(vertdict, elr[i].target.index) # a function node in set, so keep ref
keeprm[i] = false
j+=1
end
end
if j < len
elridx = elr[1].source.index
fgseg.g.inclist[elridx] = elr[keeprm]
end
elseif haskey(fgl.f, vert[1])
fgseg.f[vert[1]] = vert[2] # adding element to subgraph
fgseg.fIDs[Symbol(vert[2].label)] = vert[1]
# get edges associated with function nodes and push edges onto incidence list
el = Graphs.out_edges(vert[2], fgl.g)
elidx = el[1].source.index
fgseg.g.inclist[elidx] = el # okay because treating function nodes only
fgseg.g.nedges += length(el)
else
error("Unknown type factor graph vertex type, something is wrong")
end
end
nothing
end
# NOTICE, nodeIDs and factorIDs are not brough over by this method yet
# must sort out for incremental updates
function genSubgraph(fgl::FactorGraph,
vertdict::Dict{Int,Graphs.ExVertex})
# edgedict::Dict{Int,Graphs.Edge{Graphs.ExVertex}},
fgseg = FactorGraph() # new handle for just a segment of the graph
fgseg.g = Graphs.inclist(Graphs.ExVertex,is_directed=false)
fgseg.v = Dict{Int,Graphs.ExVertex}()
fgseg.f = Dict{Int,Graphs.ExVertex}()
fgseg.IDs = Dict{AbstractString,Int}()
fgseg.fIDs = Dict{AbstractString,Int}()
# TODO -- convert to use empty constructor since Graphs.incdict now works
fgseg.g.vertices = Array{Graphs.ExVertex,1}(length(fgl.g.vertices))
fgseg.g.inclist = Array{Array{Graphs.Edge{Graphs.ExVertex},1},1}(length(fgl.g.inclist))
addVerticesSubgraph(fgl, fgseg, vertdict)
fgseg.id = fgl.id
fgseg.bnid = fgl.bnid
fgseg.dimID = fgl.dimID
return fgseg
end
function getShortestPathNeighbors(fgl::FactorGraph;
from::Graphs.ExVertex=nothing,
to::Graphs.ExVertex=nothing,
neighbors::Int=0 )
edgelist = shortest_path(fgl.g, ones(num_edges(fgl.g)), from, to)
vertdict = Dict{Int,Graphs.ExVertex}()
edgedict = edgelist2edgedict(edgelist)
expandVertexList!(fgl, edgedict, vertdict) # grow verts
for i in 1:neighbors
expandEdgeListNeigh!(fgl, vertdict, edgedict) # grow edges
expandVertexList!(fgl, edgedict, vertdict) # grow verts
end
return vertdict
end
function subgraphShortestPath(fgl::FactorGraph;
from::Graphs.ExVertex=nothing,
to::Graphs.ExVertex=nothing,
neighbors::Int=0 )
vertdict = getShortestPathNeighbors(fgl, from=from, to=to, neighbors=neighbors)
return genSubgraph(fgl, vertdict)
end
# explore all shortest paths combinations in verts, add neighbors and reference subgraph
function subgraphFromVerts(fgl::FactorGraph,
verts::Dict{Int,Graphs.ExVertex};
neighbors::Int=0 )
allverts = Dict{Int,Graphs.ExVertex}()
allkeys = collect(keys(verts))
len = length(allkeys)
# union all shortest path combinations in a vertdict
for i in 1:len, j in (i+1):len
from = verts[allkeys[i]]
to = verts[allkeys[j]]