Skip to content
This repository has been archived by the owner on May 4, 2019. It is now read-only.

Commit

Permalink
Move Abstract* methods to abstractdataarray.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
garborg committed Jul 30, 2014
1 parent a2d8aca commit 663ef6b
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 89 deletions.
110 changes: 88 additions & 22 deletions src/abstractdataarray.jl
Expand Up @@ -41,63 +41,129 @@ Base.done(x::AbstractDataArray, state::Integer) = state > length(x)

#' @description
#'
#' Determine if any of the entries of an AbstractArray are `NA`.
#' Determine if the values of an AbstractArray are `NA`.
#'
#' @param a::AbstractArray{T, N} The AbstractArray whose elements will
#' @param a::AbstractArray{T, N} The AbstractArray whose missingness will
#' be assessed.
#'
#' @returns out::Bool Are any of the elements of `a` an `NA` value?
#' @returns na::BitArray{N} Elementwise Boolean whether entry is missing.
#'
#' @examples
#'
#' a = [1, 2, 3]
#' anyna(a)
anyna(a::AbstractArray) = false # -> Bool
#' isna(a)
isna(a::AbstractArray) = falses(size(a)) # -> BitArray

#' @description
#'
#' Determine if all of the entries of an AbstractArray are `NA`.
#' Safe and type-stable way to determine if element `i` of an
#' AbstractArray is `NA`.
#'
#' @param a::AbstractArray{T, N} The AbstractArray whose elements will
#' @param a::AbstractArray The AbstractArray whose missingness will
#' be assessed.
#' @param i::Integer The index of the element to be checked for `NA`.
#'
#' @returns out::Bool Are all of the elements of `a` an `NA` value?
#' @returns na::Bool Is the element `NA` or not?
#'
#' @examples
#'
#' a = [1, 2, 3]
#' allna(a)
allna(a::AbstractArray) = false # -> Bool
#' isna(a, 1)
isna(a::AbstractArray, i::Real) = false # -> Bool

#' @description
#'
#' Determine if the values of an AbstractArray are `NA`.
#' Determine if any of the entries of an AbstractArray are `NA`.
#'
#' @param a::AbstractArray{T, N} The AbstractArray whose missingness will
#' @param a::AbstractArray{T, N} The AbstractArray whose elements will
#' be assessed.
#'
#' @returns na::BitArray{N} Elementwise Boolean whether entry is missing.
#' @returns out::Bool Are any of the elements of `a` an `NA` value?
#'
#' @examples
#'
#' a = [1, 2, 3]
#' isna(a)
isna(a::AbstractArray) = falses(size(a)) # -> BitArray
#' anyna(a)
anyna(a::AbstractArray) = false # -> Bool

#' @description
#'
#' Safe and type-stable way to determine if element `i` of an
#' AbstractArray is `NA`.
#' Determine if all of the entries of an AbstractArray are `NA`.
#'
#' @param a::AbstractArray The AbstractArray whose missingness will
#' @param a::AbstractArray{T, N} The AbstractArray whose elements will
#' be assessed.
#' @param i::Integer The index of the element to be checked for `NA`.
#'
#' @returns na::Bool Is the element `NA` or not?
#' @returns out::Bool Are all of the elements of `a` an `NA` value?
#'
#' @examples
#'
#' a = [1, 2, 3]
#' isna(a, 1)
isna(a::AbstractArray, i::Real) = false # -> Bool
#' allna(a)
allna(a::AbstractArray) = false # -> Bool

#' @description
#'
#' NO-OP: Turn a Vector into a Vector. See dropna(dv::DataVector) for
#' rationale.
#'
#' @param v::Vector{T} Vector that will be converted to a Vector.
#'
#' @returns v::Vector{T} Vector containing all of the values of `v`.
#'
#' @examples
#'
#' v = [1, 2, 3, 4]
#' v = dropna(v)
dropna(v::AbstractVector) = copy(v) # -> AbstractVector

# Iterators
# TODO: Use values()
# Use DataValueIterator type?

type EachFailNA{T}
da::AbstractDataArray{T}
end
each_failna{T}(da::AbstractDataArray{T}) = EachFailNA(da)
Base.start(itr::EachFailNA) = 1
Base.done(itr::EachFailNA, ind::Integer) = ind > length(itr.da)
function Base.next(itr::EachFailNA, ind::Integer)
if isna(itr.da[ind])
throw(NAException())
else
(itr.da[ind], ind + 1)
end
end

