Skip to content

Commit a3eb224

Browse files
committed
Document, export, and test axes
I think it is important to export axes as using the Axis{} argument form is the only way to get a type-stable axis result
1 parent ff25e6f commit a3eb224

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

src/AxisArrays.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module AxisArrays
22

3-
export AxisArray, Axis, Interval, axisnames, axisdim
3+
export AxisArray, Axis, Interval, axisnames, axisdim, axes
44

55
include("core.jl")
66
include("intervals.jl")

src/core.jl

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ symbols for column headers.
7878
A = AxisArray(reshape(1:15, 5,3), (.1:.1:0.5, [:a, :b, :c]), (:time, :col))
7979
A[Axis{:time}(1:3)] # equivalent to A[1:3,:]
8080
A[Axis{:time}(Interval(.2,.4))] # restrict the AxisArray along the time axis
81-
A[Interval(0.,.3), [:a, :c]] # select an interval and two columns
81+
A[Interval(0.,.3), [:a, :c]] # select an interval and two columns
8282
```
8383
8484
""" ->
@@ -151,7 +151,7 @@ Base.isempty(ax::Axis) = isempty(ax.I)
151151
axisdim(::AxisArray, ::Axis) -> Int
152152
axisdim(::AxisArray, ::Type{Axis}) -> Int
153153
154-
Given an AxisArray and an Axis, return the integer dimension of
154+
Given an AxisArray and an Axis, return the integer dimension of
155155
the Axis within the array.
156156
""" ->
157157
axisdim(A::AxisArray, ax::Axis) = axisdim(A, typeof(ax))
@@ -182,10 +182,23 @@ Returns the axis names of an AxisArray or AxisArray Type as a tuple of symbols.
182182
""" ->
183183
axisnames(A::AxisArray) = axisnames(typeof(A))
184184
axisnames{T,N,D,names,Ax}(::Type{AxisArray{T,N,D,names,Ax}}) = names
185-
axisnames{T,N,D,names,Ax}(::Type{AxisArray{T,N,D,names,Ax}}) = names
186185
axisnames{T,N,D,names}(::Type{AxisArray{T,N,D,names}}) = names
186+
187+
@doc """
188+
axes(A::AxisArray) -> (AbstractVector...)
189+
axes(A::AxisArray, ax::Axis) -> AbstractVector
190+
191+
Returns the tuple of axis vectors for an AxisArray. If an specific `Axis` is
192+
specified, then only that axis vector is returned. Note that when extracting a
193+
single axis vector, `axes(A, Axis{1})`) is type-stable and will perform better
194+
than `axes(A)[1]`.
195+
""" ->
187196
axes(A::AxisArray) = A.axes
188-
axes(A::AxisArray,i::Int) = A.axes[i]
197+
axes(A::AxisArray, ax::Axis) = axes(A, typeof(ax))
198+
stagedfunction axes{T<:Axis}(A::AxisArray, ax::Type{T})
199+
dim = axisdim(A, T)
200+
:(A.axes[$dim])
201+
end
189202

190203
### Axis traits ###
191204
abstract AxisType

test/runtests.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,12 @@ A = AxisArray(1:3, (1:3,), (:a,))
115115
@test axisnames(A) == (:a,)
116116

117117
# Test axisdim
118-
A = AxisArray(reshape(1:24, 2,3,4), (:x,:y,:z))
118+
A = AxisArray(reshape(1:24, 2,3,4), (:x,:y,:z), (.1:.1:.2, 1//10:1//10:3//10, ["a", "b", "c", "d"]))
119119
@test axisdim(A, Axis{:x}) == axisdim(A, Axis{:x}()) == 1
120120
@test axisdim(A, Axis{:y}) == axisdim(A, Axis{:y}()) == 2
121121
@test axisdim(A, Axis{:z}) == axisdim(A, Axis{:z}()) == 3
122+
# Test axes
123+
@test @inferred(axes(A)) == (.1:.1:.2, 1//10:1//10:3//10, ["a", "b", "c", "d"])
124+
@test @inferred(axes(A, Axis{:x})) == @inferred(axes(A, Axis{:x}())) == .1:.1:.2
125+
@test @inferred(axes(A, Axis{:y})) == @inferred(axes(A, Axis{:y}())) == 1//10:1//10:3//10
126+
@test @inferred(axes(A, Axis{:z})) == @inferred(axes(A, Axis{:z}())) == ["a", "b", "c", "d"]

0 commit comments

Comments
 (0)