Skip to content

Commit

Permalink
more map => maplayers
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaqz committed Nov 1, 2024
1 parent 9741a8a commit cc4316e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/DimensionalData.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ export dims, refdims, metadata, name, lookup, bounds, val, layers
export dimnum, hasdim, hasselection, otherdims

# utils
export set, rebuild, reorder, modify, broadcast_dims, broadcast_dims!, mergedims, unmergedims
export set, rebuild, reorder, modify, broadcast_dims, broadcast_dims!,
mergedims, unmergedims, maplayers

export groupby, seasons, months, hours, intervals, ranges


export @d

const DD = DimensionalData
Expand Down
4 changes: 2 additions & 2 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ modify(CuArray, A)
This also works for all the data layers in a `DimStack`.
"""
function modify end
modify(f, s::AbstractDimStack) = map(a -> modify(f, a), s)
modify(f, s::AbstractDimStack) = maplayers(a -> modify(f, a), s)
# Stack optimisation to avoid compilation to build all the `AbstractDimArray`
# layers, and instead just modify the parent data directly.
modify(f, s::AbstractDimStack{<:NamedTuple}) =
modify(f, s::AbstractDimStack{<:Any,<:NamedTuple}) =
rebuild(s; data=map(a -> modify(f, a), parent(s)))
function modify(f, A::AbstractDimArray)
newdata = f(parent(A))
Expand Down
19 changes: 10 additions & 9 deletions test/stack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ end
end

@testset "similar" begin
@test all(map(similar(mixed), mixed) do s, m
@test all(maplayers(similar(mixed), mixed) do s, m
dims(s) == dims(m) && dims(s) === dims(m) && eltype(s) === eltype(m)
end)
@test eltype(similar(s, Int)) === @NamedTuple{one::Int, two::Int, three::Int}
Expand Down Expand Up @@ -221,14 +221,15 @@ end
end
end

@testset "map" begin
@test values(map(a -> a .* 2, s))[1] == values(DimStack(2da1, 2da2, 2da3))[1]
@test dims(map(a -> a .* 2, s)) == dims(DimStack(2da1, 2da2, 2da3))
@test map(a -> a[1], s) == (one=1.0, two=2.0, three=3.0)
@test values(map(a -> a .* 2, s)) == values(DimStack(2da1, 2da2, 2da3))
@test map(+, s, s, s) == map(a -> a .* 3, s)
@test_throws ArgumentError map(+, s, mixed)
@test map((s, a) -> s => a[1], (one="one", two="two", three="three"), s) == (one="one" => 1.0, two="two" => 2.0, three="three" => 3.0)
@testset "maplayers" begin
@test values(maplayers(a -> a .* 2, s))[1] == values(DimStack(2da1, 2da2, 2da3))[1]
@test dims(maplayers(a -> a .* 2, s)) == dims(DimStack(2da1, 2da2, 2da3))
@test maplayers(a -> a[1], s) == (one=1.0, two=2.0, three=3.0)
@test values(maplayers(a -> a .* 2, s)) == values(DimStack(2da1, 2da2, 2da3))
@test maplayers(+, s, s, s) == maplayers(a -> a .* 3, s)
@test_throws ArgumentError maplayers(+, s, mixed)
@test maplayers((s, a) -> s => a[1], (one="one", two="two", three="three"), s) ==
(one="one" => 1.0, two="two" => 2.0, three="three" => 3.0)
end

@testset "methods with no arguments" begin
Expand Down

0 comments on commit cc4316e

Please sign in to comment.