type EachDropNA{T}
da::AbstractDataArray{T}
end
each_dropna{T}(da::AbstractDataArray{T}) = EachDropNA(da)
function _next_nonna_ind{T}(da::AbstractDataArray{T}, ind::Int)
ind += 1
while ind <= length(da) && isna(da, ind)
ind += 1
end
ind
end
Base.start(itr::EachDropNA) = _next_nonna_ind(itr.da, 0)
Base.done(itr::EachDropNA, ind::Int) = ind > length(itr.da)
function Base.next(itr::EachDropNA, ind::Int)
(itr.da[ind], _next_nonna_ind(itr.da, ind))
end

type EachReplaceNA{S, T}
da::AbstractDataArray{S}
replacement::T
end
function each_replacena(da::AbstractDataArray, replacement::Any)
EachReplaceNA(da, convert(eltype(da), replacement))
end
function each_replacena(replacement::Any)
x -> each_replacena(x, replacement)
end
Base.start(itr::EachReplaceNA) = 1
Base.done(itr::EachReplaceNA, ind::Integer) = ind > length(itr.da)
function Base.next(itr::EachReplaceNA, ind::Integer)
item = isna(itr.da, ind) ? itr.replacement : itr.da[ind]
(item, ind + 1)
end
67 changes: 0 additions & 67 deletions src/dataarray.jl
Expand Up @@ -364,21 +364,6 @@ function array{T}(da::DataArray{T}, replacement::Any) # -> Array{T}
return array(da, convert(T, replacement))
end

#' @description
#'
#' NO-OP: Turn a Vector into a Vector. See dropna(dv::DataVector) for
#' rationale.
#'
#' @param v::Vector{T} Vector that will be converted to a Vector.
#'
#' @returns v::Vector{T} Vector containing all of the values of `v`.
#'
#' @examples
#'
#' v = [1, 2, 3, 4]
#' v = dropna(v)
dropna(v::AbstractVector) = copy(v) # -> AbstractVector

#' @description
#'
#' Turn a DataVector into a Vector. Drop any NA's.
Expand All @@ -397,58 +382,6 @@ dropna(v::AbstractVector) = copy(v) # -> AbstractVector
#' v = dropna(dv)
dropna(dv::DataVector) = copy(dv.data[!dv.na]) # -> Vector

# Iterators
# TODO: Use values()
# Use DataValueIterator type?

type EachFailNA{T}
da::AbstractDataArray{T}
end
each_failna{T}(da::AbstractDataArray{T}) = EachFailNA(da)
Base.start(itr::EachFailNA) = 1
Base.done(itr::EachFailNA, ind::Integer) = ind > length(itr.da)
function Base.next(itr::EachFailNA, ind::Integer)
if isna(itr.da[ind])
throw(NAException())
else
(itr.da[ind], ind + 1)
end
end

type EachDropNA{T}
da::AbstractDataArray{T}
end
each_dropna{T}(da::AbstractDataArray{T}) = EachDropNA(da)
function _next_nonna_ind{T}(da::AbstractDataArray{T}, ind::Int)
ind += 1
while ind <= length(da) && isna(da, ind)
ind += 1
end
ind
end
Base.start(itr::EachDropNA) = _next_nonna_ind(itr.da, 0)
Base.done(itr::EachDropNA, ind::Int) = ind > length(itr.da)
function Base.next(itr::EachDropNA, ind::Int)
(itr.da[ind], _next_nonna_ind(itr.da, ind))
end

type EachReplaceNA{S, T}
da::AbstractDataArray{S}
replacement::T
end
function each_replacena(da::AbstractDataArray, replacement::Any)
EachReplaceNA(da, convert(eltype(da), replacement))
end
function each_replacena(replacement::Any)
x -> each_replacena(x, replacement)
end
Base.start(itr::EachReplaceNA) = 1
Base.done(itr::EachReplaceNA, ind::Integer) = ind > length(itr.da)
function Base.next(itr::EachReplaceNA, ind::Integer)
item = isna(itr.da, ind) ? itr.replacement : itr.da[ind]
(item, ind + 1)
end

#' @description
#'
#' Determine if the entries of an DataArray are `NA`.
Expand Down

0 comments on commit 663ef6b

Please sign in to comment.