8000 Add Return Value to Series.plot by andrewjoc · Pull Request #105 · talmolab/sleap-roots · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add Return Value to Series.plot #105

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 6 commits into from
Apr 9, 2025
Merged

Conversation

andrewjoc
Copy link
Contributor
@andrewjoc andrewjoc commented Mar 31, 2025
  • updated plot function in series.py to return a matplotlib figure instead of returning NoneType

def plot(self, frame_idx: int, scale: float = 1.0, **kwargs):

Resolves #101

Summary by CodeRabbit

  • New Features

    • Enhanced plotting functionality to better manage figure displays. The update now ensures that figures are captured, duplicate displays are prevented, and the plotted output is returned for further use.
  • Tests

    • Added a new test to validate the return type of the updated plot method, ensuring it returns the expected figure type.

Copy link
Contributor
coderabbitai bot commented Mar 31, 2025

Walkthrough

The changes update the plot method within the Series class. The method now specifies that it returns a matplotlib.figure.Figure object, captures the current figure using plt.gcf(), closes the captured figure using plt.close(fig), and returns the figure object. This modification ensures proper management of figure rendering during the plotting process.

Changes

File Changes Summary
sleap_roots/series.py Updated the plot method: modified return type to matplotlib.figure.Figure, captures current figure with plt.gcf(), closes the captured figure with plt.close(fig), and returns the figure object.
tests/test_series.py Added new test function test_series_plot to validate the return type of the plot method, ensuring it returns an instance of matplotlib.figure.Figure. Also added import for matplotlib.figure.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant Series as Series.plot()
    participant plt as Matplotlib

    Caller->>Series: Invoke plot(frame_idx, scale, **kwargs)
    Series->>plt: Call gcf() to capture current figure
    plt-->>Series: Return current figure (fig)
    Series->>plt: Call close(fig) to close the captured figure
    Series->>Caller: Return fig
Loading

Assessment against linked issues

