|
| 1 | +using Plots |
| 2 | +using GraphPlot, Colors |
| 3 | + |
| 4 | +using LazySets |
| 5 | +const LS = LazySets |
| 6 | + |
| 7 | +# Pedagogy |
| 8 | +function pedagogy_generate_sample_prs( |
| 9 | + hc::HardCorePointProcess{T}; |
| 10 | + win::Union{Nothing,AbstractWindow}=nothing, |
| 11 | + path="", |
| 12 | + rng=-1 |
| 13 | +)::Vector{T} where {T} |
| 14 | + |
| 15 | + rng = getRNG(rng) |
| 16 | + window_ = win === nothing ? window(hc) : win |
| 17 | + |
| 18 | + n = rand(rng, Distributions.Poisson(hc.β * volume(window_))) |
| 19 | + points = Matrix{Float64}(undef, dimension(hc), n) |
| 20 | + for x in eachcol(points) |
| 21 | + x .= rand(window_; rng=rng) |
| 22 | + end |
| 23 | + |
| 24 | + i = 0 |
| 25 | + while true |
| 26 | + |
| 27 | + p = pedagogy_plot(points, true, 0.0, "white", hc.window) |
| 28 | + Plots.savefig(p, "$(path)/hard_core_$(i)_0.pdf") |
| 29 | + |
| 30 | + p = pedagogy_plot(points, true, hc.r/2, "white", hc.window) |
| 31 | + Plots.savefig(p, "$(path)/hard_core_$(i)_1.pdf") |
| 32 | + |
| 33 | + bad = vec(any(pairwise_distances(points) .< hc.r, dims=2)) |
| 34 | + !any(bad) && break |
| 35 | + |
| 36 | + pedagogy_plot!(p, points[:, bad], false, hc.r/2, "red", hc.window) |
| 37 | + Plots.savefig(p, "$(path)/hard_core_$(i)_1bad_region.pdf") |
| 38 | + |
| 39 | + pedagogy_plot!(p, points[:, bad], false, hc.r, "orange", hc.window) |
| 40 | + Plots.savefig(p, "$(path)/hard_core_$(i)_2resample_region.pdf") |
| 41 | + |
| 42 | + p = pedagogy_plot(points[:, .!bad], true, 0.0, "white", hc.window) |
| 43 | + pedagogy_plot!(p, points[:, bad], false, hc.r, "orange", hc.window) |
| 44 | + Plots.savefig(p, "$(path)/hard_core_$(i)_3resample_region.pdf") |
| 45 | + |
| 46 | + resampled = generate_sample_poisson_union_balls(hc.β, points[:, bad], hc.r; win=window_, rng=rng) |
| 47 | + pedagogy_plot!(p, resampled, true, 0, "blue", hc.window) |
| 48 | + Plots.savefig(p, "$(path)/hard_core_$(i)_4resampled_points.pdf") |
| 49 | + |
| 50 | + points = hcat(points[:, .!bad], resampled) |
| 51 | + i += 1 |
| 52 | + |
| 53 | + end |
| 54 | + return [points[:, i] for i in 1:size(points, 2)] |
| 55 | +end |
| 56 | + |
| 57 | +function pedagogy_plot!( |
| 58 | + p, |
| 59 | + points, |
| 60 | + show_center=true, |
| 61 | + radius=0.0, |
| 62 | + color="white", |
| 63 | + window=SquareWindow(zeros(2), 1.0) |
| 64 | +) |
| 65 | + for x in (points isa Matrix ? eachcol(points) : points) |
| 66 | + if show_center |
| 67 | + Plots.scatter!(p, [x[1]], [x[2]], markersize=2, color=color) |
| 68 | + end |
| 69 | + if radius > 0 |
| 70 | + Plots.plot!(p, LS.Ball2(vec(x), radius), color=color) |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + Plots.xlims!(window.c[1], window.c[1] + window.w) |
| 75 | + Plots.ylims!(window.c[2], window.c[2] + window.w) |
| 76 | + |
| 77 | + return p |
| 78 | +end |
| 79 | + |
| 80 | +function pedagogy_plot( |
| 81 | + points, |
| 82 | + show_center=true, |
| 83 | + radius=0.0, |
| 84 | + color="white", |
| 85 | + window=SquareWindow(zeros(2), 1.0) |
| 86 | +) |
| 87 | + |
| 88 | + p = Plots.plot([0], [0], |
| 89 | + label="", legend=false, |
| 90 | + color="white", |
| 91 | + linewidth=0.0, |
| 92 | + aspect_ratio=:equal, |
| 93 | + grid=:none, |
| 94 | + title="") |
| 95 | + |
| 96 | + pedagogy_plot!(p, points, show_center, radius, color, window) |
| 97 | + |
| 98 | + return p |
| 99 | +end |
| 100 | + |
| 101 | +function plot( |
| 102 | + pp::AbstractSpatialPointProcess, |
| 103 | + points; |
| 104 | + title="" |
| 105 | +) |
| 106 | + p = Plots.plot([0], [0], |
| 107 | + label="", legend=false, |
| 108 | + color="white", |
| 109 | + linewidth=0.0, |
| 110 | + aspect_ratio=:equal, |
| 111 | + grid=:none, |
| 112 | + title=title) |
| 113 | + |
| 114 | + θ = collect(range(0, 2π, length=15)) |
| 115 | + rad = pp.r / 2 # radius = interaction range / 2 |
| 116 | + circ_x, circ_y = rad .* cos.(θ), rad .* sin.(θ) |
| 117 | + |
| 118 | + for x in points |
| 119 | + Plots.plot!(x[1] .+ circ_x, |
| 120 | + x[2] .+ circ_y, |
| 121 | + color="black", |
| 122 | + linewidth=0.5) |
| 123 | + end |
| 124 | + |
| 125 | + win = pp.window |
| 126 | + Plots.xlims!(win.c[1], win.c[1] + win.w[1]) |
| 127 | + Plots.ylims!(win.c[2], win.c[2] + (win.w isa Number ? win.w[1] : win.w[2])) |
| 128 | + |
| 129 | + return p |
| 130 | +end |
| 131 | + |
| 132 | +function plot( |
| 133 | + ising::Ising, |
| 134 | + state, |
| 135 | + width::Int=0, |
| 136 | + height::Int=0 |
| 137 | +) |
| 138 | + if width == 0 || height == 0 |
| 139 | + p = GraphPlot.gplot(ising.graph, |
| 140 | + nodelabel=LG.vertices(ising.graph), |
| 141 | + nodefillc=col_nodes |
| 142 | + ) |
| 143 | + return p |
| 144 | + else |
| 145 | + pos = collect(Iterators.product(1:height, 1:width))[:] |
| 146 | + locs_x, locs_y = map(x->x[1], pos), map(x->x[2], pos) |
| 147 | + |
| 148 | + col_nodes = ifelse.( |
| 149 | + state .== 1, |
| 150 | + Colors.colorant"gray", |
| 151 | + Colors.colorant"white") |
| 152 | + |
| 153 | + p = GraphPlot.gplot( |
| 154 | + ising.graph, |
| 155 | + locs_x, |
| 156 | + reverse(locs_y), |
| 157 | + nodefillc=col_nodes) |
| 158 | + return p |
| 159 | + end |
| 160 | +end |
| 161 | + |
| 162 | +function plot( |
| 163 | + hcg::HardCoreGraph, |
| 164 | + state, |
| 165 | + width::Int=0, |
| 166 | + height::Int=0 |
| 167 | +) |
| 168 | + col_nodes = [Colors.colorant"turquoise" for _ in 1:LG.nv(hcg.graph)] |
| 169 | + col_nodes[state] .= Colors.colorant"red" |
| 170 | + |
| 171 | + if width == 0 || height == 0 |
| 172 | + p = GraphPlot.gplot(hcg.graph, |
| 173 | + nodelabel=LG.vertices(hcg.graph), |
| 174 | + nodefillc=col_nodes |
| 175 | + ) |
| 176 | + return p |
| 177 | + else |
| 178 | + pos = collect(Iterators.product(1:height, 1:width))[:] |
| 179 | + locs_x, locs_y = map(x->x[1], pos), map(x->x[2], pos) |
| 180 | + |
| 181 | + p = GraphPlot.gplot(hcg.graph, |
| 182 | + locs_x, |
| 183 | + reverse(locs_y), |
| 184 | + nodelabel=LG.vertices(hcg.graph), |
| 185 | + nodefillc=col_nodes |
| 186 | + ) |
| 187 | + return p |
| 188 | + end |
| 189 | +end |
| 190 | + |
| 191 | +function plot( |
| 192 | + rsf::RootedSpanningForest, |
| 193 | + sample, |
| 194 | + width::Int=0, |
| 195 | + height::Int=0 |
| 196 | +) |
| 197 | + col_nodes, col_edges = color_cycles(sample) |
| 198 | + col_nodes[collect(rsf.roots)] .= Colors.colorant"orange" |
| 199 | + |
| 200 | + if width == 0 || height == 0 |
| 201 | + p = GraphPlot.gplot(sample, |
| 202 | + nodelabel=LG.vertices(sample), |
| 203 | + nodefillc=col_nodes, |
| 204 | + edgestrokec=col_edges, |
| 205 | + arrowlengthfrac=0.05, |
| 206 | + ) |
| 207 | + return p |
| 208 | + else |
| 209 | + pos = collect(Iterators.product(1:height, 1:width))[:] |
| 210 | + locs_x, locs_y = map(x->x[1], pos), map(x->x[2], pos) |
| 211 | + |
| 212 | + p = GraphPlot.gplot(sample, |
| 213 | + locs_x, |
| 214 | + reverse(locs_y), |
| 215 | + nodelabel=LG.vertices(sample), |
| 216 | + nodefillc=col_nodes, |
| 217 | + edgestrokec=col_edges, |
| 218 | + arrowlengthfrac=0.05, |
| 219 | + ) |
| 220 | + return p |
| 221 | + end |
| 222 | +end |
| 223 | + |
| 224 | +function color_cycles( |
| 225 | + graph::LG.SimpleDiGraph{T} |
| 226 | +) where {T} |
| 227 | + |
| 228 | + edge_map = edgemap(graph) |
| 229 | + edge_idx = T[] |
| 230 | + nodes = Set{T}() |
| 231 | + for cycle in LG.simplecycles(graph) |
| 232 | + union!(nodes, cycle) |
| 233 | + for (x, y) in zip(cycle, circshift(cycle, -1)) |
| 234 | + push!(edge_idx, edge_map[LG.Edge(x, y)]) |
| 235 | + end |
| 236 | + end |
| 237 | + |
| 238 | + col_nodes = [colorant"turquoise" for _ in 1:LG.nv(graph)] |
| 239 | + col_nodes[collect(nodes)] .= colorant"red" |
| 240 | + |
| 241 | + col_edges = [colorant"lightgray" for _ in 1:LG.ne(graph)] |
| 242 | + col_edges[edge_idx] .= colorant"red" |
| 243 | + |
| 244 | + return col_nodes, col_edges |
| 245 | +end |
| 246 | + |
| 247 | +function plot( |
| 248 | + sfg::SinkFreeGraph, |
| 249 | + sample, |
| 250 | + width::Int=0, |
| 251 | + height::Int=0 |
| 252 | +) |
| 253 | + col_nodes = [LG.outdegree(sample, v) == 0 ? colorant"red" : colorant"turquoise" |
| 254 | + for v in LG.vertices(sample)] |
| 255 | + if width == 0 || height == 0 |
| 256 | + p = GraphPlot.gplot(sample, |
| 257 | + nodelabel=LG.vertices(sample), |
| 258 | + nodefillc=col_nodes, |
| 259 | + arrowlengthfrac=0.05, |
| 260 | + ) |
| 261 | + return p |
| 262 | + else |
| 263 | + pos = collect(Iterators.product(1:height, 1:width))[:] |
| 264 | + locs_x, locs_y = map(x->x[1], pos), map(x->x[2], pos) |
| 265 | + |
| 266 | + p = GraphPlot.gplot(sample, |
| 267 | + locs_x, |
| 268 | + reverse(locs_y), |
| 269 | + nodelabel=LG.vertices(sample), |
| 270 | + nodefillc=col_nodes, |
| 271 | + arrowlengthfrac=0.05, |
| 272 | + ) |
| 273 | + return p |
| 274 | + end |
| 275 | +end |
| 276 | + |
| 277 | +function color_sinks( |
| 278 | + graph::LG.SimpleDiGraph{T} |
| 279 | +) where {T} |
| 280 | + |
| 281 | + nodes = T[] |
| 282 | + edge_idx = T[] |
| 283 | + edge_map = edgemap(graph) |
| 284 | + for v in sink_nodes(graph) |
| 285 | + push!(nodes, v) |
| 286 | + for w in LG.inneighbors(graph, v) |
| 287 | + push!(edge_idx, edge_map[LG.Edge(w, v)]) |
| 288 | + end |
| 289 | + end |
| 290 | + |
| 291 | + col_nodes = [Colors.colorant"turquoise" for _ in 1:LG.nv(graph)] |
| 292 | + col_nodes[nodes] .= Colors.colorant"red" |
| 293 | + |
| 294 | + col_edges = [Colors.colorant"lightgray" for _ in 1:LG.ne(graph)] |
| 295 | + col_edges[edge_idx] .= Colors.colorant"red" |
| 296 | + |
| 297 | + return col_nodes, col_edges |
| 298 | +end |
0 commit comments