@@ -7,26 +7,26 @@ using ..GraphSignal, ..GraphPartition, ..BasisSpecification, ..GHWT, SparseArray
7
7
include (" common.jl" )
8
8
9
9
export HGLET_Synthesis, HGLET_jkl, HGLET_Analysis, HGLET_Analysis_All,
10
- HGLET_GHWT_BestBasis, HGLET_GHWT_Synthesis, HGLET_GHWT_BestBasis_minrelerror
10
+ HGLET_BestBasis, HGLET_GHWT_BestBasis, HGLET_GHWT_Synthesis, HGLET_GHWT_BestBasis_minrelerror
11
11
12
12
13
13
"""
14
14
function HGLET_Synthesis(dvec::Vector{Float64}, GP::GraphPart, BS::BasisSpec,
15
- G::GraphSig; method ::Symbol = :L)
15
+ G::GraphSig; gltype ::Symbol = :L)
16
16
17
17
### Input Arguments
18
18
* `dvec`: the expansion coefficients corresponding to the chosen basis
19
19
* `GP`: a GraphPart object
20
20
* `BS`: a BasisSpec object
21
21
* `G`: a GraphSig object
22
- * `method `: :L, :Lrw, or :Lsym, indicating which eigenvectors are used
22
+ * `gltype `: :L, :Lrw, or :Lsym, indicating which eigenvectors are used
23
23
24
24
### Output Argument
25
25
* `f`: the reconstructed signal
26
26
* `GS`: the reconstructed GraphSig object
27
27
"""
28
28
function HGLET_Synthesis (dvec:: Matrix{Float64} , GP:: GraphPart , BS:: BasisSpec ,
29
- G:: GraphSig ; method :: Symbol = :L )
29
+ G:: GraphSig ; gltype :: Symbol = :L )
30
30
# Preliminaries
31
31
W = G. W
32
32
jmax = size (GP. rs,2 )
@@ -63,12 +63,12 @@ function HGLET_Synthesis(dvec::Matrix{Float64}, GP::GraphPart, BS::BasisSpec,
63
63
normalizep = true
64
64
end
65
65
# compute the GL eigenvectors via svd.
66
- if ( method == :Lrw || method == :Lsym ) && normalizep
66
+ if ( gltype == :Lrw || gltype == :Lsym ) && normalizep
67
67
v,_,_ = svd (D2 * (D - Matrix (W_temp)) * D2)
68
68
else
69
69
v,_,_ = svd (D - Matrix (W_temp))
70
- if method != :L
71
- @warn (" Due to the small diagonal entries of W, we revert back to the option :L, not :" * String (method ))
70
+ if gltype != :L
71
+ @warn (" Due to the small diagonal entries of W, we revert back to the option :L, not :" * String (gltype ))
72
72
end
73
73
end
74
74
v = v[:,end : - 1 : 1 ] # reorder the ev's in the decreasing ew's
@@ -90,7 +90,7 @@ function HGLET_Synthesis(dvec::Matrix{Float64}, GP::GraphPart, BS::BasisSpec,
90
90
end
91
91
92
92
# reconstruct the signal
93
- if method == :Lrw && normalizep
93
+ if gltype == :Lrw && normalizep
94
94
f[rs1: rs3- 1 ,:] = D2* v* dmatrix[rs1: rs3- 1 ,j,:]
95
95
else
96
96
f[rs1: rs3- 1 ,:] = v* dmatrix[rs1: rs3- 1 ,j,:]
@@ -138,7 +138,7 @@ function HGLET_jkl(GP::GraphPart, drow::Int, dcol::Int)
138
138
end
139
139
140
140
"""
141
- function HGLET_Analysis(G::GraphSig, GP::GraphPart, method ::Symbol = :L)
141
+ function HGLET_Analysis(G::GraphSig, GP::GraphPart, gltype ::Symbol = :L)
142
142
143
143
For a GraphSig object 'G', generate the matrix of HGLET expansion
144
144
coefficients corresponding to the eigenvectors of specified version of
@@ -147,12 +147,12 @@ function HGLET_Analysis(G::GraphSig, GP::GraphPart, method::Symbol = :L)
147
147
### Input Arguments
148
148
* `G`: a GraphSig object
149
149
* `GP`: a GraphPart object
150
- * `method `: :L, :Lrw, or :Lsym, indicating which eigenvectors are used
150
+ * `gltype `: :L, :Lrw, or :Lsym, indicating which eigenvectors are used
151
151
152
152
### Output Argument
153
153
* `dmatrix`: the matrix of expansion coefficients using the specific GL matrix
154
154
"""
155
- function HGLET_Analysis (G:: GraphSig , GP:: GraphPart , method :: Symbol = :L )
155
+ function HGLET_Analysis (G:: GraphSig , GP:: GraphPart , gltype :: Symbol = :L )
156
156
# Preliminaries
157
157
W = G. W
158
158
ind = GP. ind
@@ -189,12 +189,12 @@ function HGLET_Analysis(G::GraphSig, GP::GraphPart, method::Symbol = :L)
189
189
end
190
190
191
191
# compute the GL eigenvectors via svd
192
- if ( method == :Lrw || method == :Lsym ) && normalizep
192
+ if ( gltype == :Lrw || gltype == :Lsym ) && normalizep
193
193
v,_,_ = svd (D2 * (D - Matrix (W_temp)) * D2)
194
194
else
195
195
v,_,_ = svd (D - Matrix (W_temp))
196
- if method != :L
197
- @warn (" Due to the small diagonal entries of W, we revert back to the option :L, not :" * String (method ))
196
+ if gltype != :L
197
+ @warn (" Due to the small diagonal entries of W, we revert back to the option :L, not :" * String (gltype ))
198
198
end
199
199
end
200
200
v = v[:,end : - 1 : 1 ] # reorder the ev's in the decreasing ew's
@@ -216,7 +216,7 @@ function HGLET_Analysis(G::GraphSig, GP::GraphPart, method::Symbol = :L)
216
216
end
217
217
218
218
# obtain the expansion coeffcients
219
- if method == :Lrw && normalizep
219
+ if gltype == :Lrw && normalizep
220
220
dmatrix[rs1: rs3- 1 ,j,:] = v' * (D.^ 0.5 )* G. f[indrs,:]
221
221
else
222
222
dmatrix[rs1: rs3- 1 ,j,:] = v' * G. f[indrs,:]
@@ -247,6 +247,8 @@ function HGLET_Analysis_All(G::GraphSig, GP::GraphPart)
247
247
W = G. W
248
248
ind = GP. ind
249
249
rs = GP. rs
250
+ # This is the type of graph Laplacian matrix used for graph partitioning;
251
+ # Not the eigenvector dictionary computed for the HGLET.
250
252
method = GP. method
251
253
N = size (G. W,1 )
252
254
jmax = size (rs,2 )
@@ -377,13 +379,95 @@ function HGLET_Analysis_All(G::GraphSig, GP::GraphPart)
377
379
end
378
380
379
381
382
+ """
383
+ function HGLET_BestBasis(GP::GraphPart;
384
+ dmatrix::Array{Float64,3} = Array{Float64,3}(undef,0,0,0),
385
+ gltype::Symbol = :L, cfspec::Any = 0.1, flatten::Any = 1)
386
+
387
+ Select the HGLET best basis from the input matrix of expansion coefficients
388
+
389
+ ### Input Arguments
390
+ * `GP`: a GraphPart object
391
+ * `dmatrix`: the matrix of HGLET expansion coefficients
392
+ * `gltype`: the type of graph Laplacian matrix used for HGLET dictionary
393
+ (default = :L)
394
+ * `cfspec`: the cost functional specification to be used: see utils.jl
395
+ for the detail. If it's a number, say, p, then the l^p norm is used.
396
+ If it's a function, then that function is used. Default is 0.1, i.e., l^0.1
397
+ * `flatten`: the method for flattening vector-valued data to scalar-valued data;
398
+ If it's a number, say, p, then the sum of the l^p norm is computed.
399
+ There are many other options, such as :ash, :entropy, etc. See utils.jl
400
+ for the detail. Default is 1, i.e, the sum of the l^1 norm of the coefs.
401
+
402
+ ### Output Argument
403
+ * `dvec`: the vector of expansion coefficients corresponding to the bestbasis
404
+ * `BS`: a BasisSpec object which specifies the best basis
405
+ """
406
+ function HGLET_BestBasis (GP:: GraphPart ;
407
+ dmatrix:: Array{Float64,3} = Array {Float64,3} (undef,0 ,0 ,0 ),
408
+ gltype:: Symbol = :L , cfspec:: Any = 0.1 , flatten:: Any = 1 )
409
+
410
+ # the cost functional to be used
411
+ costfun = cost_functional (cfspec)
412
+
413
+ # constants and dmatrix cleanup
414
+ N, jmax, fcols = size (dmatrix)
415
+ dmatrix[abs .(dmatrix) .< 10 ^ 2 * eps ()] .= 0
416
+
417
+ # flatten dmatrix for more than one input signals
418
+ if fcols > 1
419
+ dmatrix0 = deepcopy (dmatrix)
420
+ dmatrix = dropdims (dmatrix_flatten (dmatrix, flatten), dims = 3 )
421
+ end
422
+
423
+ #
424
+ # Find the HGLET best basis now!
425
+ #
426
+
427
+ # allocate/initialize ==> order matters here
428
+ dvec = dmatrix[:,jmax]
429
+ levlist = jmax* ones (Int,N)
430
+
431
+ # set the tolerance
432
+ tol = 10 ^ 4 * eps ()
433
+
434
+ # perform the basis search
435
+ for j = jmax: - 1 : 1
436
+ regioncount = count (! iszero, GP. rs[:,j]) - 1
437
+ for r = 1 : regioncount
438
+ indr = GP. rs[r,j]: (GP. rs[r+ 1 ,j]- 1 )
439
+ # ## compute the cost of the current best basis
440
+ costBB = costfun (dvec[indr])
441
+
442
+ # ## compute the cost of the HGLET coefficients
443
+ costNew = costfun (dmatrix[indr,j])
444
+ # change the best basis if the new cost is less expensive
445
+ if costBB >= costNew - tol
446
+ costBB, dvec[indr], levlist[indr] = BBchange (costNew, dmatrix[indr,j], j)
447
+ end
448
+ end
449
+ end
450
+
451
+ levlist = collect (enumerate (levlist))
452
+
453
+ BS = BasisSpec (levlist, c2f = true , description = " HGLET Best Basis" )
454
+ # levlist2levlengths!(GP, BS)
455
+
456
+ # if we flattened dmatrix, then "unflatten" the expansion coefficients
457
+ if fcols > 1
458
+ dvec = dmatrix2dvec (dmatrix0, GP, BS)
459
+ end
460
+
461
+ return dvec, BS
462
+ end
463
+
380
464
"""
381
465
function HGLET_GHWT_BestBasis(GP::GraphPart;
382
466
dmatrixH::Array{Float64,3} = Array{Float64,3}(undef,0,0,0),
383
467
dmatrixHrw::Array{Float64,3} = Array{Float64,3}(undef,0,0,0),
384
468
dmatrixHsym::Array{Float64,3} = Array{Float64,3}(undef,0,0,0),
385
469
dmatrixG::Array{Float64,3} = Array{Float64,3}(undef,0,0,0),
386
- costfun ::Any = 0.1,flatten::Any = 1)
470
+ cfspec ::Any = 0.1,flatten::Any = 1)
387
471
388
472
Select the best basis from several matrices of expansion coefficients
389
473
@@ -393,12 +477,17 @@ function HGLET_GHWT_BestBasis(GP::GraphPart;
393
477
* `dmatrixHsym`: the matrix of HGLET expansion coefficients ==> eigenvectors of Lsym
394
478
* `dmatrixG`: the matrix of GHWT expansion coefficients
395
479
* `GP`: a GraphPart object
396
- * `costfun`: the cost functional to be used
397
- * `flatten`: the method for flattening vector-valued data to scalar-valued data
480
+ * `cfspec`: the cost functional specification to be used: see utils.jl
481
+ for the detail. If it's a number, say, p, then the l^p norm is used.
482
+ If it's a function, then that function is used. Default is 0.1, i.e., l^0.1
483
+ * `flatten`: the method for flattening vector-valued data to scalar-valued data;
484
+ If it's a number, say, p, then the sum of the l^p norm is computed.
485
+ There are many other options, such as :ash, :entropy, etc. See utils.jl
486
+ for the detail. Default is 1, i.e, the sum of the l^1 norm of the coefs.
398
487
399
488
### Output Argument
400
489
* `dvec`: the vector of expansion coefficients corresponding to the bestbasis
401
- * `BS`: a BasisSpec object which specifies the best- basis
490
+ * `BS`: a BasisSpec object which specifies the best basis
402
491
* `trans`: specifies which transform was used for that portion of the signal:
403
492
00 = HGLET with L
404
493
01 = HGLET with Lrw
@@ -410,15 +499,15 @@ function HGLET_GHWT_BestBasis(GP::GraphPart;
410
499
dmatrixHrw:: Array{Float64,3} = Array {Float64,3} (undef,0 ,0 ,0 ),
411
500
dmatrixHsym:: Array{Float64,3} = Array {Float64,3} (undef,0 ,0 ,0 ),
412
501
dmatrixG:: Array{Float64,3} = Array {Float64,3} (undef,0 ,0 ,0 ),
413
- costfun :: Any = 0.1 ,flatten:: Any = 1 )
502
+ cfspec :: Any = 0.1 ,flatten:: Any = 1 )
414
503
# specify transform codes
415
504
transHsym = [true false ]
416
505
transG = [true true ]
417
506
transHrw = [false true ]
418
507
transH = [false false ]
419
508
420
509
# the cost functional to be used
421
- costfun = cost_functional (costfun )
510
+ costfun = cost_functional (cfspec )
422
511
423
512
# constants and dmatrix cleanup
424
513
if ! isempty (dmatrixHsym)
@@ -442,23 +531,23 @@ function HGLET_GHWT_BestBasis(GP::GraphPart;
442
531
if fcols > 1
443
532
if ! isempty (dmatrixHsym)
444
533
dmatrix0Hsym = deepcopy (dmatrixHsym)
445
- dmatrixdHsym = dropdims (dmatrix_flatten (dmatrixHsym,flatten),dims = 1 )
534
+ dmatrixHsym = dropdims (dmatrix_flatten (dmatrixHsym,flatten),dims = 3 )
446
535
end
447
536
if ! isempty (dmatrixG)
448
537
dmatrix0G = deepcopy (dmatrixG)
449
- dmatrixG = dropdims (dmatrix_flatten (dmatrixG,flatten),dims = 1 )
538
+ dmatrixG = dropdims (dmatrix_flatten (dmatrixG,flatten),dims = 3 )
450
539
end
451
540
if ! isempty (dmatrixHrw)
452
541
dmatrix0Hrw = deepcopy (dmatrixHrw)
453
- dmatrixHrw = dropdims (dmatrix_flatten (dmatrixHrw,flatten),dims = 1 )
542
+ dmatrixHrw = dropdims (dmatrix_flatten (dmatrixHrw,flatten),dims = 3 )
454
543
end
455
544
if ! isempty (dmatrixH)
456
545
dmatrix0H = deepcopy (dmatrixH)
457
- dmatrixH = dropdims (dmatrix_flatten (dmatrixH,flatten),dims = 1 )
546
+ dmatrixH = dropdims (dmatrix_flatten (dmatrixH,flatten),dims = 3 )
458
547
end
459
548
end
460
549
461
- # Find the HGLET/GHWT best- basis
550
+ # Find the HGLET/GHWT best basis
462
551
463
552
# allocate/initialize ==> order matters here
464
553
if ! isempty (dmatrixHsym)
@@ -508,7 +597,7 @@ function HGLET_GHWT_BestBasis(GP::GraphPart;
508
597
end
509
598
end
510
599
511
- # ## compute the cost of the GHWT coefficients
600
+ # ## compute the cost of the HGLET-Lrw coefficients
512
601
if ! isempty (dmatrixHrw)
513
602
costNew = costfun (dmatrixHrw[indr,j])
514
603
# change the best basis if the new cost is less expensive
@@ -569,6 +658,16 @@ return dvec, BS, transfull
569
658
570
659
end
571
660
661
+ function BBchange (costNew:: Float64 , dvec:: Vector{Float64} , j:: Int )
662
+ # change to the new best basis
663
+ costBB = costNew
664
+
665
+ n = length (dvec)
666
+
667
+ levlist = fill (j, n)
668
+
669
+ return costBB, dvec, levlist
670
+ end
572
671
573
672
function BBchange (costNew:: Float64 , dvec:: Vector{Float64} , j:: Int , trans:: Array{Bool,2} )
574
673
# change to the new best basis
@@ -619,8 +718,8 @@ function HGLET_GHWT_Synthesis(dvec::Matrix{Float64},GP::GraphPart,BS::BasisSpec,
619
718
# Synthesize using the transforms separately
620
719
621
720
fH,_ = HGLET_Synthesis (dvecH, GP, BS, G)
622
- fHrw,_ = HGLET_Synthesis (dvecHrw, GP, BS, G, method = :Lrw )
623
- fHsym,_ = HGLET_Synthesis (dvecHsym, GP, BS, G, method = :Lsym )
721
+ fHrw,_ = HGLET_Synthesis (dvecHrw, GP, BS, G, gltype = :Lrw )
722
+ fHsym,_ = HGLET_Synthesis (dvecHsym, GP, BS, G, gltype = :Lsym )
624
723
fG = ghwt_synthesis (dvecG, GP, BS)
625
724
626
725
f = fH + fHrw + fHsym + fG
@@ -656,7 +755,7 @@ function HGLET_GHWT_BestBasis_minrelerror(GP::GraphPart, G::GraphSig;
656
755
657
756
### Output argument:
658
757
* `dvec`: the vector of expansion coefficients corresponding to the bestbasis
659
- * `BS`: a BasisSpec object which specifies the best- basis
758
+ * `BS`: a BasisSpec object which specifies the best basis
660
759
* `trans`: specifies which transform was used for that portion of the signal
661
760
00 = HGLET with L
662
761
01 = HGLET with Lrw
@@ -684,7 +783,7 @@ function HGLET_GHWT_BestBasis_minrelerror(GP::GraphPart, G::GraphSig;
684
783
dvec_temp, BS_temp, trans_temp = HGLET_GHWT_BestBasis (GP,
685
784
dmatrixH = dmatrixH, dmatrixG = dmatrixG,
686
785
dmatrixHrw = dmatrixHrw, dmatrixHsym = dmatrixHsym,
687
- costfun = tau_temp)
786
+ cfspec = tau_temp)
688
787
689
788
# check whether any HGLET Lrw basis vectors are in the best basis
690
789
orthbasis = true
0 commit comments