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

Commit 25d615d

Browse files
committed
Major refactor
1 parent 8114f14 commit 25d615d

File tree

5 files changed

+71
-50
lines changed

5 files changed

+71
-50
lines changed

src/processes.jl

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,49 +25,51 @@ algorithm `algo` and random number generator `rng`.
2525
"""
2626
Base.rand(rng::Random.AbstractRNG,
2727
p::PointProcess, g::Geometry, n::Int;
28-
algo=default_sampling_algorithm(p)) =
28+
algo=default_sampling_algorithm(p, g)) =
2929
[rand_single(rng, p, g, algo) for i in 1:n]
3030

3131
Base.rand(rng::Random.AbstractRNG,
3232
p::PointProcess, g::Geometry;
33-
algo=default_sampling_algorithm(p)) =
33+
algo=default_sampling_algorithm(p, g)) =
3434
rand_single(rng, p, g, algo)
3535

3636
Base.rand(p::PointProcess, g::Geometry, n::Int;
37-
algo=default_sampling_algorithm(p)) =
37+
algo=default_sampling_algorithm(p, g)) =
3838
rand(Random.GLOBAL_RNG, p, g, n; algo=algo)
3939

4040
Base.rand(p::PointProcess, g::Geometry;
41-
algo=default_sampling_algorithm(p)) =
41+
algo=default_sampling_algorithm(p, g)) =
4242
rand(Random.GLOBAL_RNG, p, g; algo=algo)
4343

4444
"""
45-
rand_single(p, g, algo)
45+
rand_single(rng, p, g, algo)
4646
4747
Generate a single realization of spatial point process
4848
`p` inside geometry `g` with sampling `algo`.
4949
"""
50-
rand_single(::Random.AbstractRNG, ::PointProcess, ::Geometry, algo) =
51-
throw(ErrorException("not implemented"))
50+
function rand_single end
5251

5352
"""
54-
default_sampling_algorithm(p)
53+
default_sampling_algorithm(p, g)
5554
56-
Default sampling algorithm for spatial point process `p`.
55+
Default sampling algorithm for spatial point process `p`
56+
on geometry `g`.
5757
"""
58-
default_sampling_algorithm(::PointProcess) =
59-
throw(ErrorException("not implemented"))
58+
function default_sampling_algorithm end
6059

61-
"""
62-
p₁ ∪ p₂
60+
# --------------------
61+
# SAMPLING ALGORITHMS
62+
# --------------------
6363

64-
Union (or superposition) of spatial point processes `p₁` and `p₂`.
65-
"""
66-
Base.union(p₁::PointProcess, p₂::PointProcess) = UnionProcess(p₁, p₂)
64+
struct ProductSampling end
65+
struct ThinnedSampling end
66+
struct DiscretizedSampling end
67+
struct UnionSampling end
6768

6869
#-----------------
6970
# IMPLEMENTATIONS
7071
#-----------------
72+
7173
include("processes/binomial.jl")
7274
include("processes/poisson.jl")
7375
include("processes/union.jl")

src/processes/binomial.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@ end
1313

1414
ishomogeneous(p::BinomialProcess) = true
1515

16-
struct BinomialSampling end
17-
18-
default_sampling_algorithm(::BinomialProcess) = BinomialSampling()
16+
default_sampling_algorithm(::BinomialProcess, ::Geometry) = ProductSampling()
1917

2018
function rand_single(rng::Random.AbstractRNG,
2119
p::BinomialProcess, b::Box{Dim,T},
22-
::BinomialSampling) where {Dim,T}
20+
::ProductSampling) where {Dim,T}
2321
# region configuration
2422
lo, up = coordinates.(extrema(b))
2523

src/processes/poisson.jl

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,45 @@ struct PoissonProcess{L<:Union{Real,Function}} <: PointProcess
1111
λ::L
1212
end
1313

14+
Base.union(p₁::PoissonProcess{<:Real}, p₂::PoissonProcess{<:Real}) =
15+
PoissonProcess(p₁.λ + p₂.λ)
16+
17+
Base.union(p₁::PoissonProcess{<:Function}, p₂::PoissonProcess{<:Function}) =
18+
PoissonProcess(x -> p₁.λ(x) + p₂.λ(x))
19+
20+
Base.union(p₁::PoissonProcess{<:Real}, p₂::PoissonProcess{<:Function}) =
21+
PoissonProcess(x -> p₁.λ + p₂.λ(x))
22+
23+
Base.union(p₁::PoissonProcess{<:Function}, p₂::PoissonProcess{<:Real}) =
24+
PoissonProcess(x -> p₁.λ(x) + p₂.λ)
25+
1426
ishomogeneous(p::PoissonProcess{<:Real}) = true
1527
ishomogeneous(p::PoissonProcess{<:Function}) = false
1628

17-
struct ProductSampling end
18-
struct ThinnedSampling end
19-
struct DiscretizedSampling end
20-
21-
default_sampling_algorithm(::PoissonProcess{<:Real}) = ProductSampling()
22-
default_sampling_algorithm(::PoissonProcess{<:Function}) = DiscretizedSampling()
29+
default_sampling_algorithm(::PoissonProcess, ::Geometry) = DiscretizedSampling()
30+
default_sampling_algorithm(::PoissonProcess{<:Real}, ::Box) = ProductSampling()
2331

2432
#------------------
2533
# HOMOGENEOUS CASE
2634
#------------------
2735

2836
function rand_single(rng::Random.AbstractRNG,
29-
p::PoissonProcess{<:Real}, b::Box{Dim,T},
30-
::ProductSampling) where {Dim,T}
37+
p::PoissonProcess{<:Real}, g::Geometry,
38+
::DiscretizedSampling)
39+
# simulate number of points
40+
λ = p.λ; V = measure(g)
41+
n = rand(rng, Poisson*V))
42+
43+
# simulate homogeneous process
44+
pts = sample(g, HomogeneousSampling(n))
45+
46+
# return point pattern
47+
PointSet(collect(pts))
48+
end
49+
50+
function rand_single(rng::Random.AbstractRNG,
51+
p::PoissonProcess{<:Real}, b::Box,
52+
::ProductSampling)
3153
# region configuration
3254
lo, up = coordinates.(extrema(b))
3355

@@ -36,7 +58,7 @@ function rand_single(rng::Random.AbstractRNG,
3658
n = rand(rng, Poisson*V))
3759

3860
# product of uniform distributions
39-
U = product_distribution([Uniform(lo[i], up[i]) for i in 1:Dim])
61+
U = product_distribution([Uniform(lo[i], up[i]) for i in 1:embeddim(b)])
4062

4163
# return point pattern
4264
PointSet(rand(rng, U, n))
@@ -47,8 +69,8 @@ end
4769
#--------------------
4870

4971
function rand_single(rng::Random.AbstractRNG,
50-
p::PoissonProcess{<:Function}, g::Box{Dim,T},
51-
algo::DiscretizedSampling) where {Dim,T}
72+
p::PoissonProcess{<:Function}, b::Box,
73+
algo::DiscretizedSampling)
5274
# discretize region
5375
# TODO
5476

@@ -60,20 +82,8 @@ function rand_single(rng::Random.AbstractRNG,
6082
end
6183

6284
function rand_single(rng::Random.AbstractRNG,
63-
p::PoissonProcess{<:Function}, b::Box{Dim,T},
64-
algo::ThinnedSampling) where {Dim,T}
85+
p::PoissonProcess{<:Function}, b::Box,
86+
algo::ThinnedSampling)
6587
# Lewis-Shedler algorithm
6688
# TODO
67-
end
68-
69-
Base.union(p₁::PoissonProcess{<:Real}, p₂::PoissonProcess{<:Real}) =
70-
PoissonProcess(p₁.λ + p₂.λ)
71-
72-
Base.union(p₁::PoissonProcess{<:Function}, p₂::PoissonProcess{<:Function}) =
73-
PoissonProcess(x -> p₁.λ(x) + p₂.λ(x))
74-
75-
Base.union(p₁::PoissonProcess{<:Real}, p₂::PoissonProcess{<:Function}) =
76-
PoissonProcess(x -> p₁.λ + p₂.λ(x))
77-
78-
Base.union(p₁::PoissonProcess{<:Function}, p₂::PoissonProcess{<:Real}) =
79-
PoissonProcess(x -> p₁.λ(x) + p₂.λ)
89+
end

src/processes/union.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ struct UnionProcess{P₁<:PointProcess,P₂<:PointProcess} <: PointProcess
1212
p₂::P₂
1313
end
1414

15-
ishomogeneous(p::UnionProcess) = ishomogeneous(p.p₁) && ishomogeneous(p.p₂)
15+
Base.union(p₁::PointProcess, p₂::PointProcess) = UnionProcess(p₁, p₂)
1616

17-
struct UnionSampling end
17+
ishomogeneous(p::UnionProcess) = ishomogeneous(p.p₁) && ishomogeneous(p.p₂)
1818

19-
default_sampling_algorithm(::UnionProcess) = UnionSampling()
19+
default_sampling_algorithm(::UnionProcess, ::Geometry) = UnionSampling()
2020

2121
function rand_single(rng::Random.AbstractRNG,
2222
p::UnionProcess, g::Geometry,

test/processes.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,18 @@
1414
end
1515

1616
@testset "Poisson" begin
17-
# TODO
17+
p = PoissonProcess(100.)
18+
t = Triangle((0.,0.), (1.,0.), (1.,1.))
19+
pp = rand(p, t)
20+
xs = coordinates.(pp)
21+
@test all(0 .≤ first.(xs) .≤ 1)
22+
@test all(0 .≤ last.(xs))
23+
24+
q = Quadrangle((0.,0.), (1.,0.), (1.,1.), (0.,1.))
25+
pp = rand(p, q)
26+
xs = coordinates.(pp)
27+
@test all(0 .≤ first.(xs) .≤ 1)
28+
@test all(0 .≤ last.(xs) .≤ 1)
1829
end
1930

2031
@testset "Union" begin

0 commit comments

Comments
 (0)