Some checks failed
gitea-physics/tantri/pipeline/head There was a failure building this commit
112 lines
3.2 KiB
Python
112 lines
3.2 KiB
Python
import pytest
|
|
import tantri.binning.binning as binning
|
|
import numpy
|
|
|
|
|
|
def test_bin_construction_faulty_min():
|
|
x_list = numpy.array([5, 6, 7, 8])
|
|
|
|
bin_config = binning.BinConfig(log_scale=False, bin_width=0.8, bin_min=5.5)
|
|
|
|
with pytest.raises(ValueError):
|
|
binning._construct_bins(x_list, bin_config)
|
|
|
|
|
|
def test_bin_construction_force_min():
|
|
x_list = numpy.array([4.5, 5.5, 6.5, 7.5, 8.5])
|
|
|
|
bin_config = binning.BinConfig(log_scale=False, bin_width=1, bin_min=2)
|
|
|
|
expected_bins = numpy.array([2, 3, 4, 5, 6, 7, 8, 9])
|
|
|
|
actual_bins = binning._construct_bins(x_list, bin_config=bin_config)
|
|
numpy.testing.assert_allclose(
|
|
actual_bins, expected_bins, err_msg="The bins were not as expected"
|
|
)
|
|
|
|
|
|
def test_bin_construction_even():
|
|
x_list = numpy.array([1, 2.8, 8, 12.2, 13.6, 17, 19.71, 20, 24, 33])
|
|
|
|
bin_config = binning.BinConfig(log_scale=False, bin_width=8)
|
|
expected_bins = numpy.array([1, 9, 17, 25, 33])
|
|
|
|
actual_bins = binning._construct_bins(x_list, bin_config=bin_config)
|
|
numpy.testing.assert_allclose(
|
|
actual_bins, expected_bins, err_msg="The bins were not as expected"
|
|
)
|
|
|
|
|
|
def test_bin_construction_uneven():
|
|
x_list = numpy.array([1, 2.8, 8, 12.2, 13.6, 17, 19.71, 20, 24, 33])
|
|
|
|
bin_config = binning.BinConfig(log_scale=False, bin_width=7)
|
|
expected_bins = numpy.array([1, 8, 15, 22, 29, 36])
|
|
|
|
actual_bins = binning._construct_bins(x_list, bin_config=bin_config)
|
|
numpy.testing.assert_allclose(
|
|
actual_bins, expected_bins, err_msg="The bins were not as expected"
|
|
)
|
|
|
|
|
|
def test_bin_construction_uneven_non_integer():
|
|
x_list = numpy.array([1, 2.8, 8, 12.2, 13.6, 17, 19.71, 20, 24, 33])
|
|
|
|
bin_config = binning.BinConfig(log_scale=False, bin_width=7.5)
|
|
expected_bins = numpy.array([1, 8.5, 16, 23.5, 31, 38.5])
|
|
|
|
actual_bins = binning._construct_bins(x_list, bin_config=bin_config)
|
|
numpy.testing.assert_allclose(
|
|
actual_bins, expected_bins, err_msg="The bins were not as expected"
|
|
)
|
|
|
|
|
|
def test_group_x_bins(snapshot):
|
|
x_list = numpy.array([1, 2.8, 8, 12.2, 13.6, 17, 19.71, 20, 24, 33])
|
|
y_dict = {
|
|
"identity_plus_one": (
|
|
numpy.array([1, 2.8, 8, 12.2, 13.6, 17, 19.71, 20, 24, 33]) + 2
|
|
)
|
|
}
|
|
|
|
bin_config = binning.BinConfig(log_scale=False, bin_width=8)
|
|
# expected_bins = numpy.array([1, 9, 17, 25, 33])
|
|
|
|
binned = binning.bin_lists(x_list, y_dict, bin_config)
|
|
|
|
assert binned == snapshot
|
|
|
|
|
|
def test_group_x_bins_mean(snapshot):
|
|
x_list = numpy.array([1, 2.8, 8, 12.2, 13.6, 17, 19.71, 20, 24, 33])
|
|
y_dict = {
|
|
"identity_plus_one": (
|
|
numpy.array([1, 2.8, 8, 12.2, 13.6, 17, 19.71, 20, 24, 33]) + 2
|
|
)
|
|
}
|
|
|
|
bin_config = binning.BinConfig(log_scale=False, bin_width=8)
|
|
# expected_bins = numpy.array([1, 9, 17, 25, 33])
|
|
|
|
binned = binning.bin_lists(x_list, y_dict, bin_config)
|
|
mean_binned = [bin.mean_point() for bin in binned]
|
|
|
|
assert mean_binned == snapshot
|
|
|
|
|
|
def test_group_x_bins_summary(snapshot):
|
|
x_list = numpy.array([1, 2.8, 8, 12.2, 13.6, 17, 19.71, 20, 24, 33])
|
|
y_dict = {
|
|
"identity_plus_one": (
|
|
numpy.array([1, 2.8, 8, 12.2, 13.6, 17, 19.71, 20, 24, 33]) + 2
|
|
)
|
|
}
|
|
|
|
bin_config = binning.BinConfig(log_scale=False, bin_width=8)
|
|
# expected_bins = numpy.array([1, 9, 17, 25, 33])
|
|
|
|
binned = binning.bin_lists(x_list, y_dict, bin_config)
|
|
summary = [bin.summary_point() for bin in binned]
|
|
|
|
assert summary == snapshot
|