Skip to content

Commit

Permalink
Add and test simple nanargmin and nanargmax
Browse files Browse the repository at this point in the history
  • Loading branch information
brenhinkeller committed Feb 8, 2023
1 parent 8f28018 commit adbf7a1
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 83 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "NaNStatistics"
uuid = "b946abbf-3ea7-4610-9019-9858bfdeaf2d"
authors = ["C. Brenhin Keller"]
version = "0.6.23"
version = "0.6.24"

[deps]
IfElse = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173"
Expand Down
53 changes: 45 additions & 8 deletions src/ArrayStats/ArrayStats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
```julia
nanadd!(A, B)
```
Add the non-NaN elements of `B` to `A`, treating NaNs as zeros
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)
Expand Down Expand Up @@ -181,7 +181,7 @@
```julia
nanmaximum(A; dims)
```
Find the largest non-NaN value of an indexable collection `A`, optionally
As `maximum` but ignoring `NaN`s: Find the largest non-`NaN` value of an indexable collection `A`, optionally
along a dimension specified by `dims`.
Also supports the `dim` keyword, which behaves identically to `dims`, but
Expand All @@ -197,13 +197,50 @@
_nanmaximum(A::Array{<:Number}, ::Colon) = vreduce(nanmax, A)
export nanmaximum

"""
```julia
nanargmin(A)
```
As `argmin` but ignoring `NaN`s: Find the index of the smallest non-`NaN`
value (if any) of an indexable collection `A`
"""
function nanargmin(x)
imin = firstindex(x)
@inbounds for i in eachindex(x)
xᵢ = x[i]
if xᵢ==xᵢ && !(xᵢ > x[imin])
imin = i
end
end
return imin
end
export nanargmin

"""
```julia
nanargmax(A)
```
As `argmax` but ignoring `NaN`s: Find the index of the largest non-`NaN`
value (if any) of an indexable collection `A`
"""
function nanargmax(x)
imax = firstindex(x)
@inbounds for i in eachindex(x)
xᵢ = x[i]
if xᵢ==xᵢ && !(xᵢ < x[imax])
imax = i
end
end
return imax
end
export nanargmax

