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.159581 0.139798 0.586725
1.2 0.195753 0.217643 0.174918
1.4 0.260978 0.0249301 0.290158
1.6 0.694809 0.355708 0.226577
1.8 0.208902 0.770033 0.579044
2.0 0.36925 0.261977 0.155022Then 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.17491763183495057Or 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.159581 0.139798 0.586725
1.2 0.195753 0.217643 0.174918
1.4 0.260978 0.0249301 0.290158At 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.195753 0.174918
1.4 0.260978 0.290158Lookups
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.465958 0.250311 0.342618 0.832291 0.391362
20 0.0912115 0.971567 0.144211 0.737546 0.70774
30 0.99598 0.77689 0.232209 0.223963 0.125777
40 0.100235 0.925083 0.909358 0.638232 0.0939068
50 0.430662 0.622476 0.835841 0.19917 0.574164
60 0.873566 0.763073 0.714869 0.198425 0.487368
70 0.353655 0.997808 0.121561 0.392788 0.859067This 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.3702 0.554302 0.443703 0.268319 0.258262 0.593003
1.2 0.355773 0.746269 0.878717 0.0687604 0.725215 0.285062
1.4 0.0398245 0.216546 0.02585 0.928663 0.457007 0.54544
1.6 0.534066 0.300529 0.165966 0.19244 0.850424 0.755905
1.8 0.1136 0.35184 0.805225 0.0261943 0.506233 0.791326
2.0 0.0175646 0.222944 0.591926 0.0160203 0.776711 0.154586We 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.857812 0.982895 0.0629484 0.720861 0.62218
1.04 0.385704 0.819492 0.487291 0.108329 0.234635
1.08 0.621831 0.862037 0.776763 0.554565 0.0790929
1.12 0.93306 0.89617 0.571113 0.0301721 0.231873
⋮ ⋱ ⋮
1.84 0.927318 0.786257 0.588042 0.409381 0.388161
1.88 0.936822 0.534454 0.849027 0.584897 0.0590538
1.92 0.998183 0.3347 0.566095 0.824314 0.348056
1.96 0.616923 0.0208249 0.786575 0.779713 0.545476
2.0 0.947978 0.27242 … 0.320859 0.957989 0.831119And 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.62218 0.0629484 0.603287 0.6688 0.105623 0.857812
1.2 0.828575 0.790099 0.925379 0.945159 0.275609 0.02943
1.4 0.383224 0.0722447 0.955947 0.319212 0.313031 0.457359
1.6 0.614255 0.106162 0.725787 0.377835 0.829165 0.176539
1.8 0.320705 0.667147 0.911761 0.355943 0.554123 0.918037
2.0 0.831119 0.320859 0.223883 0.388216 0.774115 0.947978If 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.275719 0.601143 0.376801 0.293568 0.21028
1.007 0.0789474 0.570915 0.0432665 0.342221 0.952034
1.014 0.683848 0.607528 0.493411 0.542363 0.418236
1.021 0.418541 0.986708 0.963913 0.878544 0.0835572
⋮ ⋱
1.966 0.0634705 0.409878 0.827307 0.0754673 0.27206
1.973 0.119945 0.0394579 0.0412538 0.218368 0.89851
1.98 0.63265 0.828622 0.115054 0.208954 0.992606
1.987 0.0936452 0.116188 0.646284 0.983886 0.38941
1.994 0.0801141 0.692244 … 0.737133 0.103017 0.635901julia> 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.275719 0.835542 0.358392 0.872406 0.486008 0.0860536
1.203 0.18348 0.69786 0.596097 0.661387 0.630996 0.0552078
1.399 0.888909 0.838874 0.729822 0.95005 0.739578 0.536709
1.602 0.881535 0.442407 0.459933 0.837444 0.608333 0.484244
1.798 0.336157 0.172663 0.230808 0.260801 0.969247 0.629978
1.994 0.0801141 0.873523 0.871562 0.00962014 0.956877 0.971842