From: W. Trevor King Date: Wed, 20 Oct 2010 09:51:50 +0000 (-0400) Subject: Add Histogram.calculate_bin_edges(). X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=c73e6093b565516bb5cb6bdfc4ced0a68cc8946a;p=sawsim.git Add Histogram.calculate_bin_edges(). --- diff --git a/pysawsim/histogram.py b/pysawsim/histogram.py index 9f2f65d..880fd40 100644 --- a/pysawsim/histogram.py +++ b/pysawsim/histogram.py @@ -28,6 +28,25 @@ class Histogram (object): >>> h = Histogram() """ + def calculate_bin_edges(self, data, bin_width): + """ + >>> h = Histogram() + >>> h.calculate_bin_edges(numpy.array([-7.5, 18.2, 4]), 10) + array([-10., 0., 10., 20.]) + >>> h.calculate_bin_edges(numpy.array([-7.5, 18.2, 4, 20]), 10) + array([-10., 0., 10., 20.]) + >>> h.calculate_bin_edges(numpy.array([0, 18.2, 4, 20]), 10) + array([ 0., 10., 20.]) + >>> h.calculate_bin_edges(numpy.array([18.2, 4, 20]), 10) + array([ 0., 10., 20.]) + >>> h.calculate_bin_edges(numpy.array([18.2, 20]), 10) + array([ 10., 20.]) + """ + m = data.min() + M = data.max() + bin_start = m - m % bin_width + return numpy.arange(bin_start, M+bin_width, bin_width, dtype=data.dtype) + def from_data(self, data, bin_edges): """Initialize from `data`.