Skip to content
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

Add a SortedVector for keyed axis indexes and hierarchical indexing #16

Merged
merged 1 commit into from
Mar 9, 2015
Merged
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
1 change: 1 addition & 0 deletions src/AxisArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export AxisArray, Axis, Interval, axisnames, axisvalues, axisdim, axes
include("core.jl")
include("intervals.jl")
include("indexing.jl")
include("sortedvector.jl")
include("utils.jl")

end
2 changes: 1 addition & 1 deletion src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ checkaxis(ax) = checkaxis(axistrait(ax), ax)
checkaxis(::Type{Unsupported}, ax) = nothing # TODO: warn or error?
# Dimensional axes must be monotonically increasing
checkaxis{T}(::Type{Dimensional}, ax::Range{T}) = step(ax) > zero(T) || error("Dimensional axes must be monotonically increasing")
checkaxis(::Type{Dimensional}, ax) = issorted(ax, lt=(<=)) || error("Dimensional axes must be monotonically increasing")
checkaxis(::Type{Dimensional}, ax) = issorted(ax) || error("Dimensional axes must be monotonically increasing")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/monotonically increasing/sorted in increasing order'

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Wikipedia, "monotonically increasing" is correct here. The prior implementation checked for "strictly increasing".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, I had those backwards in my head. Sorry about that.

# Categorical axes must simply be unique
function checkaxis(::Type{Categorical}, ax)
seen = Set{eltype(ax)}()
Expand Down
2 changes: 2 additions & 0 deletions src/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ axisindexes(ax, idx) = axisindexes(axistrait(ax.val), ax.val, idx)
axisindexes(::Type{Unsupported}, ax, idx) = error("elementwise indexing is not supported for axes of type $(typeof(ax))")
# Dimensional axes may be indexed by intervals of their elements
axisindexes{T}(::Type{Dimensional}, ax::AbstractVector{T}, idx::Interval{T}) = searchsorted(ax, idx)
# Dimensional axes may also be indexed directy by their elements
axisindexes{T}(::Type{Dimensional}, ax::AbstractVector{T}, idx::T) = searchsorted(ax, Interval(idx,idx))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to construct the Interval type here - searchsorted should work just fine with a single value.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I'll give that a try.

# Categorical axes may be indexed by their elements
function axisindexes{T}(::Type{Categorical}, ax::AbstractVector{T}, idx::T)
i = findfirst(ax, idx)
Expand Down
100 changes: 100 additions & 0 deletions src/sortedvector.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@

export SortedVector

@doc """

A SortedVector is an AbstractVector where the underlying data is
ordered (monotonically increasing).

Indexing that would unsort the data is prohibited. A SortedVector is a
Dimensional axis, and no checking is done to ensure that the data is
sorted. Duplicate values are allowed.

A SortedVector axis can be indexed with an Interval, with a value, or
with a vector of values. Use of a SortedVector{Tuple} axis allows
indexing similar to the hierarchical index of the Python Pandas
package or the R data.table package.

### Constructors

```julia
SortedVector(x::AbstractVector)
```

### Keyword Arguments

* `x::AbstractVector` : the wrapped vector

### Examples

```julia
v = SortedVector(collect([1.; 10.; 10:15.]))
A = AxisArray(reshape(1:16, 8, 2), v, [:a, :b])
A[Interval(8.,12.), :]
A[1., :]
A[10., :]

## Hierarchical index example with three key levels

data = reshape(1.:40., 20, 2)
v = collect(zip([:a, :b, :c][rand(1:3,20)], [:x,:y][rand(1:2,20)], [:x,:y][rand(1:2,20)]))
idx = sortperm(v)
A = AxisArray(data[idx,:], SortedVector(v[idx]), [:a, :b])
A[:b, :]
A[[:a,:c], :]
A[(:a,:x), :]
A[(:a,:x,:x), :]
A[Interval(:a,:b), :]
A[Interval((:a,:x),(:b,:x)), :]
```

""" ->
immutable SortedVector{T} <: AbstractVector{T}
data::AbstractVector{T}
end

Base.getindex(v::SortedVector, idx::Int) = v.data[idx]
Base.getindex(v::SortedVector, idx::Range1) = SortedVector(v.data[idx])
Base.getindex(v::SortedVector, idx::StepRange) =
step(idx) > 0 ? SortedVector(v.data[idx]) : error("step must be positive to index a SortedVector")
Base.getindex(v::SortedVector, idx::AbstractVector) =
issorted(idx) ? SortedVector(v.data[idx]) : error("index must be monotonically increasing to index a SortedVector")

Base.length(v::SortedVector) = length(v.data)
Base.size(v::SortedVector) = size(v.data)
Base.size(v::SortedVector, i) = size(v.data, i)

axistrait(::SortedVector) = Dimensional
checkaxis(::SortedVector) = nothing


## Add some special indexing for SortedVector{Tuple}'s to achieve something like
## Panda's hierarchical indexing

axisindexes{T<:Tuple,S}(ax::Axis{S,SortedVector{T}}, idx) =
searchsorted(ax.val, idx, 1, length(ax.val), Base.ord(_isless,identity,false,Base.Forward))

axisindexes{T<:Tuple,S}(ax::Axis{S,SortedVector{T}}, idx::AbstractArray) =
vcat([axisindexes(ax, i) for i in idx]...)


## Use a modification of `isless`, so that (:a,) is not less than (:a, :b).
## This allows for more natural indexing.

_isless(x,y) = isless(x,y)

function _isless(t1::Tuple, t2::Tuple)
n1, n2 = length(t1), length(t2)
for i = 1:min(n1, n2)
a, b = t1[i], t2[i]
if !isequal(a, b)
return _isless(a, b)
end
end
return false
end
_isless{T<:Tuple}(t1::Interval{T}, t2::Tuple) = _isless(t1, Interval(t2,t2))
_isless{T<:Tuple}(t1::Tuple, t2::Interval{T}) = _isless(Interval(t1,t1), t2)
_isless(t1::Tuple, t2) = _isless(t1,(t2,))
_isless(t1, t2::Tuple) = _isless((t1,),t2)
_isless(a::Interval, b::Interval) = _isless(a.hi, b.lo)
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ using Base.Test

include("core.jl")
include("indexing.jl")
include("sortedvector.jl")
21 changes: 21 additions & 0 deletions test/sortedvector.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

# Test SortedVector
v = SortedVector(collect([1.; 10.; 10:15.]))
A = AxisArray(reshape(1:16, 8, 2), v, [:a, :b])
@test A[Interval(8.,12.), :] == A[2:5, :]
@test A[1., :] == A[1, :]
@test A[10., :] == A[2:3, :]

# Test SortedVector with a hierarchical index (indexed using Tuples)
srand(1234)
data = reshape(1.:40., 20, 2)
v = collect(zip([:a, :b, :c][rand(1:3,20)], [:x,:y][rand(1:2,20)], [:x,:y][rand(1:2,20)]))
idx = sortperm(v)
A = AxisArray(data[idx,:], SortedVector(v[idx]), [:a, :b])
@test A[:b, :] == A[5:12, :]
@test A[[:a,:c], :] == A[[1:4;13:end], :]
@test A[(:a,:y), :] == A[2:4, :]
@test A[(:c,:y,:y), :] == A[16:end, :]
@test A[Interval(:a,:b), :] == A[1:12, :]
@test A[Interval((:a,:x),(:b,:x)), :] == A[1:9, :]
@test A[[Interval((:a,:x),(:b,:x)),:c], :] == A[[1:9;13:end], :]