def _1d_histogram_tester(binlowhighs, x, weights=1): h = Histogram(binlowhighs) h.fill(x, weights=weights) if np.isscalar(weights): ynp = np.histogram(x, h.edges[0])[0] else: ynp = np.histogram(x, h.edges[0], weights=weights)[0] assert_array_almost_equal(ynp, h.values) h.reset() h._always_use_fillnd = True h.fill(x, weights=weights) assert_array_almost_equal(ynp, h.values)
def _1d_histogram_tester(binlowhighs, x, weights=1): h = Histogram(binlowhighs) h.fill(x, weights=weights) if np.isscalar(weights): ynp = np.histogram(x, h.edges[0])[0] else: ynp = np.histogram(x, h.edges[0], weights=weights)[0] assert_array_almost_equal(ynp, h.values) h.reset() h._always_use_fillnd = True h.fill(x, weights=weights) assert_array_almost_equal(ynp, h.values)
def _2d_histogram_tester(binlowhighs, x, y, weights=1): h = Histogram(*binlowhighs) h.fill(x, y, weights=weights) if np.isscalar(weights): if np.isscalar(x): assert np.isscalar(y), 'If x is a scalar, y must be also' ynp = np.histogram2d([x], [y], bins=h.edges)[0] else: ynp = np.histogram2d(x, y, bins=h.edges)[0] else: ynp = np.histogram2d(x, y, bins=h.edges, weights=weights)[0] assert_array_almost_equal(ynp, h.values) h.reset() h._always_use_fillnd = True h.fill(x, y, weights=weights) assert_array_almost_equal(ynp, h.values)
if __name__ == "__main__": import timeit import numpy as np from skbeam.core.accumulators.histogram import Histogram h = Histogram((10, 0, 10.1), (7, 0, 7.1)) x = np.random.random(1000000) * 40 y = np.random.random(1000000) * 10 w = np.ones_like(x) xi = x.astype(int) xi = xi.astype(float) wi = np.ones_like(xi) gg = globals() def timethis(stmt): return np.mean(timeit.repeat(stmt, number=10, repeat=5, globals=gg)) def histfromzero(h, fncname, x, w): h.data[:] = 0 getattr(h, fncname)(x, w) return h.data.copy() print("Timing h.fill", timethis('h.fill(x, y, weights=w)')) h._always_use_fillnd = True print("Timing h.fill with _always_use_fillnd", timethis('h.fill(x, y, weights=w)'))
import timeit import time import numpy as np from skbeam.core.accumulators.histogram import Histogram h = Histogram((10, 0, 10.1), (7, 0, 7.1)); x = np.random.random(1000000)*40 y = np.random.random(1000000)*10 w = np.ones_like(x) xi = x.astype(int) xi = xi.astype(float) wi = np.ones_like(xi) gg = globals() def timethis(stmt): return np.mean(timeit.repeat(stmt, number=10, repeat=5, globals=gg)) def histfromzero(h, fncname, x, w): h.data[:] = 0 getattr(h, fncname)(x, w) return h.data.copy() print("Timing h.fill", timethis('h.fill(x, y, weights=w)')) h._always_use_fillnd = True print("Timing h.fill with _always_use_fillnd", timethis('h.fill(x, y, weights=w)'))