Skip to content

fix: correct nice=True behavior#12

Merged
MrtinoRG merged 2 commits into
mainfrom
hotfix_nice
Mar 17, 2026
Merged

fix: correct nice=True behavior#12
MrtinoRG merged 2 commits into
mainfrom
hotfix_nice

Conversation

@MrtinoRG
Copy link
Copy Markdown
Collaborator

@MrtinoRG MrtinoRG commented Mar 16, 2026

Summary by Sourcery

Adjust range_frame axis limit handling so padding is consistently applied relative to spine bounds, and expand tests and documentation to reflect the corrected nice=True behavior.

Enhancements:

  • Apply padding to axis limits based on spine ranges for both nice and non-nice modes, ensuring consistent breathing room around the data.
  • Clarify range_frame docstring to distinguish between spine bounds and visible limits and to document nice=False behavior more precisely.

Documentation:

  • Update README documentation for range_frame to describe padding semantics relative to spine bounds and clarify interaction with the nice parameter.

Tests:

  • Add tests verifying that spine bounds follow nice tick positions while visible limits respect pad, including pad>0 and pad=0 cases when nice=True.

Summary by CodeRabbit

  • Bug Fixes

    • Axis padding now consistently extends visible limits beyond spine/data bounds regardless of tick-snapping mode.
  • Documentation

    • Clarified padding semantics: expressed as fraction of spine range, pad_x/pad_y override pad, and padding always applies even when nice is enabled.
  • Tests

    • Added tests verifying spine bounds, padding effects, and behavior with/without nice tick snapping.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Mar 16, 2026

Reviewer's Guide

Adjusts range_frame behavior so that padding is consistently applied around spine bounds for both nice and non-nice axes, adds regression tests to validate pad behavior with nice=True, and updates README documentation to reflect the new semantics.

Flow diagram for updated range_frame padding and nice behavior

flowchart TD
    A[start_range_frame] --> B[determine_pad_x_and_pad_y]
    B --> C{y_is_numeric?}
    C -- yes --> D{y_has_variation?}
    C -- no --> J[set_y_bounds_to_y_min_and_y_max]
    D -- no --> J
    D -- yes --> E{nice_true_for_y?}
    E -- yes --> F[compute_y_bound_min_y_bound_max_and_y_ticks_via_nice_tick_bounds]
    F --> G[set_yticks_to_y_ticks]
    E -- no --> H[set_y_bound_min_to_y_min_and_y_bound_max_to_y_max]
    G --> I[compute_y_range_as_y_bound_max_minus_y_bound_min]
    H --> I
    I --> K[set_ylim_to_y_bound_min_minus_pad_x_times_y_range_and_y_bound_max_plus_pad_x_times_y_range]
    J --> L[set_ylim_to_y_min_and_y_max]

    B --> M{x_is_numeric?}
    M -- yes --> N{ x_has_variation? }
    M -- no --> U[set_x_bounds_to_x_min_and_x_max]
    N -- no --> U
    N -- yes --> O{nice_true_for_x?}
    O -- yes --> P[compute_x_bound_min_x_bound_max_and_x_ticks_via_nice_tick_bounds]
    P --> Q[set_xticks_to_x_ticks]
    O -- no --> R[set_x_bound_min_to_x_min_and_x_bound_max_to_x_max]
    Q --> S[compute_x_range_as_x_bound_max_minus_x_bound_min]
    R --> S
    S --> T[set_xlim_to_x_bound_min_minus_pad_y_times_x_range_and_x_bound_max_plus_pad_y_times_x_range]
    U --> V[set_xlim_to_x_min_and_x_max]

    K --> W[end_range_frame]
    L --> W
    T --> W
    V --> W
Loading

File-Level Changes

Change Details Files
Apply padding around spine bounds for y-axis regardless of nice flag and base it on the spine (nice) range.
  • Refactor y-axis limit computation so nice=True sets tick positions only and no longer overrides ylim directly.
  • Compute y_range from y_bound_min/y_bound_max and always apply pad_x as a fraction of that range to set ylim.
  • Ensure that when nice=False, y_bound_min/y_bound_max use raw data range so padding is still applied consistently.
src/lama_aesthetics/plotutils.py
Apply padding around spine bounds for x-axis regardless of nice flag and base it on the spine (nice) range.
  • Refactor x-axis limit computation so nice=True sets tick positions only and no longer overrides xlim directly.
  • Compute x_range from x_bound_min/x_bound_max and always apply pad_y as a fraction of that range to set xlim.
  • Ensure that when nice=False, x_bound_min/x_bound_max use raw data range so padding is still applied consistently.
src/lama_aesthetics/plotutils.py
Add tests that clarify and lock in range_frame behavior with nice=True and different pad values.
  • Extend existing test_range_frame to assert that spine bounds come from nice tick locations and that default pad causes limits to extend beyond those bounds.
  • Add test_range_frame_nice_true_with_pad to assert pad is respected for nice=True with non-default pad and that limits are offset from spine bounds by the pad fraction.
  • Add test_range_frame_nice_true_pad_zero to assert that when pad=0 and nice=True the axis limits match the spine bounds exactly.
tests/test_plotutils.py
Update README documentation to describe the new, consistent padding semantics and the effect of nice on spine vs data range.
  • Clarify that pad is expressed as a fraction of the spine range and that visible limits extend beyond spine bounds by this amount.
  • Update nice parameter description to note that when False spines span the raw data range and that padding is always applied regardless of nice.
README.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 16, 2026

📝 Walkthrough

Walkthrough

The pull request makes padding behavior in range_frame consistent across nice settings, updates README and docstrings to clarify padding semantics (fraction of spine range; per-axis overrides), and adds tests covering spine bounds, padding, and nice handling.

Changes

Cohort / File(s) Summary
Documentation
README.md
Clarified pad and nice parameter descriptions: padding is always applied, expressed as a fraction of the spine range; pad_x/pad_y override pad.
Core Implementation
src/lama_aesthetics/plotutils.py
Reworked range_frame so padding is computed and applied uniformly for both nice=True and nice=False paths, using per-axis ranges (x_range, y_range) and explicit spine bounds; docstrings and inline docs updated.
Tests
tests/test_plotutils.py
Added tests verifying spine bounds equal data bounds, limits extend by padding fraction, nice=True behavior with padding and pad=0 edge case, and non-numeric axis behavior.

Sequence Diagram(s)

(omitted)

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • kjappelbaum

Poem

🐰 I hopped through ranges, neat and wide,
I nudged the spines on every side,
Whether "nice" or raw and true,
A fraction stretched the view,
Tests cheered as limits took their stride.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: correct nice=True behavior' directly addresses the main change: correcting the behavior of the nice parameter in range_frame to ensure padding is consistently applied regardless of the nice setting.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch hotfix_nice
📝 Coding Plan
  • Generate coding plan for human review comments

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

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

Tip

CodeRabbit can use Trivy to scan for security misconfigurations and secrets in Infrastructure as Code files.

Add a .trivyignore file to your project to customize which findings Trivy reports.

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • The tests asserting padding behavior use a very tight absolute tolerance of 1e-10 on floating point comparisons, which could be fragile across platforms or Matplotlib versions; consider using a slightly looser tolerance or np.isclose with relative tolerance instead.
  • The new x_range/y_range and limits computation logic for nice=True and nice=False is now duplicated for both axes; consider extracting a small helper to compute (bound_min, bound_max) -> (limit_min, limit_max) to keep the behavior consistent and easier to maintain.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The tests asserting padding behavior use a very tight absolute tolerance of 1e-10 on floating point comparisons, which could be fragile across platforms or Matplotlib versions; consider using a slightly looser tolerance or `np.isclose` with relative tolerance instead.
- The new `x_range`/`y_range` and limits computation logic for `nice=True` and `nice=False` is now duplicated for both axes; consider extracting a small helper to compute `(bound_min, bound_max) -> (limit_min, limit_max)` to keep the behavior consistent and easier to maintain.

## Individual Comments

### Comment 1
<location path="tests/test_plotutils.py" line_range="47-56" />
<code_context>
+    plt.close(fig)
+
+
+def test_range_frame_nice_true_with_pad():
+    """Test that pad is respected even when nice=True."""
+    fig, ax = plt.subplots()
+    x = np.array([1, 2, 3, 4, 5])
+    y = np.array([10, 20, 30, 40, 50])
+
+    range_frame(ax, x, y, pad=0.2, nice=True)
+
+    xlim = ax.get_xlim()
+    ylim = ax.get_ylim()
+
+    # Spine bounds come from nice ticks
+    x_spine = ax.spines["bottom"].get_bounds()
+    y_spine = ax.spines["left"].get_bounds()
+
+    # Limits must extend beyond spine bounds by the pad fraction
+    x_range = x_spine[1] - x_spine[0]
+    y_range = y_spine[1] - y_spine[0]
+
+    assert abs(xlim[0] - (x_spine[0] - 0.2 * x_range)) < 1e-10
+    assert abs(xlim[1] - (x_spine[1] + 0.2 * x_range)) < 1e-10
+    assert abs(ylim[0] - (y_spine[0] - 0.2 * y_range)) < 1e-10
+    assert abs(ylim[1] - (y_spine[1] + 0.2 * y_range)) < 1e-10
+
+    plt.close(fig)
</code_context>
<issue_to_address>
**suggestion (testing):** Add coverage for `pad_x`/`pad_y` when `nice=True`, not just the symmetric `pad` case.

