Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switched out names for dimnames #83

Merged
merged 2 commits into from
Nov 25, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/NamedDims.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using Pkg
using Requires
using Statistics

export NamedDimsArray, dim, rename, unname
export NamedDimsArray, dim, rename, unname, dimnames

function __init__()
# NOTE: NamedDims is only compatible with Tracker v0.2.2; but no nice way to enforce that.
Expand Down
4 changes: 2 additions & 2 deletions src/broadcasting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ end
function Base.copyto!(dest::AbstractArray, bc::Broadcasted{NamedDimsStyle{S}}) where S
inner_bc = unwrap_broadcasted(bc)
copyto!(dest, inner_bc)
L = unify_names(names(dest), broadcasted_names(bc))
L = unify_names(dimnames(dest), broadcasted_names(bc))
return NamedDimsArray{L}(dest)
end

Expand All @@ -69,5 +69,5 @@ function broadcasted_names(a, bs...)
b_name = broadcasted_names(bs...)
unify_names_longest(a_name, b_name)
end
broadcasted_names(a::AbstractArray) = names(a)
broadcasted_names(a::AbstractArray) = dimnames(a)
broadcasted_names(a) = tuple()
14 changes: 7 additions & 7 deletions src/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Helpers:

function nameddimsarray_result(original_nda, reduced_data, reduction_dims)
L = names(original_nda)
L = dimnames(original_nda)
return NamedDimsArray{L}(reduced_data)
end

