-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
KristofferC
committed
Mar 13, 2023
1 parent
197f0a0
commit cbf26b0
Showing
6 changed files
with
118 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module CategoricalArraysJSONExt | ||
|
||
if isdefined(Base, :get_extension) | ||
using CategoricalArrays | ||
using JSON | ||
else | ||
using ..CategoricalArrays | ||
using ..JSON | ||
end | ||
|
||
# JSON of CategoricalValue is JSON of the value it refers to | ||
JSON.lower(x::CategoricalValue) = JSON.lower(unwrap(x)) | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module CategoricalArraysRecipesBaseExt | ||
|
||
if isdefined(Base, :get_extension) | ||
using CategoricalArrays | ||
using RecipesBase | ||
else | ||
using ..CategoricalArrays | ||
using ..RecipesBase | ||
end | ||
|
||
RecipesBase.@recipe function f(::Type{T}, v::T) where T <: CategoricalValue | ||
level_strings = [map(string, levels(v)); missing] | ||
ticks --> eachindex(level_strings) | ||
v -> ismissing(v) ? length(level_strings) : Int(CategoricalArrays.refcode(v)), | ||
i -> level_strings[Int(i)] | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module CategoricalArraysSentinelArraysExt | ||
|
||
if isdefined(Base, :get_extension) | ||
using CategoricalArrays | ||
using SentinelArrays | ||
else | ||
using ..CategoricalArrays | ||
using ..SentinelArrays | ||
end | ||
|
||
Base.copyto!(dest::CategoricalArrays.CatArrOrSub{<:Any, 1}, src::SentinelArrays.ChainedVector) = | ||
copyto!(dest, 1, src, 1, length(src)) | ||
Base.copyto!(dest::CategoricalArrays.CatArrOrSub{<:Any, 1}, dstart::Union{Signed, Unsigned}, | ||
src::SentinelArrays.ChainedVector, sstart::Union{Signed, Unsigned}, | ||
n::Union{Signed, Unsigned}) = | ||
invoke(copyto!, Tuple{AbstractArray, Union{Signed, Unsigned}, | ||
SentinelArrays.ChainedVector, | ||
Union{Signed, Unsigned}, Union{Signed, Unsigned}}, | ||
dest, dstart, src, sstart, n) | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module CategoricalArraysStructTypesExt | ||
|
||
if isdefined(Base, :get_extension) | ||
using CategoricalArrays | ||
using StructTypes | ||
else | ||
using ..CategoricalArrays | ||
using ..StructTypes | ||
end | ||
|
||
# define appropriate handlers for JSON3 interface | ||
StructTypes.StructType(x::CategoricalValue) = StructTypes.StructType(unwrap(x)) | ||
StructTypes.StructType(::Type{<:CategoricalValue{T}}) where {T} = StructTypes.StructType(T) | ||
StructTypes.numbertype(::Type{<:CategoricalValue{T}}) where {T <: Number} = T | ||
StructTypes.construct(::Type{T}, x::CategoricalValue{T}) where {T} = T(unwrap(x)) | ||
|
||
# JSON3 writing/reading | ||
StructTypes.StructType(::Type{<:CategoricalVector}) = StructTypes.ArrayType() | ||
|
||
StructTypes.construct(::Type{<:CategoricalArray}, A::AbstractVector) = | ||
constructgeneral(A) | ||
StructTypes.construct(::Type{<:CategoricalArray}, A::Vector) = | ||
constructgeneral(A) | ||
|
||
function constructgeneral(A) | ||
if eltype(A) === Any | ||
# unlike `replace`, broadcast narrows the type, which allows us to return small | ||
# union eltypes (e.g. Union{String,Missing}) | ||
categorical(ifelse.(A .=== nothing, missing, A)) | ||
elseif eltype(A) >: Nothing | ||
categorical(replace(A, nothing=>missing)) | ||
else | ||
categorical(A) | ||
end | ||
end | ||
|
||
StructTypes.construct(::Type{<:CategoricalArray{Union{Missing, T}}}, | ||
A::AbstractVector) where {T} = | ||
CategoricalArray{Union{Missing, T}}(replace(A, nothing=>missing)) | ||
StructTypes.construct(::Type{<:CategoricalArray{Union{Missing, T}}}, | ||
A::Vector) where {T} = | ||
CategoricalArray{Union{Missing, T}}(replace(A, nothing=>missing)) | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters