Skip to content

Commit

Permalink
Merge pull request #48 from brenhinkeller/de-lv
Browse files Browse the repository at this point in the history
Remove LoopVectorization dependency, in light of deprecation
  • Loading branch information
brenhinkeller authored Jun 13, 2024
2 parents 929cd1c + 0af934c commit d9e60e1
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 674 deletions.
12 changes: 4 additions & 8 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
name = "NaNStatistics"
uuid = "b946abbf-3ea7-4610-9019-9858bfdeaf2d"
authors = ["C. Brenhin Keller"]
version = "0.6.35"
version = "0.6.36"

[deps]
IfElse = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173"
LoopVectorization = "bdcacae8-1622-11e9-2a5c-532679323890"
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
Static = "aedffcd0-7271-4cad-89d0-dc628f76c6d3"
VectorizationBase = "3d5dd08c-fd9d-11e8-17fa-ed2836048c2f"
StaticArrayInterface = "0d7ed370-da01-4f52-bd93-41d350b8b718"

[compat]
IfElse = "0.1"
LoopVectorization = "0.12.113"
VectorizationBase = "0.21.0 - 0.21.64"
PrecompileTools = "1"
Static = "0.8"
julia = "1.8"
StaticArrayInterface = "1"
julia = "1.10"
114 changes: 24 additions & 90 deletions src/ArrayStats/ArrayStats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,9 @@
```
Return the number of elements of `A` that are `NaN`s.
"""
function countnans(A::StridedArray{T}) where T<:PrimitiveNumber
n = 0
@turbo check_empty=true for i eachindex(A)
n += A[i]!=A[i]
end
return n
end
function countnans(A)
n = 0
@inbounds for i eachindex(A)
@inbounds @simd ivdep for i eachindex(A)
n += A[i]!=A[i]
end
return n
Expand All @@ -35,7 +28,7 @@
"""
function countnotnans(A)
n = 0
@turbo check_empty=true for i eachindex(A)
@inbounds @simd ivdep for i eachindex(A)
n += A[i]==A[i]
end
return n
Expand All @@ -57,14 +50,8 @@
```
Fill a Boolean mask of dimensions `size(A)` that is false wherever `A` is `NaN`
"""
function nanmask!(mask::StridedArray, A::StridedArray{T}) where T<:PrimitiveNumber
@turbo for i eachindex(A)
mask[i] = A[i]==A[i]
end
return mask
end
function nanmask!(mask, A)
@inbounds for i eachindex(A)
@inbounds @simd ivdep for i eachindex(A)
mask[i] = A[i]==A[i]
end
return mask
Expand All @@ -81,21 +68,11 @@
```
Replace all `NaN`s in A with zeros of the same type
"""
function zeronan!(A::StridedArray{T}) where T<:PrimitiveNumber
= zero(T)
@turbo for i eachindex(A)
Aᵢ = A[i]
A[i] = ifelse(Aᵢ==Aᵢ, Aᵢ, ∅)
end
return A
end
function zeronan!(A::AbstractArray{T}) where T
= zero(T)
@inbounds for i eachindex(A)
@inbounds @simd ivdep for i eachindex(A)
Aᵢ = A[i]
if isnan(Aᵢ)
A[i] =
end
A[i] = ifelse(Aᵢ==Aᵢ, Aᵢ, ∅)
end
return A
end
Expand Down Expand Up @@ -153,9 +130,8 @@
function nanadd(A::AbstractArray, B::AbstractArray)
result_type = promote_type(eltype(A), eltype(B))
result = similar(A, result_type)
@inbounds @simd for i eachindex(A)
Aᵢ = A[i]
Bᵢ = B[i]
@inbounds @simd ivdep for i eachindex(A,B)
Aᵢ, Bᵢ = A[i], B[i]
result[i] = (Aᵢ * (Aᵢ==Aᵢ)) + (Bᵢ * (Bᵢ==Bᵢ))
end
return result
Expand All @@ -169,9 +145,8 @@
Add the non-NaN elements of `B` to `A`, treating `NaN`s as zeros
"""
function nanadd!(A::Array, B::AbstractArray)
@inbounds @simd for i eachindex(A)
Aᵢ = A[i]
Bᵢ = B[i]
@inbounds @simd for i eachindex(A,B)
Aᵢ, Bᵢ = A[i], B[i]
A[i] = (Aᵢ * (Aᵢ==Aᵢ)) + (Bᵢ * (Bᵢ==Bᵢ))
end
return A
Expand All @@ -198,7 +173,6 @@
__nanminimum(A, ::Colon, region) = reducedims(_nanminimum(A, region), region)
_nanminimum(A, region) = reduce(nanmin, A, dims=region, init=float(eltype(A))(NaN))
_nanminimum(A, ::Colon) = reduce(nanmin, A, init=float(eltype(A))(NaN))
_nanminimum(A::Array{<:Number}, ::Colon) = vreduce(nanmin, A)
export nanminimum


Expand All @@ -219,7 +193,6 @@
__nanmaximum(A, ::Colon, region) = reducedims(_nanmaximum(A, region), region)
_nanmaximum(A, region) = reduce(nanmax, A, dims=region, init=float(eltype(A))(NaN))
_nanmaximum(A, ::Colon) = reduce(nanmax, A, init=float(eltype(A))(NaN))
_nanmaximum(A::Array{<:Number}, ::Colon) = vreduce(nanmax, A)
export nanmaximum

"""
Expand Down Expand Up @@ -312,41 +285,25 @@
mask = nanmask(A)
return sum(A.*W.*mask, dims=region) ./ sum(W.*mask, dims=region)
end
# Fallback method for non-StridedArrays
function _nanmean(A, W, ::Colon)
n = zero(eltype(W))
m = zero(promote_type(eltype(W), eltype(A)))
@inbounds @simd for i eachindex(A)
Aᵢ = A[i]
Wᵢ = W[i]
t = Aᵢ == Aᵢ
n += Wᵢ * t
m += Wᵢ * Aᵢ * t
end
return m / n
end
# Can't have NaNs if array is all Integers
function _nanmean(A::StridedArray{<:Integer}, W, ::Colon)
function _nanmean(A::AbstractArray{<:Integer}, W, ::Colon)
n = zero(eltype(W))
m = zero(promote_type(eltype(W), eltype(A)))
@turbo for i eachindex(A)
@inbounds @simd ivdep for i eachindex(A,W)
Wᵢ = W[i]
n += Wᵢ
m += Wᵢ * A[i]
end
return m / n
end
function _nanmean(A::StridedArray, W, ::Colon)
T1 = eltype(W)
T2 = promote_type(eltype(W), eltype(A))
n = zero(T1)
m = zero(T2)
@turbo for i eachindex(A)
Aᵢ = A[i]
Wᵢ = W[i]
function _nanmean(A, W, ::Colon)
n = ∅ₙ = zero(eltype(W))
m = ∅ₘ = zero(promote_type(eltype(W), eltype(A)))
@inbounds @simd ivdep for i eachindex(A,W)
Aᵢ, Wᵢ = A[i], W[i]
t = Aᵢ==Aᵢ
n += ifelse(t, Wᵢ, zero(T1))
m += ifelse(t, Wᵢ * Aᵢ, zero(T2))
n += ifelse(t, Wᵢ, ∅ₙ)
m += ifelse(t, Wᵢ * Aᵢ, ∅ₘ)
end
return m / n
end
Expand Down Expand Up @@ -374,60 +331,37 @@
w = sum(W.*mask, dims=region)
s = sum(A.*W.*mask, dims=region) ./ w
d = A .- s # Subtract mean, using broadcasting
@turbo for i eachindex(d)
@inbounds @simd ivdep for i eachindex(d, W)
dᵢ = d[i]
d[i] = (dᵢ * dᵢ * W[i]) * mask[i]
end
s .= sum(d, dims=region)
@turbo for i eachindex(s)
@inbounds @simd ivdep for i eachindex(s,n,w)
s[i] = sqrt((s[i] * n[i]) / (w[i] * (n[i] - 1)))
end
return s
end
function _nanstd(A, W, ::Colon)
@assert eachindex(A) == eachindex(W)
n = 0
w = zero(eltype(W))
m = zero(promote_type(eltype(W), eltype(A)))
@inbounds @simd for i eachindex(A)
Aᵢ = A[i]
Wᵢ = W[i]
@inbounds @simd ivdep for i eachindex(A,W)
Aᵢ, Wᵢ = A[i], W[i]
t = Aᵢ == Aᵢ
n += t
w += Wᵢ * t
m += Wᵢ * Aᵢ * t
end
mu = m / w
s = zero(typeof(mu))
@inbounds @simd for i eachindex(A)
@inbounds @simd ivdep for i eachindex(A,W)
Aᵢ = A[i]
d = Aᵢ - mu
s += (d * d * W[i]) * (Aᵢ == Aᵢ) # Zero if Aᵢ is NaN
end
return sqrt(s / w * n / (n-1))
end
function _nanstd(A::StridedArray{Ta}, W::StridedArray{Tw}, ::Colon) where {Ta<:PrimitiveNumber, Tw<:PrimitiveNumber}
n = 0
Tm = promote_type(Tw, Ta)
w = zero(Tw)
m = zero(Tm)
@turbo for i eachindex(A)
Aᵢ = A[i]
Wᵢ = W[i]
t = Aᵢ==Aᵢ
n += t
w += ifelse(t, Wᵢ, zero(Tw))
m += ifelse(t, Wᵢ * Aᵢ, zero(Tm))
end
mu = m / w
Tmu = typeof(mu)
s = zero(Tmu)
@turbo for i eachindex(A)
Aᵢ = A[i]
d = Aᵢ - mu
s += ifelse(Aᵢ==Aᵢ, d * d * W[i], zero(Tmu))
end
return sqrt(s / w * n / (n-1))
end
export nanstd


