Skip to content

Commit 13fd242

Browse files
Updated HGLET.jl and other files with various improvements
1 parent 7aa2737 commit 13fd242

File tree

5 files changed

+124
-102
lines changed

5 files changed

+124
-102
lines changed

src/HGLET.jl

Lines changed: 113 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function HGLET_Synthesis(dvec::Matrix{Float64}, GP::GraphPart, BS::BasisSpec,
3737

3838
# Perform the signal synthesis from the given coefficients
3939
for j = jmax:-1:1
40-
regioncount = count(GP.rs[:,j] .!= 0) - 1
40+
regioncount = count(!iszero, GP.rs[:,j]) - 1
4141
for r = 1:regioncount
4242
# the index that marks the start of the region
4343
rs1 = GP.rs[r,j]
@@ -49,7 +49,7 @@ function HGLET_Synthesis(dvec::Matrix{Float64}, GP::GraphPart, BS::BasisSpec,
4949
n = rs3 - rs1
5050

5151
# only proceed forward if coefficients do not exist
52-
if (j == jmax || count(dmatrix[rs1:rs3-1,j+1,:] .!= 0) == 0) && count(dmatrix[rs1:rs3-1,j,:] .!= 0) > 0
52+
if (j == jmax || count(!iszero, dmatrix[rs1:rs3-1,j+1,:]) == 0) && count(!iszero, dmatrix[rs1:rs3-1,j,:]) > 0
5353

5454
if n == 1
5555
f[rs1,:] = dmatrix[rs1,j,:]
@@ -116,9 +116,9 @@ function HGLET_jkl(GP::GraphPart, drow::Int, dcol::Int)
116116
the coefficient dmatrix(drow,dcol)
117117
118118
### Input Arguments
119-
* `GP`: a GraphPart object
120-
* `drow`: the row of the expansion coefficient
121-
* `dcol`: the column of the expansion coefficient
119+
* `GP`: a GraphPart object
120+
* `drow`: the row of the expansion coefficient
121+
* `dcol`: the column of the expansion coefficient
122122
123123
### Output Argument
124124
* `j`: the level index of the expansion coefficient
@@ -164,7 +164,7 @@ function HGLET_Analysis(G::GraphSig, GP::GraphPart, method::Symbol = :L)
164164

165165
# Perform the HGLET analysis, i.e., generating the HGLET coefficients
166166
for j = jmax-1:-1:1
167-
regioncount = count(rs[:,j] .!= 0) - 1
167+
regioncount = count(!iszero, rs[:,j]) - 1
168168
for r = 1:regioncount
169169
# the index that marks the start of the region
170170
rs1 = rs[r,j]
@@ -228,7 +228,8 @@ end
228228
"""
229229
function HGLET_Analysis_All(G::GraphSig, GP::GraphPart)
230230
231-
For a GraphSig object 'G', generate the 3 matrices of HGLET expansion coefficients corresponding to the eigenvectors of L, Lrw and Lsym
231+
For a GraphSig object 'G', generate the 3 matrices of HGLET expansion
232+
coefficients corresponding to the eigenvectors of L, Lrw and Lsym
232233
233234
### Input Arguments
234235
* `G`: a GraphSig object
@@ -255,7 +256,7 @@ function HGLET_Analysis_All(G::GraphSig, GP::GraphPart)
255256

256257
# Perform the transform ==> eigenvectors of L
257258
for j = jmax-1:-1:1
258-
regioncount = count(rs[:,j] .!= 0) - 1
259+
regioncount = count(!iszero, rs[:,j]) - 1
259260
for r = 1:regioncount
260261
# the index that marks the start of the region
261262
rs1 = rs[r,j]
@@ -273,7 +274,7 @@ function HGLET_Analysis_All(G::GraphSig, GP::GraphPart)
273274
indrs = ind[rs1:rs3-1]
274275

275276
# compute the eigenvectors of L ==> svd(L)
276-
v,_,_ = svd(diagm(0 => dropdims(sum(W[indrs,indrs],dims = 1),dims = 1))-W[indrs,indrs])
277+
v,_,_ = svd(Diagonal(vec(sum(W[indrs,indrs],dims = 1)))-W[indrs,indrs])
277278
v = v[:,end:-1:1]
278279

279280
# standardize the eigenvector signs
@@ -286,7 +287,7 @@ function HGLET_Analysis_All(G::GraphSig, GP::GraphPart)
286287
elseif v[row,col] < -10^3*eps()
287288
v[:,col] = - v[:,col]
288289
else
289-
row = row + 1
290+
row += 1
290291
end
291292
end
292293
end
@@ -298,7 +299,7 @@ function HGLET_Analysis_All(G::GraphSig, GP::GraphPart)
298299
end
299300

300301
for j = jmax-1:-1:1
301-
regioncount = count(rs[:,j] .!= 0)-1
302+
regioncount = count(!iszero, rs[:,j])-1
302303
for r = 1:regioncount
303304
# the index that marks the start of the region
304305
rs1 = rs[r,j]
@@ -323,18 +324,18 @@ function HGLET_Analysis_All(G::GraphSig, GP::GraphPart)
323324

324325
### eigenvectors of L_rw ==> svd(L_sym)
325326
W_temp = W[indrs,indrs]
326-
D_temp = sparse(Diagonal(dropdims(sum(W_temp,dims = 1), dims = 1)))
327-
D_temp_p = sparse(Diagonal(dropdims(sum(W_temp,dims = 1),dims = 1).^(-1/2)))
328-
v,_,_ = svd(Matrix(D_temp_p*(D_temp - W_temp)*D_temp_p))
327+
D = Diagonal(vec(sum(W_temp,dims = 1)))
328+
D2 = D.^(-0.5)
329+
v,_,_ = svd(D2 * (D - W_temp) * D2)
329330
v = v[:,end:-1:1]
330331

331332
else
332333
useLrw = false
333334

334335
### eigenvectors of L ==> svd(L)
335336
W_temp = W[indrs,indrs]
336-
D_temp = sparse(Diagonal(dropdims(sum(W_temp,dims = 1), dims = 1)))
337-
v,_,_ = svd(Matrix(D_temp-W_temp))
337+
D = Diagonal(vec(sum(W_temp,dims = 1)))
338+
v,_,_ = svd(D-W_temp)
338339
v = v[:,end:-1:1]
339340
end
340341

@@ -348,7 +349,7 @@ function HGLET_Analysis_All(G::GraphSig, GP::GraphPart)
348349
elseif v[row,col] < -10^3*eps()
349350
v[:,col] = - v[:,col]
350351
else
351-
row = row + 1
352+
row += 1
352353
end
353354
end
354355
end
@@ -358,7 +359,7 @@ function HGLET_Analysis_All(G::GraphSig, GP::GraphPart)
358359

359360
# obtain the expansion coeffcients for L_rw
360361
if useLrw
361-
dmatrixHrw[rs1:rs3-1,j,:] = v'*Diagonal(dropdims(sum(W_temp,dims = 1),dims = 1).^(1/2))*G.f[indrs,:]
362+
dmatrixHrw[rs1:rs3-1,j,:] = v'*(Diagonal(vec(sum(W_temp,dims = 1))).^0.5)*G.f[indrs,:]
362363
else
363364
dmatrixHrw[rs1:rs3-1,j,:] = v'*G.f[indrs,:]
364365
end
@@ -373,31 +374,39 @@ end
373374

374375

375376
"""
376-
function HGLET_GHWT_BestBasis(GP::GraphPart; dmatrixH::Array{Float64,3} = Array{Float64,3}(undef,0,0,0),dmatrixHrw::Array{Float64,3} = Array{Float64,3}(undef,0,0,0)
377-
,dmatrixHsym::Array{Float64,3} = Array{Float64,3}(undef,0,0,0),dmatrixG::Array{Float64,3} = Array{Float64,3}(undef,0,0,0), costfun::Any = 0.1,flatten::Any = 1)
378-
379-
Select the best basis from several matrices of expansion coefficients
380-
381-
### Input Arguments
382-
* `dmatrixH`: the matrix of HGLET expansion coefficients ==> eigenvectors of L
383-
* `dmatrixHrw`: the matrix of HGLET expansion coefficients ==> eigenvectors of Lrw
384-
* `dmatrixHsym`: the matrix of HGLET expansion coefficients ==> eigenvectors of Lsym
385-
* `dmatrixG`: the matrix of GHWT expansion coefficients
386-
* `GP`: a GraphPart object
387-
* `costfun`: the cost functional to be used
388-
* `flatten`: the method for flattening vector-valued data to scalar-valued data
389-
390-
### Output Argument
391-
* `dvec`: the vector of expansion coefficients corresponding to the bestbasis
392-
* `BS`: a BasisSpec object which specifies the best-basis
393-
* `trans`: specifies which transform was used for that portion of the signal:
394-
00 = HGLET with L
395-
01 = HGLET with Lrw
396-
10 = HGLET with Lsym
397-
11 = GHWT
398-
"""
399-
function HGLET_GHWT_BestBasis(GP::GraphPart; dmatrixH::Array{Float64,3} = Array{Float64,3}(undef,0,0,0),dmatrixHrw::Array{Float64,3} = Array{Float64,3}(undef,0,0,0)
400-
,dmatrixHsym::Array{Float64,3} = Array{Float64,3}(undef,0,0,0),dmatrixG::Array{Float64,3} = Array{Float64,3}(undef,0,0,0), costfun::Any = 0.1,flatten::Any = 1)
377+
function HGLET_GHWT_BestBasis(GP::GraphPart;
378+
dmatrixH::Array{Float64,3} = Array{Float64,3}(undef,0,0,0),
379+
dmatrixHrw::Array{Float64,3} = Array{Float64,3}(undef,0,0,0),
380+
dmatrixHsym::Array{Float64,3} = Array{Float64,3}(undef,0,0,0),
381+
dmatrixG::Array{Float64,3} = Array{Float64,3}(undef,0,0,0),
382+
costfun::Any = 0.1,flatten::Any = 1)
383+
384+
Select the best basis from several matrices of expansion coefficients
385+
386+
### Input Arguments
387+
* `dmatrixH`: the matrix of HGLET expansion coefficients ==> eigenvectors of L
388+
* `dmatrixHrw`: the matrix of HGLET expansion coefficients ==> eigenvectors of Lrw
389+
* `dmatrixHsym`: the matrix of HGLET expansion coefficients ==> eigenvectors of Lsym
390+
* `dmatrixG`: the matrix of GHWT expansion coefficients
391+
* `GP`: a GraphPart object
392+
* `costfun`: the cost functional to be used
393+
* `flatten`: the method for flattening vector-valued data to scalar-valued data
394+
395+
### Output Argument
396+
* `dvec`: the vector of expansion coefficients corresponding to the bestbasis
397+
* `BS`: a BasisSpec object which specifies the best-basis
398+
* `trans`: specifies which transform was used for that portion of the signal:
399+
00 = HGLET with L
400+
01 = HGLET with Lrw
401+
10 = HGLET with Lsym
402+
11 = GHWT
403+
"""
404+
function HGLET_GHWT_BestBasis(GP::GraphPart;
405+
dmatrixH::Array{Float64,3} = Array{Float64,3}(undef,0,0,0),
406+
dmatrixHrw::Array{Float64,3} = Array{Float64,3}(undef,0,0,0),
407+
dmatrixHsym::Array{Float64,3} = Array{Float64,3}(undef,0,0,0),
408+
dmatrixG::Array{Float64,3} = Array{Float64,3}(undef,0,0,0),
409+
costfun::Any = 0.1,flatten::Any = 1)
401410
# specify transform codes
402411
transHsym = [true false]
403412
transG = [true true]
@@ -471,7 +480,7 @@ function HGLET_GHWT_BestBasis(GP::GraphPart; dmatrixH::Array{Float64,3} = Array{
471480

472481
# perform the basis search
473482
for j = jmax:-1:1
474-
regioncount = count(GP.rs[:,j] .!= 0) - 1
483+
regioncount = count(!iszero, GP.rs[:,j]) - 1
475484
for r = 1:regioncount
476485
indr = GP.rs[r,j]:(GP.rs[r+1,j]-1)
477486
### compute the cost of the current best basis
@@ -578,21 +587,21 @@ function HGLET_GHWT_Synthesis(dvec::Matrix{Float64},GP::GraphPart,BS::BasisSpec,
578587
graph partitioning, and the choice of basis and corresponding transforms,
579588
reconstruct the signal.
580589
581-
### Input Arguments
582-
* `dvec`: the expansion coefficients corresponding to the chosen basis
583-
* `GP`: A GraphPart object
584-
* `BS`: A BasisSpec object
585-
* `trans`: A specification of the transforms used for the HGLET-GHWT hybrid transform
586-
00 = HGLET with L
587-
01 = HGLET with Lrw
588-
10 = HGLET with Lsym
589-
11 = GHWT
590-
* `G`: A GraphSig object
591-
592-
### Output Argument
593-
* `f`: the reconstructed signal
594-
* `GS`: the reconstructed GraphSig object
595-
"""
590+
### Input Arguments
591+
* `dvec`: the expansion coefficients corresponding to the chosen basis
592+
* `GP`: a GraphPart object
593+
* `BS`: a BasisSpec object
594+
* `trans`: a specification of the transforms used for the HGLET-GHWT hybrid transform
595+
00 = HGLET with L
596+
01 = HGLET with Lrw
597+
10 = HGLET with Lsym
598+
11 = GHWT
599+
* `G`: a GraphSig object
600+
601+
### Output Argument
602+
* `f`: the reconstructed signal
603+
* `GS`: the reconstructed GraphSig object
604+
"""
596605
function HGLET_GHWT_Synthesis(dvec::Matrix{Float64},GP::GraphPart,BS::BasisSpec,trans::Array{Bool,2},G::GraphSig)
597606
# fill out trans
598607
#_,_,transfull = BSfull(GP,BS,trans)
@@ -610,7 +619,7 @@ function HGLET_GHWT_Synthesis(dvec::Matrix{Float64},GP::GraphPart,BS::BasisSpec,
610619
fHsym,_ = HGLET_Synthesis(dvecHsym, GP, BS, G, method = :Lsym)
611620
fG = ghwt_synthesis(dvecG, GP, BS)
612621

613-
f = fH + fHrw +fHsym + fG
622+
f = fH + fHrw + fHsym + fG
614623

615624
GS = deepcopy(G)
616625
replace_data!(GS,f)
@@ -621,34 +630,44 @@ end
621630

622631

623632
"""
624-
function HGLET_GHWT_BestBasis_minrelerror(GP::GraphPart,G::GraphSig;dmatrixH::Array{Float64,3} = Array{Float64,3}(undef,0,0,0), dmatrixHrw::Array{Float64,3} = Array{Float64,3}(undef,0,0,0),
625-
dmatrixHsym::Array{Float64,3} = Array{Float64,3}(undef,0,0,0),dmatrixG::Array{Float64,3} = Array{Float64,3}(undef,0,0,0), compare::Bool = true)
626-
627-
Find the best basis for approximating the signal 'G' by performing the best basis search with a range of tau-measures
628-
as cost functionals (tau = 0.1,0.2,...,1.9) and minimizing the relative error.
629-
630-
### Input argument:
631-
* `dmatrixH`: the matrix of HGLET expansion coefficients ==> eigenvectors of L
632-
* `dmatrixHrw`: the matrix of HGLET expansion coefficients ==> eigenvectors of Lrw
633-
* `dmatrixHsym`: the matrix of HGLET expansion coefficients ==> eigenvectors of Lsym
634-
* `dmatrixG`: the matrix of GHWT expansion coefficients
635-
* `GP`: a GraphPart object
636-
* `G`: the GraphSig object
637-
* `compare`: if it is false, don't compare the hybrid best basis to the GHWT fine-to-coarse best basis
638-
639-
### Output argument:
640-
* `dvec`: the vector of expansion coefficients corresponding to the bestbasis
641-
* `BS`: a BasisSpec object which specifies the best-basis
642-
* `trans`: specifies which transform was used for that portion of the signal
643-
00 = HGLET with L
644-
01 = HGLET with Lrw
645-
10 = HGLET with Lsym
646-
11 = GHWT
647-
* `tau`: the tau that yields the smallest relative error
648-
649-
"""
650-
function HGLET_GHWT_BestBasis_minrelerror(GP::GraphPart,G::GraphSig;dmatrixH::Array{Float64,3} = Array{Float64,3}(undef,0,0,0), dmatrixHrw::Array{Float64,3} = Array{Float64,3}(undef,0,0,0),
651-
dmatrixHsym::Array{Float64,3} = Array{Float64,3}(undef,0,0,0),dmatrixG::Array{Float64,3} = Array{Float64,3}(undef,0,0,0), compare::Bool = true)
633+
function HGLET_GHWT_BestBasis_minrelerror(GP::GraphPart, G::GraphSig;
634+
dmatrixH::Array{Float64,3} = Array{Float64,3}(undef,0,0,0),
635+
dmatrixHrw::Array{Float64,3} = Array{Float64,3}(undef,0,0,0),
636+
dmatrixHsym::Array{Float64,3} = Array{Float64,3}(undef,0,0,0),
637+
dmatrixG::Array{Float64,3} = Array{Float64,3}(undef,0,0,0),
638+
compare::Bool = true)
639+
640+
Find the best basis for approximating the signal 'G' by performing the
641+
best basis search with a range of tau-measures as cost functionals
642+
(tau = 0.1,0.2,...,1.9) and minimizing the relative error.
643+
644+
### Input argument:
645+
* `dmatrixH`: the matrix of HGLET expansion coefficients ==> eigenvectors of L
646+
* `dmatrixHrw`: the matrix of HGLET expansion coefficients ==> eigenvectors of Lrw
647+
* `dmatrixHsym`: the matrix of HGLET expansion coefficients ==> eigenvectors of Lsym
648+
* `dmatrixG`: the matrix of GHWT expansion coefficients
649+
* `GP`: a GraphPart object
650+
* `G`: the GraphSig object
651+
* `compare`: if it is false, don't compare the hybrid best basis to the GHWT fine-to-coarse best basis
652+
653+
### Output argument:
654+
* `dvec`: the vector of expansion coefficients corresponding to the bestbasis
655+
* `BS`: a BasisSpec object which specifies the best-basis
656+
* `trans`: specifies which transform was used for that portion of the signal
657+
00 = HGLET with L
658+
01 = HGLET with Lrw
659+
10 = HGLET with Lsym
660+
11 = GHWT
661+
* `tau`: the tau that yields the smallest relative error
662+
663+
"""
664+
function HGLET_GHWT_BestBasis_minrelerror(GP::GraphPart, G::GraphSig;
665+
dmatrixH::Array{Float64,3} = Array{Float64,3}(undef,0,0,0),
666+
dmatrixHrw::Array{Float64,3} = Array{Float64,3}(undef,0,0,0),
667+
dmatrixHsym::Array{Float64,3} = Array{Float64,3}(undef,0,0,0),
668+
dmatrixG::Array{Float64,3} = Array{Float64,3}(undef,0,0,0),
669+
compare::Bool = true)
670+
652671
dvec, BS, trans, tau = 0, 0, 0, 0 # predefine
653672
sumrelerror = Inf
654673
for tau_temp = 0.1:0.1:1.9
@@ -658,8 +677,10 @@ function HGLET_GHWT_BestBasis_minrelerror(GP::GraphPart,G::GraphSig;dmatrixH::Ar
658677
trans_temp = trues(length(BS_temp.levlist),2)
659678
orthbasis = true
660679
else
661-
dvec_temp, BS_temp, trans_temp = HGLET_GHWT_BestBasis(GP, dmatrixH = dmatrixH, dmatrixG = dmatrixG,
662-
dmatrixHrw = dmatrixHrw, dmatrixHsym = dmatrixHsym, costfun = tau_temp)
680+
dvec_temp, BS_temp, trans_temp = HGLET_GHWT_BestBasis(GP,
681+
dmatrixH = dmatrixH, dmatrixG = dmatrixG,
682+
dmatrixHrw = dmatrixHrw, dmatrixHsym = dmatrixHsym,
683+
costfun = tau_temp)
663684

664685
# check whether any HGLET Lrw basis vectors are in the best basis
665686
orthbasis = true
@@ -676,7 +697,8 @@ function HGLET_GHWT_BestBasis_minrelerror(GP::GraphPart,G::GraphSig;dmatrixH::Ar
676697
if orthbasis
677698
relerror_temp = orth2relerror(dvec_temp[:])
678699
else
679-
B,_ = HGLET_GHWT_Synthesis(Matrix{Float64}(I,length(dvec_temp),length(dvec_temp)),GP,BS_temp,trans_temp,G)
700+
B,_ = HGLET_GHWT_Synthesis(Matrix{Float64}(I,length(dvec_temp),length(dvec_temp)),
701+
GP, BS_temp, trans_temp, G)
680702
relerror_temp = nonorth2relerror(dvec_temp[:],B)
681703
end
682704
sumrelerror_temp = sum(relerror_temp)

src/LP-NGWP.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ function Lrw_eigenvec(W; nev = 6)
3434
# return round.(vtmp; digits = 14)
3535
deg = sum(W, dims = 1)[:] # weighted degree vector
3636
if minimum(deg) <= 10^3 * eps()
37-
L = diagm(deg) - W
37+
L = Diagonal(deg) - W
3838
𝛌, 𝚽 = eigen(L)
3939
standardize_eigenvector_signs!(𝚽)
4040
return round.(𝚽[:, 2:nev]; digits = 14)
4141
end
42-
Lsym = diagm(deg.^(-1/2)) * (diagm(deg) - W) * diagm(deg.^(-1/2))
42+
Lsym = Diagonal(deg.^(-1/2)) * (Diagonal(deg) - W) * Diagonal(deg.^(-1/2))
4343
𝛌sym, 𝚽sym = eigen(Lsym)
4444
𝚽rw = Diagonal(deg.^(-1/2)) * 𝚽sym
4545
𝚽rw ./= sqrt.(sum(𝚽rw.^2; dims = 1))

src/common.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -447,17 +447,17 @@ function dvec_Threshold(dvec::Array{Float64,2}, SORH::String, keep::Float64,
447447
dvec_new = sign.(dvec).*(abs.(dvec).-T).*(abs.(dvec).-T.>0)
448448
end
449449

450-
kept = count(dvec_new.!=0)
450+
kept = count(!iszero, dvec_new)
451451

452452
elseif SORH == "h" || SORH == "hard"
453453
ind = sortperm(abs.(dvec[:]), rev = true)
454454
dvec_new[ind[kept+1:end]] .= 0
455455

456-
kept = count(dvec_new.!=0)
456+
kept = count(!iszero, dvec_new)
457457

458458
else
459459

460-
kept = count(dvec_new.!=0)
460+
kept = count(!iszero, dvec_new)
461461

462462
end
463463
return dvec_new, kept

0 commit comments

Comments
 (0)