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.582666  0.82935   0.598403  0.328938  …  0.569156  0.854234   0.00926239
 0.293679  0.786313  0.350067  0.680822     0.577896  0.416138   0.180638
 0.914838  0.885253  0.452222  0.27411      0.907416  0.947461   0.806201
 0.268828  0.558925  0.759391  0.190774     0.796165  0.0477716  0.5443
 0.245303  0.478255  0.346341  0.28982      0.468661  0.762707   0.393728
julia
julia> da = DimArray(A, (X, Y))
╭──────────────────────────╮
5×10 DimArray{Float64,2}
├──────────────────── dims ┤
X, Y
└──────────────────────────┘
 0.582666  0.82935   0.598403  0.328938  …  0.569156  0.854234   0.00926239
 0.293679  0.786313  0.350067  0.680822     0.577896  0.416138   0.180638
 0.914838  0.885253  0.452222  0.27411      0.907416  0.947461   0.806201
 0.268828  0.558925  0.759391  0.190774     0.796165  0.0477716  0.5443
 0.245303  0.478255  0.346341  0.28982      0.468661  0.762707   0.393728

We can access a value with the same dimension wrappers:

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

There are shortcuts for creating DimArray:

julia
julia> A = rand(5, 10)
5×10 Matrix{Float64}:
 0.127257  0.467965   0.172311  0.611445  …  0.92847    0.659251  0.709437
 0.290194  0.872374   0.93614   0.733544     0.367746   0.110702  0.601275
 0.305641  0.960182   0.206195  0.137647     0.0190089  0.222601  0.417202
 0.61168   0.210077   0.59951   0.198084     0.0557249  0.422875  0.509836
 0.664625  0.0619426  0.701639  0.586779     0.578452   0.20382   0.292476
julia
julia> DimArray(A, (X, Y))
╭──────────────────────────╮
5×10 DimArray{Float64,2}
├──────────────────── dims ┤
X, Y
└──────────────────────────┘
 0.127257  0.467965   0.172311  0.611445  …  0.92847    0.659251  0.709437
 0.290194  0.872374   0.93614   0.733544     0.367746   0.110702  0.601275
 0.305641  0.960182   0.206195  0.137647     0.0190089  0.222601  0.417202
 0.61168   0.210077   0.59951   0.198084     0.0557249  0.422875  0.509836
 0.664625  0.0619426  0.701639  0.586779     0.578452   0.20382   0.292476
julia
julia> DimArray(A, (X, Y); name=:DimArray, metadata=Dict())
╭───────────────────────────────────╮
5×10 DimArray{Float64,2} DimArray
├───────────────────────────── dims ┤
X, Y
├───────────────────────── metadata ┤
  Dict{Any, Any}()
└───────────────────────────────────┘
 0.127257  0.467965   0.172311  0.611445  …  0.92847    0.659251  0.709437
 0.290194  0.872374   0.93614   0.733544     0.367746   0.110702  0.601275
 0.305641  0.960182   0.206195  0.137647     0.0190089  0.222601  0.417202
 0.61168   0.210077   0.59951   0.198084     0.0557249  0.422875  0.509836
 0.664625  0.0619426  0.701639  0.586779     0.578452   0.20382   0.292476

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.599289  0.655968  0.762274   0.251952   0.957384
 0.704591  0.802517  0.0563076  0.206281   0.484484
 0.956143  0.329458  0.0143932  0.874323   0.889301
 0.16335   0.58471   0.248112   0.240458   0.975941
 0.707336  0.237122  0.236539   0.0754743  0.782378

and get a value, here another smaller DimArray:

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

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.438999  0.298725   0.847177   0.933332  0.1479    0.6117    0.509323
 0.324104  0.406433   0.0100752  0.304813  0.666138  0.310247  0.589392
 0.951924  0.245023   0.660734   0.24207   0.401878  0.236145  0.703725
 0.171529  0.164992   0.451496   0.1059    0.407057  0.987555  0.394202
 0.932095  0.715377   0.0676558  0.156216  0.301366  0.484231  0.717405
 0.341978  0.290612   0.274154   0.421862  0.866183  0.624507  0.143115
 0.375355  0.494979   0.199039   0.125336  0.214243  0.629128  0.408817
 0.58124   0.0558473  0.623421   0.836224  0.850432  0.215729  0.90298
 0.97584   0.622184   0.0538019  0.435711  0.972289  0.953325  0.376128
 0.17437   0.470656   0.0437949  0.434851  0.472609  0.154344  0.747295
julia
julia> da2[(X(3), Z(5))]
╭───────────────────────────────╮
7-element DimArray{Float64,1}
├───────────────────────── dims ┤
Y
└───────────────────────────────┘
 0.258083
 0.842618
 0.13675
 0.80984
 0.0842365
 0.41902
 0.465023

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.258083   0.786462  0.284668
 0.842618   0.415464  0.119231
 0.13675    0.487447  0.48147
 0.80984    0.431369  0.722342
 0.0842365  0.767064  0.671889
 0.41902    0.530423  0.505655
 0.465023   0.786651  0.823699

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.847177   0.121024   0.571857  0.0866494  0.324926
 0.0100752  0.756835   0.838266  0.737703   0.168422
 0.660734   0.729773   0.437252  0.124344   0.13675
 0.451496   0.959956   0.513307  0.703538   0.328679
 0.0676558  0.801155   0.481414  0.243585   0.647211
 0.274154   0.701795   0.968459  0.88546    0.857296
 0.199039   0.0567322  0.677864  0.487447   0.0688657
 0.623421   0.48147    0.98437   0.891807   0.389942
 0.0538019  0.978594   0.921291  0.16242    0.109972
 0.0437949  0.901334   0.624416  0.562608   0.948816

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.599289  0.655968  0.762274   0.251952   0.957384
 0.704591  0.802517  0.0563076  0.206281   0.484484
 0.956143  0.329458  0.0143932  0.874323   0.889301
 0.16335   0.58471   0.248112   0.240458   0.975941
 0.707336  0.237122  0.236539   0.0754743  0.782378

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.599289  0.655968  0.762274   0.251952   0.957384
 0.704591  0.802517  0.0563076  0.206281   0.484484
 0.956143  0.329458  0.0143932  0.874323   0.889301
 0.16335   0.58471   0.248112   0.240458   0.975941
 0.707336  0.237122  0.236539   0.0754743  0.782378

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.713016  0.466517  0.292432   0.70346
 0.622361  0.58204   0.0627475  0.407465
 0.995004  0.460007  0.374062   0.81146
julia
julia> sum(da5; dims=Ti)
╭───────────────────────────╮
3×4×1 DimArray{Float64,3}
├───────────────────── dims ┤
X, Y, Ti
└───────────────────────────┘
[:, :, 1]
 2.26687  2.1645   1.87536  3.01729
 3.88533  2.25729  2.41896  2.05537
 3.14147  2.7673   2.00673  3.20827

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 ns15.128 ns GC (min … max): 0.00% … 0.00%
 Time  (median):     2.795 ns               GC (median):    0.00%
 Time  (mean ± σ):   2.804 ns ±  0.279 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 ns22.431 ns GC (min … max): 0.00% … 0.00%
 Time  (median):     2.796 ns               GC (median):    0.00%
 Time  (mean ± σ):   2.814 ns ±  0.312 ns GC (mean ± σ):  0.00% ± 0.00%

                                            

  2.78 ns        Histogram: frequency by time        2.83 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.