Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
nalimilan committed Jan 22, 2017
1 parent e18c27f commit 781a709
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/array.jl
@@ -1,7 +1,7 @@
## Common code for CategoricalArray and NullableCategoricalArray

import Base: convert, copy, copy!, getindex, setindex!, similar, size,
linearindexing, unique, vcat
linearindexing, unique, vcat, repeat

# Used for keyword argument default value
_isordered(x::AbstractCategoricalArray) = isordered(x)
Expand Down Expand Up @@ -493,6 +493,12 @@ function vcat(A::CatArray...)
T(DefaultRefType[refs...;], CategoricalPool(newlevels, ordered))
end

repeat{T, R, N}(A::CatArray{T, R, N};
inner = ntuple(x -> 1, ndims(A)),
outer = ntuple(x -> 1, ndims(A))) =
arraytype(typeof(A)){T, N, R}(repeat(A.refs; inner=inner, outer=outer), deepcopy(A.pool))


## Categorical-specific methods

@inline function getindex(A::CatArray, I...)
Expand Down
24 changes: 24 additions & 0 deletions src/extras.jl
@@ -1,3 +1,26 @@
import StatsBase

function StatsBase.addcounts!{T}(cm::Dict{T,Int}, x::CatArray{T})
for v in x
cm[v] = get(cm, v, 0) + 1
end
return cm
end

function StatsBase.addcounts!{T,W}(cm::Dict{T,W}, x::CatArray{T}, wv::StatsBase.WeightVec{W})
n = length(x)
length(wv) == n || raise_dimerror()
w = values(wv)
z = zero(W)

@inbounds for i in 1:n
xi = x[i]
wi = w[i]
cm[xi] = get(cm, xi, z) + wi
end
return cm
end

function fill_refs!(refs::AbstractVector, X::AbstractArray,
breaks::AbstractVector, extend::Bool, nullok::Bool)
n = length(breaks)
Expand Down Expand Up @@ -136,6 +159,7 @@ function cut{T, N, U}(x::AbstractArray{T, N}, breaks::AbstractVector;
end

"""
cut(x::AbstractArray, ngroups::Integer)
Cut a numeric array into `ngroups` quantiles using `quantile`.
Expand Down
9 changes: 9 additions & 0 deletions test/15_extras.jl
Expand Up @@ -7,6 +7,15 @@ using Compat
# == currently throws an error for Nullables
const == = isequal

# Test countmap

d = @data [NA,3,3]
w = weights([1.1,2.2,3.3])
cm = Dict{(@compat Union{Int, NAtype}), Int}([(NA, 1), (3, 2)])
cmw = Dict{(@compat Union{Int, NAtype}), Real}([(NA, 1.1), (3, 5.5)])
@test isequal(countmap(d), cm)
@test isequal(countmap(d, w), cmw)

# Test cut

for (A, CA) in zip((Array, NullableArray, Array{Nullable}),
Expand Down

0 comments on commit 781a709

Please sign in to comment.