Skip to content

Commit b449d1b

Browse files
authored
Merge pull request #46 from JuliaArrays/teh/fix_readme_examples
Test and fix the code in the README
2 parents 60d2a11 + 3015ef6 commit b449d1b

File tree

5 files changed

+104
-9
lines changed

5 files changed

+104
-9
lines changed

src/search.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,16 @@ end
3838

3939
if VERSION > v"0.5.0-dev+4557"
4040
# When running with "--check-bounds=yes" (like on Travis), the bounds-check isn't elided
41+
@inline function Base.unsafe_getindex{T}(v::Range{T}, i::Integer)
42+
convert(T, first(v) + (i-1)*step(v))
43+
end
4144
@inline function Base.unsafe_getindex{T}(r::FloatRange{T}, i::Integer)
4245
convert(T, (r.start + (i-1)*r.step)/r.divisor)
4346
end
47+
@inline function Base.unsafe_getindex{T<:Integer}(r::StepRange, s::Range{T})
48+
st = oftype(r.start, r.start + (first(s)-1)*step(r))
49+
range(st, step(r)*step(s), length(s))
50+
end
4451
@inline function Base.unsafe_getindex(r::FloatRange, s::OrdinalRange)
4552
FloatRange(r.start + (first(s)-1)*r.step, step(s)*r.step, length(s), r.divisor)
4653
end

test/REQUIRE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
OffsetArrays
2+
SIUnits

test/indexing.jl

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,47 @@ v = AxisArray(collect(.1:.1:10.0), .1:.1:10.0)
8080
@test v[:] == v.data[:] == v[Axis{:row}(:)]
8181
@test v[3:8] == v.data[3:8] == v[ClosedInterval(.25,.85)] == v[Axis{:row}(3:8)] == v[Axis{:row}(ClosedInterval(.22,.88))]
8282

83-
# Test repeated intervals
84-
A = AxisArray([1:100 -1:-1:-100], .1:.1:10.0, [:c1, :c2])
85-
@test A[2.0..3.0, :] == A[atindex(-0.5..0.5, 25), :] == [20:30 -20:-1:-30]
86-
@test A[2.0..3.0, [:c1,:c2]] == A[atindex(-0.5..0.5, 25), [:c1, :c2]] == [20:30 -20:-1:-30]
87-
@test A[2.0..3.0, :c1] == A[atindex(-0.5..0.5, 25), :c1] == collect(20:30)
88-
@test A[atindex(-0.5..0.5, 25), :c1] == collect(20:30)
89-
@test A[atindex(-0.5..0.5, [25, 35]), :c1] == [20:30 30:40]
90-
@test_throws BoundsError A[atindex(-0.5..0.5, 5), :c1]
91-
@test_throws BoundsError A[atindex(-0.5..0.5, [5, 15, 25]), :]
83+
# Test repeated intervals, for different range types
84+
85+
# First, since integers mean "location" rather than value, we have to
86+
# create a number type from which we build a StepRange but which is
87+
# not an Int.
88+
module IL # put in a module so this file can be re-run
89+
immutable IntLike <: Number
90+
val::Int
91+
end
92+
Base.one(x::IntLike) = IntLike(0)
93+
Base.zero(x::IntLike) = IntLike(0)
94+
Base.isless(x::IntLike, y::IntLike) = isless(x.val, y.val)
95+
Base.:+(x::IntLike, y::IntLike) = IntLike(x.val+y.val)
96+
Base.:-(x::IntLike, y::IntLike) = IntLike(x.val-y.val)
97+
Base.:/(x::IntLike, y::IntLike) = x.val / y.val
98+
Base.rem(x::IntLike, y::IntLike) = IntLike(rem(x.val, y.val))
99+
Base.div(x::IntLike, y::IntLike) = div(x.val, y.val)
100+
Base.:*(x::IntLike, y::Int) = IntLike(x.val * y)
101+
Base.:*(x::Int, y::IntLike) = y*x
102+
Base.:/(x::IntLike, y::Int) = IntLike(x.val / y)
103+
Base.promote_rule(::Type{IntLike}, ::Type{Int}) = Int
104+
Base.convert(::Type{Int}, x::IntLike) = x.val
105+
using AxisArrays
106+
AxisArrays.axistrait(::AbstractVector{IntLike}) = AxisArrays.Dimensional
107+
end
108+
109+
for (r, Irel) in ((0.1:0.1:10.0, -0.5..0.5), # FloatRange
110+
(IL.IntLike(1):IL.IntLike(1):IL.IntLike(100),
111+
IL.IntLike(-5)..IL.IntLike(5))) # StepRange
112+
Iabs = r[20]..r[30]
113+
A = AxisArray([1:100 -1:-1:-100], r, [:c1, :c2])
114+
@test A[Iabs, :] == A[atindex(Irel, 25), :] == [20:30 -20:-1:-30]
115+
@test A[Iabs, :] == A[r[25]+Irel, :] == [20:30 -20:-1:-30]
116+
@test A[Iabs, [:c1,:c2]] == A[atindex(Irel, 25), [:c1, :c2]] == [20:30 -20:-1:-30]
117+
@test A[Iabs, :c1] == A[atindex(Irel, 25), :c1] == collect(20:30)
118+
@test A[atindex(Irel, 25), :c1] == collect(20:30)
119+
@test A[atindex(Irel, [25, 35]), :c1] == [20:30 30:40]
120+
@test A[r[[25, 35]] + Irel, :c1] == [20:30 30:40]
121+
@test_throws BoundsError A[atindex(Irel, 5), :c1]
122+
@test_throws BoundsError A[atindex(Irel, [5, 15, 25]), :]
123+
end
92124

