Skip to content

Commit 3e9ba97

Browse files
committed
Refactor code to put rng as first (optional) argument in related functions and make win an arg instead of kwarg
1 parent 3e809d0 commit 3e9ba97

33 files changed

+4585
-4733
lines changed

docs/plots/ball_example.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
using PartialRejectionSampling
22
const PRS = PartialRejectionSampling
33

4-
c, w = [0.0, 0.0], 10.0
5-
win = PRS.SquareWindow(c, w)
6-
7-
r = 4
8-
hc = PRS.HardCorePointProcess(b, r, win)
4+
c, r = rand(2), 10
95

6+
using Random
7+
rng = Random.MersenneTwister(123)
8+
@time pts = rand(rng, PRS.BallWindow(c, r), 10)
109
@time pts = rand(PRS.BallWindow(c, r), 10)
1110

1211
using Plots

docs/plots/hard_core_graph.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ g = LG.grid(dims)
77

88
hcg = PRS.HardCoreGraph(g, β)
99

10-
seed = 123
11-
@time sample = PRS.generate_sample_prs(hcg; rng=seed)
10+
using Random
11+
rng = Random.MersenneTwister(123)
12+
@time sample = PRS.generate_sample_prs(rng, hcg)
1213

13-
p = plot(hcg, sample, dims; file="docs/plots/output/hard_core_graph.pdf")
14+
p = plot(hcg, sample, dims, "docs/plots/output/hard_core_graph.pdf")
15+
16+
@time sample = PRS.generate_sample_prs(hcg)

docs/plots/hard_core_graph_pedagogy.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ g = LG.grid(dims)
88

99
pp = PRS.HardCoreGraph(g, β)
1010

11-
seed = 1
12-
rng = PRS.getRNG(seed)
11+
rng = Random.MersenneTwister(123)
1312

1413
path(i) = joinpath("docs/plots/output/hard_core_graph", join([lpad(i, 3, "0"), ".pdf"]))
1514

@@ -27,7 +26,6 @@ plot(pp.graph, dims, path(i); nodefillc=c_nodes)
2726

2827
proba = pp.β / (one(pp.β) + pp.β)
2928

30-
rng = PRS.getRNG(rng)
3129
adj = LG.adjacency_matrix(pp.graph)
3230
occupied = Random.randsubseq(rng, LG.vertices(pp.graph), proba)
3331

docs/plots/hard_core_spatial.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ win = PRS.SquareWindow(c, w)
1010

1111
hc = PRS.HardCorePointProcess(b, r, win)
1212

13-
seed = 123
14-
@time sample = PRS.generate_sample_prs(hc; rng=seed)
15-
# @time sample = PRS.generate_sample_grid_prs(hc; rng=seed)
13+
using Random
14+
rng = Random.MersenneTwister(123)
15+
@time sample = PRS.generate_sample_prs(rng, hc)
16+
# @time sample = PRS.generate_sample_grid_prs(rng, hc)
1617

1718
p = plot(hc, sample; title="")

docs/plots/hard_core_spatial_pedagogy.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ win = PRS.SquareWindow(c, w)
1010

1111
hc = PRS.HardCorePointProcess(b, r, win)
1212

13-
seed = 123
14-
pedagogy_generate_sample_prs(hc; rng=seed)
13+
using Random
14+
rng = Random.MersenneTwister(123)
15+
pedagogy_generate_sample_prs(hc; rng=rng)

docs/plots/ising_gibbs_perfect.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ H, J = 0.0, 0.01
77

88
ising = PRS.Ising(dims, J, H; periodic=periodic)
99

10-
seed = 123
11-
@time sample = PRS.generate_sample_gibbs_perfect(ising; rng=seed)
10+
using Random
11+
rng = Random.MersenneTwister(123)
12+
@time sample = PRS.generate_sample_gibbs_perfect(rng, ising)
1213

13-
p = plot(ising, sample, dims...; file="docs/plots/output/ising_gibbs_perfect.pdf")
14+
p = plot(ising, sample, dims, "docs/plots/output/ising_gibbs_perfect.pdf";
15+
nodelabel=nothing)

docs/plots/ising_grid_prs.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ H, J = 0.0, 0.01
77

88
ising = PRS.Ising(dims, J, H; periodic=periodic)
99

10-
seed = 123
11-
@time sample = PRS.generate_sample_grid_prs(ising; rng=seed)
10+
using Random
11+
rng = Random.MersenneTwister(123)
12+
@time sample = PRS.generate_sample_gibbs_perfect(rng, ising)
1213

13-
p = plot(ising, sample, dims...; file="docs/plots/output/ising/ising_grid_prs.pdf")
14+
p = plot(ising, sample, dims, "docs/plots/output/ising/ising_grid_prs.pdf";
15+
nodelabel=nothing)

docs/plots/pattern_free_string_prs.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@ n = 100
22
alph = ["A", "C", "G", "T"]
33
pattern = "ACAC"
44
pfs = PRS.PatternFreeString(alph, pattern)
5-
seed = 123
6-
@time PRS.generate_sample_prs(pfs, n; rng=seed)
5+
6+
using Random
7+
rng = Random.MersenneTwister(123)
8+
@time PRS.generate_sample_prs(rng, pfs, n)

