diff --git a/source/build_corr_table.py b/source/build_corr_table.py index 174da614..17072fd9 100644 --- a/source/build_corr_table.py +++ b/source/build_corr_table.py @@ -16,8 +16,9 @@ def replace_val(part, val): return vs + ' ' * (len(part) - len(vs)) -def prepend_row(tab_md, vals): +def extend_with_row(tab_md, vals, where='before'): lines = tab_md.splitlines() + lines = lines[::-1] if where == 'after' else lines assert lines[0].startswith('+') L1 = lines[1] assert (L1[0], L1[-1]) == ('|', '|') @@ -26,6 +27,7 @@ def prepend_row(tab_md, vals): for part, val in zip(parts, vals): new_parts.append(replace_val(part, val)) L1 = '|'.join(new_parts) + lines = lines[::-1] if where == 'after' else lines return '\n'.join([lines[0], f'|{L1}|'] + lines) @@ -39,11 +41,18 @@ def footerize_table(tab_md, indices=(-2, -1)): return '\n'.join(lines) -def write_footerized(df, path, prepended=None): +def to_md(df, prepended=None, extended=None): tab_md = df.to_markdown(index=None, tablefmt='grid', numalign="left") if prepended is not None: - tab_md = prepend_row(tab_md, prepended) + tab_md = extend_with_row(tab_md, prepended) + if extended is not None: + tab_md = extend_with_row(tab_md, extended, where='after') + return tab_md + + +def write_footerized(df, path, prepended=None, extended=None): print(f'Writing {path}') + tab_md = to_md(df, prepended, extended) Path(path).write_text(footerize_table(tab_md)) @@ -67,7 +76,6 @@ def write_footerized(df, path, prepended=None): ) sums = out.sum(axis='index') - not_sums = [c for c in out if ' x ' not in c] sums[not_sums] = '' sums.iloc[0] = '**SUMS**' @@ -87,6 +95,7 @@ def sumprod(col): return np.sum(col * ath) samp_sums = samp_out.agg(sumprod, axis='index') +samp_sums.iloc[0] = '**Product sums**' samp_out2 = pd.concat([samp_out, samp_sums.to_frame().T])