Example #1
0
    def test_eq(self):
        bins = [1.2, 1.4, 1.5, 1.7, 1.8]
        values = [4, 0, 3, 7.2]
        other1 = Histogram1D(bins, values, underflow=2, overflow=1)
        assert other1 == example

        bins = [1.22, 1.4, 1.5, 1.7, 1.8]
        values = [4, 0, 3, 7.2]
        other2 = Histogram1D(bins, values, underflow=2, overflow=1)
        assert other2 != example

        bins = [1.2, 1.4, 1.5, 1.7, 1.8]
        values = [4, 0, 13, 7.2]
        other3 = Histogram1D(bins, values, underflow=2, overflow=1)
        assert other3 != example

        bins = [1.2, 1.4, 1.5, 1.7, 1.8]
        values = [4, 0, 3, 7.2]
        errors2 = [4, 0, 3, 7.2]
        other4 = Histogram1D(bins, values, errors2, underflow=2, overflow=1)
        assert other4 == example

        errors2 = [4, 0, 3, 8.2]
        other5 = Histogram1D(bins, values, errors2, underflow=2, overflow=1)
        assert other5 != example
Example #2
0
    def test_compat(self):
        book1, book2 = BaseBook(), BaseBook()
        book1["a"] = Histogram1D(range(0, 4), stats={"sum": 0.0, "sum2": 0.0})
        book1["a"].fill_n(np.asarray([0, 0, 0]))
        book2["a"] = Histogram1D(range(0, 4), stats={"sum": 0.0, "sum2": 0.0})
        book2["a"].fill_n(np.asarray([0, 0, 0]))

        compat = book1.compatible(book2)
        self.assertEqual(compat, True)
Example #3
0
 def test_fill(self):
     data = [1, 1, 1, 2, 2]
     bins = range(1, 4)
     b = ArtemisBook()
     b["book.one"] = Histogram1D(bins, stats={"sum": 0.0, "sum2": 0.0})
     b["book.two"] = Histogram1D(bins, stats={"sum": 0.0, "sum2": 0.0})
     b.fill("book", "one", data)
     b.fill("book", "two", data)
     self.assertEqual(b["book.one"].frequencies.tolist(), [3, 2])
     self.assertEqual(b["book.two"].frequencies.tolist(), [3, 2])
Example #4
0
    def test_eq_with_underflows(self):
        bins = [1.2, 1.4, 1.5, 1.7, 1.8]
        values = [4, 0, 3, 7.2]
        other1 = Histogram1D(bins, values, underflow=2)
        assert other1 != example

        other2 = Histogram1D(bins, values, overflow=1)
        assert other2 != example

        other3 = Histogram1D(bins, values, overflow=1, underflow=2)
        assert other3 == example
Example #5
0
 def test_subtract_correct_histogram(self):
     bins = [1.2, 1.4, 1.5, 1.7, 1.8]
     values = [1, 0, 0, 1]
     other = Histogram1D(bins, values)
     sum = example - other
     assert np.allclose(sum.bins, example.bins)
     assert np.allclose(sum.frequencies, [3, 0, 3, 6.2])
Example #6
0
 def test_add_correct_histogram(self):
     bins = [1.2, 1.4, 1.5, 1.7, 1.8]
     values = [1, 1, 0, 1]
     other = Histogram1D(bins, values)
     sum = example + other
     assert np.allclose(sum.bins, example.bins)
     assert np.allclose(sum.frequencies, [5, 1, 3, 8.2])
Example #7
0
 def test_copy_meta(self):
     bins = [1.2, 1.4, 1.5, 1.7, 1.8]
     values = [4, 0, 3, 7.2]
     errors2 = [1, 0, 4, 2.6]
     h1 = Histogram1D(bins, values, errors2, custom1="custom1", name="name")
     copy = h1.copy()
     assert h1.meta_data == copy.meta_data
