Skip to content

Commit

Permalink
Merge pull request JuliaLang#109 from JuliaStats/testna
Browse files Browse the repository at this point in the history
Speed up anyna, allna. Reinstate NAs tests.
  • Loading branch information
garborg committed Jul 29, 2014
2 parents d78a6d3 + d85c06d commit 07f40a1
Show file tree
Hide file tree
Showing 16 changed files with 369 additions and 328 deletions.
2 changes: 1 addition & 1 deletion src/DataArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module DataArrays
DataArray,
DataMatrix,
DataVector,
dropna,
each_failNA,
each_dropna,
each_replaceNA,
Expand All @@ -43,7 +44,6 @@ module DataArrays
PooledDataVecs,
PooledDataVector,
reldiff,
dropna,
reorder,
rep,
replace!,
Expand Down
32 changes: 1 addition & 31 deletions src/abstractdataarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ typealias AbstractDataMatrix{T} AbstractDataArray{T, 2}

#' @description
#' Determine the type of the elements of an AbstractDataArray.
#'
#'
#' @param ada::AbstractDataArray{T} The AbstractDataArray whose
#' element type is desired.
#'
Expand Down Expand Up @@ -54,21 +54,6 @@ Base.done(x::AbstractDataArray, state::Integer) = state > length(x)
#' anyna(a)
anyna(a::AbstractArray) = false # -> Bool

#' @description
#'
#' Determine if any of the entries of an DataArray are `NA`.
#'
#' @param da::DataArray{T, N} The DataArray whose elements will
#' be assessed.
#'
#' @returns out::Bool Are any of the elements of `a` an `NA` value?
#'
#' @examples
#'
#' da = @data([1, 2, 3])
#' anyna(da)
anyna(d::AbstractDataArray) = any(isna, d) # -> Bool

#' @description
#'
#' Determine if all of the entries of an AbstractArray are `NA`.
Expand All @@ -84,21 +69,6 @@ anyna(d::AbstractDataArray) = any(isna, d) # -> Bool
#' allna(a)
allna(a::AbstractArray) = false # -> Bool

#' @description
#'
#' Determine if all of the entries of an DataArray are `NA`.
#'
#' @param da::DataArray{T, N} The DataArray whose elements will
#' be assessed.
#'
#' @returns out::Bool Are all of the elements of `a` an `NA` value?
#'
#' @examples
#'
#' da = @data([1, 2, 3])
#' allna(da)
allna(d::AbstractDataArray) = all(isna, d) # -> Bool

#' @description
#'
#' Determine if the values of an AbstractArray are `NA`.
Expand Down
34 changes: 32 additions & 2 deletions src/dataarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -489,11 +489,41 @@ isna(da::DataArray) = copy(da.na) # -> BitArray
getindex(da.na, I...)
end

#' @description
#'
#' Determine if any of the entries of an DataArray are `NA`.
#'
#' @param da::DataArray{T, N} The DataArray whose elements will
#' be assessed.
#'
#' @returns out::Bool Are any of the elements of `da` an `NA` value?
#'
#' @examples
#'
#' da = @data([1, 2, 3])
#' anyna(da)
anyna(da::DataArray) = any(da.na) # -> Bool

#' @description
#'
#' Determine if all of the entries of an DataArray are `NA`.
#'
#' @param da::DataArray{T, N} The DataArray whose elements will
#' be assessed.
#'
#' @returns out::Bool Are all of the elements of `da` an `NA` value?
#'
#' @examples
#'
#' da = @data([1, 2, 3])
#' allna(da)
allna(da::DataArray) = all(da.na) # -> Bool

