Skip to content

Floor the log axis lower bound when an explicit margin is set - #389

Open
Sanjays2402 wants to merge 1 commit into
reflex-dev:mainfrom
Sanjays2402:fix/log-axis-explicit-margin-underflow
Open

Floor the log axis lower bound when an explicit margin is set#389
Sanjays2402 wants to merge 1 commit into
reflex-dev:mainfrom
Sanjays2402:fix/log-axis-explicit-margin-underflow

Conversation

@Sanjays2402

@Sanjays2402 Sanjays2402 commented Jul 30, 2026

Copy link
Copy Markdown

Closes #350

The explicit-margin log branch of Figure._range() computed out_lo = 10.0 ** (transformed_lo - pad) with no floor, so a wide-but-legal domain (1e-300 to 1e10 with margin=0.1) pushed the padded exponent past the smallest representable double and the lower bound underflowed to 0.0, leaving a log axis with a non-positive lower bound. This applies the same np.nextafter(0.0, 1.0) floor the default-margin branch already uses; the lo / 10.0 clamp is deliberately not carried over, since it would override an authored margin.

Regression test added in tests/test_figure.py beside test_log_autorange_uses_positive_zone_stats; it fails on main (assert np.float64(0.0) > 0.0) and passes with the fix. tests/test_figure.py is otherwise baseline-identical (15 pre-existing failures locally, all headless-browser PNG export).

This change was prepared with AI assistance; the regression test was run locally and fails without the fix.

Review in cubic

Summary by CodeRabbit

  • Bug Fixes
    • Fixed log-scale charts with explicit margins potentially producing a zero or invalid lower bound.
    • Ensured the lower bound remains positive while preserving a valid chart range.

The explicit-margin log branch of _range() computed out_lo as
10.0 ** (transformed_lo - pad) with no floor, so a wide domain (e.g.
1e-300 to 1e10 with margin=0.1) drove the exponent below the smallest
representable double and the padded lower bound underflowed to 0.0,
leaving a log axis with a non-positive lower bound.

Apply the same np.nextafter(0.0, 1.0) floor the default-margin branch
already uses. The lo / 10.0 clamp is deliberately not applied here
because it would override an authored margin.

Adds a regression test with a pathological-but-legal log domain.

Closes reflex-dev#350
@coderabbitai

coderabbitai Bot commented Jul 30, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The explicit-margin log-axis calculation now prevents the lower bound from underflowing to zero. A regression test covers extreme data magnitudes and verifies a positive, ordered y-range.

Changes

Log-axis margin handling

Layer / File(s) Summary
Positive lower-bound protection
python/xy/_figure.py, tests/test_figure.py
The explicit-margin log-axis lower bound is floored at np.nextafter(0.0, 1.0), with a regression test confirming the lower bound is positive and below the upper bound.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Suggested reviewers: alek99, farhanaliraza

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The fix applies the positive floor in the explicit-margin log branch and adds a regression test for the underflow case.
Out of Scope Changes check ✅ Passed The changes stay focused on the reported log-axis underflow fix and its regression test, with no unrelated scope visible.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main fix: flooring the log-axis lower bound when an explicit margin is configured.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/test_figure.py`:
- Around line 1692-1698: The test
test_log_explicit_margin_keeps_lower_bound_positive should verify the exact
positive minimum-float floor, not just that lo is positive. Add an assertion
that lo equals np.nextafter(0.0, 1.0), while retaining the existing hi > lo
check.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: bdbb4482-c00e-46ff-a515-8ae15c8afb0f

📥 Commits

Reviewing files that changed from the base of the PR and between bd1d36e and 0bad3dd.

📒 Files selected for processing (2)
  • python/xy/_figure.py
  • tests/test_figure.py

Comment thread tests/test_figure.py
Comment on lines +1692 to +1698
def test_log_explicit_margin_keeps_lower_bound_positive():
"""An authored margin must not pad the lower bound down to zero."""
fig = Figure().scatter(np.array([0.0, 1.0]), np.array([1e-300, 1e10]))
fig.set_axis("y", type_="log", margin=0.1)
lo, hi = fig.y_range()
assert lo > 0.0
assert hi > lo

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Assert the intended floor, not only positivity.

This test would also pass with the prohibited lo / 10.0 clamp. Assert that lo == np.nextafter(0.0, 1.0) (or otherwise verify lo < 1e-300) so the regression test proves the explicit-margin path uses the positive minimum-float floor.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/test_figure.py` around lines 1692 - 1698, The test
test_log_explicit_margin_keeps_lower_bound_positive should verify the exact
positive minimum-float floor, not just that lo is positive. Add an assertion
that lo equals np.nextafter(0.0, 1.0), while retaining the existing hi > lo
check.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

1 issue found across 2 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="tests/test_figure.py">

<violation number="1" location="tests/test_figure.py:1697">
P3: This assertion only checks `lo > 0.0`, which would also pass under the rejected `lo / 10.0` clamp approach. To actually prove the explicit-margin path uses the `np.nextafter(0.0, 1.0)` floor rather than some other positive clamp, assert the exact expected value (e.g. `lo == np.nextafter(0.0, 1.0)`) or a tight upper bound like `lo < 1e-300`.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic

Comment thread tests/test_figure.py
fig = Figure().scatter(np.array([0.0, 1.0]), np.array([1e-300, 1e10]))
fig.set_axis("y", type_="log", margin=0.1)
lo, hi = fig.y_range()
assert lo > 0.0

@cubic-dev-ai cubic-dev-ai Bot Jul 30, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P3: This assertion only checks lo > 0.0, which would also pass under the rejected lo / 10.0 clamp approach. To actually prove the explicit-margin path uses the np.nextafter(0.0, 1.0) floor rather than some other positive clamp, assert the exact expected value (e.g. lo == np.nextafter(0.0, 1.0)) or a tight upper bound like lo < 1e-300.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At tests/test_figure.py, line 1697:

<comment>This assertion only checks `lo > 0.0`, which would also pass under the rejected `lo / 10.0` clamp approach. To actually prove the explicit-margin path uses the `np.nextafter(0.0, 1.0)` floor rather than some other positive clamp, assert the exact expected value (e.g. `lo == np.nextafter(0.0, 1.0)`) or a tight upper bound like `lo < 1e-300`.</comment>

<file context>
@@ -1689,6 +1689,15 @@ def test_log_autorange_uses_positive_zone_stats():
+    fig = Figure().scatter(np.array([0.0, 1.0]), np.array([1e-300, 1e10]))
+    fig.set_axis("y", type_="log", margin=0.1)
+    lo, hi = fig.y_range()
+    assert lo > 0.0
+    assert hi > lo
+
</file context>
Suggested change
assert lo > 0.0
assert lo == np.nextafter(0.0, 1.0)
Fix with cubic

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.

Log axis with explicit margin= can underflow its padded lower bound to 0

1 participant