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

Commit 592842d

Browse files
committed
Add PointPattern
1 parent cd15272 commit 592842d

File tree

5 files changed

+105
-4
lines changed

5 files changed

+105
-4
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ version = "0.1.0"
66
[deps]
77
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
88
GeoStatsBase = "323cb8eb-fbf6-51c0-afd0-f8fba70507b2"
9+
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
910

1011
[compat]
1112
Distributions = "0.21"
1213
GeoStatsBase = "0.7"
14+
StaticArrays = "0.12"
1315
julia = "1"
1416

1517
[extras]

src/PointPatterns.jl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,22 @@ module PointPatterns
66

77
using GeoStatsBase
88

9-
using Distributions
9+
using Distributions: Uniform, Poisson, product_distribution
10+
using StaticArrays: SVector, MVector
1011

11-
# point processes
12+
import GeoStatsBase: npoints, coordinates, coordinates!
13+
14+
include("pattern.jl")
1215
include("processes.jl")
1316

1417
export
18+
# point pattern
19+
PointPattern,
20+
npoints,
21+
coordinates,
22+
coordinates!,
23+
24+
# point processes
1525
PointProcess,
1626
BinomialProcess,
1727
PoissonProcess

src/pattern.jl

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# ------------------------------------------------------------------
2+
# Licensed under the ISC License. See LICENSE in the project root.
3+
# ------------------------------------------------------------------
4+
5+
"""
6+
PointPattern(coords)
7+
8+
A set of points with coordinate matrix `coords`. The number of rows
9+
of the matrix is the dimensionality of the pattern whereas the number
10+
of columns is the number of points in the pattern. Alternatively, `coords`
11+
can be a vector of tuples (i.e. points).
12+
"""
13+
struct PointPattern{T,N}
14+
coords::Matrix{T}
15+
end
16+
17+
PointPattern(coords::AbstractMatrix{T}) where {T} =
18+
PointPattern{T,size(coords,1)}(coords)
19+
20+
PointPattern(coords::AbstractVector{NTuple{N,T}}) where {N,T} =
21+
PointPattern([c[i] for i in 1:N, c in coords])
22+
23+
npoints(pp::PointPattern) = size(pp.coords, 2)
24+
25+
"""
26+
coordinates(pp, ind)
27+
28+
Return the coordinates of the `ind`-th point in the point pattern `pp`.
29+
"""
30+
function coordinates(pp::PointPattern{T,N}, ind::Int) where {N,T}
31+
x = MVector{N,T}(undef)
32+
coordinates!(x, pp, ind)
33+
x
34+
end
35+
36+
"""
37+
coordinates(pp, inds)
38+
39+
Return the coordinates of the points `inds` in the point pattern `pp`.
40+
"""
41+
function coordinates(pp::PointPattern{T,N}, inds::AbstractVector{Int}) where {N,T}
42+
X = Matrix{T}(undef, N, length(inds))
43+
coordinates!(X, pp, inds)
44+
X
45+
end
46+
47+
"""
48+
coordinates(pp)
49+
50+
Return the coordinates of all points in point pattern `pp`.
51+
"""
52+
coordinates(pp::PointPattern) = coordinates(pp, 1:npoints(pp))
53+
54+
"""
55+
coordinates!(buff, pp, inds)
56+
57+
Non-allocating version of [`coordinates`](@ref)
58+
"""
59+
function coordinates!(buff::AbstractMatrix, pp::PointPattern,
60+
inds::AbstractVector{Int})
61+
for j in 1:length(inds)
62+
coordinates!(view(buff,:,j), pp, inds[j])
63+
end
64+
end
65+
66+
"""
67+
coordinates!(buff, pp, ind)
68+
69+
Non-allocating version of [`coordinates`](@ref).
70+
"""
71+
function coordinates!(buff::AbstractVector{T}, pp::PointPattern{T,N},
72+
ind::Int) where {N,T}
73+
for i in 1:N
74+
@inbounds buff[i] = pp.coords[i,ind]
75+
end
76+
end
77+
78+
# ------------
79+
# IO methods
80+
# ------------
81+
function Base.show(io::IO, pp::PointPattern{T,N}) where {N,T}
82+
npts = size(pp.coords, 2)
83+
print(io, "$npts PointPattern{$T,$N}")
84+
end
85+
86+
function Base.show(io::IO, ::MIME"text/plain", pp::PointPattern{T,N}) where {N,T}
87+
println(io, pp)
88+
Base.print_array(io, pp.coords)
89+
end

src/processes/binomial.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ function rand_single(p::BinomialProcess, r::RectangleRegion{T,N}) where {N,T}
2020
U = product_distribution([Uniform(lo[i], up[i]) for i in 1:N])
2121

2222
# return point pattern
23-
PointSet(rand(U, p.n))
23+
PointPattern(rand(U, p.n))
2424
end

src/processes/poisson.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ function rand_single(p::PoissonProcess, r::RectangleRegion{T,N}) where {N,T}
2525
U = product_distribution([Uniform(lo[i], up[i]) for i in 1:N])
2626

2727
# return point pattern
28-
PointSet(rand(U, n))
28+
PointPattern(rand(U, n))
2929
end

0 commit comments

Comments
 (0)