def __create_col_metadata(marker, meta, colwidths): linelist = [marker] for colmeta, colwidth in iterators.IteratorUnion(iter(meta), iter(colwidths)): format = '%-' + str(colwidth) + 's' if marker == COLUMN_LABEL_MARKER: # add quotes colmeta = '"%s"' % colmeta linelist.append(format % colmeta) linelist.append('\n') return '\t'.join(linelist)
def __calc_col_widths(table, colformat): col_widths = [] for name, type, data in iterators.IteratorUnion(iter(table.colnames), iter(table.coltypes), iter(table.coldata)): # add 2 to name length to account for quotes width = max(len(str(name)) + 2, len(type)) if colformat == 'fixed': for row in data: width = max(width, len(str(row))) col_widths.append(width) return col_widths
def ttest_table( data1, data2, n, tcrit, regions, # general names regions1, # normalized for classification of data1 regions2, # normalized for classification of data2 timesteps, input_colnames, func_iter): COLNAMES = ['Timestep', 'Mean', 'StdDev', 'Sig', 'tval', 'pval'] COLTYPES = ['int', 'float', 'float', 'int', 'float', 'float'] def __createTable(name): return (name, datalib.Table(name, COLNAMES, COLTYPES)) # {C-colname,table}, where colname is min, max, or mean result = dict([ __createTable('-'.join(C_col)) for C_col in iterators.product(regions, input_colnames) ]) for C, C1, C2 in iterators.IteratorUnion(iter(regions), iter(regions1), iter(regions2)): for t in timesteps: for col in input_colnames: diffMean, stdDev, tval, pval = ttest( (data1, C1, t, col), (data2, C2, t, col), n, func_iter) table = result[C + '-' + col] row = table.createRow() row["Timestep"] = t row["Mean"] = diffMean row["StdDev"] = stdDev if tval >= tcrit: row["Sig"] = 1 else: row["Sig"] = 0 row["tval"] = tval row["pval"] = pval return result
def diff_mean(ita, itb): n = 0 sum = 0.0 n_inf = 0 inf = float('inf') for a, b in iterators.IteratorUnion(ita, itb): if a == inf or b == inf: n_inf += 1 else: n += 1 sum += a - b if n_inf > 0: print 'diff_mean() ignoring', n_inf, 'inf value(s)' if n < 1: return 0.0 else: return sum / n
def diff_stddev(diffmean, ita, itb): n = 0 sum = 0.0 n_inf = 0 inf = float('inf') for a, b in iterators.IteratorUnion(ita, itb): if a == inf or b == inf: n_inf += 1 else: n += 1 delta = a - b - diffmean sum += delta**2 if n_inf > 0: print 'diff_stddev() ignoring', n_inf, 'inf value(s)' if n < 1: return 0.0 else: return sqrt(sum / (n - 1))