#' @description
#'
#' Determine if the entries of an DataArray are `NaN`.
#'
#' @param a::DataArray{T, N} The DataArray whose elements will
#' @param da::DataArray{T, N} The DataArray whose elements will
#' be assessed.
#'
#' @returns na::DataArray{Bool} Elementwise Boolean whether entry is `NaN`.
Expand All @@ -511,7 +541,7 @@ end
#' Determine if the entries of an DataArray are finite, which means
#' neither `+/-NaN` nor `+/-Inf`.
#'
#' @param a::DataArray{T, N} The DataArray whose elements will
#' @param da::DataArray{T, N} The DataArray whose elements will
#' be assessed.
#'
#' @returns na::DataArray{Bool} Elementwise Boolean whether entry is finite.
Expand Down
42 changes: 41 additions & 1 deletion src/pooleddataarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ isna(pda::PooledDataArray) = pda.refs .== 0
#' Safe and type-stable way to determine if element `i` of an
#' PooledDataArray is `NA`.
#'
#' @param a::PooledDataArray The PooledDataArray whose missingness will
#' @param pda::PooledDataArray The PooledDataArray whose missingness will
#' be assessed.
#' @param i::Integer The index of the element to be checked for `NA`.
#'
Expand All @@ -200,6 +200,46 @@ isna(pda::PooledDataArray) = pda.refs .== 0
#' isna(a, 1)
isna(pda::PooledDataArray, i::Real) = pda.refs[i] == 0 # -> Bool

#' @description
#'
#' Determine if any of the entries of an PooledDataArray are `NA`.
#'
#' @param pda::PooledDataArray The PooledDataArray whose elements will
#' be assessed.
#'
#' @returns out::Bool Are any of the elements of `pda` an `NA` value?
#'
#' @examples
#'
#' pda = @pdata([1, 2, 3])
#' anyna(pda)
function anyna(pda::PooledDataArray)
for ref in pda.refs
ref == 0 && return true
end
return false
end

#' @description
#'
#' Determine if all of the entries of an PooledDataArray are `NA`.
#'
#' @param pda::PooledDataArray The PooledDataArray whose elements will
#' be assessed.
#'
#' @returns out::Bool Are all of the elements of `pda` an `NA` value?
#'
#' @examples
#'
#' pda = @pdata([1, 2, 3])
#' allna(pda)
function allna(pda::PooledDataArray)
for ref in pda.refs
ref == 0 || return false
end
return true
end

##############################################################################
##
## PooledDataArray utilities
Expand Down
14 changes: 7 additions & 7 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module TestAbstractArray
using Base.Test
using DataArrays
using Base.Test
using DataArrays

unsorted_dv = @data [2, 1, NA]
unsorted_dv = @data [2, 1, NA]

# TODO: Make this work
# tiedrank(dv)
# TODO: Make this work
# tiedrank(dv)

@assert first(unsorted_dv) == 2
@assert isna(last(unsorted_dv))
@assert first(unsorted_dv) == 2
@assert isna(last(unsorted_dv))
end
40 changes: 20 additions & 20 deletions test/booleans.jl
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
module TestBooleans
using Base.Test
using DataArrays
using Base.Test
using DataArrays

@assert NA | true == true
@assert isna(NA | false)
@assert isna(NA | NA)
@assert true | NA == true
@assert isna(false | NA)
@assert NA | true == true
@assert isna(NA | false)
@assert isna(NA | NA)
@assert true | NA == true
@assert isna(false | NA)

@assert isna(NA & true)
@assert NA & false == false
@assert isna(NA & NA)
@assert isna(true & NA)
@assert false & NA == false
@assert isna(NA & true)
@assert NA & false == false
@assert isna(NA & NA)
@assert isna(true & NA)
@assert false & NA == false

@assert any((@data [1, 2, NA]) .== 1) == true
@assert any((@data [NA, 1, 2]) .== 1) == true
@assert isna(any((@data [1, 2, NA]) .== 3))
@assert any((@data [1, 2, 3] ).== 4) == false
@assert any((@data [1, 2, NA]) .== 1) == true
@assert any((@data [NA, 1, 2]) .== 1) == true
@assert isna(any((@data [1, 2, NA]) .== 3))
@assert any((@data [1, 2, 3] ).== 4) == false

@assert isna(all((@data [1, 1, NA]) .== 1))
@assert isna(all((@data [NA, 1, 1]) .== 1))
@assert all((@data [1, 1, 1]) .== 1) == true
@assert all((@data [1, 2, 1]) .== 1) == false
@assert isna(all((@data [1, 1, NA]) .== 1))
@assert isna(all((@data [NA, 1, 1]) .== 1))
@assert all((@data [1, 1, 1]) .== 1) == true
@assert all((@data [1, 2, 1]) .== 1) == false
end
Loading

0 comments on commit 07f40a1

Please sign in to comment.