Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dots to alternating tabular lines #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
'scipy',
'tabulate',
'tensorboardX',
'wcwidth',
]

extras = dict()
Expand Down
24 changes: 20 additions & 4 deletions src/dowel/tabular_input.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""A `dowel.logger` input for tabular (key-value) data."""
import contextlib
import warnings
import wcwidth

import numpy as np
import tabulate
Expand All @@ -24,8 +25,23 @@ def __init__(self):

def __str__(self):
"""Return a string representation of the table for the logger."""
return tabulate.tabulate(
sorted(self.as_primitive_dict.items(), key=lambda x: x[0]))
# Sort first, then pad.
lines = sorted(self.as_primitive_dict.items(), key=lambda x: x[0])
key_widths = [wcwidth.wcswidth(key) for key, value in lines]
max_key_width = max(key_widths, default=0)
padded_keys = []
for line, (key, value) in enumerate(lines):
padded_key = key
if line % 2 == 1:
key_width = wcwidth.wcswidth(key)
if key_width % 2 == 1 and key_width < max_key_width:
padded_key += ' '
key_width += 1
pad_width = (max_key_width - key_width) // 2
padded_key += ' .' * pad_width
padded_keys.append(padded_key)
values = [value for key, value in lines]
return tabulate.tabulate(zip(padded_keys, values))

def record(self, key, val):
"""Save key/value entries for the table.
Expand Down Expand Up @@ -55,11 +71,11 @@ def record_misc_stat(self, key, values, placement='back'):
:param placement: Whether to put the prefix in front or in the back.
"""
if placement == 'front':
front = ""
front = ''
back = key
else:
front = key
back = ""
back = ''
if values:
self.record(front + 'Average' + back, np.average(values))
self.record(front + 'Std' + back, np.std(values))
Expand Down
30 changes: 30 additions & 0 deletions tests/dowel/test_simple_outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,36 @@ def test_record_tabular(self, mock_datetime):
self.str_out.seek(0)
assert self.str_out.read() == tab

def test_record_tabular_line_markers(self, mock_datetime):
fake_timestamp(mock_datetime)

self.tabular.record('a', 100)
self.tabular.record('bbbbbbb', 55)
self.tabular.record('ccccc', 55)
self.tabular.record('d', 55)
self.tabular.record('ee', 55)
self.tabular.record('ff', 55)

with redirect_stdout(self.str_out):
self.std_output.record(self.tabular)

self.std_output.dump()

tab = (
'------- ---\n'
'a 100\n'
'bbbbbbb 55\n'
'ccccc 55\n'
'd . . 55\n'
'ee 55\n'
'ff . . 55\n'
'------- ---\n'
) # yapf: disable
self.str_out.seek(0)
output = self.str_out.read()
print(output)
assert output == tab

def test_record_with_timestamp(self, mock_datetime):
fake_timestamp(mock_datetime)

Expand Down