예제 #1
0
def test_count_out_of_range():
    bins = [np.linspace(0., 5., 6)]
    label = ['Random distribution']
    scale = ['linear']
    title = 'Test_histogram'
    data = np.array(
        [-1., 2.2, 3.2, 4.5, 6.3, 7.1, 4.9, 3.1, 0.2, 2.1, 2.2, 1.1])
    test_data = [1, 1, 3, 2, 2]
    test_out_range = [[1], [2]]

    test_histo = Histogram(title, bins, label, scale)
    out_range = test_histo.count_out_of_range(np.array(data, ndmin=2))

    assert np.allclose(out_range, test_out_range)
예제 #2
0
def test_join_histo_managers(histogram_list):
    args, list_of_histograms = histogram_list
    histogram_manager = HistoManager(list_of_histograms)
    joined_histogram_manager = histf.join_histo_managers(
        histogram_manager, histogram_manager)

    assert len(list_of_histograms) == len(joined_histogram_manager.histos)
    for histoname, histogram in joined_histogram_manager.histos.items():
        histo1 = histogram_manager[histoname]
        true_histogram = Histogram(histoname, histo1.bins, histo1.labels,
                                   histo1.scale)
        true_histogram.data = 2 * histo1.data
        true_histogram.errors = np.sqrt(2) * histo1.errors
        true_histogram.out_range = 2 * histo1.out_range
        assert_histogram_equality(histogram, true_histogram)
예제 #3
0
def test_histogram_initialization_with_values(bins):
    label = ['Random distribution']
    title = 'Test_histogram'
    scale = ['linear']
    data = []
    out_range = []
    for ibin in bins:
        lower_limit = ibin[0] * 0.95
        upper_limit = ibin[-1] * 1.05
        data.append(np.random.uniform(lower_limit, upper_limit, 500))
        out_range.append(
            np.array([
                np.count_nonzero(data[-1] < ibin[0]),
                np.count_nonzero(data[-1] > ibin[-1])
            ]))

    data = np.array(data)
    out_range = np.array(out_range).T
    binned_data = np.histogramdd(data.T, bins)[0]
    test_histo = Histogram(title, bins, label, scale, data)

    assert np.all(a == b for a, b in zip(test_histo.bins, bins))
    assert np.allclose(test_histo.data, binned_data)
    assert np.allclose(test_histo.out_range, out_range)
    assert np.allclose(test_histo.errors, np.sqrt(binned_data))
    assert test_histo.labels == label
    assert test_histo.title == title
    assert test_histo.scale == scale
예제 #4
0
def create_histomanager_from_dicts(histobins_dict,
                                   histolabels_dict,
                                   histoscales_dict,
                                   init_fill_dict=None):
    """Creates and returns a HistoManager from a dict of bins
    and a given of labels with identical keys.

    Parameters
    ----------
    histobins_dict   : dict
    Dictionary with keys equal to histogram names
    and values equal to the binning.
    histolabels_dict : dict
    Dictionary with keys equal to histogram names 
    and values equal to the axis labels.
    histoscales_dict : dict
    Dictionary with keys equal to histogram names 
    and values equal to the y axis scale.
    init_fill_dict   : dict, optional
    Dictionary with keys equal to histogram names 
    and values equal to an initial filling.
    """
    histo_manager = HistoManager()
    if init_fill_dict is None:
        init_fill_dict = dict()

    for title, bins in histobins_dict.items():
        histo_manager.new_histogram(Histogram(title, bins,
                                              histolabels_dict [title],
                                              histoscales_dict [title],
                                              init_fill_dict.get(title, None)))
    return histo_manager
예제 #5
0
def test_join_histo_managers_with_different_histograms(histogram_list1,
                                                       histogram_list2):
    _, list_of_histograms1 = histogram_list1
    _, list_of_histograms2 = histogram_list2
    histogram_manager1 = HistoManager(list_of_histograms1)
    histogram_manager2 = HistoManager(list_of_histograms2)
    joined_histogram_manager = histf.join_histo_managers(
        histogram_manager1, histogram_manager2)

    unique_histograms = set(histogram_manager1.histos) | set(
        histogram_manager2.histos)
    common_histograms = set(histogram_manager1.histos) & set(
        histogram_manager2.histos)

    remove_names = []
    for name in unique_histograms:
        if name in common_histograms:
            if not np.all(a == b
                          for a, b in zip(histogram_manager1[name].bins,
                                          histogram_manager2[name].bins)):
                remove_names.append(name)
    list_of_names = unique_histograms - set(remove_names)

    for histoname, histogram in joined_histogram_manager.histos.items():
        assert histoname in list_of_names
        if (histoname in histogram_manager1.histos) and (
                histoname in histogram_manager2.histos):
            histo1 = histogram_manager1[histoname]
            histo2 = histogram_manager2[histoname]

            true_histogram = Histogram(histoname, histo1.bins, histo1.labels,
                                       histo1.scale)
            true_histogram.data = histo1.data + histo2.data
            true_histogram.errors = np.sqrt(histo1.errors**2 +
                                            histo2.errors**2)
            true_histogram.out_range = histo1.out_range + histo2.out_range

            assert_histogram_equality(histogram, true_histogram)

        elif histoname in histogram_manager1.histos:
            histo1 = histogram_manager1[histoname]
            assert_histogram_equality(histogram, histo1)

        elif histoname in histogram_manager2.histos:
            histo2 = histogram_manager2[histoname]
            assert_histogram_equality(histogram, histo2)
