Skip to content

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:

julia
using DimensionalData
julia
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, …, :c] ForwardOrdered
└──────────────────────────────────────────────────────────────────┘
   :a         :b         :c
 1.0  0.0775482  0.263531   0.361536
 1.2  0.838572   0.388768   0.44818
 1.4  0.166221   0.0689895  0.499307
 1.6  0.11787    0.143478   0.0831822
 1.8  0.0905873  0.909826   0.747695
 2.0  0.495624   0.810377   0.993001

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
julia> A[X=At(1.2), Y=At(:c)]
0.4481804490387552

Or within a tolerance:

julia
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.4] ForwardOrdered Irregular Points,
Y Categorical{Symbol} [:a, …, :c] ForwardOrdered
└──────────────────────────────────────────────────────────────────────┘
   :a         :b         :c
 1.0  0.0775482  0.263531   0.361536
 1.2  0.838572   0.388768   0.44818
 1.4  0.166221   0.0689895  0.499307

At can also take vectors and ranges:

julia
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.838572  0.44818
 1.4  0.166221  0.499307

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.

julia
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
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
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
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, …, :e] ForwardOrdered
└─────────────────────────────────────────────────────────────┘
   :a         :b        :c         :d        :e
 10    0.201353   0.399781  0.300696   0.895029  0.988013
 20    0.0953976  0.350857  0.0574938  0.6063    0.34589
 30    0.751498   0.98443   0.376305   0.752331  0.0143204
 40    0.241791   0.477282  0.72433    0.940909  0.660657
 50    0.412631   0.199523  0.20951    0.33945   0.750244
 60    0.300304   0.998484  0.586002   0.744107  0.396071
 70    0.421525   0.935845  0.0366225  0.635373  0.0255364

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, and Char are set to Categorical lookup.

    • order is detected as Unordered, ForwardOrdered, or ReverseOrdered
  • Arrays and ranges of Number, DateTime, and other things are set to Sampled lookups.

    • order is detected as Unordered, ForwardOrdered, or ReverseOrdered.

    • sampling is set to Points() unless the values are IntervalSets.Interval, then Intervals(Center()) is used.

    • span is detected as Regular(step(range)) for AbstractRange and Irregular(nothing, nothing) for other AbstractArray, where nothing, nothing are the unknown outer bounds of the lookup. They are not needed for Points as the outer values are the outer bounds. But they can be specified manually for Intervals

    • 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
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.163673    0.275784   0.924846    0.842552    0.0199668   0.389598
 1.2   0.418138    0.659836   0.31628     0.565883    0.789971    0.8698
 1.4   0.105474    0.129616   0.0979352   0.0329394   0.0251457   0.1435
 1.6   0.606746    0.171665   0.25853     0.106544    0.249439    0.0422736
 1.8   0.0461633   0.790418   0.0865935   0.935937    0.935022    0.704105
 2.0   0.341785    0.113361   0.658199    0.745734    0.19689     0.574623

We can define another array with partly matching indices

julia
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          1912          11         10
 1.0    0.252529    0.817575      0.151566    0.469419   0.0897522
 1.04   0.0174135   0.214157      0.1361      0.337622   0.496281
 1.08   0.20259     0.389215      0.828158    0.665963   0.287951
 1.12   0.717768    0.602631      0.0778196   0.165869   0.462785
 ⋮                            ⋱               ⋮
 1.88   0.93358     0.551807      0.961159    0.696748   0.705701
 1.92   0.474535    0.839126      0.901873    0.123899   0.351735
 1.96   0.696515    0.187655  …   0.570644    0.28849    0.669192
 2.0    0.510649    0.488136      0.356727    0.57266    0.714084

And we can simply select values from B with selectors from A:

julia
julia> B[DimSelectors(A)]
6×6 DimArray{Float64, 2}
├──────────────────────────┴───────────────────────────────────── dims ┐
X Sampled{Float64} [1.0, …, 2.0] ForwardOrdered Irregular Points,
Y Sampled{Int64} [10, …, 20] ReverseOrdered Irregular Points
└──────────────────────────────────────────────────────────────────────┘
  10           12         14         16          18          20
 1.0   0.0897522    0.151566   0.853412   0.815169    0.947461    0.252529
 1.2   0.309883     0.423461   0.379056   0.192195    0.598739    0.111155
 1.4   0.0256304    0.900204   0.129154   0.733844    0.353698    0.103636
 1.6   0.00627723   0.680215   0.956535   0.952562    0.725889    0.87656
 1.8   0.661703     0.828917   0.779987   0.0395099   0.748589    0.780424
 2.0   0.714084     0.356727   0.427859   0.252438    0.0545928   0.510649

If the lookups aren't aligned, we can use Near instead of At, which is like doing a nearest neighbor interpolation:

julia
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.928.0         28.9       29.8
 1.0     0.264124    0.850717      0.870758     0.91882    0.229304
 1.007   0.2302      0.379671      0.724234     0.321192   0.618342
 1.014   0.433895    0.940915      0.845704     0.329421   0.690575
 1.021   0.439712    0.817665      0.571564     0.66412    0.719345
 ⋮                             ⋱
 1.973   0.0481076   0.680195  …   0.84276      0.912721   0.130702
 1.98    0.783787    0.228507      0.539241     0.310082   0.980515
 1.987   0.864908    0.264195      0.955215     0.108017   0.0347435
 1.994   0.956692    0.586695      0.00757027   0.568242   0.133933
julia
julia> C[DimSelectors(A; selectors=Near)]
6×6 DimArray{Float64, 2}
├──────────────────────────┴─────────────────────────────────────── dims ┐
X Sampled{Float64} [1.0, …, 1.994] ForwardOrdered Irregular Points,
Y Sampled{Float64} [10.0, …, 19.9] ForwardOrdered Irregular Points
└────────────────────────────────────────────────────────────────────────┘
    10.0       11.8       13.6       16.3        18.1       19.9
 1.0     0.264124   0.18735    0.5857     0.197784    0.10342    0.709278
 1.203   0.503325   0.588857   0.386157   0.298074    0.774358   0.916907
 1.399   0.1871     0.869773   0.514602   0.43143     0.834496   0.560759
 1.602   0.721758   0.159226   0.179027   0.870966    0.200424   0.54068
 1.798   0.170931   0.228107   0.350355   0.0497595   0.434415   0.729139
 1.994   0.956692   0.849872   0.449971   0.660179    0.783966   0.679627