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 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.127082 0.973847 0.0943594
1.2 0.677743 0.406072 0.698462
1.4 0.822783 0.874735 0.698995
1.6 0.381692 0.751977 0.983399
1.8 0.992013 0.806006 0.319013
2.0 0.359758 0.376108 0.157175
Then 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.6984622051778979
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.127082 0.973847 0.0943594
1.2 0.677743 0.406072 0.698462
1.4 0.822783 0.874735 0.698995
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.677743 0.698462
1.4 0.822783 0.698995
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
The 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.0
To 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
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.444305 0.969079 0.101231 0.642658 0.522816
20 0.184738 0.764895 0.339858 0.679337 0.227694
30 0.772277 0.86273 0.973357 0.735544 0.389375
40 0.711133 0.748041 0.925367 0.976465 0.0898635
50 0.883222 0.621603 0.41767 0.48849 0.511313
60 0.802776 0.768488 0.594101 0.956886 0.165145
70 0.156538 0.869012 0.530389 0.00114293 0.87255
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 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.0928922 0.973622 0.229418 0.679453 0.21921 0.357367
1.2 0.441181 0.942925 0.228248 0.442111 0.506221 0.246886
1.4 0.621662 0.314906 0.749731 0.882656 0.680987 0.771237
1.6 0.72217 0.196478 0.201129 0.683795 0.396585 0.0429074
1.8 0.896257 0.791844 0.97293 0.12668 0.687921 0.870348
2.0 0.301659 0.758149 0.883323 0.575595 0.647225 0.825204
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.11787 0.371583 0.400001 0.92906 0.337296 0.760043
1.04 0.0905873 0.564657 0.986155 0.668806 0.466288 0.215999
1.08 0.495624 0.952489 0.397388 0.208304 0.515929 0.467332
1.12 0.263531 0.10454 0.074921 0.158368 0.624812 0.3926
⋮ ⋱ ⋮
1.84 0.568763 0.397873 0.953145 0.737346 0.545196 0.788252
1.88 0.896624 0.630782 0.298791 0.212246 0.320737 0.216905
1.92 0.823123 0.898833 0.542826 0.213848 0.312277 0.931705
1.96 0.631878 0.429465 0.109509 … 0.737151 0.5053 0.997569
2.0 0.29205 0.244582 0.499362 0.801242 0.328169 0.822161
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.760043 0.92906 0.122323 0.475301 0.400001 0.11787
1.2 0.651104 0.797969 0.244449 0.35128 0.586663 0.422318
1.4 0.0534248 0.760577 0.845805 0.326566 0.117547 0.44818
1.6 0.860352 0.525557 0.169812 0.713043 0.536294 0.753597
1.8 0.460775 0.952744 0.460204 0.41747 0.187648 0.574678
2.0 0.822161 0.801242 0.107466 0.246027 0.499362 0.29205
If 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.399781 0.148229 0.449093 0.560553 0.565202
1.007 0.717006 0.615703 0.925484 0.0485471 0.794437
1.014 0.661197 0.360751 0.739562 0.366935 0.923642
1.021 0.887979 0.0284535 0.352175 0.127118 0.639886
⋮ ⋱
1.966 0.294489 0.949143 0.39036 0.633501 0.531598
1.973 0.725774 0.525431 … 0.520799 0.961561 0.0889688
1.98 0.707629 0.640577 0.945549 0.67027 0.934843
1.987 0.271952 0.948532 0.27236 0.782344 0.93513
1.994 0.294534 0.680648 0.53422 0.906871 0.503183
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.399781 0.0646533 0.611333 0.198465 0.0887762 0.302922
1.203 0.594314 0.50095 0.315896 0.878116 0.728728 0.928246
1.399 0.819291 0.235618 0.535219 0.112537 0.390661 0.170889
1.602 0.482064 0.629542 0.893616 0.58833 0.182349 0.680387
1.798 0.690159 0.219552 0.580422 0.167206 0.640598 0.966742
1.994 0.294534 0.910144 0.490752 0.374164 0.395148 0.265639