diff --git a/Project.toml b/Project.toml index 9cd9b22..55f4c28 100644 --- a/Project.toml +++ b/Project.toml @@ -20,7 +20,7 @@ BenchmarkTools = "0.5" IntervalSets = "0.5.1" InvertedIndices = "1.0" LazyStack = "0.0.7, 0.0.8" -NamedDims = "0.2.21" +NamedDims = "0.2.27" OffsetArrays = "0.10, 0.11, 1.0" Tables = "0.2, 1" julia = "1" diff --git a/README.md b/README.md index 5f8cc7e..481311b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # AxisKeys.jl -[![Build Status](https://travis-ci.org/mcabbott/AxisKeys.jl.svg?branch=master)](https://travis-ci.org/mcabbott/AxisKeys.jl) +[![Build Status](https://travis-ci.org/mcabbott/AxisKeys.jl.svg?branch=master)](https://travis-ci.com/mcabbott/AxisKeys.jl) diff --git a/src/functions.jl b/src/functions.jl index 134f2d2..b7c36e4 100644 --- a/src/functions.jl +++ b/src/functions.jl @@ -92,9 +92,10 @@ end for (T, S) in [(:KeyedVecOrMat, :KeyedVecOrMat), # KeyedArray gives ambiguities (:KeyedVecOrMat, :AbstractVecOrMat), (:AbstractVecOrMat, :KeyedVecOrMat), - (:NdaKaVoM, :NdaKaVoM), # These are needed because hcat(NamedDimsArray...) relies on similar() + (:NdaKaVoM, :NdaKaVoM), (:NdaKaVoM, :KeyedVecOrMat), (:KeyedVecOrMat, :NdaKaVoM), - (:NdaKaVoM, :AbstractVecOrMat), (:AbstractVecOrMat, :NdaKaVoM) ] + (:NdaKaVoM, :AbstractVecOrMat), (:AbstractVecOrMat, :NdaKaVoM), + ] @eval function Base.vcat(A::$T, B::$S, Cs::AbstractVecOrMat...) data = vcat(keyless(A), keyless(B), keyless.(Cs)...) @@ -118,13 +119,23 @@ for (T, S) in [ (:KeyedArray, :KeyedArray), (:KeyedArray, :NamedDimsArray), (:NamedDimsArray, :KeyedArray), (:NdaKa, :NdaKa), (:NdaKa, :KeyedArray), (:KeyedArray, :NdaKa), - (:NdaKa, :AbstractArray), (:AbstractArray, :NdaKa) ] + (:NdaKa, :AbstractArray), (:AbstractArray, :NdaKa), + ] @eval function Base.cat(A::$T, B::$S, Cs::AbstractArray...; dims) - # numerical_dims = hasnames(A) || hasnames(B) ? ... todo! - data = cat(keyless(A), keyless(B), keyless.(Cs)...; dims=dims) + numerical_dims, data = if any(hasnames.((A, B, Cs...))) + old_names = NamedDims.unify_names_longest(dimnames(A), dimnames(B), dimnames.(Cs)...) + new_names = NamedDims.expand_dimnames(old_names, dims) + α = NamedDims.dim(new_names, dims) + β = cat(keyless(A), keyless(B), keyless.(Cs)...; dims=dims) + α, β + else + α = val_strip(dims) + β = cat(keyless(A), keyless(B), keyless.(Cs)...; dims=numerical_dims) + α, β + end new_keys = ntuple(ndims(data)) do d - if d in dims + if d in numerical_dims key_vcat(keys_or_axes(A,d), keys_or_axes(B,d), keys_or_axes.(Cs,d)...) else unify_one(keys_or_axes(A,d), keys_or_axes(B,d), keys_or_axes.(Cs,d)...) @@ -134,6 +145,8 @@ for (T, S) in [ (:KeyedArray, :KeyedArray), end end +val_strip(dims::Val{d}) where {d} = d +val_strip(dims) = dims key_vcat(a::AbstractVector, b::AbstractVector) = vcat(a,b) key_vcat(a::Base.OneTo, b::Base.OneTo) = Base.OneTo(a.stop + b.stop) key_vcat(a,b,cs...) = key_vcat(key_vcat(a,b),cs...) diff --git a/src/names.jl b/src/names.jl index 2424567..bb20672 100644 --- a/src/names.jl +++ b/src/names.jl @@ -4,7 +4,9 @@ using NamedDims NdaKa{L,T,N} = NamedDimsArray{L,T,N,<:KeyedArray{T,N}} KaNda{L,T,N} = KeyedArray{T,N,<:NamedDimsArray{L,T,N}} -NdaKaVoM{L,T,N} = NamedDimsArray{L,T,N,<:KeyedVecOrMat} + +NdaKaVoM{L,T} = Union{NamedDimsArray{L,T,1,<:KeyedArray}, NamedDimsArray{L,T,2,<:KeyedArray}} +# NdaKaV{L,T} = NamedDimsArray{L,T,1,<:KeyedArray{T,1}} # NamedDims now uses dimnames, which behaves like size(A,d), axes(A,d) etc. diff --git a/test/_functions.jl b/test/_functions.jl index 7925ffe..ef6dee1 100644 --- a/test/_functions.jl +++ b/test/_functions.jl @@ -4,6 +4,7 @@ M = wrapdims(rand(Int8, 3,4), r='a':'c', c=2:5) MN = NamedDimsArray(M.data.data, r='a':'c', c=2:5) V = wrapdims(rand(1:99, 10), v=10:10:100) VN = NamedDimsArray(V.data.data, v=10:10:100) +A3 = wrapdims(rand(Int8, 3,4,2), r='a':'c', c=2:5, p=[10.0, 20.0]) @testset "dims" begin @@ -151,7 +152,11 @@ end @test axiskeys(cat(MN,MN, dims=3)) == ('a':1:'c', 2:5, Base.OneTo(2)) @test axiskeys(cat(M,MN, dims=3)) == ('a':1:'c', 2:5, Base.OneTo(2)) - @test_broken axiskeys(cat(M,M, dims=:r)) # doesn't work in NamedDims either + @test axiskeys(cat(M,M, dims=:z)) == ('a':1:'c', 2:5, Base.OneTo(2)) + @test dimnames(cat(M,M, dims=:z)) == (:r, :c, :z) + + @test axiskeys(cat(A3, A3, dims=:p)) == ('a':1:'c', 2:5, [10.0, 20.0, 10.0, 20.0]) + @test axiskeys(hcat(A3, A3)) == ('a':1:'c', vcat(2:5,2:5), [10.0, 20.0]) end @testset "matmul" begin