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.95794 0.706651 0.986726 0.598791 … 0.749523 0.841492 0.105674
0.566832 0.158364 0.502735 0.806244 0.978328 0.845699 0.736963
0.745416 0.625966 0.0432049 0.68365 0.351699 0.31541 0.0160512
0.402922 0.595666 0.541878 0.0656426 0.657565 0.74914 0.611547
0.373373 0.936929 0.989761 0.50757 0.833247 0.651389 0.894886julia> da = DimArray(A, (X, Y))╭──────────────────────────╮
│ 5×10 DimArray{Float64,2} │
├──────────────────── dims ┤
↓ X, → Y
└──────────────────────────┘
0.95794 0.706651 0.986726 0.598791 … 0.749523 0.841492 0.105674
0.566832 0.158364 0.502735 0.806244 0.978328 0.845699 0.736963
0.745416 0.625966 0.0432049 0.68365 0.351699 0.31541 0.0160512
0.402922 0.595666 0.541878 0.0656426 0.657565 0.74914 0.611547
0.373373 0.936929 0.989761 0.50757 0.833247 0.651389 0.894886We can access a value with the same dimension wrappers:
julia> da[Y(1), X(2)]0.5668318889001001There are shortcuts for creating DimArray:
julia> A = rand(5, 10)5×10 Matrix{Float64}:
0.639056 0.162344 0.40033 0.642957 … 0.700505 0.759531 0.288071
0.885087 0.509442 0.56509 0.373112 0.378071 0.136832 0.645115
0.832529 0.952885 0.488262 0.0610271 0.899446 0.837848 0.139867
0.573179 0.743008 0.609853 0.849561 0.61555 0.499949 0.866682
0.657422 0.495045 0.958831 0.617645 0.69769 0.791376 0.192733julia> DimArray(A, (X, Y))╭──────────────────────────╮
│ 5×10 DimArray{Float64,2} │
├──────────────────── dims ┤
↓ X, → Y
└──────────────────────────┘
0.639056 0.162344 0.40033 0.642957 … 0.700505 0.759531 0.288071
0.885087 0.509442 0.56509 0.373112 0.378071 0.136832 0.645115
0.832529 0.952885 0.488262 0.0610271 0.899446 0.837848 0.139867
0.573179 0.743008 0.609853 0.849561 0.61555 0.499949 0.866682
0.657422 0.495045 0.958831 0.617645 0.69769 0.791376 0.192733julia> DimArray(A, (X, Y); name=:DimArray, metadata=Dict())╭───────────────────────────────────╮
│ 5×10 DimArray{Float64,2} DimArray │
├───────────────────────────── dims ┤
↓ X, → Y
├───────────────────────── metadata ┤
Dict{Any, Any}()
└───────────────────────────────────┘
0.639056 0.162344 0.40033 0.642957 … 0.700505 0.759531 0.288071
0.885087 0.509442 0.56509 0.373112 0.378071 0.136832 0.645115
0.832529 0.952885 0.488262 0.0610271 0.899446 0.837848 0.139867
0.573179 0.743008 0.609853 0.849561 0.61555 0.499949 0.866682
0.657422 0.495045 0.958831 0.617645 0.69769 0.791376 0.192733Constructing 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.819611 0.501832 0.859494 0.439131 0.103532
0.560248 0.405434 0.437809 0.856655 0.0984775
0.834067 0.953677 0.991849 0.1499 0.32101
0.478914 0.913216 0.779148 0.802391 0.28041
0.731579 0.401525 0.543813 0.0608885 0.514969and get a value, here another smaller DimArray:
julia> da1[a=3, b=1:3]╭───────────────────────────────╮
│ 3-element DimArray{Float64,1} │
├───────────────────────── dims ┤
↓ b
└───────────────────────────────┘
0.834067
0.953677
0.991849Dimensional 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.0629211 0.690933 0.819368 0.222394 0.00724389 0.584807 0.175053
0.810479 0.310918 0.294164 0.501109 0.709241 0.60229 0.456803
0.238421 0.12695 0.159222 0.65032 0.0339698 0.697498 0.296854
0.435004 0.942832 0.24031 0.54557 0.764729 0.149246 0.557318
0.0027024 0.538618 0.173126 0.733856 0.521097 0.939021 0.93024
0.183257 0.805305 0.517698 0.862588 0.398093 0.209704 0.952969
0.0974046 0.116782 0.0418875 0.356085 0.771008 0.996109 0.122355
0.350782 0.373363 0.964235 0.887114 0.297322 0.332306 0.632346
0.404913 0.279065 0.222385 0.426133 0.124574 0.550992 0.0795287
0.981742 0.247913 0.0563747 0.708618 0.57619 0.469371 0.882867julia> da2[(X(3), Z(5))]╭───────────────────────────────╮
│ 7-element DimArray{Float64,1} │
├───────────────────────── dims ┤
↓ Y
└───────────────────────────────┘
0.57588
0.518985
0.312162
0.806361
0.129221
0.724209
0.10971We 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.57588 0.358174 0.0804921
0.518985 0.439565 0.108621
0.312162 0.0736582 0.712785
0.806361 0.0912803 0.890638
0.129221 0.829605 0.450389
0.724209 0.864244 0.514191
0.10971 0.984254 0.586429DimIndices 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.819368 0.246754 0.651186 0.751 0.85602
0.294164 0.129869 0.139423 0.00965474 0.267839
0.159222 0.606901 0.741978 0.733037 0.312162
0.24031 0.884997 0.408142 0.939402 0.70099
0.173126 0.779491 0.727995 0.742541 0.818683
0.517698 0.958101 0.467201 0.364871 0.603676
0.0418875 0.873105 0.27668 0.0736582 0.814274
0.964235 0.712785 0.157566 0.418905 0.175405
0.222385 0.0891463 0.773459 0.926034 0.382124
0.0563747 0.252972 0.037491 0.823647 0.840436The 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:777
╭─────────────────────────╮
│ 5×5 DimArray{Float64,2} │
├─────────────────── dims ┤
↓ a, → b
└─────────────────────────┘
0.819611 0.501832 0.859494 0.439131 0.103532
0.560248 0.405434 0.437809 0.856655 0.0984775
0.834067 0.953677 0.991849 0.1499 0.32101
0.478914 0.913216 0.779148 0.802391 0.28041
0.731579 0.401525 0.543813 0.0608885 0.514969It 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:777
╭─────────────────────────╮
│ 5×5 DimArray{Float64,2} │
├─────────────────── dims ┤
↓ a, → b
└─────────────────────────┘
0.819611 0.501832 0.859494 0.439131 0.103532
0.560248 0.405434 0.437809 0.856655 0.0984775
0.834067 0.953677 0.991849 0.1499 0.32101
0.478914 0.913216 0.779148 0.802391 0.28041
0.731579 0.401525 0.543813 0.0608885 0.514969In 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.829889 0.976549 0.44062 0.617458
0.74677 0.385933 0.606753 0.273625
0.604571 0.916434 0.084422 0.996737julia> sum(da5; dims=Ti)╭───────────────────────────╮
│ 3×4×1 DimArray{Float64,3} │
├───────────────────── dims ┤
↓ X, → Y, ↗ Ti
└───────────────────────────┘
[:, :, 1]
2.77628 2.33498 2.16491 1.68556
3.80704 1.5169 1.88431 2.44022
3.16867 3.39636 1.76523 3.20259Dims 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): 3.095 ns … 18.615 ns ┊ GC (min … max): 0.00% … 0.00%
Time (median): 3.106 ns ┊ GC (median): 0.00%
Time (mean ± σ): 3.119 ns ± 0.375 ns ┊ GC (mean ± σ): 0.00% ± 0.00%
█
▃▇▁▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▅▁█▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▂▁▃▁▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▂▂ ▂
3.1 ns Histogram: 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.167 ns ┊ GC (min … max): 0.00% … 0.00%
Time (median): 3.106 ns ┊ GC (median): 0.00%
Time (mean ± σ): 3.112 ns ± 0.309 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.