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.534915   0.4518     0.194465  0.780132  …  0.476495   0.0239356  0.526295
 0.707692   0.454886   0.588506  0.168291     0.222482   0.493417   0.134246
 0.0999314  0.0425477  0.44119   0.410463     0.0400372  0.0622255  0.350104
 0.132894   0.865298   0.778166  0.723438     0.0740736  0.137619   0.202743
 0.966106   0.854076   0.895608  0.147478     0.815365   0.24737    0.860933
julia
julia> da = DimArray(A, (X, Y))
╭──────────────────────────╮
5×10 DimArray{Float64,2}
├──────────────────── dims ┤
X, Y
└──────────────────────────┘
 0.534915   0.4518     0.194465  0.780132  …  0.476495   0.0239356  0.526295
 0.707692   0.454886   0.588506  0.168291     0.222482   0.493417   0.134246
 0.0999314  0.0425477  0.44119   0.410463     0.0400372  0.0622255  0.350104
 0.132894   0.865298   0.778166  0.723438     0.0740736  0.137619   0.202743
 0.966106   0.854076   0.895608  0.147478     0.815365   0.24737    0.860933

We can access a value with the same dimension wrappers:

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

There are shortcuts for creating DimArray:

julia
julia> A = rand(5, 10)
5×10 Matrix{Float64}:
 0.764613  0.657441   0.625257   0.587556  …  0.344408  0.0140644  0.0127924
 0.760205  0.298179   0.545129   0.989571     0.554791  0.0697249  0.949105
 0.553558  0.500157   0.295319   0.925873     0.386433  0.392848   0.26069
 0.912065  0.917543   0.0965083  0.997672     0.492141  0.625296   0.391155
 0.948512  0.0621546  0.167745   0.228964     0.90251   0.710967   0.885914
julia
julia> DimArray(A, (X, Y))
╭──────────────────────────╮
5×10 DimArray{Float64,2}
├──────────────────── dims ┤
X, Y
└──────────────────────────┘
 0.764613  0.657441   0.625257   0.587556  …  0.344408  0.0140644  0.0127924
 0.760205  0.298179   0.545129   0.989571     0.554791  0.0697249  0.949105
 0.553558  0.500157   0.295319   0.925873     0.386433  0.392848   0.26069
 0.912065  0.917543   0.0965083  0.997672     0.492141  0.625296   0.391155
 0.948512  0.0621546  0.167745   0.228964     0.90251   0.710967   0.885914
julia
julia> DimArray(A, (X, Y); name=:DimArray, metadata=Dict())
╭───────────────────────────────────╮
5×10 DimArray{Float64,2} DimArray
├───────────────────────────── dims ┤
X, Y
├───────────────────────── metadata ┤
  Dict{Any, Any}()
└───────────────────────────────────┘
 0.764613  0.657441   0.625257   0.587556  …  0.344408  0.0140644  0.0127924
 0.760205  0.298179   0.545129   0.989571     0.554791  0.0697249  0.949105
 0.553558  0.500157   0.295319   0.925873     0.386433  0.392848   0.26069
 0.912065  0.917543   0.0965083  0.997672     0.492141  0.625296   0.391155
 0.948512  0.0621546  0.167745   0.228964     0.90251   0.710967   0.885914

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.88841    0.329515   0.620055  0.20051     0.500642
 0.284399   0.152765   0.465631  0.00337686  0.885929
 0.0463176  0.143704   0.965831  0.754468    0.0684698
 0.257822   0.411529   0.219583  0.6642      0.714286
 0.706267   0.0450386  0.441735  0.137131    0.350422

and get a value, here another smaller DimArray:

julia
julia> da1[a=3, b=1:3]
╭───────────────────────────────╮
3-element DimArray{Float64,1}
├───────────────────────── dims ┤
b
└───────────────────────────────┘
 0.0463176
 0.143704
 0.965831

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.0760848  0.0641884  0.680225  0.683696   0.0205228  0.563623  0.673966
 0.0992684  0.970459   0.330777  0.241056   0.0196808  0.669021  0.107475
 0.206406   0.444708   0.11996   0.322115   0.225865   0.802122  0.421692
 0.94412    0.0192911  0.351413  0.468968   0.269685   0.150126  0.215458
 0.917457   0.162553   0.184309  0.719495   0.221163   0.4231    0.721331
 0.916702   0.794163   0.880796  0.839618   0.380161   0.180894  0.375182
 0.48817    0.498764   0.904961  0.392377   0.625435   0.786147  0.769313
 0.339888   0.358625   0.290734  0.0778416  0.451425   0.879774  0.605324
 0.481184   0.828395   0.870276  0.0323182  0.195774   0.467484  0.255916
 0.758865   0.680352   0.679221  0.920736   0.0375522  0.296639  0.139067
