Skip to content

Commit bc14081

Browse files
committed
add figure scripts of NGWP JFAA paper and rearrange paperscripts folder
1 parent 236eb74 commit bc14081

19 files changed

+3909
-174
lines changed
Binary file not shown.
39.7 MB
Binary file not shown.

test/paperscripts/NGWP_JFAA2021/datasets/new_toronto_graph.lgz

Lines changed: 3382 additions & 0 deletions
Large diffs are not rendered by default.
83.8 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# script for Fig.1, Fig.2, Fig.4, Fig.6
2+
3+
using MultiscaleGraphSignalTransforms, LightGraphs, Plots, LaTeXStrings, MultivariateStats
4+
pyplot(dpi = 200)
5+
6+
Nx, Ny = 7, 3
7+
G = LightGraphs.grid([Nx, Ny]); N = nv(G);
8+
L = Matrix(laplacian_matrix(G))
9+
Q = incidence_matrix(G; oriented = true)
10+
𝛌, 𝚽 = eigen(L); 𝚽 = 𝚽 .* sign.(𝚽[1, :])';
11+
12+
#################### Fig. 1(a) eigenvectors by nondecreasing eigenvalue ordering
13+
plot(layout = Plots.grid(3, 7))
14+
for i in 1:N
15+
heatmap!(reshape(𝚽[:, i],(Nx, Ny))', c = :viridis, cbar = false,
16+
clims = (-0.4,0.4), frame = :none, ratio = 1,
17+
title = latexstring("\\phi_{", i-1, "}"), titlefont = 12,
18+
subplot = i)
19+
end
20+
plt = current()
21+
# savefig(plt, joinpath(@__DIR__, "../paperfigs/grid7x3_evsp_title.png"))
22+
23+
#################### Fig. 1(b) eigenvectors by natural frequency ordering
24+
# find correct 2D index
25+
grid2eig_ind = [1,2,3,6,8,12,15,4,5,7,9,13,16,18,10,11,14,17,19,20,21];
26+
eig2grid_ind = sortperm(grid2eig_ind);
27+
eig2dct = Array{Int64,3}(undef, Nx, Ny, 2);
28+
for i = 1:Nx; for j = 1:Ny; eig2dct[i,j,1] = i-1; eig2dct[i,j,2] = j-1; end; end
29+
eig2dct = reshape(eig2dct, (N, 2)); eig2dct = eig2dct[eig2grid_ind, :];
30+
31+
plot(layout = Plots.grid(3, 7))
32+
for i in 1:N
33+
k = grid2eig_ind[i]
34+
heatmap!(reshape(𝚽[:,k],(Nx,Ny))', c = :viridis, cbar = false,
35+
clims = (-0.4,0.4), frame = :none, ratio = 1,
36+
title = latexstring("\\varphi_{", string(eig2dct[k,1]),
37+
",", string(eig2dct[k,2]), "}"), titlefont = 12, subplot = i)
38+
end
39+
plt = current()
40+
# savefig(plt, joinpath(@__DIR__, "../paperfigs/grid7x3_dct_title2.png"))
41+
42+
# DAG pseudo-metric
43+
distDAG = eigDAG_Distance(𝚽, Q, N)
44+
45+
# MDS embedding into R²
46+
D = distDAG
47+
E = transform(fit(MDS, D, maxoutdim=2, distances=true))
48+
49+
# set up all heatmap plots' positions
50+
dx = 0.01; dy = dx;
51+
xej = zeros(Nx, N); yej=zeros(Ny, N);
52+
a = 5.0; b = 9.0;
53+
for k = 1:N
54+
xej[:,k] = LinRange(E[1,k] - Ny * a * dx, E[1, k] + Ny * a * dx, Nx)
55+
yej[:,k] = LinRange(E[2,k] - a * dy, E[2, k] + a * dy, Ny)
56+
end
57+
58+
#################### Fig. 2
59+
plot()
60+
for k=1:N
61+
heatmap!(xej[:, k], yej[:, k], reshape(𝚽[:, k], (Nx, Ny))', c = :viridis,
62+
colorbar = false, ratio = 1, annotations = (xej[4, k],
63+
yej[3, k] + b*dy, text(latexstring("\\varphi_{",
64+
string(eig2dct[k, 1]), ",", string(eig2dct[k, 2]), "}"), 10)))
65+
end
66+
plt = plot!(xlim = [-1.4, 1.3], ylim = [-1.4, 1.3], grid = false, clims = (-0.4, 0.4))
67+
# savefig(plt, joinpath(@__DIR__, "../paperfigs/Grid7x3_DAG_MDS.png"))
68+
69+
#################### Fig. 4
70+
# first level partition
71+
p1x = [-0.2, 1.0, NaN]; p1y = [1.3, -1.0, NaN];
72+
plot!(p1x, p1y, c = :red, legend = false, width = 3)
73+
# second level partition
74+
p2x = [-1.0, 0.2, NaN, 0.4, 1.2, NaN]; p2y = [-0.8, 0.45, NaN, 0.25, 0.2, NaN];
75+
plot!(p2x, p2y, c=:orange, legend = false, width = 2)
76+
plt = current()
77+
# savefig(plt, joinpath(@__DIR__, "../paperfigs/Grid7x3_DAG_2levels_partition.png"))
78+
79+
80+
## Build Dual Graph
81+
Gstar_Sig = dualgraph(distDAG)
82+
GP_dual = partition_tree_fiedler(Gstar_Sig; swapRegion = false)
83+
GP_primal = pairclustering(𝚽, GP_dual)
84+
85+
@time VM_NGWP = vm_ngwp(𝚽, GP_dual)
86+
87+
## level 2 VM-NGWP vectors
88+
j = 3; W_VM = VM_NGWP[:, j, :]'
89+
90+
wav_kl = [[0 0];[0 1];[0 2];[1 0];[1 1];[1 2];[1 3];[2 0];[2 1];[2 2];[3 0];
91+
[3 1];[3 2];[3 3];[2 3];[2 4];[2 5];[2 6];[3 4];[3 5];[3 6]];
92+
wav_kl = wav_kl[eig2grid_ind,:];
93+
94+
# reorder_ind = [2,3,1,5,7,4,6, 16,17,15, 9,11,8,10, 18,20,21,19, 13,14,12]
95+
reorder_ind = [1,3,2,5,7,4,6, 9,10,8,16,18,15,17, 11,13,14,12,20,21,19]
96+
W_VM = W_VM[:,reorder_ind[eig2grid_ind]];
97+
sgn = ones(N); sgn[grid2eig_ind[[4,6,8,10,14]]] .= -1; W_VM = W_VM * Diagonal(sgn);
98+
99+
#################### Fig. 6
100+
plot()
101+
for k=1:N
102+
heatmap!(xej[:,k],yej[:,k],reshape(W_VM[:,k],(Nx,Ny))',c=:viridis,colorbar=false,ratio=1,annotations=(xej[4,k], yej[3,k]+b*dy, text(latexstring("\\psi_{", string(wav_kl[k,1]), ",", string(wav_kl[k,2]), "}"),10)))
103+
end
104+
plot!(aspect_ratio = 1, xlim = [-1.4, 1.3], ylim = [-1.4, 1.3], grid = false, clims=(-0.34,0.34))
105+
# first level partition
106+
p1x = [-0.2, 1.0, NaN]; p1y = [1.3, -1.0, NaN]; plot!(p1x, p1y, c = :red, legend = false, width = 3)
107+
# second level partition
108+
p2x = [-1.0, 0.2, NaN, 0.4, 1.2, NaN]; p2y = [-0.8, 0.45, NaN, 0.25, 0.2, NaN]; plot!(p2x, p2y, c=:orange, legend = false, width = 2)
109+
plt = current()
110+
# savefig(plt, joinpath(@__DIR__, "../paperfigs/Grid7x3_DAG_VM_NGWP_lvl2_wavelets.png"))
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# script for Fig.5
2+
3+
using MultiscaleGraphSignalTransforms, LightGraphs, Plots; gr(dpi = 200)
4+
import WaveletsExt: wiggle
5+
6+
## Build Graph
7+
N = 512; G = path_graph(N)
8+
X = zeros(N,2); X[:, 1] = 1:N
9+
L = Matrix(laplacian_matrix(G))
10+
𝛌, 𝚽 = eigen(L); 𝚽 = 𝚽 .* sign.(𝚽[1,:])'
11+
W = 1.0 * adjacency_matrix(G)
12+
13+
## Build NGWPs
14+
Gstar_Sig = GraphSig(W)
15+
G_Sig = GraphSig(W, xy = X)
16+
GP_dual = partition_tree_fiedler(Gstar_Sig; swapRegion = false)
17+
GP_primal = pairclustering(𝚽, GP_dual)
18+
19+
@time VM_NGWP = vm_ngwp(𝚽, GP_dual)
20+
21+
#################### Fig.5
22+
j = 5
23+
for k in [1, 2, 5]
24+
WW = sort_wavelets(VM_NGWP[GP_dual.rs[k, j]:(GP_dual.rs[k + 1, j] - 1), j, :]')
25+
if k == 2
26+
WW[:, end] *= -1
27+
end
28+
plt = wiggle(WW; sc = 0.75)
29+
# savefig(plt, joinpath(@__DIR__, "../paperfigs/Path512_VM_NGWP_j$(j-1)k$(k-1).png"))
30+
end
31+
current()
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# script for Fig.8(b)(c), Fig.9, Fig.10(b)(c), Fig.11
2+
3+
using MultiscaleGraphSignalTransforms, LightGraphs, Plots; gr(dpi = 200)
4+
5+
## Build weighted graph
6+
G, L, X = SunFlowerGraph(N = 400); N = nv(G)
7+
𝛌, 𝚽 = eigen(Matrix(L))
8+
sgn = (maximum(𝚽, dims = 1)[:] .> -minimum(𝚽, dims = 1)[:]) .* 2 .- 1
9+
𝚽 = 𝚽 * Diagonal(sgn)
10+
Q = incidence_matrix(G; oriented = true)
11+
W = 1.0 * adjacency_matrix(G)
12+
edge_weight = [e.weight for e in edges(G)]
13+
14+
## Build Dual Graph by DAG metric
15+
distDAG = eigDAG_Distance(𝚽, Q, N; edge_weight = edge_weight)
16+
Gstar_Sig = dualgraph(distDAG)
17+
G_Sig = GraphSig(W, xy = X)
18+
GP_dual = partition_tree_fiedler(Gstar_Sig; swapRegion = false)
19+
GP_primal = pairclustering(𝚽, GP_dual)
20+
21+
@time VM_NGWP = vm_ngwp(𝚽, GP_dual) #54.986524 seconds (1.19 M allocations: 25.127 GiB, 2.36% gc time)
22+
@time PC_NGWP = pc_ngwp(𝚽, GP_dual, GP_primal) #0.611176 seconds (225.41 k allocations: 844.488 MiB, 11.71% gc time)
23+
24+
25+
#################### Fig. 8(b) barbara eye graph signal
26+
using MAT
27+
f = matread(joinpath(@__DIR__, "../datasets",
28+
"sunflower_barbara_voronoi.mat"))["f_eye_voronoi"]
29+
G_Sig.f = reshape(f, (N, 1))
30+
scatter_gplot(X; marker = f, ms = LinRange(4.0, 14.0, N), c = :greys);
31+
plt = plot!(xlim = [-1.2,1.2], ylim = [-1.2,1.2], frame = :none)
32+
# savefig(plt, joinpath(@__DIR__, "../paperfigs/SunFlower_barbara_feye.png"))
33+
34+
#################### Fig. 8(c) barbara eye relative l2 approximation error by various methods
35+
DVEC = getall_expansioncoeffs(G_Sig, GP_dual, VM_NGWP, PC_NGWP, 𝚽)
36+
approx_error_plot(DVEC);
37+
plt = plot!(xguidefontsize = 16, yguidefontsize = 16, legendfontsize = 12)
38+
# savefig(plt, joinpath(@__DIR__, "../paperfigs/SunFlower_barbara_feye_DAG_approx.png"))
39+
40+
#################### Fig. 9 barbara eye 16 most important VM-NGWP vectors (ignore the DC vector)
41+
dmatrix_VM = ngwp_analysis(G_Sig, VM_NGWP)
42+
dvec_vm_ngwp, BS_vm_ngwp = ngwp_bestbasis(dmatrix_VM, GP_dual)
43+
important_idx = sortperm(dvec_vm_ngwp[:].^2; rev = true)
44+
for i in 2:17
45+
dr, dc = BS_vm_ngwp.levlist[important_idx[i]]
46+
w = VM_NGWP[dr, dc, :]
47+
println("(j, k, l) = ", NGWP_jkl(GP_dual, dr, dc))
48+
scatter_gplot(X; marker = w, ms = LinRange(4.0, 14.0, N), c = :greys)
49+
plt = plot!(xlim = [-1.2, 1.2], ylim = [-1.2, 1.2], frame = :none,
50+
cbar = false, clims = (-0.15, 0.15))
51+
# savefig(plt, joinpath(@__DIR__,
52+
# "../paperfigs/SunFlower_barbara_feye_DAG_VM_NGW_important_basis_vector$(lpad(i,2,"0")).png"))
53+
end
54+
55+
#################### Fig. 10(b) barbara pants graph signal
56+
f = matread(joinpath(@__DIR__, "../datasets",
57+
"sunflower_barbara_voronoi.mat"))["f_trouser_voronoi"]
58+
scatter_gplot(X; marker = f, ms = LinRange(4.0, 14.0, N), c = :greys);
59+
plt = plot!(xlim = [-1.2,1.2], ylim = [-1.2,1.2], frame = :none)
60+
# savefig(plt, joinpath(@__DIR__, "../paperfigs/SunFlower_barbara_ftrouser.png"))
61+
62+
#################### Fig. 10(c) barbara eye relative l2 approximation error by various methods
63+
G_Sig.f = reshape(f, (N, 1))
64+
DVEC = getall_expansioncoeffs(G_Sig, GP_dual, VM_NGWP, PC_NGWP, 𝚽)
65+
approx_error_plot(DVEC);
66+
plt = plot!(xguidefontsize = 16, yguidefontsize = 16, legendfontsize = 12)
67+
# savefig(plt, joinpath(@__DIR__, "../paperfigs/SunFlower_barbara_ftrouser_DAG_approx.png"))
68+
69+
#################### Fig. 11 barbara pants 16 most important VM-NGWP vectors (ignore the DC vector)
70+
dmatrix_VM = ngwp_analysis(G_Sig, VM_NGWP)
71+
dvec_vm_ngwp, BS_vm_ngwp = ngwp_bestbasis(dmatrix_VM, GP_dual)
72+
important_idx = sortperm(dvec_vm_ngwp[:].^2; rev = true)
73+
for i in 2:17
74+
dr, dc = BS_vm_ngwp.levlist[important_idx[i]]
75+
w = VM_NGWP[dr, dc, :]
76+
println("(j, k, l) = ", NGWP_jkl(GP_dual, dr, dc))
77+
scatter_gplot(X; marker = w, ms = LinRange(4.0, 14.0, N), c = :greys)
78+
plt = plot!(xlim = [-1.2, 1.2], ylim = [-1.2, 1.2], frame = :none,
79+
cbar = false, clims = (-0.15, 0.15))
80+
# savefig(plt, joinpath(@__DIR__,
81+
# "../paperfigs/SunFlower_barbara_ftrouser_DAG_VM_NGW_important_basis_vector$(lpad(i,2,"0")).png"))
82+
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## script for Fig.7, Fig.8(a), Fig.10(a)
2+
using VoronoiDelaunay, VoronoiCells, GeometricalPredicates
3+
using MultiscaleGraphSignalTransforms, Plots, LightGraphs, JLD; gr(dpi = 200)
4+
5+
barbara = JLD.load(joinpath(@__DIR__, "..", "datasets", "barbara_gray_matrix.jld"), "barbara")
6+
G, L, X = SunFlowerGraph(); N = nv(G)
7+
#################### Fig. 7(a) sunflower graph
8+
gplot(1.0*adjacency_matrix(G),X; width=1); scatter_gplot!(X; c = :red, ms = LinRange(1,9,N)); plt = plot!(frame = :none)
9+
# savefig(plt, joinpath(@__DIR__, "../paperfigs/SunFlower.png"))
10+
11+
## Voronoi tessellation
12+
width_x = maximum(abs.(X[:,1])) * 2; width_y = maximum(abs.(X[:,2])) * 2;
13+
width = VoronoiDelaunay.max_coord - VoronoiDelaunay.min_coord
14+
center_coord = (VoronoiDelaunay.min_coord + VoronoiDelaunay.max_coord)/2
15+
X_transform = zeros(N,2)
16+
for i in 1:N
17+
X_transform[i,:] = X[i,:] ./ [width_x/width, width_y/width] + [center_coord, center_coord]
18+
end
19+
20+
pts = [Point2D(X_transform[i,1], X_transform[i,2]) for i in 1:N]
21+
tess = DelaunayTessellation(N)
22+
push!(tess, pts)
23+
#################### Fig. 7(b) voronoi tessellation
24+
xx, yy = getplotxy(voronoiedges(tess))
25+
plt = plot(xx, yy, xlim=[1,2], ylim=[1,2], linestyle=:auto, linewidth=1, linecolor=:blue, grid=false, label="", aspect_ratio=1, frame=:box)
26+
# savefig(plt, joinpath(@__DIR__, "../paperfigs/Sunflower_Barbara_Voronoi_cells.png"))
27+
28+
#################### Fig. 8(a) barbara sunflower eye
29+
heatmap(barbara, yflip=true, ratio=1, c=:greys); scatter_gplot!(transform2D(X; s = 20, t = [395, 100]); ms = 2, c = :red); sample_location_plt = plot!(cbar = false, frame = :none)
30+
# savefig(sample_location_plt, joinpath(@__DIR__, "../paperfigs/barb_sunflower_eye.png"))
31+
32+
#################### Fig. 10(a) barbara sunflower pants
33+
heatmap(barbara, yflip=true, ratio=1, c=:greys); scatter_gplot!(transform2D(X; s = 20, t = [280, 320]); ms = 2, c = :red); sample_location_plt = plot!(cbar = false, frame = :none)
34+
# savefig(sample_location_plt, joinpath(@__DIR__, "../paperfigs/barb_sunflower_pants.png"))

0 commit comments

Comments
 (0)