Skip to content

Replace event based logging #557

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

Merged
merged 12 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
format negative scientific notation correctly
  • Loading branch information
adrianmolzon committed Apr 7, 2025
commit c9d56b62208c5692ad69de887d268abd1c4d0c7a
34 changes: 28 additions & 6 deletions bayes_opt/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,38 @@ def _format_number(self, x: float) -> str:
-------
A stringified, formatted version of `x`.
"""
if isinstance(x, int):
s = f"{x:<{self._default_cell_size}}"
if abs(x) > 1e7 - 1:
s = f"{x:.5e}"
else:
s = f"{x:<{self._default_cell_size}.{self._default_precision}}"
s = str(x)

if len(s) > self._default_cell_size:
# Convert to str representation of scientific notation
result = ""
width = self._default_cell_size
# Keep negative sign, exponent, and as many decimal places as possible
if "-" in s:
result += "-"
width -= 1
s = s[1:]
if "e" in s:
e_pos = s.find("e")
end = s[e_pos:]
width -= len(end)
if "." in s:
return s[: self._default_cell_size]
return s[: self._default_cell_size - 3] + "..."
return s
dot_pos = s.find(".") + 1
result += s[:dot_pos]
width -= dot_pos
if width > 0:
result += s[dot_pos : dot_pos + width]
else:
result += s[:width]
if "e" in s:
result += end
result = result.ljust(self._default_cell_size)
else:
result = s.ljust(self._default_cell_size)
return result

def _format_bool(self, x: bool) -> str:
"""Format a boolean.
Expand Down
27 changes: 26 additions & 1 deletion tests/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,37 @@ def test_format_number():
long_int = 12345678901234
formatted = logger._format_number(long_int)
assert len(formatted) == logger._default_cell_size
assert "..." in formatted
assert formatted == "1.234e+13"

# Test long float truncation
long_float = 1234.5678901234
formatted = logger._format_number(long_float)
assert len(formatted) == logger._default_cell_size
assert formatted == "1234.5678"

# Test negative long float truncation
long_float = -1234.5678901234
formatted = logger._format_number(long_float)
assert len(formatted) == logger._default_cell_size
assert formatted == "-1234.567"

# Test scientific notation truncation
sci_float = 12345678901234.5678901234
formatted = logger._format_number(sci_float)
assert len(formatted) == logger._default_cell_size
assert formatted == "1.234e+13"

# Test negative scientific notation truncation
sci_float = -12345678901234.5678901234
formatted = logger._format_number(sci_float)
assert len(formatted) == logger._default_cell_size
assert formatted == "-1.23e+13"

# Test long scientific notation truncation
sci_float = -12345678901234.534e132
formatted = logger._format_number(sci_float)
assert len(formatted) == logger._default_cell_size
assert formatted == "-1.2e+145"


def test_format_bool():
Expand Down
Loading