Skip to content

Commit a39f189

Browse files
authored
Fix some inbounds errors (#867)
- Addresses #638 - Added tests
1 parent 586066d commit a39f189

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

src/deviation.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function counteq(a::AbstractArray, b::AbstractArray)
1212
n = length(a)
1313
length(b) == n || throw(DimensionMismatch("Inconsistent lengths."))
1414
c = 0
15-
for i = 1:n
15+
for i in eachindex(a, b)
1616
@inbounds if a[i] == b[i]
1717
c += 1
1818
end
@@ -31,7 +31,7 @@ function countne(a::AbstractArray, b::AbstractArray)
3131
n = length(a)
3232
length(b) == n || throw(DimensionMismatch("Inconsistent lengths."))
3333
c = 0
34-
for i = 1:n
34+
for i in eachindex(a, b)
3535
@inbounds if a[i] != b[i]
3636
c += 1
3737
end
@@ -50,7 +50,7 @@ function sqL2dist(a::AbstractArray{T}, b::AbstractArray{T}) where T<:Number
5050
n = length(a)
5151
length(b) == n || throw(DimensionMismatch("Input dimension mismatch"))
5252
r = 0.0
53-
for i = 1:n
53+
for i in eachindex(a, b)
5454
@inbounds r += abs2(a[i] - b[i])
5555
end
5656
return r
@@ -78,7 +78,7 @@ function L1dist(a::AbstractArray{T}, b::AbstractArray{T}) where T<:Number
7878
n = length(a)
7979
length(b) == n || throw(DimensionMismatch("Input dimension mismatch"))
8080
r = 0.0
81-
for i = 1:n
81+
for i in eachindex(a, b)
8282
@inbounds r += abs(a[i] - b[i])
8383
end
8484
return r
@@ -97,7 +97,7 @@ function Linfdist(a::AbstractArray{T}, b::AbstractArray{T}) where T<:Number
9797
n = length(a)
9898
length(b) == n || throw(DimensionMismatch("Input dimension mismatch"))
9999
r = 0.0
100-
for i = 1:n
100+
for i in eachindex(a, b)
101101
@inbounds v = abs(a[i] - b[i])
102102
if r < v
103103
r = v
@@ -118,7 +118,7 @@ Efficient equivalent of `sum(a*log(a/b)-a+b)`.
118118
function gkldiv(a::AbstractArray{T}, b::AbstractArray{T}) where T<:AbstractFloat
119119
n = length(a)
120120
r = 0.0
121-
for i = 1:n
121+
for i in eachindex(a, b)
122122
@inbounds ai = a[i]
123123
@inbounds bi = b[i]
124124
if ai > 0

test/deviation.jl

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using StatsBase
1+
using StatsBase, OffsetArrays
22
using Test
33

44
a = [1, 2, 3, 4, 5, 6, 7]
@@ -7,6 +7,12 @@ b = [1, 3, 3, 4, 6, 7, 8]
77
@test counteq(a, b) == 3
88
@test countne(a, b) == 4
99

10+
a = OffsetArray(a, -5:1)
11+
b = OffsetArray(b, -5:1)
12+
13+
@test counteq(a, b) == 3
14+
@test countne(a, b) == 4
15+
1016
a = rand(5, 6)
1117
b = rand(5, 6)
1218

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

33+
a = OffsetArray(a, 5, -10)
34+
b = OffsetArray(b, 5, -10)
35+
36+
@test sqL2dist(a, b) sum(abs2.(a - b))
37+
@test L2dist(a, b) sqrt(sqL2dist(a, b))
38+
@test L1dist(a, b) sum(abs.(a - b))
39+
@test Linfdist(a, b) maximum(abs.(a - b))
40+
41+
@test gkldiv(a, b) sum(a .* log.(a ./ b) - a + b)
42+
43+
@test meanad(a, b) mean(abs.(a - b))
44+
@test maxad(a, b) maximum(abs.(a - b))
45+
@test msd(a, b) mean(abs2.(a - b))
46+
@test rmsd(a, b) sqrt(msd(a, b))
47+
@test rmsd(a, b; normalize=true) rmsd(a, b) / (maximum(a) - minimum(a))
48+
@test psnr(a, b, 2) 10 * log10(4 / msd(a, b))

0 commit comments

Comments
 (0)