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 DimensionalData
julia> 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.772277 0.536911 0.101231
1.2 0.711133 0.0653496 0.339858
1.4 0.883222 0.748041 0.191494
1.6 0.802776 0.621603 0.341976
1.8 0.156538 0.768488 0.87255
2.0 0.969079 0.869012 0.415714
Then 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.33985840915074383
Or 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.772277 0.536911 0.101231
1.2 0.711133 0.0653496 0.339858
1.4 0.883222 0.748041 0.191494
At
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.711133 0.339858
1.4 0.883222 0.191494
Lookups
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.Lookups
Sampled(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 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.0
To 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
1
Lookup 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.441181 0.12452 0.201129 0.12668 0.647225
20 0.621662 0.196478 0.792553 0.575595 0.357367
30 0.72217 0.791844 0.883323 0.915251 0.246886
40 0.896257 0.758149 0.679453 0.506221 0.667841
50 0.301659 0.229418 0.442111 0.680987 0.0429074
60 0.973622 0.228248 0.882656 0.396585 0.870348
70 0.942925 0.749731 0.683795 0.687921 0.825204
This 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
andChar
are set toCategorical
lookup.order
is detected asUnordered
,ForwardOrdered
orReverseOrdered
Arrays and ranges of
Number
,DateTime
and other things are set toSampled
lookups.order
is detected asUnordered
,ForwardOrdered
orReverseOrdered
.sampling
is set toPoints()
unless the values areIntervalSets.Interval
, thenIntervals(Center())
is used.span
is detected asRegular(step(range))
forAbstractRange
andIrregular(nothing, nothing)
for otherAbstractArray
, wherenothing, nothing
are the unknown outer bounds of the lookup. They are not needed forPoints
as the outer values are the outer bounds. But they can be specified manually forIntervals
Empty 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.11787 0.664642 0.0831822 0.528009 0.631878 0.990294
1.2 0.0905873 0.30391 0.552153 0.229792 0.29205 0.610173
1.4 0.495624 0.810377 0.578253 0.574678 0.371583 0.460775
1.6 0.263531 0.361536 0.753597 0.568763 0.564657 0.788252
1.8 0.388768 0.44818 0.0300922 0.896624 0.952489 0.216905
2.0 0.422318 0.499307 0.228081 0.823123 0.10454 0.931705
We 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.300304 0.660657 0.677543 0.712342 0.806495 0.727464
1.04 0.421525 0.835674 0.458427 0.248873 0.412078 0.455255
1.08 0.399781 0.824659 0.392334 0.675459 0.95213 0.91666
1.12 0.717006 0.623124 0.482654 0.964964 0.304266 0.313361
⋮ ⋱ ⋮
1.84 0.880853 0.578329 0.394576 0.255579 0.750947 0.27206
1.88 0.363918 0.533041 0.545522 0.516268 0.531012 0.503183
1.92 0.259597 0.358535 0.748884 0.575727 0.227865 0.865713
1.96 0.34589 0.94196 0.657054 0.728852 0.462859 0.767151
2.0 0.0143204 0.985407 0.476387 … 0.0417586 0.286 0.777391
And 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.727464 0.712342 0.14203 0.207735 0.677543 0.300304
1.2 0.815253 0.642187 0.49973 0.0387029 0.319496 0.887979
1.4 0.391307 0.197423 0.166425 0.48124 0.300285 0.0574938
1.6 0.49224 0.795274 0.24625 0.735391 0.699976 0.973586
1.8 0.539197 0.0661958 0.811917 0.822008 0.848624 0.930699
2.0 0.777391 0.0417586 0.873218 0.834418 0.476387 0.0143204
If 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.168617 0.417756 0.258764 0.195897 0.747615
1.007 0.395053 0.994295 0.770101 0.403836 0.891596
1.014 0.144422 0.370454 0.878565 0.753744 0.0149561
1.021 0.938771 0.917143 0.679296 0.180397 0.436168
⋮ ⋱
1.966 0.454047 0.92977 0.662833 0.160403 0.276742
1.973 0.846736 0.0923954 0.43915 0.405912 0.245638
1.98 0.946655 0.0506805 0.0821229 0.0787968 0.613895
1.987 0.00686195 0.266013 0.485952 0.193179 0.0628947
1.994 0.84475 0.574619 … 0.0760101 0.950007 0.662413
julia> 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.168617 0.397386 0.133475 0.0630398 0.247976 0.145293
1.203 0.233685 0.243195 0.331147 0.584048 0.717841 0.832538
1.399 0.0164892 0.729706 0.0315018 0.899356 0.847872 0.358513
1.602 0.0426808 0.61781 0.808889 0.140817 0.188976 0.054083
1.798 0.226699 0.0983553 0.661444 0.750622 0.650842 0.986606
1.994 0.84475 0.366873 0.734751 0.590953 0.621527 0.23502