X(1)
X 1
Named dimensions for julia data
2024-07-10
They mark that the wrapped object belongs to the dimension:
“Standard” dimensions (90% of spatial data):
Points
or Intervals
You can define them manually when you need to:
using DimensionalData.Lookups
l = Sampled(1:10; sampling=Intervals(Start()), order=ForwardOrdered(), span=Regular())
Sampled{Int64} ForwardOrdered Regular Intervals{Start}
wrapping: 1:10
With standard dimensions in a Tuple
:
╭─────────────────────────╮
│ 3×4 DimArray{Float64,2} │
├─────────────────────────┴─────────────────────────────────── dims ┐
↓ X Categorical{Symbol} [:a, :b, :c] ForwardOrdered,
→ Y Sampled{Float64} 10.0:10.0:40.0 ForwardOrdered Regular Points
└───────────────────────────────────────────────────────────────────┘
↓ → 10.0 20.0 30.0 40.0
:a 0.342617 0.46755 0.324454 0.811161
:b 0.17629 0.618897 0.23128 0.260502
:c 0.452733 0.251785 0.85885 0.834195
With arbitrary Dim
dimensions, in a NamedTuple
:
╭─────────────────────────╮
│ 3×4 DimArray{Float64,2} │
├─────────────────────────┴─────────────────────────────────── dims ┐
↓ a Categorical{Symbol} [:a, :b, :c] ForwardOrdered,
→ b Sampled{Float64} 10.0:10.0:40.0 ForwardOrdered Regular Points
└───────────────────────────────────────────────────────────────────┘
↓ → 10.0 20.0 30.0 40.0
:a 0.670531 0.431686 0.930199 0.260112
:b 0.518717 0.412699 0.776897 0.251387
:c 0.517501 0.731436 0.64018 0.628815
Shorthands: rand
, fill
, zeros
, ones
╭─────────────────────────╮
│ 6×6 DimArray{Float64,2} │
├─────────────────────────┴────────────────────────── dims ┐
↓ X,
→ Y Sampled{Int64} 10:2:20 ForwardOrdered Regular Points
└──────────────────────────────────────────────────────────┘
10 12 14 16 18 20
0.718435 0.158657 0.808367 0.100717 0.816818 0.862287
0.640028 0.0344655 0.995837 0.799657 0.542344 0.921293
0.422047 0.065179 0.166129 0.296414 0.920396 0.856656
0.352291 0.0740475 0.447463 0.587279 0.0916422 0.392502
0.151813 0.948753 0.219828 0.272536 0.518881 0.456842
0.276715 0.867893 0.173395 0.64557 0.905879 0.122918
ds = X([:a, :b, :c]), Ti(10.0:10:40.0)
S = DimStack((layer1=rand(Float32, 3, 4), layer2=zeros(Bool, 3, 4)), ds)
╭──────────────╮
│ 3×4 DimStack │
├──────────────┴─────────────────────────────────────────────── dims ┐
↓ X Categorical{Symbol} [:a, :b, :c] ForwardOrdered,
→ Ti Sampled{Float64} 10.0:10.0:40.0 ForwardOrdered Regular Points
├──────────────────────────────────────────────────────────── layers ┤
:layer1 eltype: Float32 dims: X, Ti size: 3×4
:layer2 eltype: Bool dims: X, Ti size: 3×4
└────────────────────────────────────────────────────────────────────┘
x, ti = X([:a, :b, :c]), Ti(10.0:10:40.0)
DimStack((twodims=rand(Float32, x, ti), onedim=zeros(Bool, x)))
╭──────────────╮
│ 3×4 DimStack │
├──────────────┴─────────────────────────────────────────────── dims ┐
↓ X Categorical{Symbol} [:a, :b, :c] ForwardOrdered,
→ Ti Sampled{Float64} 10.0:10.0:40.0 ForwardOrdered Regular Points
├──────────────────────────────────────────────────────────── layers ┤
:twodims eltype: Float32 dims: X, Ti size: 3×4
:onedim eltype: Bool dims: X size: 3
└────────────────────────────────────────────────────────────────────┘
DimArray:
using BenchmarkTools
@btime $A[3, 4] # Base Julia Array syntax
@btime $A[Y(4), X(3)] # Dimension wrappers
@btime $A[Y=4, X=3] # Keyword syntax
2.785 ns (0 allocations: 0 bytes)
2.785 ns (0 allocations: 0 bytes)
3.095 ns (0 allocations: 0 bytes)
0.8341949897901655
DimStack:
Find exact or approximate matches
Find the closest match
Find the interval that contains a value
Select data inside an interval
╭─────────────────────────────╮
│ 5-element DimArray{Int64,1} │
├─────────────────────────────┴──────────────────────────────────────── dims ┐
↓ X Sampled{Float64} 10.0:1.0:14.0 ForwardOrdered Regular Intervals{Start}
└────────────────────────────────────────────────────────────────────────────┘
10.0 1000
11.0 1100
12.0 1200
13.0 1300
14.0 1400
Make dimensional queries
╭──────────────────────────────╮
│ 50-element DimArray{Int64,1} │
├──────────────────────────────┴───────────────────────────────────────── dims ┐
↓ X Sampled{Float64} [1.0, 3.0, …, 97.0, 99.0] ForwardOrdered Irregular Intervals{Start}
└──────────────────────────────────────────────────────────────────────────────┘
1.0 100
3.0 300
5.0 500
7.0 700
9.0 900
11.0 1100
⋮
91.0 9100
93.0 9300
95.0 9500
97.0 9700
99.0 9900
dims
and rebuild
methods let other array types work like a DimArray
(And checkout the new docs by Lazaro Alonzo!)