-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
uncompact #41
uncompact #41
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -463,6 +463,21 @@ function compact{T, N}(A::CatArray{T, N}) | |
convert(arraytype(typeof(A)){T, N, R}, A) | ||
end | ||
|
||
""" | ||
uncompact(A::CategoricalArray) | ||
uncompact(A::NullableCategoricalArray) | ||
|
||
Return a copy of categorical array `A` using the default reference type. If `A` is using | ||
a small reference type (such as UInt8 or UInt16) the uncompact array will have room for | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Backticks around type names. Also, "uncompacted" would be better. |
||
more levels. | ||
|
||
Avoid using compact to avoid having to call uncompact. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "compact" should be a link, see how |
||
""" | ||
uncompact{T, N}(A::CatArray{T, N}) = | ||
convert(arraytype(typeof(A)){T, N, DefaultRefType}, A) | ||
uncompact{T}(P::CategoricalPool{T}) = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should go into pool.jl. But it is really needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not really, no. I'll remove it |
||
convert(CategoricalPool{T, DefaultRefType}, P) | ||
|
||
arraytype(A::CategoricalArray...) = CategoricalArray | ||
arraytype(A::CatArray...) = NullableCategoricalArray | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -182,5 +182,5 @@ ordered!(pool::CategoricalPool, ordered) = (pool.ordered = ordered; pool) | |
# LevelsException | ||
function Base.showerror{T, R}(io::IO, err::LevelsException{T, R}) | ||
levs = join(repr.(err.levels), ", ", " and ") | ||
print(io, "cannot store level(s) $levs since reference type $R can only hold $(typemax(R)) levels. Convert categorical array to a larger reference type to add more levels.") | ||
print(io, "cannot store level(s) $levs since reference type $R can only hold $(typemax(R)) levels. Convert categorical array to a larger reference type to add more levels (see uncompact).") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would simply say "Use the uncompact function to add more levels". |
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -239,41 +239,43 @@ for (CA, A) in ((CategoricalArray, Array), (NullableCategoricalArray, NullableAr | |
|
||
res = @test_throws LevelsException{Int, UInt8} CA{Int, 1, UInt8}(256:-1:1) | ||
@test res.value.levels == [1] | ||
@test sprint(showerror, res.value) == "cannot store level(s) 1 since reference type UInt8 can only hold 255 levels. Convert categorical array to a larger reference type to add more levels." | ||
@test sprint(showerror, res.value) == "cannot store level(s) 1 since reference type UInt8 can only hold 255 levels. Convert categorical array to a larger reference type to add more levels (see uncompact)." | ||
|
||
x = CA{Int, 1, UInt8}(254:-1:1) | ||
x[1] = 1000 | ||
res = @test_throws LevelsException{Int, UInt8} x[1] = 1001 | ||
@test res.value.levels == [1001] | ||
@test sprint(showerror, res.value) == "cannot store level(s) 1001 since reference type UInt8 can only hold 255 levels. Convert categorical array to a larger reference type to add more levels." | ||
@test sprint(showerror, res.value) == "cannot store level(s) 1001 since reference type UInt8 can only hold 255 levels. Convert categorical array to a larger reference type to add more levels (see uncompact)." | ||
@test x == A(vcat(1000, 253:-1:1)) | ||
@test levels(x) == vcat(1:254, 1000) | ||
|
||
x = CA{Int, 1, UInt8}(1:254) | ||
res = @test_throws LevelsException{Int, UInt8} x[1:2] = 1000:1001 | ||
@test res.value.levels == [1001] | ||
@test sprint(showerror, res.value) == "cannot store level(s) 1001 since reference type UInt8 can only hold 255 levels. Convert categorical array to a larger reference type to add more levels." | ||
@test sprint(showerror, res.value) == "cannot store level(s) 1001 since reference type UInt8 can only hold 255 levels. Convert categorical array to a larger reference type to add more levels (see uncompact)." | ||
@test x == A(vcat(1000, 2:254)) | ||
@test levels(x) == vcat(1:254, 1000) | ||
|
||
x = CA{Int, 1, UInt8}([1, 3, 256]) | ||
res = @test_throws LevelsException{Int, UInt8} levels!(x, collect(1:256)) | ||
@test res.value.levels == [255] | ||
@test sprint(showerror, res.value) == "cannot store level(s) 255 since reference type UInt8 can only hold 255 levels. Convert categorical array to a larger reference type to add more levels." | ||
@test sprint(showerror, res.value) == "cannot store level(s) 255 since reference type UInt8 can only hold 255 levels. Convert categorical array to a larger reference type to add more levels (see uncompact)." | ||
|
||
x = CA(30:2:131115) | ||
res = @test_throws LevelsException{Int, UInt16} CategoricalVector{Int, UInt16}(x) | ||
@test res.value.levels == collect(131100:2:131114) | ||
@test sprint(showerror, res.value) == "cannot store level(s) 131100, 131102, 131104, 131106, 131108, 131110, 131112 and 131114 since reference type UInt16 can only hold 65535 levels. Convert categorical array to a larger reference type to add more levels." | ||
@test sprint(showerror, res.value) == "cannot store level(s) 131100, 131102, 131104, 131106, 131108, 131110, 131112 and 131114 since reference type UInt16 can only hold 65535 levels. Convert categorical array to a larger reference type to add more levels (see uncompact)." | ||
|
||
x = CA{String, 1, UInt8}(string.(Char.(65:318))) | ||
res = @test_throws LevelsException{String, UInt8} levels!(x, vcat(levels(x), "az", "bz", "cz")) | ||
@test res.value.levels == ["bz", "cz"] | ||
@test sprint(showerror, res.value) == "cannot store level(s) \"bz\" and \"cz\" since reference type UInt8 can only hold 255 levels. Convert categorical array to a larger reference type to add more levels." | ||
@test sprint(showerror, res.value) == "cannot store level(s) \"bz\" and \"cz\" since reference type UInt8 can only hold 255 levels. Convert categorical array to a larger reference type to add more levels (see uncompact)." | ||
@test x == A(string.(Char.(65:318))) | ||
lev = copy(levels(x)) | ||
levels!(x, vcat(lev, "az")) | ||
@test levels(x) == vcat(lev, "az") | ||
x2 = uncompact(x) | ||
levels!(x2, vcat(levels(x2), "bz", "cz")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Put this test in a different block. You don't actually need to test calling |
||
end | ||
|
||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"default reference type ($DefaultRefType)" should work.