Skip to content

Conversation

adrianmolzon
Copy link
Contributor

@adrianmolzon adrianmolzon commented Aug 28, 2025

Fixing #589 (comment)
I was checking whether a minus sign was present, the formatter will now check that the number is negative when formatting for a negative number

Summary by CodeRabbit

  • Bug Fixes

    • Improved numeric sign handling and width-based truncation in log number formatting so scientific notation and negative values display correctly (exponent-aware truncation, sign determined by value).
  • Tests

    • Added/expanded tests covering small-magnitude scientific notation for positive and negative values to prevent regressions.

Copy link

coderabbitai bot commented Aug 28, 2025

Walkthrough

Updates numeric sign handling and truncation in bayes_opt/logger.py to use the numeric value (x < 0) and to avoid replacing the leading digit when formatting small scientific-notation values; adds unit tests for positive and negative small-magnitude scientific notation.

Changes

Cohort / File(s) Summary
Logger formatting logic
bayes_opt/logger.py
Replace string-based sign detection with numeric check (x < 0); compute sign and adjust width from the numeric sign; when formatting scientific notation, truncate the magnitude portion (mantissa) before appending exponent; retain decimal/exponent padding logic.
Unit tests
tests/test_logger.py
Add/expand tests for small-magnitude scientific notation formatting, including 1.11111111e-5 -> "1.111e-05" and -1.11111111e-5 -> "-1.11e-05".

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant C as Caller
  participant L as logger._format_number

  Note over L: High-level formatting flow

  C->>L: _format_number(x, width, prec)
  alt x < 0
    L->>L: sign = "-"
    L->>L: mag = abs(x)
    L->>L: width = width - 1
  else
    L->>L: sign = ""
    L->>L: mag = x
  end
  L->>L: fmt = format(mag, decide fixed or scientific)
  alt scientific notation
    L->>L: split into mantissa and exponent
    L->>L: truncate mantissa to available width/precision
    L->>L: append normalized exponent (e.g., e-05)
  else
    L->>L: truncate/round decimal part as before
  end
  L->>L: result = sign + formatted(mantissa [+ exponent])
  L->>L: pad result to requested width
  L-->>C: formatted string
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Assessment against linked issues

Objective Addressed Explanation
Fix formatting error where positive scientific-notation numbers with long mantissas and negative exponents get a leading minus replacing the first digit (#589)

Poem

I twitch my nose and tidy logs tonight,
No dashes stealing digits from the light.
Tiny e-05s now sparkle true,
Positive and negative—both in view.
A rabbit's hop for fixes done just right.

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.


📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 56ff0b5 and 006b979.

📒 Files selected for processing (2)
  • bayes_opt/logger.py (2 hunks)
  • tests/test_logger.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • tests/test_logger.py
  • bayes_opt/logger.py
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
  • GitHub Check: Python 3.9 - numpy >=2
  • GitHub Check: Python 3.12 - numpy >=2
  • GitHub Check: build-docs-and-publish
  • GitHub Check: Python 3.12 - numpy >=1.25,<2
  • GitHub Check: Python 3.9 - numpy >=1.25,<2
  • GitHub Check: Python 3.13 - numpy >=2
  • GitHub Check: Python 3.10 - numpy >=1.25,<2
  • GitHub Check: Python 3.11 - numpy >=1.25,<2
  • GitHub Check: Python 3.11 - numpy >=2
  • GitHub Check: Python 3.10 - numpy >=2
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbit in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbit in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbit gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbit read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbit help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbit ignore or @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbit summary or @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbit or @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

codecov bot commented Aug 28, 2025

Codecov Report

❌ Patch coverage is 33.33333% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.72%. Comparing base (14786ff) to head (006b979).

Files with missing lines Patch % Lines
bayes_opt/logger.py 33.33% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #590      +/-   ##
==========================================
- Coverage   97.80%   97.72%   -0.09%     
==========================================
  Files          10       10              
  Lines        1186     1187       +1     
==========================================
  Hits         1160     1160              
- Misses         26       27       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
bayes_opt/logger.py (1)

93-103: Avoid potential exponent duplication when no decimal point exists.

If s contains 'e' but no '.' (rare, but possible with str(x)), result += s[:width] followed by result += end can duplicate the exponent. Minor guard below keeps only the mantissa before appending end.

-            else:
-                result += s[:width]
+            else:
+                # Use only the mantissa when no decimal point is present to avoid duplicating the exponent.
+                head = s[:e_pos] if "e" in s else s
+                result += head[:width]
tests/test_logger.py (1)

90-93: Great regression test covering the reported bug.

This precisely guards against the false-negative formatting for positive numbers with negative exponents. Consider also adding the negative counterpart for symmetry.

     sci_float = 1.11111111e-5
     formatted = logger._format_number(sci_float)
     assert formatted == "1.111e-05"
+
+    # Symmetric negative case
+    sci_float_neg = -1.11111111e-5
+    formatted = logger._format_number(sci_float_neg)
+    assert formatted == "-1.111e-05"
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 14786ff and 56ff0b5.

📒 Files selected for processing (2)
  • bayes_opt/logger.py (1 hunks)
  • tests/test_logger.py (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
tests/test_logger.py (1)
bayes_opt/logger.py (1)
  • _format_number (66-106)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (9)
  • GitHub Check: Python 3.12 - numpy >=1.25,<2
  • GitHub Check: Python 3.9 - numpy >=2
  • GitHub Check: Python 3.11 - numpy >=1.25,<2
  • GitHub Check: Python 3.10 - numpy >=2
  • GitHub Check: Python 3.9 - numpy >=1.25,<2
  • GitHub Check: Python 3.11 - numpy >=2
  • GitHub Check: Python 3.13 - numpy >=2
  • GitHub Check: Python 3.12 - numpy >=2
  • GitHub Check: Python 3.10 - numpy >=1.25,<2
🔇 Additional comments (1)
bayes_opt/logger.py (1)

85-89: Correct fix for false negatives from exponent substrings.

Switching to x < 0 removes the accidental sign detection from 'e-XX' and resolves the reported formatting bug. Looks good.

@adrianmolzon
Copy link
Contributor Author

@LiemuR Thanks again for pointing out this mistake. To be clear, this was just a formatting issue, but the formatting issue has now been fixed.

@till-m Can I get an approval here

Copy link
Member

@till-m till-m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be fine with merging this as-is, but maybe check if the clanker had some nitpicks worth considering.

Copy link
Member

@till-m till-m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but I think it makes sense to add coverage of this little bit of added code.

@@ -97,7 +97,8 @@ def _format_number(self, x: float) -> str:
if width > 0:
result += s[dot_pos : dot_pos + width]
else:
result += s[:width]
head = s[:e_pos] if "e" in s else s
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a test for this maybe?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants