DimArrays
DimArrays are wrappers for other kinds of AbstractArray that add named dimension lookups.
Here we define a Matrix of Float64, and give it X and Y dimensions
using DimensionalData
A = rand(5, 10)
da = DimArray(A, (X, Y))We can access a value with the same dimension wrappers:
da[Y(1), X(2)]There are shortcuts for creating DimArray:
A = rand(5, 10)
DimArray(A, (X, Y))
DimArray(A, (X, Y); name=:DimArray, metadata=Dict())Constructing DimArray with arbitrary dimension names
For arbitrary names, we can use the Dim{:name} dims by using Symbols, and indexing with keywords:
da1 = DimArray(rand(5, 5), (:a, :b))and get a value, here another smaller DimArray:
da1[a=3, b=1:3]Dimensional Indexing
When used for indexing, dimension wrappers free us from knowing the order of our objects axes. These are the same:
da[X(2), Y(1)] == da[Y(1), X(2)]We also can use Tuples of dimensions, like CartesianIndex, but they don't have to be in order of consecutive axes.
da2 = rand(X(10), Y(7), Z(5))
da2[(X(3), Z(5))]We can index with Vector of Tuple{Vararg(Dimension}} like vectors of CartesianIndex. This will merge the dimensions in the tuples:
inds = [(X(3), Z(5)), (X(7), Z(4)), (X(8), Z(2))]
da2[inds]DimIndices can be used like CartesianIndices but again, without the constraint of consecutive dimensions or known order.
da2[DimIndices(dims(da2, (X, Z))), Y(3)]The Dimension indexing layer sits on top of regular indexing and can not be combined with it! Regular indexing specifies order, so doesn't mix well with our dimensions.
Mixing them will throw an error:
da1[X(3), 4]Begin End indexing
WARNING
In base julia the keywords begin and end can be used to index the first or last element of an array. But this doesn't work when named indexing is used. Instead you can use the types Begin and End.
da[X=Begin+1, Y=End]It also works in ranges, even with basic math:
da[X=Begin:Begin+1, Y=Begin+1:End-1]Indexing
Indexing AbstractDimArrays works with getindex, setindex! and view. The result is still an AbstracDimArray, unless using all single Int or Selectors that resolve to Int inside Dimension.
dims keywords
In many Julia functions like, size or sum, you can specify the dimension along which to perform the operation as an Int. It is also possible to do this using Dimension types with AbstractDimArray:
da5 = rand(X(3), Y(4), Ti(5))
sum(da5; dims=Ti)Dims keywords
Methods where dims, dim types, or Symbols can be used to indicate the array dimension:
size,axes,firstindex,lastindexcat,reverse,dropdimsreduce,mapreducesum,prod,maximum,minimummean,median,extrema,std,var,cor,covpermutedims,adjoint,transpose,Transposemapslices,eachslice
Performance
Indexing with Dimensions has no runtime cost. Let's benchmark it:
using BenchmarkTools
da4 = ones(X(3), Y(3))
@benchmark $da4[X(1), Y(2)]the same as accessing the parent array directly:
@benchmark parent($da4)[1, 2]