Skip to content

Commit bfe8fbf

Browse files
author
Michael Abbott
committed
repalce names -> dimnames
1 parent b8e7aad commit bfe8fbf

File tree

10 files changed

+53
-55
lines changed

10 files changed

+53
-55
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
1414

1515
[compat]
1616
IntervalSets = "0.3, 0.4"
17-
NamedDims = "0.2.14"
17+
NamedDims = "0.2.18"
1818
LazyStack = "0.0.7"
1919
OffsetArrays = "0.10, 0.11"
2020
Tables = "0.2, 1"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ ranges(ans,1) # 10:10:100 with indices 0:9
9898
### Functions
9999

100100
As usual `axes(A)` returns (a tuple of vectors of) indices, and `ranges(A)` returns keys.
101-
If the array has names, then `names(A)` returns them, and functions like `axes(A, name)`
101+
If the array has names, then `dimnames(A)` returns them, and functions like `axes(A, name)`
102102
give just one.
103103

104104
Many functions should work, for example:

src/AxisRanges.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ include("struct.jl")
44
export RangeArray, ranges
55

66
include("names.jl")
7-
export NamedDimsArray, namedranges, namedaxes
7+
export NamedDimsArray, dimnames
88

99
include("wrap.jl")
1010
export wrapdims

src/functions.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ tuple_flatten() = ()
3535

3636
function Base.mapreduce(f, op, A::RangeArray; dims=:) # sum, prod, etc
3737
dims === Colon() && return mapreduce(f, op, parent(A))
38-
numerical_dims = hasnames(A) ? NamedDims.dim(names(A), dims) : dims
38+
numerical_dims = hasnames(A) ? NamedDims.dim(dimnames(A), dims) : dims
3939
data = mapreduce(f, op, parent(A); dims=numerical_dims)
4040
new_ranges = ntuple(d -> d in numerical_dims ? Base.OneTo(1) : ranges(A,d), ndims(A))
4141
return RangeArray(data, map(copy, new_ranges))#, copy(A.meta))
@@ -45,23 +45,23 @@ using Statistics
4545
for fun in [:mean, :std, :var] # These don't use mapreduce, but could perhaps be handled better?
4646
@eval function Statistics.$fun(A::RangeArray; dims=:)
4747
dims === Colon() && return $fun(parent(A))
48-
numerical_dims = hasnames(A) ? NamedDims.dim(names(A), dims) : dims
48+
numerical_dims = hasnames(A) ? NamedDims.dim(dimnames(A), dims) : dims
4949
data = $fun(parent(A); dims=numerical_dims)
5050
new_ranges = ntuple(d -> d in numerical_dims ? Base.OneTo(1) : ranges(A,d), ndims(A))
5151
return RangeArray(data, map(copy, new_ranges))#, copy(A.meta))
5252
end
5353
VERSION >= v"1.3" &&
5454
@eval function Statistics.$fun(f, A::RangeArray; dims=:)
5555
dims === Colon() && return $fun(f, parent(A))
56-
numerical_dims = hasnames(A) ? NamedDims.dim(names(A), dims) : dims
56+
numerical_dims = hasnames(A) ? NamedDims.dim(dimnames(A), dims) : dims
5757
data = $fun(f, parent(A); dims=numerical_dims)
5858
new_ranges = ntuple(d -> d in numerical_dims ? Base.OneTo(1) : ranges(A,d), ndims(A))
5959
return RangeArray(data, map(copy, new_ranges))#, copy(A.meta))
6060
end
6161
end
6262

