Skip to content

Commit b789cb7

Browse files
authored
Default args, whitespace, docstrings (#41)
The only functional change here is to call `pairwise_consts` only if *both* `pσ` and `pϕ` are `nothing`. The implementation discards any user-supplied values, and this could lead to surprising results if the user supplies one but not the other. Accepting the error when you try to index `nothing` seems like the safer approach, as it will force the user to supply both or neither. Aside from that, this just cleans up whitespace and fixes some docstrings.
1 parent 1de4994 commit b789cb7

File tree

3 files changed

+23
-29
lines changed

3 files changed

+23
-29
lines changed

src/GaussianMixtureAlignment.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
GaussianMixtureAlignment.jl
33
===========================
44
5-
GaussianMixtureAlignment.jl is a package used to align Gaussian mixture models. In particular, it uses an implementation
6-
of the [GOGMA algorithm (Campbell, 2016)](https://arxiv.org/abs/1603.00150) to find globally optimal alignments of mixtures of
5+
GaussianMixtureAlignment.jl is a package used to align Gaussian mixture models. In particular, it uses an implementation
6+
of the [GOGMA algorithm (Campbell, 2016)](https://arxiv.org/abs/1603.00150) to find globally optimal alignments of mixtures of
77
isotropic (spherical) Gaussian distributions.
88
99
REPL help

src/gogma/overlap.jl

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
ovlp = overlap(distsq, s, w)
33
4-
Calculates the unnormalized overlap between two Gaussian distributions with width `s`,
4+
Calculates the unnormalized overlap between two Gaussian distributions with width `s`,
55
weight `w', and squared distance `distsq`.
66
"""
77
function overlap(distsq::Real, s::Real, w::Real)
@@ -16,36 +16,30 @@ end
1616
Calculates the unnormalized overlap between two Gaussian distributions with variances
1717
`σx` and `σy`, weights `ϕx` and `ϕy`, and means separated by distance `dist`.
1818
"""
19-
function overlap(dist::Real, σx::Real, σy::Real, ϕx::Real, ϕy::Real)
19+
function overlap(dist::Real, σx::Real, σy::Real, ϕx::Real, ϕy::Real)
2020
return overlap(dist^2, σx^2 + σy^2, ϕx*ϕy)
2121
end
2222

2323
"""
24-
ovlp = overlap(x::IsotropicGaussian, y::IsotropicGaussian, xtform=identity)
24+
ovlp = overlap(x::IsotropicGaussian, y::IsotropicGaussian)
2525
2626
Calculates the unnormalized overlap between two `IsotropicGaussian` objects.
2727
"""
28-
function overlap(x::AbstractIsotropicGaussian, y::AbstractIsotropicGaussian, s=nothing, w=nothing)
29-
if isnothing(s)
30-
s = x.σ^2 + y.σ^2
31-
end
32-
if isnothing(w)
33-
w = x.ϕ*y.ϕ
34-
end
35-
return overlap(sum(abs2, x.μ.-y.μ), s, w)
28+
function overlap(x::AbstractIsotropicGaussian, y::AbstractIsotropicGaussian, s=x.σ^2+y.σ^2, w=x.ϕ*y.ϕ)
29+
return overlap(sum(abs2, x.μ.-y.μ), s, w)
3630
end
3731

3832
"""
39-
ovlp = overlap(x::AbstractSingleGMM, y::AbstractSingleGMM, xtform=identity)
33+
ovlp = overlap(x::AbstractSingleGMM, y::AbstractSingleGMM)
4034
4135
Calculates the unnormalized overlap between two `AbstractSingleGMM` objects.
4236
"""
4337
function overlap(x::AbstractSingleGMM, y::AbstractSingleGMM, pσ=nothing, pϕ=nothing)
4438
# prepare pairwise widths and weights, if not provided
45-
if isnothing(pσ) || isnothing(pϕ)
39+
if isnothing(pσ) && isnothing(pϕ)
4640
pσ, pϕ = pairwise_consts(x, y)
4741
end
48-
42+
4943
# sum overlaps for all pairwise combinations of Gaussians between x and y
5044
ovlp = zero(promote_type(numbertype(x),numbertype(y)))
5145
for (i,gx) in enumerate(x.gaussians)
@@ -57,16 +51,16 @@ function overlap(x::AbstractSingleGMM, y::AbstractSingleGMM, pσ=nothing, pϕ=no
5751
end
5852

5953
"""
60-
ovlp = overlap(x::AbstractMultiGMM, y::AbstractMultiGMM, xtform=identity)
54+
ovlp = overlap(x::AbstractMultiGMM, y::AbstractMultiGMM)
6155
6256
Calculates the unnormalized overlap between two `AbstractMultiGMM` objects.
6357
"""
6458
function overlap(x::AbstractMultiGMM, y::AbstractMultiGMM, mpσ=nothing, mpϕ=nothing, interactions=nothing)
6559
# prepare pairwise widths and weights, if not provided
66-
if isnothing(mpσ) || isnothing(mpϕ)
60+
if isnothing(mpσ) && isnothing(mpϕ)
6761
mpσ, mpϕ = pairwise_consts(x, y, interactions)
6862
end
69-
63+
7064
# sum overlaps from each keyed pairs of GMM
7165
ovlp = zero(promote_type(numbertype(x),numbertype(y)))
7266
for k1 in keys(mpσ)
@@ -76,9 +70,9 @@ function overlap(x::AbstractMultiGMM, y::AbstractMultiGMM, mpσ=nothing, mpϕ=no
7670
end
7771
return ovlp
7872
end
79-
73+
8074
"""
81-
l2dist = distance(x, y)
75+
l2dist = distance(x, y)
8276
8377
Calculates the L2 distance between two GMMs made up of spherical Gaussian distributions.
8478
"""
@@ -87,7 +81,7 @@ function distance(x::AbstractGMM, y::AbstractGMM)
8781
end
8882

8983
"""
90-
tani = tanimoto(x, y)
84+
tani = tanimoto(x, y)
9185
9286
Calculates the tanimoto distance based on Gaussian overlap between two GMMs.
9387
"""

test/runtests.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ const GMA = GaussianMixtureAlignment
5050
lb = gauss_l2_bounds(x,y,RotationRegion/2/sqrt3))[1]
5151
@test lb -GMA.overlap(5^2,2*σ^2*ϕ)
5252
lb = gauss_l2_bounds(x,y,RotationRegion(RotationVec(0,0/4),SVector{3}(0.,0.,0.),π/4/(sqrt3)))[1]
53-
@test lb -GMA.overlap(5^2,2*σ^2*ϕ)
54-
53+
@test lb -GMA.overlap(5^2,2*σ^2*ϕ)
54+
5555
# translation distance, no rotation
5656
# translation region centered at origin
5757
lb, ub = gauss_l2_bounds(x,y,TranslationRegion(1/sqrt3))
@@ -61,7 +61,7 @@ const GMA = GaussianMixtureAlignment
6161
lb, ub = gauss_l2_bounds(x+SVector(1,0,0),y,TranslationRegion(1/sqrt3))
6262
@test lb -GMA.overlap(7^2,2*σ^2*ϕ)
6363
@test ub -GMA.overlap(8^2,2*σ^2*ϕ)
64-
# centered with translation of 3 in +y
64+
# centered with translation of 3 in +y
6565
lb, ub = gauss_l2_bounds(x+SVector(0,3,0),y,TranslationRegion(1/sqrt3))
6666
@test lb -GMA.overlap(((58)-1)^2,2*σ^2*ϕ)
6767
@test ub -GMA.overlap(58,2*σ^2*ϕ)
@@ -97,7 +97,7 @@ end
9797

9898
@testset "bounds for shrinking searchspace around an optimum" begin
9999
# two sets of points, each forming a 3-4-5 triangle
100-
xpts = [[0.,0.,0.], [3.,0.,0.,], [0.,4.,0.]]
100+
xpts = [[0.,0.,0.], [3.,0.,0.,], [0.,4.,0.]]
101101
ypts = [[1.,1.,1.], [1.,-2.,1.], [1.,1.,-3.]]
102102
σ = ϕ = 1.
103103
gmmx = IsotropicGMM([IsotropicGaussian(x, σ, ϕ) for x in xpts])
@@ -121,7 +121,7 @@ end
121121

122122
@testset "GOGMA runs without errors" begin
123123
# two sets of points, each forming a 3-4-5 triangle
124-
xpts = [[0.,0.,0.], [3.,0.,0.,], [0.,4.,0.]]
124+
xpts = [[0.,0.,0.], [3.,0.,0.,], [0.,4.,0.]]
125125
ypts = [[1.,1.,1.], [1.,-2.,1.], [1.,1.,-3.]]
126126
σ = ϕ = 1.
127127
gmmx = IsotropicGMM([IsotropicGaussian(x, σ, ϕ) for x in xpts])
@@ -175,7 +175,7 @@ end
175175
end
176176

177177
@testset "GO-ICP and GO-IH run without errors" begin
178-
xpts = [[0.,0.,0.], [3.,0.,0.,], [0.,4.,0.]]
178+
xpts = [[0.,0.,0.], [3.,0.,0.,], [0.,4.,0.]]
179179
ypts = [[1.,1.,1.], [1.,-2.,1.], [1.,1.,-3.]]
180180

181181
xset = PointSet(xpts);
@@ -189,7 +189,7 @@ end
189189
end
190190

191191
@testset "Kabsch" begin
192-
xpts = [[0.,0.,0.], [3.,0.,0.,], [0.,4.,0.]]
192+
xpts = [[0.,0.,0.], [3.,0.,0.,], [0.,4.,0.]]
193193
ypts = [[1.,1.,1.], [1.,-2.,1.], [1.,1.,-3.]]
194194

195195
xset = PointSet(xpts, ones(3))

0 commit comments

Comments
 (0)