예제 #6
0
def test_bin_data():
    bins = [np.linspace(0., 5., 6)]
    label = ['Random distribution']
    scale = ['linear']
    title = 'Test_histogram'
    data = np.array(
        [-1., 2.2, 3.2, 4.5, 6.3, 7.1, 4.9, 3.1, 0.2, 2.1, 2.2, 1.1])
    test_data = [1, 1, 3, 2, 2]
    test_out_range = [[1], [2]]

    test_histo = Histogram(title, bins, label, scale)
    binned_data, out_range = test_histo.bin_data(data,
                                                 data_weights=np.ones(
                                                     len(data)))

    assert np.allclose(binned_data, test_data)
    assert np.allclose(out_range, test_out_range)
예제 #7
0
파일: hist_io.py 프로젝트: gondiaz/Olivia
def get_histograms_from_file(file_input, group_name='HIST'):
    histo_manager = HistoManager()

    def name_selection(x):
        selection = (('bins' not in x) and ('labels' not in x)
                     and ('errors' not in x) and ('outRange' not in x)
                     and ('scales' not in x))
        return selection

    with tb.open_file(file_input, "r") as h5in:
        histogram_list = []
        group = getattr(h5in.root, group_name)
        for histoname in filter(name_selection, group._v_children):
            entries = np.array(getattr(group, histoname)[:])
            bins = getattr(group, histoname + '_bins')[:]
            out_range = getattr(group, histoname + '_outRange')[:]
            errors = np.array(getattr(group, histoname + '_errors')[:])
            labels = getattr(group, histoname + '_labels')[:]
            labels = [str(lab)[2:-1].replace('\\\\', '\\') for lab in labels]
            try:
                scale = getattr(group, histoname + '_scales')[:]
                scale = [str(scl)[2:-1].replace('\\\\', '\\') for scl in scale]
            except tb.NoSuchNodeError:
                scale = ["linear"]

            histogram = Histogram(histoname, bins, labels, scale)
            histogram.data = entries
            histogram.out_range = out_range
            histogram.errors = errors
            histogram.scale = scale

            histogram_list.append(histogram)

    return HistoManager(histogram_list)
예제 #8
0
def empty_histograms(draw, dimension=0):
    if dimension <= 0:
        dimension = draw(sampled_from((1, 2)))
    bins = draw(bins_arrays(dimension=dimension))
    labels = draw(
        lists(text(characters, min_size=5),
              min_size=dimension,
              max_size=dimension))
    scales = draw(
        lists(text(characters, min_size=5),
              min_size=dimension,
              max_size=dimension))
    args = draw(titles()), bins, labels, scales
    return args, Histogram(*args)
예제 #9
0
def test_histogram_initialization(bins):
    label = ['Random distribution']
    title = 'Test_histogram'
    scale = ['linear']
    test_histo = Histogram(title, bins, label, scale)

    assert np.all(a == b for a, b in zip(test_histo.bins, bins))
    assert np.allclose(test_histo.data,
                       np.zeros(shape=tuple(len(x) - 1 for x in bins)))
    assert np.allclose(test_histo.errors,
                       np.zeros(shape=tuple(len(x) - 1 for x in bins)))
    assert np.allclose(test_histo.out_range, np.zeros(shape=(2, len(bins))))
    assert test_histo.labels == label
    assert test_histo.title == title
    assert test_histo.scale == scale
예제 #10
0
def test_create_histomanager_from_dicts(bins):
    histobins_dict = {}
    histolabels_dict = {}
    histoscales_dict = {}
    histograms_dict = {}
    for i, bins_element in enumerate(bins):
        title = f"Histo_{i}"
        labels = [f"Xlabel_{i}", f"Ylabel_{i}"]
        scales = ["linear"]
        histobins_dict[title] = bins_element
        histolabels_dict[title] = labels
        histoscales_dict[title] = scales
        histograms_dict[title] = Histogram(title, bins_element, labels, scales)

    histo_manager = histf.create_histomanager_from_dicts(
        histobins_dict, histolabels_dict, histoscales_dict)

    assert len(histograms_dict) == len(histo_manager.histos)
    for histoname, histogram in histo_manager.histos.items():
        assert_histogram_equality(histogram, histograms_dict[histoname])
예제 #11
0
def filled_histograms(draw, dimension=0, fixed_bins=None):
    if fixed_bins is not None:
        dimension = len(fixed_bins)
    if dimension <= 0:
        dimension = draw(sampled_from((1, 2)))

    if fixed_bins is not None:
        bins = [
            np.linspace(bin_range[0], bin_range[1], bin_range[2] + 1)
            for bin_range in fixed_bins
        ]
    else:
        bins = draw(bins_arrays(dimension=dimension))

    labels = draw(
        lists(text(characters, min_size=5),
              min_size=dimension,
              max_size=dimension))
    scales = draw(
        lists(text(characters, min_size=5),
              min_size=dimension,
              max_size=dimension))
    shape = draw(integers(50, 100)),
    data = []
    for i in range(dimension):
        lower_limit = bins[i][0] - draw(
            floats(0.5, 1e8, allow_nan=False, allow_infinity=False))
        upper_limit = bins[i][-1] + draw(
            floats(0.5, 1e8, allow_nan=False, allow_infinity=False))
        data.append(
            draw(
                arrays(
                    float, shape,
                    floats(lower_limit,
                           upper_limit,
                           allow_nan=False,
                           allow_infinity=False))))
    data = np.array(data)
    args = draw(titles()), bins, labels, scales, data
    return args, Histogram(*args)