Skip to content

Commit 8e9aad3

Browse files
authored
Merge pull request #24 from mcabbott/cat
Fix cat ambiguities
2 parents c56be54 + 033bb31 commit 8e9aad3

File tree

5 files changed

+30
-10
lines changed

5 files changed

+30
-10
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ BenchmarkTools = "0.5"
2020
IntervalSets = "0.5.1"
2121
InvertedIndices = "1.0"
2222
LazyStack = "0.0.7, 0.0.8"
23-
NamedDims = "0.2.21"
23+
NamedDims = "0.2.27"
2424
OffsetArrays = "0.10, 0.11, 1.0"
2525
Tables = "0.2, 1"
2626
julia = "1"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# AxisKeys.jl
22

3-
[![Build Status](https://travis-ci.org/mcabbott/AxisKeys.jl.svg?branch=master)](https://travis-ci.org/mcabbott/AxisKeys.jl)
3+
[![Build Status](https://travis-ci.org/mcabbott/AxisKeys.jl.svg?branch=master)](https://travis-ci.com/mcabbott/AxisKeys.jl)
44

55
<!--<img src="docs/readmefigure.png" alt="block picture" width="400" align="right">-->
66

src/functions.jl

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,10 @@ end
9292

9393
for (T, S) in [(:KeyedVecOrMat, :KeyedVecOrMat), # KeyedArray gives ambiguities
9494
(:KeyedVecOrMat, :AbstractVecOrMat), (:AbstractVecOrMat, :KeyedVecOrMat),
95-
(:NdaKaVoM, :NdaKaVoM), # These are needed because hcat(NamedDimsArray...) relies on similar()
95+
(:NdaKaVoM, :NdaKaVoM),
9696
(:NdaKaVoM, :KeyedVecOrMat), (:KeyedVecOrMat, :NdaKaVoM),
97-
(:NdaKaVoM, :AbstractVecOrMat), (:AbstractVecOrMat, :NdaKaVoM) ]
97+
(:NdaKaVoM, :AbstractVecOrMat), (:AbstractVecOrMat, :NdaKaVoM),
98+
]
9899

99100
@eval function Base.vcat(A::$T, B::$S, Cs::AbstractVecOrMat...)
100101
data = vcat(keyless(A), keyless(B), keyless.(Cs)...)
@@ -118,13 +119,23 @@ for (T, S) in [ (:KeyedArray, :KeyedArray),
118119
(:KeyedArray, :NamedDimsArray), (:NamedDimsArray, :KeyedArray),
119120
(:NdaKa, :NdaKa),
120121
(:NdaKa, :KeyedArray), (:KeyedArray, :NdaKa),
121-
(:NdaKa, :AbstractArray), (:AbstractArray, :NdaKa) ]
122+
(:NdaKa, :AbstractArray), (:AbstractArray, :NdaKa),
123+
]
122124

123125
@eval function Base.cat(A::$T, B::$S, Cs::AbstractArray...; dims)
124-
# numerical_dims = hasnames(A) || hasnames(B) ? ... todo!
125-
data = cat(keyless(A), keyless(B), keyless.(Cs)...; dims=dims)
126+
numerical_dims, data = if any(hasnames.((A, B, Cs...)))
127+
old_names = NamedDims.unify_names_longest(dimnames(A), dimnames(B), dimnames.(Cs)...)
128+
new_names = NamedDims.expand_dimnames(old_names, dims)
129+
α = NamedDims.dim(new_names, dims)
130+
β = cat(keyless(A), keyless(B), keyless.(Cs)...; dims=dims)
131+
α, β
132+
else
133+
α = val_strip(dims)
134+
β = cat(keyless(A), keyless(B), keyless.(Cs)...; dims=numerical_dims)
135+
α, β
136+
end
126137
new_keys = ntuple(ndims(data)) do d
127-
if d in dims
138+
if d in numerical_dims
128139
key_vcat(keys_or_axes(A,d), keys_or_axes(B,d), keys_or_axes.(Cs,d)...)
129140
else
130141
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),
134145
end
135146

136147
end
148+
val_strip(dims::Val{d}) where {d} = d
149+
val_strip(dims) = dims
137150
key_vcat(a::AbstractVector, b::AbstractVector) = vcat(a,b)
138151
key_vcat(a::Base.OneTo, b::Base.OneTo) = Base.OneTo(a.stop + b.stop)
139152
key_vcat(a,b,cs...) = key_vcat(key_vcat(a,b),cs...)

src/names.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ using NamedDims
44

55
NdaKa{L,T,N} = NamedDimsArray{L,T,N,<:KeyedArray{T,N}}
66
KaNda{L,T,N} = KeyedArray{T,N,<:NamedDimsArray{L,T,N}}
7-
NdaKaVoM{L,T,N} = NamedDimsArray{L,T,N,<:KeyedVecOrMat}
7+
8+
NdaKaVoM{L,T} = Union{NamedDimsArray{L,T,1,<:KeyedArray}, NamedDimsArray{L,T,2,<:KeyedArray}}
9+
# NdaKaV{L,T} = NamedDimsArray{L,T,1,<:KeyedArray{T,1}}
810

911
# NamedDims now uses dimnames, which behaves like size(A,d), axes(A,d) etc.
1012

test/_functions.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ M = wrapdims(rand(Int8, 3,4), r='a':'c', c=2:5)
44
MN = NamedDimsArray(M.data.data, r='a':'c', c=2:5)
55
V = wrapdims(rand(1:99, 10), v=10:10:100)
66
VN = NamedDimsArray(V.data.data, v=10:10:100)
7+
A3 = wrapdims(rand(Int8, 3,4,2), r='a':'c', c=2:5, p=[10.0, 20.0])
78

89
@testset "dims" begin
910

@@ -151,7 +152,11 @@ end
151152
@test axiskeys(cat(MN,MN, dims=3)) == ('a':1:'c', 2:5, Base.OneTo(2))
152153
@test axiskeys(cat(M,MN, dims=3)) == ('a':1:'c', 2:5, Base.OneTo(2))
153154

154-
@test_broken axiskeys(cat(M,M, dims=:r)) # doesn't work in NamedDims either
155+
@test axiskeys(cat(M,M, dims=:z)) == ('a':1:'c', 2:5, Base.OneTo(2))
156+
@test dimnames(cat(M,M, dims=:z)) == (:r, :c, :z)
157+
158+
@test axiskeys(cat(A3, A3, dims=:p)) == ('a':1:'c', 2:5, [10.0, 20.0, 10.0, 20.0])
159+
@test axiskeys(hcat(A3, A3)) == ('a':1:'c', vcat(2:5,2:5), [10.0, 20.0])
155160

156161
end
157162
@testset "matmul" begin

0 commit comments

Comments
 (0)