-
Notifications
You must be signed in to change notification settings - Fork 42
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you need to construct the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ using Base.Test | |
|
||
include("core.jl") | ||
include("indexing.jl") | ||
include("sortedvector.jl") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], :] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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'
There was a problem hiding this comment.
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".
There was a problem hiding this comment.
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.