Skip to content

Commit 609bad2

Browse files
committed
Minor re-arrangement, docs, and comments
1 parent 9813aca commit 609bad2

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/core.jl

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,25 @@ immutable AxisArray{T,N,D,Ax} <: AbstractArray{T,N}
160160
axes::Ax # Ax<:NTuple{N, Axis}, but with specialized Axis{...} types
161161
(::Type{AxisArray{T,N,D,Ax}}){T,N,D,Ax}(data::AbstractArray{T,N}, axs::Tuple{Vararg{Axis,N}}) = new{T,N,D,Ax}(data, axs)
162162
end
163-
#
164-
_defaultdimname(i) = i == 1 ? (:row) : i == 2 ? (:col) : i == 3 ? (:page) : Symbol(:dim_, i)
165163

164+
# Helper functions: Default axis names (if not provided)
165+
_defaultdimname(i) = i == 1 ? (:row) : i == 2 ? (:col) : i == 3 ? (:page) : Symbol(:dim_, i)
166166
# Why doesn't @pure work here?
167167
@generated function _nextaxistype{M}(axs::NTuple{M,Axis})
168168
name = _defaultdimname(M+1)
169169
:(Axis{$(Expr(:quote, name))})
170170
end
171171

172+
"""
173+
default_axes(A::AbstractArray)
174+
default_axes(A::AbstractArray, axs)
175+
176+
Return a tuple of Axis objects that appropriately index into the array A.
172177
178+
The optional second argument can take a tuple of vectors or axes, which will be
179+
wrapped with the appropriate axis name, and it will ensure no axis goes beyond
180+
the dimensionality of the array A.
181+
"""
173182
@inline default_axes(A::AbstractArray, args=indices(A)) = _default_axes(A, args, ())
174183
_default_axes{T,N}(A::AbstractArray{T,N}, args::Tuple{}, axs::NTuple{N,Axis}) = axs
175184
_default_axes{T,N}(A::AbstractArray{T,N}, args::Tuple{Any, Vararg{Any}}, axs::NTuple{N,Axis}) = throw(ArgumentError("too many axes provided"))
@@ -181,16 +190,10 @@ _default_axes{T,N}(A::AbstractArray{T,N}, args::Tuple{Axis, Vararg{Any}}, axs::N
181190
@inline _default_axes{T,N}(A::AbstractArray{T,N}, args::Tuple{Axis, Vararg{Any}}, axs::Tuple) =
182191
_default_axes(A, Base.tail(args), (axs..., args[1]))
183192

184-
function AxisArray{T,N}(A::AbstractArray{T,N}, axs::NTuple{N,Axis})
185-
checksizes(axs, _size(A)) || throw(ArgumentError("the length of each axis must match the corresponding size of data"))
186-
checknames(axisnames(axs...)...)
187-
AxisArray{T,N,typeof(A),typeof(axs)}(A, axs)
188-
end
189-
193+
# Axis consistency checks — ensure sizes match and the names are unique
190194
@inline checksizes(axs, sz) =
191195
(length(axs[1]) == sz[1]) & checksizes(tail(axs), tail(sz))
192196
checksizes(::Tuple{}, sz) = true
193-
194197
@inline function checknames(name::Symbol, names...)
195198
matches = false
196199
for n in names
@@ -202,11 +205,18 @@ end
202205
checknames(name, names...) = throw(ArgumentError("the Axis names must be Symbols"))
203206
checknames() = ()
204207

205-
# Simple non-type-stable constructors to specify just the name or axis values
206-
AxisArray(A::AbstractArray) = AxisArray(A, ()) # Disambiguation
207-
AxisArray(A::AbstractArray, names::Symbol...) = (inds = indices(A); AxisArray(A, ntuple(i->Axis{names[i]}(inds[i]), length(names))))
208+
# The primary AxisArray constructors — specify an array to wrap and the axes
208209
AxisArray(A::AbstractArray, vects::Union{AbstractVector, Axis}...) = AxisArray(A, vects)
209210
AxisArray(A::AbstractArray, vects::Tuple{Vararg{Union{AbstractVector, Axis}}}) = AxisArray(A, default_axes(A, vects))
211+
function AxisArray{T,N}(A::AbstractArray{T,N}, axs::NTuple{N,Axis})
212+
checksizes(axs, _size(A)) || throw(ArgumentError("the length of each axis must match the corresponding size of data"))
213+
checknames(axisnames(axs...)...)
214+
AxisArray{T,N,typeof(A),typeof(axs)}(A, axs)
215+
end
216+
217+
# Simple non-type-stable constructors to specify names as symbols
218+
AxisArray(A::AbstractArray) = AxisArray(A, ()) # Disambiguation
219+
AxisArray(A::AbstractArray, names::Symbol...) = (inds = indices(A); AxisArray(A, ntuple(i->Axis{names[i]}(inds[i]), length(names))))
210220
function AxisArray{T,N}(A::AbstractArray{T,N}, names::NTuple{N,Symbol}, steps::NTuple{N,Number}, offsets::NTuple{N,Number}=map(zero, steps))
211221
axs = ntuple(i->Axis{names[i]}(range(offsets[i], steps[i], size(A,i))), N)
212222
AxisArray(A, axs...)

0 commit comments

Comments
 (0)