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

Commit

Permalink
Merge pull request #139 from JuliaStats/depr
Browse files Browse the repository at this point in the history
Match Base syntax, remove type instability from gl
  • Loading branch information
garborg committed Jan 26, 2015
2 parents 359b9f3 + 5c1b361 commit 3a8cad5
Show file tree
Hide file tree
Showing 15 changed files with 96 additions and 82 deletions.
4 changes: 2 additions & 2 deletions benchmark/operators.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module DataArraysBenchmark
using DataArrays, Benchmark
using DataArrays, Benchmark, Compat

# seed rng for more consistent timings
srand(1776)
Expand All @@ -26,7 +26,7 @@ function make_test_types(genfunc, sz)
)
end

make_bools{N}(x::NTuple{N}) = convert(Array{Bool, N}, randbool(x...))
make_bools{N}(x::NTuple{N}) = convert(Array{Bool, N}, bitrand(x...))
make_bools(x::Integer...) = make_bools(x)

macro perf(fn, replications, idx...)
Expand Down
19 changes: 10 additions & 9 deletions src/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ function check_broadcast_shape(shape::Dims, As::Union(AbstractArray,Number)...)
samesize = true
for A in As
if ndims(A) > length(shape)
error("cannot broadcast array to have fewer dimensions")
throw(DimensionMismatch("cannot broadcast array to have fewer dimensions"))
end
for k in 1:length(shape)
n, nA = shape[k], size(A, k)
samesize &= (n == nA)
if n != nA != 1
error("array could not be broadcast to match destination")
throw(DimensionMismatch("array could not be broadcast to match destination"))
end
end
end
Expand Down Expand Up @@ -183,9 +183,9 @@ for bsig in (DataArray, PooledDataArray), asig in (Union(Array,BitArray,Number),
@eval let cache = Dict{Function,Dict{Uint64,Dict{Int,Function}}}()
function Base.map!(f::Base.Callable, B::$bsig, As::$asig...)
nd = ndims(B)
length(As) <= 8 || error("too many arguments")
length(As) <= 8 || throw(ArgumentError("too many arguments"))
samesize = check_broadcast_shape(size(B), As...)
samesize || error("dimensions must match")
samesize || throw(DimensionMismatch("Argument dimensions must match"))
arrtype = datype_int(As...)

cache_f = @get! cache f Dict{Uint64,Dict{Int,Function}}()
Expand All @@ -200,7 +200,7 @@ for bsig in (DataArray, PooledDataArray), asig in (Union(Array,BitArray,Number),
invoke(Base.map!, (Base.Callable, $bsig, $asig), f, B, r)
function Base.broadcast!(f::Function, B::$bsig, As::$asig...)
nd = ndims(B)
length(As) <= 8 || error("too many arguments")
length(As) <= 8 || throw(ArgumentError("too many arguments"))
samesize = check_broadcast_shape(size(B), As...)
arrtype = datype_int(As...)

Expand Down Expand Up @@ -235,7 +235,7 @@ macro da_broadcast_vararg(func)
if (func.head != :function && func.head != :(=)) ||
func.args[1].head != :call || !isa(func.args[1].args[end], Expr) ||
func.args[1].args[end].head != :...
error("@da_broadcast_vararg may only be applied to vararg functions")
throw(ArgumentError("@da_broadcast_vararg may only be applied to vararg functions"))
end

va = func.args[1].args[end]
Expand All @@ -261,8 +261,9 @@ end

macro da_broadcast_binary(func)
if (func.head != :function && func.head != :(=)) ||
func.args[1].head != :call || length(func.args[1].args) != 3
error("@da_broadcast_binary may only be applied to two-argument functions")
func.args[1].head != :call ||
length(func.args[1].args) != 3
throw(ArgumentError("@da_broadcast_binary may only be applied to two-argument functions"))
end
(f, A, B) = func.args[1].args
body = func.args[2]
Expand All @@ -276,7 +277,7 @@ end
# Broadcasting DataArrays returns a DataArray
@da_broadcast_vararg Base.broadcast(f::Function, As...) = databroadcast(f, As...)

# Definitions for operators,
# Definitions for operators,
Base.(:(.*))(A::BitArray, B::Union(DataArray{Bool}, PooledDataArray{Bool})) = databroadcast(*, A, B)
Base.(:(.*))(A::Union(DataArray{Bool}, PooledDataArray{Bool}), B::BitArray) = databroadcast(*, A, B)
@da_broadcast_vararg Base.(:(.*))(As...) = databroadcast(*, As...)
Expand Down
2 changes: 1 addition & 1 deletion src/extras.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ cut(x::AbstractVector, ngroups::Integer) = cut(x, quantile(x, [1 : ngroups - 1]

function rep{T <: Integer}(x::AbstractVector, lengths::AbstractVector{T})
if length(x) != length(lengths)
error("vector lengths must match")
throw(DimensionMismatch("vector lengths must match"))
end
res = similar(x, sum(lengths))
i = 1
Expand Down
8 changes: 4 additions & 4 deletions src/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ end

# Indexing with NA throws an error
function Base.to_index(A::DataArray)
any(A.na) && error("cannot index an array with a DataArray containing NA values")
any(A.na) && throw(NAException("cannot index an array with a DataArray containing NA values"))
Base.to_index(A.data)
end

# Fast implementation of checkbounds for DataArray input
Base.checkbounds(sz::Int, I::AbstractDataVector{Bool}) =
length(I) == sz || throw(BoundsError())
function Base.checkbounds{T<:Real}(sz::Int, I::AbstractDataArray{T})
anyna(I) && error("cannot index into an array with a DataArray containing NAs")
anyna(I) && throw(NAException("cannot index into an array with a DataArray containing NAs"))
extr = daextract(I)
for i = 1:length(I)
@inbounds v = unsafe_getindex_notna(I, extr, i)
Expand All @@ -100,9 +100,9 @@ end

# Fallbacks to avoid ambiguity
setindex!(t::AbstractDataArray, x, i::Real) =
error("setindex! not defined for ",typeof(t))
throw(MethodError(setindex!, typeof(t), typeof(x), typeof(i)))
getindex(t::AbstractDataArray, i::Real) =
error("indexing not defined for ", typeof(t))
throw(MethodError(getindex, typeof(t), typeof(i)))

## getindex: DataArray

Expand Down
8 changes: 4 additions & 4 deletions src/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ end
macro swappable(func, syms...)
if (func.head != :function && func.head != :(=)) ||
func.args[1].head != :call || length(func.args[1].args) != 3
error("@swappable may only be applied to functions of two arguments")
throw(ArgumentError("@swappable may only be applied to functions of two arguments"))
end

func2 = deepcopy(func)
fname = func2.args[1].args[1]
if isa(fname, Expr) && fname.head == :curly
Expand All @@ -238,7 +238,7 @@ macro swappable(func, syms...)

for s in unique([fname, syms...])
if swapargs(func2, s) < 1
error("No argument swapped")
throw(ErrorException("No argument swapped"))
end
end
esc(Expr(:block, func, func2))
Expand Down Expand Up @@ -796,7 +796,7 @@ end
Base.(:/){T,N}(b::AbstractArray{T,N}, ::NAtype) =
DataArray(Array(T, size(b)), trues(size(b)))
@dataarray_binary_scalar Base.(:/) Base.(:/) eltype(a) <: FloatingPoint || typeof(b) <: FloatingPoint ?
promote_type(eltype(a), typeof(b)) : Float64 false
promote_type(eltype(a), typeof(b)) : Float64 false
@swappable Base.(:./){T,N}(::NAtype, b::AbstractArray{T,N}) =
DataArray(Array(T, size(b)), trues(size(b)))
@dataarray_binary_scalar Base.(:./) Base.(:/) eltype(a) <: FloatingPoint || typeof(b) <: FloatingPoint ?
Expand Down
18 changes: 10 additions & 8 deletions src/pooleddataarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type PooledDataArray{T, R<:Integer, N} <: AbstractDataArray{T, N}
p::Vector{T})
# refs mustn't overflow pool
if length(rs.a) > 0 && maximum(rs.a) > prod(size(p))
error("Reference array points beyond the end of the pool")
throw(ArgumentError("Reference array points beyond the end of the pool"))
end
new(rs.a,p)
end
Expand Down Expand Up @@ -67,7 +67,7 @@ function PooledDataArray{T,R<:Integer,N}(d::AbstractArray{T, N},
m::AbstractArray{Bool, N},
r::Type{R} = DEFAULT_POOLED_REF_TYPE)
if length(pool) > typemax(R)
error("Cannot construct a PooledDataVector with type $R with a pool of size $(length(pool))")
throw(ArgumentError("Cannot construct a PooledDataVector with type $R with a pool of size $(length(pool))"))
end

newrefs = Array(R, size(d))
Expand Down Expand Up @@ -474,9 +474,11 @@ function getpoolidx{T,R<:Union(Uint8, Uint16, Int8, Int16)}(pda::PooledDataArray
push!(pda.pool, val)
pool_idx = length(pda.pool)
if pool_idx > typemax(R)
error("You're using a PooledDataArray with ref type $R, which can only hold $(int(typemax(R))) values,\n",
"and you just tried to add the $(typemax(R)+1)th reference. Please change the ref type\n",
"to a larger int type, or use the default ref type ($DEFAULT_POOLED_REF_TYPE).")
throw(ErrorException(
"You're using a PooledDataArray with ref type $R, which can only hold $(int(typemax(R))) values,\n",
"and you just tried to add the $(typemax(R)+1)th reference. Please change the ref type\n",
"to a larger int type, or use the default ref type ($DEFAULT_POOLED_REF_TYPE)."
))
end
end
return pool_idx
Expand Down Expand Up @@ -529,7 +531,7 @@ end
function replace!{S, T}(x::PooledDataArray{S}, fromval::T, toval::NAtype)
fromidx = findfirst(x.pool, fromval)
if fromidx == 0
error("can't replace a value not in the pool in a PooledDataVector!")
throw(ErrorException("can't replace a value not in the pool in a PooledDataVector!"))
end

x.refs[x.refs .== fromidx] = 0
Expand All @@ -553,7 +555,7 @@ function replace!{R, S, T}(x::PooledDataArray{R}, fromval::S, toval::T)
# throw error if fromval isn't in the pool
fromidx = findfirst(x.pool, fromval)
if fromidx == 0
error("can't replace a value not in the pool in a PooledDataArray!")
throw(ErrorException("can't replace a value not in the pool in a PooledDataArray!"))
end

# if toval is in the pool too, use that and remove fromval from the pool
Expand Down Expand Up @@ -767,7 +769,7 @@ function array{T, R}(da::PooledDataArray{T, R})
res = Array(T, size(da))
for i in 1:n
if da.refs[i] == zero(R)
error(NAException())
throw(NAException())
else
res[i] = da.pool[da.refs[i]]
end
Expand Down
17 changes: 9 additions & 8 deletions src/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function mapreduce_seq_impl_skipna(f, op, T, A::DataArray, ifirst::Int, ilast::I
data = A.data
na = A.na
chunks = na.chunks

v, i = skipna_init(f, op, na, data, ifirst, ilast)

while i < ilast
Expand Down Expand Up @@ -64,7 +64,7 @@ end

mapreduce_impl_skipna{T}(f, op, A::DataArray{T}) =
mapreduce_seq_impl_skipna(f, op, T, A, 1, length(A.data))
mapreduce_impl_skipna(f, op::Base.AddFun, A::DataArray) =
mapreduce_impl_skipna(f, op::Base.AddFun, A::DataArray) =
mapreduce_pairwise_impl_skipna(f, op, A, 1, length(A.na.chunks),
length(A.na)-countnz(A.na),
max(128, Base.sum_pairwise_blocksize(f)))
Expand Down Expand Up @@ -144,9 +144,9 @@ function Base.varm{T}(A::DataArray{T}, m::Number; corrected::Bool=true, skipna::
nna == n && return convert(Base.momenttype(T), NaN)
nna == n-1 && return convert(Base.momenttype(T),
abs2(A.data[Base.findnextnot(na, 1)] - m)/(1 - int(corrected)))

/(nna == 0 ? Base.centralize_sumabs2(A.data, m, 1, n) :
mapreduce_impl_skipna(Base.CentralizedAbs2Fun(m), Base.AddFun(), A),
mapreduce_impl_skipna(Base.CentralizedAbs2Fun(m), Base.AddFun(), A),
n - nna - int(corrected))
else
any(A.na) && return NA
Expand All @@ -165,14 +165,15 @@ end
function Base.var(A::DataArray; corrected::Bool=true, mean=nothing, skipna::Bool=false)
mean == 0 ? Base.varzm(A; corrected=corrected, skipna=skipna) :
mean == nothing ? varm(A, Base.mean(A; skipna=skipna); corrected=corrected, skipna=skipna) :
isa(mean, Union(Number, NAtype)) ? varm(A, mean; corrected=corrected, skipna=skipna) :
error("Invalid value of mean.")
isa(mean, Union(Number, NAtype)) ?
varm(A, mean; corrected=corrected, skipna=skipna) :
throw(ErrorException("Invalid value of mean."))
end

Base.stdm(A::DataArray, m::Number; corrected::Bool=true, skipna::Bool=false) =
Base.stdm(A::DataArray, m::Number; corrected::Bool=true, skipna::Bool=false) =
sqrt(varm(A, m; corrected=corrected, skipna=skipna))

Base.std(A::DataArray; corrected::Bool=true, mean=nothing, skipna::Bool=false) =
Base.std(A::DataArray; corrected::Bool=true, mean=nothing, skipna::Bool=false) =
sqrt(var(A; corrected=corrected, mean=mean, skipna=skipna))

## weighted mean
Expand Down
Loading

0 comments on commit 3a8cad5

Please sign in to comment.