From 47433f528f6005cb7c04221afa853d2f6b5ee0be Mon Sep 17 00:00:00 2001 From: Sean Garborg Date: Tue, 29 Jul 2014 18:03:17 -0400 Subject: [PATCH 1/2] Speed up anyna, allna. Reinstate nas tests. --- src/DataArrays.jl | 2 +- src/abstractdataarray.jl | 32 +------------- src/dataarray.jl | 34 ++++++++++++++- src/pooleddataarray.jl | 42 +++++++++++++++++- test/nas.jl | 87 +++++++++++++++++++++++--------------- test/newtests/dataarray.jl | 24 +---------- test/runtests.jl | 2 +- 7 files changed, 132 insertions(+), 91 deletions(-) diff --git a/src/DataArrays.jl b/src/DataArrays.jl index 5d678cfb1..81fd9abdc 100644 --- a/src/DataArrays.jl +++ b/src/DataArrays.jl @@ -20,6 +20,7 @@ module DataArrays DataArray, DataMatrix, DataVector, + dropna, each_failNA, each_dropna, each_replaceNA, @@ -43,7 +44,6 @@ module DataArrays PooledDataVecs, PooledDataVector, reldiff, - dropna, reorder, rep, replace!, diff --git a/src/abstractdataarray.jl b/src/abstractdataarray.jl index 877a363e9..756144fe1 100644 --- a/src/abstractdataarray.jl +++ b/src/abstractdataarray.jl @@ -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. #' @@ -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`. @@ -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`. diff --git a/src/dataarray.jl b/src/dataarray.jl index 8fe16eac9..7959ad2ea 100644 --- a/src/dataarray.jl +++ b/src/dataarray.jl @@ -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`. @@ -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. diff --git a/src/pooleddataarray.jl b/src/pooleddataarray.jl index 2d3bc2d69..472b7564e 100644 --- a/src/pooleddataarray.jl +++ b/src/pooleddataarray.jl @@ -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`. #' @@ -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 diff --git a/test/nas.jl b/test/nas.jl index e40e9bcee..a780028af 100644 --- a/test/nas.jl +++ b/test/nas.jl @@ -2,44 +2,65 @@ module TestNAs using Base.Test using DataArrays + + # anyna(a::AbstractArray) + anyna([1, 2]) + anyna(repeat([1, 2], outer = [1, 2])) + @test !anyna(repeat([1, 2], outer = [1, 2, 2])) + + # anyna(da::DataArray) + anyna(DataArray([1, 2], falses(2))) + anyna(DataArray(repeat([1, 2], outer = [1, 2]), falses(2, 2))) + da = DataArray(repeat([1, 2], outer = [1, 2, 2]), falses(2, 2, 2)) + @test !anyna(da) + da[2] = NA + @test anyna(da) + + # anyna(pda::PooledDataArray) + anyna(PooledDataArray([1, 2], falses(2))) + anyna(PooledDataArray(repeat([1, 2], outer = [1, 2]), falses(2, 2))) + pda = PooledDataArray(repeat([1, 2], outer = [1, 2, 2]), falses(2, 2, 2)) + @test !anyna(pda) + pda[2] = NA + @test anyna(pda) + + # allna(a::AbstractArray) + allna([1, 2]) + allna(repeat([1, 2], outer = [1, 2])) + @test !allna(repeat([1, 2], outer = [1, 2, 2])) + + # allna(da::DataArray) + allna(DataArray([1, 2], falses(2))) + allna(DataArray(repeat([1, 2], outer = [1, 2]), falses(2, 2))) + da = DataArray(repeat([1, 2], outer = [1, 2, 2]), falses(2, 2, 2)) + da[1] = NA + @test !allna(da) + da[:] = NA + @test allna(da) + + # allna(da::PooledDataArray) + allna(PooledDataArray([1, 2], falses(2))) + allna(PooledDataArray(repeat([1, 2], outer = [1, 2]), falses(2, 2))) + pda = PooledDataArray(repeat([1, 2], outer = [1, 2, 2]), falses(2, 2, 2)) + pda[1] = NA + @test !allna(pda) + pda[:] = NA + @test allna(pda) + dv = DataArray([1, 2, 3], bitpack([false, false, false])) dv = DataArray([1, 2, 3], [false, false, false]) - failNA(dv) - dropna(dv) - replaceNA(dv, 3) - for v in each_failNA(dv) - println(v) - end - for v in each_dropna(dv) - println(v) - end - for v in each_replaceNA(dv, 3) - println(v) - end + a = dropna(dv) + for v in each_failNA(dv); end + @test collect(each_failNA(dv)) == a + @test collect(each_dropna(dv)) == a + @test collect(each_replaceNA(dv, 4)) == a dv[1] = NA - failNA(dv) - dropna(dv) - replaceNA(dv, 3) - for v in each_failNA(dv) - println(v) - end - for v in each_dropna(dv) - println(v) - end - for v in each_replaceNA(dv, 3) - println(v) - end - - dv = DataArray(ComplexPair, 5) - - type MyType - a::Int - b::Int - end - - dv = DataArray(MyType, 5) + a = dropna(dv) + @test_throws NAException for v in each_failNA(dv); end + @test collect(each_dropna(dv)) == a + @test collect(each_replaceNA(dv, 4)) == [4, a] end diff --git a/test/newtests/dataarray.jl b/test/newtests/dataarray.jl index 0be0b5b54..812dc42a8 100644 --- a/test/newtests/dataarray.jl +++ b/test/newtests/dataarray.jl @@ -137,11 +137,11 @@ module TestDataArrays [1, 2, 3][dinds] # Base.getindex{S, T}(x::Vector{S}, inds::AbstractDataArray{T}) - dinds = @data([1, 2, NA]) + dinds = @data([1, 2, NA]) @test_throws ErrorException [1.0, 2.0, 3.0, 4.0][dinds] # Base.getindex{S, T}(x::Array{S}, inds::AbstractDataArray{T}) - dinds = @data([1, 2, NA]) + dinds = @data([1, 2, NA]) @test_throws ErrorException [1.0 2.0; 3.0 4.0][dinds] # Base.getindex(d::DataArray, i::SingleIndex) @@ -259,26 +259,6 @@ module TestDataArrays isfinite(DataArray(repeat([1, 2], outer = [1, 2]), falses(2, 2))) isfinite(DataArray(repeat([1, 2], outer = [1, 2, 2]), falses(2, 2, 2))) - # anyna(a::AbstractArray) - anyna([1, 2]) - anyna(repeat([1, 2], outer = [1, 2])) - anyna(repeat([1, 2], outer = [1, 2, 2])) - - # anyna(d::AbstractDataArray) - anyna(DataArray([1, 2], falses(2))) - anyna(DataArray(repeat([1, 2], outer = [1, 2]), falses(2, 2))) - anyna(DataArray(repeat([1, 2], outer = [1, 2, 2]), falses(2, 2, 2))) - - # allna(a::AbstractArray) - allna([1, 2]) - allna(repeat([1, 2], outer = [1, 2])) - allna(repeat([1, 2], outer = [1, 2, 2])) - - # allna(d::AbstractDataArray) - allna(DataArray([1, 2], falses(2))) - allna(DataArray(repeat([1, 2], outer = [1, 2]), falses(2, 2))) - allna(DataArray(repeat([1, 2], outer = [1, 2, 2]), falses(2, 2, 2))) - # Base.start(x::AbstractDataArray) start(DataArray([1, 2], falses(2))) start(DataArray(repeat([1, 2], outer = [1, 2]), falses(2, 2))) diff --git a/test/runtests.jl b/test/runtests.jl index 16bb1244d..69275b24d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -15,7 +15,7 @@ my_tests = ["abstractarray.jl", "datamatrix.jl", "indexing.jl", "linalg.jl", - #"test/nas.jl", + "nas.jl", "operators.jl", "reduce.jl", "reducedim.jl", From d85c06daf5bb6bf33d659db73708bbab07a9263c Mon Sep 17 00:00:00 2001 From: Sean Garborg Date: Tue, 29 Jul 2014 18:13:43 -0400 Subject: [PATCH 2/2] Replace tabs with spaces --- test/abstractarray.jl | 14 ++-- test/booleans.jl | 40 +++++----- test/constructors.jl | 180 +++++++++++++++++++++--------------------- test/dataarray.jl | 70 ++++++++-------- test/extras.jl | 86 ++++++++++---------- test/linalg.jl | 10 +-- test/nas.jl | 96 +++++++++++----------- test/operators.jl | 32 ++++---- test/padding.jl | 14 ++-- test/statistics.jl | 8 +- 10 files changed, 275 insertions(+), 275 deletions(-) diff --git a/test/abstractarray.jl b/test/abstractarray.jl index d7c81e789..21a4bd2e9 100644 --- a/test/abstractarray.jl +++ b/test/abstractarray.jl @@ -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 diff --git a/test/booleans.jl b/test/booleans.jl index 31b6d95a4..281c9f596 100644 --- a/test/booleans.jl +++ b/test/booleans.jl @@ -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 diff --git a/test/constructors.jl b/test/constructors.jl index bc9dcc60b..95e3f3828 100644 --- a/test/constructors.jl +++ b/test/constructors.jl @@ -1,128 +1,128 @@ module TestConstructors - using Base.Test - using DataArrays + using Base.Test + using DataArrays - # - # NA's - # + # + # NA's + # - @assert isna(NAtype()) - @assert isna(NA) + @assert isna(NAtype()) + @assert isna(NA) - # - # DataVector's - # + # + # DataVector's + # - dv = DataArray([1, 2, 3], falses(3)) - @assert isequal(dv.data, [1, 2, 3]) - @assert isequal(dv.na, falses(3)) - @assert isequal(dv, DataArray([1, 2, 3], [false, false, false])) - @assert isequal(dv, DataArray([1, 2, 3])) + dv = DataArray([1, 2, 3], falses(3)) + @assert isequal(dv.data, [1, 2, 3]) + @assert isequal(dv.na, falses(3)) + @assert isequal(dv, DataArray([1, 2, 3], [false, false, false])) + @assert isequal(dv, DataArray([1, 2, 3])) - dv = convert(DataArray, trues(3)) - @assert isequal(dv.data, [true, true, true]) - @assert isequal(dv.na, falses(3)) - @assert isequal(dv, convert(DataArray, trues(3))) + dv = convert(DataArray, trues(3)) + @assert isequal(dv.data, [true, true, true]) + @assert isequal(dv.na, falses(3)) + @assert isequal(dv, convert(DataArray, trues(3))) - dv = DataArray([1, 2, 3], falses(3)) - @assert isequal(dv, convert(DataArray, 1:3)) + dv = DataArray([1, 2, 3], falses(3)) + @assert isequal(dv, convert(DataArray, 1:3)) - dv = DataArray(Int, 3) - @assert isequal(eltype(dv), Int) - @assert isequal(dv.na, trues(3)) + dv = DataArray(Int, 3) + @assert isequal(eltype(dv), Int) + @assert isequal(dv.na, trues(3)) - dv = convert(DataArray, zeros(3)) - @assert isequal(dv, convert(DataArray, zeros(3))) + dv = convert(DataArray, zeros(3)) + @assert isequal(dv, convert(DataArray, zeros(3))) - dv = convert(DataArray, zeros(Int, 3)) - @assert isequal(dv, convert(DataArray, zeros(Int, 3))) + dv = convert(DataArray, zeros(Int, 3)) + @assert isequal(dv, convert(DataArray, zeros(Int, 3))) - dv = convert(DataArray, ones(3)) - @assert isequal(dv, convert(DataArray, ones(3))) + dv = convert(DataArray, ones(3)) + @assert isequal(dv, convert(DataArray, ones(3))) - dv = convert(DataArray, ones(Int, 3)) - @assert isequal(dv, convert(DataArray, ones(Int, 3))) + dv = convert(DataArray, ones(Int, 3)) + @assert isequal(dv, convert(DataArray, ones(Int, 3))) - dv = convert(DataArray, falses(3)) - @assert isequal(dv, convert(DataArray, falses(3))) + dv = convert(DataArray, falses(3)) + @assert isequal(dv, convert(DataArray, falses(3))) - dv = convert(DataArray, trues(3)) - @assert isequal(dv, convert(DataArray, trues(3))) + dv = convert(DataArray, trues(3)) + @assert isequal(dv, convert(DataArray, trues(3))) - # - # PooledDataArray's - # + # + # PooledDataArray's + # - pdv = PooledDataArray([1, 2, 3], falses(3)) - @assert all(pdv .== [1, 2, 3]) - @assert all(isna(pdv) .== falses(3)) + pdv = PooledDataArray([1, 2, 3], falses(3)) + @assert all(pdv .== [1, 2, 3]) + @assert all(isna(pdv) .== falses(3)) - @assert isequal(pdv, PooledDataArray([1, 2, 3], [false, false, false])) - @assert isequal(pdv, PooledDataArray([1, 2, 3])) + @assert isequal(pdv, PooledDataArray([1, 2, 3], [false, false, false])) + @assert isequal(pdv, PooledDataArray([1, 2, 3])) - pdv = convert(PooledDataArray, trues(3)) - @assert all(pdv .== [true, true, true]) - @assert all(isna(pdv) .== falses(3)) - @assert isequal(pdv, convert(PooledDataArray, trues(3))) + pdv = convert(PooledDataArray, trues(3)) + @assert all(pdv .== [true, true, true]) + @assert all(isna(pdv) .== falses(3)) + @assert isequal(pdv, convert(PooledDataArray, trues(3))) - pdv = PooledDataArray([1, 2, 3], falses(3)) - @assert isequal(pdv, convert(PooledDataArray, 1:3)) - @assert isequal(pdv, convert(PooledDataArray, PooledDataArray([1, 2, 3]))) + pdv = PooledDataArray([1, 2, 3], falses(3)) + @assert isequal(pdv, convert(PooledDataArray, 1:3)) + @assert isequal(pdv, convert(PooledDataArray, PooledDataArray([1, 2, 3]))) - pdv = PooledDataArray(Int, 3) - @assert isequal(eltype(pdv), Int) - @assert all(isna(pdv) .== trues(3)) + pdv = PooledDataArray(Int, 3) + @assert isequal(eltype(pdv), Int) + @assert all(isna(pdv) .== trues(3)) - pdv = convert(PooledDataArray, zeros(3)) - @assert isequal(pdv, convert(PooledDataArray, zeros(3))) + pdv = convert(PooledDataArray, zeros(3)) + @assert isequal(pdv, convert(PooledDataArray, zeros(3))) - pdv = convert(PooledDataArray, zeros(Int, 3)) - @assert isequal(pdv, convert(PooledDataArray, zeros(Int, 3))) + pdv = convert(PooledDataArray, zeros(Int, 3)) + @assert isequal(pdv, convert(PooledDataArray, zeros(Int, 3))) - pdv = convert(PooledDataArray, ones(3)) - @assert isequal(pdv, convert(PooledDataArray, ones(3))) + pdv = convert(PooledDataArray, ones(3)) + @assert isequal(pdv, convert(PooledDataArray, ones(3))) - pdv = convert(PooledDataArray, ones(Int, 3)) - @assert isequal(pdv, convert(PooledDataArray, ones(Int, 3))) + pdv = convert(PooledDataArray, ones(Int, 3)) + @assert isequal(pdv, convert(PooledDataArray, ones(Int, 3))) - pdv = convert(PooledDataArray, falses(3)) - @assert isequal(pdv, convert(PooledDataArray, falses(3))) + pdv = convert(PooledDataArray, falses(3)) + @assert isequal(pdv, convert(PooledDataArray, falses(3))) - pdv = convert(PooledDataArray, trues(3)) - @assert isequal(pdv, convert(PooledDataArray, trues(3))) + pdv = convert(PooledDataArray, trues(3)) + @assert isequal(pdv, convert(PooledDataArray, trues(3))) - # - # DataMatrix - # + # + # DataMatrix + # - dm = DataArray([1 2; 3 4], falses(2, 2)) - @assert isequal(dm.data, [1 2; 3 4]) - @assert isequal(dm.na, falses(2, 2)) + dm = DataArray([1 2; 3 4], falses(2, 2)) + @assert isequal(dm.data, [1 2; 3 4]) + @assert isequal(dm.na, falses(2, 2)) - @assert isequal(dm, DataArray([1 2; 3 4], [false false; false false])) - @assert isequal(dm, DataArray([1 2; 3 4])) + @assert isequal(dm, DataArray([1 2; 3 4], [false false; false false])) + @assert isequal(dm, DataArray([1 2; 3 4])) - dm = convert(DataArray, trues(2, 2)) - @assert isequal(dm.data, trues(2, 2)) - @assert isequal(dm.na, falses(2, 2)) + dm = convert(DataArray, trues(2, 2)) + @assert isequal(dm.data, trues(2, 2)) + @assert isequal(dm.na, falses(2, 2)) - @assert isequal(dm, convert(DataArray, trues(2, 2))) + @assert isequal(dm, convert(DataArray, trues(2, 2))) - dm = DataArray(Int, 2, 2) - @assert isequal(eltype(dm), Int) - @assert isequal(dm.na, trues(2, 2)) + dm = DataArray(Int, 2, 2) + @assert isequal(eltype(dm), Int) + @assert isequal(dm.na, trues(2, 2)) - convert(DataArray, zeros(2, 2)) + convert(DataArray, zeros(2, 2)) - convert(DataArray, zeros(Int, 2, 2)) + convert(DataArray, zeros(Int, 2, 2)) - convert(DataArray, ones(2, 2)) - convert(DataArray, ones(Int, 2, 2)) + convert(DataArray, ones(2, 2)) + convert(DataArray, ones(Int, 2, 2)) - convert(DataArray, falses(2, 2)) - convert(DataArray, trues(2, 2)) + convert(DataArray, falses(2, 2)) + convert(DataArray, trues(2, 2)) - convert(DataArray, eye(3, 2)) - convert(DataArray, eye(2)) - convert(DataArray, diagm(Float64[pi, pi])) + convert(DataArray, eye(3, 2)) + convert(DataArray, eye(2)) + convert(DataArray, diagm(Float64[pi, pi])) end diff --git a/test/dataarray.jl b/test/dataarray.jl index 68016b8f5..1a336b69f 100644 --- a/test/dataarray.jl +++ b/test/dataarray.jl @@ -1,31 +1,31 @@ module TestDataArray - using Base.Test - using DataArrays + using Base.Test + using DataArrays - v = [1, 2, 3, 4] - dv = DataArray(v, falses(size(v))) + v = [1, 2, 3, 4] + dv = DataArray(v, falses(size(v))) - m = [1 2; 3 4] - dm = DataArray(m, falses(size(m))) + m = [1 2; 3 4] + dm = DataArray(m, falses(size(m))) - t = Array(Int, 2, 2, 2) - t[1:2, 1:2, 1:2] = 1 - dt = DataArray(t, falses(size(t))) + t = Array(Int, 2, 2, 2) + t[1:2, 1:2, 1:2] = 1 + dt = DataArray(t, falses(size(t))) - dv = DataArray(v) - dv = DataArray(v, [false, false, false, false]) + dv = DataArray(v) + dv = DataArray(v, [false, false, false, false]) - dv = DataArray(Int, 2) - dm = DataArray(Int, 2, 2) - dt = DataArray(Int, 2, 2, 2) + dv = DataArray(Int, 2) + dm = DataArray(Int, 2, 2) + dt = DataArray(Int, 2, 2, 2) - similar(dv) - similar(dm) - similar(dt) + similar(dv) + similar(dm) + similar(dt) - similar(dv, 2) - similar(dm, 2, 2) - similar(dt, 2, 2, 2) + similar(dv, 2) + similar(dm, 2, 2) + similar(dt, 2, 2, 2) x = DataArray([9, 9, 8]) @@ -49,19 +49,19 @@ module TestDataArray @assert isequal(levels(x), @data [1, -2, 4]) @assert isequal(levels(reverse(x)), @data [4, 1, -2]) - # Test vecbind - # a = [1:4] - # d = DataArray(a) - # @assert isequal(vecbind(a,a), [a,a]) - # @assert isequal(vecbind(a,1.0 * a), 1.0 * [a,a]) - # @assert isequal(vecbind(d,a), [d,d]) - # @assert isequal(vecbind(a,d), [d,d]) - # @assert isequal(vecbind(1.0 * a,d), 1.0 * [d,d]) - # @assert isequal(vecbind(d,d), [d,d]) - # @assert isequal(vecbind(a,IndexedVector(a)), [a,a]) - # @assert isequal(vecbind(a,IndexedVector(d)), [d,d]) - # @assert isequal(vecbind(PooledDataArray(a),IndexedVector(d)), [d,d]) - # @assert isequal(vecbind(PooledDataArray(a),IndexedVector(a)), [a,a]) - # @assert isequal(vecbind(a,RepeatedVector(a,2)), [a,a,a]) - # @assert (vecbind(a,StackedVector({a,1.0*a})) == [a,a,a]) + # Test vecbind + # a = [1:4] + # d = DataArray(a) + # @assert isequal(vecbind(a,a), [a,a]) + # @assert isequal(vecbind(a,1.0 * a), 1.0 * [a,a]) + # @assert isequal(vecbind(d,a), [d,d]) + # @assert isequal(vecbind(a,d), [d,d]) + # @assert isequal(vecbind(1.0 * a,d), 1.0 * [d,d]) + # @assert isequal(vecbind(d,d), [d,d]) + # @assert isequal(vecbind(a,IndexedVector(a)), [a,a]) + # @assert isequal(vecbind(a,IndexedVector(d)), [d,d]) + # @assert isequal(vecbind(PooledDataArray(a),IndexedVector(d)), [d,d]) + # @assert isequal(vecbind(PooledDataArray(a),IndexedVector(a)), [a,a]) + # @assert isequal(vecbind(a,RepeatedVector(a,2)), [a,a,a]) + # @assert (vecbind(a,StackedVector({a,1.0*a})) == [a,a,a]) end diff --git a/test/extras.jl b/test/extras.jl index 2af843a60..1cc305e64 100644 --- a/test/extras.jl +++ b/test/extras.jl @@ -1,51 +1,51 @@ module TestExtras - using Base.Test - using DataArrays - using StatsBase - - ########## - ## countmap - ########## + using Base.Test + using DataArrays + using StatsBase - d = @data [NA,3,3] - w = weights([1.1,2.2,3.3]) - cm = Dict{Union(Int, NAtype), Int}(@data([NA,3]), [1,2]) - cmw = Dict{Union(Int, NAtype), Real}(@data([NA,3]), [1.1,5.5]) - @assert isequal(countmap(d), cm) - @assert isequal(countmap(d, w), cmw) + ########## + ## countmap + ########## - ########## - ## cut - ########## + d = @data [NA,3,3] + w = weights([1.1,2.2,3.3]) + cm = Dict{Union(Int, NAtype), Int}(@data([NA,3]), [1,2]) + cmw = Dict{Union(Int, NAtype), Real}(@data([NA,3]), [1.1,5.5]) + @assert isequal(countmap(d), cm) + @assert isequal(countmap(d, w), cmw) - @assert isequal(cut([2, 3, 5], [1, 3, 6]), PooledDataArray(["(1,3]", "(1,3]", "(3,6]"])) - @assert isequal(cut([2, 3, 5], [3, 6]), PooledDataArray(["[2,3]", "[2,3]", "(3,6]"])) - @assert isequal(cut([2, 3, 5, 6], [3, 6]), PooledDataArray(["[2,3]", "[2,3]", "(3,6]", "(3,6]"])) - @assert isequal(cut([1, 2, 4], [1, 3, 6]), PooledDataArray(["[1,3]", "[1,3]", "(3,6]"])) - @assert isequal(cut([1, 2, 4], [3, 6]), PooledDataArray(["[1,3]", "[1,3]", "(3,6]"])) - @assert isequal(cut([1, 2, 4], [3]), PooledDataArray(["[1,3]", "[1,3]", "(3,4]"])) - @assert isequal(cut([1, 5, 7], [3, 6]), PooledDataArray(["[1,3]", "(3,6]", "(6,7]"])) + ########## + ## cut + ########## - ages = [20, 22, 25, 27, 21, 23, 37, 31, 61, 45, 41, 32] - bins = [18, 25, 35, 60, 100] - cats = cut(ages, bins) - pdv = PooledDataArray(["(18,25]", "(18,25]", "(18,25]", - "(25,35]", "(18,25]", "(18,25]", - "(35,60]", "(25,35]", "(60,100]", - "(35,60]", "(35,60]", "(25,35]"]) - @assert isequal(cats, pdv) + @assert isequal(cut([2, 3, 5], [1, 3, 6]), PooledDataArray(["(1,3]", "(1,3]", "(3,6]"])) + @assert isequal(cut([2, 3, 5], [3, 6]), PooledDataArray(["[2,3]", "[2,3]", "(3,6]"])) + @assert isequal(cut([2, 3, 5, 6], [3, 6]), PooledDataArray(["[2,3]", "[2,3]", "(3,6]", "(3,6]"])) + @assert isequal(cut([1, 2, 4], [1, 3, 6]), PooledDataArray(["[1,3]", "[1,3]", "(3,6]"])) + @assert isequal(cut([1, 2, 4], [3, 6]), PooledDataArray(["[1,3]", "[1,3]", "(3,6]"])) + @assert isequal(cut([1, 2, 4], [3]), PooledDataArray(["[1,3]", "[1,3]", "(3,4]"])) + @assert isequal(cut([1, 5, 7], [3, 6]), PooledDataArray(["[1,3]", "(3,6]", "(6,7]"])) - ########## - ## rep - ########## + ages = [20, 22, 25, 27, 21, 23, 37, 31, 61, 45, 41, 32] + bins = [18, 25, 35, 60, 100] + cats = cut(ages, bins) + pdv = PooledDataArray(["(18,25]", "(18,25]", "(18,25]", + "(25,35]", "(18,25]", "(18,25]", + "(35,60]", "(25,35]", "(60,100]", + "(35,60]", "(35,60]", "(25,35]"]) + @assert isequal(cats, pdv) - @assert rep(3, 2) == [3,3] - @assert rep([3,4], 2) == [3,4,3,4] - @assert rep([3,4], 1, 2) == [3,3,4,4] - @assert rep([3,4], each = 2) == [3,3,4,4] - @assert rep([3,4], times = 2) == [3,4,3,4] - @assert rep([3,4], [2,3]) == [3,3,4,4,4] - @assert isequal(rep((@data [NA,3,4]), 2), (@data [NA,3,4,NA,3,4])) - @assert isequal(rep((@data [NA,3,4]), [2,1,2]), (@data [NA,NA,3,4,4])) - @assert isequal(rep((@data [NA,3,4]), [2,1,0]), (@data [NA,NA,3])) + ########## + ## rep + ########## + + @assert rep(3, 2) == [3,3] + @assert rep([3,4], 2) == [3,4,3,4] + @assert rep([3,4], 1, 2) == [3,3,4,4] + @assert rep([3,4], each = 2) == [3,3,4,4] + @assert rep([3,4], times = 2) == [3,4,3,4] + @assert rep([3,4], [2,3]) == [3,3,4,4,4] + @assert isequal(rep((@data [NA,3,4]), 2), (@data [NA,3,4,NA,3,4])) + @assert isequal(rep((@data [NA,3,4]), [2,1,2]), (@data [NA,NA,3,4,4])) + @assert isequal(rep((@data [NA,3,4]), [2,1,0]), (@data [NA,NA,3])) end diff --git a/test/linalg.jl b/test/linalg.jl index 24e0aadd1..957559065 100644 --- a/test/linalg.jl +++ b/test/linalg.jl @@ -1,9 +1,9 @@ module TestLinAlg - using Base.Test - using DataArrays + using Base.Test + using DataArrays - d = @data eye(3, 3) - d[1, 1] = NA + d = @data eye(3, 3) + d[1, 1] = NA - svd(d) + svd(d) end diff --git a/test/nas.jl b/test/nas.jl index a780028af..2407c8ab7 100644 --- a/test/nas.jl +++ b/test/nas.jl @@ -1,66 +1,66 @@ module TestNAs - using Base.Test - using DataArrays + using Base.Test + using DataArrays - # anyna(a::AbstractArray) - anyna([1, 2]) - anyna(repeat([1, 2], outer = [1, 2])) - @test !anyna(repeat([1, 2], outer = [1, 2, 2])) + # anyna(a::AbstractArray) + anyna([1, 2]) + anyna(repeat([1, 2], outer = [1, 2])) + @test !anyna(repeat([1, 2], outer = [1, 2, 2])) - # anyna(da::DataArray) - anyna(DataArray([1, 2], falses(2))) - anyna(DataArray(repeat([1, 2], outer = [1, 2]), falses(2, 2))) - da = DataArray(repeat([1, 2], outer = [1, 2, 2]), falses(2, 2, 2)) - @test !anyna(da) - da[2] = NA - @test anyna(da) + # anyna(da::DataArray) + anyna(DataArray([1, 2], falses(2))) + anyna(DataArray(repeat([1, 2], outer = [1, 2]), falses(2, 2))) + da = DataArray(repeat([1, 2], outer = [1, 2, 2]), falses(2, 2, 2)) + @test !anyna(da) + da[2] = NA + @test anyna(da) - # anyna(pda::PooledDataArray) - anyna(PooledDataArray([1, 2], falses(2))) - anyna(PooledDataArray(repeat([1, 2], outer = [1, 2]), falses(2, 2))) - pda = PooledDataArray(repeat([1, 2], outer = [1, 2, 2]), falses(2, 2, 2)) - @test !anyna(pda) - pda[2] = NA - @test anyna(pda) + # anyna(pda::PooledDataArray) + anyna(PooledDataArray([1, 2], falses(2))) + anyna(PooledDataArray(repeat([1, 2], outer = [1, 2]), falses(2, 2))) + pda = PooledDataArray(repeat([1, 2], outer = [1, 2, 2]), falses(2, 2, 2)) + @test !anyna(pda) + pda[2] = NA + @test anyna(pda) - # allna(a::AbstractArray) - allna([1, 2]) - allna(repeat([1, 2], outer = [1, 2])) - @test !allna(repeat([1, 2], outer = [1, 2, 2])) + # allna(a::AbstractArray) + allna([1, 2]) + allna(repeat([1, 2], outer = [1, 2])) + @test !allna(repeat([1, 2], outer = [1, 2, 2])) - # allna(da::DataArray) - allna(DataArray([1, 2], falses(2))) - allna(DataArray(repeat([1, 2], outer = [1, 2]), falses(2, 2))) - da = DataArray(repeat([1, 2], outer = [1, 2, 2]), falses(2, 2, 2)) - da[1] = NA - @test !allna(da) - da[:] = NA - @test allna(da) + # allna(da::DataArray) + allna(DataArray([1, 2], falses(2))) + allna(DataArray(repeat([1, 2], outer = [1, 2]), falses(2, 2))) + da = DataArray(repeat([1, 2], outer = [1, 2, 2]), falses(2, 2, 2)) + da[1] = NA + @test !allna(da) + da[:] = NA + @test allna(da) - # allna(da::PooledDataArray) - allna(PooledDataArray([1, 2], falses(2))) - allna(PooledDataArray(repeat([1, 2], outer = [1, 2]), falses(2, 2))) - pda = PooledDataArray(repeat([1, 2], outer = [1, 2, 2]), falses(2, 2, 2)) - pda[1] = NA - @test !allna(pda) - pda[:] = NA - @test allna(pda) + # allna(da::PooledDataArray) + allna(PooledDataArray([1, 2], falses(2))) + allna(PooledDataArray(repeat([1, 2], outer = [1, 2]), falses(2, 2))) + pda = PooledDataArray(repeat([1, 2], outer = [1, 2, 2]), falses(2, 2, 2)) + pda[1] = NA + @test !allna(pda) + pda[:] = NA + @test allna(pda) - dv = DataArray([1, 2, 3], bitpack([false, false, false])) + dv = DataArray([1, 2, 3], bitpack([false, false, false])) - dv = DataArray([1, 2, 3], [false, false, false]) + dv = DataArray([1, 2, 3], [false, false, false]) - a = dropna(dv) + a = dropna(dv) for v in each_failNA(dv); end - @test collect(each_failNA(dv)) == a + @test collect(each_failNA(dv)) == a @test collect(each_dropna(dv)) == a - @test collect(each_replaceNA(dv, 4)) == a + @test collect(each_replaceNA(dv, 4)) == a - dv[1] = NA + dv[1] = NA - a = dropna(dv) - @test_throws NAException for v in each_failNA(dv); end + a = dropna(dv) + @test_throws NAException for v in each_failNA(dv); end @test collect(each_dropna(dv)) == a @test collect(each_replaceNA(dv, 4)) == [4, a] end diff --git a/test/operators.jl b/test/operators.jl index 0a925d55b..7a7347ccd 100644 --- a/test/operators.jl +++ b/test/operators.jl @@ -1,19 +1,19 @@ module TestOperators - using Base.Test - using DataArrays, StatsBase - - macro test_da_pda(da, code) - esc(quote - let $da = copy($da) - $code - end - let $da = PooledDataArray($da) - $code - end - end) - end - - # All unary operators return NA when evaluating NA + using Base.Test + using DataArrays, StatsBase + + macro test_da_pda(da, code) + esc(quote + let $da = copy($da) + $code + end + let $da = PooledDataArray($da) + $code + end + end) + end + + # All unary operators return NA when evaluating NA for f in map(eval, DataArrays.unary_operators) @assert isna(f(NA)) end @@ -213,7 +213,7 @@ module TestOperators isequal((curdv/2)[i], (curdv[i]/2)) end end - + # Unary vector operators on DataVector's dv = convert(DataArray, ones(5)) for f in map(eval, DataArrays.unary_vector_operators) diff --git a/test/padding.jl b/test/padding.jl index 9257a19ef..adfcc7955 100644 --- a/test/padding.jl +++ b/test/padding.jl @@ -1,10 +1,10 @@ module TestPadding - using Base.Test - using DataArrays + using Base.Test + using DataArrays - dv = @data ones(3) - @assert isequal(dv, padNA(dv, 0, 0)) - @assert length(padNA(dv, 2, 0)) == length(dv) + 2 - @assert length(padNA(dv, 0, 2)) == length(dv) + 2 - @assert length(padNA(dv, 2, 2)) == length(dv) + 4 + dv = @data ones(3) + @assert isequal(dv, padNA(dv, 0, 0)) + @assert length(padNA(dv, 2, 0)) == length(dv) + 2 + @assert length(padNA(dv, 0, 2)) == length(dv) + 2 + @assert length(padNA(dv, 2, 2)) == length(dv) + 4 end diff --git a/test/statistics.jl b/test/statistics.jl index e70188c32..5dc06860f 100644 --- a/test/statistics.jl +++ b/test/statistics.jl @@ -1,8 +1,8 @@ module TestStats - using Base.Test - using DataArrays + using Base.Test + using DataArrays - autocor(DataArray([1, 2, 3, 4, 5])) + autocor(DataArray([1, 2, 3, 4, 5])) - @assert isequal(xtabs([1, 2, 2, 2, 3]), [2 => 3, 3 => 1, 1 => 1]) + @assert isequal(xtabs([1, 2, 2, 2, 3]), [2 => 3, 3 => 1, 1 => 1]) end