Objective Addressed Explanation
Series.plot() should return plot [#101]

Poem

I’m a little bunny with a spring in my hop,
Capturing plots so none ever stop.
I close a window with a gentle click,
Returning figures fast and slick.
Hoppy coding days, a joyful hop! 🐇


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4f57ec5 and 72e89da.

📒 Files selected for processing (1)
  • tests/test_series.py (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/test_series.py
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Tests (windows-2022, Python 3.11)

🪧 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.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai 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:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @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

Documentation and Community

  • 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 Mar 31, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.20%. Comparing base (f5a8542) to head (72e89da).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #105      +/-   ##
==========================================
+ Coverage   77.44%   82.20%   +4.75%     
==========================================
  Files          13       13              
  Lines        1419     1422       +3     
==========================================
+ Hits         1099     1169      +70     
+ Misses        320      253      -67     

☔ 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.

@andrewjoc andrewjoc marked this pull request as ready for review March 31, 2025 20:55
Copy link
Contributor
@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

🔭 Outside diff range comments (1)
sleap_roots/series.py (1)

270-277: 🛠️ Refactor suggestion

Update the docstring to document the return value.

The docstring needs to be updated to document that the method now returns a matplotlib figure object.

    """Plot predictions on top of the image.

    Args:
        frame_idx: Frame index to visualize.
        scale: Relative size of the visualized image. Useful for plotting smaller
            images within notebooks.
+
+    Returns:
+        A matplotlib.figure.Figure object containing the plot of predictions on top of the image.
    """
🧹 Nitpick comments (3)
sleap_roots/series.py (3)

270-270: Update the method signature to reflect the new return value.

Now that the plot method returns a figure object, the method signature should be updated to indicate the return type.

-def plot(self, frame_idx: int, scale: float = 1.0, **kwargs):
+def plot(self, frame_idx: int, scale: float = 1.0, **kwargs) -> matplotlib.figure.Figure:

314-315: Improve comment clarity.

The current comment may be slightly misleading since the code is getting the figure but not displaying it.

-    # Get the current figure and display it
+    # Get the current figure for returning to the caller
     fig = plt.gcf()
🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 315-315: sleap_roots/series.py#L315
Added line #L315 was not covered by tests


317-318: Consider a more robust approach for managing figure display.

The hardcoded figure ID (1) may be brittle in different contexts. Consider a more flexible approach.

-    # Close one window to prevent plot from displaying twice
-    plt.close(1)
+    # Close the current figure window to prevent display while still returning the figure object
+    plt.close(fig)
🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 318-318: sleap_roots/series.py#L318
Added line #L318 was not covered by tests

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8958e82 and a858d45.

📒 Files selected for processing (1)
  • sleap_roots/series.py (1 hunks)
🧰 Additional context used
🪛 GitHub Check: codecov/patch
sleap_roots/series.py

[warning] 315-315: sleap_roots/series.py#L315
Added line #L315 was not covered by tests


[warning] 318-318: sleap_roots/series.py#L318
Added line #L318 was not covered by tests


[warning] 320-320: sleap_roots/series.py#L320
Added line #L320 was not covered by tests

🔇 Additional comments (1)
sleap_roots/series.py (1)

314-320: LGTM! The implementation satisfies the PR objective.

The changes successfully implement the required feature of returning a matplotlib figure from the plot method, which aligns with the PR objective of "Add Return Value to Series.plot".

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 315-315: sleap_roots/series.py#L315
Added line #L315 was not covered by tests


[warning] 318-318: sleap_roots/series.py#L318
Added line #L318 was not covered by tests


[warning] 320-320: sleap_roots/series.py#L320
Added line #L320 was not covered by tests

Copy link
Collaborator
@eberrigan eberrigan left a comment

Choose a reason for hiding this comment

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

  • Add tests
  • Does .plt.close(1) always work?
  • update docstrings and functions signatures
  • Use returned figure in a notebook and modify the figure to make sure it meets our use case
  • Go through code rabbit knitpick comments.

Thank you!!

@andrewjoc andrewjoc force-pushed the andrew/return-series-plot branch from a858d45 to d5a8319 Compare April 6, 2025 08:54
Copy link
Contributor
@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 (1)
sleap_roots/series.py (1)

324-324: Consider a more targeted approach to closing plots.

The current implementation uses plt.close("all") which closes all open plots. While this works for the current use case, a more targeted approach might be less disruptive if the function is used in a context where other plots exist.

- # Close all current open plots to avoid duplication.
- plt.close("all")
+ # Close only the current figure to avoid duplication.
+ plt.close(fig)
🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 324-324: sleap_roots/series.py#L324
Added li 8000 ne #L324 was not covered by tests

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d5a8319 and 1d6aaaa.

📒 Files selected for processing (1)
  • sleap_roots/series.py (2 hunks)
🧰 Additional context used
🪛 GitHub Check: codecov/patch
sleap_roots/series.py

[warning] 320-320: sleap_roots/series.py#L320
Added line #L320 was not covered by tests


[warning] 324-324: sleap_roots/series.py#L324
Added line #L324 was not covered by tests


[warning] 327-327: sleap_roots/series.py#L327
Added line #L327 was not covered by tests

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Tests (windows-2022, Python 3.11)
🔇 Additional comments (3)
sleap_roots/series.py (3)

270-272: Function signature update looks good.

The return type annotation correctly specifies the return type as matplotlib.figure.Figure, which aligns with the PR objectives to return a figure object instead of None.


280-281: Docstring update is appropriate.

The docstring has been properly updated to document the return value, maintaining consistency between the code implementation and documentation.


319-327: Implementation correctly returns the figure object, but needs tests.

The implementation correctly captures the current figure, closes all open plots to prevent duplication, and returns the figure object. This matches the PR objectives to return a matplotlib figure.

However, these new lines are not covered by tests according to the static analysis.

Consider adding a test case that verifies:

  1. The plot method returns a valid matplotlib figure object
  2. The resource cleanup (plt.close) is working as expected
#!/bin/bash
# Check for existing test patterns related to plot functionality
rg -A 2 -B 2 "def test.*plot" --type py
🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 320-320: sleap_roots/series.py#L320
Added line #L320 was not covered by tests


[warning] 324-324: sleap_roots/series.py#L324
Added line #L324 was not covered by tests


[warning] 327-327: sleap_roots/series.py#L327
Added line #L327 was not covered by tests

@andrewjoc andrewjoc requested a review from eberrigan April 8, 2025 16:33
Copy link
Collaborator
@eberrigan eberrigan left a comment

Choose a reason for hiding this comment

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

great!

@andrewjoc andrewjoc merged commit 92c56cd into main Apr 9, 2025
6 checks passed
@andrewjoc andrewjoc deleted the andrew/return-series-plot branch April 9, 2025 18:33
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.

Series.plot() should return plot
2 participants
0