Skip to content

DimStacks

An AbstractDimStack represents a collection of AbstractDimArray layers that share some or all dimensions. For any two layers, a dimension of the same name must have the identical lookup - in fact only one is stored for all layers to enforce this consistency.

julia
julia> using DimensionalData

julia> x, y = X(1.0:10.0), Y(5.0:10.0)
X 1.0:1.0:10.0,
Y 5.0:1.0:10.0
julia
julia> st = DimStack((a=rand(x, y), b=rand(x, y), c=rand(y), d=rand(x)))
╭───────────────╮
10×6 DimStack
├───────────────┴──────────────────────────────────────────── dims ┐
X Sampled{Float64} 1.0:1.0:10.0 ForwardOrdered Regular Points,
Y Sampled{Float64} 5.0:1.0:10.0 ForwardOrdered Regular Points
├────────────────────────────────────────────────────────── layers ┤
  :a eltype: Float64 dims: X, Y size: 10×6
  :b eltype: Float64 dims: X, Y size: 10×6
  :c eltype: Float64 dims: Y size: 6
  :d eltype: Float64 dims: X size: 10
└──────────────────────────────────────────────────────────────────┘

The behaviour of a DimStack is at times like a NamedTuple of DimArray and, others an AbstractArray of NamedTuple.

NamedTuple-like indexing

Layers can be accessed with .name or [:name]

julia
julia> st.a
╭────────────────────────────╮
10×6 DimArray{Float64,2} a
├────────────────────────────┴─────────────────────────────── dims ┐
X Sampled{Float64} 1.0:1.0:10.0 ForwardOrdered Regular Points,
Y Sampled{Float64} 5.0:1.0:10.0 ForwardOrdered Regular Points
└──────────────────────────────────────────────────────────────────┘
  5.0        6.0       7.0        8.0        9.0       10.0
  1.0  0.688541   0.361062  0.928231   0.944279   0.873843   0.573661
  2.0  0.11726    0.389465  0.371732   0.269398   0.9704     0.0281263
  3.0  0.856458   0.392057  0.505377   0.112034   0.437296   0.13363
  4.0  0.466214   0.201498  0.706129   0.316551   0.401068   0.179228
  5.0  0.0348108  0.419218  0.680324   0.706632   0.781876   0.136633
  6.0  0.536752   0.195665  0.928737   0.36841    0.932654   0.275852
  7.0  0.543197   0.835027  0.898725   0.483554   0.908784   0.76149
  8.0  0.270362   0.225353  0.0424941  0.209485   0.159686   0.705786
  9.0  0.47412    0.277163  0.423238   0.0170795  0.691882   0.335604
 10.0  0.448086   0.254743  0.488194   0.0982239  0.58037    0.102238
julia
julia> st[:c]
╭─────────────────────────────────╮
6-element DimArray{Float64,1} c
├─────────────────────────────────┴───────────────────────── dims ┐
Y Sampled{Float64} 5.0:1.0:10.0 ForwardOrdered Regular Points
└─────────────────────────────────────────────────────────────────┘
  5.0  0.0183714
  6.0  0.445407
  7.0  0.572208
  8.0  0.0701996
  9.0  0.94393
 10.0  0.782814

Array-like indexing

Indexing with a scalar returns a NamedTuple of values, one for each layer:

julia
julia> st[X=1, Y=4]
(a = 0.9442785773582415, b = 0.20751849772440256, c = 0.0701995540900271, d = 0.3995229599738085)

Reducing functions

Base functions like mean, maximum, reverse are applied to all layers of the stack.

julia
julia> maximum(st)
(a = 0.9703999039749159, b = 0.9887585559524303, c = 0.943930215971094, d = 0.9453002689503803)
julia
julia> maximum(st; dims=Y)
╭───────────────╮
10×1 DimStack
├───────────────┴──────────────────────────────────────────── dims ┐
X Sampled{Float64} 1.0:1.0:10.0 ForwardOrdered Regular Points,
Y Sampled{Float64} 7.5:6.0:7.5 ForwardOrdered Regular Points
├────────────────────────────────────────────────────────── layers ┤
  :a eltype: Float64 dims: X, Y size: 10×1
  :b eltype: Float64 dims: X, Y size: 10×1
  :c eltype: Float64 dims: Y size: 1
  :d eltype: Float64 dims: X size: 10
└──────────────────────────────────────────────────────────────────┘

broadcast_dims broadcasts functions over any mix of AbstractDimStack and AbstractDimArray returning a new AbstractDimStack with layers the size of the largest layer in the broadcast. This will work even if dimension permutation does not match in the objects.

Only atrix layers can be rotaed

julia
julia> rotl90(st[(:a, :b)])
╭───────────────╮
6×10 DimStack
├───────────────┴───────────────────────────────────────────── dims ┐
Y Sampled{Float64} 10.0:-1.0:5.0 ReverseOrdered Regular Points,
X Sampled{Float64} 1.0:1.0:10.0 ForwardOrdered Regular Points
├─────────────────────────────────────────────────────────── layers ┤
  :a eltype: Float64 dims: Y, X size: 6×10
  :b eltype: Float64 dims: Y, X size: 6×10
└───────────────────────────────────────────────────────────────────┘
julia
julia> rotl90(st[(:a, :b)], 2)
╭───────────────╮
10×6 DimStack
├───────────────┴───────────────────────────────────────────── dims ┐
X Sampled{Float64} 10.0:-1.0:1.0 ReverseOrdered Regular Points,
Y Sampled{Float64} 10.0:-1.0:5.0 ReverseOrdered Regular Points
├─────────────────────────────────────────────────────────── layers ┤
  :a eltype: Float64 dims: X, Y size: 10×6
  :b eltype: Float64 dims: X, Y size: 10×6
└───────────────────────────────────────────────────────────────────┘

Performance

Indexing stack is fast - indexing a single value return a NamedTuple from all layers is usally measures in nanoseconds, and no slower than manually indexing into each parent array directly.

There are some compilation overheads to this though, and stacks with very many layers can take a long time to compile.

julia
julia> using BenchmarkTools

julia> @btime $st[X=1, Y=4]
  4.027 ns (0 allocations: 0 bytes)
(a = 0.9442785773582415, b = 0.20751849772440256, c = 0.0701995540900271, d = 0.3995229599738085)
julia
julia> @btime $st[1, 4]
  4.027 ns (0 allocations: 0 bytes)
(a = 0.9442785773582415, b = 0.20751849772440256, c = 0.0701995540900271, d = 0.3995229599738085)