Skip to content

Commit

Permalink
Merge branch 'trunk-patch' into trunk-minor
Browse files Browse the repository at this point in the history
  • Loading branch information
joaander committed Sep 19, 2023
2 parents 498018a + df6075d commit dfc4bef
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
15 changes: 9 additions & 6 deletions hoomd/pytest/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def logger():
logger = hoomd.logging.Logger(categories=['scalar', "string"])
logger[('dummy', 'loggable', 'int')] = (Identity(42000000), 'scalar')
logger[('dummy', 'loggable', 'float')] = (Identity(3.1415), 'scalar')
logger[('dummy', 'loggable', 'small_float')] = (Identity(0.0000001),
'scalar')
logger[('dummy', 'loggable', 'string')] = (Identity("foobarbaz"), 'string')
return logger

Expand All @@ -46,7 +48,8 @@ def expected_values():
return {
'dummy.loggable.int': 42000000,
'dummy.loggable.float': 3.1415,
'dummy.loggable.string': "foobarbaz"
'dummy.loggable.string': "foobarbaz",
'dummy.loggable.small_float': 0.0000001
}


Expand All @@ -72,7 +75,8 @@ def test_header_generation(device, logger):
lines = output_str.split('\n')
headers = lines[0].split()
expected_headers = [
'dummy.loggable.int', 'dummy.loggable.float', 'dummy.loggable.string'
'dummy.loggable.int', 'dummy.loggable.float', 'dummy.loggable.string',
'dummy.loggable.small_float'
]
assert all(hdr in headers for hdr in expected_headers)
for i in range(1, 10):
Expand Down Expand Up @@ -112,9 +116,8 @@ def test_equality(expected, given):

for row in lines[1:]:
values = row.split()
assert all(
test_equality(expected_values[hdr], v)
for hdr, v in zip(headers, values))
for hdr, v in zip(headers, values):
assert test_equality(expected_values[hdr], v)


@skip_mpi
Expand Down Expand Up @@ -154,7 +157,7 @@ def test_delimiter(device, logger):
table_writer._comm = device.communicator
table_writer.write()
lines = output.getvalue().split('\n')
assert all(len(row.split(',')) == 3 for row in lines[:-1])
assert all(len(row.split(',')) == len(logger) for row in lines[:-1])


@pytest.mark.serial
Expand Down
18 changes: 13 additions & 5 deletions hoomd/write/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ def __call__(self, value, column_width):
else:
return self.format_num(value, column_width)

@staticmethod
def _digits_from_decimal(num):
"""Return digits to represent to the first significant digit."""
digits = int(log10(abs(num)))
# Positive exponents require an extra space (10^0 == 1)
digits = 1 + digits if digits >= 0 else -digits
if num < 0:
digits += 1 # - (negative symbol)
return digits + 1 # decimal point

def format_num(self, value, column_width):
# Always output full integer values
if isinstance(value, Integral):
Expand All @@ -107,11 +117,9 @@ def format_num(self, value, column_width):
# information past the decimal point. For values less than 1 the
# smallest is 0.xxx. The plus one is for the decimal point. We
# already attempt to print out as many decimal points as possible so
# we only need to determine the minimum size to the left of the
# decimal point including the decimal point.
min_len_repr = int(log10(max(abs(value), 1))) + 1
if value < 0:
min_len_repr += 1 # add 1 for the negative sign
# we only need to determine the minimum size to from the decimal
# point including the decimal point.
min_len_repr = self._digits_from_decimal(value) + 1
# Use scientific formatting
if not min_len_repr < 6 or min_len_repr > column_width:
# Determine the number of decimals to use
Expand Down

0 comments on commit dfc4bef

Please sign in to comment.