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.486605 0.74358 0.672124
1.2 0.0948766 0.255117 0.902924
1.4 0.788685 0.0518411 0.224263
1.6 0.921654 0.540534 0.772217
1.8 0.564925 0.700604 0.889339
2.0 0.855162 0.366107 0.47156Then 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.9029244579268744Or 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.486605 0.74358 0.672124
1.2 0.0948766 0.255117 0.902924
1.4 0.788685 0.0518411 0.224263At 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.0948766 0.902924
1.4 0.788685 0.224263Lookups
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.366221 0.214733 0.30706 0.392976 0.794792
20 0.0456598 0.0651095 0.463113 0.289349 0.381209
30 0.308172 0.930091 0.624751 0.934133 0.889463
40 0.969529 0.353019 0.377543 0.383031 0.530034
50 0.556152 0.107397 0.165141 0.93981 0.559799
60 0.00484408 0.0868385 0.867892 0.494671 0.0766532
70 0.525497 0.249446 0.751529 0.579141 0.116836This 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 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 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.180988 0.821526 0.277448 0.535212 0.88671 0.70421
1.2 0.420643 0.311703 0.600425 0.595862 0.995208 0.264621
1.4 0.427579 0.171992 0.193942 0.63236 0.479924 0.863911
1.6 0.0791628 0.862192 0.338323 0.37746 0.819781 0.324112
1.8 0.0668179 0.477716 0.188061 0.931093 0.584911 0.529155
2.0 0.654184 0.336586 0.405907 0.851117 0.992735 0.383076We 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 … 12 11 10
1.0 0.015552 0.94607 0.573439 0.660559 0.0903863
1.04 0.766466 0.998614 0.84697 0.647139 0.0253976
1.08 0.894927 0.013432 0.0960544 0.687562 0.488948
1.12 0.763743 0.0674259 0.840139 0.554324 0.677101
⋮ ⋱ ⋮
1.84 0.948694 0.0713194 0.107935 0.607749 0.926143
1.88 0.446429 0.685622 0.674979 0.979409 0.14733
1.92 0.971136 0.439927 0.580407 0.33591 0.872204
1.96 0.730237 0.112532 0.756405 0.0772937 0.723273
2.0 0.781521 0.737631 … 0.454062 0.500038 0.21034And 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.0903863 0.573439 0.367979 0.444091 0.518519 0.015552
1.2 0.828352 0.0430457 0.931565 0.68245 0.760041 0.0252584
1.4 0.292608 0.290848 0.783242 0.985999 0.788589 0.784265
1.6 0.539049 0.656643 0.289907 0.719035 0.429794 0.603701
1.8 0.983653 0.84746 0.603968 0.933062 0.801711 0.831267
2.0 0.21034 0.454062 0.956386 0.118931 0.478125 0.781521If the lookups aren't aligned we can use Near instead of At, which 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.246934 0.103356 0.880099 0.980343 0.643925
1.007 0.115213 0.261838 0.1576 0.89401 0.244054
1.014 0.0540814 0.404719 0.254164 0.104542 0.768877
1.021 0.82871 0.0609186 0.935995 0.338376 0.665843
⋮ ⋱
1.966 0.932692 0.28812 0.674324 0.717168 0.331161
1.973 0.237553 0.740514 0.705772 0.709688 0.425974
1.98 0.823827 0.631055 0.55297 0.405462 0.781756
1.987 0.453897 0.366318 0.685089 0.526883 0.575048
1.994 0.509778 0.541329 … 0.034559 0.876982 0.290693julia> 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 13.6 16.3 18.1 19.9
1.0 0.246934 0.77453 0.168038 0.882342 0.658382 0.834928
1.203 0.611552 0.889014 0.589572 0.036924 0.482589 0.808567
1.399 0.718699 0.030589 0.243251 0.316897 0.879872 0.709224
1.602 0.928009 0.0775366 0.694753 0.292979 0.584428 0.841047
1.798 0.24065 0.58728 0.641835 0.914308 0.962598 0.0101373
1.994 0.509778 0.0665786 0.7418 0.684055 0.787073 0.782368