Skip to content

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

julia
julia> using DimensionalData

julia> A = rand(5, 10)
5×10 Matrix{Float64}:
 0.67178   0.760205  0.796812   0.545129  …  0.661715  0.321923   0.0697249
 0.677207  0.553558  0.280686   0.054613     0.557524  0.342437   0.392848
 0.768325  0.912065  0.917543   0.8234       0.874037  0.847981   0.625296
 0.514177  0.413756  0.0621546  0.700218     0.15388   0.912676   0.222681
 0.764613  0.7826    0.625257   0.366217     0.344408  0.0140644  0.870094
julia
julia> da = DimArray(A, (X, Y))
5×10 DimArray{Float64, 2}
├───────────────────── dims ┤
X, Y
└───────────────────────────┘
 0.67178   0.760205  0.796812   0.545129  …  0.661715  0.321923   0.0697249
 0.677207  0.553558  0.280686   0.054613     0.557524  0.342437   0.392848
 0.768325  0.912065  0.917543   0.8234       0.874037  0.847981   0.625296
 0.514177  0.413756  0.0621546  0.700218     0.15388   0.912676   0.222681
 0.764613  0.7826    0.625257   0.366217     0.344408  0.0140644  0.870094

We can access a value with the same dimension wrappers:

julia
julia> da[Y(1), X(2)]
0.6772070772014024

There are shortcuts for creating DimArray:

julia
julia> A = rand(5, 10)
5×10 Matrix{Float64}:
 0.677974   0.468066   0.347379  0.250913  …  0.385262  0.52496    0.468184
 0.0599255  0.247198   0.813229  0.391395     0.208542  0.750146   0.106224
 0.557369   0.0558987  0.44104   0.178902     0.561365  0.475747   0.605217
 0.104867   0.497181   0.570254  0.906883     0.986207  0.0287126  0.108968
 0.176197   0.675467   0.208384  0.438447     0.704858  0.299799   0.540566
julia
julia> DimArray(A, (X, Y))
5×10 DimArray{Float64, 2}
├───────────────────── dims ┤
X, Y
└───────────────────────────┘
 0.677974   0.468066   0.347379  0.250913  …  0.385262  0.52496    0.468184
 0.0599255  0.247198   0.813229  0.391395     0.208542  0.750146   0.106224
 0.557369   0.0558987  0.44104   0.178902     0.561365  0.475747   0.605217
 0.104867   0.497181   0.570254  0.906883     0.986207  0.0287126  0.108968
 0.176197   0.675467   0.208384  0.438447     0.704858  0.299799   0.540566
julia
julia> DimArray(A, (X, Y); name=:DimArray, metadata=Dict())
5×10 DimArray{Float64, 2} DimArray
├────────────────────────────── dims ┤
X, Y
└────────────────────────────────────┘
 0.677974   0.468066   0.347379  0.250913  …  0.385262  0.52496    0.468184
 0.0599255  0.247198   0.813229  0.391395     0.208542  0.750146   0.106224
 0.557369   0.0558987  0.44104   0.178902     0.561365  0.475747   0.605217
 0.104867   0.497181   0.570254  0.906883     0.986207  0.0287126  0.108968
 0.176197   0.675467   0.208384  0.438447     0.704858  0.299799   0.540566

Constructing DimArray with arbitrary dimension names

For arbitrary names, we can use the Dim{:name} dims by using Symbols, and indexing with keywords:

julia
julia> da1 = DimArray(rand(5, 5), (:a, :b))
5×5 DimArray{Float64, 2}
├──────────────────── dims ┤
a, b
└──────────────────────────┘
 0.137436   0.206406  0.811947   0.444708  0.358625
 0.252633   0.94412   0.481184   0.286785  0.828395
 0.573147   0.917457  0.758865   0.127356  0.680352
 0.0760848  0.774452  0.0641884  0.303772  0.680225
 0.0992684  0.784295  0.970459   0.498764  0.530271

and get a value, here another smaller DimArray:

julia
julia> da1[a=3, b=1:3]
3-element DimArray{Float64, 1}
├────────────────────────── dims ┤
b
└────────────────────────────────┘
 0.573147
 0.917457
 0.758865

Dimensional Indexing

When used for indexing, dimension wrappers free us from knowing the order of our objects axes. These are the same:

julia
julia> da[X(2), Y(1)] == da[Y(1), X(2)]
true

We also can use Tuples of dimensions, like CartesianIndex, but they don't have to be in order of consecutive axes.

julia
julia> da2 = rand(X(10), Y(7), Z(5))
10×7×5 DimArray{Float64, 3}
├─────────────────────── dims ┤
X, Y, Z
└─────────────────────────────┘
[:, :, 1]
 0.0987741  0.959907  0.0726015  0.0442881  0.877156  0.725503   0.86641
 0.848841   0.877295  0.55884    0.896093   0.976255  0.249525   0.609585
 0.0851053  0.893793  0.858332   0.556985   0.857562  0.0105671  0.991037
 0.732574   0.337006  0.870536   0.542646   0.924924  0.241047   0.0889246
 0.385228   0.251766  0.292204   0.0326231  0.299078  0.771093   0.804936
 0.798934   0.146259  0.689557   0.0129614  0.217848  0.665549   0.919702
 0.829194   0.395143  0.0342701  0.0470287  0.752915  0.109204   0.649382
 0.972201   0.537364  0.475142   0.868227   0.302211  0.793706   0.777313
 0.453674   0.819533  0.481182   0.538717   0.821808  0.216046   0.554297
 0.199504   0.733034  0.578734   0.818616   0.210304  0.660868   0.404788
