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

Commit ed1e69e

Browse files
committed
Refactor code
1 parent 0ac50f7 commit ed1e69e

File tree

5 files changed

+57
-55
lines changed

5 files changed

+57
-55
lines changed

src/processes.jl

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -86,34 +86,4 @@ struct ConstantIntensity <: PointPatternAlgo end
8686
include("processes/binomial.jl")
8787
include("processes/poisson.jl")
8888
include("processes/cluster.jl")
89-
90-
# ----------------
91-
# UNION PROCESSES
92-
# ----------------
93-
94-
"""
95-
UnionProcess(p₁, p₂)
96-
97-
Union (or superposition) of spatial point processes `p₁` and `p₂`.
98-
"""
99-
struct UnionProcess{P₁<:PointProcess,P₂<:PointProcess} <: PointProcess
100-
p₁::P₁
101-
p₂::P₂
102-
end
103-
104-
"""
105-
p₁ ∪ p₂
106-
107-
Return the union of point processes `p₁` and `p₂`.
108-
"""
109-
Base.union(p₁::PointProcess, p₂::PointProcess) = UnionProcess(p₁, p₂)
110-
111-
ishomogeneous(p::UnionProcess) = ishomogeneous(p.p₁) && ishomogeneous(p.p₂)
112-
113-
default_sampling_algorithm(::UnionProcess, ::Any) = nothing
114-
115-
function rand_single(rng::Random.AbstractRNG, p::UnionProcess, g, ::Nothing)
116-
pp₁ = rand(rng, p.p₁, g)
117-
pp₂ = rand(rng, p.p₂, g)
118-
PointSet([coordinates.(pp₁); coordinates.(pp₂)])
119-
end
89+
include("processes/union.jl")

src/processes/binomial.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,5 @@ ishomogeneous(p::BinomialProcess) = true
1515

1616
default_sampling_algorithm(::BinomialProcess, ::Any) = ConstantIntensity()
1717

18-
function rand_single(rng::Random.AbstractRNG, p::BinomialProcess, g, ::ConstantIntensity)
19-
points = sample(rng, g, HomogeneousSampling(p.n))
20-
PointSet(points)
21-
end
18+
rand_single(rng::Random.AbstractRNG, p::BinomialProcess, g, ::ConstantIntensity) =
19+
PointSet(sample(rng, g, HomogeneousSampling(p.n)))

src/processes/cluster.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
# ------------------------------------------------------------------
44

55
"""
6-
ClusterProcess(p, ofun)
6+
ClusterProcess(proc, ofun)
77
8-
A cluster process with parent process `p` and offsprings generated
8+
A cluster process with parent process `proc` and offsprings generated
99
with `ofun`. It is a function that takes a parent point and returns
1010
a point pattern from another point process.
1111
12-
ClusterProcess(p, o, gfun)
12+
ClusterProcess(proc, offs, gfun)
1313
14-
Alternatively, specify the parent process `p`, the offspring process `o`
15-
and the geometry function `gfun`. It is a function that takes a parent
16-
point and returns a geometry or domain for the offspring process.
14+
Alternatively, specify the parent process `proc`, the offspring process
15+
`offs` and the geometry function `gfun`. It is a function that takes a
16+
parent point and returns a geometry or domain for the offspring process.
1717
1818
## Examples
1919
@@ -22,22 +22,22 @@ point and returns a geometry or domain for the offspring process.
2222
```
2323
"""
2424
struct ClusterProcess{P<:PointProcess,F<:Function} <: PointProcess
25-
p::P
25+
proc::P
2626
ofun::F
2727
end
2828

2929
ClusterProcess(p::PointProcess, o::PointProcess, gfun::Function) =
3030
ClusterProcess(p, parent -> rand(o, gfun(parent)))
3131

32-
default_sampling_algorithm(p::ClusterProcess, g) = nothing
32+
default_sampling_algorithm(::ClusterProcess, ::Any) = nothing
3333

3434
function rand_single(rng::Random.AbstractRNG, p::ClusterProcess, g, ::Nothing)
3535
# retrieve parameters
3636
Dim = embeddim(g)
3737
T = coordtype(g)
3838

