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

Preserve dimensions in `stack`, `vcat`

Open jariji opened this issue 1 year ago • 2 comments

I want to preserve dimensions.

julia> let
           ka = KeyedArray(
               rand(4,3);
               a=[1,2,1,2],
               b=1:3,
           )
           stack([ka, ka])
       end
4×3×2 Array{Float64, 3}:
[:, :, 1] =
 0.157501  0.15453   0.236119
 0.62595   0.142494  0.503847
 0.646891  0.829133  0.323376
 0.487593  0.930134  0.338322

[:, :, 2] =
 0.157501  0.15453   0.236119
 0.62595   0.142494  0.503847
 0.646891  0.829133  0.323376
 0.487593  0.930134  0.338322

jariji avatar Dec 01 '24 02:12 jariji

stack should produce a KeyedArray when both the inner arrays and the outer container itself are KeyedArrays.

aplavin avatar Dec 01 '24 03:12 aplavin

To preserve them in more cases, this package could dispatch on stack(::AbstractArray{<:KeyedArray}) etc.

For the general case of stack(f, A, B) where f returns a KeyedArray, it's possible that overloading internal functions could catch this? Some of them see the iterator's eltype before proceeding. Maybe _typed_stack(::Colon, ::Type, ::Type{<:KeyedArray}, A, ...) & _dim_stack? But I haven't tried... and of course these are internal (so ideally would at least want to check they still exist before overloading).

mcabbott avatar Dec 01 '24 20:12 mcabbott