Skip to content

Commit dee0c63

Browse files
authored
Merge pull request #116 from invenia/categorical-default
Use categorical indexing behaviour by default for unknown array eltypes
2 parents 8b54281 + b03ff59 commit dee0c63

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/core.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ axistrait(::Type{Axis{name,T}}) where {name,T} = axistrait(T)
539539
axistrait(::Type{T}) where {T<:AbstractVector} = _axistrait_el(eltype(T))
540540
_axistrait_el(::Type{<:Union{Number, Dates.AbstractTime}}) = Dimensional
541541
_axistrait_el(::Type{<:Union{Symbol, AbstractString}}) = Categorical
542-
_axistrait_el(::Type{T}) where {T} = Unsupported
542+
_axistrait_el(::Type{T}) where {T} = Categorical
543543

544544
checkaxis(ax::Axis) = checkaxis(ax.val)
545545
checkaxis(ax) = checkaxis(axistrait(ax), ax)

src/indexing.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ Base.start(::Value) = false
2626
Base.next(x::Value, state) = (x, true)
2727
Base.done(x::Value, state) = state
2828

29+
# Values have the indexing trait of their wrapped type
30+
_axistrait_el(::Type{<:Value{T}}) where {T} = _axistrait_el(T)
31+
2932
# How to show Value objects (e.g. in a BoundsError)
3033
Base.show(io::IO, v::TolValue) =
3134
print(io, string("TolValue(", v.val, ", tol=", v.tol, ")"))

test/indexing.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,13 @@ acc = zeros(Int, 4, 1, 2)
210210
Base.mapreducedim!(x->x>5, +, acc, A3)
211211
@test acc == reshape([1 3; 2 3; 2 3; 2 3], 4, 1, 2)
212212

213+
# Value axistraits
214+
@testset for typ in (IL.IntLike, Complex{Float32}, DateTime, String, Symbol, Int)
215+
@test AxisArrays.axistrait(Axis{:foo, Vector{AxisArrays.ExactValue{typ}}}) ===
216+
AxisArrays.axistrait(Axis{:foo, Vector{AxisArrays.TolValue{typ}}}) ===
217+
AxisArrays.axistrait(Axis{:bar, Vector{typ}})
218+
end
219+
213220
# Indexing by value using `atvalue`
214221
A = AxisArray([1 2; 3 4], Axis{:x}([1.0,4.0]), Axis{:y}([2.0,6.1]))
215222
@test @inferred(A[atvalue(1.0)]) == @inferred(A[atvalue(1.0), :]) == [1,2]
@@ -259,6 +266,16 @@ A = AxisArray([1 2; 3 4], Axis{:x}([:a, :b]), Axis{:y}(["c", "d"]))
259266
@test @inferred(A[Axis{:x}(atvalue(:b))]) == [3,4]
260267
@test @inferred(A[Axis{:y}(atvalue("d"))]) == [2,4]
261268

269+
# Index by mystery types categorically
270+
struct Foo
271+
x
272+
end
273+
A = AxisArray(1:10, Axis{:x}(map(Foo, 1:10)))
274+
@test A[map(Foo, 3:6)] == collect(3:6)
275+
@test_throws ArgumentError A[map(Foo, 3:11)]
276+
@test A[Foo(4)] == 4
277+
@test_throws ArgumentError A[Foo(0)]
278+
262279
# Test using dates
263280
using Base.Dates: Day, Month
264281
A = AxisArray(1:365, Date(2017,1,1):Date(2017,12,31))

0 commit comments

Comments
 (0)