Skip to content

Commit 4988b2c

Browse files
author
Michael Abbott
committed
docs
1 parent b98244e commit 4988b2c

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Build Status](https://travis-ci.org/mcabbott/AxisKeys.jl.svg?branch=master)](https://travis-ci.org/mcabbott/AxisKeys.jl)
44

5-
<img src="docs/readmefigure.png" alt="block picture" width="400" align="right">
5+
<!--<img src="docs/readmefigure.png" alt="block picture" width="400" align="right">-->
66

77
This package defines a thin wrapper which, alongside any array, stores a vector of "keys"
88
for each dimension. This may be useful to store perhaps actual times of measurements,

docs/repl.jl

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
julia> #===== Examples of how to use the AxisArrays package. =====#
1+
julia> #===== Examples of how to use the AxisKeys package. =====#
22

3-
(v1.3) pkg> add https://github.com/mcabbott/AxisKeys.jl # Not a registered package
3+
(v1.3) pkg> add AxisKeys # Now registered
44

55
julia> using AxisKeys, Random
66

77
julia> Random.seed!(42);
88

9+
julia> KeyedArray((a=3, b=5, c=7)) # Using keys of a NamedTuple
10+
1-dimensional KeyedArray(...) with keys:
11+
3-element Vector{Symbol}
12+
And data, 3-element Array{Int64,1}:
13+
(:a) 3
14+
(:b) 5
15+
(:c) 7
16+
917
julia> D = wrapdims(rand(Int8,5), iter = 10:10:50) # Convenience constructor
1018
1-dimensional KeyedArray(NamedDimsArray(...)) with range:
1119
iter 5-element StepRange{Int64,...}
@@ -39,7 +47,7 @@ julia> @view E[col=1] # Fixing one index gives a slice
3947
row -105
4048
3
4149

42-
julia> C = wrapdims(rand(2,10) .+ (0:1), obs=["dog", "cat"], time=range(0, step=0.5, length=10))
50+
julia> C = KeyedArray(rand(2,10) .+ (0:1), obs=["dog", "cat"], time=range(0, step=0.5, length=10))
4351
2-dimensional KeyedArray(NamedDimsArray(...)) with ranges:
4452
obs 2-element Vector{String}
4553
time 10-element StepRangeLen{Float64,...}
@@ -48,7 +56,7 @@ And data, 2×10 Array{Float64,2}:
4856
("dog") 0.160006 0.602298 0.383491 0.745181 0.0823367 0.452418 0.281987
4957
("cat") 1.42296 1.36346 1.59291 1.26281 1.24468 1.76372 1.14364
5058

51-
julia> names(C) # Works like size & axes, i.e. names(C,2) == :time
59+
julia> dimnames(C) # Works like size & axes, i.e. dimnames(C,2) == :time
5260
(:obs, :time)
5361

5462
julia> axiskeys(C) # Likewise, axiskeys(C, :time) == 0:0.5:4.5
@@ -89,10 +97,10 @@ And data, 2×2 Array{Float64,2}:
8997
("cat") 5.85958 21.8234
9098

9199
julia> ans("mouse")
92-
ERROR: key of type String is ambiguous, matches dimensions (1, 2)
100+
ERROR: ArgumentError: key of type String is ambiguous, matches dimensions (1, 2)
93101

94102
julia> C("mouse")
95-
ERROR: could not find key "mouse" in range ["dog", "cat"]
103+
ERROR: ArgumentError: could not find key "mouse" in vector ["dog", "cat"]
96104

97105
julia> for (i,t) in enumerate(C.time)
98106
t > 3 && println("at time $t, value cat = ", C[2,i])
@@ -178,7 +186,19 @@ And data, 3×81×2 Array{Float64,3}:
178186
(:b) 0.123933 0.988803 0.243089 0.701553 0.11737
179187
(:c) 0.850917 0.0495313 0.0470764 0.322251 0.642556
180188

181-
# Ranges are printed with colours based on eltype, btw!
182-
183-
julia> H(:a, -14, "one") # uses UniqueVector's fast lookup
189+
julia> H(:a, -14, "one") # uses the UniqueVector's fast lookup
184190
0.9948971186701887
191+
192+
julia> using LazyStack # A package for concatenating arrays
193+
194+
julia> stack(:pre, n .* D for n in 1:10)
195+
2-dimensional NamedDimsArray(KeyedArray(...)) with keys:
196+
iter 5-element StepRange{Int64,...}
197+
pre 10-element OneTo{Int}
198+
And data, 5×10 Array{Int64,2}:
199+
(1) (2) (3) (4) (5) (6) (7) (8) (9) (10)
200+
(10) 115 230 345 460 575 690 805 920 1035 1150
201+
(20) 99 198 297 396 495 594 693 792 891 990
202+
(30) 0 0 0 0 0 0 0 0 0 0
203+
(40) 57 114 171 228 285 342 399 456 513 570
204+
(50) 88 176 264 352 440 528 616 704 792 880

docs/speed.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ and of other packages of similar or overlapping concerns.
44
Plus generally a place to collect their various syntax, for comparison.
55
=#
66

7-
using AxisKeys, BenchmarkTools # Julia 1.3 + macbook escape
7+
using AxisKeys, BenchmarkTools # Julia 1.4 + macbook escape
88

99
#==============================#
1010
#===== getkey vs getindex =====#
@@ -19,9 +19,9 @@ bothmat2 = wrapdims(mat.data, x=collect(11:13), y=collect(21:24))
1919

2020
@btime $bothmat[3,4] # 1.700 ns
2121
@btime $bothmat[x=3, y=4] # 1.701 ns
22-
@btime $bothmat(13, 24) # 5.874 ns
23-
@btime $bothmat(x=13, y=24) # 43.302 ns (2 allocations: 64 bytes)
24-
@btime $bothmat2(13, 24) # 16.719 ns
22+
@btime $bothmat(13, 24) # 5.874 ns -- fast range lookup
23+
@btime $bothmat(x=13, y=24) # 14.063 ns
24+
@btime $bothmat2(13, 24) # 16.719 ns -- collected vector
2525

2626
ind_collect(A) = [@inbounds(A[ijk...]) for ijk in Iterators.ProductIterator(axes(A))]
2727
key_collect(A) = [@inbounds(A(vals...)) for vals in Iterators.ProductIterator(axiskeys(A))]
@@ -31,14 +31,14 @@ bigmat2 = wrapdims(rand(100,100), collect(1:100), collect(1:100));
3131

3232
@btime ind_collect($(bigmat.data)); # 9.117 μs (4 allocations: 78.25 KiB)
3333
@btime ind_collect($bigmat); # 11.530 μs (4 allocations: 78.25 KiB)
34-
@btime key_collect($bigmat); # 20.671 μs (4 allocations: 78.27 KiB) -- fast range lookup
34+
@btime key_collect($bigmat); # 64.064 μs (4 allocations: 78.27 KiB) -- fast range lookup, was 20μs!
3535
@btime key_collect($bigmat2); # 718.804 μs (5 allocations: 78.27 KiB) -- findfirst(..., vector) lookup
3636

3737
twomat = wrapdims(mat.data, x=[:a, :b, :c], y=21:24)
38-
@btime $twomat(x=:a, y=24) # 57.396 ns (2 allocations: 64 bytes)
38+
@btime $twomat(x=:a, y=24) # 36.734 ns (2 allocations: 64 bytes)
3939

4040
@btime $twomat(24.0) # 26.686 ns (4 allocations: 112 bytes)
41-
@btime $twomat(y=24.0) # 52.372 ns (6 allocations: 144 bytes)
41+
@btime $twomat(y=24.0) # 33.860 ns (4 allocations: 112 bytes)
4242
@btime view($twomat, :,3) # 24.951 ns (4 allocations: 112 bytes)
4343

4444

@@ -50,7 +50,7 @@ using OffsetArrays #===== OffsetArrays =====#
5050

5151
of1 = OffsetArray(rand(3,4), 11:13, 21:24)
5252

53-
@btime $of1[13,24] # 3.652 ns
53+
@btime $of1[13,24] # 1.700 ns
5454

5555
bigoff = OffsetArray(bigmat.data, 1:100, 1:100);
5656
@btime ind_collect($bigoff); # 15.372 μs (5 allocations: 78.30 KiB)

0 commit comments

Comments
 (0)