Skip to content

Commit b9256a4

Browse files
committed
Restructure backend grid prs to work on square rectangle
1 parent 238d8b9 commit b9256a4

File tree

8 files changed

+66
-25
lines changed

8 files changed

+66
-25
lines changed

src/display.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ function plot(
6969
locs_x, locs_y = map(x->x[1], pos), map(x->x[2], pos)
7070

7171
p = GraphPlot.gplot(hcg.g,
72-
locs_x, locs_y,
72+
locs_x,
73+
reverse(locs_y),
7374
nodelabel=LG.vertices(hcg.g),
7475
nodefillc=col_nodes
7576
)
@@ -99,7 +100,8 @@ function plot(
99100
locs_x, locs_y = map(x->x[1], pos), map(x->x[2], pos)
100101

101102
p = GraphPlot.gplot(sample,
102-
locs_x, locs_y,
103+
locs_x,
104+
reverse(locs_y),
103105
nodelabel=LG.vertices(sample),
104106
nodefillc=col_nodes,
105107
edgestrokec=col_edges,
@@ -129,7 +131,8 @@ function plot(
129131
locs_x, locs_y = map(x->x[1], pos), map(x->x[2], pos)
130132

131133
p = GraphPlot.gplot(sample,
132-
locs_x, locs_y,
134+
locs_x,
135+
reverse(locs_y),
133136
nodelabel=LG.vertices(sample),
134137
nodefillc=col_nodes,
135138
arrowlengthfrac=0.05,

src/grid_prs.jl

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ function generate_sample_grid_prs(
2525
rng=-1
2626
)::Vector{T} where {T}
2727
rng = getRNG(rng)
28-
g = weighted_interaction_graph(pp; rng=rng)
2928

30-
cells = initialize_cells(pp, LG.nv(g))
29+
g = weighted_interaction_graph(pp; rng=rng)
30+
cells = initialize_cells(pp)
3131
resample_indices = Set(1:length(cells))
3232

3333
while !isempty(resample_indices)
@@ -47,7 +47,17 @@ function weighted_interaction_graph(
4747
rng=-1
4848
)::SWG.SimpleWeightedGraph
4949
rng = getRNG(rng)
50-
g = SWG.SimpleWeightedGraph(king_graph(ceil(Int, inv(pp.r))))
50+
51+
window_ = window(pp)
52+
g = SWG.SimpleWeightedGraph()
53+
nb_cells_x = nb_cells_y = 1
54+
if allequal(window_.w)
55+
nb_cells_x = ceil(Int, window_.w[1] / pp.r)
56+
g = SWG.SimpleWeightedGraph(king_graph(nb_cells_x))
57+
else
58+
nb_cells_x, nb_cells_y = ceil.(Int, window_.w ./ pp.r)
59+
g = SWG.SimpleWeightedGraph(king_graph(nb_cells_x, nb_cells_y))
60+
end
5161
for e in LG.edges(g)
5262
i, j = Tuple(e)
5363
@inbounds g.weights[i, j] = g.weights[j, i] = rand(rng, weighttype(g))
@@ -144,14 +154,21 @@ end
144154

145155
function initialize_cells(
146156
spp::AbstractSpatialPointProcess{T},
147-
size::Integer
148157
)::Vector{SpatialCellGridPRS{T}} where {T}
149-
cells = Vector{SpatialCellGridPRS{T}}(undef, size)
150-
k = ceil(Int, inv(spp.r))
158+
dimension(spp) != 2 && throw(DomainError(spp, "must be 2D point process for now"))
159+
window_ = window(spp)
160+
nb_cells_x = nb_cells_y = 1
161+
if allequal(window_.w) # SquareWindow
162+
nb_cells_x = nb_cells_y = ceil(Int, window_.w[1] / spp.r)
163+
else
164+
nb_cells_x, nb_cells_y = ceil.(Int, window_.w ./ spp.r)
165+
end
166+
167+
cells = Vector{SpatialCellGridPRS{T}}(undef, nb_cells_x * nb_cells_y)
151168
for i in eachindex(cells)
152-
c_y, c_x = divrem(i-1, k)
153-
c = spp.window.c + spp.r .* [c_x, c_y]
154-
win_i = rectangle_square_window(c, min.(spp.r, spp.window.w .- c))
169+
c_ij = divrem(i-1, nb_cells_x) # coordinate (i, j) of the cell in the grid
170+
c_xy = @. window_.c + spp.r * c_ij
171+
win_i = rectangle_square_window(c_xy, @. min(spp.r, window_.w - c_xy))
155172
cells[i] = SpatialCellGridPRS(win_i, T[])
156173
end
157174
return cells

src/ising/ising_sampling_grid_prs.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ function weighted_interaction_graph(
1414
end
1515

1616
function initialize_cells(
17-
ising::Ising{T},
18-
size::Integer
17+
ising::Ising{T}
1918
)::Vector{GraphCellGridPRS{T}} where {T}
20-
return [GraphCellGridPRS(GraphNode(i), zero(T)) for i in 1:size]
19+
return [GraphCellGridPRS(GraphNode(i), zero(T)) for i in 1:LG.nv(ising.g)]
2120
end
2221

2322
function generate_sample!(

src/poisson.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ function generate_sample(
1919
rng=-1
2020
)::Matrix{Float64}
2121
rng = getRNG(rng)
22-
win_ = win === nothing ? window(hp) : win
23-
n = rand(rng, Distributions.Poisson(hp.β * volume(win_)))
24-
return rand(win_, n; rng=rng)
22+
window_ = win === nothing ? window(hp) : win
23+
n = rand(rng, Distributions.Poisson(hp.β * volume(window_)))
24+
return rand(window_, n; rng=rng)
2525
end
2626

2727
"""

src/utils.jl

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,35 @@ end
2525
edgemap(g::LG.AbstractGraph) = Dict(LG.edges(g) .=> 1:LG.ne(g))
2626
sink_nodes(g::LG.SimpleDiGraph) = [v for v in LG.vertices(g) if LG.outdegree(g, v) == 0]
2727

28-
2928
"""
30-
Return the [strong product](https://en.wikipedia.org/wiki/Strong_product_of_graphs) of a graph with itself
29+
Return the [strong product](https://en.wikipedia.org/wiki/Strong_product_of_graphs) of two graphs
3130
3231
See also:
3332
Section 5 of [alternative text](https://drops.dagstuhl.de/opus/volltexte/2019/11538/pdf/LIPIcs-ISAAC-2019-42.pdf)
3433
"""
34+
function strong_product(g::G, h::G)::G where {G<:LG.AbstractGraph}
35+
sp_gh = G(LG.nv(g) * LG.nv(h))
36+
id(i, j) = (i - 1) * LG.nv(h) + j
37+
for e1 in LG.edges(g)
38+
i1, i2 = Tuple(e1)
39+
for j = 1:LG.nv(h)
40+
LG.add_edge!(sp_gh, id(i1, j), id(i2, j))
41+
end
42+
for e2 in LG.edges(h)
43+
j1, j2 = Tuple(e2)
44+
LG.add_edge!(sp_gh, id(i1, j1), id(i2, j2))
45+
LG.add_edge!(sp_gh, id(i1, j2), id(i2, j1))
46+
end
47+
end
48+
for e in LG.edges(h)
49+
j1, j2 = Tuple(e)
50+
for i in LG.vertices(g)
51+
LG.add_edge!(sp_gh, id(i, j1), id(i, j2))
52+
end
53+
end
54+
return sp_gh
55+
end
56+
3557
function strong_product(g::G)::G where {G<:LG.AbstractGraph}
3658
n = LG.nv(g)
3759
sp_gg = G(n * n) # nv = n^2, ne = 2n(2n+1)
@@ -51,6 +73,7 @@ function strong_product(g::G)::G where {G<:LG.AbstractGraph}
5173
return sp_gg
5274
end
5375

76+
king_graph(k::Integer, l::Integer) = strong_product(LG.path_graph(l), LG.path_graph(k))
5477
king_graph(k::Integer) = strong_product(LG.path_graph(k))
5578

5679
function random_edge_orientation(
@@ -63,7 +86,6 @@ function random_edge_orientation(
6386
return LG.SimpleDiGraphFromIterator(rand(rng) < p ? e : reverse(e) for e in LG.edges(g))
6487
end
6588

66-
6789
"""
6890
pairwise_distances(X, Y)
6991

src/window.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ function SquareWindow(c::AbstractVector, w::Real=1.0)
4444
return SquareWindow{Float64}(c, w)
4545
end
4646

47-
function rectangle_square_window(c::AbstractVector, w::Union{Real, AbstractVector})
48-
return w isa Real ? SquareWindow(c, w) : RectangleWindow(c, w)
47+
function rectangle_square_window(c, w)
48+
return allequal(w) ? SquareWindow(c, w[1]) : RectangleWindow(c, w)
4949
end
5050

5151
struct BallWindow{T<:Float64} <: AbstractSpatialWindow{T}

test/hard_core_spatial.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ hc = PRS.HardCorePointProcess(b, r, win)
1212

1313
seed = -1
1414
@time sample = PRS.generate_sample_prs(hc; rng=seed)
15-
# @time sample = PRS.generate_sample_grid_prs(hc; rng=-1)
15+
# @time sample = PRS.generate_sample_grid_prs(hc; rng=seed)
1616

1717
p = PRS.plot(hc, sample; title="")

test/strauss_grid_prs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const PRS = PartialRejectionSampling
55
r = 0.05 # interaction range = 2*radius
66
β, γ = β₀ /* (r/2)^2), 0.1 # γ = 0 ≡ Hard core
77

8-
c, w = [0.0, 0.0], 1.0
8+
c, w = [0.0, 0.0], 2.0
99
win = PRS.SquareWindow(c, w)
1010

1111
strauss = PRS.StraussPointProcess(β, γ, r, win)

0 commit comments

Comments
 (0)