Skip to content

WIP: Access the values of an axis with getproperty #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,21 @@ And data, a 2400001×2 Array{Float64,2}:

```

AxisArrays behave like regular arrays, but they additionally use the axis
information to enable all sorts of fancy behaviors. For example, we can specify
AxisArrays behave like regular arrays, but they carry extra information about
their axes along with them:

```jldoctest
julia> A.time
0.0 s:2.5e-5 s:60.0 s

julia> A.chan
2-element Array{Symbol,1}:
:c1
:c2

```

This enables all sorts of fancy indexing behaviors. For example, we can specify
indices in *any* order, just so long as we annotate them with the axis name:

```jldoctest
Expand Down
14 changes: 14 additions & 0 deletions src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,20 @@ function axisdim(::Type{AxisArray{T,N,D,Ax}}, ::Type{<:Axis{name,S} where S}) wh
idx
end

# Access to the values of axes by name
function Base.getproperty(A::AxisArray, name::Symbol)
if name === :data || name === :axes
getfield(A, name)
else
# Other things are axis names
getfield(A, :axes)[axisdim(A, Axis{name})].val
end
end
function Base.propertynames(A::AxisArray, private=false)
ns = axisnames(A)
private ? (ns..., :data, :axes) : ns
end

# Base definitions that aren't provided by AbstractArray
@inline Base.size(A::AxisArray) = size(A.data)
@inline Base.size(A::AxisArray, Ax::Axis) = size(A.data, axisdim(A, Ax))
Expand Down
12 changes: 12 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,18 @@ A = @inferred(AxisArray(reshape(1:24, 2,3,4),
@test axisdim(A, Axis{:x}) == axisdim(A, Axis{:x}()) == 1
@test axisdim(A, Axis{:y}) == axisdim(A, Axis{:y}()) == 2
@test axisdim(A, Axis{:z}) == axisdim(A, Axis{:z}()) == 3
# Test that getproperty is fully inferred when a const name is supplied
let getx(A) = A.x,
getz(A) = A.z,
getdata(A) = A.data
@test @inferred(getx(A)) == A.axes[1].val
@test @inferred(getz(A)) == A.axes[3].val
@test @inferred(AxisArrays.axes(A)) === A.axes
@test @inferred(getdata(A)) === A.data
end
@test propertynames(A) == (:x, :y, :z)
@test propertynames(A, true) == (:x, :y, :z, :data, :axes)

# Test axes
@test @inferred(AxisArrays.axes(A)) == (Axis{:x}(.1:.1:.2), Axis{:y}(1//10:1//10:3//10), Axis{:z}(["a", "b", "c", "d"]))
@test @inferred(AxisArrays.axes(A, Axis{:x})) == @inferred(AxisArrays.axes(A, Axis{:x}())) == Axis{:x}(.1:.1:.2)
Expand Down