Skip to content

Commit 6773f81

Browse files
authored
Evaluate GMM at a point (#46)
This supports evaluating a GMM `gmm` at a point `pt` with the syntax `gmm(pt)`.
1 parent a52eb6f commit 6773f81

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "GaussianMixtureAlignment"
22
uuid = "f2431ed1-b9c2-4fdb-af1b-a74d6c93b3b3"
33
authors = ["Tom McGrath <[email protected]> and contributors"]
4-
version = "0.2"
4+
version = "0.2.1"
55

66
[deps]
77
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"

src/gogma/gmm.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ weights(mgmm::AbstractMultiGMM) = vcat([weights(gmm) for (k,gmm) in mgmm.gmms]..
6161
widths(mgmm::AbstractMultiGMM) = vcat([widths(gmm) for (k,gmm) in mgmm.gmms]...)
6262

6363
"""
64-
A structure that defines an isotropic Gaussian distribution with the location of the mean, `μ`, standard deviation `σ`,
65-
and scaling factor `ϕ`.
64+
A structure that defines an isotropic Gaussian distribution with the location of the mean, `μ`, standard deviation `σ`,
65+
and scaling factor `ϕ`.
6666
6767
"""
6868
struct IsotropicGaussian{N,T} <: AbstractIsotropicGaussian{N,T}
@@ -80,8 +80,9 @@ end
8080
IsotropicGaussian(g::AbstractIsotropicGaussian) = IsotropicGaussian(g.μ, g.σ, g.ϕ)
8181

8282
convert(::Type{IsotropicGaussian{N,T}}, g::AbstractIsotropicGaussian) where {N,T} = IsotropicGaussian(SVector{N,T}(g.μ), T(g.σ), T(g.ϕ))
83-
promote_rule(::Type{IsotropicGaussian{N,T}}, ::Type{IsotropicGaussian{N,S}}) where {N,T<:Real,S<:Real} = IsotropicGaussian{N,promote_type(T,S)}
83+
promote_rule(::Type{IsotropicGaussian{N,T}}, ::Type{IsotropicGaussian{N,S}}) where {N,T<:Real,S<:Real} = IsotropicGaussian{N,promote_type(T,S)}
8484

85+
(g::IsotropicGaussian)(pos::AbstractVector) = exp(-sum(abs2, pos-g.μ)/(2*g.σ^2))*g.ϕ
8586

8687
"""
8788
A collection of `IsotropicGaussian`s, making up a Gaussian Mixture Model (GMM).
@@ -95,9 +96,11 @@ IsotropicGMM(gmm::AbstractIsotropicGMM) = IsotropicGMM(gmm.gaussians)
9596
convert(t::Type{IsotropicGMM}, gmm::AbstractIsotropicGMM) = t(gmm.gaussians)
9697
promote_rule(::Type{IsotropicGMM{N,T}}, ::Type{IsotropicGMM{N,S}}) where {T,S,N} = IsotropicGMM{N,promote_type(T,S)}
9798

99+
(gmm::IsotropicGMM)(pos::AbstractVector) = sum(g(pos) for g in gmm.gaussians)
100+
98101
"""
99-
A collection of labeled `IsotropicGMM`s, to each be considered separately during an alignment procedure. That is,
100-
only alignment scores between `IsotropicGMM`s with the same key are considered when aligning two `MultiGMM`s.
102+
A collection of labeled `IsotropicGMM`s, to each be considered separately during an alignment procedure. That is,
103+
only alignment scores between `IsotropicGMM`s with the same key are considered when aligning two `MultiGMM`s.
101104
"""
102105
struct IsotropicMultiGMM{N,T,K} <: AbstractIsotropicMultiGMM{N,T,K}
103106
gmms::Dict{K, IsotropicGMM{N,T}}

src/gogma/overlap.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ function force!(f::AbstractVector, x::AbstractIsotropicGMM, y::AbstractIsotropic
123123
end
124124

125125
function force!(f::AbstractVector, x::AbstractMultiGMM, y::AbstractMultiGMM; interactions=nothing)
126-
mpσ, mpϕ = pairwise_consts(x, y, interactions)
126+
mpσ, mpϕ = pairwise_consts(x, y, interactions)
127127
for k1 in keys(mpσ)
128128
for k2 in keys(mpσ[k1])
129129
# don't pass coef as a keyword argument, since the interaction coefficient is baked into mpϕ

test/runtests.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,17 @@ end
141141
@test isapprox(rocs_align(gmmx, gmmy).minimum, -overlap(gmmx,gmmx); atol=1E-12)
142142
end
143143

144+
@testset "Evaluation at a point" begin
145+
pts = [[0.,0.,0.], [3.,0.,0.,], [0.,4.,0.]]
146+
σ = ϕ = 1.
147+
gmm = IsotropicGMM([IsotropicGaussian(x, σ, ϕ) for x in pts])
148+
149+
pt = [1, 1, 1]
150+
gpt = [g(pt) for g in gmm.gaussians]
151+
@test gpt [exp(-3/2), exp(-6/2), exp(-11/2)]
152+
@test gmm(pt) == sum(gpt)
153+
end
154+
144155
@testset "MultiGMMs with interactions" begin
145156
tetrahedral = [
146157
[0.,0.,1.],

0 commit comments

Comments
 (0)