Selectors
As well as choosing dimensions by name, we can also select values in them.
First, we can create DimArray with lookup values as well as dimension names:
using DimensionalDatajulia> A = rand(X(1.0:0.2:2.0), Y([:a, :b, :c]))╭─────────────────────────╮
│ 6×3 DimArray{Float64,2} │
├─────────────────────────┴───────────────────────────────── dims ┐
↓ X Sampled{Float64} 1.0:0.2:2.0 ForwardOrdered Regular Points,
→ Y Categorical{Symbol} [:a, :b, :c] ForwardOrdered
└─────────────────────────────────────────────────────────────────┘
↓ → :a :b :c
1.0 0.190975 0.533185 0.351547
1.2 0.77701 0.482625 0.639468
1.4 0.260198 0.647913 0.263198
1.6 0.959904 0.905721 0.266215
1.8 0.749218 0.660909 0.293299
2.0 0.629129 0.935263 0.469573Then we can use Selector to select values from the array:
At(x) gets the index or indices exactly matching the passed in value/s.
julia> A[X=At(1.2), Y=At(:c)]0.6394684469070069Or within a tolerance:
julia> A[X=At(0.99:0.201:1.5; atol=0.05)]╭─────────────────────────╮
│ 3×3 DimArray{Float64,2} │
├─────────────────────────┴─────────────────────────────────────── dims ┐
↓ X Sampled{Float64} [1.0, 1.2, 1.4] ForwardOrdered Irregular Points,
→ Y Categorical{Symbol} [:a, :b, :c] ForwardOrdered
└───────────────────────────────────────────────────────────────────────┘
↓ → :a :b :c
1.0 0.190975 0.533185 0.351547
1.2 0.77701 0.482625 0.639468
1.4 0.260198 0.647913 0.263198At can also take vectors and ranges:
julia> A[X=At(1.2:0.2:1.5), Y=At([:a, :c])]╭─────────────────────────╮
│ 2×2 DimArray{Float64,2} │
├─────────────────────────┴────────────────────────────────── dims ┐
↓ X Sampled{Float64} [1.2, 1.4] ForwardOrdered Irregular Points,
→ Y Categorical{Symbol} [:a, :c] ForwardOrdered
└──────────────────────────────────────────────────────────────────┘
↓ → :a :c
1.2 0.77701 0.639468
1.4 0.260198 0.263198Lookups
Selectors find indices in the Lookup of each dimension. Lookups wrap other AbstractArray (often AbstractRange) but add aditional traits to facilitate fast lookups or specifing point or interval behviour. These are usually detected automatically.
using DimensionalData.LookupsSampled(x) lookups hold values sampled along an axis. They may be Ordered/Unordered, Intervals/Points, and Regular/Irregular.
Most of these properties are usually detected autoatically, but here we create a Sampled lookup manually:
julia> l = Sampled(10.0:10.0:100.0; order=ForwardOrdered(), span=Regular(10.0), sampling=Intervals(Start()))Sampled{Float64} ForwardOrdered Regular Intervals{Start}
wrapping: 10.0:10.0:100.0TO specify Irregular Intervals we should include the outer bounds of the lookup, as we cant determine them from the vector.
julia> l = Sampled([13, 8, 5, 3, 2, 1]; order=ForwardOrdered(), span=Irregular(1, 21), sampling=Intervals(Start()))Sampled{Int64} ForwardOrdered Irregular Intervals{Start}
wrapping: 6-element Vector{Int64}:
13
8
5
3
2
1Lookup autodetection
When we define an array, extra properties are detected:
julia> A = DimArray(rand(7, 5), (X(10:10:70), Y([:a, :b, :c, :d, :e])))╭─────────────────────────╮
│ 7×5 DimArray{Float64,2} │
├─────────────────────────┴───────────────────────────── dims ┐
↓ X Sampled{Int64} 10:10:70 ForwardOrdered Regular Points,
→ Y Categorical{Symbol} [:a, :b, :c, :d, :e] ForwardOrdered
└─────────────────────────────────────────────────────────────┘
↓ → :a :b :c :d :e
10 0.644314 0.40199 0.762975 0.54614 0.09768
20 0.583041 0.513987 0.05256 0.452448 0.389676
30 0.931769 0.461815 0.443556 0.74855 0.862191
40 0.246431 0.313272 0.682136 0.897627 0.247933
50 0.790788 0.453181 0.0385099 0.143357 0.763674
60 0.650154 0.124248 0.379419 0.489492 0.890425
70 0.132868 0.486585 0.482718 0.512601 0.821052This array has a Sampled lookup with ForwardOrdered Regular Points for X, and a Categorical ForwardOrdered for Y.
Most lookup types and properties are detected automatically like this from the arrays and ranges used.
Arrays and ranges of
String,SymbolandCharare set toCategoricallookup.orderis detected asUnordered,ForwardOrderedorReverseOrdered
Arrays and ranges of
Number,DateTimeand other things are set toSampledlookups.orderis detected asUnordered,ForwardOrderedorReverseOrdered.samplingis set toPoints()unless the values areIntervalSets.Interval, thenIntervals(Center())is used.spanis detected asRegular(step(range))forAbstractRangeandIrregular(nothing, nothing)for otherAbstractArray, wherenothing, nothingare the unknown outer bounds of the lookup. They are not needed forPointsas the outer values are the outer bounds. But they can be specified manually forIntervalsEmtpy dimensions or dimension types are assigned
NoLookup()ranges that can't be used with selectors as they hold no values.
DimSelector
We can also index with arrays of selectors DimSelectors. These are like CartesianIndices or DimIndices but holding Selectors At, Near or Contains.
julia> A = rand(X(1.0:0.2:2.0), Y(10:2:20))╭─────────────────────────╮
│ 6×6 DimArray{Float64,2} │
├─────────────────────────┴───────────────────────────────── dims ┐
↓ X Sampled{Float64} 1.0:0.2:2.0 ForwardOrdered Regular Points,
→ Y Sampled{Int64} 10:2:20 ForwardOrdered Regular Points
└─────────────────────────────────────────────────────────────────┘
↓ → 10 12 14 16 18 20
1.0 0.467056 0.592728 0.0849015 0.184609 0.113622 0.0727427
1.2 0.168243 0.788527 0.0947264 0.394144 0.824309 0.294748
1.4 0.992785 0.101364 0.293113 0.753047 0.580347 0.706387
1.6 0.164737 0.9798 0.556787 0.937874 0.471263 0.875016
1.8 0.286466 0.0627835 0.680164 0.669228 0.695903 0.954785
2.0 0.421809 0.0326527 0.859084 0.398508 0.458733 0.45565We can define another array with partly matching indices
julia> B = rand(X(1.0:0.04:2.0), Y(20:-1:10))╭───────────────────────────╮
│ 26×11 DimArray{Float64,2} │
├───────────────────────────┴──────────────────────────────── dims ┐
↓ X Sampled{Float64} 1.0:0.04:2.0 ForwardOrdered Regular Points,
→ Y Sampled{Int64} 20:-1:10 ReverseOrdered Regular Points
└──────────────────────────────────────────────────────────────────┘
↓ → 20 19 18 … 12 11 10
1.0 0.173819 0.618238 0.87506 0.356037 0.60087 0.539518
1.04 0.541454 0.614278 0.0625651 0.252514 0.114902 0.452803
1.08 0.114128 0.401202 0.155348 0.214653 0.125336 0.630811
1.12 0.903375 0.760066 0.685348 0.757307 0.786964 0.769718
⋮ ⋱ ⋮
1.84 0.898443 0.855647 0.432531 0.561028 0.368072 0.87415
1.88 0.237915 0.610631 0.0216045 0.658695 0.526022 0.743854
1.92 0.138131 0.661946 0.229045 0.580577 0.678624 0.00141495
1.96 0.758126 0.600942 0.330758 0.565678 0.835736 0.953006
2.0 0.419348 0.189016 0.558501 … 0.351803 0.613121 0.55665And we can simply select values from B with selectors from A:
julia> B[DimSelectors(A)]╭─────────────────────────╮
│ 6×6 DimArray{Float64,2} │
├─────────────────────────┴────────────────────────────────────────────── dims ┐
↓ X Sampled{Float64} [1.0, 1.2, …, 1.8, 2.0] ForwardOrdered Irregular Points,
→ Y Sampled{Int64} [10, 12, …, 18, 20] ReverseOrdered Irregular Points
└──────────────────────────────────────────────────────────────────────────────┘
↓ → 10 12 14 16 18 20
1.0 0.539518 0.356037 0.137394 0.176871 0.87506 0.173819
1.2 0.660519 0.987632 0.276294 0.506645 0.00754483 0.648636
1.4 0.938749 0.810233 0.0777518 0.620712 0.123312 0.770359
1.6 0.0622495 0.56286 0.70349 0.653022 0.00405575 0.111112
1.8 0.059808 0.0343627 0.399497 0.239699 0.664781 0.614935
2.0 0.55665 0.351803 0.637228 0.650761 0.558501 0.419348If the lookups aren't aligned we can use Near instead of At, which like doing a nearest neighor interpolation:
julia> C = rand(X(1.0:0.007:2.0), Y(10.0:0.9:30))╭────────────────────────────╮
│ 143×23 DimArray{Float64,2} │
├────────────────────────────┴────────────────────────────────── dims ┐
↓ X Sampled{Float64} 1.0:0.007:1.994 ForwardOrdered Regular Points,
→ Y Sampled{Float64} 10.0:0.9:29.8 ForwardOrdered Regular Points
└─────────────────────────────────────────────────────────────────────┘
↓ → 10.0 10.9 … 28.0 28.9 29.8
1.0 0.0283433 0.832972 0.979172 0.0736753 0.523361
1.007 0.0513514 0.0973439 0.0264366 0.156817 0.335525
1.014 0.640305 0.0260018 0.998495 0.960339 0.819635
1.021 0.0732561 0.605198 0.881583 0.351295 0.108742
⋮ ⋱
1.966 0.201993 0.107596 0.194626 0.52311 0.964257
1.973 0.366986 0.259897 0.355226 0.689888 0.766329
1.98 0.20346 0.590548 0.46804 0.0414787 0.960788
1.987 0.415383 0.10183 0.0332035 0.388863 0.0343267
1.994 0.179967 0.105908 … 0.766435 0.50832 0.951297julia> C[DimSelectors(A; selectors=Near)]╭─────────────────────────╮
│ 6×6 DimArray{Float64,2} │
├─────────────────────────┴────────────────────────────────────────────── dims ┐
↓ X Sampled{Float64} [1.0, 1.203, …, 1.798, 1.994] ForwardOrdered Irregular Points,
→ Y Sampled{Float64} [10.0, 11.8, …, 18.1, 19.9] ForwardOrdered Irregular Points
└──────────────────────────────────────────────────────────────────────────────┘
↓ → 10.0 11.8 … 16.3 18.1 19.9
1.0 0.0283433 0.327828 0.561888 0.386329 0.603356
1.203 0.00334971 0.303217 0.260807 0.571027 0.887141
1.399 0.569849 0.028817 0.934274 0.811194 0.371714
1.602 0.844139 0.235148 0.483087 0.235633 0.32203
1.798 0.729494 0.316387 … 0.0270892 0.328892 0.0768237
1.994 0.179967 0.746499 0.753331 0.903898 0.818005