Skip to content

Commit

Permalink
Fix some inbounds errors (#867)
Browse files Browse the repository at this point in the history
- Addresses #638
- Added tests
  • Loading branch information
itsdebartha committed Jun 21, 2023
1 parent 586066d commit a39f189
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/deviation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function counteq(a::AbstractArray, b::AbstractArray)
n = length(a)
length(b) == n || throw(DimensionMismatch("Inconsistent lengths."))
c = 0
for i = 1:n
for i in eachindex(a, b)
@inbounds if a[i] == b[i]
c += 1
end
Expand All @@ -31,7 +31,7 @@ function countne(a::AbstractArray, b::AbstractArray)
n = length(a)
length(b) == n || throw(DimensionMismatch("Inconsistent lengths."))
c = 0
for i = 1:n
for i in eachindex(a, b)
@inbounds if a[i] != b[i]
c += 1
end
Expand All @@ -50,7 +50,7 @@ function sqL2dist(a::AbstractArray{T}, b::AbstractArray{T}) where T<:Number
n = length(a)
length(b) == n || throw(DimensionMismatch("Input dimension mismatch"))
r = 0.0
for i = 1:n
for i in eachindex(a, b)
@inbounds r += abs2(a[i] - b[i])
end
return r
Expand Down Expand Up @@ -78,7 +78,7 @@ function L1dist(a::AbstractArray{T}, b::AbstractArray{T}) where T<:Number
n = length(a)
length(b) == n || throw(DimensionMismatch("Input dimension mismatch"))
r = 0.0
for i = 1:n
for i in eachindex(a, b)
@inbounds r += abs(a[i] - b[i])
end
return r
Expand All @@ -97,7 +97,7 @@ function Linfdist(a::AbstractArray{T}, b::AbstractArray{T}) where T<:Number
n = length(a)
length(b) == n || throw(DimensionMismatch("Input dimension mismatch"))
r = 0.0
for i = 1:n
for i in eachindex(a, b)
@inbounds v = abs(a[i] - b[i])
if r < v
r = v
Expand All @@ -118,7 +118,7 @@ Efficient equivalent of `sum(a*log(a/b)-a+b)`.
function gkldiv(a::AbstractArray{T}, b::AbstractArray{T}) where T<:AbstractFloat
n = length(a)
r = 0.0
for i = 1:n
for i in eachindex(a, b)
@inbounds ai = a[i]
@inbounds bi = b[i]
if ai > 0
Expand Down
24 changes: 23 additions & 1 deletion test/deviation.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using StatsBase
using StatsBase, OffsetArrays
using Test

a = [1, 2, 3, 4, 5, 6, 7]
Expand All @@ -7,6 +7,12 @@ b = [1, 3, 3, 4, 6, 7, 8]
@test counteq(a, b) == 3
@test countne(a, b) == 4

a = OffsetArray(a, -5:1)
b = OffsetArray(b, -5:1)

@test counteq(a, b) == 3
@test countne(a, b) == 4

a = rand(5, 6)
b = rand(5, 6)

Expand All @@ -24,3 +30,19 @@ b = rand(5, 6)
@test rmsd(a, b; normalize=true) rmsd(a, b) / (maximum(a) - minimum(a))
@test psnr(a, b, 2) 10 * log10(4 / msd(a, b))

a = OffsetArray(a, 5, -10)
b = OffsetArray(b, 5, -10)

@test sqL2dist(a, b) sum(abs2.(a - b))
@test L2dist(a, b) sqrt(sqL2dist(a, b))
@test L1dist(a, b) sum(abs.(a - b))
@test Linfdist(a, b) maximum(abs.(a - b))

@test gkldiv(a, b) sum(a .* log.(a ./ b) - a + b)

@test meanad(a, b) mean(abs.(a - b))
@test maxad(a, b) maximum(abs.(a - b))
@test msd(a, b) mean(abs2.(a - b))
@test rmsd(a, b) sqrt(msd(a, b))
@test rmsd(a, b; normalize=true) rmsd(a, b) / (maximum(a) - minimum(a))
@test psnr(a, b, 2) 10 * log10(4 / msd(a, b))

0 comments on commit a39f189

Please sign in to comment.