|
| 1 | +################################################################################ |
| 2 | +####################### Approximation error plot ############################### |
| 3 | +################################################################################ |
| 4 | +function approx_error2(DVEC::Array{Array{Float64,1},1}, |
| 5 | + T::Vector{String}, L::Vector{Tuple{Symbol,Symbol}}, |
| 6 | + frac::Float64 = 0.30) |
| 7 | + # This version plots the relative L2 errors against |
| 8 | + # the FRACTION of coefficients retained. |
| 9 | + plot(xaxis = "Fraction of Coefficients Retained", |
| 10 | + yaxis = "Relative Approximation Error") |
| 11 | + for i = 1:length(DVEC) |
| 12 | + dvec = DVEC[i] |
| 13 | + N = length(dvec) |
| 14 | + dvec_norm = norm(dvec,2) |
| 15 | + dvec_sort = sort(dvec.^2) # the smallest first |
| 16 | + er = sqrt.(reverse(cumsum(dvec_sort)))/dvec_norm |
| 17 | + er[er .== 0.0] .= minimum(er[er .!= 0.0]) # avoid blowup by taking log in the plot below |
| 18 | + # er is the relative L^2 error of the whole thing: length(er)=N. |
| 19 | + p = Int64(floor(frac*N)) + 1 # upper limit |
| 20 | + plot!(frac*(0:(p-1))/(p-1), er[1:p], yaxis=:log, xlims = (0.,frac), |
| 21 | + label = T[i], line = L[i], linewidth = 2, grid = false) |
| 22 | + end |
| 23 | + display(current()) |
| 24 | +end |
| 25 | + |
| 26 | + |
| 27 | +function approx_error3(DVEC::Array{Array{Float64,1},1}, |
| 28 | + T::Vector{String}, L::Vector{Tuple{Symbol,Symbol}}, |
| 29 | + N::Int64) |
| 30 | + # This version plots the relative L2 errors against |
| 31 | + # the NUMBER of coefficients retained. |
| 32 | + plot(xaxis = "Number of Coefficients Retained", |
| 33 | + yaxis = "Relative Approximation Error") |
| 34 | + for i = 1:length(DVEC) |
| 35 | + dvec = DVEC[i] |
| 36 | + dvec_norm = norm(dvec,2) |
| 37 | + dvec_sort = sort(dvec.^2) # the smallest first |
| 38 | + er = sqrt.(reverse(cumsum(dvec_sort)))/dvec_norm |
| 39 | + er[er .== 0.0] .= minimum(er[er .!= 0.0]) # avoid blowup by taking log in the plot below |
| 40 | + # er is the relative L^2 error of the whole thing: length(er)=N. |
| 41 | + plot!(0:N-1, er[1:N], yaxis=:log, label = T[i], line = L[i], |
| 42 | + linewidth = 2, grid = false) |
| 43 | + end |
| 44 | + display(current()) |
| 45 | +end |
| 46 | + |
| 47 | +################################################################################ |
| 48 | +############ Computing a weight matrix using the Gaussian affinity ############# |
| 49 | +################################################################################ |
| 50 | +function image_Gaussian_affinity(img::Matrix{Float64}, r::Int64, σ::Float64) |
| 51 | +# Get the neighbors for affinity matrix computation |
| 52 | + l = 2*r + 1 |
| 53 | + temp_x, temp_y = fill(1,l^2), fill(1,l^2) |
| 54 | + temp_xy = CartesianIndices((1:l,1:l)) |
| 55 | + for i = 1:l^2 |
| 56 | + temp_x[i], temp_y[i] = temp_xy[i][1], temp_xy[i][2] |
| 57 | + end |
| 58 | + # (r+1, r+1) is the index of the center location. |
| 59 | + temp_ind = ((temp_x .- (r + 1)).^2 + (temp_y .- (r + 1)).^2) .<= r^2 |
| 60 | + # Now, temp_ind indicates those points withinin the circle of radius r |
| 61 | + # from the center while neighbor_x, neighbor_y represent relative positions |
| 62 | + # of those points from the center. |
| 63 | + neighbor_x = temp_x[temp_ind] .- (r + 1) |
| 64 | + neighbor_y = temp_y[temp_ind] .- (r + 1) |
| 65 | + # So, for any index (x, y), points within (x ± neighbor_x, y ± neighbor_y) are |
| 66 | + # its neighbors for the purpose of calculating affinity |
| 67 | + |
| 68 | +# Create affinity matrix |
| 69 | + m, n = size(img) |
| 70 | + sig = img[:] |
| 71 | + W = fill(0., (m*n,m*n)) |
| 72 | + for i = 1:m*n |
| 73 | + cur = CartesianIndices((m,n))[i] |
| 74 | + for j = 1:length(neighbor_x) # assuming dim(neighbor_x) == dim(neighbor_y) |
| 75 | + if 1 <= cur[1] + neighbor_x[j] <= m && 1 <= cur[2] + neighbor_y[j] <= n |
| 76 | + tempd = LinearIndices((m,n))[cur[1] + neighbor_x[j], cur[2] + neighbor_y[j]] |
| 77 | + W[i,tempd] = exp(-(sig[i] - sig[tempd])^2/σ) |
| 78 | + end |
| 79 | + end |
| 80 | + end |
| 81 | + return sparse(W) |
| 82 | +end # end of the image_Gaussian_affinity function |
| 83 | + |
| 84 | +################################################################################ |
| 85 | +######## Display top 9 basis vectors of various bases for an image data ######## |
| 86 | +################################################################################ |
| 87 | +function top_vectors_plot2(dvec::Array{Float64, 2}, m::Int64, n::Int64, BS::BasisSpec, GP::GraphPart; |
| 88 | + clims::Tuple{Float64,Float64} = (-0.01, 0.01)) |
| 89 | + |
| 90 | + # Get the indices from max to min in terms of absolute value of the coefs |
| 91 | + # Note that m*n == length(dvec); m and n are the image size. |
| 92 | + sorted_ind = sortperm(abs.(dvec[:]), rev = true); |
| 93 | + |
| 94 | + # Set up the layout as 3 x 3 subplots |
| 95 | + plot(9, layout = (3,3), framestyle = :none, legend = false) |
| 96 | + |
| 97 | + # Do the display! |
| 98 | + for i=1:9 |
| 99 | + dvecT = fill(0., size(dvec)) |
| 100 | + dvecT[sorted_ind[i]] = 1 |
| 101 | + f = ghwt_synthesis(dvecT, GP, BS) |
| 102 | + heatmap!(reshape(f, (m,n)), subplot=i, ratio=1, yaxis=:flip, |
| 103 | + showaxis=false, ticks = false, color = :grays, clims = clims) |
| 104 | + end |
| 105 | + display(current()) |
| 106 | +end |
| 107 | +################################################################################ |
| 108 | + |
| 109 | +function top_vectors_plot3(dvec::Array{Float64, 2}, m::Int64, n::Int64, BS::BasisSpec, |
| 110 | + GP::GraphPart; clims::Tuple{Float64,Float64} = (-0.01, 0.01), K::Int64 = 9) |
| 111 | + |
| 112 | + # Get the indices from max to min in terms of absolute value of the coefs |
| 113 | + # Note that m*n == length(dvec); m and n are the image size. |
| 114 | + sorted_ind = sortperm(abs.(dvec[:]), rev = true); |
| 115 | + |
| 116 | + # Set up the layout as 3 x 3 subplots |
| 117 | + plot(K, layout = (Int(sqrt(K)), Int(sqrt(K))), framestyle = :none, legend = false) |
| 118 | + |
| 119 | + # Do the display! |
| 120 | + for i=1:K |
| 121 | + dvecT = fill(0., size(dvec)) |
| 122 | + dvecT[sorted_ind[i]] = 1 |
| 123 | + f = ghwt_synthesis(dvecT, GP, BS) |
| 124 | + heatmap!(reshape(f, (m,n)), subplot=i, ratio=1, yaxis=:flip, |
| 125 | + showaxis=false, ticks = false, color = :grays, clims = clims) |
| 126 | + end |
| 127 | + display(current()) |
| 128 | +end |
| 129 | +################################################################################ |
| 130 | + |
0 commit comments