-
Notifications
You must be signed in to change notification settings - Fork 28
/
repl.jl
204 lines (167 loc) Β· 7.02 KB
/
repl.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
julia> #===== Examples of how to use the AxisKeys package. =====#
(v1.3) pkg> add AxisKeys # Now registered
julia> using AxisKeys, Random
julia> Random.seed!(42);
julia> KeyedArray((a=3, b=5, c=7)) # Using keys of a NamedTuple
1-dimensional KeyedArray(...) with keys:
β 3-element Vector{Symbol}
And data, 3-element Array{Int64,1}:
(:a) 3
(:b) 5
(:c) 7
julia> D = wrapdims(rand(Int8,5), iter = 10:10:50) # Convenience constructor
1-dimensional KeyedArray(NamedDimsArray(...)) with range:
β iter β 5-element StepRange{Int64,...}
And data, 5-element Array{Int8,1}:
(10) 115
(20) 99
(30) 0
(40) 57
(50) 88
julia> D isa AbstractArray
true
julia> D[2] # Square brackets index as usual
99
julia> D(20) # Round brackets lookup by key
99
julia> D.iter # The range of keys is property D.name
10:10:50
julia> E = wrapdims(rand(Int8, 2,3), :row, :col) # Pretty printing of NamedDimsArray
2Γ3 NamedDimsArray(::Array{Int8,2}, (:row, :col)):
β col
β row -105 76 52
3 -27 -112
julia> @view E[col=1] # Fixing one index gives a slice
2-element NamedDimsArray(view(::Array{Int8,2}, :, 1), (:row,)):
β row -105
3
julia> C = KeyedArray(rand(2,10) .+ (0:1), obs=["dog", "cat"], time=range(0, step=0.5, length=10))
2-dimensional KeyedArray(NamedDimsArray(...)) with ranges:
β obs β 2-element Vector{String}
β time β 10-element StepRangeLen{Float64,...}
And data, 2Γ10 Array{Float64,2}:
(0.0) (0.5) (1.0) (1.5) β¦ (3.5) (4.0) (4.5)
("dog") 0.160006 0.602298 0.383491 0.745181 0.0823367 0.452418 0.281987
("cat") 1.42296 1.36346 1.59291 1.26281 1.24468 1.76372 1.14364
julia> dimnames(C) # Works like size & axes, i.e. dimnames(C,2) == :time
(:obs, :time)
julia> axiskeys(C) # Likewise, axiskeys(C, :time) == 0:0.5:4.5
(["dog", "cat"], 0.0:0.5:4.5)
julia> axes(C) # Base.axes is untouched
(Base.OneTo(2), Base.OneTo(10))
julia> C[1,3]
0.3834911947029529
julia> C("dog", Index[3]) # Selector Index[i] lets you mix lookup and indexing
0.3834911947029529
julia> C(time=Near(1.1), obs="dog") # Selector Near(val) finds one closest index
0.3834911947029529
julia> C(!=("cat"), Index[end]) # Functions allowed as selectors, and Index[end] works
1-dimensional KeyedArray(NamedDimsArray(...)) with range:
β obs β 1-element view(::Vector{String},...)
And data, 1-element view(::Array{Float64,2}, [1], 10) with eltype Float64:
("dog") 0.28198708251379423
julia> C(0.5) # Here 0.5 is unambiguous as types of ranges are distinct
1-dimensional KeyedArray(NamedDimsArray(...)) with range:
β obs β 2-element Vector{String}
And data, 2-element view(::Array{Float64,2}, :, 2) with eltype Float64:
("dog") 0.602297580266383
("cat") 1.3634584219520556
julia> C * C' # Functions like adjoint and * work through wrappers
2-dimensional KeyedArray(NamedDimsArray(...)) with ranges:
β obs β 2-element Vector{String}
β obs β 2-element Vector{String}
And data, 2Γ2 Array{Float64,2}:
("dog") ("cat")
("dog") 1.92623 5.85958
("cat") 5.85958 21.8234
julia> ans("mouse")
ERROR: ArgumentError: key of type String is ambiguous, matches dimensions (1, 2)
julia> C("mouse")
ERROR: ArgumentError: could not find key "mouse" in vector ["dog", "cat"]
julia> for (i,t) in enumerate(C.time)
t > 3 && println("at time $t, value cat = ", C[2,i])
end
at time 3.5, value cat = 1.244682870516645
at time 4.0, value cat = 1.763719368415045
at time 4.5, value cat = 1.1436376769992096
julia> using Statistics
julia> mean(C, dims=:time) # Reduction functions should accept dimension names
2-dimensional KeyedArray(NamedDimsArray(...)) with ranges:
β obs β 2-element Vector{String}
β time β 1-element OneTo{Int}
And data, 2Γ1 Array{Float64,2}:
(1)
("dog") 0.3918636606806696
("cat") 1.4542825908906043
julia> map(sqrt, D) .* sqrt.(D) # map, broadcasting, and generators should work
1-dimensional KeyedArray(NamedDimsArray(...)) with range:
β iter β 5-element StepRange{Int64,...}
And data, 5-element Array{Float64,1}:
(10) 114.99999999999999
(20) 99.0
(30) 0.0
(40) 57.0
(50) 88.0
julia> vcat(D', zero(D'), similar(D'))
2-dimensional KeyedArray(NamedDimsArray(...)) with ranges:
β _ β 3-element OneTo{Int}
β iter β 5-element StepRange{Int64,...}
And data, 3Γ5 Array{Int8,2}:
(10) (20) (30) (40) (50)
(1) 115 99 0 57 88
(2) 0 0 0 0 0
(3) 48 -44 -18 8 1
julia> F = wrapdims(rand(1:100, 5), π€ = 'a':'z') # ranges are adjusted if possible
β Warning: range 'a':1:'z' replaced by 'a':1:'e', to match size(A, 1) == 5
β @ AxisKeys ~/.julia/dev/AxisKeys/src/wrap.jl:46
1-dimensional KeyedArray(NamedDimsArray(...)) with range:
β π€ β 5-element StepRange{Char,...}
And data, 5-element Array{Int64,1}:
('a') 16
('b') 87
('c') 49
('d') 91
('e') 44
julia> push!(F, 10^6) # push! also knows to extend 'a':'e' by one
1-dimensional KeyedArray(NamedDimsArray(...)) with range:
β π€ β 6-element StepRange{Char,...}
And data, 6-element Array{Int64,1}:
('a') 16
('b') 87
('c') 49
('d') 91
('e') 44
('f') 1000000
julia> using UniqueVectors # https://github.com/garrison/UniqueVectors.jl
julia> u = unique(rand(Int8, 100));
julia> H = wrapdims(rand(3,length(u),2), UniqueVector; # apply this type to all ranges
row=[:a, :b, :c], col=u, page=["one", "two"])
3-dimensional KeyedArray(NamedDimsArray(...)) with ranges:
β row β 3-element UniqueVector{Symbol}
β col β 81-element UniqueVector{Int8}
β‘ page β 2-element UniqueVector{String}
And data, 3Γ81Γ2 Array{Float64,3}:
[:, :, 1] ~ (:, :, "one"):
(-25) (-96) (0) β¦ (69) (-14)
(:a) 0.293286 0.97221 0.857084 0.894496 0.994897
(:b) 0.966373 0.112904 0.98633 0.0459311 0.393979
(:c) 0.410052 0.69666 0.800045 0.524544 0.195882
[:, :, 2] ~ (:, :, "two"):
(-25) (-96) (0) β¦ (69) (-14)
(:a) 0.486264 0.111887 0.632189 0.0597532 0.493346
(:b) 0.123933 0.988803 0.243089 0.701553 0.11737
(:c) 0.850917 0.0495313 0.0470764 0.322251 0.642556
julia> H(:a, -14, "one") # uses the UniqueVector's fast lookup
0.9948971186701887
julia> using LazyStack # A package for concatenating arrays
julia> stack(:pre, n .* D for n in 1:10)
2-dimensional NamedDimsArray(KeyedArray(...)) with keys:
β iter β 5-element StepRange{Int64,...}
β pre β 10-element OneTo{Int}
And data, 5Γ10 Array{Int64,2}:
(1) (2) (3) (4) (5) (6) (7) (8) (9) (10)
(10) 115 230 345 460 575 690 805 920 1035 1150
(20) 99 198 297 396 495 594 693 792 891 990
(30) 0 0 0 0 0 0 0 0 0 0
(40) 57 114 171 228 285 342 399 456 513 570
(50) 88 176 264 352 440 528 616 704 792 880