docs/plots/pedagogy_graph.jl

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,50 +64,54 @@ end
6464
function plot(
6565
pp::PRS.Ising,
6666
state,
67-
dims=zeros(2);
67+
dims=zeros(2),
68+
file="";
6869
kwargs...
6970
)
7071
c_nodes = ifelse.(
7172
state .== 1,
7273
Colors.colorant"gray",
7374
Colors.colorant"white")
7475

75-
plot(pp.graph, dims; nodefillc=c_nodes, kwargs...)
76+
plot(pp.graph, dims, file; nodefillc=c_nodes, kwargs...)
7677
end
7778

7879
function plot(
7980
pp::PRS.HardCoreGraph,
8081
state,
81-
dims=zeros(2);
82+
dims=zeros(2),
83+
file="";
8284
kwargs...
8385
)
8486
c_nodes = [Colors.colorant"white" for _ in 1:LG.nv(pp.graph)]
85-
c_nodes[state] .= Colors.colorant"black"
87+
c_nodes[state] .= Colors.colorant"grey"
8688

87-
plot(pp.graph, dims; nodefillc=c_nodes, kwargs...)
89+
plot(pp.graph, dims, file; nodefillc=c_nodes, kwargs...)
8890
end
8991

9092
function plot(
9193
pp::PRS.SinkFreeGraph,
9294
sample,
93-
dims=zeros(2);
95+
dims=zeros(2),
96+
file="";
9497
kwargs...
9598
)
9699
c_nodes, c_edges = color_sinks(sample)
97100

98-
plot(sample, dims; nodefillc=c_nodes, edgestrokec=c_edges, kwargs...)
101+
plot(sample, dims, file; nodefillc=c_nodes, edgestrokec=c_edges, kwargs...)
99102
end
100103

101104
function plot(
102105
pp::PRS.RootedSpanningForest,
103106
sample,
104-
dims=zeros(2);
107+
dims=zeros(2),
108+
file="";
105109
kwargs...
106110
)
107111
c_nodes, c_edges = color_cycles(sample)
108112
c_nodes[collect(pp.roots)] .= Colors.colorant"orange"
109113

110-
plot(sample, dims; nodefillc=c_nodes, edgestrokec=c_edges)
114+
plot(sample, dims, file; nodefillc=c_nodes, edgestrokec=c_edges)
111115
end
112116

113117
function color_cycles(

docs/plots/pedagogy_spatial.jl

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@ using Plots
55

66
# Pedagogy
77
function pedagogy_generate_sample_prs(
8-
pp::PRS.HardCorePointProcess{T};
9-
win::Union{Nothing,PRS.AbstractWindow}=nothing,
10-
rng=-1
8+
pp::PRS.HardCorePointProcess{T};
9+
win::Union{Nothing,PRS.AbstractWindow}=nothing,
10+
rng=-1
1111
)::Vector{T} where {T}
1212

1313
path(i) = joinpath("docs/plots/output/hard_core_spatial", join([lpad(i, 3, "0"), ".pdf"]))
1414

15-
rng = PRS.getRNG(rng)
1615
window_ = win === nothing ? PRS.window(pp) : win
1716

1817
n = rand(rng, Distributions.Poisson(pp.β * PRS.volume(window_)))
1918
points = Matrix{Float64}(undef, PRS.dimension(pp), n)
2019
for x in eachcol(points)
21-
x .= rand(window_; rng=rng)
20+
x .= rand(rng, window_)
2221
end
2322

2423
i = 0
@@ -44,7 +43,7 @@ function pedagogy_generate_sample_prs(
4443
i+=1
4544
Plots.savefig(p, path(i))
4645

47-
resampled = PRS.generate_sample_poisson_union_balls(pp.β, points[:, bad], pp.r; win=window_, rng=rng)
46+
resampled = PRS.generate_sample_poisson_union_balls(rng, pp.β, points[:, bad], pp.r, window_)
4847
pedagogy_plot!(p, resampled, true, 0, "blue", pp.window)
4948
i+=1
5049
Plots.savefig(p, path(i))
@@ -61,12 +60,12 @@ function pedagogy_generate_sample_prs(
6160
end
6261

6362
function pedagogy_plot!(
64-
p,
65-
points,
66-
show_center=true,
67-
radius=0,
68-
color="white",
69-
window=SquareWindow(zeros(2), 1)
63+
p,
64+
points,
65+
show_center=true,
66+
radius=0,
67+
color="white",
68+
window=SquareWindow(zeros(2), 1)
7069
)
7170
for x in (points isa Matrix ? eachcol(points) : points)
7271
if radius > 0
@@ -114,9 +113,9 @@ function plot_disk!(p, center, radius, color=:white)
114113
end
115114

116115
function plot(
117-
pp::PRS.AbstractSpatialPointProcess,
118-
points;
119-
title=""
116+
pp::PRS.AbstractSpatialPointProcess,
117+
points;
118+
title=""
120119
)
121120
p = Plots.plot([0], [0],
122121
label="", legend=false,

0 commit comments

Comments
 (0)