1
- julia> #= ==== Examples of how to use the AxisArrays package. =====#
1
+ julia> #= ==== Examples of how to use the AxisKeys package. =====#
2
2
3
- (v1.3 ) pkg> add https : // github . com / mcabbott / AxisKeys. jl # Not a registered package
3
+ (v1.3 ) pkg> add AxisKeys # Now registered
4
4
5
5
julia> using AxisKeys, Random
6
6
7
7
julia> Random. seed! (42 );
8
8
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
+
9
17
julia> D = wrapdims (rand (Int8,5 ), iter = 10 : 10 : 50 ) # Convenience constructor
10
18
1 - dimensional KeyedArray (NamedDimsArray (... )) with range:
11
19
↓ iter ∈ 5 - element StepRange{Int64,... }
@@ -39,7 +47,7 @@ julia> @view E[col=1] # Fixing one index gives a slice
39
47
↓ row - 105
40
48
3
41
49
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 ))
43
51
2 - dimensional KeyedArray (NamedDimsArray (... )) with ranges:
44
52
↓ obs ∈ 2 - element Vector{String}
45
53
→ time ∈ 10 - element StepRangeLen{Float64,... }
@@ -48,7 +56,7 @@ And data, 2×10 Array{Float64,2}:
48
56
(" dog" ) 0.160006 0.602298 0.383491 0.745181 0.0823367 0.452418 0.281987
49
57
(" cat" ) 1.42296 1.36346 1.59291 1.26281 1.24468 1.76372 1.14364
50
58
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
52
60
(:obs , :time )
53
61
54
62
julia> axiskeys (C) # Likewise, axiskeys(C, :time) == 0:0.5:4.5
@@ -89,10 +97,10 @@ And data, 2×2 Array{Float64,2}:
89
97
(" cat" ) 5.85958 21.8234
90
98
91
99
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 )
93
101
94
102
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" ]
96
104
97
105
julia> for (i,t) in enumerate (C. time)
98
106
t > 3 && println (" at time $t , value cat = " , C[2 ,i])
@@ -178,7 +186,19 @@ And data, 3×81×2 Array{Float64,3}:
178
186
(:b ) 0.123933 0.988803 0.243089 0.701553 0.11737
179
187
(:c ) 0.850917 0.0495313 0.0470764 0.322251 0.642556
180
188
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
184
190
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
0 commit comments