From 08ae8ad48e4080af159ffa5f8622befc1023c27c Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Fri, 11 Nov 2022 22:43:13 -0500 Subject: [PATCH] Make `movmean` safe for non-one-indexed arrays --- Project.toml | 2 +- src/ArrayStats/ArrayStats.jl | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Project.toml b/Project.toml index 1b875d6..6ff23fb 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "NaNStatistics" uuid = "b946abbf-3ea7-4610-9019-9858bfdeaf2d" authors = ["C. Brenhin Keller"] -version = "0.6.14" +version = "0.6.15" [deps] IfElse = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173" diff --git a/src/ArrayStats/ArrayStats.jl b/src/ArrayStats/ArrayStats.jl index 9f87189..3b39bb3 100644 --- a/src/ArrayStats/ArrayStats.jl +++ b/src/ArrayStats/ArrayStats.jl @@ -470,8 +470,8 @@ m = Array{mean_type}(undef, size(x)) δ = ceil(Int, (n-1)/2) @inbounds for i ∈ eachindex(x) - iₗ = max(i-δ, 1) - iᵤ = min(i+δ, length(x)) + iₗ = max(i-δ, firstindex(x)) + iᵤ = min(i+δ, lastindex(x)) m[i] = nanmean(view(x, iₗ:iᵤ)) end return m @@ -480,15 +480,15 @@ mean_type = Base.promote_op(/, eltype(x), Int64) m = Array{mean_type}(undef, size(x)) δ = ceil(Int, (n-1)/2) - 𝐼 = repeat(1:size(x,1), 1, size(x,2)) - 𝐽 = repeat((1:size(x,2))', size(x,1), 1) - @inbounds for k ∈ eachindex(x) + 𝐼 = repeat((firstindex(x,1):lastindex(x,1)), 1, size(x,2)) + 𝐽 = repeat((firstindex(x,2):lastindex(x,2))', size(x,1), 1) + @inbounds for k ∈ eachindex(𝐼,𝐽) i = 𝐼[k] - iₗ = max(i-δ, 1) - iᵤ = min(i+δ, size(x,1)) + iₗ = max(i-δ, firstindex(x,1)) + iᵤ = min(i+δ, lastindex(x,1)) j = 𝐽[k] - jₗ = max(j-δ, 1) - jᵤ = min(j+δ, size(x,2)) + jₗ = max(j-δ, firstindex(x,2)) + jᵤ = min(j+δ, lastindex(x,2)) m[i,j] = nanmean(view(x, iₗ:iᵤ, jₗ:jᵤ)) end return m