Since the change also affects the per-axis `pad_x`/`pad_y` handling with `nice=True`, add a test where they differ (e.g. `pad_x=0.0, pad_y=0.2`) and assert that each axis limit uses its own pad value. This will better safeguard the `nice` + per-axis padding behavior against regressions.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread tests/test_plotutils.py
Comment on lines +47 to +56
def test_range_frame_nice_true_with_pad():
"""Test that pad is respected even when nice=True."""
fig, ax = plt.subplots()
x = np.array([1, 2, 3, 4, 5])
y = np.array([10, 20, 30, 40, 50])

range_frame(ax, x, y, pad=0.2, nice=True)

xlim = ax.get_xlim()
ylim = ax.get_ylim()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion (testing): Add coverage for pad_x/pad_y when nice=True, not just the symmetric pad case.

Since the change also affects the per-axis pad_x/pad_y handling with nice=True, add a test where they differ (e.g. pad_x=0.0, pad_y=0.2) and assert that each axis limit uses its own pad value. This will better safeguard the nice + per-axis padding behavior against regressions.

Copy link
Copy Markdown

@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: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/lama_aesthetics/plotutils.py`:
- Around line 81-83: The sentence claiming "Regardless of `nice`, the `pad` /
`pad_x` / `pad_y` parameters control how far the visible axis limits extend
beyond the spine bounds..." should be narrowed to numeric axes only (or
explicitly state categorical behavior): update the documentation in plotutils.py
near the `nice`, `pad`, `pad_x`, and `pad_y` parameter descriptions so it reads
that these padding parameters apply to numeric (continuous) axes; also add a
brief note that non-numeric/categorical axes bypass this padding (or describe
how categorical spacing is handled) so readers know the exception.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 69c3899a-e683-4ac3-8a14-3399539ad2af

📥 Commits

Reviewing files that changed from the base of the PR and between 7e8982e and 565cdb1.

📒 Files selected for processing (1)
  • src/lama_aesthetics/plotutils.py

Comment on lines +81 to +83
Regardless of `nice`, the `pad` / `pad_x` / `pad_y` parameters
control how far the visible axis limits extend beyond the spine bounds,
giving data points breathing room from the spine edges.
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Clarify scope of the “regardless of nice” statement.

Current wording reads as if padding always applies for all axis types, but non-numeric axes still bypass padding. Please narrow this sentence to numeric axes (or explicitly document the categorical behavior).

✏️ Suggested doc wording
-    Regardless of `nice`, the `pad` / `pad_x` / `pad_y` parameters
-    control how far the visible axis limits extend beyond the spine bounds,
-    giving data points breathing room from the spine edges.
+    For numeric axes, regardless of `nice`, the `pad` / `pad_x` / `pad_y`
+    parameters control how far the visible axis limits extend beyond the
+    spine bounds, giving data points breathing room from the spine edges.
+    For non-numeric axes, limits follow categorical index bounds.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
Regardless of `nice`, the `pad` / `pad_x` / `pad_y` parameters
control how far the visible axis limits extend beyond the spine bounds,
giving data points breathing room from the spine edges.
For numeric axes, regardless of `nice`, the `pad` / `pad_x` / `pad_y`
parameters control how far the visible axis limits extend beyond the
spine bounds, giving data points breathing room from the spine edges.
For non-numeric axes, limits follow categorical index bounds.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/lama_aesthetics/plotutils.py` around lines 81 - 83, The sentence claiming
"Regardless of `nice`, the `pad` / `pad_x` / `pad_y` parameters control how far
the visible axis limits extend beyond the spine bounds..." should be narrowed to
numeric axes only (or explicitly state categorical behavior): update the
documentation in plotutils.py near the `nice`, `pad`, `pad_x`, and `pad_y`
parameter descriptions so it reads that these padding parameters apply to
numeric (continuous) axes; also add a brief note that non-numeric/categorical
axes bypass this padding (or describe how categorical spacing is handled) so
readers know the exception.

@MrtinoRG MrtinoRG merged commit f9aaa7e into main Mar 17, 2026
5 checks passed
@MrtinoRG MrtinoRG deleted the hotfix_nice branch March 17, 2026 15:42
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.

1 participant