Add Histogram.calculate_bin_edges().
authorW. Trevor King <wking@drexel.edu>
Wed, 20 Oct 2010 09:51:50 +0000 (05:51 -0400)
committerW. Trevor King <wking@drexel.edu>
Wed, 20 Oct 2010 09:51:50 +0000 (05:51 -0400)
pysawsim/histogram.py

index 9f2f65d8f020d8ffa5b8a2dc90fe2738f8834259..880fd409ec4f8c1b487cf0b8eba2487f82cc6b8c 100644 (file)
@@ -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`.