Skip to content

Commit 8e9ace9

Browse files
authored
Merge pull request #86 from invenia/custom-index-docs
Docs for functions related to custom indexing behaviour
2 parents ff82bc9 + 83a275c commit 8e9ace9

File tree

4 files changed

+57
-24
lines changed

4 files changed

+57
-24
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
*.cov
22
*.mem
3-
doc/build
4-
doc/site
3+
docs/build
4+
docs/site

src/core.jl

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ end
1010

1111
const Symbols = Tuple{Symbol,Vararg{Symbol}}
1212

13-
@doc """
13+
"""
1414
Type-stable axis-specific indexing and identification with a
1515
parametric type.
1616
@@ -47,7 +47,7 @@ A[Axis{:row}(2)] # grabs the second row
4747
A[Axis{2}(2:5)] # grabs the second through 5th columns
4848
```
4949
50-
""" ->
50+
"""
5151
immutable Axis{name,T}
5252
val::T
5353
end
@@ -70,7 +70,7 @@ Base.length(A::Axis) = length(A.val)
7070
Base.convert{name,T}(::Type{Axis{name,T}}, ax::Axis{name,T}) = ax
7171
Base.convert{name,T}(::Type{Axis{name,T}}, ax::Axis{name}) = Axis{name}(convert(T, ax.val))
7272

73-
@doc """
73+
"""
7474
An AxisArray is an AbstractArray that wraps another AbstractArray and
7575
adds axis names and values to each array dimension. AxisArrays can be indexed
7676
by using the named axes as an alternative to positional indexing by
@@ -130,16 +130,10 @@ Two main types of axes supported by default include:
130130
131131
User-defined axis types can be added along with custom indexing
132132
behaviors. To add add a custom type as a Categorical or Dimensional
133-
axis, add a trait using `AxisArrays.axistrait`. Here is the example of
134-
adding a custom Dimensional axis:
135-
136-
```julia
137-
AxisArrays.axistrait(v::MyCustomAxis) = AxisArrays.Dimensional
138-
```
133+
axis, add a trait using [`AxisArrays.axistrait`](@ref).
139134
140135
For more advanced indexing, you can define custom methods for
141-
`AxisArrays.axisindexes`.
142-
136+
[`AxisArrays.axisindexes`](@ref).
143137
144138
### Examples
145139
@@ -154,7 +148,7 @@ A[Axis{:time}(ClosedInterval(.2,.4))] # restrict the AxisArray along the time ax
154148
A[ClosedInterval(0.,.3), [:a, :c]] # select an interval and two columns
155149
```
156150
157-
""" ->
151+
"""
158152
immutable AxisArray{T,N,D,Ax} <: AbstractArray{T,N}
159153
data::D # D <:AbstractArray, enforced in constructor to avoid dispatch bugs (https://github.com/JuliaLang/julia/issues/6383)
160154
axes::Ax # Ax<:NTuple{N, Axis}, but with specialized Axis{...} types
@@ -229,13 +223,13 @@ HasAxes{A<:AbstractArray}(::Type{A}) = HasAxes{false}()
229223
HasAxes(A::AbstractArray) = HasAxes(typeof(A))
230224

231225
# Axis definitions
232-
@doc """
226+
"""
233227
axisdim(::AxisArray, ::Axis) -> Int
234228
axisdim(::AxisArray, ::Type{Axis}) -> Int
235229
236230
Given an AxisArray and an Axis, return the integer dimension of
237231
the Axis within the array.
238-
""" ->
232+
"""
239233
axisdim(A::AxisArray, ax::Axis) = axisdim(A, typeof(ax))
240234
@generated function axisdim{T<:Axis}(A::AxisArray, ax::Type{T})
241235
dim = axisdim(A, T)
@@ -460,14 +454,14 @@ function Base.summary(A::AxisArray)
460454
end
461455

462456
# Custom methods specific to AxisArrays
463-
@doc """
457+
"""
464458
axisnames(A::AxisArray) -> (Symbol...)
465459
axisnames(::Type{AxisArray{...}}) -> (Symbol...)
466460
axisnames(ax::Axis...) -> (Symbol...)
467461
axisnames(::Type{Axis{...}}...) -> (Symbol...)
468462
469463
Returns the axis names of an AxisArray or list of Axises as a tuple of Symbols.
470-
""" ->
464+
"""
471465
axisnames{T,N,D,Ax}(::AxisArray{T,N,D,Ax}) = _axisnames(Ax)
472466
axisnames{T,N,D,Ax}(::Type{AxisArray{T,N,D,Ax}}) = _axisnames(Ax)
473467
axisnames{Ax<:Tuple{Vararg{Axis}}}(::Type{Ax}) = _axisnames(Ax)
@@ -481,17 +475,17 @@ axisname{name,T}(::Type{Axis{name,T}}) = name
481475
axisname{name }(::Type{Axis{name }}) = name
482476
axisname(ax::Axis) = axisname(typeof(ax))
483477