6363
function Base.dropdims(A::RangeArray; dims)
64-
numerical_dims = hasnames(A) ? NamedDims.dim(names(A), dims) : dims
64+
numerical_dims = hasnames(A) ? NamedDims.dim(dimnames(A), dims) : dims
6565
data = dropdims(A.data; dims=dims)
6666
ranges = range_skip(A.ranges, numerical_dims...)
6767
RangeArray(data, ranges)#, A.meta)
@@ -73,7 +73,7 @@ range_skip(tup::Tuple, d, dims...) = range_skip(
7373
range_skip(tup::Tuple) = tup
7474

7575
function Base.permutedims(A::RangeArray, perm)
76-
numerical_perm = hasnames(A) ? NamedDims.dim(names(A), perm) : perm
76+
numerical_perm = hasnames(A) ? NamedDims.dim(dimnames(A), perm) : perm
7777
data = permutedims(A.data, numerical_perm)
7878
new_ranges = ntuple(d -> copy(ranges(A, perm[d])), ndims(A))
7979
RangeArray(data, new_ranges)#, copy(A.meta))
@@ -128,7 +128,7 @@ range_vcat(a::Base.OneTo, b::Base.OneTo) = Base.OneTo(a.stop + b.stop)
128128
range_vcat(a,b,cs...) = range_vcat(range_vcat(a,b),cs...)
129129

130130
function Base.sort(A::RangeArray; dims, kw...)
131-
dims′ = hasnames(A) ? NamedDims.dim(names(A), dims) : dims
131+
dims′ = hasnames(A) ? NamedDims.dim(dimnames(A), dims) : dims
132132
data = sort(parent(A); dims=dims′, kw...)
133133
# sorts each (say) col independently, thus range along them loses meaning.
134134
new_ranges = ntuple(d -> d==dims′ ? OneTo(size(A,d)) : ranges(A,d), ndims(A))
@@ -140,7 +140,7 @@ function Base.sort(A::RangeVector; kw...)
140140
end
141141

142142
function Base.sortslices(A::RangeArray; dims, kw...)
143-
dims′ = hasnames(A) ? NamedDims.dim(names(A), dims) : dims
143+
dims′ = hasnames(A) ? NamedDims.dim(dimnames(A), dims) : dims
144144
data = sortslices(parent(A); dims=dims′, kw...)
145145
# It would be nice to sort the range to match, but there is no sortpermslices.
146146
# https://github.com/davidavdav/NamedArrays.jl/issues/79 constructs something
@@ -169,11 +169,11 @@ for fun in [:copy, :deepcopy, :similar, :zero, :one]
169169
@eval Base.$fun(A::RangeArray) = RangeArray($fun(parent(A)), map(copy, ranges(A)))
170170
@eval Base.$fun(A::NdaRa) = NamedDimsArray(RangeArray(
171171
$fun(parent(parent(A))),
172-
map(copy, ranges(A))), names(A))
172+
map(copy, ranges(A))), dimnames(A))
173173
end
174174
Base.similar(A::RangeArray, T::Type) = RangeArray(similar(A.data, T), map(copy, A.ranges))
175175
Base.similar(A::NdaRa, T::Type) = NamedDimsArray(RangeArray(
176-
similar(A.data.data, T), map(copy, ranges(A))), names(A))
176+
similar(A.data.data, T), map(copy, ranges(A))), dimnames(A))
177177
Base.similar(A::RangeArray, T::Type, dims::Int...) = similar(A.data, T, dims...)
178178
Base.similar(A::RangeArray, dims::Int...) = similar(A.data, dims...)
179179

src/names.jl

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ NdaRaVoM{L,T,N} = NamedDimsArray{L,T,N,<:RangeVecOrMat}
1010

1111
# Just make names get the names, and behave like size(A,d), axes(A,d) etc.
1212

13-
Base.names(A::RaNda{L}) where {L} = L
14-
Base.names(A::RaNda{L,T,N}, d::Int) where {L,T,N} = d <= N ? L[d] : :_
15-
Base.names(A::NamedDimsArray{L}) where {L} = L # 🏴‍☠️
16-
Base.names(A::NamedDimsArray{L,T,N}, d::Int) where {L,T,N} = d <= N ? L[d] : :_ # 🏴‍☠️
13+
NamedDims.dimnames(A::RaNda{L}) where {L} = L
14+
NamedDims.dimnames(A::RaNda{L,T,N}, d::Int) where {L,T,N} = d <= N ? L[d] : :_
1715

