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> using DimensionalData
julia> A = rand(5, 10)5×10 Matrix{Float64}:
0.0275537 0.757268 0.447874 0.985231 … 0.959784 0.675362 0.653554
0.455273 0.258717 0.667238 0.751023 0.265133 0.147719 0.642599
0.333692 0.981656 0.650329 0.13193 0.861447 0.503406 0.890094
0.5207 0.877966 0.203675 0.667218 0.798997 0.924805 0.274086
0.757128 0.73999 0.0769601 0.0820618 0.124025 0.00201458 0.276811julia> da = DimArray(A, (X, Y))┌ 5×10 DimArray{Float64, 2} ┐
├───────────────────── dims ┤
↓ X, → Y
└───────────────────────────┘
0.0275537 0.757268 0.447874 0.985231 … 0.959784 0.675362 0.653554
0.455273 0.258717 0.667238 0.751023 0.265133 0.147719 0.642599
0.333692 0.981656 0.650329 0.13193 0.861447 0.503406 0.890094
0.5207 0.877966 0.203675 0.667218 0.798997 0.924805 0.274086
0.757128 0.73999 0.0769601 0.0820618 0.124025 0.00201458 0.276811We can access a value with the same dimension wrappers:
julia> da[Y(1), X(2)]0.45527307575873366There are shortcuts for creating DimArray:
julia> A = rand(5, 10)5×10 Matrix{Float64}:
0.546357 0.00618571 0.355605 … 0.790492 0.98348 0.881394
0.407207 0.245773 0.329038 0.807284 0.754703 0.422977
0.67539 0.979703 0.666189 0.231238 0.699601 0.759528
0.115811 0.000139574 0.90568 0.0765721 0.463484 0.00653598
0.120066 0.817239 0.643279 0.881771 0.77483 0.585542julia> DimArray(A, (X, Y))┌ 5×10 DimArray{Float64, 2} ┐
├───────────────────── dims ┤
↓ X, → Y
└───────────────────────────┘
0.546357 0.00618571 0.355605 … 0.790492 0.98348 0.881394
0.407207 0.245773 0.329038 0.807284 0.754703 0.422977
0.67539 0.979703 0.666189 0.231238 0.699601 0.759528
0.115811 0.000139574 0.90568 0.0765721 0.463484 0.00653598
0.120066 0.817239 0.643279 0.881771 0.77483 0.585542julia> DimArray(A, (X, Y); name=:DimArray, metadata=Dict())┌ 5×10 DimArray{Float64, 2} DimArray ┐
├────────────────────────────── dims ┤
↓ X, → Y
└────────────────────────────────────┘
0.546357 0.00618571 0.355605 … 0.790492 0.98348 0.881394
0.407207 0.245773 0.329038 0.807284 0.754703 0.422977
0.67539 0.979703 0.666189 0.231238 0.699601 0.759528
0.115811 0.000139574 0.90568 0.0765721 0.463484 0.00653598
0.120066 0.817239 0.643279 0.881771 0.77483 0.585542Constructing DimArray with arbitrary dimension names
For arbitrary names, we can use the Dim{:name} dims by using Symbols, and indexing with keywords:
julia> da1 = DimArray(rand(5, 5), (:a, :b))┌ 5×5 DimArray{Float64, 2} ┐
├──────────────────── dims ┤
↓ a, → b
└──────────────────────────┘
0.768325 0.912065 0.917543 0.0965083 0.997672
0.514177 0.948512 0.0621546 0.700218 0.228964
0.764613 0.657441 0.625257 0.366217 0.968494
0.760205 0.796812 0.545129 0.989571 0.552422
0.553558 0.280686 0.295319 0.925873 0.00263829and get a value, here another smaller DimArray:
julia> da1[a=3, b=1:3]┌ 3-element DimArray{Float64, 1} ┐
├────────────────────────── dims ┤
↓ b
└────────────────────────────────┘
0.764613
0.657441
0.625257Dimensional Indexing
When used for indexing, dimension wrappers free us from knowing the order of our objects axes. These are the same:
julia> da[X(2), Y(1)] == da[Y(1), X(2)]trueWe also can use Tuples of dimensions, like CartesianIndex, but they don't have to be in order of consecutive axes.
julia> da2 = rand(X(10), Y(7), Z(5))┌ 10×7×5 DimArray{Float64, 3} ┐
├─────────────────────── dims ┤
↓ X, → Y, ↗ Z
└─────────────────────────────┘
[:, :, 1]
0.0599255 0.813229 0.0779205 0.570192 0.750146 0.308174 0.687453
0.557369 0.44104 0.20412 0.630969 0.475747 0.570137 0.7805
0.104867 0.570254 0.740523 0.638266 0.0287126 0.87065 0.742596
0.176197 0.208384 0.319301 0.273986 0.299799 0.817882 0.800511
0.468066 0.250913 0.627267 0.385262 0.468184 0.917105 0.868049
0.247198 0.947741 0.592835 0.208542 0.106224 0.710958 0.991875
0.0558987 0.178902 0.968159 0.561365 0.605217 0.917951 0.0917002
0.389839 0.906883 0.834838 0.986207 0.27863 0.106973 0.376538
0.675467 0.438447 0.267014 0.704858 0.885491 0.437817 0.173624
0.347379 0.71194 0.940686 0.567383 0.0389849 0.657598 0.179898julia> da2[(X(3), Z(5))]┌ 7-element DimArray{Float64, 1} ┐
├────────────────────────── dims ┤
↓ Y
└────────────────────────────────┘
0.164379
0.419756
0.672293
0.193266
0.627471
0.19179
0.643556We can index with Vector of Tuple{Vararg(Dimension}} like vectors of CartesianIndex. This will merge the dimensions in the tuples:
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> 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.164379 0.938774 0.905906
0.419756 0.210603 0.308707
0.672293 0.16783 0.0775618
0.193266 0.173932 0.495488
0.627471 0.277093 0.255455
0.19179 0.712478 0.784027
0.643556 0.614556 0.121036DimIndices can be used like CartesianIndices but again, without the constraint of consecutive dimensions or known order.
julia> da2[DimIndices(dims(da2, (X, Z))), Y(3)]┌ 10×5 DimArray{Float64, 2} ┐
├───────────────────── dims ┤
↓ X, → Z
└───────────────────────────┘
0.0779205 0.968774 0.645481 0.525227 0.222905
0.20412 0.482303 0.162018 0.412453 0.707476
0.740523 0.174992 0.0664978 0.331021 0.672293
0.319301 0.992219 0.371413 0.0198902 0.776018
0.627267 0.81465 0.993419 0.457036 0.710733
0.592835 0.0370225 0.389081 0.840958 0.764747
0.968159 0.949525 0.73536 0.16783 0.0150942
0.834838 0.0775618 0.752568 0.518257 0.349141
0.267014 0.80327 0.649287 0.11373 0.456834
0.940686 0.415099 0.723872 0.205311 0.340186The 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> 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> da[X=Begin+1, Y=End]0.6425986993790781It also works in ranges, even with basic math:
julia> da[X=Begin:Begin+1, Y=Begin+1:End-1]┌ 2×8 DimArray{Float64, 2} ┐
├──────────────────── dims ┤
↓ X, → Y
└──────────────────────────┘
0.757268 0.447874 0.985231 0.451545 … 0.426413 0.959784 0.675362
0.258717 0.667238 0.751023 0.32134 0.269096 0.265133 0.147719Indexing
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> da5 = rand(X(3), Y(4), Ti(5))┌ 3×4×5 DimArray{Float64, 3} ┐
├────────────────────── dims ┤
↓ X, → Y, ↗ Ti
└────────────────────────────┘
[:, :, 1]
0.511078 0.712711 0.0458864 0.21149
0.503823 0.343493 0.106985 0.112836
0.503967 0.514714 0.724709 0.528669julia> sum(da5; dims=Ti)┌ 3×4×1 DimArray{Float64, 3} ┐
├────────────────────── dims ┤
↓ X, → Y, ↗ Ti
└────────────────────────────┘
[:, :, 1]
2.35514 2.47022 2.5103 2.62207
2.22915 2.60609 2.09106 1.99155
3.03697 2.31787 2.98577 2.94348Dims 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:
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.0julia> @benchmark $da4[X(1), Y(2)]BenchmarkTools.Trial: 10000 samples with 1000 evaluations per sample.
Range (min … max): 3.095 ns … 17.683 ns ┊ GC (min … max): 0.00% … 0.00%
Time (median): 3.106 ns ┊ GC (median): 0.00%
Time (mean ± σ): 3.135 ns ± 0.488 ns ┊ GC (mean ± σ): 0.00% ± 0.00%
█
▃▁▅▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▆▁▁█▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▂▁▃ ▂
3.1 ns Histogram: frequency by time 3.12 ns <
Memory estimate: 0 bytes, allocs estimate: 0.the same as accessing the parent array directly:
julia> @benchmark parent($da4)[1, 2]BenchmarkTools.Trial: 10000 samples with 1000 evaluations per sample.
Range (min … max): 3.396 ns … 31.007 ns ┊ GC (min … max): 0.00% … 0.00%
Time (median): 3.416 ns ┊ GC (median): 0.00%
Time (mean ± σ): 3.449 ns ± 0.624 ns ┊ GC (mean ± σ): 0.00% ± 0.00%
█ ▁ █ ▂
▂▁▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁█▁▁█▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁█▁█ ▂
3.4 ns Histogram: frequency by time 3.42 ns <
Memory estimate: 0 bytes, allocs estimate: 0.