# Path: kvarq/util.py
# class TextHist:
# 
#     ''' outputs a text histogram in the form of::
# 
#             [  0-100]  33(10%)********
#             [100-200] 185(50%)****************************************
#             [200-300]  66(20%)****************
#             [300-400]  40(15%)************
#             [400-500]  40(15%)************
#             [500-600]   0( 0%)
# 
#             totaling 330, average 172.35 '''
# 
#     def __init__(self, bins=15, width=65, title=None):
#         self.bins = bins
#         self.width = width
#         self.title = title
# 
# 
#     def draw(self, data, indexed=False):
#         ''' data is already sorted
# 
#               - indexed=``False`` : data is array containing actual values
#               - indexed=``True`` : each entry in data represents number of
#                 occurences of its index '''
# 
#         if not data:
#             return 'no data --> CANNOT GENERATE HISTOGRAM'
#         if indexed:
#             bw = len(data)/float(self.bins)
#             N = int(log(len(data))/log(10)) +1
#         else:
#             if data[-1]==0:
#                 return 'all data zero --> CANNOT GENERATE HISTOGRAM'
#             bw = (data[-1]-data[0])/float(self.bins)
#             N = int(log(data[-1])/log(10)) +1
#         if not bw:
#             return 'bw=0 --> CANNOT GENERATE HISTOGRAM'
#         n = int(log(bw)/log(10)) -1
#         bw = int(bw/10**n) * 10**n
#         bw = max(bw, 1.)
# 
#         xs = []
#         i = bi = x = mx = sx = s = 0
#         while i<len(data):
#             if not indexed and (data[i]>(bi+1)*bw) or \
#                     indexed and (i>(bi+1)*bw):
#                 xs.append(x)
#                 sx += x
#                 if x > mx: mx = x
#                 x = 0
#                 bi += 1
#             else:
#                 if indexed:
#                     x += data[i]
#                     s += data[i]*i
#                 else:
#                     x += 1
#                     s += data[i]
#                 i += 1
#         if x:
#             xs.append(x)
#             sx += x
#             if x > mx: mx = x
# 
#         fmt = '[%%%dd-%%%dd] %%%dd (%%2d%%%%)'%(
#                 max(N,4), max(N,4), int(log(max(1, mx))/log(10)) +1)
#         ret = ''
#         if self.title:
#             ret += self.title + '\n' + '-'*(len(self.title)) + '\n'
#         for bi, x in enumerate(xs):
#             ret += fmt%(bi*bw, (bi+1)*bw, x, int(100*x/sx))
#             ret += '*'*int(self.width*x/mx) + '\n'
# 
#         if indexed:
#             avg = float(s)/sum(data)
#         else:
#             avg = float(s)/len(data)
#         ret += 'totaling %d, average %.2f'%(mx, avg)
# 
#         return ret


import unittest
from kvarq.util import TextHist




class UtilTest(unittest.TestCase):

    def test_texthist(self):
        # min bin width is 1
        data = [0, 0, 1]
        hist = TextHist().draw(sorted(data), indexed=False)
        assert '100%' in hist
        data = [0, 0, 2]

# Path: kvarq/util.py
# class TextHist:
# 
#     ''' outputs a text histogram in the form of::
# 
#             [  0-100]  33(10%)********
#             [100-200] 185(50%)****************************************
#             [200-300]  66(20%)****************
#             [300-400]  40(15%)************
#             [400-500]  40(15%)************
#             [500-600]   0( 0%)
# 
#             totaling 330, average 172.35 '''
# 
#     def __init__(self, bins=15, width=65, title=None):
#         self.bins = bins
#         self.width = width
#         self.title = title
# 
# 
#     def draw(self, data, indexed=False):
#         ''' data is already sorted
# 
#               - indexed=``False`` : data is array containing actual values
#               - indexed=``True`` : each entry in data represents number of
#                 occurences of its index '''
# 
#         if not data:
#             return 'no data --> CANNOT GENERATE HISTOGRAM'
#         if indexed:
#             bw = len(data)/float(self.bins)
#             N = int(log(len(data))/log(10)) +1
#         else:
#             if data[-1]==0:
#                 return 'all data zero --> CANNOT GENERATE HISTOGRAM'
#             bw = (data[-1]-data[0])/float(self.bins)
#             N = int(log(data[-1])/log(10)) +1
#         if not bw:
#             return 'bw=0 --> CANNOT GENERATE HISTOGRAM'
#         n = int(log(bw)/log(10)) -1
#         bw = int(bw/10**n) * 10**n
#         bw = max(bw, 1.)
# 
#         xs = []
#         i = bi = x = mx = sx = s = 0
#         while i<len(data):
#             if not indexed and (data[i]>(bi+1)*bw) or \
#                     indexed and (i>(bi+1)*bw):
#                 xs.append(x)
#                 sx += x
#                 if x > mx: mx = x
#                 x = 0
#                 bi += 1
#             else:
#                 if indexed:
#                     x += data[i]
#                     s += data[i]*i
#                 else:
#                     x += 1
#                     s += data[i]
#                 i += 1
#         if x:
#             xs.append(x)
#             sx += x
#             if x > mx: mx = x
# 
#         fmt = '[%%%dd-%%%dd] %%%dd (%%2d%%%%)'%(
#                 max(N,4), max(N,4), int(log(max(1, mx))/log(10)) +1)
#         ret = ''
#         if self.title:
#             ret += self.title + '\n' + '-'*(len(self.title)) + '\n'
#         for bi, x in enumerate(xs):
#             ret += fmt%(bi*bw, (bi+1)*bw, x, int(100*x/sx))
#             ret += '*'*int(self.width*x/mx) + '\n'
# 
#         if indexed:
#             avg = float(s)/sum(data)
#         else:
#             avg = float(s)/len(data)
#         ret += 'totaling %d, average %.2f'%(mx, avg)
# 
#         return ret


# Path: tests/test_util.py
import unittest
from kvarq.util import TextHist





class UtilTest(unittest.TestCase):

    def test_texthist(self):
        # min bin width is 1
        data = [0, 0, 1]
        hist = TextHist().draw(sorted(data), indexed=False)
        assert '100%' in hist
        data = [0, 0, 2]

        hist = TextHist().draw(sorted(data), indexed=False)