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

Enable reduce_indices over AbstractArrays #194

Closed glennmoy closed 3 years ago

glennmoy commented 3 years ago

Somewhat closes #193 as it only accepts integer dims.

julia> A = AxisArray(collect(reshape(1:15,3,5)), :y, :x)
2-dimensional AxisArray{Int64,2,...} with axes:
    :y, Base.OneTo(3)
    :x, Base.OneTo(5)
And data, a 3×5 Array{Int64,2}:
 1  4  7  10  13
 2  5  8  11  14
 3  6  9  12  15

julia> mapslices(sum, A; dims=1)
1×5 Array{Int64,2}:
 6  15  24  33  42

I'm not fully sure what's needed to enable Axis dims, perhaps overloading Base.mapslices directly since it seems to assume the dims args are always integers (used for indexing).

julia> mapslices(sum, A; dims=Axis{:y})
ERROR: ArgumentError: invalid index: Axis{:y,T} where T of type UnionAll
Stacktrace:
 [1] to_index(::Type{T} where T) at ./indices.jl:297
 [2] to_index(::Array{Any,1}, ::Type{T} where T) at ./indices.jl:274
 [3] to_indices at ./indices.jl:325 [inlined]
 [4] to_indices at ./indices.jl:322 [inlined]
 [5] setindex!(::Array{Any,1}, ::Base.Slice{Base.OneTo{Int64}}, ::Type{T} where T) at ./abstractarray.jl:1153
 [6] mapslices(::typeof(sum), ::AxisArray{Int64,2,Array{Int64,2},Tuple{Axis{:y,Base.OneTo{Int64}},Axis{:x,Base.OneTo{Int64}}}}; dims::Type{T} where T) at ./abstractarray.jl:2078
 [7] top-level scope at REPL[203]:1
glennmoy commented 3 years ago

Yeah, I think you'll need to overload mapslices directly.

Closing in favour of https://github.com/JuliaArrays/AxisArrays.jl/pull/195