Example #8
0
 def test_match(self):
     bins = range(-5, 5)
     h = Histogram1D(bins, stats={"sum": 0.0, "sum2": 0.0})
     outer = BaseBook()
     outer["one-a"] = h
     outer["one-b"] = h
     outer["one-c"] = h
     self.assertEqual(len(outer["one*"]), 3)
Example #9
0
    def test_fill_dtype(self):
        h = Histogram1D([[0, 1], [1, 2], [2, 3]], [1, 2, 3])
        assert h.dtype == np.int64
        assert np.allclose(h.frequencies, [1, 2, 3])

        h.fill(1.3, weight=2.2)
        # assert h.dtype == np.float
        assert np.allclose(h.frequencies, [1, 4.2, 3])
Example #10
0
    def test_msg(self):
        book1, book2 = ArtemisBook(), ArtemisBook()
        book1["a"] = Histogram1D(range(0, 4), stats={"sum": 0.0, "sum2": 0.0})
        book1["a"].fill_n(np.asarray([0, 0, 0]))
        book1["b"] = Histogram1D(range(0, 4), stats={"sum": 0.0, "sum2": 0.0})
        book1["b"].fill_n(np.asarray([0, 1, 1]))
        book1["d"] = Histogram1D(range(0, 4), stats={"sum": 0.0, "sum2": 0.0})
        book1["d"].fill_n(np.asarray([]))
        book2["a"] = Histogram1D(range(0, 4), stats={"sum": 0.0, "sum2": 0.0})
        book2["a"].fill_n(np.asarray([2, 2]))
        book2["b"] = Histogram1D(range(0, 4), stats={"sum": 0.0, "sum2": 0.0})
        book2["b"].fill_n(np.asarray([0, 3, 3, 3, 3]))
        book2["c"] = Histogram1D(range(0, 4), stats={"sum": 0.0, "sum2": 0.0})
        book2["c"].fill_n(np.asarray([0, 1, 2, 3]))

        book = book1 + book2
        self.assertEqual(book["a"].frequencies.tolist(), [3, 0, 2])
        self.assertEqual(book["b"].frequencies.tolist(), [2, 2, 4])
        self.assertEqual(book["d"].frequencies.tolist(), [0, 0, 0])
        self.assertEqual(book["c"].frequencies.tolist(), [1, 1, 2])

        msg = book._to_message()
        book3 = book._from_message(msg)
        # TODO
        # assertion fails in CI
        print(book == book3)
        # self.assertEqual(book, book3)
        self.assertEqual(book3["a"].frequencies.tolist(), [3, 0, 2])
        self.assertEqual(book3["b"].frequencies.tolist(), [2, 2, 4])
        self.assertEqual(book3["d"].frequencies.tolist(), [0, 0, 0])
        self.assertEqual(book3["c"].frequencies.tolist(), [1, 1, 2])
Example #11
0
 def test_subtract_wrong_histograms(self):
     with self.assertRaises(RuntimeError):
         wrong_bins = [
             [],  # No bins
             [1.2, 1.5, 1.7, 1.8],  # Too few
             [1.2, 1.44, 1.5, 1.7, 1.8],  # Different
             [1.2, 1.4, 1.5, 1.7, 1.8, 1.0],  # Too many
         ]
         values = [1, 1, 0, 2.2, 3, 4, 4]
         for binset in wrong_bins:
             other = Histogram1D(binset, values[:len(binset) - 1])
             with self.assertRaises(RuntimeError):
                 example - other
Example #12
0
    def book(self, algname, name, bins, axis_name=None, timer=False):
        name_ = "."
        name_ = name_.join([algname, name])
        self.__logger.info("Booking %s", name_)
        # TODO
        # Explore more options for correctly initializing h1
        value = self._get(name)
        if value is not None:
            self.__logger.error("Histogram already exists %s", name_)
        else:
            try:
                h = Histogram1D(bins, stats={"sum": 0.0, "sum2": 0.0})
            except Exception:
                self.__logger.error("Physt fails to book")
                raise
            self[name_] = h

            if timer is True:
                self._timers[name_] = []

        if axis_name:
            self._get(name_).axis_name = axis_name