"""
```julia
nanextrema(A; dims)
```
Find the extrema (maximum & minimum) of an indexable collection `A`,
ignoring NaNs, optionally along a dimension specified by `dims`.
ignoring `NaN`s, optionally along a dimension specified by `dims`.
Also supports the `dim` keyword, which behaves identically to `dims`, but
also drops any singleton dimensions that have been reduced over (as is the
Expand All @@ -221,7 +258,7 @@
nanrange(A; dims)
```
Calculate the range (maximum - minimum) of an indexable collection `A`,
ignoring NaNs, optionally along a dimension specified by `dims`.
ignoring `NaN`s, optionally along a dimension specified by `dims`.
Also supports the `dim` keyword, which behaves identically to `dims`, but
also drops any singleton dimensions that have been reduced over (as is the
Expand All @@ -235,7 +272,7 @@
```julia
nanmean(A, W; dims)
```
Ignoring NaNs, calculate the weighted mean of an indexable
Ignoring `NaN`s, calculate the weighted mean of an indexable
collection `A`, optionally along dimensions specified by `dims`.
Also supports the `dim` keyword, which behaves identically to `dims`, but
Expand Down Expand Up @@ -295,7 +332,7 @@
```julia
nanstd(A, W; dims)
```
Calculate the weighted standard deviation, ignoring NaNs, of an
Calculate the weighted standard deviation, ignoring `NaN`s, of an
indexable collection `A`, optionally along a dimension specified by `dims`.
Also supports the `dim` keyword, which behaves identically to `dims`, but
Expand Down Expand Up @@ -374,7 +411,7 @@
```julia
nanmad(A; dims)
```
Median absolute deviation from the median, ignoring NaNs, of an indexable
Median absolute deviation from the median, ignoring `NaN`s, of an indexable
collection `A`, optionally along a dimension specified by `dims`.
Note that for a Normal distribution, sigma = 1.4826 * MAD.
Expand Down Expand Up @@ -403,7 +440,7 @@
```julia
nanaad(A; dims)
```
Mean (average) absolute deviation from the mean, ignoring NaNs, of an
Mean (average) absolute deviation from the mean, ignoring `NaN`s, of an
indexable collection `A`, optionally along a dimension specified by `dims`.
Note that for a Normal distribution, sigma = 1.253 * AAD.
Expand Down
2 changes: 1 addition & 1 deletion src/ArrayStats/nancumsum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
```julia
nancumsum(A; dims)
```
Calculate the sum of an indexable collection `A`, ignoring NaNs, optionally
Calculate the sum of an indexable collection `A`, ignoring `NaN`s, optionally
along dimensions specified by `dims`.
## Examples
Expand Down
2 changes: 1 addition & 1 deletion src/ArrayStats/nansum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
```julia
nansum(A; dims)
```
Calculate the sum of an indexable collection `A`, ignoring NaNs, optionally
Calculate the sum of an indexable collection `A`, ignoring `NaN`s, optionally
along dimensions specified by `dims`.
Also supports the `dim` keyword, which behaves identically to `dims`, but
Expand Down
14 changes: 7 additions & 7 deletions src/Binning.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
```julia
nanbinmean!(MU, [N], x, y, xedges::AbstractRange)
```
Ignoring NaNs, fill the array `MU` with the means (and optionally `N` with
Ignoring `NaN`s, fill the array `MU` with the means (and optionally `N` with
the counts) of non-NAN `y` values that fall into each of `length(xedges)-1`
equally spaced bins along the `x` axis with bin edges specified by `xedges`.
Expand Down Expand Up @@ -97,7 +97,7 @@
```julia
nanbinmean!(MU, N, x, y, z, xedges::AbstractRange, yedges::AbstractRange)
```
Ignoring NaNs, fill the matrix `MU` with the means and `N` with
Ignoring `NaN`s, fill the matrix `MU` with the means and `N` with
the counts of non-NAN `z` values that fall into a 2D grid of x and y bins
defined by `xedges` and `yedges`. The independent variables `x` and `y`,
as well as the dependent variable `z`, are all expected as 1D vectors (any
Expand Down Expand Up @@ -150,7 +150,7 @@
```julia
nanbinmean!(MU, W, x, y, w, xedges::AbstractRange)
```
Ignoring NaNs, fill the array `MU` with the weighted means (and `W` with
Ignoring `NaN`s, fill the array `MU` with the weighted means (and `W` with
the sum of weights) of non-NAN `y` values that fall into each of
`length(xedges)-1` equally spaced bins along the `x` axis with bin edges
specified by `xedges`.
Expand Down Expand Up @@ -308,7 +308,7 @@
```julia
nanbinmean(x, y, xedges::AbstractRange)
```
Ignoring NaNs, calculate the mean of `y` values that fall into each of
Ignoring `NaN`s, calculate the mean of `y` values that fall into each of
`length(xedges)-1` equally spaced bins along the `x` axis with bin edges
specified by `xedges`.
Expand Down Expand Up @@ -346,7 +346,7 @@
```julia
nanbinmean(x, y, z, xedges, yedges)
```
Ignoring NaNs, calculate the mean of `z` values that fall into a 2D grid of
Ignoring `NaN`s, calculate the mean of `z` values that fall into a 2D grid of
x and y bins with bin edges defined by `xedges` and `yedges`. The independent
variables `x` and `y`, as well as the dependent variable `z`, are all expected
as 1D vectors (any subtype of AbstractVector).
Expand Down Expand Up @@ -381,7 +381,7 @@
```julia
nanbinmean(x, y, xedges::AbstractRange)
```
Ignoring NaNs, calculate the weighted mean of `y` values that
Ignoring `NaN`s, calculate the weighted mean of `y` values that
fall into each of `length(xedges)-1` equally spaced bins along the `x`
axis with bin edges specified by `xedges`.
Expand All @@ -402,7 +402,7 @@
```julia
nanbinmedian(x, y, xedges::AbstractRange)
```
Calculate the median, ignoring NaNs, of y values that fall into each of
Calculate the median, ignoring `NaN`s, of y values that fall into each of
`length(xedges)-1` equally spaced bins along the `x` axis with bin edges
specified by `xedges`.
Expand Down
6 changes: 3 additions & 3 deletions src/Histograms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
```julia
histcounts(x, xedges::AbstractRange; T=Int64)::Vector{T}
```
A 1D histogram, ignoring NaNs: calculate the number of `x` values that fall into
A 1D histogram, ignoring `NaN`s: calculate the number of `x` values that fall into
each of `length(xedges)-1` equally spaced bins along the `x` axis with bin edges
specified by `xedges`.
Expand Down Expand Up @@ -40,7 +40,7 @@ histcounts(x, xmin::Number, xmax::Number, nbins::Integer; T=Int64) = histcounts(
```julia
histcountindices(x, xedges::AbstractRange; T=Int64)::Vector{T}
```
A 1D histogram, ignoring NaNs; as `histcounts` but also returning a vector of
A 1D histogram, ignoring `NaN`s; as `histcounts` but also returning a vector of
the bin index of each `x` value.
## Examples
Expand All @@ -65,7 +65,7 @@ export histcountindices
```julia
histcounts(x, y, xedges::AbstractRange, yedges::AbstractRange; T=Int64)::Matrix{T}
```
A 2D histogram, ignoring NaNs: calculate the number of `x, y` pairs that fall into
A 2D histogram, ignoring `NaN`s: calculate the number of `x, y` pairs that fall into
each square of a 2D grid of equally-spaced square bins with edges specified by
`xedges` and `yedges`.
Expand Down
2 changes: 2 additions & 0 deletions src/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
nanvar(A)
nanminimum(A)
nanmaximum(A)
nanargmin(A)
nanargmax(A)
# nanmedian(A)
# nanpctile(A, 50)

Expand Down
Loading

0 comments on commit adbf7a1

Please sign in to comment.