Skip to content

Commit

Permalink
Extend build_corr_table
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-brett committed Jun 13, 2024
1 parent 94fed46 commit 82d5859
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions source/build_corr_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]) == ('|', '|')
Expand All @@ -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)


Expand All @@ -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))


Expand All @@ -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**'
Expand All @@ -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])

Expand Down

0 comments on commit 82d5859

Please sign in to comment.