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.254966 0.0542736 0.174367 0.52276 … 0.257671 0.433249 0.37079
0.0196069 0.492355 0.0284599 0.681254 0.266973 0.841434 0.723008
0.585697 0.945226 0.82122 0.834784 0.622533 0.860144 0.430653
0.556947 0.342535 0.0125467 0.75126 0.636407 0.0268885 0.0670457
0.268822 0.0363362 0.53834 0.319352 0.153277 0.543613 0.783523julia> da = DimArray(A, (X, Y))╭──────────────────────────╮
│ 5×10 DimArray{Float64,2} │
├──────────────────── dims ┤
↓ X, → Y
└──────────────────────────┘
0.254966 0.0542736 0.174367 0.52276 … 0.257671 0.433249 0.37079
0.0196069 0.492355 0.0284599 0.681254 0.266973 0.841434 0.723008
0.585697 0.945226 0.82122 0.834784 0.622533 0.860144 0.430653
0.556947 0.342535 0.0125467 0.75126 0.636407 0.0268885 0.0670457
0.268822 0.0363362 0.53834 0.319352 0.153277 0.543613 0.783523We can access a value with the same dimension wrappers:
julia> da[Y(1), X(2)]0.01960686848243043There are shortcuts for creating DimArray:
julia> A = rand(5, 10)5×10 Matrix{Float64}:
0.473731 0.478363 0.490415 0.973651 … 0.906265 0.209399 0.234222
0.84213 0.197936 0.50529 0.802567 0.74795 0.877415 0.274847
0.12631 0.431317 0.191017 0.487295 0.510128 0.478535 0.285446
0.314178 0.328764 0.615541 0.862096 0.00462179 0.873724 0.860179
0.474796 0.906484 0.536741 0.232471 0.525516 0.273485 0.277565julia> DimArray(A, (X, Y))╭──────────────────────────╮
│ 5×10 DimArray{Float64,2} │
├──────────────────── dims ┤
↓ X, → Y
└──────────────────────────┘
0.473731 0.478363 0.490415 0.973651 … 0.906265 0.209399 0.234222
0.84213 0.197936 0.50529 0.802567 0.74795 0.877415 0.274847
0.12631 0.431317 0.191017 0.487295 0.510128 0.478535 0.285446
0.314178 0.328764 0.615541 0.862096 0.00462179 0.873724 0.860179
0.474796 0.906484 0.536741 0.232471 0.525516 0.273485 0.277565julia> DimArray(A, (X, Y); name=:DimArray, metadata=Dict())╭───────────────────────────────────╮
│ 5×10 DimArray{Float64,2} DimArray │
├───────────────────────────── dims ┤
↓ X, → Y
├───────────────────────── metadata ┤
Dict{Any, Any}()
└───────────────────────────────────┘
0.473731 0.478363 0.490415 0.973651 … 0.906265 0.209399 0.234222
0.84213 0.197936 0.50529 0.802567 0.74795 0.877415 0.274847
0.12631 0.431317 0.191017 0.487295 0.510128 0.478535 0.285446
0.314178 0.328764 0.615541 0.862096 0.00462179 0.873724 0.860179
0.474796 0.906484 0.536741 0.232471 0.525516 0.273485 0.277565Constructing 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.630672 0.411295 0.390882 0.93114 0.25481
0.459446 0.223727 0.672225 0.960334 0.366746
0.926632 0.602688 0.420311 0.0614758 0.0500562
0.912899 0.407069 0.692156 0.435304 0.455434
0.799753 0.698676 0.408161 0.0036255 0.892026and get a value, here another smaller DimArray:
julia> da1[a=3, b=1:3]╭───────────────────────────────╮
│ 3-element DimArray{Float64,1} │
├───────────────────────── dims ┤
↓ b
└───────────────────────────────┘
0.926632
0.602688
0.420311Dimensional 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.494198 0.891012 0.35591 0.879507 0.306018 0.171146 0.900921
0.90456 0.581675 0.662664 0.475175 0.34708 0.318687 0.125854
0.49259 0.51576 0.435284 0.829837 0.491404 0.808217 0.488783
0.674695 0.776362 0.740192 0.301411 0.869942 0.585707 0.197516
0.179302 0.72292 0.982988 0.846096 0.267424 0.423173 0.121698
0.101133 0.311115 0.106988 0.401742 0.906479 0.607331 0.194732
0.949688 0.10438 0.839559 0.875198 0.850127 0.617745 0.347996
0.66229 0.679423 0.752713 0.360941 0.358949 0.180831 0.732175
0.688789 0.879564 0.350734 0.851987 0.8019 0.673176 0.436439
0.96233 0.462039 0.903333 0.0222559 0.521157 0.173682 0.11301julia> da2[(X(3), Z(5))]╭───────────────────────────────╮
│ 7-element DimArray{Float64,1} │
├───────────────────────── dims ┤
↓ Y
└───────────────────────────────┘
0.399328
0.0768945
0.725217
0.0782474
0.480972
0.739601
0.796302We 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.399328 0.222351 0.845011
0.0768945 0.622988 0.996835
0.725217 0.428327 0.433753
0.0782474 0.056375 0.172586
0.480972 0.00889783 0.533393
0.739601 0.0311501 0.768995
0.796302 0.660471 0.409441DimIndices 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.35591 0.425549 0.897045 0.390221 0.343369
0.662664 0.538327 0.57397 0.771498 0.512143
0.435284 0.107479 0.25401 0.103998 0.725217
0.740192 0.603077 0.536009 0.332878 0.878539
0.982988 0.396291 0.686698 0.92885 0.310618
0.106988 0.696538 0.758446 0.0499511 0.615719
0.839559 0.822314 0.607239 0.428327 0.24181
0.752713 0.433753 0.528599 0.169286 0.445842
0.350734 0.653801 0.264563 0.536341 0.657623
0.903333 0.842037 0.669504 0.206849 0.90977The 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.630672 0.411295 0.390882 0.93114 0.25481
0.459446 0.223727 0.672225 0.960334 0.366746
0.926632 0.602688 0.420311 0.0614758 0.0500562
0.912899 0.407069 0.692156 0.435304 0.455434
0.799753 0.698676 0.408161 0.0036255 0.892026It 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.630672 0.411295 0.390882 0.93114 0.25481
0.459446 0.223727 0.672225 0.960334 0.366746
0.926632 0.602688 0.420311 0.0614758 0.0500562
0.912899 0.407069 0.692156 0.435304 0.455434
0.799753 0.698676 0.408161 0.0036255 0.892026In 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.0119886 0.752378 0.209112 0.473908
0.354549 0.598904 0.190014 0.187816
0.0929424 0.899065 0.366571 0.024557julia> sum(da5; dims=Ti)╭───────────────────────────╮
│ 3×4×1 DimArray{Float64,3} │
├───────────────────── dims ┤
↓ X, → Y, ↗ Ti
└───────────────────────────┘
[:, :, 1]
1.0199 3.55923 2.08786 3.0408
2.09372 2.19568 2.25177 1.6164
1.5156 3.16999 2.77272 2.77511Dims 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.
Range (min … max): 2.785 ns … 25.538 ns ┊ GC (min … max): 0.00% … 0.00%
Time (median): 2.795 ns ┊ GC (median): 0.00%
Time (mean ± σ): 2.805 ns ± 0.372 ns ┊ GC (mean ± σ): 0.00% ± 0.00%
▆▄ █ ▆ ▃ ▁ ▂ ▁
██▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁█▁█▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁█▁█▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁██ █
2.78 ns Histogram: log(frequency) by time 2.82 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): 2.785 ns … 32.962 ns ┊ GC (min … max): 0.00% … 0.00%
Time (median): 2.795 ns ┊ GC (median): 0.00%
Time (mean ± σ): 2.806 ns ± 0.411 ns ┊ GC (mean ± σ): 0.00% ± 0.00%
▆▄ █▆ ▃ ▁ ▃▁ ▁
██▁▁▁▁▁▁▁▁▁▁▁▁██▁▁▁▁▁▁▁▁▁▁▁▁█▁█▁▁▁▁▁▁▁▁▁▁▁▁██▁▁▁▁▁▁▁▁▁▁▁▁▆ █
2.78 ns Histogram: log(frequency) by time 2.82 ns <
Memory estimate: 0 bytes, allocs estimate: 0.