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 DimStacks. Currently, @d does not work on DimStack.
Example: scaling along the time dimension
Define some dimensions:
using DimensionalData
using Dates
using Statisticsjulia> 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 12And 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.775377 0.59534 0.743848 0.535567 0.991357 0.878324
2 0.576903 0.637446 0.375855 0.147241 0.637288 0.20265
3 0.534915 0.982761 0.610407 0.353647 0.0403772 0.886217
⋮ ⋱ ⋮
97 0.165626 0.671548 0.968645 0.793024 0.463557 0.738568
98 0.507123 0.887135 0.0953293 0.141802 0.905626 0.0460473
99 0.825027 0.346344 0.951313 0.826774 0.351722 0.12355
100 0.887804 0.290569 0.048901 … 0.533551 0.590203 0.194993A regular broadcast fails:
julia> scaled = data .* month_scalarsERROR: DimensionMismatch: arrays could not be broadcast to a common size: a has axes Base.OneTo(100) and b has axes 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.775377 0.59534 0.743848 0.535567 0.991357 0.878324
2 0.576903 0.637446 0.375855 0.147241 0.637288 0.20265
3 0.534915 0.982761 0.610407 0.353647 0.0403772 0.886217
⋮ ⋱ ⋮
97 0.165626 0.671548 0.968645 0.793024 0.463557 0.738568
98 0.507123 0.887135 0.0953293 0.141802 0.905626 0.0460473
99 0.825027 0.346344 0.951313 0.826774 0.351722 0.12355
100 0.887804 0.290569 0.048901 … 0.533551 0.590203 0.194993We 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.509149
2000-02-01T00:00:00 0.50037
2000-03-01T00:00:00 0.494141
2000-04-01T00:00:00 0.490035
2000-05-01T00:00:00 0.499786
⋮
2000-08-01T00:00:00 0.504722
2000-09-01T00:00:00 0.500603
2000-10-01T00:00:00 0.49601
2000-11-01T00:00:00 0.503478
2000-12-01T00:00:00 0.489599julia> 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.509149
2000-02-01T00:00:00 1.00074
2000-03-01T00:00:00 1.48242
2000-04-01T00:00:00 1.96014
2000-05-01T00:00:00 2.49893
⋮
2000-08-01T00:00:00 4.03778
2000-09-01T00:00:00 4.50543
2000-10-01T00:00:00 4.9601
2000-11-01T00:00:00 5.53826
2000-12-01T00:00:00 5.87519You 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.775377 0.59534 0.743848 0.535567 0.991357 0.878324
2 0.576903 0.637446 0.375855 0.147241 0.637288 0.20265
3 0.534915 0.982761 0.610407 0.353647 0.0403772 0.886217
⋮ ⋱ ⋮
97 0.165626 0.671548 0.968645 0.793024 0.463557 0.738568
98 0.507123 0.887135 0.0953293 0.141802 0.905626 0.0460473
99 0.825027 0.346344 0.951313 0.826774 0.351722 0.12355
100 0.887804 0.290569 0.048901 … 0.533551 0.590203 0.194993And 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 … 98 99 100
2000-01-01T00:00:00 0.775377 0.507123 0.825027 0.887804
2000-02-01T00:00:00 0.573513 1.09029 0.0156437 1.92325
2000-03-01T00:00:00 0.700354 0.486974 2.57895 0.963306
⋮ ⋱ ⋮
2000-09-01T00:00:00 8.75144 0.00175277 1.86087 2.1219
2000-10-01T00:00:00 6.09013 … 5.58649 5.7329 6.64644
2000-11-01T00:00:00 8.74168 0.851296 8.57574 1.27239
2000-12-01T00:00:00 3.69662 7.31219 0.810321 6.09965Or
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 … 98 99 100
2000-01-01T00:00:00 0.775377 0.507123 0.825027 0.887804
2000-02-01T00:00:00 0.573513 1.09029 0.0156437 1.92325
2000-03-01T00:00:00 0.700354 0.486974 2.57895 0.963306
⋮ ⋱ ⋮
2000-09-01T00:00:00 8.75144 0.00175277 1.86087 2.1219
2000-10-01T00:00:00 6.09013 … 5.58649 5.7329 6.64644
2000-11-01T00:00:00 8.74168 0.851296 8.57574 1.27239
2000-12-01T00:00:00 3.69662 7.31219 0.810321 6.09965