From c73e6093b565516bb5cb6bdfc4ced0a68cc8946a Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Wed, 20 Oct 2010 05:51:50 -0400 Subject: [PATCH] Add Histogram.calculate_bin_edges(). --- pysawsim/histogram.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) 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`. -- 2.26.2