def test_reverse_cumulative(self): histo = Histogram(n_bins=5) histo.histogram(self.data) rev_cumulative = histo.reverse_cumulative(maximum=None) assert_items_almost_equal(list(rev_cumulative.values()), [10, 5, 5, 3, 2, 1]) rev_cumulative = histo.reverse_cumulative(maximum=1.0) assert_items_almost_equal(list(rev_cumulative.values()), [1.0, 0.5, 0.5, 0.3, 0.2, 0.1])
def test_compare_parameters(self): assert self.hist_nbins.compare_parameters(None) is False assert (self.hist_nbins_range.compare_parameters( self.hist_binwidth_range) is True) assert (self.hist_binwidth_range.compare_parameters( self.hist_nbins_range) is True) histo = Histogram(n_bins=5) assert self.hist_nbins_range.compare_parameters(histo) is False histo.histogram(self.data) assert self.hist_nbins_range.compare_parameters(histo) is False assert (self.hist_nbins_range.compare_parameters(self.hist_nbins) is False) assert histo.compare_parameters(self.hist_nbins) is False assert self.hist_nbins.compare_parameters(histo) is False
def test_normalized(self): histo = Histogram(n_bins=5) _ = histo.histogram(self.data) assert (list(histo.normalized().values()) == [1.0, 0.0, 0.4, 0.2, 0.2, 0.2]) assert (list(histo.normalized(raw_probability=True).values()) == [0.5, 0.0, 0.2, 0.1, 0.1, 0.1])
def test_cumulative(self): histo = Histogram(n_bins=5) _ = histo.histogram(self.data) cumulative = list(histo.cumulative(None).values()) assert_items_almost_equal(cumulative, [5.0, 5.0, 7.0, 8.0, 9.0, 10.0]) assert_items_almost_equal(histo.cumulative(maximum=1.0), [0.5, 0.5, 0.7, 0.8, 0.9, 1.0])
def test_cumulative(self): histo = Histogram(n_bins=5) hist = histo.histogram(self.data) cumulative = list(histo.cumulative(None).values()) assert_items_almost_equal(cumulative, [5.0, 5.0, 7.0, 8.0, 9.0, 10.0]) assert_items_almost_equal(histo.cumulative(maximum=1.0), [0.5, 0.5, 0.7, 0.8, 0.9, 1.0])
def test_normalized(self): histo = Histogram(n_bins=5) hist = histo.histogram(self.data) assert_items_equal(list(histo.normalized().values()), [1.0, 0.0, 0.4, 0.2, 0.2, 0.2]) assert_items_equal(list(histo.normalized(raw_probability=True).values()), [0.5, 0.0, 0.2, 0.1, 0.1, 0.1])
def test_compare_parameters(self): assert_equal(self.hist_nbins.compare_parameters(None), False) assert_equal( self.hist_nbins_range.compare_parameters(self.hist_binwidth_range), True) assert_equal( self.hist_binwidth_range.compare_parameters(self.hist_nbins_range), True) histo = Histogram(n_bins=5) assert_equal(self.hist_nbins_range.compare_parameters(histo), False) histo.histogram(self.data) assert_equal(self.hist_nbins_range.compare_parameters(histo), False) assert_equal(self.hist_nbins_range.compare_parameters(self.hist_nbins), False) assert_equal(histo.compare_parameters(self.hist_nbins), False) assert_equal(self.hist_nbins.compare_parameters(histo), False)
def test_xvals(self): histo = Histogram(n_bins=5) _ = histo.histogram(self.data) # need this to set the bins assert histo.left_bin_edges == self.left_bin_edges assert histo.bin_widths == self.bin_widths assert all(histo.xvals("l") == [1.0, 1.5, 2.0, 2.5, 3.0, 3.5]) assert all(histo.xvals("r") == [1.5, 2.0, 2.5, 3.0, 3.5, 4.0]) assert all(histo.xvals("m") == [1.25, 1.75, 2.25, 2.75, 3.25, 3.75])
def test_normalized(self): histo = Histogram(n_bins=5) hist = histo.histogram(self.data) assert_items_equal(histo.normalized().values(), [1.0, 0.0, 0.4, 0.2, 0.2, 0.2]) assert_items_equal( histo.normalized(raw_probability=True).values(), [0.5, 0.0, 0.2, 0.1, 0.1, 0.1])
def test_xvals(self): histo = Histogram(n_bins=5) hist = histo.histogram(self.data) # need this to set the bins assert_equal(histo.left_bin_edges, self.left_bin_edges) assert_equal(histo.bin_widths, self.bin_widths) assert_items_equal(histo.xvals("l"), [1.0, 1.5, 2.0, 2.5, 3.0, 3.5]) assert_items_equal(histo.xvals("r"), [1.5, 2.0, 2.5, 3.0, 3.5, 4.0]) assert_items_equal(histo.xvals("m"), [1.25, 1.75, 2.25, 2.75, 3.25, 3.75])
def test_reverse_cumulative(self): histo = Histogram(n_bins=5) hist = histo.histogram(self.data) rev_cumulative = histo.reverse_cumulative(maximum=None) assert_items_almost_equal(list(rev_cumulative.values()), [10, 5, 5, 3, 2, 1]) rev_cumulative = histo.reverse_cumulative(maximum=1.0) assert_items_almost_equal(list(rev_cumulative.values()), [1.0, 0.5, 0.5, 0.3, 0.2, 0.1])
def test_compare_parameters(self): assert_equal(self.hist_nbins.compare_parameters(None), False) assert_equal( self.hist_nbins_range.compare_parameters(self.hist_binwidth_range), True ) assert_equal( self.hist_binwidth_range.compare_parameters(self.hist_nbins_range), True ) histo = Histogram(n_bins=5) assert_equal(self.hist_nbins_range.compare_parameters(histo), False) histo.histogram(self.data) assert_equal(self.hist_nbins_range.compare_parameters(histo), False) assert_equal( self.hist_nbins_range.compare_parameters(self.hist_nbins), False ) assert_equal(histo.compare_parameters(self.hist_nbins), False) assert_equal(self.hist_nbins.compare_parameters(histo), False)
def test_left_bin_error(self): histo = Histogram(bin_width=0.5, bin_range=(-1.0, 3.5)) histo.histogram([3.5]) assert histo.reverse_cumulative() != 0
class TestHistogram(object): def setup(self): self.data = [1.0, 1.1, 1.2, 1.3, 2.0, 1.4, 2.3, 2.5, 3.1, 3.5] self.nbins = 5 hist_counts = [5, 0, 2, 1, 1, 1] self.bins = [1.0, 1.5, 2.0, 2.5, 3.0, 3.5] self.left_bin_edges = (1.0,) self.bin_widths = (0.5,) self.hist = collections.Counter({(0,): 5, (2,): 2, (3,): 1, (4,): 1, (5,): 1}) self.default_hist = Histogram() self.hist_nbins = Histogram(n_bins=5) self.hist_nbins_binwidth = Histogram(n_bins=5, bin_width=1.0) self.hist_nbins_range = Histogram(n_bins=5, bin_range=(1.0, 3.5)) self.hist_binwidth_range = Histogram(bin_width=0.5, bin_range=(1.0, 3.5)) def test_initialization(self): assert_equal(self.default_hist.bins, 20) assert_equal(self.hist_nbins.bins, 5) assert_equal(self.hist_nbins.bin_width, None) assert_equal(self.hist_nbins_binwidth.bins, 5) assert_equal(self.hist_nbins_binwidth.bin_width, None) assert_items_equal(self.hist_nbins_range.bins, self.bins) assert_items_equal(self.hist_binwidth_range.bins, self.bins) def test_build_from_data(self): hist = self.hist_nbins.histogram(self.data) assert_equal(self.hist_nbins.count, 10) assert_items_equal(hist, self.hist) hist2 = self.hist_binwidth_range.histogram(self.data) assert_equal(self.hist_binwidth_range.count, 10) assert_items_equal(hist2, self.hist) @raises(RuntimeError) def test_build_from_data_fail(self): histo = Histogram(n_bins=5) histo.histogram() def test_add_data_to_histogram(self): histogram = Histogram(n_bins=5, bin_range=(1.0, 3.5)) hist = histogram.add_data_to_histogram(self.data) assert_equal(histogram.count, 10) assert_items_equal(hist, self.hist) hist2 = histogram.add_data_to_histogram(self.data) assert_items_equal(hist2, hist+hist) assert_equal(histogram.count, 20) def test_compare_parameters(self): assert_equal(self.hist_nbins.compare_parameters(None), False) assert_equal( self.hist_nbins_range.compare_parameters(self.hist_binwidth_range), True ) assert_equal( self.hist_binwidth_range.compare_parameters(self.hist_nbins_range), True ) histo = Histogram(n_bins=5) assert_equal(self.hist_nbins_range.compare_parameters(histo), False) histo.histogram(self.data) assert_equal(self.hist_nbins_range.compare_parameters(histo), False) assert_equal( self.hist_nbins_range.compare_parameters(self.hist_nbins), False ) assert_equal(histo.compare_parameters(self.hist_nbins), False) assert_equal(self.hist_nbins.compare_parameters(histo), False) def test_xvals(self): histo = Histogram(n_bins=5) hist = histo.histogram(self.data) # need this to set the bins assert_equal(histo.left_bin_edges, self.left_bin_edges) assert_equal(histo.bin_widths, self.bin_widths) assert_items_equal(histo.xvals("l"), [1.0, 1.5, 2.0, 2.5, 3.0, 3.5]) assert_items_equal(histo.xvals("r"), [1.5, 2.0, 2.5, 3.0, 3.5, 4.0]) assert_items_equal(histo.xvals("m"), [1.25, 1.75, 2.25, 2.75, 3.25, 3.75]) def test_normalization(self): histo = Histogram(n_bins=5) hist = histo.histogram(self.data) assert_equal(histo._normalization(), 5.0) def test_normalized(self): histo = Histogram(n_bins=5) hist = histo.histogram(self.data) assert_items_equal(list(histo.normalized().values()), [1.0, 0.0, 0.4, 0.2, 0.2, 0.2]) assert_items_equal(list(histo.normalized(raw_probability=True).values()), [0.5, 0.0, 0.2, 0.1, 0.1, 0.1]) def test_cumulative(self): histo = Histogram(n_bins=5) hist = histo.histogram(self.data) cumulative = list(histo.cumulative(None).values()) assert_items_almost_equal(cumulative, [5.0, 5.0, 7.0, 8.0, 9.0, 10.0]) assert_items_almost_equal(histo.cumulative(maximum=1.0), [0.5, 0.5, 0.7, 0.8, 0.9, 1.0]) def test_reverse_cumulative(self): histo = Histogram(n_bins=5) hist = histo.histogram(self.data) rev_cumulative = histo.reverse_cumulative(maximum=None) assert_items_almost_equal(list(rev_cumulative.values()), [10, 5, 5, 3, 2, 1]) rev_cumulative = histo.reverse_cumulative(maximum=1.0) assert_items_almost_equal(list(rev_cumulative.values()), [1.0, 0.5, 0.5, 0.3, 0.2, 0.1])
def test_build_from_data_fail(self): histo = Histogram(n_bins=5) histo.histogram()
def test_normalization(self): histo = Histogram(n_bins=5) _ = histo.histogram(self.data) assert histo._normalization() == 5.0
def test_normalization(self): histo = Histogram(n_bins=5) hist = histo.histogram(self.data) assert_equal(histo._normalization(), 5.0)
class TestHistogram(object): def setup(self): self.data = [1.0, 1.1, 1.2, 1.3, 2.0, 1.4, 2.3, 2.5, 3.1, 3.5] self.nbins = 5 self.bins = [1.0, 1.5, 2.0, 2.5, 3.0, 3.5] self.left_bin_edges = (1.0,) self.bin_widths = (0.5,) self.hist = collections.Counter({(0,): 5, (2,): 2, (3,): 1, (4,): 1, (5,): 1}) self.default_hist = Histogram() self.hist_nbins = Histogram(n_bins=5) self.hist_nbins_binwidth = Histogram(n_bins=5, bin_width=1.0) self.hist_nbins_range = Histogram(n_bins=5, bin_range=(1.0, 3.5)) self.hist_binwidth_range = Histogram(bin_width=0.5, bin_range=(1.0, 3.5)) def test_initialization(self): assert self.default_hist.bins == 20 assert self.hist_nbins.bins == 5 assert self.hist_nbins.bin_width is None assert self.hist_nbins_binwidth.bins == 5 assert self.hist_nbins_binwidth.bin_width is None assert self.hist_nbins_range.bins == self.bins assert self.hist_binwidth_range.bins == self.bins def test_build_from_data(self): hist = self.hist_nbins.histogram(self.data) assert self.hist_nbins.count == 10 assert hist == self.hist hist2 = self.hist_binwidth_range.histogram(self.data) assert self.hist_binwidth_range.count == 10 assert hist2 == self.hist def test_build_from_data_fail(self): histo = Histogram(n_bins=5) with pytest.raises(RuntimeError, match="called without data"): histo.histogram() def test_add_data_to_histogram(self): histogram = Histogram(n_bins=5, bin_range=(1.0, 3.5)) hist = histogram.add_data_to_histogram(self.data) assert histogram.count == 10 assert hist == self.hist hist2 = histogram.add_data_to_histogram(self.data) assert hist2 == hist+hist assert histogram.count == 20 def test_compare_parameters(self): assert self.hist_nbins.compare_parameters(None) is False assert ( self.hist_nbins_range.compare_parameters(self.hist_binwidth_range) is True ) assert ( self.hist_binwidth_range.compare_parameters(self.hist_nbins_range) is True ) histo = Histogram(n_bins=5) assert self.hist_nbins_range.compare_parameters(histo) is False histo.histogram(self.data) assert self.hist_nbins_range.compare_parameters(histo) is False assert ( self.hist_nbins_range.compare_parameters(self.hist_nbins) is False ) assert histo.compare_parameters(self.hist_nbins) is False assert self.hist_nbins.compare_parameters(histo) is False def test_compare_parameters_empty(self): # regression test; this was preventing histograms from being added hist = self.hist_binwidth_range assert hist.compare_parameters(hist.empty_copy()) def test_xvals(self): histo = Histogram(n_bins=5) _ = histo.histogram(self.data) # need this to set the bins assert histo.left_bin_edges == self.left_bin_edges assert histo.bin_widths == self.bin_widths assert all(histo.xvals("l") == [1.0, 1.5, 2.0, 2.5, 3.0, 3.5]) assert all(histo.xvals("r") == [1.5, 2.0, 2.5, 3.0, 3.5, 4.0]) assert all(histo.xvals("m") == [1.25, 1.75, 2.25, 2.75, 3.25, 3.75]) def test_normalization(self): histo = Histogram(n_bins=5) _ = histo.histogram(self.data) assert histo._normalization() == 5.0 def test_normalized(self): histo = Histogram(n_bins=5) _ = histo.histogram(self.data) assert (list(histo.normalized().values()) == [1.0, 0.0, 0.4, 0.2, 0.2, 0.2]) assert (list(histo.normalized(raw_probability=True).values()) == [0.5, 0.0, 0.2, 0.1, 0.1, 0.1]) def test_cumulative(self): histo = Histogram(n_bins=5) _ = histo.histogram(self.data) cumulative = list(histo.cumulative(None).values()) assert_items_almost_equal(cumulative, [5.0, 5.0, 7.0, 8.0, 9.0, 10.0]) assert_items_almost_equal(histo.cumulative(maximum=1.0), [0.5, 0.5, 0.7, 0.8, 0.9, 1.0]) def test_cumulative_all_zero_warn(self): histo = Histogram(bin_width=0.5, bin_range=(1.0, 3.5)) histo._histogram = collections.Counter({(0,): 0, (1,): 0}) with pytest.warns(UserWarning, match=r"No non-zero"): cumul = histo.cumulative() assert cumul(2.13) == 0 for val in cumul.values(): assert val == 0 def test_reverse_cumulative(self): histo = Histogram(n_bins=5) histo.histogram(self.data) rev_cumulative = histo.reverse_cumulative(maximum=None) assert_items_almost_equal(list(rev_cumulative.values()), [10, 5, 5, 3, 2, 1]) rev_cumulative = histo.reverse_cumulative(maximum=1.0) assert_items_almost_equal(list(rev_cumulative.values()), [1.0, 0.5, 0.5, 0.3, 0.2, 0.1]) def test_reverse_cumulative_all_zero_warn(self): histo = Histogram(bin_width=0.5, bin_range=(1.0, 3.5)) histo._histogram = collections.Counter({(0,): 0, (1,): 0}) with pytest.warns(UserWarning, match=r"No non-zero"): rcumul = histo.reverse_cumulative() assert rcumul(3.12) == 0 for val in rcumul.values(): assert val == 0 def test_left_bin_error(self): histo = Histogram(bin_width=0.5, bin_range=(-1.0, 3.5)) histo.histogram([3.5]) assert histo.reverse_cumulative() != 0
def test_build_from_data_fail(self): histo = Histogram(n_bins=5) with pytest.raises(RuntimeError, match="called without data"): histo.histogram()
class testHistogram(object): def setup(self): self.data = [1.0, 1.1, 1.2, 1.3, 2.0, 1.4, 2.3, 2.5, 3.1, 3.5] self.nbins = 5 hist_counts = [5, 0, 2, 1, 1, 1] self.bins = [1.0, 1.5, 2.0, 2.5, 3.0, 3.5] self.left_bin_edges = (1.0, ) self.bin_widths = (0.5, ) self.hist = collections.Counter({ (0, ): 5, (2, ): 2, (3, ): 1, (4, ): 1, (5, ): 1 }) self.default_hist = Histogram() self.hist_nbins = Histogram(n_bins=5) self.hist_nbins_binwidth = Histogram(n_bins=5, bin_width=1.0) self.hist_nbins_range = Histogram(n_bins=5, bin_range=(1.0, 3.5)) self.hist_binwidth_range = Histogram(bin_width=0.5, bin_range=(1.0, 3.5)) def test_initialization(self): assert_equal(self.default_hist.bins, 20) assert_equal(self.hist_nbins.bins, 5) assert_equal(self.hist_nbins.bin_width, None) assert_equal(self.hist_nbins_binwidth.bins, 5) assert_equal(self.hist_nbins_binwidth.bin_width, None) assert_items_equal(self.hist_nbins_range.bins, self.bins) assert_items_equal(self.hist_binwidth_range.bins, self.bins) def test_build_from_data(self): hist = self.hist_nbins.histogram(self.data) assert_equal(self.hist_nbins.count, 10) assert_items_equal(hist, self.hist) hist2 = self.hist_binwidth_range.histogram(self.data) assert_equal(self.hist_binwidth_range.count, 10) assert_items_equal(hist2, self.hist) @raises(RuntimeError) def test_build_from_data_fail(self): histo = Histogram(n_bins=5) histo.histogram() def test_add_data_to_histogram(self): histogram = Histogram(n_bins=5, bin_range=(1.0, 3.5)) hist = histogram.add_data_to_histogram(self.data) assert_equal(histogram.count, 10) assert_items_equal(hist, self.hist) hist2 = histogram.add_data_to_histogram(self.data) assert_items_equal(hist2, hist + hist) assert_equal(histogram.count, 20) def test_compare_parameters(self): assert_equal(self.hist_nbins.compare_parameters(None), False) assert_equal( self.hist_nbins_range.compare_parameters(self.hist_binwidth_range), True) assert_equal( self.hist_binwidth_range.compare_parameters(self.hist_nbins_range), True) histo = Histogram(n_bins=5) assert_equal(self.hist_nbins_range.compare_parameters(histo), False) histo.histogram(self.data) assert_equal(self.hist_nbins_range.compare_parameters(histo), False) assert_equal(self.hist_nbins_range.compare_parameters(self.hist_nbins), False) assert_equal(histo.compare_parameters(self.hist_nbins), False) assert_equal(self.hist_nbins.compare_parameters(histo), False) def test_xvals(self): histo = Histogram(n_bins=5) hist = histo.histogram(self.data) # need this to set the bins assert_equal(histo.left_bin_edges, self.left_bin_edges) assert_equal(histo.bin_widths, self.bin_widths) assert_items_equal(histo.xvals("l"), [1.0, 1.5, 2.0, 2.5, 3.0, 3.5]) assert_items_equal(histo.xvals("r"), [1.5, 2.0, 2.5, 3.0, 3.5, 4.0]) assert_items_equal(histo.xvals("m"), [1.25, 1.75, 2.25, 2.75, 3.25, 3.75]) def test_normalization(self): histo = Histogram(n_bins=5) hist = histo.histogram(self.data) assert_equal(histo._normalization(), 5.0) def test_normalized(self): histo = Histogram(n_bins=5) hist = histo.histogram(self.data) assert_items_equal(histo.normalized().values(), [1.0, 0.0, 0.4, 0.2, 0.2, 0.2]) assert_items_equal( histo.normalized(raw_probability=True).values(), [0.5, 0.0, 0.2, 0.1, 0.1, 0.1]) def test_cumulative(self): histo = Histogram(n_bins=5) hist = histo.histogram(self.data) cumulative = histo.cumulative(None).values() assert_items_almost_equal(cumulative, [5.0, 5.0, 7.0, 8.0, 9.0, 10.0]) assert_items_almost_equal(histo.cumulative(maximum=1.0), [0.5, 0.5, 0.7, 0.8, 0.9, 1.0]) def test_reverse_cumulative(self): histo = Histogram(n_bins=5) hist = histo.histogram(self.data) rev_cumulative = histo.reverse_cumulative(maximum=None) assert_items_almost_equal(rev_cumulative.values(), [10, 5, 5, 3, 2, 1]) rev_cumulative = histo.reverse_cumulative(maximum=1.0) assert_items_almost_equal(rev_cumulative.values(), [1.0, 0.5, 0.5, 0.3, 0.2, 0.1])