Selectors
In addition to choosing dimensions by name, we can also select values within them.
First, we can create a 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, …, :c] ForwardOrdered
└──────────────────────────────────────────────────────────────────┘
↓ → :a :b :c
1.0 0.822783 0.744467 0.698995
1.2 0.381692 0.827519 0.983399
1.4 0.992013 0.806006 0.35493
1.6 0.359758 0.376108 0.173315
1.8 0.973847 0.0943594 0.640666
2.0 0.406072 0.698462 0.952943Then we can use the Selector to select values from the array:
The At(x) selector gets the index or indices exactly matching the passed in value(s).
julia> A[X=At(1.2), Y=At(:c)]0.9833991278548375Or 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.4] ForwardOrdered Irregular Points,
→ Y Categorical{Symbol} [:a, …, :c] ForwardOrdered
└──────────────────────────────────────────────────────────────────────┘
↓ → :a :b :c
1.0 0.822783 0.744467 0.698995
1.2 0.381692 0.827519 0.983399
1.4 0.992013 0.806006 0.35493At 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.381692 0.983399
1.4 0.992013 0.35493Lookups
Selectors find indices in the Lookup of each dimension. Lookups wrap other AbstractArray (often AbstractRange) but add additional traits to facilitate fast lookups or specifying point or interval behaviour. These are usually detected automatically.
using DimensionalData.LookupsThe Sampled(x) lookup holds values sampled along an axis. They may be Ordered/Unordered, Intervals/Points, and Regular/Irregular.
Most of these properties are usually detected automatically, 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 can't 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, …, :e] ForwardOrdered
└─────────────────────────────────────────────────────────────┘
↓ → :a :b :c :d :e
10 0.772277 0.0653496 0.191494 0.735544 0.389375
20 0.711133 0.748041 0.341976 0.895233 0.0898635
30 0.883222 0.621603 0.41767 0.824126 0.606635
40 0.802776 0.768488 0.594101 0.956886 0.87806
50 0.156538 0.869012 0.530389 0.00114293 0.87255
60 0.969079 0.101231 0.642658 0.522816 0.415714
70 0.536911 0.339858 0.679337 0.227694 0.0862889This 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,Symbol, andCharare set toCategoricallookup.orderis detected asUnordered,ForwardOrdered, orReverseOrdered
Arrays and ranges of
Number,DateTime, and other things are set toSampledlookups.orderis detected asUnordered,ForwardOrdered, orReverseOrdered.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 forIntervalsEmpty 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 the 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.621662 0.12452 0.749731 0.882656 0.680987 0.667841
1.2 0.72217 0.206404 0.201129 0.683795 0.396585 0.218854
1.4 0.896257 0.791844 0.792553 0.12668 0.687921 0.870348
1.6 0.301659 0.758149 0.320681 0.575595 0.647225 0.825204
1.8 0.973622 0.229418 0.679453 0.915251 0.357367 0.0521357
2.0 0.942925 0.228248 0.442111 0.467074 0.246886 0.666072We 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.495624 0.952489 0.397388 0.208304 0.515929 0.467332
1.04 0.263531 0.10454 0.074921 0.158368 0.624812 0.3926
1.08 0.388768 0.990294 0.240023 0.636573 0.737826 0.665416
1.12 0.422318 0.610173 0.164932 0.797969 0.383724 0.951879
⋮ ⋱ ⋮
1.88 0.42568 0.429465 0.109509 0.581136 0.5053 0.997569
1.92 0.750522 0.244582 0.499362 0.985911 0.328169 0.822161
1.96 0.371583 0.400001 0.528383 … 0.337296 0.760043 0.189863
2.0 0.564657 0.986155 0.536442 0.466288 0.215999 0.663057And 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, …, 2.0] ForwardOrdered Irregular Points,
→ Y Sampled{Int64} [10, …, 20] ReverseOrdered Irregular Points
└──────────────────────────────────────────────────────────────────────┘
↓ → 10 12 14 16 18 20
1.0 0.467332 0.208304 0.441131 0.856029 0.397388 0.495624
1.2 0.87475 0.867165 0.0990865 0.497732 0.0564029 0.30391
1.4 0.130549 0.097066 0.53071 0.0566306 0.827833 0.0831822
1.6 0.801023 0.547435 0.503092 0.630338 0.614092 0.293505
1.8 0.216905 0.212246 0.719849 0.845326 0.298791 0.896624
2.0 0.663057 0.466288 0.53517 0.337452 0.536442 0.564657If the lookups aren't aligned, we can use Near instead of At, which is like doing a nearest neighbor 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.661197 0.883464 0.739562 0.366935 0.923642
1.007 0.887979 0.0284535 0.352175 0.127118 0.639886
1.014 0.0676472 0.109635 0.231255 0.171322 0.897075
1.021 0.894974 0.268113 0.369938 0.0335436 0.0662576
⋮ ⋱
1.973 0.271952 0.948532 … 0.906332 0.393936 0.93513
1.98 0.294534 0.680648 0.53422 0.414049 0.693914
1.987 0.148229 0.0646533 0.560553 0.565202 0.81171
1.994 0.28595 0.195926 0.0485471 0.794437 0.767151julia> C[DimSelectors(A; selectors=Near)]┌ 6×6 DimArray{Float64, 2} ┐
├──────────────────────────┴─────────────────────────────────────── dims ┐
↓ X Sampled{Float64} [1.0, …, 1.994] ForwardOrdered Irregular Points,
→ Y Sampled{Float64} [10.0, …, 19.9] ForwardOrdered Irregular Points
└────────────────────────────────────────────────────────────────────────┘
↓ → 10.0 11.8 13.6 16.3 18.1 19.9
1.0 0.661197 0.990004 0.790587 0.773939 0.533517 0.0227173
1.203 0.964912 0.181243 0.495549 0.953234 0.289721 0.763448
1.399 0.434291 0.0073401 0.393438 0.455783 0.217338 0.0476535
1.602 0.478164 0.589126 0.859992 0.00962192 0.674491 0.989189
1.798 0.822212 0.685352 0.206816 0.652086 0.184061 0.56708
1.994 0.28595 0.831654 0.0163064 0.0640115 0.942258 0.618476