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.939804 0.708256 0.0708227 0.300989 … 0.752328 0.218814 0.110826
0.426853 0.345254 0.113567 0.380793 0.0192019 0.706319 0.225241
0.363856 0.911145 0.967062 0.805293 0.721264 0.529671 0.294644
0.136283 0.441675 0.745408 0.255154 0.829709 0.709679 0.298631
0.260868 0.129696 0.132641 0.0991051 0.350119 0.301844 0.833925julia> da = DimArray(A, (X, Y))╭──────────────────────────╮
│ 5×10 DimArray{Float64,2} │
├──────────────────── dims ┤
↓ X, → Y
└──────────────────────────┘
0.939804 0.708256 0.0708227 0.300989 … 0.752328 0.218814 0.110826
0.426853 0.345254 0.113567 0.380793 0.0192019 0.706319 0.225241
0.363856 0.911145 0.967062 0.805293 0.721264 0.529671 0.294644
0.136283 0.441675 0.745408 0.255154 0.829709 0.709679 0.298631
0.260868 0.129696 0.132641 0.0991051 0.350119 0.301844 0.833925We can access a value with the same dimension wrappers:
julia> da[Y(1), X(2)]0.4268531084428593There are shortcuts for creating DimArray:
julia> A = rand(5, 10)5×10 Matrix{Float64}:
0.0388844 0.323156 0.97572 0.330774 … 0.12724 0.279941 0.322042
0.284825 0.123626 0.219463 0.729223 0.866158 0.097571 0.290725
0.134688 0.853951 0.0103985 0.290724 0.846191 0.929669 0.334629
0.51006 0.344856 0.855964 0.794764 0.360023 0.731512 0.952122
0.241206 0.691694 0.626991 0.128295 0.552699 0.923862 0.647411julia> DimArray(A, (X, Y))╭──────────────────────────╮
│ 5×10 DimArray{Float64,2} │
├──────────────────── dims ┤
↓ X, → Y
└──────────────────────────┘
0.0388844 0.323156 0.97572 0.330774 … 0.12724 0.279941 0.322042
0.284825 0.123626 0.219463 0.729223 0.866158 0.097571 0.290725
0.134688 0.853951 0.0103985 0.290724 0.846191 0.929669 0.334629
0.51006 0.344856 0.855964 0.794764 0.360023 0.731512 0.952122
0.241206 0.691694 0.626991 0.128295 0.552699 0.923862 0.647411julia> DimArray(A, (X, Y); name=:DimArray, metadata=Dict())╭───────────────────────────────────╮
│ 5×10 DimArray{Float64,2} DimArray │
├───────────────────────────── dims ┤
↓ X, → Y
├───────────────────────── metadata ┤
Dict{Any, Any}()
└───────────────────────────────────┘
0.0388844 0.323156 0.97572 0.330774 … 0.12724 0.279941 0.322042
0.284825 0.123626 0.219463 0.729223 0.866158 0.097571 0.290725
0.134688 0.853951 0.0103985 0.290724 0.846191 0.929669 0.334629
0.51006 0.344856 0.855964 0.794764 0.360023 0.731512 0.952122
0.241206 0.691694 0.626991 0.128295 0.552699 0.923862 0.647411Constructing 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.296208 0.168796 0.183537 0.0473468 0.596398
0.664242 0.22805 0.692487 0.793677 0.0114994
0.370578 0.705501 0.995503 0.917671 0.503338
0.827661 0.143531 0.539677 0.7592 0.804455
0.411452 0.44614 0.819224 0.173487 0.390066and get a value, here another smaller DimArray:
julia> da1[a=3, b=1:3]╭───────────────────────────────╮
│ 3-element DimArray{Float64,1} │
├───────────────────────── dims ┤
↓ b
└───────────────────────────────┘
0.370578
0.705501
0.995503Dimensional 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.429998 0.0633643 0.87766 0.121871 0.965851 0.704423 0.543174
0.567078 0.791289 0.577309 0.259413 0.109471 0.315563 0.570447
0.665083 0.380129 0.931924 0.378576 0.223156 0.573515 0.632107
0.31723 0.119036 0.622428 0.912035 0.328054 0.985047 0.400428
0.605211 0.343329 0.885206 0.134366 0.135082 0.473268 0.16321
0.515025 0.423069 0.0391422 0.915837 0.60883 0.38521 0.128261
0.126145 0.999199 0.223933 0.572838 0.0980478 0.318691 0.872674
0.919621 0.0374407 0.947019 0.171 0.726356 0.596024 0.237229
0.526648 0.411383 0.75912 0.238931 0.240174 0.942989 0.775896
0.61813 0.741288 0.863301 0.0130037 0.386816 0.566516 0.377755julia> da2[(X(3), Z(5))]╭───────────────────────────────╮
│ 7-element DimArray{Float64,1} │
├───────────────────────── dims ┤
↓ Y
└───────────────────────────────┘
0.86377
0.230177
0.517938
0.815815
0.0940857
0.189053
0.19857We 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 2julia> 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.86377 0.531293 0.23022
0.230177 0.135265 0.799907
0.517938 0.885725 0.216153
0.815815 0.0746626 0.912584
0.0940857 0.594563 0.269461
0.189053 0.558989 0.780627
0.19857 0.579017 0.491913DimIndices 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.87766 0.256516 0.529993 0.950096 0.70021
0.577309 0.802101 0.650197 0.77497 0.428174
0.931924 0.310251 0.250632 0.828465 0.517938
0.622428 0.993128 0.454266 0.0480755 0.532147
0.885206 0.56534 0.0473475 0.415839 0.52137
0.0391422 0.250806 0.919836 0.660243 0.212883
0.223933 0.032736 0.259537 0.885725 0.824207
0.947019 0.216153 0.0562666 0.797087 0.809778
0.75912 0.965238 0.478949 0.59942 0.621163
0.863301 0.644399 0.435852 0.449365 0.970146The 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
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:775
╭─────────────────────────╮
│ 5×5 DimArray{Float64,2} │
├─────────────────── dims ┤
↓ a, → b
└─────────────────────────┘
0.296208 0.168796 0.183537 0.0473468 0.596398
0.664242 0.22805 0.692487 0.793677 0.0114994
0.370578 0.705501 0.995503 0.917671 0.503338
0.827661 0.143531 0.539677 0.7592 0.804455
0.411452 0.44614 0.819224 0.173487 0.390066It also works in ranges, even with basic math:
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:775
╭─────────────────────────╮
│ 5×5 DimArray{Float64,2} │
├─────────────────── dims ┤
↓ a, → b
└─────────────────────────┘
0.296208 0.168796 0.183537 0.0473468 0.596398
0.664242 0.22805 0.692487 0.793677 0.0114994
0.370578 0.705501 0.995503 0.917671 0.503338
0.827661 0.143531 0.539677 0.7592 0.804455
0.411452 0.44614 0.819224 0.173487 0.390066In 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> da5 = rand(X(3), Y(4), Ti(5))╭───────────────────────────╮
│ 3×4×5 DimArray{Float64,3} │
├───────────────────── dims ┤
↓ X, → Y, ↗ Ti
└───────────────────────────┘
[:, :, 1]
0.692769 0.162341 0.631598 0.584558
0.60419 0.415005 0.0776921 0.462316
0.859108 0.213167 0.112591 0.477844julia> sum(da5; dims=Ti)╭───────────────────────────╮
│ 3×4×1 DimArray{Float64,3} │
├───────────────────── dims ┤
↓ X, → Y, ↗ Ti
└───────────────────────────┘
[:, :, 1]
2.35361 2.822 2.33995 2.46607
2.85701 2.54995 2.70962 2.53516
1.90523 1.84341 2.13907 1.77548Dims 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. Lets 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.
Range (min … max): 3.095 ns … 16.130 ns ┊ GC (min … max): 0.00% … 0.00%
Time (median): 3.105 ns ┊ GC (median): 0.00%
Time (mean ± σ): 3.112 ns ± 0.270 ns ┊ GC (mean ± σ): 0.00% ± 0.00%
▅▇ ▆ █ ▂ ▂ ▂
██▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁█▁█▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁█▁█▁▄▁▁▁▁▁▁▁▁▁▁▁▁▁▁██ █
3.1 ns Histogram: log(frequency) by time 3.13 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.
Range (min … max): 3.095 ns … 20.598 ns ┊ GC (min … max): 0.00% … 0.00%
Time (median): 3.105 ns ┊ GC (median): 0.00%
Time (mean ± σ): 3.115 ns ± 0.362 ns ┊ GC (mean ± σ): 0.00% ± 0.00%
▄ █
▅█▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▅▁█▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▂▁▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▂▂ ▂
3.1 ns Histogram: frequency by time 3.13 ns <
Memory estimate: 0 bytes, allocs estimate: 0.