Skip to content

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:

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, :b, :c] ForwardOrdered
└─────────────────────────────────────────────────────────────────┘
   :a        :b        :c
 1.0  0.326233  0.990413  0.935376
 1.2  0.108302  0.349911  0.734743
 1.4  0.261777  0.288584  0.374869
 1.6  0.498284  0.882119  0.0986562
 1.8  0.452644  0.658879  0.631835
 2.0  0.863666  0.447222  0.960528

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

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.2, 1.4] ForwardOrdered Irregular Points,
Y Categorical{Symbol} [:a, :b, :c] ForwardOrdered
└───────────────────────────────────────────────────────────────────────┘
   :a        :b        :c
 1.0  0.326233  0.990413  0.935376
 1.2  0.108302  0.349911  0.734743
 1.4  0.261777  0.288584  0.374869

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.108302  0.734743
 1.4  0.261777  0.374869

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.

julia
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
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
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, :b, :c, :d, :e] ForwardOrdered
└─────────────────────────────────────────────────────────────┘
   :a          :b        :c         :d        :e
 10    0.178133    0.743254  0.10062    0.846349  0.987453
 20    0.511853    0.136949  0.0497404  0.986941  0.673953
 30    0.00708901  0.28477   0.699157   0.860895  0.0399487
 40    0.181422    0.806089  0.473333   0.763555  0.220075
 50    0.31963     0.52929   0.242921   0.640734  0.767884
 60    0.459656    0.988727  0.486461   0.31077   0.601944
 70    0.546344    0.974689  0.549562   0.515751  0.650012

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 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.108916   0.314655    0.405311   0.0050131   0.130744   0.96537
 1.2   0.306098   0.0657522   0.583985   0.246167    0.418913   0.349432
 1.4   0.461462   0.530948    0.161922   0.535831    0.379436   0.234367
 1.6   0.986765   0.0128207   0.435801   0.274429    0.36289    0.664298
 1.8   0.113851   0.765082    0.833051   0.866351    0.382175   0.280626
 2.0   0.759907   0.250441    0.15832    0.501557    0.513543   0.683966

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.771765   0.37514        0.227886    0.229074     0.520176
 1.04   0.86593    0.0937724      0.859019    0.458953     0.988666
 1.08   0.83944    0.375688       0.0741439   0.421614     0.207881
 1.12   0.560078   0.917015       0.155101    0.591209     0.752366
 ⋮                            ⋱               ⋮
 1.84   0.600387   0.742414       0.11767     0.419243     0.0539546
 1.88   0.370554   0.430453       0.5749      0.713012     0.0142866
 1.92   0.330526   0.0511722      0.168252    0.838284     0.830319
 1.96   0.783769   0.901774       0.323239    0.209392     0.265751
 2.0    0.649481   0.151858   …   0.297532    0.00842093   0.88361

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, 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.520176   0.227886   0.745055   0.371903    0.339148    0.771765
 1.2   0.360512   0.5003     0.211778   0.292802    0.876074    0.672295
 1.4   0.165564   0.109776   0.848887   0.059169    0.0212502   0.563006
 1.6   0.320158   0.717813   0.449829   0.358835    0.442382    0.570849
 1.8   0.224691   0.233153   0.197464   0.153444    0.843981    0.543613
 2.0   0.88361    0.297532   0.554806   0.0400377   0.419721    0.649481

If the lookups aren't aligned we can use Near instead of At, which 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.791319    0.872067       0.157379   0.577245     0.970567
 1.007   0.351792    0.134748       0.104144   0.438601     0.218088
 1.014   0.427727    0.268779       0.994888   0.00791864   0.470339
 1.021   0.0403134   0.0819136      0.270471   0.20022      0.502738
 ⋮                              ⋱
 1.966   0.351997    0.130843       0.269909   0.112796     0.220418
 1.973   0.217465    0.446297       0.924343   0.993606     0.693792
 1.98    0.577464    0.494652       0.771003   0.825141     0.0106204
 1.987   0.0925266   0.0961501      0.665117   0.176917     0.821553
 1.994   0.575325    0.723555   …   0.847834   0.515887     0.624672
julia
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.791319   0.860737    0.441241    0.677328   0.856379   0.451773
 1.203   0.317878   0.147681    0.417339    0.302546   0.177366   0.273528
 1.399   0.25767    0.373657    0.155466    0.286167   0.292758   0.139033
 1.602   0.528083   0.357062    0.0978214   0.155566   0.128301   0.0510312
 1.798   0.99433    0.0147696   0.80479     0.200087   0.358111   0.387175
 1.994   0.575325   0.266876    0.95302     0.712358   0.373811   0.97809