Skip to content

Instantly share code, notes, and snippets.

@douglasdavis
Created January 10, 2019 21:30
Show Gist options
  • Save douglasdavis/e829c3ee6edc37b3adc473bfbdc075ea to your computer and use it in GitHub Desktop.
Save douglasdavis/e829c3ee6edc37b3adc473bfbdc075ea to your computer and use it in GitHub Desktop.
testing fast hist
import numpy as np
from fast_histogram import histogram1d
import math
def my_slow(x, xlim=(0,10), nbins=None, weights=None, overflow=False, density=False):
if nbins is None:
nbins = int(math.pow(len(x), 0.33333))
if overflow and weights is not None:
under = x < xlim[0]
over = x > xlim[1]
wunder = np.sum(weights[under])
wover = np.sum(weights[over])
elif overflow:
under = x < xlim[0]
over = x > xlim[1]
wunder = under.sum()
wover = over.sum()
else:
pass
binning = np.linspace(xlim[0], xlim[1], nbins+1)
h = np.histogram(x, bins=binning, weights=weights, density=density)
if overflow:
h[0][0] += wunder
h[0][-1] += wover
return h
def my_fast(x, xlim=(0,10), nbins=None, weights=None, overflow=False):
if nbins is None:
nbins = int(math.pow(len(x), 0.33333))
if overflow and weights is not None:
under = x < xlim[0]
over = x > xlim[1]
wunder = np.sum(weights[under])
wover = np.sum(weights[over])
elif overflow:
under = x < xlim[0]
over = x > xlim[1]
wunder = under.sum()
wover = over.sum()
else:
pass
h = histogram1d(x, nbins, range=[xlim[0], xlim[1]], weights=weights)
if overflow:
h[0] += wunder
h[-1] += wover
return h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment