Split Histogram.analyze() out of Histogram.from_stream().
authorW. Trevor King <wking@drexel.edu>
Fri, 5 Nov 2010 17:00:35 +0000 (13:00 -0400)
committerW. Trevor King <wking@drexel.edu>
Fri, 5 Nov 2010 17:00:35 +0000 (13:00 -0400)
pysawsim/histogram.py

index bb66c05f2bb0c3eebd74cb40b67ae1bafa21ebfb..1b743eebed8c7053e37db477369513db27b3ac5f 100644 (file)
@@ -125,18 +125,7 @@ class Histogram (object):
             self.counts.append(float(count))
         bin_width = self.bin_edges[1] - self.bin_edges[0]
         self.bin_edges.append(self.bin_edges[-1]+bin_width)
-        self.total = float(sum(self.counts))
-        self.mean = 0
-        for bin,count in zip(self.bin_edges, self.counts):
-            bin += bin_width / 2.0
-            self.mean += bin * count
-        self.mean /=  float(self.total)
-        variance = 0
-        for bin,count in zip(self.bin_edges, self.counts):
-            bin += bin_width / 2.0
-            variance += (bin - self.mean)**2 * count
-        self.std_dev = numpy.sqrt(variance)
-        self.normalize()
+        self.analyze()
 
     def to_stream(self, stream):
         """Write to `stream` with the same format as `from_stream()`.
@@ -158,7 +147,29 @@ class Histogram (object):
         for bin,count in zip(self.bin_edges, self.counts):
             stream.write('%g\t%g\n' % (bin, count))
 
+    def analyze(self):
+        """Analyze `.counts` and `.bin_edges` if the raw data is missing.
+
+        Generate `.total`, `.mean`, and `.std_dev`, and run
+        `.normalize()`.
+        """
+        bin_width = self.bin_edges[1] - self.bin_edges[0]
+        self.total = float(sum(self.counts))
+        self.mean = 0
+        for bin,count in zip(self.bin_edges, self.counts):
+            bin += bin_width / 2.0
+            self.mean += bin * count
+        self.mean /=  float(self.total)
+        variance = 0
+        for bin,count in zip(self.bin_edges, self.counts):
+            bin += bin_width / 2.0
+            variance += (bin - self.mean)**2 * count
+        self.std_dev = numpy.sqrt(variance)
+        self.normalize()
+
     def normalize(self):
+        """Generate `.probabilities` from `.total` and `.counts`.
+        """
         self.total = float(self.total)
         self.probabilities = [count/self.total for count in self.counts]