Skip to content

Commit 83b4fde

Browse files
authored
Merge pull request #97 from ajkeller34/morevalue
Indexing by an array of values
2 parents fd2a6a5 + 448480b commit 83b4fde

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,18 @@ And data, a 1-element Array{Float64,1}:
116116
6.14454
117117
```
118118

119+
You can even index by multiple values by broadcasting `atvalue` over an array:
120+
121+
```julia
122+
julia> A[atvalue.([2.5e-5s, 75.0µs])]
123+
2-dimensional AxisArray{Float64,2,...} with axes:
124+
:time, Quantity{Float64, Dimensions:{𝐓}, Units:{s}}[2.5e-5 s, 7.5e-5 s]
125+
:chan, Symbol[:c1, :c2]
126+
And data, a 2×2 Array{Float64,2}:
127+
6.14454 12.2891
128+
1.37825 2.75649
129+
```
130+
119131
Sometimes, though, what we're really interested in is a window of time about a
120132
specific index. One of the operations above (looking for values in the window from 40µs
121133
to 220µs) might be more clearly expressed as a symmetrical window about a

docs/src/index.md

+12
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,18 @@ And data, a 1-element Array{Float64,1}:
137137
6.14454
138138
```
139139

140+
You can even index by multiple values by broadcasting `atvalue` over an array:
141+
142+
```jldoctest
143+
julia> A[atvalue.([2.5e-5s, 75.0µs])]
144+
2-dimensional AxisArray{Float64,2,...} with axes:
145+
:time, Quantity{Float64, Dimensions:{𝐓}, Units:{s}}[2.5e-5 s, 7.5e-5 s]
146+
:chan, Symbol[:c1, :c2]
147+
And data, a 2×2 Array{Float64,2}:
148+
6.14454 12.2891
149+
1.37825 2.75649
150+
```
151+
140152
Sometimes, though, what we're really interested in is a window of time about a
141153
specific index. One of the operations above (looking for values in the window from 40µs
142154
to 220µs) might be more clearly expressed as a symmetrical window about a

src/indexing.jl

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ struct Value{T}
88
end
99
Value(x, tol=Base.rtoldefault(typeof(x))*abs(x)) = Value(promote(x,tol)...)
1010
atvalue(x; rtol=Base.rtoldefault(typeof(x)), atol=zero(x)) = Value(x, atol+rtol*abs(x))
11+
const Values = AbstractArray{<:Value}
1112

1213
# For throwing a BoundsError with a Value index, we need to define the following
1314
# (note that we could inherit them for free, were Value <: Number)
@@ -287,6 +288,9 @@ end
287288
elseif I[i] <: AbstractArray{Bool}
288289
push!(ex.args, :(find(I[$i])))
289290
n += 1
291+
elseif I[i] <: Values
292+
push!(ex.args, :(axisindexes.(A.axes[$i], I[$i])))
293+
n += 1
290294
elseif I[i] <: CartesianIndex
291295
for j = 1:length(I[i])
292296
push!(ex.args, :(I[$i][$j]))

test/indexing.jl

+5
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ A = AxisArray([1 2; 3 4], Axis{:x}([1.0,4.0]), Axis{:y}([2.0,6.1]))
196196
@test @inferred(A[Axis{:x}(atvalue(2.0, atol=5))]) == [1,2]
197197
@test_throws BoundsError A[Axis{:x}(atvalue(4.00000001, rtol=0))]
198198

199+
# Indexing by array of values
200+
A = AxisArray([1 2 3 4; 5 6 7 8; 9 10 11 12], -1:1, [5.1, 5.4, 5.7, 5.8])
201+
@test @inferred(A[atvalue(-1), atvalue.([5.1, 5.7])]) == [1, 3]
202+
@test_throws BoundsError A[atvalue.([1,2])]
203+
199204
# Indexing by value into an OffsetArray
200205
A = AxisArray(OffsetArrays.OffsetArray([1 2; 3 4], 0:1, 1:2),
201206
Axis{:x}([1.0,4.0]), Axis{:y}([2.0,6.1]))

0 commit comments

Comments
 (0)