Beispiel #1
0
def test_resample_bins_nd():
    from pyirf.binning import resample_histogram1d
    n = 10

    # shape 2x3x10
    data = np.array([
        [
            np.ones(n),
            np.ones(n) * 2,
            np.ones(n) * 3,
        ],
        [
            np.ones(n) * 4,
            np.ones(n) * 5,
            np.ones(n) * 6,
        ],
    ])

    edges = np.arange(n + 1)
    new_data = resample_histogram1d(data, edges, edges, axis=2)
    np.testing.assert_array_almost_equal(new_data, data)

    edges = np.arange(3 + 1)
    new_data = resample_histogram1d(data, edges, edges, axis=1)
    np.testing.assert_array_almost_equal(new_data, data)
Beispiel #2
0
def test_resample_bins_1d():
    from pyirf.binning import resample_histogram1d

    n = 10
    data = np.ones(n)
    edges = np.arange(n + 1)

    # no resampling
    new_data = resample_histogram1d(data, edges, edges)
    np.testing.assert_array_almost_equal(new_data, data)

    # resampling, less bins
    new_edges = np.arange(n + 1, step=2)
    true_new_data = np.ones(n // 2) * 2
    new_data = resample_histogram1d(data, edges, new_edges)
    np.testing.assert_array_almost_equal(new_data, true_new_data)

    # resampling, smaller range (lose normalization)
    new_edges = np.arange(n // 2 + 1)
    true_new_data = np.ones(n // 2)
    new_data = resample_histogram1d(data, edges, new_edges)
    np.testing.assert_array_almost_equal(new_data, true_new_data)

    # resampling, larger range (fill with zeros)
    new_edges = np.arange(-n, 2 * n + 1)
    true_new_data = np.concatenate([np.zeros(n), np.ones(n), np.zeros(n)])
    new_data = resample_histogram1d(data, edges, new_edges)
    np.testing.assert_array_almost_equal(new_data, true_new_data)
Beispiel #3
0
def test_resample_bins_units():
    from pyirf.binning import resample_histogram1d
    import pytest

    n = 10
    data = np.ones(n) * u.m

    old_edges = np.arange(n + 1) * u.TeV
    new_edges = old_edges.copy().to(u.GeV)

    new_data = resample_histogram1d(data, old_edges, new_edges)

    np.testing.assert_array_almost_equal(
        new_data.to_value(data.unit), data.to_value(data.unit),
    )

    with pytest.raises(ValueError):
        new_data = resample_histogram1d(data, old_edges, new_edges.value)

    with pytest.raises(u.core.UnitConversionError):
        new_data = resample_histogram1d(data, old_edges, u.Quantity(new_edges.value, unit=u.m))