Dimensional broadcasts with @d
and broadcast_dims
Broadcasting over AbstractDimArray works as usual with Base Julia broadcasts, except that dimensions are checked for compatibility with each other, and that values match. Strict checks can be turned off globally with strict_broadcast!(false)
. To avoid even dimension name checks, broadcast over parent(dimarray)
.
The @d
macro is a dimension-aware extension to regular dot broadcasting. broadcast_dims
is analogous to Base Julia's broadcast
.
Because we know the names of the dimensions, there is no ambiguity in which ones we mean to broadcast together. This means we can permute and reshape dims so that broadcasts that would fail with a regular Array
just work with a DimArray
.
As an added bonus, broadcast_dims
even works on DimStack
s. Currently, @d
does not work on DimStack
.
Example: scaling along the time dimension
Define some dimensions:
using DimensionalData
using Dates
using Statistics
julia> x, y, t = X(1:100), Y(1:25), Ti(DateTime(2000):Month(1):DateTime(2000, 12))
(↓ X 1:100,
→ Y 1:25,
↗ Ti DateTime("2000-01-01T00:00:00"):Month(1):DateTime("2000-12-01T00:00:00"))
A DimArray from 1:12 to scale with:
julia> month_scalars = DimArray(month, t)
┌ 12-element DimArray{Int64, 1} month(Ti) ┐
├─────────────────────────────────────────┴────────────────────────────── dims ┐
↓ Ti Sampled{DateTime} DateTime("2000-01-01T00:00:00"):Month(1):DateTime("2000-12-01T00:00:00") ForwardOrdered Regular Points
└──────────────────────────────────────────────────────────────────────────────┘
2000-01-01T00:00:00 1
2000-02-01T00:00:00 2
2000-03-01T00:00:00 3
2000-04-01T00:00:00 4
2000-05-01T00:00:00 5
⋮
2000-08-01T00:00:00 8
2000-09-01T00:00:00 9
2000-10-01T00:00:00 10
2000-11-01T00:00:00 11
2000-12-01T00:00:00 12
And a larger DimArray for example data:
julia> data = rand(x, y, t)
┌ 100×25×12 DimArray{Float64, 3} ┐
├────────────────────────────────┴─────────────────────────────────────── dims ┐
↓ X Sampled{Int64} 1:100 ForwardOrdered Regular Points,
→ Y Sampled{Int64} 1:25 ForwardOrdered Regular Points,
↗ Ti Sampled{DateTime} DateTime("2000-01-01T00:00:00"):Month(1):DateTime("2000-12-01T00:00:00") ForwardOrdered Regular Points
└──────────────────────────────────────────────────────────────────────────────┘
[:, :, 1]
↓ → 1 2 3 … 23 24 25
1 0.333692 0.46747 0.618895 0.808742 0.576437 0.657325
2 0.5207 0.95715 0.534996 0.25951 0.877483 0.287422
3 0.757128 0.978177 0.692047 0.274087 0.803685 0.933502
⋮ ⋱ ⋮
97 0.335082 0.14166 0.290357 0.393876 0.177009 0.826134
98 0.249064 0.0313839 0.0966582 0.857851 0.80082 0.547268
99 0.171798 0.0649413 0.671101 0.826641 0.259839 0.728112
100 0.380872 0.393821 0.314181 … 0.931262 0.616368 0.767844
A regular broadcast fails:
julia> scaled = data .* month_scalars
ERROR: DimensionMismatch: arrays could not be broadcast to a common size: a has axes X(Base.OneTo(100)) and b has axes Ti(Base.OneTo(12))
But @d
knows to broadcast over the Ti
dimension:
julia> scaled = @d data .* month_scalars
┌ 100×25×12 DimArray{Float64, 3} ┐
├────────────────────────────────┴─────────────────────────────────────── dims ┐
↓ X Sampled{Int64} 1:100 ForwardOrdered Regular Points,
→ Y Sampled{Int64} 1:25 ForwardOrdered Regular Points,
↗ Ti Sampled{DateTime} DateTime("2000-01-01T00:00:00"):Month(1):DateTime("2000-12-01T00:00:00") ForwardOrdered Regular Points
└──────────────────────────────────────────────────────────────────────────────┘
[:, :, 1]
↓ → 1 2 3 … 23 24 25
1 0.333692 0.46747 0.618895 0.808742 0.576437 0.657325
2 0.5207 0.95715 0.534996 0.25951 0.877483 0.287422
3 0.757128 0.978177 0.692047 0.274087 0.803685 0.933502
⋮ ⋱ ⋮
97 0.335082 0.14166 0.290357 0.393876 0.177009 0.826134
98 0.249064 0.0313839 0.0966582 0.857851 0.80082 0.547268
99 0.171798 0.0649413 0.671101 0.826641 0.259839 0.728112
100 0.380872 0.393821 0.314181 … 0.931262 0.616368 0.767844
We can see the means of each month are scaled by the broadcast :
julia> mean(eachslice(data; dims=(X, Y)))
┌ 12-element DimArray{Float64, 1} ┐
├─────────────────────────────────┴────────────────────────────────────── dims ┐
↓ Ti Sampled{DateTime} DateTime("2000-01-01T00:00:00"):Month(1):DateTime("2000-12-01T00:00:00") ForwardOrdered Regular Points
└──────────────────────────────────────────────────────────────────────────────┘
2000-01-01T00:00:00 0.501086
2000-02-01T00:00:00 0.50466
2000-03-01T00:00:00 0.498604
2000-04-01T00:00:00 0.503934
2000-05-01T00:00:00 0.497502
⋮
2000-08-01T00:00:00 0.491305
2000-09-01T00:00:00 0.501987
2000-10-01T00:00:00 0.512221
2000-11-01T00:00:00 0.506841
2000-12-01T00:00:00 0.503408
julia> mean(eachslice(scaled; dims=(X, Y)))
┌ 12-element DimArray{Float64, 1} ┐
├─────────────────────────────────┴────────────────────────────────────── dims ┐
↓ Ti Sampled{DateTime} DateTime("2000-01-01T00:00:00"):Month(1):DateTime("2000-12-01T00:00:00") ForwardOrdered Regular Points
└──────────────────────────────────────────────────────────────────────────────┘
2000-01-01T00:00:00 0.501086
2000-02-01T00:00:00 1.00932
2000-03-01T00:00:00 1.49581
2000-04-01T00:00:00 2.01574
2000-05-01T00:00:00 2.48751
⋮
2000-08-01T00:00:00 3.93044
2000-09-01T00:00:00 4.51789
2000-10-01T00:00:00 5.12221
2000-11-01T00:00:00 5.57525
2000-12-01T00:00:00 6.04089
You can also use broadcast_dims
the same way:
julia> broadcast_dims(*, data, month_scalars)
┌ 100×25×12 DimArray{Float64, 3} ┐
├────────────────────────────────┴─────────────────────────────────────── dims ┐
↓ X Sampled{Int64} 1:100 ForwardOrdered Regular Points,
→ Y Sampled{Int64} 1:25 ForwardOrdered Regular Points,
↗ Ti Sampled{DateTime} DateTime("2000-01-01T00:00:00"):Month(1):DateTime("2000-12-01T00:00:00") ForwardOrdered Regular Points
└──────────────────────────────────────────────────────────────────────────────┘
[:, :, 1]
↓ → 1 2 3 … 23 24 25
1 0.333692 0.46747 0.618895 0.808742 0.576437 0.657325
2 0.5207 0.95715 0.534996 0.25951 0.877483 0.287422
3 0.757128 0.978177 0.692047 0.274087 0.803685 0.933502
⋮ ⋱ ⋮
97 0.335082 0.14166 0.290357 0.393876 0.177009 0.826134
98 0.249064 0.0313839 0.0966582 0.857851 0.80082 0.547268
99 0.171798 0.0649413 0.671101 0.826641 0.259839 0.728112
100 0.380872 0.393821 0.314181 … 0.931262 0.616368 0.767844
And with the @d
macro you can set the dimension order and other properties of the output array, by passing a single assignment or a NamedTuple
argument to @d
after the broadcast:
julia> @d data .* month_scalars dims=(Ti, X, Y)
┌ 12×100×25 DimArray{Float64, 3} ┐
├────────────────────────────────┴─────────────────────────────────────── dims ┐
↓ Ti Sampled{DateTime} DateTime("2000-01-01T00:00:00"):Month(1):DateTime("2000-12-01T00:00:00") ForwardOrdered Regular Points,
→ X Sampled{Int64} 1:100 ForwardOrdered Regular Points,
↗ Y Sampled{Int64} 1:25 ForwardOrdered Regular Points
└──────────────────────────────────────────────────────────────────────────────┘
[:, :, 1]
↓ → 1 2 … 98 99 100
2000-01-01T00:00:00 0.333692 0.5207 0.249064 0.171798 0.380872
2000-02-01T00:00:00 1.32867 1.30479 1.91317 1.77189 1.86003
2000-03-01T00:00:00 2.96646 2.41472 0.411866 2.80454 0.726928
⋮ ⋱ ⋮
2000-09-01T00:00:00 3.85246 4.69114 7.54669 4.75139 2.80107
2000-10-01T00:00:00 9.27949 9.94204 … 1.02435 9.31906 0.507183
2000-11-01T00:00:00 8.06674 8.57906 9.12414 9.52623 7.20807
2000-12-01T00:00:00 9.36757 5.16825 2.69071 4.7913 3.15659
Or
julia> @d data .* month_scalars (dims=(Ti, X, Y), name=:scaled)
┌ 12×100×25 DimArray{Float64, 3} scaled ┐
├───────────────────────────────────────┴──────────────────────────────── dims ┐
↓ Ti Sampled{DateTime} DateTime("2000-01-01T00:00:00"):Month(1):DateTime("2000-12-01T00:00:00") ForwardOrdered Regular Points,
→ X Sampled{Int64} 1:100 ForwardOrdered Regular Points,
↗ Y Sampled{Int64} 1:25 ForwardOrdered Regular Points
└──────────────────────────────────────────────────────────────────────────────┘
[:, :, 1]
↓ → 1 2 … 98 99 100
2000-01-01T00:00:00 0.333692 0.5207 0.249064 0.171798 0.380872
2000-02-01T00:00:00 1.32867 1.30479 1.91317 1.77189 1.86003
2000-03-01T00:00:00 2.96646 2.41472 0.411866 2.80454 0.726928
⋮ ⋱ ⋮
2000-09-01T00:00:00 3.85246 4.69114 7.54669 4.75139 2.80107
2000-10-01T00:00:00 9.27949 9.94204 … 1.02435 9.31906 0.507183
2000-11-01T00:00:00 8.06674 8.57906 9.12414 9.52623 7.20807
2000-12-01T00:00:00 9.36757 5.16825 2.69071 4.7913 3.15659