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.306672 0.222512 0.904699
1.2 0.0849378 0.822296 0.995188
1.4 0.204592 0.78133 0.770918
1.6 0.400533 0.815238 0.481081
1.8 0.404487 0.33106 0.129202
2.0 0.837224 0.304495 0.820764
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.9951881331087541
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.306672 0.222512 0.904699
1.2 0.0849378 0.822296 0.995188
1.4 0.204592 0.78133 0.770918
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.0849378 0.995188
1.4 0.204592 0.770918
Lookups
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.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 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.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.349688 0.298715 0.948931 0.881456 0.467612
20 0.659465 0.424456 0.403465 0.817397 0.611375
30 0.912554 0.264652 0.794807 0.198741 0.25366
40 0.605377 0.596662 0.811628 0.225126 0.988402
50 0.578195 0.722197 0.446594 0.0705267 0.533796
60 0.237075 0.267916 0.395985 0.459872 0.636845
70 0.531982 0.323476 0.56009 0.013092 0.841307
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.460785 0.303917 0.296085 0.849304 0.172701 0.322996
1.2 0.0480609 0.415404 0.882681 0.571661 0.904981 0.286717
1.4 0.838069 0.706012 0.105789 0.187893 0.4305 0.949062
1.6 0.572779 0.531017 0.604703 0.577863 0.795417 0.128296
1.8 0.554127 0.131096 0.25044 0.0438203 0.43751 0.240143
2.0 0.108289 0.978098 0.950874 0.644413 0.576141 0.152358
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 … 12 11 10
1.0 0.0878505 0.596072 0.361001 0.0179131 0.117734
1.04 0.189864 0.145234 0.820535 0.654841 0.424499
1.08 0.709749 0.417918 0.0973258 0.928181 0.459373
1.12 0.342904 0.214021 0.866582 0.0772728 0.950371
⋮ ⋱ ⋮
1.84 0.267347 0.934997 0.205882 0.474625 0.969167
1.88 0.633411 0.955582 0.862519 0.902328 0.57748
1.92 0.136487 0.926927 0.406194 0.416786 0.381632
1.96 0.435483 0.0974933 0.251633 0.612243 0.107379
2.0 0.975789 0.191469 … 0.230977 0.179315 0.55567
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.117734 0.361001 0.880507 0.732267 0.457524 0.0878505
1.2 0.774016 0.62432 0.949827 0.847044 0.746195 0.299106
1.4 0.95638 0.536896 0.281963 0.833729 0.197068 0.633219
1.6 0.443287 0.294259 0.133365 0.194661 0.414141 0.44967
1.8 0.285757 0.422381 0.770465 0.955679 0.0400391 0.605696
2.0 0.55567 0.230977 0.8074 0.424142 0.079086 0.975789
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.027392 0.475206 0.442737 0.71882 0.268133
1.007 0.777812 0.758844 0.840186 0.744444 0.894533
1.014 0.499696 0.983274 0.754009 0.117349 0.117161
1.021 0.726062 0.227475 0.127854 0.947295 0.974554
⋮ ⋱
1.966 0.916746 0.487089 0.668646 0.340623 0.172196
1.973 0.551353 0.69931 0.355556 0.600864 0.394367
1.98 0.631073 0.613892 0.731778 0.00318179 0.477357
1.987 0.40657 0.288068 0.83582 0.446915 0.811094
1.994 0.0225737 0.237817 … 0.68825 0.870645 0.752698
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.027392 0.147893 0.98375 0.275406 0.313695 0.146932
1.203 0.972964 0.939529 0.970686 0.925354 0.944288 0.999473
1.399 0.336155 0.99802 0.63638 0.956171 0.674424 0.637422
1.602 0.13231 0.370889 0.666647 0.386555 0.811893 0.726467
1.798 0.933549 0.742484 0.518088 0.271098 0.0648605 0.840037
1.994 0.0225737 0.765788 0.351885 0.190948 0.726437 0.796689