3939
# generate parents
40-
parents = rand_single(rng, p.p, g)
40+
parents = rand(rng, p.proc, g)
4141

4242
# generate offsprings
4343
offsprings = p.ofun.(parents)

src/processes/poisson.jl

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,6 @@ struct PoissonProcess{L<:Union{Real,Function,AbstractVector}} <: PointProcess
1515
λ::L
1616
end
1717

18-
Base.union(p₁::PoissonProcess{<:Real}, p₂::PoissonProcess{<:Real}) = PoissonProcess(p₁.λ + p₂.λ)
19-
20-
Base.union(p₁::PoissonProcess{<:Function}, p₂::PoissonProcess{<:Function}) = PoissonProcess(x -> p₁.λ(x) + p₂.λ(x))
21-
22-
Base.union(p₁::PoissonProcess{<:Real}, p₂::PoissonProcess{<:Function}) = PoissonProcess(x -> p₁.λ + p₂.λ(x))
23-
24-
Base.union(p₁::PoissonProcess{<:Function}, p₂::PoissonProcess{<:Real}) = PoissonProcess(x -> p₁.λ(x) + p₂.λ)
25-
26-
Base.union(p₁::PoissonProcess{<:AbstractVector}, p₂::PoissonProcess{<:AbstractVector}) =
27-
PoissonProcess(x -> p₁.λ + p₂.λ)
28-
2918
ishomogeneous(p::PoissonProcess{<:Real}) = true
3019

3120
ishomogeneous(p::PoissonProcess{<:Function}) = false

src/processes/union.jl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# ------------------------------------------------------------------
2+
# Licensed under the MIT License. See LICENSE in the project root.
3+
# ------------------------------------------------------------------
4+
5+
"""
6+
UnionProcess(p₁, p₂)
7+
8+
Union (or superposition) of spatial point processes `p₁` and `p₂`.
9+
"""
10+
struct UnionProcess{P₁<:PointProcess,P₂<:PointProcess} <: PointProcess
11+
p₁::P₁
12+
p₂::P₂
13+
end
14+
15+
ishomogeneous(p::UnionProcess) = ishomogeneous(p.p₁) && ishomogeneous(p.p₂)
16+
17+
default_sampling_algorithm(::UnionProcess, ::Any) = nothing
18+
19+
function rand_single(rng::Random.AbstractRNG, p::UnionProcess, g, ::Nothing)
20+
pp₁ = rand(rng, p.p₁, g)
21+
pp₂ = rand(rng, p.p₂, g)
22+
PointSet([collect(pp₁); collect(pp₂)])
23+
end
24+
25+
# ----------
26+
# OPERATION
27+
# ----------
28+
29+
"""
30+
p₁ ∪ p₂
31+
32+
Return the union of point processes `p₁` and `p₂`.
33+
"""
34+
Base.union(p₁::PointProcess, p₂::PointProcess) = UnionProcess(p₁, p₂)
35+
36+
Base.union(p₁::PoissonProcess{<:Real}, p₂::PoissonProcess{<:Real}) = PoissonProcess(p₁.λ + p₂.λ)
37+
38+
Base.union(p₁::PoissonProcess{<:Function}, p₂::PoissonProcess{<:Function}) = PoissonProcess(x -> p₁.λ(x) + p₂.λ(x))
39+
40+
Base.union(p₁::PoissonProcess{<:Real}, p₂::PoissonProcess{<:Function}) = PoissonProcess(x -> p₁.λ + p₂.λ(x))
41+
42+
Base.union(p₁::PoissonProcess{<:Function}, p₂::PoissonProcess{<:Real}) = PoissonProcess(x -> p₁.λ(x) + p₂.λ)
43+
44+
Base.union(p₁::PoissonProcess{<:AbstractVector}, p₂::PoissonProcess{<:AbstractVector}) =
45+
PoissonProcess(x -> p₁.λ + p₂.λ)

0 commit comments

Comments
 (0)