Skip to content
This repository was archived by the owner on Nov 6, 2023. It is now read-only.

Commit 1e05a3a

Browse files
committed
Add support for rng
1 parent 23bbdf2 commit 1e05a3a

File tree

8 files changed

+49
-28
lines changed

8 files changed

+49
-28
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ version = "0.3.15"
77
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
88
GeoStatsBase = "323cb8eb-fbf6-51c0-afd0-f8fba70507b2"
99
Meshes = "eacbb407-ea5a-433e-ab97-5258b1ca43fa"
10+
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1011

1112
[compat]
1213
Distributions = "0.21, 0.22, 0.23, 0.24, 0.25"

src/PointPatterns.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ using GeoStatsBase
99

1010
using Distributions
1111

12+
import Random
13+
1214
include("processes.jl")
1315
include("thinning.jl")
1416

src/processes.jl

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,46 @@ Tells whether or not the spatial point process `p` is homogeneous.
1717
ishomogeneous(p::PointProcess) = false
1818

1919
"""
20-
rand(p, g, n=1; [algo])
20+
rand([rng], p, g, n=1; [algo])
2121
2222
Generate `n` realizations of spatial point process `p`
2323
inside geometry `g`. Optionally specify sampling
24-
algorithm `algo`.
24+
algorithm `algo` and random number generator `rng`.
2525
"""
26-
Base.rand(p::PointProcess, g::Geometry, n::Int;
26+
Base.rand(rng::Random.AbstractRNG,
27+
p::PointProcess, g::Geometry, n::Int;
2728
algo=default_sampling_algorithm(p)) =
28-
[rand_single(p, g, algo) for i in 1:n]
29+
[rand_single(rng, p, g, algo) for i in 1:n]
2930

30-
Base.rand(p::PointProcess, g::Geometry;
31+
Base.rand(rng::Random.AbstractRNG,
32+
p::PointProcess, g::Geometry;
3133
algo=default_sampling_algorithm(p)) =
32-
rand_single(p, g, algo)
34+
rand_single(rng, p, g, algo)
35+
36+
Base.rand(p::PointProcess, g::Geometry, n::Int;
37+
algo=default_sampling_algorithm(p)) =
38+
rand(Random.GLOBAL_RNG, p, g, n; algo=algo)
39+
40+
Base.rand(p::PointProcess, g::Geometry;
41+
algo=default_sampling_algorithm(p)) =
42+
rand(Random.GLOBAL_RNG, p, g; algo=algo)
3343

3444
"""
3545
rand_single(p, g, algo)
3646
3747
Generate a single realization of spatial point process
3848
`p` inside geometry `g` with sampling `algo`.
3949
"""
40-
rand_single(::PointProcess, ::Geometry, algo) =
41-
@error "not implemented"
50+
rand_single(::Random.AbstractRNG, ::PointProcess, ::Geometry, algo) =
51+
throw(ErrorException("not implemented"))
4252

4353
"""
4454
default_sampling_algorithm(p)
4555
4656
Default sampling algorithm for spatial point process `p`.
4757
"""
48-
default_sampling_algorithm(::PointProcess) = @error "not implemented"
58+
default_sampling_algorithm(::PointProcess) =
59+
throw(ErrorException("not implemented"))
4960

5061
"""
5162
p₁ ∪ p₂

src/processes/binomial.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ struct BinomialSampling end
1717

1818
default_sampling_algorithm(::BinomialProcess) = BinomialSampling()
1919

20-
function rand_single(p::BinomialProcess, b::Box{Dim,T},
20+
function rand_single(rng::Random.AbstractRNG,
21+
p::BinomialProcess, b::Box{Dim,T},
2122
::BinomialSampling) where {Dim,T}
2223
# region configuration
2324
lo, up = coordinates.(extrema(b))
@@ -26,5 +27,5 @@ function rand_single(p::BinomialProcess, b::Box{Dim,T},
2627
U = product_distribution([Uniform(lo[i], up[i]) for i in 1:Dim])
2728

2829
# return point pattern
29-
PointSet(rand(U, p.n))
30+
PointSet(rand(rng, U, p.n))
3031
end

src/processes/poisson.jl

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,30 @@ default_sampling_algorithm(::PoissonProcess{<:Function}) = DiscretizedSampling()
2424
#------------------
2525
# HOMOGENEOUS CASE
2626
#------------------
27-
function rand_single(p::PoissonProcess{<:Real}, b::Box{Dim,T},
27+
28+
function rand_single(rng::Random.AbstractRNG,
29+
p::PoissonProcess{<:Real}, b::Box{Dim,T},
2830
::ProductSampling) where {Dim,T}
2931
# region configuration
3032
lo, up = coordinates.(extrema(b))
3133

3234
# simulate number of points
3335
λ = p.λ; V = measure(b)
34-
n = rand(Poisson*V))
36+
n = rand(rng, Poisson*V))
3537

3638
# product of uniform distributions
3739
U = product_distribution([Uniform(lo[i], up[i]) for i in 1:Dim])
3840

3941
# return point pattern
40-
PointSet(rand(U, n))
42+
PointSet(rand(rng, U, n))
4143
end
4244

4345
#--------------------
4446
# INHOMOGENEOUS CASE
4547
#--------------------
46-
function rand_single(p::PoissonProcess{<:Function}, g::Box{Dim,T},
48+
49+
function rand_single(rng::Random.AbstractRNG,
50+
p::PoissonProcess{<:Function}, g::Box{Dim,T},
4751
algo::DiscretizedSampling) where {Dim,T}
4852
# discretize region
4953
# TODO
@@ -55,7 +59,8 @@ function rand_single(p::PoissonProcess{<:Function}, g::Box{Dim,T},
5559
# TODO
5660
end
5761

58-
function rand_single(p::PoissonProcess{<:Function}, b::Box{Dim,T},
62+
function rand_single(rng::Random.AbstractRNG,
63+
p::PoissonProcess{<:Function}, b::Box{Dim,T},
5964
algo::ThinnedSampling) where {Dim,T}
6065
# Lewis-Shedler algorithm
6166
# TODO

src/processes/union.jl

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@ struct UnionSampling end
1818

1919
default_sampling_algorithm(::UnionProcess) = UnionSampling()
2020

21-
function rand_single(p::UnionProcess, g::Geometry, algo::UnionSampling)
22-
pp₁ = rand(p.p₁, g)
23-
pp₂ = rand(p.p₂, g)
21+
function rand_single(rng::Random.AbstractRNG,
22+
p::UnionProcess, g::Geometry,
23+
::UnionSampling)
24+
pp₁ = rand(rng, p.p₁, g)
25+
pp₂ = rand(rng, p.p₂, g)
2426

25-
X = [coordinates(pp₁[i]) for i in 1:nelements(pp₁)]
26-
Y = [coordinates(pp₂[i]) for i in 1:nelements(pp₂)]
27-
28-
PointSet([X; Y])
27+
PointSet([coordinates.(pp₁); coordinates.(pp₂)])
2928
end

src/thinning/random.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ struct RandomThinning{P<:Union{Real,Function}} <: AbstractThinning
1212
end
1313

1414
# -----------------------
15-
# thinning point process
15+
# THINNING POINT PROCESS
1616
# -----------------------
17+
1718
thin(p::PoissonProcess{<:Real}, t::RandomThinning{<:Real}) =
1819
PoissonProcess(t.p * p.λ)
1920

@@ -27,8 +28,9 @@ thin(p::PoissonProcess{<:Function}, t::RandomThinning{<:Real}) =
2728
PoissonProcess(x -> t.p * p.λ(x))
2829

2930
# -----------------------
30-
# thinning point pattern
31+
# THINNING POINT PATTERN
3132
# -----------------------
33+
3234
function thin(pp::PointSet, t::RandomThinning{<:Real})
3335
draws = rand(Bernoulli(t.p), nelements(pp))
3436
inds = findall(isequal(1), draws)

test/processes.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
@testset "Processes" begin
22
@testset "Basic" begin
33
for p in [BinomialProcess(100), PoissonProcess(100.)]
4-
r = Box((0.,1.), (1.,2.))
5-
pp = rand(p, r)
6-
xs = [coordinates(pp[i]) for i in 1:nelements(pp)]
4+
b = Box((0.,1.), (1.,2.))
5+
pp = rand(p, b)
6+
xs = coordinates.(pp)
77
@test all(0 .≤ first.(xs) .≤ 1)
88
@test all(1 .≤ last.(xs) .≤ 2)
99
end

0 commit comments

Comments
 (0)