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.

@ansi
using DimensionalData
x, y = X(1.0:10.0), Y(5.0:10.0)
st = DimStack((a=rand(x, y), b=rand(x, y), c=rand(y), d=rand(x)))

The behavior of a DimStack is at times like a NamedTuple of DimArray and, at other times, an AbstractArray of NamedTuple.

NamedTuple-like indexing ​

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

@ansi
st.a
st[:c]

Array-like indexing ​

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

@ansi
st[X=1, Y=4]

Reducing functions ​

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

julia
using Statistics
@ansi
maximum(st)
maximum(st; dims=Y)

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 matrix layers can be rotated

@ansi
rotl90(st[(:a, :b)])
rotl90(st[(:a, :b)], 2)

Performance ​

Indexing a stack is fast - indexing a single value and returning a NamedTuple from all layers is usually measured 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.

@ansi
using BenchmarkTools
@btime $st[X=1, Y=4]
@btime $st[1, 4]