julia
julia> da2[(X(3), Z(5))]
7-element DimArray{Float64, 1}
├────────────────────────── dims ┤
Y
└────────────────────────────────┘
 0.639398
 0.144391
 0.370946
 0.974725
 0.664311
 0.805566
 0.359254

We can index with Vector of Tuple{Vararg(Dimension}} like vectors of CartesianIndex. This will merge the dimensions in the tuples:

julia
julia> inds = [(X(3), Z(5)), (X(7), Z(4)), (X(8), Z(2))]
3-element Vector{Tuple{X{Int64}, Z{Int64}}}:
 (X 3, Z 5)
 (X 7, Z 4)
 (X 8, Z 2)
julia
julia> da2[inds]
7×3 DimArray{Float64, 2}
├──────────────────────────┴─────────────────────────────────────── dims ┐
Y,
XZ MergedLookup{Tuple{Int64, Int64}} [(3, 5), …, (8, 2)]X, Z
└────────────────────────────────────────────────────────────────────────┘
  (3, 5)    (7, 4)    (8, 2)
 0.639398  0.559092  0.504242
 0.144391  0.241109  0.159094
 0.370946  0.190387  0.849392
 0.974725  0.668515  0.425368
 0.664311  0.236262  0.976043
 0.805566  0.965946  0.709747
 0.359254  0.299806  0.115367

DimIndices can be used like CartesianIndices but again, without the constraint of consecutive dimensions or known order.

julia
julia> da2[DimIndices(dims(da2, (X, Z))), Y(3)]
10×5 DimArray{Float64, 2}
├───────────────────── dims ┤
X, Z
└───────────────────────────┘
 0.0726015  0.189031   0.478776   0.636878  0.607268
 0.55884    0.242935   0.647999   0.823697  0.327547
 0.858332   0.507807   0.761782   0.750189  0.370946
 0.870536   0.65229    0.0977422  0.474751  0.852659
 0.292204   0.788363   0.129544   0.937845  0.373488
 0.689557   0.0563826  0.88885    0.58454   0.0073424
 0.0342701  0.11003    0.160568   0.190387  0.598658
 0.475142   0.849392   0.103129   0.834542  0.566304
 0.481182   0.126864   0.135832   0.898508  0.302643
 0.578734   0.669608   0.569461   0.698929  0.141916

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:

julia
julia> da1[X(3), 4]
ERROR: ArgumentError: invalid index: X{Int64}(3) of type X{Int64}

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.

julia
julia> da[X=Begin+1, Y=End]
0.39284805878379914

It also works in ranges, even with basic math:

julia
julia> da[X=Begin:Begin+1, Y=Begin+1:End-1]
2×8 DimArray{Float64, 2}
├──────────────────── dims ┤
X, Y
└──────────────────────────┘
 0.760205  0.796812  0.545129  0.989571  …  0.3037   0.661715  0.321923
 0.553558  0.280686  0.054613  0.925873     0.36706  0.557524  0.342437

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:

julia
julia> da5 = rand(X(3), Y(4), Ti(5))
3×4×5 DimArray{Float64, 3}
├────────────────────── dims ┤
X, Y, Ti
└────────────────────────────┘
[:, :, 1]
 0.888027  0.537621  0.507171  0.519136
 0.906864  0.220951  0.207805  0.0766932
 0.91711   0.537011  0.95494   0.877763
julia
julia> sum(da5; dims=Ti)
3×4×1 DimArray{Float64, 3}
├────────────────────── dims ┤
X, Y, Ti
└────────────────────────────┘
[:, :, 1]
 3.00706  1.78108  1.45863  2.91389
 2.09261  2.65699  2.40165  1.2761
 2.09856  1.04     2.83859  3.14096

Dims keywords

Methods where dims, dim types, or Symbols can be used to indicate the array dimension:

  • size, axes, firstindex, lastindex

  • cat, reverse, dropdims

  • reduce, mapreduce

  • sum, prod, maximum, minimum

  • mean, median, extrema, std, var, cor, cov

  • permutedims, adjoint, transpose, Transpose

  • mapslices, eachslice

Performance

Indexing with Dimensions has no runtime cost. Let's benchmark it:

julia
julia> using BenchmarkTools

julia> da4 = ones(X(3), Y(3))
3×3 DimArray{Float64, 2}
├──────────────────── dims ┤
X, Y
└──────────────────────────┘
 1.0  1.0  1.0
 1.0  1.0  1.0
 1.0  1.0  1.0
julia
julia> @benchmark $da4[X(1), Y(2)]
BenchmarkTools.Trial: 10000 samples with 1000 evaluations per sample.
 Range (minmax):  3.396 ns20.849 ns GC (min … max): 0.00% … 0.00%
 Time  (median):     3.406 ns               GC (median):    0.00%
 Time  (mean ± σ):   3.464 ns ±  0.601 ns GC (mean ± σ):  0.00% ± 0.00%

                                                          

  3.4 ns         Histogram: frequency by time        5.43 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

the same as accessing the parent array directly:

julia
julia> @benchmark parent($da4)[1, 2]
BenchmarkTools.Trial: 10000 samples with 1000 evaluations per sample.
 Range (minmax):  3.396 ns25.338 ns GC (min … max): 0.00% … 0.00%
 Time  (median):     3.407 ns               GC (median):    0.00%
 Time  (mean ± σ):   3.440 ns ±  0.528 ns GC (mean ± σ):  0.00% ± 0.00%

                            

  3.4 ns         Histogram: frequency by time        3.42 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.