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

Commit 84da362

Browse files
authored
Merge pull request #38 from JuliaEarth/refactor
Erase algorithms
2 parents 890d558 + a556d04 commit 84da362

File tree

7 files changed

+52
-126
lines changed

7 files changed

+52
-126
lines changed

src/PointPatterns.jl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ export
2323
UnionProcess,
2424
ishomogeneous,
2525

26-
# algorithms
27-
PointPatternAlgo,
28-
ConstantIntensity,
29-
LewisShedler,
30-
3126
# thinning methods
3227
ThinningMethod,
3328
RandomThinning,

src/processes.jl

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

1919
"""
20-
rand([rng], p, g, n=1; [algo])
20+
rand([rng], p, g, n=1)
2121
2222
Generate `n` realizations of spatial point process `p`
23-
inside geometry or domain `g`. Optionally specify sampling
24-
algorithm `algo` and random number generator `rng`.
23+
inside geometry or domain `g`. Optionally specify the
24+
random number generator `rng`.
2525
"""
26-
Base.rand(rng::Random.AbstractRNG, p::PointProcess, g, n::Int; algo=defaultalgo(p, g)) =
27-
[randsingle(rng, p, g, algo) for i in 1:n]
26+
Base.rand(rng::Random.AbstractRNG, p::PointProcess, g, n::Int) = [randsingle(rng, p, g) for _ in 1:n]
2827

29-
Base.rand(rng::Random.AbstractRNG, p::PointProcess, g; algo=defaultalgo(p, g)) =
30-
randsingle(rng, p, g, algo)
28+
Base.rand(rng::Random.AbstractRNG, p::PointProcess, g) = randsingle(rng, p, g)
3129

32-
Base.rand(p::PointProcess, g, n::Int; algo=defaultalgo(p, g)) =
33-
rand(Random.GLOBAL_RNG, p, g, n; algo=algo)
30+
Base.rand(p::PointProcess, g, n::Int) = rand(Random.GLOBAL_RNG, p, g, n)
3431

35-
Base.rand(p::PointProcess, g; algo=defaultalgo(p, g)) = rand(Random.GLOBAL_RNG, p, g; algo=algo)
32+
Base.rand(p::PointProcess, g) = rand(Random.GLOBAL_RNG, p, g)
3633

3734
"""
38-
randsingle(rng, p, g, algo)
35+
randsingle(rng, p, g)
3936
40-
Generate a single realization of spatial point process
41-
`p` inside geometry or domain `g` with sampling `algo`.
37+
Generate a single point pattern of point process `p`
38+
inside geometry or domain `g`.
4239
"""
4340
function randsingle end
4441

45-
"""
46-
defaultalgo(p, g)
47-
48-
Default sampling algorithm for spatial point process `p`
49-
on geometry or domain `g`.
50-
"""
51-
function defaultalgo end
52-
53-
# -------------------------
54-
# POINT PATTERN ALGORITHMS
55-
# -------------------------
56-
57-
"""
58-
PointPatternAlgo
59-
60-
A method for sampling point patterns.
61-
"""
62-
abstract type PointPatternAlgo end
63-
64-
"""
65-
LewisShedler(λmax)
66-
67-
Generate sample using Lewis-Shedler algorithm (1979) with
68-
maximum real value `λmax` of the intensity function.
69-
"""
70-
struct LewisShedler{T<:Real} <: PointPatternAlgo
71-
λmax::T
72-
end
73-
74-
"""
75-
ConstantIntensity()
76-
77-
Generate sample assuming the intensity is constant over a `Geometry`
78-
or piecewise constant over a `Domain`.
79-
"""
80-
struct ConstantIntensity <: PointPatternAlgo end
81-
8242
#-----------------
8343
# IMPLEMENTATIONS
8444
#-----------------

src/processes/binomial.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,5 @@ end
1313

1414
ishomogeneous(p::BinomialProcess) = true
1515

16-
defaultalgo(::BinomialProcess, ::Any) = ConstantIntensity()
17-
18-
randsingle(rng::Random.AbstractRNG, p::BinomialProcess, g, ::ConstantIntensity) =
16+
randsingle(rng::Random.AbstractRNG, p::BinomialProcess, g) =
1917
PointSet(sample(rng, g, HomogeneousSampling(p.n)))

src/processes/cluster.jl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ end
2929
ClusterProcess(p::PointProcess, o::PointProcess, gfun::Function) =
3030
ClusterProcess(p, parent -> rand(o, gfun(parent)))
3131

