96
96
hist = similar (first (centers), (axes (c, 1 ) for c in centers). .. )
97
97
98
98
# values for dims 2 and up
99
- coord_rest = ((0.0 for _ in 2 : N). .. ,)
100
- index_rest = ((1 for _ in 2 : N). .. ,)
99
+ coord_rest = (([0.0 ] for _ in 2 : N). .. ,)
101
100
102
101
# nothing weights are interpreted as unity weight
103
- x1 = [( 0.33 , coord_rest... )]
102
+ x1 = ([ 0.33 ] , coord_rest... )
104
103
fill! (hist, 0 )
105
104
_histogram! (style, hist, edges′, x1, nothing )
106
105
@test sum (hist) * step (edges[1 ])^ N == 1.0
109
108
@test hist == _histogram (style, x1, edges′; weights = [1 ])
110
109
111
110
# out-of-bounds elements are not binned
112
- x0 = [( - 1.0 , (0.0 for _ in 1 : N- 1 ). .. ,)]
111
+ x0 = ([ - 1.0 ] , ([ 0.0 ] for _ in 1 : N- 1 ). .. )
113
112
fill! (hist, 0 )
114
113
_histogram! (style, hist, edges′, x0, nothing )
115
114
@test sum (hist) == 0.0
138
137
139
138
# make data unitful, with mixed unit axes
140
139
units = (u " m" , u " s^-2" , u " kg" )[1 : N]
141
- vals = [x .* units for x in x1]
140
+ vals = map ((x, u) -> x .* u, x1, units)
142
141
uedges = map ((e, u) -> e .* u, edges, units)
143
142
uhist = zeros (_hist_eltype (uedges), axes (hist)... )
144
143
# verify that the function accepts unitful quantities
@@ -153,14 +152,30 @@ end
153
152
@test eltype (uhist3) == eltype (uhist)
154
153
@test uhist3 == uhist
155
154
end
155
+
156
+ @testset " Non-bitstype numbers" begin
157
+ edges = (big " 0.5" :1 : big " 5.5" , 0.5 : 1 : 5.5 , 0.5f0 : 1 : 5.5f0 )
158
+
159
+ coords = ([big " 1.0" ], [2.0 ], [3f0 ])
160
+ H1 = _histogram (HB, coords[1 : 1 ], edges[1 : 1 ]. .. )
161
+ @test eltype (H1) == BigFloat
162
+ @test H1[1 ] == 1 && sum (H1) == 1.0
163
+
164
+ H2 = _histogram (HB, coords[1 : 2 ], edges[1 : 2 ]. .. )
165
+ @test eltype (H2) == BigFloat
166
+ @test H2[1 ,2 ] == 1 && sum (H2) == 1.0
167
+
168
+ H3 = _histogram (HB, coords[1 : 3 ], edges[1 : 3 ]. .. )
169
+ @test eltype (H3) == BigFloat
170
+ @test H3[1 ,2 ,3 ] == 1 && sum (H3) == 1.0
171
+ end
156
172
end
157
173
158
174
@testset " Weighting" begin
159
175
using . Histogramming: HistEdge, _histogram!
160
176
161
177
N = 100
162
178
rv = randn (N)
163
- data = reinterpret (reshape, Tuple{Float64}, rv)
164
179
165
180
Nlen = Float64 (N)
166
181
Npos = Float64 (count (>= (0 ), rv))
179
194
fill! (H0, 0 ); fill! (H1, 0 ); fill! (H2, 0 )
180
195
181
196
# binning uses the sum of weights (not effective sample size as KDE does)
182
- wsum0 = _histogram! (style, H0, edges, data , nothing )
183
- wsum1 = _histogram! (style, H1, edges, data , weight1)
184
- wsum2 = _histogram! (style, H2, edges, data , weight2)
197
+ wsum0 = _histogram! (style, H0, edges, (rv,) , nothing )
198
+ wsum1 = _histogram! (style, H1, edges, (rv,) , weight1)
199
+ wsum2 = _histogram! (style, H2, edges, (rv,) , weight2)
185
200
@test wsum0 == N
186
201
@test wsum1 == N
187
202
@test wsum2 == 2 N
194
209
# binning weights respect limits and ignore out-of-bounds entries
195
210
fill! (H0, 0 ); fill! (H1, 0 ); fill! (H2, 0 )
196
211
197
- wsum0 = _histogram! (style, @view (H1[1 : end ÷ 2 ]), edges_pos, data , weight1)
198
- wsum1 = _histogram! (style, @view (H1[1 : end ÷ 2 ]), edges_pos, data , weight1)
199
- wsum2 = _histogram! (style, @view (H2[1 : end ÷ 2 ]), edges_pos, data , weight2)
212
+ wsum0 = _histogram! (style, @view (H1[1 : end ÷ 2 ]), edges_pos, (rv,) , weight1)
213
+ wsum1 = _histogram! (style, @view (H1[1 : end ÷ 2 ]), edges_pos, (rv,) , weight1)
214
+ wsum2 = _histogram! (style, @view (H2[1 : end ÷ 2 ]), edges_pos, (rv,) , weight2)
200
215
@test wsum0 == Npos
201
216
@test wsum1 == Npos
202
217
@test wsum2 == 2 Npos
@@ -212,13 +227,11 @@ end
212
227
l32 = LinRange (r32[1 ], r32[end ], length (r32))
213
228
214
229
@testset " $r " for r in (r64, l64, r32, l32)
215
- v = reinterpret (reshape, Tuple{eltype (r)}, Vector (r))
216
-
217
230
edges = (HistEdge (r),)
218
231
# For regular histogram binning, using the bin edges as values must result in a uniform
219
232
# distribution except the last bin which is doubled (due to being closed on the right).
220
233
H = zeros (eltype (r), length (r) - 1 )
221
- ν = _histogram! (HB, H, edges, v , nothing )
234
+ ν = _histogram! (HB, H, edges, (r,) , nothing )
222
235
@test ν == length (r)
223
236
@test_broken all (@view (H[1 : end - 1 ]) .== H[1 ])
224
237
@test H[end ] == 2 H[1 ]
228
241
# neighbors. (The remaining interior bins give up half of their weight but
229
242
# simultaneously gain from a neighbor, so they are unchanged.)
230
243
fill! (H, 0.0 )
231
- ν = _histogram! (LB, H, edges, v , nothing )
244
+ ν = _histogram! (LB, H, edges, (r,) , nothing )
232
245
@test ν == length (r)
233
246
@test all (@view (H[2 : end - 1 ]) .≈ H[2 ])
234
247
@test H[end ] ≈ H[1 ]
249
262
250
263
H = zeros (nbins)
251
264
edges = (HistEdge (lo, hi, nbins),)
252
- _histogram! (LB, H, edges, [(x,)] , nothing )
265
+ _histogram! (LB, H, edges, ([x],) , nothing )
253
266
@test all (iszero, @view H[1 : end - 1 ])
254
267
@test H[end ] > 0.0
255
268
end
0 commit comments