Skip to content

Commit

Permalink
Add CategoricalArrays.jl extension (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliascarv committed Oct 10, 2023
1 parent 2cd49e3 commit 9f1ab66
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 deletions.
7 changes: 5 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ authors = ["Elias Carvalho <eliascarvdev@gmail.com> and contributors"]
version = "0.1.0"

[weakdeps]
CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
CoDa = "5900dafe-f573-5c72-b367-76665857777b"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
DynamicQuantities = "06fc5a27-2a28-4c7c-a15d-362465fb6821"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[extensions]
SciTypesCategoricalArraysExt = "CategoricalArrays"
SciTypesCoDaExt = "CoDa"
SciTypesUnitfulExt = "Unitful"
SciTypesDynamicQuantitiesExt = "DynamicQuantities"
SciTypesUnitfulExt = "Unitful"

[compat]
CategoricalArrays = "0.10"
CoDa = "1.0"
DynamicQuantities = "0.7"
Unitful = "1.17"
Expand Down
11 changes: 11 additions & 0 deletions ext/SciTypesCategoricalArraysExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module SciTypesCategoricalArraysExt

using SciTypes
using CategoricalArrays

SciTypes.scitype(::Type{<:CategoricalValue}) = SciTypes.Categorical
SciTypes.elscitype(::Type{<:CategoricalArray}) = SciTypes.Categorical

SciTypes.isordered(array::CategoricalArray) = isordered(array)

end
12 changes: 12 additions & 0 deletions src/SciTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ Convert the scientific type of elements of the iterable `itr` to `S`.
"""
coerce(::Type{S}, itr) where {S<:SciType} = map(x -> sciconvert(S, x), itr)

"""
isordered(itr)
Checks whether the categorical variables of the iterable `itr` are ordered.
"""
function isordered(itr)
if !(elscitype(itr) <: Categorical)
throw(ArgumentError("iterable elements are not categorical"))
end
false
end

#---------
# EXPORTS
#---------
Expand Down
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[deps]
CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
CoDa = "5900dafe-f573-5c72-b367-76665857777b"
DynamicQuantities = "06fc5a27-2a28-4c7c-a15d-362465fb6821"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
30 changes: 28 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SciTypes
using CategoricalArrays
import DynamicQuantities
import Unitful
using CoDa
Expand Down Expand Up @@ -92,6 +93,16 @@ using Test
@test elscitype(coerce(SciTypes.Continuous, (1, 2, 3))) <: SciTypes.Continuous
end

@testset "isordered" begin
@test !SciTypes.isordered([1, 2, 3])
@test !SciTypes.isordered((:a, :b, :c))
@test !SciTypes.isordered(['a', 'b', 'c'])
@test !SciTypes.isordered(("a", "b", "c"))

# throws
@test_throws ArgumentError SciTypes.isordered([1.0, 2.0, 3.0])
end

@testset "missing values" begin
@test scitype(Missing) <: SciTypes.Unknown
@test scitype(Union{Float64,Missing}) <: SciTypes.Continuous
Expand All @@ -105,9 +116,9 @@ using Test
@testset "CoDa" begin
c1 = Composition(a=0.2, b=0.8)
c2 = Composition(a=0.5, b=0.5)
@test scitype(Composition{2, (:a, :b)}) <: SciTypes.Compositional
@test scitype(Composition{2,(:a, :b)}) <: SciTypes.Compositional
@test scitype(c1) <: SciTypes.Compositional
@test elscitype(Vector{Composition{2, (:a, :b)}}) <: SciTypes.Compositional
@test elscitype(Vector{Composition{2,(:a, :b)}}) <: SciTypes.Compositional
@test elscitype([c1, c2]) <: SciTypes.Compositional
@test elscitype([c1, missing, c2]) <: SciTypes.Compositional
end
Expand Down Expand Up @@ -160,4 +171,19 @@ using Test
@test isequal(coerce(SciTypes.Continuous, [1 * ui, missing, 3 * ui]), [1.0 * uf, missing, 3.0 * uf])
@test elscitype(coerce(SciTypes.Continuous, [1 * ui, missing, 3 * ui])) <: SciTypes.Continuous
end

@testset "CategoricalArrays" begin
carr = categorical([1, 2, 3])
cval = first(carr)
CV = typeof(cval)
CA = typeof(carr)
@test scitype(CV) <: SciTypes.Categorical
@test scitype(cval) <: SciTypes.Categorical
@test elscitype(CA) <: SciTypes.Categorical
@test elscitype(carr) <: SciTypes.Categorical
@test elscitype(categorical([1, missing, 3])) <: SciTypes.Categorical
@test !SciTypes.isordered(carr)
carr = categorical([1, 3, 2], ordered=true)
@test SciTypes.isordered(carr)
end
end

0 comments on commit 9f1ab66

Please sign in to comment.