32-
defaultalgo(::ClusterProcess, ::Any) = nothing
33-
34-
function randsingle(rng::Random.AbstractRNG, p::ClusterProcess, g, ::Nothing)
32+
function randsingle(rng::Random.AbstractRNG, p::ClusterProcess, g)
3533
# retrieve parameters
3634
Dim = embeddim(g)
3735
T = coordtype(g)
@@ -50,4 +48,3 @@ function randsingle(rng::Random.AbstractRNG, p::ClusterProcess, g, ::Nothing)
5048
# return point pattern
5149
PointSet(points)
5250
end
53-

src/processes/poisson.jl

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,11 @@ ishomogeneous(p::PoissonProcess{<:Function}) = false
2121

2222
ishomogeneous(p::PoissonProcess{<:AbstractVector}) = false
2323

24-
defaultalgo(::PoissonProcess, ::Any) = ConstantIntensity()
25-
26-
defaultalgo(p::PoissonProcess{<:Function}, g) = LewisShedler(maxintensity(p, g))
27-
28-
function maxintensity(p::PoissonProcess{<:Function}, g)
29-
points = sample(g, HomogeneousSampling(10000))
30-
λmin, λmax = extrema(p.λ, points)
31-
λmax + 0.05 * (λmax - λmin)
32-
end
33-
3424
#------------------
3525
# HOMOGENEOUS CASE
3626
#------------------
3727

38-
function randsingle(rng::Random.AbstractRNG, p::PoissonProcess{<:Real}, g, ::ConstantIntensity)
28+
function randsingle(rng::Random.AbstractRNG, p::PoissonProcess{<:Real}, g)
3929
# simulate number of points
4030
λ = p.λ
4131
V = measure(g)
@@ -49,23 +39,31 @@ end
4939
# INHOMOGENEOUS CASE
5040
#--------------------
5141

52-
function randsingle(rng::Random.AbstractRNG, p::PoissonProcess{<:Function}, g, algo::LewisShedler)
42+
function randsingle(rng::Random.AbstractRNG, p::PoissonProcess{<:Function}, g)
43+
# upper bound for intensity
44+
λmax = maxintensity(p, g)
45+
5346
# simulate a homogeneous process
54-
pp = randsingle(rng, PoissonProcess(algo.λmax), g, ConstantIntensity())
47+
pset = randsingle(rng, PoissonProcess(λmax), g)
5548

5649
# thin point pattern
57-
isnothing(pp) ? nothing : PointSet(collect(thin(pp, RandomThinning(x -> p.λ(x) / algo.λmax))))
50+
isnothing(pset) ? nothing : thin(pset, RandomThinning(x -> p.λ(x) / λmax))
5851
end
5952

60-
randsingle(rng::Random.AbstractRNG, p::PoissonProcess{<:Function}, d::Domain, algo::ConstantIntensity) =
61-
randsingle(rng, PoissonProcess(p.λ.(centroid.(d))), d, algo)
53+
randsingle(rng::Random.AbstractRNG, p::PoissonProcess{<:Function}, d::Domain) =
54+
randsingle(rng, PoissonProcess(p.λ.(centroid.(d))), d)
6255

63-
function randsingle(rng::Random.AbstractRNG, p::PoissonProcess{<:AbstractVector}, d::Domain, algo::ConstantIntensity)
56+
function randsingle(rng::Random.AbstractRNG, p::PoissonProcess{<:AbstractVector}, d::Domain)
6457
# simulate number of points
65-
λ = p.λ
66-
V = measure.(d)
67-
n = rand(rng, Poisson(sum.* V)))
58+
λ = p.λ .* measure(d)
59+
n = rand(rng, Poisson(sum(λ)))
6860

69-
# simulate n points
70-
iszero(n) ? nothing : PointSet(sample(rng, d, HomogeneousSampling(n, λ .* V)))
61+
# simulate point pattern
62+
iszero(n) ? nothing : PointSet(sample(rng, d, HomogeneousSampling(n, λ)))
63+
end
64+
65+
function maxintensity(p::PoissonProcess{<:Function}, g)
66+
points = sample(g, HomogeneousSampling(10000))
67+
λmin, λmax = extrema(p.λ, points)
68+
λmax + 0.05 * (λmax - λmin)
7169
end

src/processes/union.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ end
1414

1515
ishomogeneous(p::UnionProcess) = ishomogeneous(p.p₁) && ishomogeneous(p.p₂)
1616

17-
defaultalgo(::UnionProcess, ::Any) = nothing
18-
19-
function randsingle(rng::Random.AbstractRNG, p::UnionProcess, g, ::Nothing)
17+
function randsingle(rng::Random.AbstractRNG, p::UnionProcess, g)
2018
pp₁ = rand(rng, p.p₁, g)
2119
pp₂ = rand(rng, p.p₂, g)
2220
PointSet([collect(pp₁); collect(pp₂)])

test/processes.jl

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,57 @@
11
@testset "Processes" begin
2+
# geometries and domains
23
seg = Segment((0.0, 0.0), (11.3, 11.3))
34
tri = Triangle((0.0, 0.0), (5.65, 0.0), (5.65, 5.65))
45
quad = Quadrangle((0.0, 0.0), (0.0, 4.0), (4.0, 4.0), (4.0, 0.0))
56
box = Box((0.0, 0.0), (4.0, 4.0))
67
ball = Ball((1.0, 1.0), 2.25)
7-
outer = Point2[(0, -4), (4, -1), (4, 1.5), (0, 3)]
8-
hole1 = Point2[(0.2, -0.2), (1.4, -0.2), (1.4, 0.6), (0.2, 0.6)]
9-
hole2 = Point2[(2, -0.2), (3, -0.2), (3, 0.4), (2, 0.4)]
8+
outer = [(0, -4), (4, -1), (4, 1.5), (0, 3)]
9+
hole1 = [(0.2, -0.2), (1.4, -0.2), (1.4, 0.6), (0.2, 0.6)]
10+
hole2 = [(2, -0.2), (3, -0.2), (3, 0.4), (2, 0.4)]
1011
poly = PolyArea(outer, [hole1, hole2])
1112
grid = CartesianGrid((0, 0), (4, 4), dims=(10, 10))
1213
points = Point2[(0, 0), (4.5, 0), (0, 4.2), (4, 4.3), (1.5, 1.5)]
1314
connec = connect.([(1, 2, 5), (2, 4, 5), (4, 3, 5), (3, 1, 5)], Triangle)
1415
mesh = SimpleMesh(points, connec)
16+
geoms = [seg, tri, quad, box, ball, poly, grid, mesh]
17+
18+
# point processes
19+
λ(s) = sum(coordinates(s) .^ 2)
20+
binom = BinomialProcess(100)
21+
poisson1 = PoissonProcess(100.0)
22+
poisson2 = PoissonProcess(λ)
23+
procs = [binom, poisson1, poisson2]
1524

1625
@testset "Basic" begin
17-
for p in [BinomialProcess(100), PoissonProcess(100.0)]
18-
b = Box((0.0, 1.0), (1.0, 2.0))
19-
pp = rand(p, b)
20-
xs = coordinates.(pp)
21-
@test all(0 .≤ first.(xs) .≤ 1)
22-
@test all(1 .≤ last.(xs) .≤ 2)
26+
for p in procs, g in geoms
27+
pp = rand(p, g)
28+
@test all((g), pp)
2329
end
2430
end
2531

2632
@testset "Binomial" begin
2733
p = BinomialProcess(10)
28-
for g in [seg, tri, quad, box, ball, poly, grid, mesh]
34+
for g in geoms
2935
pp = rand(p, g)
3036
@test nelements(pp) == 10
31-
@test all((g), pp)
3237
end
3338
end
3439

3540
@testset "Poisson" begin
36-
# helper function
37-
λ(s::Point2) = sum(coordinates(s) .^ 2)
38-
39-
# homogeneous
40-
p = PoissonProcess(10.0)
41-
for g in [seg, tri, quad, box, ball, poly, grid, mesh]
42-
pp = rand(p, g)
43-
@test all((g), pp)
44-
end
45-
46-
# inhomogeneous with intensity function
47-
p = PoissonProcess(λ)
48-
for g in [seg, tri, quad, box, ball, poly, grid, mesh]
49-
pp = rand(p, g)
50-
@test all((g), pp)
51-
52-
pp = rand(p, g, algo=LewisShedler(λ(Point2(12.0, 12.0))))
53-
@test all((g), pp)
54-
55-
g = discretize(g)
56-
pp = rand(p, g, algo=ConstantIntensity())
57-
@test all((g), g)
58-
end
59-
6041
# inhomogeneous with piecewise constant intensity
6142
for g in [grid, mesh]
6243
λvec = λ.(centroid.(g))
6344
p = PoissonProcess(λvec)
64-
# discretizedsampling by default
6545
pp = rand(p, g)
6646
@test all((g), pp)
6747
end
6848

6949
# empty pointsets
70-
for g in [seg, tri, quad, box, ball, poly, grid, mesh]
50+
for g in geoms
7151
@test isnothing(rand(PoissonProcess(0.0), seg))
7252
end
73-
ps = PointSet(rand(Point2, 10))
74-
@test isnothing(rand(PoissonProcess(100.0), ps))
53+
pp = PointSet(rand(Point2, 10))
54+
@test isnothing(rand(PoissonProcess(100.0), pp))
7555
end
7656

7757
@testset "Union" begin

0 commit comments

Comments
 (0)