julia
julia> da2[(X(3), Z(5))]
╭───────────────────────────────╮
7-element DimArray{Float64,1}
├───────────────────────── dims ┤
Y
└───────────────────────────────┘
 0.794017
 0.865233
 0.0540645
 0.0474302
 0.521021
 0.836107
 0.167104

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), (7, 4), (8, 2)]X, Z
└──────────────────────────────────────────────────────────────────────────┘
  (3, 5)     (7, 4)    (8, 2)
 0.794017   0.690695  0.89862
 0.865233   0.992316  0.864765
 0.0540645  0.409622  0.575698
 0.0474302  0.133195  0.161393
 0.521021   0.246162  0.337182
 0.836107   0.365341  0.394703
 0.167104   0.847018  0.213826

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.680225  0.457891   0.230047   0.634499  0.678022
 0.330777  0.0275306  0.352138   0.739241  0.353691
 0.11996   0.904604   0.708321   0.837335  0.0540645
 0.351413  0.853553   0.934609   0.208365  0.803293
 0.184309  0.91732    0.615764   0.526688  0.5573
 0.880796  0.683091   0.761212   0.956031  0.3162
 0.904961  0.589895   0.945371   0.409622  0.110998
 0.290734  0.575698   0.497346   0.750906  0.354046
 0.870276  0.320667   0.0559616  0.341835  0.234458
 0.679221  0.950645   0.727554   0.306208  0.173513

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

julia
julia> da1[X=Begin+1, Y=End]
Warning: (X, Y) dims were not found in object.
@ DimensionalData.Dimensions ~/work/DimensionalData.jl/DimensionalData.jl/src/Dimensions/primitives.jl:777
╭─────────────────────────╮
5×5 DimArray{Float64,2}
├─────────────────── dims ┤
a, b
└─────────────────────────┘
 0.88841    0.329515   0.620055  0.20051     0.500642
 0.284399   0.152765   0.465631  0.00337686  0.885929
 0.0463176  0.143704   0.965831  0.754468    0.0684698
 0.257822   0.411529   0.219583  0.6642      0.714286
 0.706267   0.0450386  0.441735  0.137131    0.350422

It also works in ranges, even with basic math:

julia
julia> da1[X=Begin:Begin+1, Y=Begin+1:End-1]
Warning: (X, Y) dims were not found in object.
@ DimensionalData.Dimensions ~/work/DimensionalData.jl/DimensionalData.jl/src/Dimensions/primitives.jl:777
╭─────────────────────────╮
5×5 DimArray{Float64,2}
├─────────────────── dims ┤
a, b
└─────────────────────────┘
 0.88841    0.329515   0.620055  0.20051     0.500642
 0.284399   0.152765   0.465631  0.00337686  0.885929
 0.0463176  0.143704   0.965831  0.754468    0.0684698
 0.257822   0.411529   0.219583  0.6642      0.714286
 0.706267   0.0450386  0.441735  0.137131    0.350422

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.

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.0610019  0.578989  0.760111  0.291577
 0.980256   0.388907  0.711714  0.827852
 0.225583   0.395594  0.433955  0.691887
julia
julia> sum(da5; dims=Ti)
╭───────────────────────────╮
3×4×1 DimArray{Float64,3}
├───────────────────── dims ┤
X, Y, Ti
└───────────────────────────┘
[:, :, 1]
 1.31572  3.28438  1.73011  2.13035
 2.5962   1.55745  2.5074   2.07151
 2.31468  1.40687  3.06474  3.2858

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.
 Range (minmax):  2.785 ns26.941 ns GC (min … max): 0.00% … 0.00%
 Time  (median):     2.795 ns               GC (median):    0.00%
 Time  (mean ± σ):   2.807 ns ±  0.362 ns GC (mean ± σ):  0.00% ± 0.00%

                                            

  2.78 ns        Histogram: frequency by time        2.82 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.
 Range (minmax):  2.785 ns23.123 ns GC (min … max): 0.00% … 0.00%
 Time  (median):     2.795 ns               GC (median):    0.00%
 Time  (mean ± σ):   2.805 ns ±  0.314 ns GC (mean ± σ):  0.00% ± 0.00%

                                            

  2.78 ns        Histogram: frequency by time        2.82 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.