1
2 """
3 This module contains code to generate ascii representations
4 of statistical objects
5 """
6 from __future__ import division
7 from numpy import histogram, ceil
8 __author__="fccoelho"
9 __date__ ="$12/10/2009 14:25:05$"
10 __licence__="GPL v3"
11 __docformat__ = "restructuredtext en"
12
13
14
16 """
17 Ascii histogram
18 """
19 - def __init__(self, data, bins=10, rnge=None):
20 """
21 Class constructor
22
23 :Parameters:
24 - `data`: array like object
25 - `bins`: int or sequence of scalars, optional. If `bins` is an int, it defines the number of equal-width bins in the given range (10, by default). If `bins` is a sequence, it defines the bin edges, including the rightmost edge, allowing for non-uniform bin widths.
26 - `rnge`: (float, float), optional. The lower and upper range of the bins. If not provided, range is simply ``(a.min(), a.max())``. Values outside the range are ignored. Note that with `new` set to False, values below the range are ignored, while those above the range are tallied in the rightmost bin.
27 """
28 self.data = data
29 self.bins = bins
30 self.rnge = rnge
31 self.h = histogram(self.data, bins=self.bins, range=self.rnge)
33 """Returns a multiline string containing a
34 a horizontal histogram representation of self.data
35
36 :Parameters:
37 - `height`: Height of the histogram in characters
38 - `character`: Character to use
39
40 >>> d = normal(size=1000)
41 >>> h = Histogram(d,bins=25)
42 >>> print h.horizontal(5,'|')
43 106 |||
44 |||||
45 |||||||
46 ||||||||||
47 |||||||||||||
48 -3.42 3.09
49 """
50 his = """"""
51 bars = self.h[0]/max(self.h[0])*height
52 for l in reversed(range(1,height+1)):
53 line = ""
54 if l == height:
55 line = '%s '%max(self.h[0])
56 else:
57 line = ' '*(len(str(max(self.h[0])))+1)
58 for c in bars:
59 if c >= ceil(l):
60 line += character
61 else:
62 line += ' '
63 line +='\n'
64 his += line
65 his += '%.2f'%self.h[1][0] + ' '*(self.bins) +'%.2f'%self.h[1][-1] + '\n'
66 return his
67 - def vertical(self,height=20, character ='|'):
68 """
69 Returns a Multi-line string containing a
70 a vertical histogram representation of self.data
71
72 :Parameters:
73 - `height`: Height of the histogram in characters
74 - `character`: Character to use
75
76 >>> d = normal(size=1000)
77 >>> Histogram(d,bins=10)
78 >>> print h.vertical(15,'*')
79 236
80 -3.42:
81 -2.78:
82 -2.14: ***
83 -1.51: *********
84 -0.87: *************
85 -0.23: ***************
86 0.41 : ***********
87 1.04 : ********
88 1.68 : *
89 2.32 :
90 """
91 his = """"""
92 xl = ['%.2f'%n for n in self.h[1]]
93 lxl = [len(l) for l in xl]
94 bars = self.h[0]/max(self.h[0])*height
95 his += ' '*(max(bars)+2+max(lxl))+'%s\n'%max(self.h[0])
96 for i,c in enumerate(bars):
97 line = xl[i] +' '*(max(lxl)-lxl[i])+': '+ character*c+'\n'
98 his += line
99 return his
100
101
102
103 if __name__ == "__main__":
104 from numpy.random import normal, seed
105 seed(1)
106 d = normal(size=1000)
107 h = Histogram(d,bins=10, rnge=(0, 1))
108 print h.vertical(15)
109 print h.horizontal(5)
110