Expand Down
50 changes: 7 additions & 43 deletions src/ArrayStats/nancov.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ function _nancov(x::AbstractVector, y::AbstractVector, corrected::Bool, μᵪ::N
# Calculate covariance
σᵪᵧ == zero(promote_type(typeof(μᵪ), typeof(μᵧ), Int))
n = 0
@turbo check_empty=true for i eachindex(x,y)
@inbounds @simd ivdep for i eachindex(x,y)
δᵪ = x[i] - μᵪ
δᵧ = y[i] - μᵧ
δ² = δᵪ * δᵧ
Expand All @@ -19,7 +19,7 @@ function _nancov(x::AbstractVector, y::AbstractVector, corrected::Bool)
n = 0
Σᵪ = ∅ᵪ = zero(eltype(x))
Σᵧ = ∅ᵧ = zero(eltype(y))
@turbo check_empty=true for i eachindex(x,y)
@inbounds @simd ivdep for i eachindex(x,y)
xᵢ, yᵢ = x[i], y[i]
notnan = (xᵢ==xᵢ) & (yᵢ==yᵢ)
n += notnan
Expand Down Expand Up @@ -103,49 +103,13 @@ function nancor(x::AbstractVector, y::AbstractVector; corrected::Bool=true)
@assert eachindex(x) == eachindex(y)
return _nancor(x, y, corrected)
end

# Pair-wise nan-covariance
function _nancor(x::StridedVector{T}, y::StridedVector{T}, corrected::Bool) where T<:PrimitiveNumber
function _nancor(x::AbstractVector{Tx}, y::AbstractVector{Ty}, corrected::Bool) where {Tx, Ty}
# Parwise nan-means
n = 0
Σᵪ = ∅ᵪ = zero(T)
Σᵧ = ∅ᵧ = zero(T)
@turbo check_empty=true for i eachindex(x,y)
xᵢ, yᵢ = x[i], y[i]
notnan = (xᵢ==xᵢ) & (yᵢ==yᵢ)
n += notnan
Σᵪ += ifelse(notnan, xᵢ, ∅ᵪ)
Σᵧ += ifelse(notnan, yᵢ, ∅ᵧ)
end
μᵪ = Σᵪ/n
μᵧ = Σᵧ/n
n == 0 && return (∅ᵪ+∅ᵧ)/0 # Return appropriate NaN if no data

# Pairwise nan-variances
σ²ᵪ = ∅ᵪ = zero(typeof(μᵪ))
σ²ᵧ = ∅ᵧ = zero(typeof(μᵧ))
@turbo check_empty=true for i eachindex(x,y)
δᵪ = x[i] - μᵪ
δᵧ = y[i] - μᵧ
notnan = (δᵪ==δᵪ) & (δᵧ==δᵧ)
σ²ᵪ += ifelse(notnan, δᵪ * δᵪ, ∅ᵪ)
σ²ᵧ += ifelse(notnan, δᵧ * δᵧ, ∅ᵧ)
end
σᵪ = sqrt(σ²ᵪ / max(n-corrected, 0))
σᵧ = sqrt(σ²ᵧ / max(n-corrected, 0))

# Covariance and correlation
σᵪᵧ = _nancov(x, y, corrected, μᵪ, μᵧ)
ρᵪᵧ = σᵪᵧ / (σᵪ * σᵧ)

return ρᵪᵧ
end
function _nancor(x::AbstractVector, y::AbstractVector, corrected::Bool)
# Parwise nan-means
n = 0
Σᵪ = ∅ᵪ = zero(eltype(x))
Σᵧ = ∅ᵧ = zero(eltype(y))
@inbounds for i eachindex(x,y)
Σᵪ = ∅ᵪ = zero(Tx)
Σᵧ = ∅ᵧ = zero(Ty)
@inbounds @simd ivdep for i eachindex(x,y)
xᵢ, yᵢ = x[i], y[i]
notnan = (xᵢ==xᵢ) & (yᵢ==yᵢ)
n += notnan
Expand All @@ -159,7 +123,7 @@ function _nancor(x::AbstractVector, y::AbstractVector, corrected::Bool)
# Pairwise nan-variances
σ²ᵪ = ∅ᵪ = zero(typeof(μᵪ))
σ²ᵧ = ∅ᵧ = zero(typeof(μᵧ))
@inbounds for i eachindex(x,y)
@inbounds @simd ivdep for i eachindex(x,y)
δᵪ = x[i] - μᵪ
δᵧ = y[i] - μᵧ
notnan = (δᵪ==δᵪ) & (δᵧ==δᵧ)
Expand Down
Loading

2 comments on commit d9e60e1

@brenhinkeller
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/108912

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.36 -m "<description of version>" d9e60e147d1857701052c17c4e588ecafdf325aa
git push origin v0.6.36

Please sign in to comment.