-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
fzeiser
committed
May 16, 2020
1 parent
2b68504
commit 5b669d0
Showing
4 changed files
with
109 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import pytest | ||
from contextlib import contextmanager | ||
import numpy as np | ||
from numpy.testing import assert_equal, assert_allclose | ||
import ompy as om | ||
|
||
vals_rebinned = [10-2*2.5, 2.5+5, 20-2*5, 0.25*20+0.25*30] | ||
def test_rebin_vector(): | ||
before = om.Vector(values=[10, 20, 30], E=[10., 20, 30]) | ||
after = om.Vector(values=vals_rebinned, | ||
E=[10., 15, 20, 25]) | ||
rebinned = before.rebin(mids=after.E, inplace=False) | ||
assert_equal(rebinned.E, after.E) | ||
assert_allclose(rebinned.values, after.values) | ||
|
||
|
||
@pytest.mark.xfail | ||
def test_rebin_vector_non_equidistant(before, after): | ||
before = om.Vector(values=[10, 20, 30], E=[10., 20, 30]) | ||
after = om.Vector(values=[10-2*2.5, 2.5+5, 20-2*5, 30], | ||
E=[10., 15, 20, 30]) | ||
rebinned = before.rebin(mids=after.E, inplace=False) | ||
assert_equal(rebinned.E, after.E) | ||
assert_allclose(rebinned.values, after.values) | ||
|
||
|
||
def test_rebin_matrix_Eg(): | ||
values = np.array([[10, 20, 30], [10, 20, 30], [10, 20, 30]]) | ||
before = om.Matrix(values=values, | ||
Ex=[10., 20, 30], | ||
Eg=[10., 20, 30]) | ||
|
||
values = np.array([vals_rebinned, vals_rebinned, vals_rebinned]) | ||
after = om.Matrix(values=values, | ||
Ex=[10, 20, 30], | ||
Eg=[10., 15, 20, 25]) | ||
rebinned = before.rebin(axis="Eg", mids=after.Eg, inplace=False) | ||
assert_equal(rebinned.Eg, after.Eg) | ||
assert_equal(rebinned.Ex, after.Ex) | ||
assert_allclose(rebinned.values, after.values) | ||
|
||
|
||
def test_rebin_matrix_Ex(): | ||
values = np.array([[10, 20, 30], [10, 20, 30], [10, 20, 30]]).T | ||
before = om.Matrix(values=values, | ||
Eg=[10., 20, 30], | ||
Ex=[10., 20, 30]) | ||
|
||
values = np.array([vals_rebinned, vals_rebinned, vals_rebinned]).T | ||
after = om.Matrix(values=values, | ||
Eg=[10, 20, 30], | ||
Ex=[10., 15, 20, 25]) | ||
rebinned = before.rebin(axis="Ex", mids=after.Ex, inplace=False) | ||
assert_equal(rebinned.Eg, after.Eg) | ||
assert_equal(rebinned.Ex, after.Ex) | ||
assert_allclose(rebinned.values, after.values) | ||
|
||
|
||
np.random.seed(678456456) | ||
vec1 = om.Vector(values=[1., 1, 1, 1], E=[2., 3, 4, 5]) | ||
vec2 = om.Vector(values=[0.2, 1, 4, 1], E=[2., 2.5, 4, 5]) | ||
vec3 = om.Vector(values=np.random.uniform(size=50), | ||
E=np.linspace(-10, 10, num=50)) | ||
@pytest.mark.parametrize( | ||
"vec, factor", | ||
[(vec1, 1), | ||
(vec2, 1.5), | ||
(vec2, 2.), | ||
(vec3, 5), | ||
# (vec2, 3), | ||
pytest.param(vec1, 1/4, | ||
marks=pytest.mark.xfail(reason="see issue #122")) | ||
],) | ||
def test_rebin_factor_preserve_counts(vec, factor): | ||
sum_before = vec.values.sum() | ||
vec = vec.rebin(factor=factor, inplace=False) | ||
sum_after = vec.values.sum() | ||
assert_allclose(sum_before, sum_after) | ||
|
||
|
||
@contextmanager | ||
def does_not_raise(): | ||
yield | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"vec, mids, expectation", | ||
[(vec3, np.linspace(vec3.E[0], vec3.E[-1], num=7), | ||
does_not_raise()), | ||
(vec3, np.linspace(vec3.E[0]-2, vec3.E[-1], num=9), | ||
does_not_raise()), | ||
(vec3, np.linspace(vec3.E[0]-2, vec3.E[-1]+2, num=3), | ||
does_not_raise()), | ||
(vec3, np.linspace(vec3.E[1], vec3.E[-1], num=31), | ||
pytest.raises(AssertionError)), | ||
pytest.param(vec3, np.linspace(vec3.E[0], vec3.E[-1], num=51), | ||
does_not_raise(), | ||
marks=pytest.mark.xfail(reason="see issue #122")) | ||
],) | ||
def test_rebin_mids_preserve_counts(vec, mids, expectation): | ||
with expectation: | ||
sum_before = vec.values.sum() | ||
vec = vec.rebin(mids=mids, inplace=False) | ||
sum_after = vec.values.sum() | ||
assert_allclose(sum_before, sum_after) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters