This repository was archived by the owner on Aug 17, 2021. It is now read-only.
forked from JuliaArrays/AxisArrays.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.jl
More file actions
282 lines (261 loc) · 10.4 KB
/
core.jl
File metadata and controls
282 lines (261 loc) · 10.4 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
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, ctranspose)
@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,)
VERSION >= v"0.5.0-dev" && @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)
VERSION >= v"0.5.0-dev" && @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,)
VERSION >= v"0.5.0-dev" && @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)
VERSION >= v"0.5.0-dev" && @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,)
VERSION >= v"0.5.0-dev" && @inferred(axisnames(A))
@test axisvalues(A) == (.1:.1:.3,)
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)
VERSION >= v"0.5.0-dev" && @inferred(axisnames(A))
@test axisvalues(A) == (.5:.5:1, 1:2, 1:2, 1:2)
A = AxisArray([0]', :x, :y)
@test axisnames(squeeze(A, 1)) == (:y,)
@test axisnames(squeeze(A, 2)) == (:x,)
@test axisnames(squeeze(A, (1,2))) == axisnames(squeeze(A, (2,1))) == ()
@test axisnames(@inferred(squeeze(A, Axis{:x}))) == (:y,)
@test axisnames(@inferred(squeeze(A, Axis{:x,UnitRange{Int}}))) == (:y,)
@test axisnames(@inferred(squeeze(A, Axis{:y}))) == (:x,)
@test axisnames(@inferred(squeeze(squeeze(A, Axis{:x}), 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"])))
# 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 axes
@test @inferred(axes(A)) == (Axis{:x}(.1:.1:.2), Axis{:y}(1//10:1//10:3//10), Axis{:z}(["a", "b", "c", "d"]))
@test @inferred(axes(A, Axis{:x})) == @inferred(axes(A, Axis{:x}())) == Axis{:x}(.1:.1:.2)
@test @inferred(axes(A, Axis{:y})) == @inferred(axes(A, Axis{:y}())) == Axis{:y}(1//10:1//10:3//10)
@test @inferred(axes(A, Axis{:z})) == @inferred(axes(A, Axis{:z}())) == Axis{:z}(["a", "b", "c", "d"])
@test axes(A, 2) == Axis{:y}(1//10:1//10:3//10)
Aplain = rand(2,3)
@test @inferred(axes(Aplain)) === axes(AxisArray(Aplain))
@test axes(Aplain, 1) === axes(AxisArray(Aplain))[1]
@test axes(Aplain, 2) === 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 indices(Axis{:row}(2:7)) === (Base.OneTo(6),)
@test indices(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 axes(A) == (Axis{:x}(-1:1),)
@test A[-1] == 5
A[0] = 12
@test A.data[0] == 12
@test indices(A) == (-1:1,)
@test linearindices(A) == -1:1
A = AxisArray(OffsetArrays.OffsetArray(rand(4,5), -1:2, 5:9), :x, :y)
@test indices(A) == (-1:2, 5:9)
@test linearindices(A) == 1:20
@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
for op in functions # together, cover both reduced_indices and reduced_indices0
axv = axisvalues(C)
C1 = @inferred(op(C, 1))
@test typeof(C1) == typeof(C)
@test axisnames(C1) == (:y,:x)
@test axisvalues(C1) === (oftype(axv[1], Base.OneTo(1)), axv[2])
C2 = op(C, 2)
@test typeof(C2) == typeof(C)
@test axisnames(C2) == (:y,:x)
@test axisvalues(C2) === (axv[1], oftype(axv[2], Base.OneTo(1)))
C12 = @inferred(op(C, (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
C1t = @inferred(op(C, Axis{:y}))
@test C1t == C1
C2t = @inferred(op(C, Axis{:x}))
@test C2t == C2
C12t = @inferred(op(C, (Axis{:y},Axis{:x})))
@test C12t == C12
C1t = @inferred(op(C, Axis{:y}()))
@test C1t == C1
C2t = @inferred(op(C, Axis{:x}()))
@test C2t == C2
C12t = @inferred(op(C, (Axis{:y}(),Axis{:x}())))
@test C12t == C12
end
end