diff --git a/src/show.jl b/src/show.jl index 5b4102c..a3d0bb9 100644 --- a/src/show.jl +++ b/src/show.jl @@ -37,10 +37,6 @@ _print_type_short(io, ::Type{<:ComponentArray{T,N,<:SubArray}}; color=:normal) w _print_type_short(io, ::Type{<:ComponentArray{T,1,<:SubArray}}; color=:normal) where {T} = printstyled(io, "ComponentVector{$T,SubArray...}"; color=color) _print_type_short(io, ::Type{<:ComponentArray{T,2,<:SubArray}}; color=:normal) where {T} = printstyled(io, "ComponentMatrix{$T,SubArray...}"; color=color) -@static if v"1.6" ≤ VERSION < v"1.8" - Base.print_type_stacktrace(io, CA::Type{<:ComponentArray}; color=:normal) = _print_type_short(io, CA; color=color) -end - function Base.show(io::IO, x::ComponentVector) print(io, "(") for (i,key) in enumerate(keys(x)) diff --git a/test/runtests.jl b/test/runtests.jl index f281f61..863bcfe 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -725,132 +725,130 @@ end # Make sure we aren't doing type piracy on `reshape` @test ndims(dropdims(ones(1,1), dims=(1,2))) == 0 - if VERSION >= v"1.9" - # `stack` was introduced in Julia 1.9 - # Issue #254 - x = ComponentVector(a=[1, 2]) - y = ComponentVector(a=[3, 4]) - xy = stack([x, y]) - # The data in `xy` should be the same as what we'd get if we used plain Vectors: - @test getdata(xy) == stack(getdata.([x, y])) - # Check the axes. - xy_ax = getaxes(xy) - # Should have two axes since xy should be a ComponentMatrix. - @test length(xy_ax) == 2 - # First axis should be the same as x. - @test xy_ax[1] == only(getaxes(x)) - # Second axis should be a FlatAxis. - @test xy_ax[2] == FlatAxis() - - # Does the dims argument to stack work? - # Using `dims=2` should be the same as the default value. - xy2 = stack([x, y]; dims=2) - @test xy2 == xy - # Using `dims=1` should stack things vertically. - xy3 = stack([x, y]; dims=1) - @test all(xy3[1, :a] .== xy[:a, 1]) - @test all(xy3[2, :a] .== xy[:a, 2]) - - # But can we stack 2D arrays? - x = ComponentVector(a=[1, 2]) - y = ComponentVector(b=[3, 4]) - X = x .* y' - Y = x .* y' .+ 4 - XY = stack([X, Y]) - # The data in `XY` should be the same as what we'd get if we used plain Vectors: - @test getdata(XY) == stack(getdata.([X, Y])) - # Check the axes. - XY_ax = getaxes(XY) - # Should have three axes since XY should be a 3D ComponentArray. - @test length(XY_ax) == 3 - # First two axes should be the same as XY. - @test XY_ax[1] == getaxes(XY)[1] - @test XY_ax[2] == getaxes(XY)[2] - # Third should be a FlatAxis. - @test XY_ax[3] == FlatAxis() - # Should test indexing too. - @test all(XY[:a, :b, 1] .== X) - @test all(XY[:a, :b, 2] .== Y) - - # Make sure the dims argument works. - # Using `dims=3` should be the same as the default value. - XY_d3 = stack([X, Y]; dims=3) - @test XY_d3 == XY - # Using `dims=2` stacks along the second axis. - XY_d2 = stack([X, Y]; dims=2) - @test all(XY_d2[:a, 1, :b] .== XY[:a, :b, 1]) - @test all(XY_d2[:a, 2, :b] .== XY[:a, :b, 2]) - # Using `dims=1` stacks along the first axis. - XY_d1 = stack([X, Y]; dims=1) - @test all(XY_d1[1, :a, :b] .== XY[:a, :b, 1]) - @test all(XY_d1[2, :a, :b] .== XY[:a, :b, 2]) - - # Issue #254, tuple of arrays: - x = ComponentVector(a=[1, 2]) - y = ComponentVector(b=[3, 4]) - Xstack1 = stack((x, y, x); dims=1) - Xstack1_noca = stack((getdata(x), getdata(y), getdata(x)); dims=1) - @test all(Xstack1 .== Xstack1_noca) - @test all(Xstack1[1, :a] .== Xstack1_noca[1, :]) - @test all(Xstack1[2, :a] .== Xstack1_noca[2, :]) - - # Issue #254, Array of tuples. - Xstack2 = stack(ComponentArray(a=(1,2,3), b=(4,5,6))) - Xstack2_noca = stack([(1,2,3), (4,5,6)]) - @test all(Xstack2 .== Xstack2_noca) - @test all(Xstack2[:, :a] .== Xstack2_noca[:, 1]) - @test all(Xstack2[:, :b] .== Xstack2_noca[:, 2]) - - Xstack2_d1 = stack(ComponentArray(a=(1,2,3), b=(4,5,6)); dims=1) - Xstack2_noca_d1 = stack([(1,2,3), (4,5,6)]; dims=1) - @test all(Xstack2_d1 .== Xstack2_noca_d1) - @test all(Xstack2_d1[:a, :] .== Xstack2_noca_d1[1, :]) - @test all(Xstack2_d1[:b, :] .== Xstack2_noca_d1[2, :]) - - # Issue #254, generator of arrays. - Xstack3 = stack(ComponentArray(z=[x,x]) for x in 1:4) - Xstack3_noca = stack([x, x] for x in 1:4) - # That should give me - # [1 2 3 4; - # 1 2 3 4] - @test all(Xstack3 .== Xstack3_noca) - @test all(Xstack3[:z, 1] .== Xstack3_noca[:, 1]) - @test all(Xstack3[:z, 2] .== Xstack3_noca[:, 2]) - @test all(Xstack3[:z, 3] .== Xstack3_noca[:, 3]) - @test all(Xstack3[:z, 4] .== Xstack3_noca[:, 4]) - - Xstack3_d1 = stack(ComponentArray(z=[x,x]) for x in 1:4; dims=1) - Xstack3_noca_d1 = stack([x, x] for x in 1:4; dims=1) - # That should give me - # [1 1; - # 2 2; - # 3 3; - # 4 4;] - @test all(Xstack3_d1 .== Xstack3_noca_d1) - @test all(Xstack3_d1[1, :z] .== Xstack3_noca_d1[1, :]) - @test all(Xstack3_d1[2, :z] .== Xstack3_noca_d1[2, :]) - @test all(Xstack3_d1[3, :z] .== Xstack3_noca_d1[3, :]) - @test all(Xstack3_d1[4, :z] .== Xstack3_noca_d1[4, :]) - - # Issue #254, map then stack. - Xstack4_d1 = stack(x -> ComponentArray(a=x, b=[x+1,x+2]), [5 6; 7 8]; dims=1) # map then stack - Xstack4_noca_d1 = stack(x -> [x, x+1, x+2], [5 6; 7 8]; dims=1) # map then stack - @test all(Xstack4_d1 .== Xstack4_noca_d1) - @test all(Xstack4_d1[:, :a] .== Xstack4_noca_d1[:, 1]) - @test all(Xstack4_d1[:, :b] .== Xstack4_noca_d1[:, 2:3]) - - Xstack4_d2 = stack(x -> ComponentArray(a=x, b=[x+1,x+2]), [5 6; 7 8]; dims=2) # map then stack - Xstack4_noca_d2 = stack(x -> [x, x+1, x+2], [5 6; 7 8]; dims=2) # map then stack - @test all(Xstack4_d2 .== Xstack4_noca_d2) - @test all(Xstack4_d2[:a, :] .== Xstack4_noca_d2[1, :]) - @test all(Xstack4_d2[:b, :] .== Xstack4_noca_d2[2:3, :]) - - Xstack4_dcolon = stack(x -> ComponentArray(a=x, b=[x+1,x+2]), [5 6; 7 8]; dims=:) # map then stack - Xstack4_noca_dcolon = stack(x -> [x, x+1, x+2], [5 6; 7 8]; dims=:) # map then stack - @test all(Xstack4_dcolon .== Xstack4_noca_dcolon) - @test all(Xstack4_dcolon[:a, :, :] .== Xstack4_noca_dcolon[1, :, :]) - @test all(Xstack4_dcolon[:b, :, :] .== Xstack4_noca_dcolon[2:3, :, :]) - end + # `stack` was introduced in Julia 1.9 + # Issue #254 + x = ComponentVector(a=[1, 2]) + y = ComponentVector(a=[3, 4]) + xy = stack([x, y]) + # The data in `xy` should be the same as what we'd get if we used plain Vectors: + @test getdata(xy) == stack(getdata.([x, y])) + # Check the axes. + xy_ax = getaxes(xy) + # Should have two axes since xy should be a ComponentMatrix. + @test length(xy_ax) == 2 + # First axis should be the same as x. + @test xy_ax[1] == only(getaxes(x)) + # Second axis should be a FlatAxis. + @test xy_ax[2] == FlatAxis() + + # Does the dims argument to stack work? + # Using `dims=2` should be the same as the default value. + xy2 = stack([x, y]; dims=2) + @test xy2 == xy + # Using `dims=1` should stack things vertically. + xy3 = stack([x, y]; dims=1) + @test all(xy3[1, :a] .== xy[:a, 1]) + @test all(xy3[2, :a] .== xy[:a, 2]) + + # But can we stack 2D arrays? + x = ComponentVector(a=[1, 2]) + y = ComponentVector(b=[3, 4]) + X = x .* y' + Y = x .* y' .+ 4 + XY = stack([X, Y]) + # The data in `XY` should be the same as what we'd get if we used plain Vectors: + @test getdata(XY) == stack(getdata.([X, Y])) + # Check the axes. + XY_ax = getaxes(XY) + # Should have three axes since XY should be a 3D ComponentArray. + @test length(XY_ax) == 3 + # First two axes should be the same as XY. + @test XY_ax[1] == getaxes(XY)[1] + @test XY_ax[2] == getaxes(XY)[2] + # Third should be a FlatAxis. + @test XY_ax[3] == FlatAxis() + # Should test indexing too. + @test all(XY[:a, :b, 1] .== X) + @test all(XY[:a, :b, 2] .== Y) + + # Make sure the dims argument works. + # Using `dims=3` should be the same as the default value. + XY_d3 = stack([X, Y]; dims=3) + @test XY_d3 == XY + # Using `dims=2` stacks along the second axis. + XY_d2 = stack([X, Y]; dims=2) + @test all(XY_d2[:a, 1, :b] .== XY[:a, :b, 1]) + @test all(XY_d2[:a, 2, :b] .== XY[:a, :b, 2]) + # Using `dims=1` stacks along the first axis. + XY_d1 = stack([X, Y]; dims=1) + @test all(XY_d1[1, :a, :b] .== XY[:a, :b, 1]) + @test all(XY_d1[2, :a, :b] .== XY[:a, :b, 2]) + + # Issue #254, tuple of arrays: + x = ComponentVector(a=[1, 2]) + y = ComponentVector(b=[3, 4]) + Xstack1 = stack((x, y, x); dims=1) + Xstack1_noca = stack((getdata(x), getdata(y), getdata(x)); dims=1) + @test all(Xstack1 .== Xstack1_noca) + @test all(Xstack1[1, :a] .== Xstack1_noca[1, :]) + @test all(Xstack1[2, :a] .== Xstack1_noca[2, :]) + + # Issue #254, Array of tuples. + Xstack2 = stack(ComponentArray(a=(1,2,3), b=(4,5,6))) + Xstack2_noca = stack([(1,2,3), (4,5,6)]) + @test all(Xstack2 .== Xstack2_noca) + @test all(Xstack2[:, :a] .== Xstack2_noca[:, 1]) + @test all(Xstack2[:, :b] .== Xstack2_noca[:, 2]) + + Xstack2_d1 = stack(ComponentArray(a=(1,2,3), b=(4,5,6)); dims=1) + Xstack2_noca_d1 = stack([(1,2,3), (4,5,6)]; dims=1) + @test all(Xstack2_d1 .== Xstack2_noca_d1) + @test all(Xstack2_d1[:a, :] .== Xstack2_noca_d1[1, :]) + @test all(Xstack2_d1[:b, :] .== Xstack2_noca_d1[2, :]) + + # Issue #254, generator of arrays. + Xstack3 = stack(ComponentArray(z=[x,x]) for x in 1:4) + Xstack3_noca = stack([x, x] for x in 1:4) + # That should give me + # [1 2 3 4; + # 1 2 3 4] + @test all(Xstack3 .== Xstack3_noca) + @test all(Xstack3[:z, 1] .== Xstack3_noca[:, 1]) + @test all(Xstack3[:z, 2] .== Xstack3_noca[:, 2]) + @test all(Xstack3[:z, 3] .== Xstack3_noca[:, 3]) + @test all(Xstack3[:z, 4] .== Xstack3_noca[:, 4]) + + Xstack3_d1 = stack(ComponentArray(z=[x,x]) for x in 1:4; dims=1) + Xstack3_noca_d1 = stack([x, x] for x in 1:4; dims=1) + # That should give me + # [1 1; + # 2 2; + # 3 3; + # 4 4;] + @test all(Xstack3_d1 .== Xstack3_noca_d1) + @test all(Xstack3_d1[1, :z] .== Xstack3_noca_d1[1, :]) + @test all(Xstack3_d1[2, :z] .== Xstack3_noca_d1[2, :]) + @test all(Xstack3_d1[3, :z] .== Xstack3_noca_d1[3, :]) + @test all(Xstack3_d1[4, :z] .== Xstack3_noca_d1[4, :]) + + # Issue #254, map then stack. + Xstack4_d1 = stack(x -> ComponentArray(a=x, b=[x+1,x+2]), [5 6; 7 8]; dims=1) # map then stack + Xstack4_noca_d1 = stack(x -> [x, x+1, x+2], [5 6; 7 8]; dims=1) # map then stack + @test all(Xstack4_d1 .== Xstack4_noca_d1) + @test all(Xstack4_d1[:, :a] .== Xstack4_noca_d1[:, 1]) + @test all(Xstack4_d1[:, :b] .== Xstack4_noca_d1[:, 2:3]) + + Xstack4_d2 = stack(x -> ComponentArray(a=x, b=[x+1,x+2]), [5 6; 7 8]; dims=2) # map then stack + Xstack4_noca_d2 = stack(x -> [x, x+1, x+2], [5 6; 7 8]; dims=2) # map then stack + @test all(Xstack4_d2 .== Xstack4_noca_d2) + @test all(Xstack4_d2[:a, :] .== Xstack4_noca_d2[1, :]) + @test all(Xstack4_d2[:b, :] .== Xstack4_noca_d2[2:3, :]) + + Xstack4_dcolon = stack(x -> ComponentArray(a=x, b=[x+1,x+2]), [5 6; 7 8]; dims=:) # map then stack + Xstack4_noca_dcolon = stack(x -> [x, x+1, x+2], [5 6; 7 8]; dims=:) # map then stack + @test all(Xstack4_dcolon .== Xstack4_noca_dcolon) + @test all(Xstack4_dcolon[:a, :, :] .== Xstack4_noca_dcolon[1, :, :]) + @test all(Xstack4_dcolon[:b, :, :] .== Xstack4_noca_dcolon[2:3, :, :]) # Test fix https://github.com/Deltares/Ribasim/issues/2028 a = range(0.0, 1.0, length=0) |> collect