93125
# Indexing with CartesianIndex{0}
94126
A = AxisArray(reshape(1:15, 3, 5), :x, :y)

test/readme.jl

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Intended to ensure the README stays working (this is a copy)
2+
3+
using AxisArrays, SIUnits
4+
import SIUnits.ShortUnits: s, ms, µs
5+
6+
fs = 40000
7+
y = randn(60*fs+1)*3
8+
for spk = (sin(0.8:0.2:8.6) .* [0:0.01:.1; .15:.1:.95; 1:-.05:.05] .* 50,
9+
sin(0.8:0.4:8.6) .* [0:0.02:.1; .15:.1:1; 1:-.2:.1] .* 50)
10+
i = rand(round(Int,.001fs):1fs)
11+
while i+length(spk)-1 < length(y)
12+
y[i:i+length(spk)-1] += spk
13+
i += rand(round(Int,.001fs):1fs)
14+
end
15+
end
16+
17+
A = AxisArray([y 2y], Axis{:time}(0s:1s/fs:60s), Axis{:chan}([:c1, :c2]))
18+
A[Axis{:time}(4)]
19+
A[Axis{:chan}(:c2), Axis{:time}(1:5)]
20+
ax = A[40µs .. 220µs, :c1]
21+
axes(ax, 1)
22+
A[atindex(-90µs .. 90µs, 5), :c2]
23+
idxs = find(diff(A[:,:c1] .< -15) .> 0)
24+
spks = A[atindex(-200µs .. 800µs, idxs), :c1]
25+
26+
27+
# # A possible "dynamic verification" strategy
28+
# const readmefile = joinpath(dirname(dirname(@__FILE__)), "README.md")
29+
30+
# function extract_julialines(iowr, filein)
31+
# open(filein) do iord
32+
# while !eof(iord)
33+
# line = readline(iord)
34+
# if startswith(line, "julia>")
35+
# print(iowr, line[8:end])
36+
# while !eof(iord)
37+
# line = readline(iord)
38+
# if !startswith(line, " ")
39+
# break
40+
# end
41+
# print(iowr, line[8:end])
42+
# end
43+
# end
44+
# end
45+
# end
46+
# end
47+
48+
# tmpfile, iowr = mktemp()
49+
# extract_julialines(iowr, readmefile)
50+
# close(iowr)
51+
52+
# include(tmpfile)
53+
# rm(tmpfile)

test/runtests.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ include("sortedvector.jl")
1010
include("search.jl")
1111
include("combine.jl")
1212

13+
include("readme.jl")
14+
1315
nothing

0 commit comments

Comments
 (0)