1
1
# Core types and definitions
2
2
3
+ if VERSION < v " 0.5.0-dev"
4
+ macro pure (ex)
5
+ esc (ex)
6
+ end
7
+ else
8
+ using Base: @pure
9
+ end
10
+
3
11
@doc """
4
12
Type-stable axis-specific indexing and identification with a
5
13
parametric type.
@@ -148,8 +156,9 @@ _defaultdimname(i) = i == 1 ? (:row) : i == 2 ? (:col) : i == 3 ? (:page) : Symb
148
156
AxisArray (A:: AbstractArray , axs:: Axis... ) = AxisArray (A, axs)
149
157
@generated function AxisArray {T,N,L} (A:: AbstractArray{T,N} , axs:: NTuple{L,Axis} )
150
158
ax = Expr (:tuple )
151
- Ax = Tuples. concatenate (axs, NTuple (i-> Axis{_defaultdimname (i+ L),UnitRange{Int64}},N- L))
152
- if ! isa (axisnames (Tuples. collect (axs)... ), Tuple{Vararg{Symbol}})
159
+ Ax = Tuple{axs. parameters... ,
160
+ ntuple (i-> Axis{_defaultdimname (i+ L),UnitRange{Int64}},N- L)... }
161
+ if ! all (x-> isa (axisname (x),Symbol), axs. parameters)
153
162
return :(throw (ArgumentError (" the Axis names must be Symbols" )))
154
163
end
155
164
for i= 1 : L
194
203
axisdim {T,N,D,Ax,name,S} (A:: Type{AxisArray{T,N,D,Ax}} , :: Type{Axis{name,S}} ) = axisdim (A, Axis{name})
195
204
function axisdim {T,N,D,Ax,name} (:: Type{AxisArray{T,N,D,Ax}} , :: Type{Axis{name}} )
196
205
isa (name, Int) && return name <= N ? name : error (" axis $name greater than array dimensionality $N " )
197
- names = axisnames (Tuples . collect (Ax) ... )
206
+ names = axisnames (Ax )
198
207
idx = findfirst (names, name)
199
208
idx == 0 && error (" axis $name not found in array axes $names " )
200
209
idx
@@ -230,8 +239,8 @@ Base.similar{T}(A::AxisArray{T}, S::Type, axs::Axis...) = similar(A, S, axs)
230
239
push! (ax. args, :(axes (A, Axis{$ d})))
231
240
end
232
241
to_delete = Int[]
233
- for i= 1 : Tuples . length (axs)
234
- a = Tuples . getindex ( axs, i)
242
+ for i= 1 : length (axs. parameters )
243
+ a = axs. parameters[i]
235
244
d = axisdim (A, a)
236
245
axistype (a) <: Tuple{} && push! (to_delete, d)
237
246
sz. args[d] = :(length (axs[$ i]. val))
@@ -248,15 +257,28 @@ end
248
257
# A simple display method to include axis information. It might be nice to
249
258
# eventually display the axis labels alongside the data array, but that is
250
259
# much more difficult.
251
- @compat function Base. show {T,N} (io:: IO , m:: MIME"text/plain" , A:: AxisArray{T,N} )
252
- println (io, " $N -dimensional AxisArray{$T ,$N ,...} with axes:" )
253
- for (name, val) in zip (axisnames (A), axisvalues (A))
254
- print (io, " :$name , " )
255
- show (IOContext (io, :limit => true ), val)
256
- println (io)
260
+ if VERSION < v " 0.5.0-dev"
261
+ function Base. writemime {T,N} (io:: IO , m:: MIME"text/plain" , A:: AxisArray{T,N} )
262
+ println (io, " $N -dimensional AxisArray{$T ,$N ,...} with axes:" )
263
+ for (name, val) in zip (axisnames (A), axisvalues (A))
264
+ print (io, " :$name , " )
265
+ Base. showlimited (io, val)
266
+ println (io)
267
+ end
268
+ print (io, " And data, a " )
269
+ writemime (io, m, A. data)
270
+ end
271
+ else
272
+ function Base. show {T,N} (io:: IO , m:: MIME"text/plain" , A:: AxisArray{T,N} )
273
+ println (io, " $N -dimensional AxisArray{$T ,$N ,...} with axes:" )
274
+ for (name, val) in zip (axisnames (A), axisvalues (A))
275
+ print (io, " :$name , " )
276
+ show (IOContext (io, :limit => true ), val)
277
+ println (io)
278
+ end
279
+ print (io, " And data, a " )
280
+ show (io, m, A. data)
257
281
end
258
- print (io, " And data, a " )
259
- show (io, m, A. data)
260
282
end
261
283
262
284
# Custom methods specific to AxisArrays
@@ -268,13 +290,19 @@ end
268
290
269
291
Returns the axis names of an AxisArray or list of Axises as a tuple of Symbols.
270
292
""" ->
271
- axisnames {T,N,D,Ax} (:: AxisArray{T,N,D,Ax} ) = axisnames (Tuples. collect (Ax)... )
272
- axisnames {T,N,D,Ax} (:: Type{AxisArray{T,N,D,Ax}} ) = axisnames (Tuples. collect (Ax)... )
293
+ axisnames {T,N,D,Ax} (:: AxisArray{T,N,D,Ax} ) = _axisnames (Ax)
294
+ axisnames {T,N,D,Ax} (:: Type{AxisArray{T,N,D,Ax}} ) = _axisnames (Ax)
295
+ axisnames {Ax<:Tuple{Vararg{Axis}}} (:: Type{Ax} ) = _axisnames (Ax)
296
+ @pure _axisnames (Ax) = axisnames (Ax. parameters... )
273
297
axisnames () = ()
274
298
axisnames {name } (:: Axis{name} , B:: Axis... ) = tuple (name, axisnames (B... )... )
275
299
axisnames {name } (:: Type{Axis{name}} , B:: Type... ) = tuple (name, axisnames (B... )... )
276
300
axisnames {name,T} (:: Type{Axis{name,T}} , B:: Type... ) = tuple (name, axisnames (B... )... )
277
301
302
+ axisname {name,T} (:: Type{Axis{name,T}} ) = name
303
+ axisname {name } (:: Type{Axis{name }} ) = name
304
+ axisname (ax:: Axis ) = axisname (typeof (ax))
305
+
278
306
@doc """
279
307
axisvalues(A::AxisArray) -> (AbstractVector...)
280
308
axisvalues(ax::Axis...) -> (AbstractVector...)
0 commit comments