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
55julia> using AxisKeys, Random
66
77julia> 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+
917julia> D = wrapdims (rand (Int8,5 ), iter = 10 : 10 : 50 ) # Convenience constructor
10181 - 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 ))
43512 - 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
5462julia> 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
9199julia> 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
94102julia> 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
97105julia> 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
1841900.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
0 commit comments