@@ -160,16 +160,25 @@ immutable AxisArray{T,N,D,Ax} <: AbstractArray{T,N}
160
160
axes:: Ax # Ax<:NTuple{N, Axis}, but with specialized Axis{...} types
161
161
(:: 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)
162
162
end
163
- #
164
- _defaultdimname (i) = i == 1 ? (:row ) : i == 2 ? (:col ) : i == 3 ? (:page ) : Symbol (:dim_ , i)
165
163
164
+ # Helper functions: Default axis names (if not provided)
165
+ _defaultdimname (i) = i == 1 ? (:row ) : i == 2 ? (:col ) : i == 3 ? (:page ) : Symbol (:dim_ , i)
166
166
# Why doesn't @pure work here?
167
167
@generated function _nextaxistype {M} (axs:: NTuple{M,Axis} )
168
168
name = _defaultdimname (M+ 1 )
169
169
:(Axis{$ (Expr (:quote , name))})
170
170
end
171
171
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.
172
177
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
+ """
173
182
@inline default_axes (A:: AbstractArray , args= indices (A)) = _default_axes (A, args, ())
174
183
_default_axes {T,N} (A:: AbstractArray{T,N} , args:: Tuple{} , axs:: NTuple{N,Axis} ) = axs
175
184
_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
181
190
@inline _default_axes {T,N} (A:: AbstractArray{T,N} , args:: Tuple{Axis, Vararg{Any}} , axs:: Tuple ) =
182
191
_default_axes (A, Base. tail (args), (axs... , args[1 ]))
183
192
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
190
194
@inline checksizes (axs, sz) =
191
195
(length (axs[1 ]) == sz[1 ]) & checksizes (tail (axs), tail (sz))
192
196
checksizes (:: Tuple{} , sz) = true
193
-
194
197
@inline function checknames (name:: Symbol , names... )
195
198
matches = false
196
199
for n in names
@@ -202,11 +205,18 @@ end
202
205
checknames (name, names... ) = throw (ArgumentError (" the Axis names must be Symbols" ))
203
206
checknames () = ()
204
207
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
208
209
AxisArray (A:: AbstractArray , vects:: Union{AbstractVector, Axis} ...) = AxisArray (A, vects)
209
210
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))))
210
220
function AxisArray {T,N} (A:: AbstractArray{T,N} , names:: NTuple{N,Symbol} , steps:: NTuple{N,Number} , offsets:: NTuple{N,Number} = map (zero, steps))
211
221
axs = ntuple (i-> Axis {names[i]} (range (offsets[i], steps[i], size (A,i))), N)
212
222
AxisArray (A, axs... )
0 commit comments