From 8d40e9db8b51f742befd99a06e506007d29c81f3 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Sat, 15 Oct 2022 23:30:41 +1100 Subject: [PATCH] Improve formatting of tabular p values --- yli/regress.py | 16 ++++++++-------- yli/utils.py | 30 ++++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/yli/regress.py b/yli/regress.py index aacac25..39b6545 100644 --- a/yli/regress.py +++ b/yli/regress.py @@ -189,10 +189,10 @@ class RegressionResult: if html: right_col.append(('F:', format(f_result.statistic, '.2f'))) - right_col.append(('p (F):', fmt_p(f_result.pvalue, html=True, nospace=True))) + right_col.append(('p (F):', fmt_p(f_result.pvalue, html=True, tabular=True))) else: right_col.append(('F:', format(f_result.statistic, '.2f'))) - right_col.append(('p (F):', fmt_p(f_result.pvalue, html=False, nospace=True))) + right_col.append(('p (F):', fmt_p(f_result.pvalue, html=False, tabular=True))) else: # Otherwise report likelihood ratio test as overall test lrtest_result = self.lrtest_null() @@ -200,9 +200,9 @@ class RegressionResult: right_col.append(('LL-Model:', format(self.llf, '.2f'))) right_col.append(('LL-Null:', format(self.llnull, '.2f'))) if html: - right_col.append(('p (LR):', fmt_p(lrtest_result.pvalue, html=True, nospace=True))) + right_col.append(('p (LR):', fmt_p(lrtest_result.pvalue, html=True, tabular=True))) else: - right_col.append(('p (LR):', fmt_p(lrtest_result.pvalue, html=False, nospace=True))) + right_col.append(('p (LR):', fmt_p(lrtest_result.pvalue, html=False, tabular=True))) return left_col, right_col @@ -232,7 +232,7 @@ class RegressionResult: if self.exp: beta = np.exp(beta) - out += '{}{:.2f}({:.2f}–{:.2f}){}'.format(term_name, beta.point, beta.ci_lower, beta.ci_upper, fmt_p(term.pvalue, html=True, nospace=True)) + out += '{}{:.2f}({:.2f}–{:.2f}){}'.format(term_name, beta.point, beta.ci_lower, beta.ci_upper, fmt_p(term.pvalue, html=True, tabular=True)) elif isinstance(term, CategoricalTerm): # Categorical term out += '{}'.format(term_name) @@ -247,7 +247,7 @@ class RegressionResult: if self.exp: beta = np.exp(beta) - out += '{}{:.2f}({:.2f}–{:.2f}){}'.format(sub_term_name, beta.point, beta.ci_lower, beta.ci_upper, fmt_p(sub_term.pvalue, html=True, nospace=True)) + out += '{}{:.2f}({:.2f}–{:.2f}){}'.format(sub_term_name, beta.point, beta.ci_lower, beta.ci_upper, fmt_p(sub_term.pvalue, html=True, tabular=True)) else: raise Exception('Attempt to render unknown term type') @@ -287,7 +287,7 @@ class RegressionResult: beta = np.exp(beta) # Add some extra padding - table_data.append([term_name + ' ', format(beta.point, '.2f'), '({:.2f}'.format(beta.ci_lower), '-', '{:.2f})'.format(beta.ci_upper), ' ' + fmt_p(term.pvalue, html=False, nospace=True)]) + table_data.append([term_name + ' ', format(beta.point, '.2f'), '({:.2f}'.format(beta.ci_lower), '-', '{:.2f})'.format(beta.ci_upper), ' ' + fmt_p(term.pvalue, html=False, tabular=True)]) elif isinstance(term, CategoricalTerm): # Categorical term table_data.append([term_name + ' ', '', '', '', '', '']) @@ -302,7 +302,7 @@ class RegressionResult: if self.exp: beta = np.exp(beta) - table_data.append([sub_term_name + ' ', format(beta.point, '.2f'), '({:.2f}'.format(beta.ci_lower), '-', '{:.2f})'.format(beta.ci_upper), ' ' + fmt_p(sub_term.pvalue, html=False, nospace=True)]) + table_data.append([sub_term_name + ' ', format(beta.point, '.2f'), '({:.2f}'.format(beta.ci_lower), '-', '{:.2f})'.format(beta.ci_upper), ' ' + fmt_p(sub_term.pvalue, html=False, tabular=True)]) else: raise Exception('Attempt to render unknown term type') diff --git a/yli/utils.py b/yli/utils.py index b83c2d1..aff3356 100644 --- a/yli/utils.py +++ b/yli/utils.py @@ -96,30 +96,44 @@ def do_fmt_p(p): # OK to round to pvalue_min_dps return None, '{0:.{dps}f}'.format(p, dps=config.pvalue_min_dps) -def fmt_p(p, *, html, nospace=False): - """Format p value""" +def fmt_p(p, *, html, tabular=False): + """ + Format p value + + tabular: If true, output in ‘tabular’ format of p values where decimal points align + """ sign, fmt = do_fmt_p(p) + # Strip leading zero if required if not config.pvalue_leading_zero: fmt = fmt.lstrip('0') + + # Add significance asterisk if required if p < config.alpha: fmt += '*' if sign is not None: - if nospace: + if html: + # Escape angle quotes + sign = sign.replace('<', '<') + sign = sign.replace('>', '>') + + if tabular: pfmt = sign + fmt # e.g. "<0.001" else: pfmt = sign + ' ' + fmt # e.g. "< 0.001" else: - if nospace: - pfmt = fmt # e.g. "0.05" + if tabular: + # Tabular format with no sign: add a preceding space to get decimal points to line up + if html: + # Insert space with width of '=' which should be width of '<' and '>' too + pfmt = '=' + fmt # e.g. "0.05" + else: + pfmt = ' ' + fmt # e.g. "0.05" else: pfmt = '= ' + fmt # e.g. "= 0.05" - if html: - pfmt = pfmt.replace('<', '<') - return pfmt # ------------------------------