Skip to content

Commit

Permalink
added tests for rebinning
Browse files Browse the repository at this point in the history
  • Loading branch information
fzeiser committed May 16, 2020
1 parent 2b68504 commit 5b669d0
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 1 deletion.
2 changes: 2 additions & 0 deletions ompy/rebin.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ cdef double overlap(double edge_in_l, double edge_in_u,
def rebin_1D(np.ndarray counts, np.ndarray mids_in, np.ndarray mids_out):
"""Rebin an array of counts from binning mids_in to binning mids_out
Assumes equidistant binning.
Args:
counts: Array of counts to be rebinned
mids_in: Array of mid-bins energies giving
Expand Down
2 changes: 1 addition & 1 deletion ompy/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def cut_nan(self, inplace: bool = True) -> Vector:
def rebin(self, mids: Optional[Sequence[float]] = None,
factor: Optional[float] = None,
inplace: bool = True) -> Optional[Vector]:
""" Rebins vector
""" Rebins vector, assuming equidistant binning
Args:
mids: The new energy mids. Can not be
Expand Down
105 changes: 105 additions & 0 deletions tests/test_rebin.py
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)
1 change: 1 addition & 0 deletions tests/test_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def test_numericals():
assert_equal((vector2@vector1).values, values2@values1)
assert_equal((vector1@vector2).values, values1@values2)


# This does not work as of now...
# def test_mutable():
# E = np.array([0, 1, 2])
Expand Down

0 comments on commit 5b669d0

Please sign in to comment.