NamedArrays question / request

Using NamedAarrays.jl I can define an array as:

x  = NamedArray( zeros(2,3,4),
    (
        [:A, :B],
        [:X, :Y, :Z],
        1:4
    ),
        (:Dim1, :DimXYZ, :DimNum)
   )

I’d like to write it as:

  x  = NamedArray( value = 0, dim_struct = 
       (
           :Dim1    = [:A, :B],
           :DimXYZ  = [:X, :Y, :Z],
           :DimNum  = 1:4
       ))

Using a named tuple of arrays to represent the dimension structure and item names,
and have the system take the length of each dimension from the length of each array.

Similarly I can specify x[:A, :, :]
But it would by nice to write it as x.Dim1.A

Are there any packages that allow this?

There are definately some packages with nicer syntax for this - like DimensionalData.jl and AxisKeys.jl. But I don’t think they the getproperty “.” syntax! (?) It’s not very array-like, and it forces you to know the order of the indices.

In DimensionalData.jl you would just write

x  = DimArray(zeros(2,3,4), (dim1=[:A, :B], dim2=[:X, :Y, :Z], dimnum=1:4))
x[dim1=:A]
2 Likes

Thanks Raf. Much appreciated

Is there any way to avoid hard coding the numbers in the zeros function?
can do this but its not very elegant.

dims = ( A =[:A, :B], X=[:X, :Y] )

Data  = zeros( tuple( length.([dims...])... ) )

A    = KeyedArray( Data  ; dims... )

What is KeyedArray (is that in AxisKeys?) I use DimArray day-to-day, as in my example, as I’m the author of DimensionalData.jl…

so using an example I know:

d = (dim1=[:A, :B], dim2=[:X, :Y, :Z], dimnum=1:4)
A = zeros(map(length, d))
x  = DimArray(A, d)

yes, thought I’d try both packages. this bit is quite similar.
Thanks again. Need a … in the second line: zeros( map(length, d)… )

right, I didn’t test that time :slight_smile: