forked from jhykes/rebin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_rebin.py
557 lines (406 loc) · 15 KB
/
test_rebin.py
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
"""
Testing bebin histogram values.
"""
import numpy as np
from numpy.random import uniform
from numpy.testing import assert_allclose
from scipy.interpolate import splrep, splint
import uncertainties.unumpy as unp
import rebin
from bounded_splines import BoundedUnivariateSpline, BoundedRectBivariateSpline
# ---------------------------------------------------------------------------- #
# Tests for piecewise continuous rebinning
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
def test_x2_same_as_x1():
"""
x2 same as x1
"""
# old size
m = 6
# new size
n = 6
# bin edges
x_old = np.linspace(0., 1., m+1)
x_new = np.linspace(0., 1., n+1)
# some arbitrary distribution
y_old = 1. + np.sin(x_old[:-1]*np.pi) / np.ediff1d(x_old)
# rebin
y_new = rebin.rebin(x_old, y_old, x_new, interp_kind='piecewise_constant')
assert_allclose(y_new, y_old)
# ---------------------------------------------------------------------------- #
def test_x2_surrounds_x1():
"""
x2 range surrounds x1 range
"""
# old size
m = 2
# new size
n = 3
# bin edges
x_old = np.linspace(0., 1., m+1)
x_new = np.linspace(-0.1, 1.2, n+1)
# some arbitrary distribution
y_old = 1. + np.sin(x_old[:-1]*np.pi) / np.ediff1d(x_old)
# rebin
y_new = rebin.rebin(x_old, y_old, x_new, interp_kind='piecewise_constant')
# compute answer here to check rebin
y_old_ave = y_old / np.ediff1d(x_old)
y_new_here = [y_old_ave[0]*(x_new[1]-0.),
y_old_ave[0]*(x_old[1]-x_new[1]) + y_old_ave[1]*(x_new[2]-x_old[1]),
y_old_ave[1]*(x_old[-1]-x_new[-2])]
assert_allclose(y_new, y_new_here)
assert_allclose(y_new.sum(), y_old.sum())
# ---------------------------------------------------------------------------- #
def test_x2_lower_than_x1():
"""
x2 range is completely lower than x1 range
"""
# old size
m = 2
# new size
n = 3
# bin edges
x_old = np.linspace(0., 1., m+1)
x_new = np.linspace(-0.2, -0.0, n+1)
# some arbitrary distribution
y_old = 1. + np.sin(x_old[:-1]*np.pi) / np.ediff1d(x_old)
# rebin
y_new = rebin.rebin(x_old, y_old, x_new, interp_kind='piecewise_constant')
assert_allclose(y_new, [0.,0.,0.])
assert_allclose(y_new.sum(), 0.)
# ---------------------------------------------------------------------------- #
def test_x2_above_x1():
"""
x2 range is completely above x1 range
"""
# old size
m = 20
# new size
n = 30
# bin edges
x_old = np.linspace(0., 1., m+1)
x_new = np.linspace(1.2, 10., n+1)
# some arbitrary distribution
y_old = 1. + np.sin(x_old[:-1]*np.pi) / np.ediff1d(x_old)
# rebin
y_new = rebin.rebin(x_old, y_old, x_new, interp_kind='piecewise_constant')
assert_allclose(y_new, np.zeros((n,)))
assert_allclose(y_new.sum(), 0.)
# ---------------------------------------------------------------------------- #
def test_x2_in_x1():
"""
x2 only has one bin, and it is surrounded by x1 range
"""
# old size
m = 4
# new size
n = 1
# bin edges
x_old = np.linspace(0., 1., m+1)
x_new = np.linspace(0.3, 0.65, n+1)
# some arbitrary distribution
y_old = 1. + np.sin(x_old[:-1]*np.pi) / np.ediff1d(x_old)
# rebin
y_new = rebin.rebin(x_old, y_old, x_new, interp_kind='piecewise_constant')
# compute answer here to check rebin
y_old_ave = y_old / np.ediff1d(x_old)
y_new_here = ( y_old_ave[1]*(x_old[2]-x_new[0])
+ y_old_ave[2]*(x_new[1]-x_old[2]) )
assert_allclose(y_new, y_new_here)
# ---------------------------------------------------------------------------- #
def test_x2_in_x1_2():
"""
x2 has a couple of bins, each of which span more than one original bin
"""
# old size
m = 10
# bin edges
x_old = np.linspace(0., 1., m+1)
x_new = np.array([0.25, 0.55, 0.75])
# some arbitrary distribution
y_old = 1. + np.sin(x_old[:-1]*np.pi) / np.ediff1d(x_old)
y_old = unp.uarray(y_old, 0.1*y_old*uniform((m,)))
# rebin
y_new = rebin.rebin(x_old, y_old, x_new, interp_kind='piecewise_constant')
# compute answer here to check rebin
y_new_here = unp.uarray(np.zeros(2), np.zeros(2))
y_new_here[0] = 0.5 * y_old[2] + y_old[3] + y_old[4] + 0.5 * y_old[5]
y_new_here[1] = 0.5 * y_old[5] + y_old[6] + 0.5 * y_old[7]
assert_allclose(unp.nominal_values(y_new),
unp.nominal_values(y_new_here))
# mean or nominal value comparison
assert_allclose(unp.std_devs(y_new),
unp.std_devs(y_new_here))
# ---------------------------------------------------------------------------- #
def test_y1_uncertainties():
"""
x2 range surrounds x1 range, y1 has uncertainties
"""
# old size
m = 2
# new size
n = 3
# bin edges
x_old = np.linspace(0., 1., m+1)
x_new = np.linspace(-0.1, 1.2, n+1)
# some arbitrary distribution
y_old = 1. + np.sin(x_old[:-1]*np.pi) / np.ediff1d(x_old)
# with uncertainties
y_old = unp.uarray(y_old, 0.1*y_old*uniform((m,)))
# rebin
y_new = rebin.rebin(x_old, y_old, x_new, interp_kind='piecewise_constant')
# compute answer here to check rebin
y_old_ave = y_old / np.ediff1d(x_old)
y_new_here = np.array(
[y_old_ave[0]*(x_new[1]-0.),
y_old_ave[0]*(x_old[1]-x_new[1]) + y_old_ave[1]*(x_new[2]-x_old[1]),
y_old_ave[1]*(x_old[-1]-x_new[-2])]
)
# mean or nominal value comparison
assert_allclose(unp.nominal_values(y_new),
unp.nominal_values(y_new_here))
# mean or nominal value comparison
assert_allclose(unp.std_devs(y_new),
unp.std_devs(y_new_here))
assert_allclose(unp.nominal_values(y_new).sum(),
unp.nominal_values(y_new_here).sum())
# ---------------------------------------------------------------------------- #
# Tests for cubic-spline rebinning
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
def test_x2_surrounds_x1_with_constant_distribution():
"""
x2 domain completely surrounds x1 domain
"""
# old size
m = 20
# new size
n = 30
# bin edges
x_old = np.linspace(0., 1., m+1)
x_new = np.linspace(-0.5, 1.5, n+1)
# constant spline
mms_spline = BoundedUnivariateSpline([0,.1,.2,1], [1,1,1,1], s=0.)
y_old = np.array(
[ mms_spline.integral(x_old[i],x_old[i+1]) for i in range(m) ])
y_new_mms = np.array(
[ mms_spline.integral(x_new[i],x_new[i+1]) for i in range(n) ])
# rebin
y_new = rebin.rebin(x_old, y_old, x_new, interp_kind=3)
assert_allclose(y_new, y_new_mms)
# ---------------------------------------------------------------------------- #
def test_x2_left_overlap_x1_with_constant_distribution():
"""
x2 domain overlaps x1 domain from the left
"""
# old size
m = 20
# new size
n = 30
# bin edges
x_old = np.linspace(0., 1., m+1)
x_new = np.linspace(-0.75, 0.45, n+1)
# constant spline
mms_spline = BoundedUnivariateSpline([0,.1,.2,1], [1,1,1,1], s=0.)
y_old = np.array(
[ mms_spline.integral(x_old[i],x_old[i+1]) for i in range(m) ])
y_new_mms = np.array(
[ mms_spline.integral(x_new[i],x_new[i+1]) for i in range(n) ])
# rebin
y_new = rebin.rebin(x_old, y_old, x_new, interp_kind=3)
assert_allclose(y_new, y_new_mms)
# ---------------------------------------------------------------------------- #
def test_x2_right_overlap_x1_with_constant_distribution():
"""
x2 domain overlaps x1 domain from the right
"""
# old size
m = 20
# new size
n = 30
# bin edges
x_old = np.linspace(0., 1., m+1)
x_new = np.linspace(0.95, 1.05, n+1)
# constant spline
mms_spline = BoundedUnivariateSpline([0,.1,.2,1], [1,1,1,1], s=0.)
y_old = np.array(
[ mms_spline.integral(x_old[i],x_old[i+1]) for i in range(m) ])
y_new_mms = np.array(
[ mms_spline.integral(x_new[i],x_new[i+1]) for i in range(n) ])
# rebin
y_new = rebin.rebin(x_old, y_old, x_new, interp_kind=3)
assert_allclose(y_new, y_new_mms, atol=1e-15)
# ---------------------------------------------------------------------------- #
def test_x1_surrounds_x2_with_constant_distribution():
"""
x1 domain surrounds x2
"""
# old size
m = 20
# new size
n = 30
# bin edges
x_old = np.linspace(0., 1., m+1)
x_new = np.linspace(0.05, 0.26, n+1)
# constant spline
mms_spline = BoundedUnivariateSpline([0,.1,.2,1], [1,1,1,1], s=0.)
y_old = np.array(
[ mms_spline.integral(x_old[i],x_old[i+1]) for i in range(m) ])
y_new_mms = np.array(
[ mms_spline.integral(x_new[i],x_new[i+1]) for i in range(n) ])
# rebin
y_new = rebin.rebin(x_old, y_old, x_new, interp_kind=3)
assert_allclose(y_new, y_new_mms)
# ---------------------------------------------------------------------------- #
def test_x2_surrounds_x1_sine_spline():
"""
x2 range is completely above x1 range
using a random vector to build spline
"""
# old size
m = 5
# new size
n = 6
# bin edges
x_old = np.linspace(0., 1., m+1)
x_new = np.array([-.3, -.09, 0.11, 0.14, 0.2, 0.28, 0.73])
subbins = np.array([-.3, -.09, 0., 0.11, 0.14, 0.2, 0.28, 0.4, 0.6, 0.73])
y_old = 1.+np.sin(x_old[:-1]*np.pi)
# compute spline ----------------------------------
x_mids = x_old[:-1] + 0.5*np.ediff1d(x_old)
xx = np.hstack([x_old[0], x_mids, x_old[-1]])
yy = np.hstack([y_old[0], y_old, y_old[-1]])
# build spline
spl = splrep(xx, yy)
area_old = np.array(
[ splint(x_old[i],x_old[i+1], spl) for i in range(m) ])
# computing subbin areas
area_subbins = np.zeros((subbins.size-1,))
for i in range(area_subbins.size):
a, b = subbins[i:i+2]
a = max([a,x_old[0]])
b = min([b,x_old[-1]])
if b>a:
area_subbins[i] = splint(a, b, spl)
# summing subbin contributions in y_new_ref
y_new_ref = np.zeros((x_new.size-1,))
y_new_ref[1] = y_old[0] * area_subbins[2] / area_old[0]
y_new_ref[2] = y_old[0] * area_subbins[3] / area_old[0]
y_new_ref[3] = y_old[0] * area_subbins[4] / area_old[0]
y_new_ref[4] = y_old[1] * area_subbins[5] / area_old[1]
y_new_ref[5] = y_old[1] * area_subbins[6] / area_old[1]
y_new_ref[5] += y_old[2] * area_subbins[7] / area_old[2]
y_new_ref[5] += y_old[3] * area_subbins[8] / area_old[3]
# call rebin function
y_new = rebin.rebin(x_old, y_old, x_new, interp_kind=3)
assert_allclose(y_new, y_new_ref)
# ---------------------------------------------------------------------------- #
def test_y1_uncertainties_spline_with_constant_distribution():
"""
"""
# old size
m = 5
# new size
n = 6
# bin edges
x_old = np.linspace(0., 1., m+1)
x_new = np.array([-.3, -.09, 0.11, 0.14, 0.2, 0.28, 0.73])
subbins = np.array([-.3, -.09, 0., 0.11, 0.14, 0.2, 0.28, 0.4, 0.6, 0.73])
y_old = 1.+np.sin(x_old[:-1]*np.pi)
# compute spline ----------------------------------
x_mids = x_old[:-1] + 0.5*np.ediff1d(x_old)
xx = np.hstack([x_old[0], x_mids, x_old[-1]])
yy = np.hstack([y_old[0], y_old, y_old[-1]])
# build spline
spl = splrep(xx, yy)
area_old = np.array(
[ splint(x_old[i],x_old[i+1], spl) for i in range(m) ])
# with uncertainties
y_old = unp.uarray(y_old, 0.1*y_old*uniform((m,)))
# computing subbin areas
area_subbins = np.zeros((subbins.size-1,))
for i in range(area_subbins.size):
a, b = subbins[i:i+2]
a = max([a,x_old[0]])
b = min([b,x_old[-1]])
if b>a:
area_subbins[i] = splint(a, b, spl)
# summing subbin contributions in y_new_ref
a = np.zeros((x_new.size-1,))
y_new_ref = unp.uarray(a,a)
y_new_ref[1] = y_old[0] * area_subbins[2] / area_old[0]
y_new_ref[2] = y_old[0] * area_subbins[3] / area_old[0]
y_new_ref[3] = y_old[0] * area_subbins[4] / area_old[0]
y_new_ref[4] = y_old[1] * area_subbins[5] / area_old[1]
y_new_ref[5] = y_old[1] * area_subbins[6] / area_old[1]
y_new_ref[5] += y_old[2] * area_subbins[7] / area_old[2]
y_new_ref[5] += y_old[3] * area_subbins[8] / area_old[3]
# call rebin function
y_new = rebin.rebin(x_old, y_old, x_new, interp_kind=3)
# mean or nominal value comparison
assert_allclose(unp.nominal_values(y_new),
unp.nominal_values(y_new_ref))
# mean or nominal value comparison
assert_allclose(unp.std_devs(y_new),
unp.std_devs(y_new_ref))
# ---------------------------------------------------------------------------- #
# Tests for 2d rebinning
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
def test_2d_same():
"""
x1, y1 == x2, y2 implies z1 == z2
2d
"""
# old size
m = 20
n = 30
# bin edges
x_old = np.linspace(0., 1., m+1)
y_old = np.linspace(-0.5, 1.5, n+1)
z_old = np.random.random((m,n))
# rebin
z_new = rebin.rebin2d(x_old, y_old, z_old, x_old, y_old)
assert_allclose(z_old, z_new)
# ---------------------------------------------------------------------------- #
def test_2d_constant_distribution():
"""
various new domains with a constant underlying distribution
2d
"""
# old size
m = 8
n = 11
# new size
p = 5
q = 14
new_bounds = [ (0., 1., -1.5, 1.7),
(0., 1., -1.5, 0.7),
(0., 1., -1.5, -0.7),
(-1., 1.5, -1.5, 1.7),
(-1., 0.5, -1., 0.5),
(0.1, 0.6, 0.1, 0.5),
(0.01, 0.02, -10.0, 20.7)]
for (a,b,c,d) in new_bounds:
# bin edges
x_old = np.linspace(0., 1., m+1)
y_old = np.linspace(-0.5, 1.5, n+1)
x_new = np.linspace(a, b, p+1)
y_new = np.linspace(c, d, q+1)
# constant spline
z_old = np.ones((m+1,n+1))
mms_spline = BoundedRectBivariateSpline(x_old, y_old, z_old, s=0.)
z_old = np.zeros((m,n))
for i in range(m):
for j in range(n):
z_old[i,j] = mms_spline.integral(x_old[i], x_old[i+1],
y_old[j], y_old[j+1])
z_new_mms = np.zeros((p,q))
for i in range(p):
for j in range(q):
z_new_mms[i,j] = mms_spline.integral(x_new[i], x_new[i+1],
y_new[j], y_new[j+1])
# rebin
z_new = rebin.rebin2d(x_old, y_old, z_old, x_new, y_new)
assert_allclose(z_new, z_new_mms)