|
| 1 | +#!/usr/bin/python |
| 2 | +import math |
| 3 | +import sys, os |
| 4 | +from bashplotlib.utils import helpers |
| 5 | +import collections |
| 6 | + |
| 7 | +def calc_bins(n, min_val, max_val, h=None): |
| 8 | + "calculate number of bins for the histogram" |
| 9 | + if not h: |
| 10 | + h = max(10, math.log(n + 1, 2)) |
| 11 | + bin_width = (max_val - min_val) / h |
| 12 | + for b in helpers.drange(min_val, max_val, bin_width): |
| 13 | + yield b |
| 14 | + |
| 15 | +def read_numbers(numbers): |
| 16 | + "read input optimally; skip NA values. Takes a list() or a file." |
| 17 | + if not numbers: |
| 18 | + numbers = [] |
| 19 | + if isinstance(numbers, basestring): |
| 20 | + try: |
| 21 | + # read numbers from file |
| 22 | + # ignore empty rows |
| 23 | + numbers = [line for line in open(numbers, 'r') if line.strip()] |
| 24 | + except Exception, err: |
| 25 | + pass |
| 26 | + if isinstance(numbers, collections.Iterable): |
| 27 | + for number in numbers: |
| 28 | + number = helpers.try_cast_str_to_number(number) |
| 29 | + if number: |
| 30 | + yield number |
| 31 | + |
| 32 | + |
| 33 | +def plot_hist(data, height=20.0, bincount=None, pch="o", colour="white", title="", xlab=None, showSummary=False): |
| 34 | + """make a histogram for continuous variable. |
| 35 | +
|
| 36 | + Arguments: |
| 37 | + data: List of numbers or file with numbers |
| 38 | + height: The height of the histogram in # of lines |
| 39 | + bincount: Number of bins in the histogram |
| 40 | + pch: Shape of the bars in the plot |
| 41 | + colour: Colour of the bars in the terminal |
| 42 | + title: Title at the top of the plot |
| 43 | + xlab: Boolen value for whether or not to display x-axis labels |
| 44 | + showSummary: Boolean value for whether or not to display a summary |
| 45 | + """ |
| 46 | + if pch is None: |
| 47 | + pch = "o" |
| 48 | + colour = helpers.get_colour(colour) |
| 49 | + min_val, max_val = None, None |
| 50 | + n, mean = 0., 0. |
| 51 | + for number in read_numbers(data): |
| 52 | + n += 1 |
| 53 | + |
| 54 | + if not min_val or number < min_val: |
| 55 | + min_val = number |
| 56 | + if not max_val or number > max_val: |
| 57 | + max_val = number |
| 58 | + mean += number |
| 59 | + mean /= n |
| 60 | + |
| 61 | + bins = list(calc_bins(n, min_val, max_val, bincount)) |
| 62 | + hist = {} |
| 63 | + for i in range(len(bins)): |
| 64 | + hist[i] = 0 |
| 65 | + for number in read_numbers(data): |
| 66 | + for i, b in enumerate(bins): |
| 67 | + if number < b: |
| 68 | + hist[i] += 1 |
| 69 | + break |
| 70 | + |
| 71 | + min_y, max_y = min(hist.values()), max(hist.values()) |
| 72 | + |
| 73 | + ys = list(helpers.drange(min_y, max_y, (max_y-min_y)/height)) |
| 74 | + ys.reverse() |
| 75 | + |
| 76 | + nlen = max(len(str(min_y)), len(str(max_y))) + 1 |
| 77 | + |
| 78 | + if title: |
| 79 | + print helpers.box_text(title, len(hist)*2, nlen) |
| 80 | + print |
| 81 | + used_labs = set() |
| 82 | + for y in ys: |
| 83 | + ylab = str(int(y)) |
| 84 | + if ylab in used_labs: |
| 85 | + ylab = "" |
| 86 | + else: |
| 87 | + used_labs.add(ylab) |
| 88 | + ylab = " "*(nlen - len(ylab)) + ylab + "|" |
| 89 | + |
| 90 | + print ylab, |
| 91 | + |
| 92 | + for i in range(len(hist)): |
| 93 | + if y < hist[i]: |
| 94 | + helpers.printcolor(pch, True, colour) |
| 95 | + else: |
| 96 | + helpers.printcolor(" ", True, colour) |
| 97 | + print |
| 98 | + xs = hist.keys() * 2 |
| 99 | + |
| 100 | + print " "*(nlen+1) + "-"*len(xs) |
| 101 | + |
| 102 | + |
| 103 | + if xlab: |
| 104 | + for i in range(0, nlen): |
| 105 | + helpers.printcolor(" "*(nlen+1), True, colour) |
| 106 | + for x in range(0, len(hist)): |
| 107 | + num = str(bins[x]) |
| 108 | + if x%2==0: |
| 109 | + print " ", |
| 110 | + elif i < len(num): |
| 111 | + print num[i], |
| 112 | + print |
| 113 | + center = max(map(len, map(str, [n, min_val, mean, max_val]))) |
| 114 | + center += 15 |
| 115 | + |
| 116 | + if showSummary: |
| 117 | + print |
| 118 | + print "-"*(2 + center) |
| 119 | + print "|" + "Summary".center(center) + "|" |
| 120 | + print "-"*(2 + center) |
| 121 | + summary = "|" + ("observations: %d" % n).center(center) + "|\n" |
| 122 | + summary += "|" + ("min value: %f" % min_val).center(center) + "|\n" |
| 123 | + summary += "|" + ("mean : %f" % mean).center(center) + "|\n" |
| 124 | + summary += "|" + ("max value: %f" % max_val).center(center) + "|\n" |
| 125 | + summary += "-"*(2 + center) |
| 126 | + print summary |
0 commit comments