1816
Base.axes(A::RaNda{L}, s::Symbol) where {L} = axes(A, NamedDims.dim(L,s))
1917
Base.size(A::RaNda{L,T,N}, s::Symbol) where {T,N,L} = size(A, NamedDims.dim(L,s))
@@ -71,12 +69,12 @@ Base.getproperty(A::NamedDimsArray{L}, s::Symbol) where {L} =
7169

7270
@inline @propagate_inbounds function Base.getindex(A::RangeArray; kw...)
7371
hasnames(A) || error("must have names!")
74-
inds = NamedDims.order_named_inds(Val(names(A)); kw...)
72+
inds = NamedDims.order_named_inds(Val(dimnames(A)); kw...)
7573
getindex(A, inds...)
7674
end
7775
@inline @propagate_inbounds function Base.view(A::RangeArray; kw...)
7876
hasnames(A) || error("must have names!")
79-
inds = NamedDims.order_named_inds(Val(names(A)); kw...)
77+
inds = NamedDims.order_named_inds(Val(dimnames(A)); kw...)
8078
view(A, inds...)
8179
end
8280

@@ -88,7 +86,7 @@ end
8886
@inline @propagate_inbounds (A::NdaRa)(;kw...) = getkey(A; kw...)
8987

9088
@inline @propagate_inbounds function getkey(A; kw...)
91-
list = names(A)
89+
list = dimnames(A)
9290
issubset(kw.itr, list) || error("some keywords not in list of names!")
9391
args = map(s -> Base.sym_in(s, kw.itr) ? getfield(kw.data, s) : Colon(), list)
9492
A(args...)
@@ -101,7 +99,7 @@ end
10199
namedranges(A)
102100
namedaxes(A)
103101
104-
Combines `names(A)` and either `ranges(A)` or `axes(A)` into a `NamedTuple`.
102+
Combines `dimnames(A)` and either `ranges(A)` or `axes(A)` into a `NamedTuple`.
105103
"""
106104
namedranges(A::NdaRa{L}) where {L} = NamedTuple{L}(ranges(A))
107105
namedranges(A::RaNda{L}) where {L} = NamedTuple{L}(ranges(A))

src/show.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function _summary(io, x)
99
for d in 1:ndims(x)
1010
print(io, d==1 ? "" : d==2 ? "" : "", " ")
1111
c = colour(x, d)
12-
hasnames(x) && printstyled(io, names(x,d), "", color=c)
12+
hasnames(x) && printstyled(io, dimnames(x,d), "", color=c)
1313
printstyled(io, length(ranges(x,d)), "-element ", shorttype(ranges(x,d)), "\n", color=c)
1414
end
1515
print(io, "And data, ", summary(rangeless(unname(x))))
@@ -173,7 +173,7 @@ function limited_show_nd(io::IO, a::AbstractArray, print_matrix::Function, label
173173
printstyled(io, "[:, :", color=c3)
174174
for i = 1:nd
175175
if hasnames(a) && !hasranges(a)
176-
name = names(a, i+2)
176+
name = dimnames(a, i+2)
177177
printstyled(io, ", $name=$(idxs[i])", color=c3)
178178
else
179179
printstyled(io, ", $(idxs[i])", color=c3)
@@ -186,7 +186,7 @@ function limited_show_nd(io::IO, a::AbstractArray, print_matrix::Function, label
186186
for i = 1:nd
187187
key = sprint(show, ranges(a, i+2)[idxs[i]], context=io)
188188
# if hasnames(a)
189-
# name = names(a, i+2)
189+
# name = dimnames(a, i+2)
190190
# printstyled(io, ", $name = $key", color=c3)
191191
# else
192192
printstyled(io, ", $key", color=c3)
@@ -208,13 +208,13 @@ end
208208
function Base.summary(io::IO, A::NamedDimsArray)
209209
print(io, Base.dims2string(size(A)), " NamedDimsArray(")
210210
Base.showarg(io, parent(A), false)
211-
print(io, ", ", names(A), ")")
211+
print(io, ", ", dimnames(A), ")")
212212
end
213213

214214
function Base.print_matrix(io::IO, A::NamedDimsArray)
215-
s1 = string("", names(A,1)) * " "
215+
s1 = string("", dimnames(A,1)) * " "
216216
if ndims(A)==2
217-
s2 = string(" "^length(s1), "", names(A,2), "\n")
217+
s2 = string(" "^length(s1), "", dimnames(A,2), "\n")
218218
printstyled(io, s2, color=:magenta)
219219
end
220220
ioc = IOContext(io, :displaysize => displaysize(io) .- (2, 0))
@@ -225,12 +225,12 @@ end
225225
# A hack to avoid big messy function?
226226
227227
function Base.print_array(io::IO, A::NamedDimsArray{L,T,3}) where {L,T}
228-
s3 = string("[", names(A,3), " ⤋ ]")
228+
s3 = string("[", dimnames(A,3), " ⤋ ]")
229229
printstyled(io, s3, color=:magenta)
230230
Base.show_nd(io, A, Base.print_matrix, true)
231231
end
232232
function Base.print_array(io::IO, A::NamedDimsArray{L,T,4}) where {L,T}
233-
s3 = string("[", names(A,3),", ", names(A,4), " ⤋ ]")
233+
s3 = string("[", dimnames(A,3),", ", dimnames(A,4), " ⤋ ]")
234234
printstyled(io, s3, color=:magenta)
235235
Base.show_nd(io, A, Base.print_matrix, true)
236236
end

src/stack.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ function LazyStack.getnames(xs::RangeArray{<:AbstractArray,N,OT}) where {N,OT}
5454
(dimnames(eltype(xs))..., dimnames(OT)...)
5555
end
5656
function LazyStack.getnames(xs::AbstractArray{<:RangeArray{T,N,IT}}) where {T,N,IT}
57-
out_names = hasnames(xs) ? names(xs) : NamedDims.dimnames(xs)
57+
out_names = hasnames(xs) ? dimnames(xs) : NamedDims.dimnames(xs)
5858
(NamedDims.dimnames(IT)..., out_names...)
5959
end

src/tables.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Tables.istable(::Type{<:RangeArray}) = true
1010
Tables.rowaccess(::Type{<:RangeArray}) = true
1111

1212
function Tables.rows(A::Union{RangeArray, NdaRa})
13-
L = hasnames(A) ? (names(A)..., :value) : # should gensym() if :value in names(A)
13+
L = hasnames(A) ? (dimnames(A)..., :value) : # should gensym() if :value in dimnames(A)
1414
(ntuple(d -> Symbol(:dim_,d), ndims(A))..., :value)
1515
R = ranges_or_axes(A)
1616
nt(inds) = NamedTuple{L}((map(getindex, R, inds)..., A[inds...]))
@@ -35,7 +35,7 @@ Tables.columnaccess(::Type{<:RangeArray{T,N,AT}}) where {T,N,AT} =
3535
IndexStyle(AT) === IndexLinear()
3636

3737
function Tables.columns(A::Union{RangeArray, NdaRa})
38-
L = hasnames(A) ? (names(A)..., :value) :
38+
L = hasnames(A) ? (dimnames(A)..., :value) :
3939
(ntuple(d -> Symbol(:dim_,d), ndims(A))..., :value)
4040
R = ranges_or_axes(A)
4141
G = ntuple(ndims(A)) do d

test/_basic.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ end
8282
@testset "names" begin
8383

8484
# constructor
85-
@test names(wrapdims(rand(3), :a)) == (:a,)
86-
@test names(wrapdims(rand(3), b=1:3), 1) == :b
85+
@test dimnames(wrapdims(rand(3), :a)) == (:a,)
86+
@test dimnames(wrapdims(rand(3), b=1:3), 1) == :b
8787
@test ranges(wrapdims(rand(3), b=1:3)) == (1:3,)
8888
@test ranges(wrapdims(rand(3), b=1:3), :b) == 1:3
8989

@@ -99,13 +99,13 @@ end
9999
# indexing etc, of commutative wrappers
100100
data = rand(1:99, 3,4)
101101
N1 = wrapdims(data, obs = ['a', 'b', 'c'], iter = 10:10:40)
102-
N2 = NamedDimsArray(RangeArray(data, ranges(N1)), names(N1))
103-
N3 = RangeArray(NamedDimsArray(data, names(N1)), ranges(N1))
102+
N2 = NamedDimsArray(RangeArray(data, ranges(N1)), dimnames(N1))
103+
N3 = RangeArray(NamedDimsArray(data, dimnames(N1)), ranges(N1))
104104

105105
@testset "with $(typeof(N).name) outside" for N in [N2, N3]
106106
@test ranges(N) == (['a', 'b', 'c'], 10:10:40)
107107
@test ranges(N, :iter) == 10:10:40
108-
@test names(N) == (:obs, :iter)
108+
@test dimnames(N) == (:obs, :iter)
109109
@test axes(N, :obs) == 1:3
110110
@test size(N, :obs) == 3
111111

@@ -116,7 +116,7 @@ end
116116
@test N(obs='a') == N('a') == N[1,:] == N[obs=1]
117117
@test N(obs='a') == N('a') == view(N, 1,:) == view(N, obs=1)
118118

119-
@test names(N(obs='a')) == (:iter,)
119+
@test dimnames(N(obs='a')) == (:iter,)
120120
@test ranges(N(obs='b')) == (10:10:40,)
121121

122122
@test_throws Exception N(obs=55) # ideally ArgumentError
@@ -150,10 +150,10 @@ end
150150
mat_y = wrapdims(ones(2,2), :_, :y)
151151

152152
@test ranges(vec_x .+ mat_r .+ mat_y) == ranges(mat_r)
153-
@test names(vec_x .+ mat_r .+ mat_y) == (:x, :y)
153+
@test dimnames(vec_x .+ mat_r .+ mat_y) == (:x, :y)
154154

155155
@test ranges(sqrt.(vec_x .+ vec_y') ./ mat_r) == ranges(mat_r)
156-
@test names(sqrt.(vec_x .+ vec_y') ./ mat_r) == (:x, :y)
156+
@test dimnames(sqrt.(vec_x .+ vec_y') ./ mat_r) == (:x, :y)
157157

158158
yy = vec_y .+ mat_y
159159
@test ranges(yy' .+ mat_r) == (11:12, 'α':'β')
@@ -168,10 +168,10 @@ end
168168
v3 = wrapdims(ones(2), [11, 22])
169169
z = zeros(2,2)
170170

171-
@test names(v1 .= v1 .+ v2) == (,)
171+
@test dimnames(v1 .= v1 .+ v2) == (,)
172172
@test v1[1] == 2
173173

174-
@test names(v2 .= v3 .+ 5) == (,)
174+
@test dimnames(v2 .= v3 .+ 5) == (,)
175175
@test v2[1] == 6
176176

177177
@test ranges(z .= v1 .+ v2') == (["a", "b"], Base.OneTo(2))

test/_functions.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ V = wrapdims(rand(1:99, 10), v=10:10:100)
2323
@test ranges(dropdims(M2, dims=:c)) == ('a':'c',)
2424

2525
M3 = dropdims(M2, dims=:c)
26-
@test names(M3) == (:r,)
26+
@test dimnames(M3) == (:r,)
2727

2828
# permutedims
2929
@test size(permutedims(M)) == (4,3)
30-
@test names(transpose(M2)) == (:c, :r)
30+
@test dimnames(transpose(M2)) == (:c, :r)
3131
@test ranges(permutedims(M, (2,1))) == (2:5, 'a':'c')
32-
@test names(M3') == (:_, :r)
32+
@test dimnames(M3') == (:_, :r)
3333
@test ranges(transpose(transpose(V))) == ranges(V)
3434
@test ranges(permutedims(transpose(V))) == (ranges(V,1), Base.OneTo(1))
3535

@@ -58,7 +58,7 @@ end
5858

5959
mapM = map(exp, M)
6060
@test ranges(mapM) == ('a':'c', 2:5) # fails with nda(ra(...)), has lost ranges?
61-
@test names(mapM) == (:r, :c)
61+
@test dimnames(mapM) == (:r, :c)
6262

6363
@test ranges(map(+, M, M, M)) == ('a':'c', 2:5)
6464
@test ranges(map(+, M, parent(M))) == ('a':'c', 2:5)
@@ -72,20 +72,20 @@ end
7272

7373
genM = [exp(x) for x in M]
7474
@test ranges(genM) == ('a':'c', 2:5) # fails with nda(ra(...))
75-
@test names(genM) == (:r, :c)
75+
@test dimnames(genM) == (:r, :c)
7676

7777
@test ranges([exp(x) for x in V]) == (10:10:100,)
7878

7979
gen3 = [x+y for x in M, y in V];
8080
@test ranges(gen3) == ('a':'c', 2:5, 10:10:100)
81-
@test names(gen3) == (:r, :c, :v)
81+
@test dimnames(gen3) == (:r, :c, :v)
8282

8383
gen1 = [x^i for (i,x) in enumerate(V)]
8484
@test ranges(gen1) == (10:10:100,)
85-
@test names(gen1) == (:v,)
85+
@test dimnames(gen1) == (:v,)
8686

8787
@test ranges(filter(isodd, V2),1) isa Vector{Int}
88-
@test names(filter(isodd, V2)) == (:v,)
88+
@test dimnames(filter(isodd, V2)) == (:v,)
8989

9090
@test filter(isodd, M) isa Array
9191

@@ -132,15 +132,15 @@ end
132132
@test (V' * rand(Int, 10)) isa Int
133133
@test (rand(Int, 10)' * V) isa Int
134134
@test ranges(V * V') === (10:10:100, 10:10:100)
135-
@test names(V * V') === (:v, :v)
135+
@test dimnames(V * V') === (:v, :v)
136136
@test ranges(V * rand(1,10)) === (10:10:100, Base.OneTo(10))
137-
@test names(V * rand(1,10)) === (:v, :_)
137+
@test dimnames(V * rand(1,10)) === (:v, :_)
138138

139139
# matrix * vector
140140
@test ranges(M * M('a')) === ('a':'c',)
141-
@test names(M * M('a')) === (:r,)
141+
@test dimnames(M * M('a')) === (:r,)
142142
@test ranges(M(5)' * M) === (Base.OneTo(1), 2:5)
143-
@test names(M(5)' * M) === (:_, :c)
143+
@test dimnames(M(5)' * M) === (:_, :c)
144144

145145
end
146146
@testset "div" begin # doesn't work for names yet
@@ -166,8 +166,8 @@ end
166166
@test ranges(similar(MN, Int)) == ranges(M)
167167
@test AxisRanges.hasranges(similar(M, Int, 3,3)) == false
168168
@test AxisRanges.hasranges(similar(MN, Int, 3,3)) == false
169-
@test names(similar(M, 3,3)) == (:r, :c)
170-
@test names(similar(MN, 3,3)) == (:r, :c)
169+
@test dimnames(similar(M, 3,3)) == (:r, :c)
170+
@test dimnames(similar(MN, 3,3)) == (:r, :c)
171171
@test AxisRanges.hasnames(similar(M, 2,2,2)) == false
172172
@test AxisRanges.hasnames(similar(MN, 2,2,2)) == false
173173

0 commit comments

Comments
 (0)