Skip to content
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

Improve array promotion rules #387

Merged
merged 2 commits into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions src/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -304,16 +304,12 @@ CategoricalMatrix(A::CategoricalArray{T, 2, R};

## Promotion methods

Base.promote_rule(::Type{<:CategoricalArray{S}},
::Type{<:CategoricalArray{T}}) where {S, T} =
CategoricalArray{cat_promote_type(S, T)}
Base.promote_rule(::Type{<:CategoricalArray{S, N}},
::Type{<:CategoricalArray{T, N}}) where {S, T, N} =
CategoricalArray{cat_promote_type(S, T), N}
Base.promote_rule(::Type{<:CategoricalArray{S, N, R1}},
::Type{<:CategoricalArray{T, N, R2}}) where
{S, T, N, R1<:Integer, R2<:Integer} =
CategoricalArray{cat_promote_type(S, T), N, promote_type(R1, R2)}
# Identical behavior to the Array method
# Needed to prevent promote_result from returning an Array
# Note that eltype returns Any if a type parameter is omitted
Base.promote_rule(x::Type{<:CategoricalArray},
y::Type{<:CategoricalArray}) =
Base.el_same(promote_type(eltype(x), eltype(y)), x, y)

## Conversion methods

Expand Down Expand Up @@ -348,6 +344,13 @@ convert(::Type{CategoricalMatrix}, A::CategoricalMatrix) = A
convert(::Type{CategoricalArray{T, N, R}}, A::AbstractArray{S, N}) where {S, T, N, R} =
_convert(CategoricalArray{T, N, R}, A)

convert(::Type{CategoricalArray{T, N, R, V, C, U}},
A::CategoricalArray{T, N, R, V, C, U}) where {T, N, R, V, C, U} = A
# V, C and U are not used since they are recomputed from T and R
convert(::Type{CategoricalArray{T, N, R, V, C, U}},
A::AbstractArray{S, N}) where {S, T, N, R, V, C, U} =
_convert(CategoricalArray{T, N, R}, A)

function _convert(::Type{CategoricalArray{T, N, R}}, A::AbstractArray{S, N};
levels::Union{AbstractVector, Nothing}=nothing) where {S, T, N, R}
check_supported_eltype(T, T)
Expand Down
60 changes: 23 additions & 37 deletions test/13_arraycommon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2225,53 +2225,39 @@ end
end

@testset "promotion" begin
@test promote_type(CategoricalVector{Int},
CategoricalVector{String}) ==
CategoricalVector{Union{Int, String}}
@test promote_type(CategoricalVector{Int, UInt32},
CategoricalVector{String, UInt32}) ==
CategoricalVector{Union{Int, String}, UInt32}
@test promote_type(CategoricalArray{Int, UInt32},
CategoricalArray{String, UInt32}) ==
CategoricalArray{Union{Int, String}, UInt32}
@test promote_type(CategoricalVector{Int, UInt32},
CategoricalMatrix{String, UInt32}) ==
CategoricalArray{Union{Int, String}}
@test promote_type(CategoricalVector{Int, UInt8},
CategoricalVector{String, UInt16}) ==
CategoricalVector{Union{Int, String}, UInt16}

@test promote_type(CategoricalVector{Int8},
CategoricalVector{Float64}) ==
CategoricalVector{Float64}
@test promote_type(CategoricalVector{Int8, UInt32},
CategoricalVector{Float64, UInt32}) ==
CategoricalVector{Float64, UInt32}
@test promote_type(CategoricalArray{Int8, UInt32},
CategoricalArray{Float64, UInt32}) ==
CategoricalArray{Float64, UInt32}
@test promote_type(CategoricalVector{Int8, UInt32},
CategoricalMatrix{Float64, UInt32}) ==
CategoricalArray{Float64}
@test promote_type(CategoricalVector{Int8, UInt8},
CategoricalVector{Float64, UInt16}) ==
CategoricalVector{Float64, UInt16}
Comment on lines -2228 to -2258
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately this PR isn't enough to get these two work. CategoricalVector{T, R} isn't a concrete type, and even if the element type of such arrays is completely determined by T and R, the type system doesn't know that, which makes promote_rule and el_same above fail to return a precise type. It is probably fixable, but that's not trivial either. So for now instead of changing these tests to use verbose CategoricalArray{T, R, N, V, C U} types, I rely on array tests below, which are more compact and equivalent.

What do you think? Maybe it's worth supporting promotion of short CategoricalArray{T, N, R} forms, if only to limit confusing users (who will never write all type parameters when checking things manually)?


@test [CategoricalVector([1, 2]),
CategoricalVector(["a", "b"])] isa
Vector{CategoricalVector{Union{Int, String}, UInt32}}
Vector{CategoricalVector{<:Any, UInt32, <:Any, <:Any, Union{}}}
@test [CategoricalVector([1, missing]),
CategoricalVector(["a", "b"])] isa
Vector{CategoricalVector{Union{Int, String, Missing}, UInt32}}
Vector{CategoricalVector{<:Any, UInt32}}
@test [CategoricalVector([1, missing]),
CategoricalVector([1, 2])] isa
Vector{CategoricalVector{Union{Missing, Int}, UInt32, Int,
CategoricalValue{Int, UInt32}, Missing}}
@test [CategoricalVector([1, missing]),
CategoricalVector(["a", missing])] isa
Vector{CategoricalVector{Union{Int, String, Missing}, UInt32}}
Vector{CategoricalVector{<:Any, UInt32, <:Any, <:Any, Missing}}
@test [CategoricalVector([Int8(1), missing]),
CategoricalVector([Int16(2)])] isa
Vector{CategoricalVector{Union{Int16, Missing}, UInt32}}
Vector{CategoricalVector{<:Any, UInt32}}
@test [CategoricalVector([1, 2]),
CategoricalMatrix(["a" "b"])] isa
Vector{CategoricalArray{Union{Int, String}}}
Vector{CategoricalArray{<:Any, <:Any, UInt32, <:Any, <:Any, Union{}}}
@test [CategoricalVector([1, 2]),
CategoricalMatrix([1 2])] isa
Vector{CategoricalArray{Int, <:Any, UInt32, Int,
CategoricalValue{Int, UInt32}, Union{}}}
@test [CategoricalVector([1, 2]),
CategoricalMatrix([1 missing])] isa
Vector{CategoricalArray{<:Any, <:Any, UInt32, Int,
CategoricalValue{Int, UInt32}}}
@test [categorical([1, 2], compress=true),
CategoricalVector([1, 2])] isa
Vector{CategoricalVector{Int, UInt32, Int, CategoricalValue{Int, UInt32}, Union{}}}
@test [categorical([1, 2], compress=true),
CategoricalVector(["a", "b"])] isa
Vector{CategoricalVector{<:Any, <:Integer, <:Any, <:Any, Union{}}}
end

end