Exemple #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
Exemple #2
0
def _add_stats_box(h1: Histogram1D, ax: Axes, stats: Union[str, bool] = "all"):
    """Insert a small legend-like box with statistical information.

    Parameters
    ----------
    stats : "all" | "total" | True
        What info to display

    Note
    ----
    Very basic implementation.
    """

    # place a text box in upper left in axes coords
    if stats in ["all", True]:
        text = "Total: {0}\nMean: {1:.2f}\nStd.dev: {2:.2f}".format(
            h1.total, h1.mean(), h1.std())
    elif stats == "total":
        text = "Total: {0}".format(h1.total)
    else:
        raise ValueError("Invalid stats specification")

    ax.text(0.05,
            0.95,
            text,
            transform=ax.transAxes,
            verticalalignment='top',
            horizontalalignment='left')
Exemple #3
0
 def test_json_write_collection(self):
     bins = [1.2, 1.4, 1.5, 1.7, 1.8 ]
     values1 = [4, 0, 3, 7.2]
     values2 = [14, 10, 13, 17.2]
     col = HistogramCollection()
     col.add(Histogram1D(bins, values1))
     col.add(Histogram1D(bins, values2))
     json = col.to_json()
     read = io.parse_json(json)
     assert read == col
Exemple #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
Exemple #5
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
Exemple #6
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])
Exemple #7
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])
Exemple #8
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])
Exemple #9
0
def fist() -> Histogram1D:
    """A simple histogram in the shape of a fist."""
    import numpy as np
    from ..histogram1d import Histogram1D
    widths = [0, 1.2, 0.2, 1, 0.1, 1, 0.1, 0.9, 0.1, 0.8]
    edges = np.cumsum(widths)
    heights = np.asarray([4, 1, 7.5, 6, 7.6, 6, 7.5, 6, 7.2]) + 5
    return Histogram1D(edges, heights, axis_name="Is this a fist?", title="Physt \"logo\"")
Exemple #10
0
 def test_subtract_wrong_histograms(self):
     with pytest.raises(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.]  # Too many
         ]
         values = [1, 1, 0, 2.2, 3, 4, 4]
         for binset in wrong_bins:
             other = Histogram1D(binset, values[:len(binset) - 1])
             with pytest.raises(RuntimeError):
                 example - other
Exemple #11
0
 def test_json_write_string(self):
     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)
     output = io.save_json(example)
Exemple #12
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
Exemple #13
0
import sys
import os
sys.path = [os.path.join(os.path.dirname(__file__), "..")] + sys.path
from physt.histogram1d import Histogram1D
from physt import h1
import numpy as np
import pytest

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:
    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])
        assert np.allclose(example.bin_centers, [1.3, 1.45, 1.6, 1.75])

    def test_numpy_bins(self):
        assert np.allclose(example.numpy_bins, [1.2, 1.4, 1.5, 1.7, 1.8])

    def test_widths(self):
        assert np.allclose(example.bin_widths, [0.2, 0.1, 0.2, 0.1])


class TestValues:
    def test_values(self):