Skip to content

Commit

Permalink
Fixes #130, #133
Browse files Browse the repository at this point in the history
  • Loading branch information
davidavdav committed Aug 4, 2024
1 parent f9adb23 commit 5123d2a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/convert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ convert(::Type{NamedVector}, a::AbstractArray) = NamedArray(a)
convert(::Type{NamedArray{T}}, n::NamedArray) where {T} = NamedArray(T.(n.array), n.dicts, n.dimnames)

@inline convert(::Type{NamedArray}, n::NamedArray) = n
@inline convert(::Type{NamedVector}, n::NamedVector) = n
14 changes: 10 additions & 4 deletions src/names.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Extract the names of the indices along dimension `d`, or all dimentsions if `d`
```jldoctest
julia> n = NamedArray([1, 2, 3], (["one", "二", "trois"],))
3-element Named Vector{Int64}
A │
A │
──────┼──
one │ 1
二 │ 2
Expand All @@ -30,6 +30,12 @@ julia> names(n)
names(dict::AbstractDict) = collect(keys(dict))
names(n::NamedArray) = [names(dict) for dict in n.dicts]
names(n::NamedArray, d::Integer) = names(n.dicts[d])
function names(n::NamedArray, d)
i = findfirst(x -> x == d, dimnames(n))
isnothing(i) && throw(KeyError("dimension name"))
return names(n, i)
end

defaultnames(a::AbstractArray) = [defaultnames(a, d) for d in 1:ndims(a)]
defaultnames(a::AbstractArray, d::Integer) = defaultnames(size(a, d))

Expand All @@ -42,7 +48,7 @@ defaultnames(a::AbstractArray, d::Integer) = defaultnames(size(a, d))
Return the names of the `d`'th dimension of NamedArray `n`, or of all dimensions if `d` is unspecified.
# Example
# Example
```jldoctest
julia> n = NamedArray([1 2; 3 4; 5 6], (["一", "二", "三"], ["first", "second"]), ("cmn", "en"))
Expand Down Expand Up @@ -76,7 +82,7 @@ strdimnames(n::NamedArray, d::Integer) = string(n.dimnames[d])
"""
setnames!(n::NamedArray, v::Vector{T}, d::Integer)
Set the names of `n` along dimension `d` to `v`.
Set the names of `n` along dimension `d` to `v`.
The NamedArray `n` must already have names of type `T`
Expand Down Expand Up @@ -155,7 +161,7 @@ setdimnames!(n::NamedArray, dn::Vector) = setdimnames!(n, tuple(dn...))
"""
setdimnames!(n::NamedArray, name, d::Integer)
Set the name of the dimension `d` of NamedArray `n` to `name`.
Set the name of the dimension `d` of NamedArray `n` to `name`.
# Example
```jldoctest
Expand Down
28 changes: 28 additions & 0 deletions test/bugs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,32 @@ end
deleteat!(v, ("b", "h"))
@test v["i"] == a[9]
@test v.dicts[1] == Dict("c" => 1, "d" => 2, "f" => 3, "i" => 4, "j" => 5)
end

@testset "issue #130" begin
n = NamedArray([1 2; 3 4], (["a", "b"], ["one", "two"]), ("A", "B"))
@test names(n, "A") == ["a", "b"]
@test names(n, "B") == ["one", "two"]
@test_throws KeyError names(n, "C")
end

@testset "issue #133" begin
mutable struct NamedArrayHolder
named_array::NamedArray
end

mutable struct NamedVectorHolder
named_vector::NamedVector
end

na = NamedArray([1, 2, 3], names=(["x", "y", "z"],))
nb = NamedArray([10, 20, 30], names=(["x", "y", "z"],))

nah = NamedArrayHolder(na)
nah.named_array = nb
@test nah.named_array === nb

nvh = NamedVectorHolder(na)
nvh.named_vector = nb
@test nvh.named_vector === nb
end

0 comments on commit 5123d2a

Please sign in to comment.