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

Commit 3a8cad5

Browse files
committed
Merge pull request #139 from JuliaStats/depr
Match Base syntax, remove type instability from gl
2 parents 359b9f3 + 5c1b361 commit 3a8cad5

15 files changed

+96
-82
lines changed

benchmark/operators.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module DataArraysBenchmark
2-
using DataArrays, Benchmark
2+
using DataArrays, Benchmark, Compat
33

44
# seed rng for more consistent timings
55
srand(1776)
@@ -26,7 +26,7 @@ function make_test_types(genfunc, sz)
2626
)
2727
end
2828

29-
make_bools{N}(x::NTuple{N}) = convert(Array{Bool, N}, randbool(x...))
29+
make_bools{N}(x::NTuple{N}) = convert(Array{Bool, N}, bitrand(x...))
3030
make_bools(x::Integer...) = make_bools(x)
3131

3232
macro perf(fn, replications, idx...)

src/broadcast.jl

+10-9
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ function check_broadcast_shape(shape::Dims, As::Union(AbstractArray,Number)...)
99
samesize = true
1010
for A in As
1111
if ndims(A) > length(shape)
12-
error("cannot broadcast array to have fewer dimensions")
12+
throw(DimensionMismatch("cannot broadcast array to have fewer dimensions"))
1313
end
1414
for k in 1:length(shape)
1515
n, nA = shape[k], size(A, k)
1616
samesize &= (n == nA)
1717
if n != nA != 1
18-
error("array could not be broadcast to match destination")
18+
throw(DimensionMismatch("array could not be broadcast to match destination"))
1919
end
2020
end
2121
end
@@ -183,9 +183,9 @@ for bsig in (DataArray, PooledDataArray), asig in (Union(Array,BitArray,Number),
183183
@eval let cache = Dict{Function,Dict{Uint64,Dict{Int,Function}}}()
184184
function Base.map!(f::Base.Callable, B::$bsig, As::$asig...)
185185
nd = ndims(B)
186-
length(As) <= 8 || error("too many arguments")
186+
length(As) <= 8 || throw(ArgumentError("too many arguments"))
187187
samesize = check_broadcast_shape(size(B), As...)
188-
samesize || error("dimensions must match")
188+
samesize || throw(DimensionMismatch("Argument dimensions must match"))
189189
arrtype = datype_int(As...)
190190

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

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

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

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

279-
# Definitions for operators,
280+
# Definitions for operators,
280281
Base.(:(.*))(A::BitArray, B::Union(DataArray{Bool}, PooledDataArray{Bool})) = databroadcast(*, A, B)
281282
Base.(:(.*))(A::Union(DataArray{Bool}, PooledDataArray{Bool}), B::BitArray) = databroadcast(*, A, B)
282283
@da_broadcast_vararg Base.(:(.*))(As...) = databroadcast(*, As...)

src/extras.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ cut(x::AbstractVector, ngroups::Integer) = cut(x, quantile(x, [1 : ngroups - 1]
6565

6666
function rep{T <: Integer}(x::AbstractVector, lengths::AbstractVector{T})
6767
if length(x) != length(lengths)
68-
error("vector lengths must match")
68+
throw(DimensionMismatch("vector lengths must match"))
6969
end
7070
res = similar(x, sum(lengths))
7171
i = 1

src/indexing.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,15 @@ end
8282

8383
# Indexing with NA throws an error
8484
function Base.to_index(A::DataArray)
85-
any(A.na) && error("cannot index an array with a DataArray containing NA values")
85+
any(A.na) && throw(NAException("cannot index an array with a DataArray containing NA values"))
8686
Base.to_index(A.data)
8787
end
8888

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

101101
# Fallbacks to avoid ambiguity
102102
setindex!(t::AbstractDataArray, x, i::Real) =
103-
error("setindex! not defined for ",typeof(t))
103+
throw(MethodError(setindex!, typeof(t), typeof(x), typeof(i)))
104104
getindex(t::AbstractDataArray, i::Real) =
105-
error("indexing not defined for ", typeof(t))
105+
throw(MethodError(getindex, typeof(t), typeof(i)))
106106

107107
## getindex: DataArray
108108

src/operators.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,9 @@ end
227227
macro swappable(func, syms...)
228228
if (func.head != :function && func.head != :(=)) ||
229229
func.args[1].head != :call || length(func.args[1].args) != 3
230-
error("@swappable may only be applied to functions of two arguments")
230+
throw(ArgumentError("@swappable may only be applied to functions of two arguments"))
231231
end
232-
232+
233233
func2 = deepcopy(func)
234234
fname = func2.args[1].args[1]
235235
if isa(fname, Expr) && fname.head == :curly
@@ -238,7 +238,7 @@ macro swappable(func, syms...)
238238

239239
for s in unique([fname, syms...])
240240
if swapargs(func2, s) < 1
241-
error("No argument swapped")
241+
throw(ErrorException("No argument swapped"))
242242
end
243243
end
244244
esc(Expr(:block, func, func2))
@@ -796,7 +796,7 @@ end
796796
Base.(:/){T,N}(b::AbstractArray{T,N}, ::NAtype) =
797797
DataArray(Array(T, size(b)), trues(size(b)))
798798
@dataarray_binary_scalar Base.(:/) Base.(:/) eltype(a) <: FloatingPoint || typeof(b) <: FloatingPoint ?
799-
promote_type(eltype(a), typeof(b)) : Float64 false
799+
promote_type(eltype(a), typeof(b)) : Float64 false
800800
@swappable Base.(:./){T,N}(::NAtype, b::AbstractArray{T,N}) =
801801
DataArray(Array(T, size(b)), trues(size(b)))
802802
@dataarray_binary_scalar Base.(:./) Base.(:/) eltype(a) <: FloatingPoint || typeof(b) <: FloatingPoint ?

src/pooleddataarray.jl

+10-8
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type PooledDataArray{T, R<:Integer, N} <: AbstractDataArray{T, N}
2727
p::Vector{T})
2828
# refs mustn't overflow pool
2929
if length(rs.a) > 0 && maximum(rs.a) > prod(size(p))
30-
error("Reference array points beyond the end of the pool")
30+
throw(ArgumentError("Reference array points beyond the end of the pool"))
3131
end
3232
new(rs.a,p)
3333
end
@@ -67,7 +67,7 @@ function PooledDataArray{T,R<:Integer,N}(d::AbstractArray{T, N},
6767
m::AbstractArray{Bool, N},
6868
r::Type{R} = DEFAULT_POOLED_REF_TYPE)
6969
if length(pool) > typemax(R)
70-
error("Cannot construct a PooledDataVector with type $R with a pool of size $(length(pool))")
70+
throw(ArgumentError("Cannot construct a PooledDataVector with type $R with a pool of size $(length(pool))"))
7171
end
7272

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

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

559561
# if toval is in the pool too, use that and remove fromval from the pool
@@ -767,7 +769,7 @@ function array{T, R}(da::PooledDataArray{T, R})
767769
res = Array(T, size(da))
768770
for i in 1:n
769771
if da.refs[i] == zero(R)
770-
error(NAException())
772+
throw(NAException())
771773
else
772774
res[i] = da.pool[da.refs[i]]
773775
end

src/reduce.jl

+9-8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function mapreduce_seq_impl_skipna(f, op, T, A::DataArray, ifirst::Int, ilast::I
2323
data = A.data
2424
na = A.na
2525
chunks = na.chunks
26-
26+
2727
v, i = skipna_init(f, op, na, data, ifirst, ilast)
2828

2929
while i < ilast
@@ -64,7 +64,7 @@ end
6464

6565
mapreduce_impl_skipna{T}(f, op, A::DataArray{T}) =
6666
mapreduce_seq_impl_skipna(f, op, T, A, 1, length(A.data))
67-
mapreduce_impl_skipna(f, op::Base.AddFun, A::DataArray) =
67+
mapreduce_impl_skipna(f, op::Base.AddFun, A::DataArray) =
6868
mapreduce_pairwise_impl_skipna(f, op, A, 1, length(A.na.chunks),
6969
length(A.na)-countnz(A.na),
7070
max(128, Base.sum_pairwise_blocksize(f)))
@@ -144,9 +144,9 @@ function Base.varm{T}(A::DataArray{T}, m::Number; corrected::Bool=true, skipna::
144144
nna == n && return convert(Base.momenttype(T), NaN)
145145
nna == n-1 && return convert(Base.momenttype(T),
146146
abs2(A.data[Base.findnextnot(na, 1)] - m)/(1 - int(corrected)))
147-
147+
148148
/(nna == 0 ? Base.centralize_sumabs2(A.data, m, 1, n) :
149-
mapreduce_impl_skipna(Base.CentralizedAbs2Fun(m), Base.AddFun(), A),
149+
mapreduce_impl_skipna(Base.CentralizedAbs2Fun(m), Base.AddFun(), A),
150150
n - nna - int(corrected))
151151
else
152152
any(A.na) && return NA
@@ -165,14 +165,15 @@ end
165165
function Base.var(A::DataArray; corrected::Bool=true, mean=nothing, skipna::Bool=false)
166166
mean == 0 ? Base.varzm(A; corrected=corrected, skipna=skipna) :
167167
mean == nothing ? varm(A, Base.mean(A; skipna=skipna); corrected=corrected, skipna=skipna) :
168-
isa(mean, Union(Number, NAtype)) ? varm(A, mean; corrected=corrected, skipna=skipna) :
169-
error("Invalid value of mean.")
168+
isa(mean, Union(Number, NAtype)) ?
169+
varm(A, mean; corrected=corrected, skipna=skipna) :
170+
throw(ErrorException("Invalid value of mean."))
170171
end
171172

172-
Base.stdm(A::DataArray, m::Number; corrected::Bool=true, skipna::Bool=false) =
173+
Base.stdm(A::DataArray, m::Number; corrected::Bool=true, skipna::Bool=false) =
173174
sqrt(varm(A, m; corrected=corrected, skipna=skipna))
174175

175-
Base.std(A::DataArray; corrected::Bool=true, mean=nothing, skipna::Bool=false) =
176+
Base.std(A::DataArray; corrected::Bool=true, mean=nothing, skipna::Bool=false) =
176177
sqrt(var(A; corrected=corrected, mean=mean, skipna=skipna))
177178

178179
## weighted mean

0 commit comments

Comments
 (0)