def graph_pts(table, percent=True, axes=True, zeroes=True, exact=False): '''returns graph pts for a table. axes=True returns (x_axis, y_axis). axes=False returns [(x,y), (x,y)...] zeroes includes zero freq values from the min_val to max_val of the table percent=True converts y values into percent of total y values. exact=True/False only works with pct. exact=False only good to ten decimal places, but much much faster. percent=False leaves as raw long ints, which will often be too large for graphing functions to handle. for that case, graph_pts_overflow(table) is reccommended.''' if not table.values(): raise ValueError('empty table') if zeroes: the_pts = table.frequency_range(table.values_min(), table.values_max() + 1) else: the_pts = table.frequency_all() if percent: temp = [] total_freq = table.total_frequency() if exact: for value, freq in the_pts: temp.append((value, li_div(100 * freq, total_freq))) else: factor = 10**50 for value, freq in the_pts: y_val = (freq * factor) // total_freq temp.append((value, (y_val*100.)/factor)) the_pts = temp[:] if axes: return [tuple([pair[0] for pair in the_pts]), tuple([pair[1] for pair in the_pts])] else: return the_pts
def stats(table, values): '''table is a LongIntTable. values is a list. returns tuple of strings (string of input values, combinations from input values, total combos, 1 in how many chance, percent chance)''' total_freq = table.total_frequency() lst_freq = 0 no_copies = set(values) for value in no_copies: lst_freq += table.frequency(value)[1] if lst_freq == 0: chance = 'infinity' pct = scinote(0) else: chance = scinote(li_div(total_freq, lst_freq)) pct = scinote(100 * li_div(lst_freq, total_freq)) return (list_to_string(values), scinote(lst_freq), scinote(total_freq), chance, pct)
def ascii_graph_helper(table): '''table is a LongIntTable. makes a list of tuples which [(value, x's representing value), ...]''' output_list = [] max_frequency = table.frequency_highest()[1] max_graph_height = 80 divisor = 1 add_s = '' if max_frequency > max_graph_height: divisor = li_div(max_frequency, max_graph_height) add_s = 's' val_len = len(str(table.values_max())) for value, frequency in table.frequency_all(): num_of_xs = int(round(li_div(frequency, divisor))) output_list.append((value, '{0:>{1}}:{2}'.format(value, val_len, num_of_xs*'x') )) output_list.append((None, 'each x represents {} occurence{}'.format( scinote(divisor), add_s ) )) return output_list