def bootstrap_mean_formatter(series, q=0.95): if q < 0 or q > 1: raise ValueError("Invalid q %0.2f, needs to be within [0, 1]" % q) q = q * 100 value = series.mean() low = np.percentile(series, (100 - q) / 2.0) high = np.percentile(series, (q + ((100 - q) / 2.0))) return ("%s, %d%% CI (%s, %s)" % (float_str(value), int(q), float_str(low), float_str(high)))
def hr_posterior_formatter(series, q=0.95, n=None, summary='mean', q_format="{q}% CI ({low}, {high})", stat='HR', stat_format="{stat}={value}", include_p=False, p_compare=0): # construct resulting string string_parts = list() if n: string_parts.append("n=%d" % (int(n))) # summarize stat if summary is given as a list if isinstance(summary, list): for summary_type in summary: if summary_type == 'mean': value = series.mean() elif summary_type == 'median': value = series.median() else: raise ValueError("invalid summary %s; needs to be either None, 'mean', or 'median'" % summary_type) string_parts.append(stat_format.format(stat='{} ({})'.format(stat, summary_type), value=float_str(value)) ) # summarize stat if summary is given as a single str elif isinstance(summary, str): if summary == 'mean': value = series.mean() elif summary == 'median': value = series.median() else: raise ValueError("invalid summary %s; needs to be either None, 'mean', or 'median'" % summary) string_parts.append(stat_format.format(stat=stat, value=float_str(value))) # add posterior interval if q is given if q: if q < 0 or q > 1: raise ValueError("Invalid q %0.2f, needs to be within [0, 1]" % q) q = q * 100 low = np.percentile(series, (100 - q) / 2.0) high = np.percentile(series, (q + ((100 - q) / 2.0))) string_parts.append(q_format.format(q=int(q), low=float_str(low), high=float_str(high))) # compute 'bayesian p-value', ie probability that HR <> 0 if include_p: prob = 1-np.mean(series >= p_compare) direction = '>' if prob >= 0.5: prob = 1-prob direction = '<' string_parts.append('p({}{}{})={}'.format(stat, direction, p_compare, float_str(prob))) if len(string_parts) == 0: raise ValueError('No summary components specified') return ', '.join(string_parts)
def test_rounding(): for f, expected_str in ROUNDING_CASES.items(): eq_(expected_str, float_str(f))
def round_func(val): if round_to_int: return format(int(round(val)), ",d") else: return float_str(val)
def spearmanr_formatter(results): return "n=%d, Spearman rho=%s p=%s" % (len(results.series_x), float_str(results.coeff), float_str(results.p_value))
def pearsonr_formatter(results): return "n=%d, Pearson r=%s p=%s" % (len(results.series_x), float_str(results.coeff), float_str(results.p_value))
def logrank_formatter(results): return "n=%d, log-rank p=%s" % ( len(results.with_condition_series) + len(results.without_condition_series), float_str(results.p_value))
def fishers_exact_formatter(results): return "n=%d, Fisher's Exact p=%s" % (len(results.with_condition1_series) + len(results.without_condition1_series), float_str(results.p_value))
def mann_whitney_formatter(results): return "n=%d, Mann-Whitney p=%s" % (len(results.with_condition_series) + len(results.without_condition_series), float_str(results.p_value))