-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathcore.jl
More file actions
353 lines (327 loc) · 13.6 KB
/
core.jl
File metadata and controls
353 lines (327 loc) · 13.6 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# FIXME: type stability broken. The following should NOT error
A = @inferred(AxisArray(reshape(1:24, 2,3,4), .1:.1:.2, .1:.1:.3, .1:.1:.4))
@test_throws ArgumentError AxisArray(reshape(1:24, 2,3,4), .1:.1:.1, .1:.1:.3, .1:.1:.4)
@test_throws ArgumentError AxisArray(reshape(1:24, 2,3,4), .1:.1:.1, .1:.1:.3)
@test_throws ArgumentError AxisArray(reshape(1:24, 2,3,4), .1:.1:.2, .1:.1:.3, .1:.1:.4, 1:1)
@test parent(A) === reshape(1:24, 2,3,4)
# Test iteration
for (a,b) in zip(A, A.data)
@test a == b
end
# Cartesian indexing
for idx in eachindex(A)
@test A[idx] == A.data[idx]
end
# Conversion and similar
@test Array(A) == A.data
@test reshape(A, length(A)) == A.data[:]
B = similar(A, Float64)
for i in eachindex(A)
B[i] = A[i]
end
@test A == B
for i=1:length(A)
@test float(A[i]) === B[i]
end
C = similar(A, 0)
@test isa(C, Array{Int,1})
@test C == []
D = similar(A)
@test size(A) == size(D)
@test eltype(A) == eltype(D)
# permutedims and transpose
@test axisnames(permutedims(A, (2,1,3))) == (:col, :row, :page)
@test axisnames(permutedims(A, (2,3,1))) == (:col, :page, :row)
@test axisnames(permutedims(A, (3,2,1))) == (:page, :col, :row)
@test axisnames(permutedims(A, (3,1,2))) == (:page, :row, :col)
for perm in ((:col, :row, :page), (:col, :page, :row),
(:page, :col, :row), (:page, :row, :col),
(:row, :page, :col), (:row, :col, :page))
@test axisnames(permutedims(A, perm)) == perm
end
@test axisnames(permutedims(A, (:col,))) == (:col, :row, :page)
@test axisnames(permutedims(A, (:page,))) == (:page, :row, :col)
A2 = @inferred(AxisArray(reshape(1:15, 3, 5)))
A1 = AxisArray(1:5, :t)
for f in (transpose, adjoint)
@test f(A2).data == f(A2.data)
@test axisnames(f(A2)) == (:col, :row)
@test f(A1).data == f(A1.data)
@test axisnames(f(A1)) == (:transpose, :t)
end
# Test modifying a particular axis
E = similar(A, Float64, Axis{:col}(1:2))
@test size(E) == (2,2,4)
@test eltype(E) == Float64
F = similar(A, Axis{:row}())
@test size(F) == size(A)[2:end]
@test eltype(F) == eltype(A)
@test axisvalues(F) == axisvalues(A)[2:end]
@test axisnames(F) == axisnames(A)[2:end]
G = similar(A, Float64)
@test size(G) == size(A)
@test eltype(G) == Float64
@test axisvalues(A) == axisvalues(G)
@test axisnames(A) == axisnames(G)
H = similar(A, 1,1,1)
@test size(H) == (1,1,1)
@test eltype(H) == eltype(A)
@test typeof(H) <: Array
H = similar(A, Float64, 1,1,1)
@test size(H) == (1,1,1)
@test eltype(H) == Float64
@test typeof(H) <: Array
# Size
@test size(A, 1) == size(A, Axis{1}) == size(A, Axis{:row}) == size(A, Axis{:row}())
## Test constructors
# No axis or time args
A = AxisArray(1:3)
@test A.data == 1:3
@test axisnames(A) == (:row,)
@inferred(axisnames(A))
@test axisvalues(A) == (1:3,)
A = AxisArray(reshape(1:16, 2,2,2,2))
@test A.data == reshape(1:16, 2,2,2,2)
@test axisnames(A) == (:row,:col,:page,:dim_4)
@inferred(axisnames(A))
@test axisvalues(A) == (1:2, 1:2, 1:2, 1:2)
# Just axis names
A = AxisArray(1:3, :a)
@test A.data == 1:3
@test axisnames(A) == (:a,)
@inferred(axisnames(A))
@test axisvalues(A) == (1:3,)
A = AxisArray([1 3; 2 4], :a)
@test A.data == [1 3; 2 4]
@test axisnames(A) == (:a, :col)
@inferred(axisnames(A))
@test axisvalues(A) == (1:2, 1:2)
# Just axis values
A = @inferred(AxisArray(1:3, .1:.1:.3))
@test A.data == 1:3
@test axisnames(A) == (:row,)
@inferred(axisnames(A))
@test axisvalues(A) == (.1:.1:.3,)
# FIXME: reintroduce inferred
A = @inferred(AxisArray(reshape(1:16, 2,2,2,2), .5:.5:1))
@test A.data == reshape(1:16, 2,2,2,2)
@test axisnames(A) == (:row,:col,:page,:dim_4)
@inferred(axisnames(A))
@test axisvalues(A) == (.5:.5:1, 1:2, 1:2, 1:2)
A = AxisArray([0]', :x, :y)
@test axisnames(dropdims(A, dims=1)) == (:y,)
@test axisnames(dropdims(A, dims=2)) == (:x,)
@test axisnames(dropdims(A, dims=(1,2))) == axisnames(dropdims(A, dims=(2,1))) == ()
@test axisnames((dropdims(A, dims=Axis{:x}))) == (:y,)
@test axisnames((dropdims(A, dims=Axis{:x,UnitRange{Int}}))) == (:y,)
@test axisnames((dropdims(A, dims=Axis{:y}))) == (:x,)
@test axisnames((dropdims(dropdims(A, dims=Axis{:x}), dims=Axis{:y}))) == ()
@test_broken @inferred(dropdims(A, dims=Axis{:x}))
@test_broken @inferred(dropdims(A, dims=Axis{:x,UnitRange{Int}}))
@test_broken @inferred(dropdims(A, dims=Axis{:y}))
@test_broken @inferred(dropdims(dropdims(A, dims=Axis{:x}), dims=Axis{:y}))
# Names, steps, and offsets
B = AxisArray([1 4; 2 5; 3 6], (:x, :y), (0.2, 100))
@test axisnames(B) == (:x, :y)
@test axisvalues(B) == (0:0.2:0.4, 0:100:100)
B = AxisArray([1 4; 2 5; 3 6], (:x, :y), (0.2, 100), (-3,14))
@test axisnames(B) == (:x, :y)
@test axisvalues(B) == (-3:0.2:-2.6, 14:100:114)
@test AxisArrays.HasAxes(A) == AxisArrays.HasAxes{true}()
@test AxisArrays.HasAxes([1]) == AxisArrays.HasAxes{false}()
@test_throws ArgumentError AxisArray(reshape(1:24, 2,3,4),
Axis{1}(.1:.1:.2),
Axis{2}(1//10:1//10:3//10),
Axis{3}(["a", "b", "c", "d"])) # Axis need to be symbols
@test_throws ArgumentError AxisArray(reshape(1:24, 2,3,4),
Axis{:x}(.1:.1:.2),
Axis{:y}(1//10:1//10:3//10),
Axis{:z}(["a", "b", "c", "d"]),
Axis{:_}(1:1)) # Too many Axes
A = @inferred(AxisArray(reshape(1:24, 2,3,4),
Axis{:x}(.1:.1:.2),
Axis{:y}(1//10:1//10:3//10),
Axis{:z}(["a", "b", "c", "d"])))
# recursive constructor
@test A === @inferred AxisArray(A)
@test axisnames(AxisArray(A, Axis{:yoyo}(1:length(A[Axis{:x}])))) == (:yoyo, :y, :z)
@test AxisArray(A, Axis{:yoyo}(1:length(A[Axis{:x}]))).data === A.data
@test AxisArray(A, (Axis{:yoyo}(1:length(A[Axis{:x}])),)).data === A.data
@test axisnames(AxisArray(A, :something, :in, :the)) == (:something, :in, :the)
@test AxisArray(A, :way, :you, :move).data === A.data
@test axisnames(AxisArray(A, (:c, :a, :b), (2, 3, 4))) == (:c, :a, :b)
@test AxisArray(A, (:c, :a, :b), (2, 3, 4)).data === A.data
@inferred AxisArray(A, Axis{:yoyo}(1:length(A[Axis{:x}])))
@inferred AxisArray(A, (Axis{:yoyo}(1:length(A[Axis{:x}])),))
# Test axisdim
@test axisdim(A, Axis{:x}) == axisdim(A, Axis{:x}()) == 1
@test axisdim(A, Axis{:y}) == axisdim(A, Axis{:y}()) == 2
@test axisdim(A, Axis{:z}) == axisdim(A, Axis{:z}()) == 3
# Test that getproperty is fully inferred when a const name is supplied
let getx(A) = A.x,
getz(A) = A.z,
getdata(A) = A.data
@test @inferred(getx(A)) == A.axes[1].val
@test @inferred(getz(A)) == A.axes[3].val
@test @inferred(AxisArrays.axes(A)) === A.axes
@test @inferred(getdata(A)) === A.data
end
@test propertynames(A) == (:x, :y, :z)
@test propertynames(A, true) == (:x, :y, :z, :data, :axes)
# Test axes
@test @inferred(AxisArrays.axes(A)) == (Axis{:x}(.1:.1:.2), Axis{:y}(1//10:1//10:3//10), Axis{:z}(["a", "b", "c", "d"]))
@test @inferred(AxisArrays.axes(A, Axis{:x})) == @inferred(AxisArrays.axes(A, Axis{:x}())) == Axis{:x}(.1:.1:.2)
@test @inferred(AxisArrays.axes(A, Axis{:y})) == @inferred(AxisArrays.axes(A, Axis{:y}())) == Axis{:y}(1//10:1//10:3//10)
@test @inferred(AxisArrays.axes(A, Axis{:z})) == @inferred(AxisArrays.axes(A, Axis{:z}())) == Axis{:z}(["a", "b", "c", "d"])
@test AxisArrays.axes(A, 2) == Axis{:y}(1//10:1//10:3//10)
Aplain = rand(2,3)
@test @inferred(AxisArrays.axes(Aplain)) === AxisArrays.axes(AxisArray(Aplain))
@test AxisArrays.axes(Aplain, 1) === AxisArrays.axes(AxisArray(Aplain))[1]
@test AxisArrays.axes(Aplain, 2) === AxisArrays.axes(AxisArray(Aplain))[2]
@test Axis{:col}(1) == Axis{:col}(1)
@test Axis{:col}(1) != Axis{:com}(1)
@test Axis{:x}(1:3) == Axis{:x}(Base.OneTo(3))
@test hash(Axis{:col}(1)) == hash(Axis{:col}(1.0))
@test hash(Axis{:row}()) != hash(Axis{:col}())
@test hash(Axis{:x}(1:3)) == hash(Axis{:x}(Base.OneTo(3)))
@test AxisArrays.axistype(Axis{1}(1:2)) == typeof(1:2)
@test AxisArrays.axistype(Axis{1,UInt32}) == UInt32
@test axisnames(Axis{1}, Axis{2}, Axis{3}) == (1,2,3)
@test Axis{:row}(2:7)[4] == 5
@test eltype(Axis{:row}(1.0:1.0:3.0)) == Float64
@test size(Axis{:row}(2:7)) === (6,)
T = A[AxisArrays.Axis{:x}]
@test T[end] == 0.2
@test Base.axes(Axis{:row}(2:7)) === (Base.OneTo(6),)
@test Base.axes(Axis{:row}(-1:1), 1) === Base.OneTo(3)
@test length(Axis{:col}(-1:2)) === 4
@test AxisArrays.axisname(Axis{:foo}(1:2)) == :foo
@test AxisArrays.axisname(Axis{:foo}) == :foo
# Test Timetype axis construction
dt, vals = DateTime(2010, 1, 2, 3, 40), randn(5,2)
A = @inferred(AxisArray(vals, Axis{:Timestamp}(dt-Dates.Hour(2):Dates.Hour(1):dt+Dates.Hour(2)), Axis{:Cols}([:A, :B])))
@test A[:, :A].data == vals[:, 1]
@test A[dt, :].data == vals[3, :]
@test AxisArrays.axistrait(A.axes[1]) == AxisArrays.Dimensional
@test AxisArrays.axistrait(typeof(A.axes[1])) == AxisArrays.Dimensional
@test AxisArrays.axistrait(A.axes[1].val) == AxisArrays.Dimensional
@test AxisArrays.axistrait(typeof(A.axes[1].val)) == AxisArrays.Dimensional
@test AxisArrays.axistrait(A.axes[2]) == AxisArrays.Categorical
@test AxisArrays.axistrait(typeof(A.axes[2])) == AxisArrays.Categorical
@test AxisArrays.axistrait(A.axes[2].val) == AxisArrays.Categorical
@test AxisArrays.axistrait(typeof(A.axes[2].val)) == AxisArrays.Categorical
@test_throws ArgumentError AxisArrays.checkaxis(Axis{:x}(10:-1:1))
@test_throws ArgumentError AxisArrays.checkaxis(10:-1:1)
# Simply run the display method to ensure no stupid errors
show(IOBuffer(),MIME("text/plain"),A)
# With unconventional indices
import OffsetArrays # import rather than using because OffsetArrays has a deprecation for ..
A = AxisArray(OffsetArrays.OffsetArray([5,3,4], -1:1), :x)
@test AxisArrays.axes(A) == (Axis{:x}(-1:1),)
@test A[-1] == 5
A[0] = 12
@test A.data[0] == 12
@test Base.axes(A) == Base.axes(A.data)
@test LinearIndices(A) == LinearIndices(A.data)
A = AxisArray(OffsetArrays.OffsetArray(rand(4,5), -1:2, 5:9), :x, :y)
@test Base.axes(A) == Base.axes(A.data)
@test LinearIndices(A) == LinearIndices(A.data)
@test AxisArrays.matchingdims((A, A))
f1(x) = x < 0
A2 = map(f1, A)
@test isa(A2, AxisArray)
@test A2.axes == A.axes
@test A2.data == map(f1, A.data)
map!(~, A2, A2)
@test isa(A2, AxisArray)
@test A2.axes == A.axes
@test A2.data == map(~, map(f1, A).data)
A2 = map(+, A, A)
@test isa(A2, AxisArray)
@test A2.axes == A.axes
@test A2.data == A.data .+ A.data
map!(*, A2, A, A)
@test isa(A2, AxisArray)
@test A2.axes == A.axes
@test A2.data == A.data .* A.data
# Reductions (issue #55)
A = AxisArray(collect(reshape(1:15,3,5)), :y, :x)
B = @inferred(AxisArray(collect(reshape(1:15,3,5)), Axis{:y}(0.1:0.1:0.3), Axis{:x}(10:10:50)))
arrays = (A, B)
functions = (sum, minimum)
for C in arrays
local C
for op in functions # together, cover both reduced_indices and reduced_indices0
axv = axisvalues(C)
@test_broken @inferred(op(C; dims=1))
C1 = op(C; dims=1)
@test typeof(C1) == typeof(C)
@test axisnames(C1) == (:y,:x)
@test axisvalues(C1) === (oftype(axv[1], Base.OneTo(1)), axv[2])
@test_broken @inferred(op(C, dims=2))
C2 = op(C, dims=2)
@test typeof(C2) == typeof(C)
@test axisnames(C2) == (:y,:x)
@test axisvalues(C2) === (axv[1], oftype(axv[2], Base.OneTo(1)))
@test_broken @inferred(op(C, dims=(1,2)))
C12 = op(C, dims=(1,2))
@test typeof(C12) == typeof(C)
@test axisnames(C12) == (:y,:x)
@test axisvalues(C12) === (oftype(axv[1], Base.OneTo(1)), oftype(axv[2], Base.OneTo(1)))
if op == sum
@test C1 == [6 15 24 33 42]
@test C2 == reshape([35,40,45], 3, 1)
@test C12 == reshape([120], 1, 1)
else
@test C1 == [1 4 7 10 13]
@test C2 == reshape([1,2,3], 3, 1)
@test C12 == reshape([1], 1, 1)
end
# TODO: add @inferred
@test (op(C, dims=Axis{:y})) == C1
@test (op(C, dims=Axis{:x})) == C2
@test (op(C, dims=(Axis{:y},Axis{:x}))) == C12
@test (op(C, dims=Axis{:y}())) == C1
@test (op(C, dims=Axis{:x}())) == C2
@test (op(C, dims=(Axis{:y}(),Axis{:x}()))) == C12
end
end
function typeof_noaxis(::AxisArray{T,N,D}) where {T,N,D}
AxisArray{T,N,D}
end
# uninferrable
C = AxisArray(collect(reshape(1:15,3,5)), Axis{:y}([:a,:b,:c]), Axis{:x}(["a","b","c","d","e"]))
for op in functions # together, cover both reduced_indices and reduced_indices0
axv = axisvalues(C)
C1 = op(C, dims=1)
@test typeof_noaxis(C1) == typeof_noaxis(C)
@test axisnames(C1) == (:y,:x)
@test axisvalues(C1) === (Base.OneTo(1), axv[2])
C2 = op(C, dims=2)
@test typeof_noaxis(C2) == typeof_noaxis(C)
@test axisnames(C2) == (:y,:x)
@test axisvalues(C2) === (axv[1], Base.OneTo(1))
C12 = op(C, dims=(1,2))
@test typeof_noaxis(C12) == typeof_noaxis(C)
@test axisnames(C12) == (:y,:x)
@test axisvalues(C12) === (Base.OneTo(1), Base.OneTo(1))
if op == sum
@test C1 == [6 15 24 33 42]
@test C2 == reshape([35,40,45], 3, 1)
@test C12 == reshape([120], 1, 1)
else
@test C1 == [1 4 7 10 13]
@test C2 == reshape([1,2,3], 3, 1)
@test C12 == reshape([1], 1, 1)
end
# TODO: These should be @inferred, but are currently broken
@test (op(C, dims=Axis{:y})) == C1
@test (op(C, dims=Axis{:x})) == C2
# Unfortunately the type of (Axis{:y},Axis{:x}) is Tuple{UnionAll,UnionAll} so methods will not specialize
@test (op(C, dims=(Axis{:y},Axis{:x}))) == C12
@test (op(C, dims=Axis{:y}())) == C1
@test (op(C, dims=Axis{:x}())) == C2
@test (op(C, dims=(Axis{:y}(),Axis{:x}()))) == C12
end
C = AxisArray(collect(reshape(1:15,3,5)), Axis{:y}([:a,:b,:c]), Axis{:x}(["a","b","c","d","e"]))
@test occursin(r"axes:\n\s+:y,", summary(C))