Expand Down Expand Up @@ -43,7 +43,7 @@ for (mod, funs) in (
# Vector case
@eval function $mod.$fun(a::NamedDimsArray{L, T, 1}; kwargs...) where {L, T}
data = $mod.$fun(parent(a); kwargs...)
return NamedDimsArray{NamedDims.names(a)}(data)
return NamedDimsArray{dimnames(a)}(data)
end
end
end
Expand Down Expand Up @@ -134,15 +134,15 @@ Base.pop!(A::NamedDimsArray) = pop!(parent(A))
Base.popfirst!(A::NamedDimsArray) = popfirst!(parent(A))

function Base.append!(A::NamedDimsArray{L,T,1}, B::AbstractVector) where {L,T}
newL = unify_names(L, names(B))
newL = unify_names(L, dimnames(B))
data = append!(parent(A), unname(B))
return NamedDimsArray{newL}(data)
end

################################################
# map, collect

Base.map(f, A::NamedDimsArray) = NamedDimsArray(map(f, parent(A)), names(A))
Base.map(f, A::NamedDimsArray) = NamedDimsArray(map(f, parent(A)), dimnames(A))

for (T, S) in [
(:NamedDimsArray, :AbstractArray),
Expand All @@ -154,15 +154,15 @@ for (T, S) in [
# Here f::F where {F} is needed to avoid ambiguities in Julia 1.0
@eval function Base.$fun(f::F, a::$T, b::$S, cs::AbstractArray...) where {F}
data = $fun(f, unname(a), unname(b), unname.(cs)...)
new_names = unify_names(names(a), names(b), names.(cs)...)
new_names = unify_names(dimnames(a), dimnames(b), dimnames.(cs)...)
return NamedDimsArray(data, new_names)
end

end

@eval function Base.foreach(f::F, a::$T, b::$S, cs::AbstractArray...) where {F}
data = foreach(f, unname(a), unname(b), unname.(cs)...)
unify_names(names(a), names(b), names.(cs)...)
unify_names(dimnames(a), dimnames(b), dimnames.(cs)...)
return nothing
end
end
Expand All @@ -188,6 +188,6 @@ Base.collect(x::Base.Generator{<:Iterators.ProductIterator{<:Tuple{<:NamedDimsAr

function collect_product(x)
data = collect(Base.Generator(x.f, Iterators.product(unname.(x.iter.iterators)...)))
all_names = tuple_cat(names.(x.iter.iterators)...)
all_names = tuple_cat(dimnames.(x.iter.iterators)...)
return NamedDimsArray(data, all_names)
end
2 changes: 1 addition & 1 deletion src/functions_dims.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ rename(nda::NamedDimsArray, names) = NamedDimsArray(parent(nda), names)
function Base.dropdims(nda::NamedDimsArray; dims)
numerical_dims = dim(nda, dims)
data = dropdims(parent(nda); dims=numerical_dims)
L = remaining_dimnames_after_dropping(names(nda), numerical_dims)
L = remaining_dimnames_after_dropping(dimnames(nda), numerical_dims)
return NamedDimsArray{L}(data)
end

Expand Down
4 changes: 2 additions & 2 deletions src/functions_math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ macro declare_matmul(MatrixT, VectorT=nothing)

quote
function Base.:*(a::$TA_named, b::$TB_other)
return *(a, NamedDims.NamedDimsArray{NamedDims.names(b)}(b))
return *(a, NamedDims.NamedDimsArray{dimnames(b)}(b))
end
function Base.:*(a::$TA_other, b::$TB_named)
return *(NamedDims.NamedDimsArray{NamedDims.names(a)}(a), b)
return *(NamedDims.NamedDimsArray{dimnames(a)}(a), b)
end
end
end
Expand Down
14 changes: 7 additions & 7 deletions src/wrapper_array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ unname(x::NamedDimsArray) = parent(x)
unname(x::AbstractArray) = x

"""
names(A) -> Tuple
dimnames(A) -> Tuple
Return the names of all the dimensions of the array `A`.
"""
names(::Type{<:NamedDimsArray{L}}) where L = L
names(::Type{<:AbstractArray{T, N}}) where {T, N} = ntuple(_->:_, N)
names(x::T) where T<:AbstractArray = names(T)
dimnames(::Type{<:NamedDimsArray{L}}) where L = L
dimnames(::Type{<:AbstractArray{T, N}}) where {T, N} = ntuple(_->:_, N)
dimnames(x::T) where T<:AbstractArray = dimnames(T)

dim(a::NamedDimsArray{L}, name) where L = dim(L, name)

Expand All @@ -103,7 +103,7 @@ Base.axes(a::NamedDimsArray, d) = axes(parent(a), dim(a, d))


function named_size(a::AbstractArray{T,N}) where {T,N}
L = names(a)
L = dimnames(a)
NamedTuple{L, NTuple{N,Int}}(size(a))
end
function Base.similar(
Expand Down Expand Up @@ -157,7 +157,7 @@ order_named_inds(A; y=5) == (:, 5)
This provides the core indexed lookup for `getindex` and `setindex` on the Array `A`
"""
order_named_inds(A::AbstractArray; named_inds...) = order_named_inds(names(A); named_inds...)
order_named_inds(A::AbstractArray; named_inds...) = order_named_inds(dimnames(A); named_inds...)

###################
# getindex / view / dotview
Expand All @@ -183,7 +183,7 @@ for f in (:getindex, :view, :dotview)
@propagate_inbounds function Base.$f(a::NamedDimsArray, inds...)
# Some nonscalar case, will return an array, so need to give that names.
data = Base.$f(parent(a), inds...)
L = remaining_dimnames_from_indexing(names(a), inds)
L = remaining_dimnames_from_indexing(dimnames(a), inds)
return NamedDimsArray{L}(data)
end
end
Expand Down
28 changes: 14 additions & 14 deletions test/broadcasting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ using Tracker

@testset "standard case" begin
@test nda .+ nda == 2ones(3)
@test names(nda .+ nda) == (:a,)
@test dimnames(nda .+ nda) == (:a,)

@test nda .+ nda .+ nda == 3ones(3)
@test names(nda .+ nda .+ nda) == (:a,)
@test dimnames(nda .+ nda .+ nda) == (:a,)

# in-place
@test names(nda .= 0 .* nda .+ 7) == (:a,)
@test dimnames(nda .= 0 .* nda .+ 7) == (:a,)
@test unname(nda .= 0 .* nda .+ 7) == 7*ones(3)
end

Expand All @@ -24,7 +24,7 @@ using Tracker

lhs = ndx .+ ndy
rhs = ndy .+ ndx
@test names(lhs) == (:x, :y) == names(rhs)
@test dimnames(lhs) == (:x, :y) == dimnames(rhs)
@test lhs == 2ones(3, 5) == rhs
end

Expand All @@ -41,15 +41,15 @@ using Tracker
ones(3, 3, 3, 3)
)
@test lhs_sum == ones(3, 3, 3, 3)
@test names(lhs_sum) == (:a, :b, :c, :d)
@test dimnames(lhs_sum) == (:a, :b, :c, :d)


rhs_sum = .+(
zeros(3, 3, 3, 3),
NamedDimsArray{(:w, :x, :y, :z)}(ones(3, 3, 3, 3))
)
@test rhs_sum == ones(3, 3, 3, 3)
@test names(rhs_sum) == (:w, :x, :y, :z)
@test dimnames(rhs_sum) == (:w, :x, :y, :z)
end

@testset "broadcasting" begin
Expand All @@ -61,9 +61,9 @@ using Tracker
@test s .+ m == ones(3, 3) == m .+ s
@test s .+ v .+ m == ones(3, 3) == m .+ s .+ v

@test names(v .+ m) == (:time, :value) == names(m .+ v)
@test names(s .+ m) == (:time, :value) == names(m .+ s)
@test names(s .+ v .+ m) == (:time, :value) == names(m .+ s .+ v)
@test dimnames(v .+ m) == (:time, :value) == dimnames(m .+ v)
@test dimnames(s .+ m) == (:time, :value) == dimnames(m .+ s)
@test dimnames(s .+ v .+ m) == (:time, :value) == dimnames(m .+ s .+ v)
end

@testset "Mixed array types" begin
Expand All @@ -82,15 +82,15 @@ using Tracker

total = T1(ones(3, 6)) .+ T2(2ones(3, 6)) .+ T3(3ones(3, 6))
@test total == 6ones(3, 6)
@test names(total) == (:foo, :bar)
@test dimnames(total) == (:foo, :bar)
end
end

@testset "Regression test again #8b" begin
# https://github.com/invenia/NamedDims.jl/issues/8#issuecomment-490124369
nda = NamedDimsArray{(:x,:y,:z)}(ones(10,20,30))
@test nda .+ ones(1,20) == 2ones(10,20,30)
@test names(nda .+ ones(1,20)) == (:x, :y, :z)
@test dimnames(nda .+ ones(1,20)) == (:x, :y, :z)
end

@testset "in-place assignment .=" begin
Expand All @@ -108,12 +108,12 @@ using Tracker
@test_throws DimensionMismatch ac .= ab .+ ba

# check that dest is written into:
@test names(z .= ab .+ ba') == (:a, :b)
@test dimnames(z .= ab .+ ba') == (:a, :b)
@test z == (ab.data .+ ba.data')
@test z isa Array # has not itself magically gained names

@test names(z .= ab .+ a_) == (:a, :b)
@test names(a_ .= ba' .+ ab) == (:a, :b)
@test dimnames(z .= ab .+ a_) == (:a, :b)
@test dimnames(a_ .= ba' .+ ab) == (:a, :b)
end

end
Expand Down
Loading