I’d like to apply a function to slices of a named array such that the function has access to the array index names as well as the slice contents.
The syntax below uses a KeyedArray and passes a NamedTuple ( slice, index1, index2 ...)
to the function. This is one possible syntax.
Maybe one of the other named array packages already supports this ?
function my_func( (; slice, I, X ) )
"$I $X $(slice(:a))"
end
K = KeyedArray( [1;2;;3;4;;;5;6;;7;8], A=[:a,:b], I=[:i,:j], X=[:x,:y] )
# proposed syntax
mapslices( my_func, K; dims=(:A) )
#would give the same result as
@tullio Z[i,x] := my_func( (; slice=K[:,i,x], I=K.I[i], X=K.X[x]) )
KeyedArray( Z, I=K.I, X=K.X)
I’ve also put this as an AxisKeys request