484-
@doc """
478+
"""
485479
axisvalues(A::AxisArray) -> (AbstractVector...)
486480
axisvalues(ax::Axis...) -> (AbstractVector...)
487481
488482
Returns the axis values of an AxisArray or list of Axises as a tuple of vectors.
489-
""" ->
483+
"""
490484
axisvalues(A::AxisArray) = axisvalues(A.axes...)
491485
axisvalues() = ()
492486
axisvalues(ax::Axis, axs::Axis...) = tuple(ax.val, axisvalues(axs...)...)
493487

494-
@doc """
488+
"""
495489
axes(A::AxisArray) -> (Axis...)
496490
axes(A::AxisArray, ax::Axis) -> Axis
497491
axes(A::AxisArray, dim::Int) -> Axis
@@ -503,7 +497,7 @@ than `axes(A)[1]`.
503497
504498
For an AbstractArray without `Axis` information, `axes` returns the
505499
default axes, i.e., those that would be produced by `AxisArray(A)`.
506-
""" ->
500+
"""
507501
axes(A::AxisArray) = A.axes
508502
axes(A::AxisArray, dim::Int) = A.axes[dim]
509503
axes(A::AxisArray, ax::Axis) = axes(A, typeof(ax))
@@ -520,6 +514,23 @@ immutable Dimensional <: AxisTrait end
520514
immutable Categorical <: AxisTrait end
521515
immutable Unsupported <: AxisTrait end
522516

517+
"""
518+
axistrait(ax::Axis) -> Type{<:AxisTrait}
519+
520+
Returns the indexing type of an `Axis`, any subtype of `AxisTrait`.
521+
The default is `Unsupported`, meaning there is no special indexing behaviour for this axis
522+
and indexes into this axis are passed directly to the underlying array.
523+
524+
Two main types of axes supported by default are `Categorical` and `Dimensional`; see
525+
[Indexing](@ref) for more information on these types.
526+
527+
User-defined axis types can be added along with custom indexing behaviors by defining new
528+
methods of this function. Here is the example of adding a custom Dimensional axis:
529+
530+
```julia
531+
AxisArrays.axistrait(v::MyCustomAxis) = AxisArrays.Dimensional
532+
```
533+
"""
523534
axistrait(::Any) = Unsupported
524535
axistrait(ax::Axis) = axistrait(ax.val)
525536
axistrait{T<:Union{Number, Dates.AbstractTime}}(::AbstractVector{T}) = Dimensional

src/indexing.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,28 @@ end
144144
### Indexing along values of the axes ###
145145

146146
# Default axes indexing throws an error
147+
"""
148+
axisindexes(ax::Axis, axis_idx) -> array_idx
149+
axisindexes(::Type{<:AxisTrait}, axis_values, axis_idx) -> array_idx
150+
151+
Translate an index into an axis into an index into the underlying array.
152+
Users can add additional indexing behaviours for custom axes or custom indices by adding
153+
methods to this function.
154+
155+
## Examples
156+
157+
Add a method for indexing into an `Axis{name, SortedSet}`:
158+
159+
```julia
160+
AxisArrays.axisindexes(::Type{Categorical}, ax::SortedSet, idx::AbstractVector) = findin(collect(ax), idx)
161+
```
162+
163+
Add a method for indexing into a `Categorical` axis with a `SortedSet`:
164+
165+
```julia
166+
AxisArrays.axisindexes(::Type{Categorical}, ax::AbstractVector, idx::SortedSet) = findin(ax, idx)
167+
```
168+
"""
147169
axisindexes(ax, idx) = axisindexes(axistrait(ax.val), ax.val, idx)
148170
axisindexes(::Type{Unsupported}, ax, idx) = error("elementwise indexing is not supported for axes of type $(typeof(ax))")
149171
axisindexes(t, ax, idx) = error("cannot index $(typeof(ax)) with $(typeof(idx)); expected $(eltype(ax)) axis value or Integer index")

src/sortedvector.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
export SortedVector
33

4-
@doc """
4+
"""
55
66
A SortedVector is an AbstractVector where the underlying data is
77
ordered (monotonically increasing).
@@ -48,7 +48,7 @@ A[ClosedInterval(:a,:b), :]
4848
A[ClosedInterval((:a,:x),(:b,:x)), :]
4949
```
5050
51-
""" ->
51+
"""
5252
immutable SortedVector{T} <: AbstractVector{T}
5353
data::AbstractVector{T}
5454
end

0 commit comments

Comments
 (0)