JuliaArrays / AxisArrays.jl

Performant arrays where each dimension can have a named axis with values
http://JuliaArrays.github.io/AxisArrays.jl/latest/
Other
200 stars 41 forks source link

Feature Request: Rational index should be interpreted categorically #181

Open fredcallaway opened 4 years ago

fredcallaway commented 4 years ago

Example:

julia> x = AxisArray(rand(3), foo=[1//2, 1//3, 1//4])
julia> x[foo=atvalue(1//2)]
ERROR: BoundsError: attempt to access 3-element Array{Rational{Int64},1} at index [TolValue(1//2, tol=0//1)]

Seems to me that this should work the same way it does for symbols.

mcabbott commented 4 years ago

This seems like a bug to me, since it has an explicit atvalue. Compare:

julia> z = AxisArray(rand(3), sy=[:a, :b, :c]);

julia> z[atvalue(:b)] # explicit
0.5205382494635875

julia> z[:b] # this can guess based on type
0.5205382494635875

julia> y = AxisArray(rand(3), bar=[10,20,30]);

julia> y[atvalue(20)] # explicit
0.6526959443703837

julia> y[20] # interpreted as an index
ERROR: BoundsError: attempt to access 3-element Array{Float64,1} at index [20]

Rewrites without the bug:

julia> using DimensionalData

julia> d = DimensionalArray(rand(3), Dim{:foo}([1//2, 1//3, 1//4]));

julia> d[Dim{:foo} <| At(1//3)]
0.8922925051403834

julia> using AxisKeys

julia> k = KeyedArray(rand(3), foo=[1//2, 1//3, 1//4]);

julia> k[foo = Key(1//3)]
0.8963527992711511

julia> k(foo = 1//3)
0.8963527992711511
fredcallaway commented 4 years ago

@mcabbott Ah the growing pains of a new language... AxisKeys looks great, will give that a shot.