Ensure that printed histograms always have at least two bins listed.
authorW. Trevor King <wking@tremily.us>
Fri, 18 May 2012 17:00:54 +0000 (13:00 -0400)
committerW. Trevor King <wking@tremily.us>
Fri, 18 May 2012 17:00:54 +0000 (13:00 -0400)
Otherwise it is impossible to calculate the bin width from the printed
histogram.

pysawsim/histogram.py

index cdb0bbf086120789595bf4061969d3f1fd73b814..6c326d2ef6cb53e6efd83fffbf338dfff565f860 100644 (file)
@@ -151,11 +151,21 @@ class Histogram (object):
         1.5e-10\t10
         2e-10\t40
         2.5e-10\t5
+        >>> h.bin_edges = h.bin_edges[:2]
+        >>> h.counts = h.counts[:1]
+        >>> h.to_stream(sys.stdout)
+        ... # doctest: +NORMALIZE_WHITESPACE, +REPORT_UDIFF
+        #Force (N)\tUnfolding events
+        1.5e-10\t10
+        2e-10\t0
         """
         if self.headings != None:
             stream.write('#%s\n' % '\t'.join(self.headings))
         for bin,count in zip(self.bin_edges, self.counts):
             stream.write('%g\t%g\n' % (bin, count))
+        if len(self.counts) == 1:
+            # print an extra line so that readers can determine bin width
+            stream.write('%g\t0\n' % (self.bin_edges[-1]))
 
     def analyze(self):
         """Analyze `.counts` and `.bin_edges` if the raw data is missing.