Example #13
0
    def rebook(self, excludes=[]):
        """
        Force reset of all histograms with a copy
        do NOT include copying data
        """

        self._rebooked = True
        for n, x in self:
            if n in excludes:
                continue
            timer = self._timers.get(n, None)
            if timer is None:
                bins = x.binning
            else:
                try:
                    bins = autobinning(timer)
                except IndexError:
                    self.__logger.warning("%s fails rebook, use original bins",
                                          n)
                    bins = x.binning

                del self._timers[n]
            self._set(n, Histogram1D(bins, stats={"sum": 0.0, "sum2": 0.0}))
Example #14
0
    def test_iadd(self):

        book1, book2 = BaseBook(), BaseBook()
        book1["a"] = Histogram1D(range(0, 4), stats={"sum": 0.0, "sum2": 0.0})
        book1["a"].fill_n(np.asarray([0, 0, 0]))
        book1["b"] = Histogram1D(range(0, 4), stats={"sum": 0.0, "sum2": 0.0})
        book1["b"].fill_n(np.asarray([0, 1, 1]))
        book1["d"] = Histogram1D(range(0, 4), stats={"sum": 0.0, "sum2": 0.0})
        book1["d"].fill_n(np.asarray([]))
        book2["a"] = Histogram1D(range(0, 4), stats={"sum": 0.0, "sum2": 0.0})
        book2["a"].fill_n(np.asarray([2, 2]))
        book2["b"] = Histogram1D(range(0, 4), stats={"sum": 0.0, "sum2": 0.0})
        book2["b"].fill_n(np.asarray([0, 3, 3, 3, 3]))
        book2["c"] = Histogram1D(range(0, 4), stats={"sum": 0.0, "sum2": 0.0})
        book2["c"].fill_n(np.asarray([0, 1, 2, 3]))
        book1 += book2
        self.assertEqual(book1["a"].frequencies.tolist(), [3, 0, 2])
        self.assertEqual(book1["b"].frequencies.tolist(), [2, 2, 4])
        self.assertEqual(book1["d"].frequencies.tolist(), [0, 0, 0])
        self.assertEqual(book1["c"].frequencies.tolist(), [1, 1, 2])
Example #15
0
 def test_get_set(self):
     book = BaseBook()
     book["a"] = Histogram1D(range(0, 4), stats={"sum": 0.0, "sum2": 0.0})
     self.assertEqual(isinstance(book["a"], HistogramBase), True)
Example #16
0
#   This module includes code from the Physt Project
#
#   (C) Jan Pipek, 2016-9, MIT licence
#   See https://github.com/janpipek/physt
import sys
import os

sys.path = [os.path.join(os.path.dirname(__file__), "..")] + sys.path
from artemis_externals.physt.histogram1d import Histogram1D
from artemis_externals.physt import h1
import numpy as np
import unittest

bins = [1.2, 1.4, 1.5, 1.7, 1.8]
values = [4, 0, 3, 7.2]
example = Histogram1D(bins, values, overflow=1, underflow=2)


class TestBins(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_nbins(self):
        assert example.bin_count == 4

    def test_edges(self):
        assert np.allclose(example.bin_left_edges, [1.2, 1.4, 1.5, 1.7])
        assert np.allclose(example.bin_right_edges, [1.4, 1.5, 1.7, 1.8])
Example #17
0
 def test_copy_with_errors(self):
     bins = [1.2, 1.4, 1.5, 1.7, 1.8]
     values = [4, 0, 3, 7.2]
     errors2 = [1, 0, 4, 2.6]
     h1 = Histogram1D(bins, values, errors2)
     assert h1.copy() == h1