ComponentArrays.jl icon indicating copy to clipboard operation
ComponentArrays.jl copied to clipboard

labels and valkeys does not work on Axis

Open baggepinnen opened this issue 3 years ago • 1 comments

I'm not sure if there is anything preventing this, but it would sometimes be convenient to be able to ask about the labels in an axis, similar to how it can be done for a ComponentArray.

julia> uax
Axis(u1 = 1, u2 = 2)

julia> labels(uax)
""

julia> valkeys(uax)
ERROR: MethodError: no method matching valkeys(::Axis{(u1 = 1, u2 = 2)})
Closest candidates are:
  valkeys(::ComponentVector{T, A, Axes} where {T, A, Axes}) at /home/fredrikb/.julia/packages/ComponentArrays/Xo6aP/src/componentarray.jl:313
Stacktrace:
 [1] top-level scope
   @ REPL[27]:1

If I try to indx into an Axis with integers, I get something, but I can't judge if it makes sense or not.

julia> uax[1]
ComponentIndex(1, NullAxis())

julia> uax[2]
ComponentIndex(2, NullAxis())

julia> uax[1:2]
ComponentIndex(1:2, FlatAxis())

I'm trying to implement indexing with symbols (https://github.com/jonniedie/ComponentArrays.jl/issues/88) and this issue came up.

baggepinnen avatar May 25 '21 08:05 baggepinnen

Yeah, that would make sense to add some of those methods for the AbstractAxis types. Although there should be a much nicer way to do what you're doing in #88 than using labels. I'll try to make that a little easier.

The ComponentIndex type is kinda weird. The original intention was to have something that could be used as an index on a normal array to build a ComponentArray in order to simplify some of the ComponentArray getindex and getproperty code. Unfortunately, getproperty for ComponentArrays is already right at the edge of what the compiler is willing to inline for constant propagation. When I tried implementing the ComponentIndex code the way I wanted to do it, it broke inference and made everything slow. So right now ComponentIndex is just a temporary store of information that gets unpacked for other uses. It could just as well be a Tuple of an index and and axis.

But it would still be nice to have ComponentIndex work the way it's supposed to, even if it's not being used for that internally. In fact, the only thing it's missing there is a getindex method like:

Base.getindex(A::AbstractArray, inds::ComponentIndex...) = ComponentArray(A[(i.idx for i in inds)...], Tuple(i.ax for i in inds))

So you could do things like:

julia> ca = ComponentArray(a=5, b=(a=3, b=[1,6]))
ComponentVector{Int64}(a = 5, b = (a = 3, b = [1, 6]))

julia> ax = only(getaxes(ca))
Axis(a = 1, b = ViewAxis(2:4, Axis(a = 1, b = 2:3)))

julia> A = getdata(ca)
4-element Vector{Int64}:
 5
 3
 1
 6

julia> A[ax[:b]]
ComponentVector{Int64}(a = 3, b = [1, 6])

jonniedie avatar May 25 '21 13:05 jonniedie