Skip to content
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

Add a method for progress logging multiple alignments #199

Merged
merged 4 commits into from
Jan 7, 2025

Conversation

clintval
Copy link
Member

@clintval clintval commented Dec 23, 2024

Often I am iterating through a BAM by query name and find I'm having to manually loop over all records to increment the progress logger. This new method would make logging all the records in a template more convenient.

Copy link

codecov bot commented Dec 23, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.34%. Comparing base (f9aa849) to head (3b12692).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #199      +/-   ##
==========================================
+ Coverage   91.31%   91.34%   +0.02%     
==========================================
  Files          18       18              
  Lines        2281     2287       +6     
  Branches      334      335       +1     
==========================================
+ Hits         2083     2089       +6     
  Misses        126      126              
  Partials       72       72              

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

@clintval clintval marked this pull request as ready for review December 23, 2024 22:26
@clintval clintval requested review from nh13 and tfenne as code owners December 23, 2024 22:26
Copy link
Contributor

coderabbitai bot commented Dec 23, 2024

Warning

Rate limit exceeded

@clintval has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 4 minutes and 53 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between c93fc80 and 3b12692.

📒 Files selected for processing (2)
  • fgpyo/util/logging.py (2 hunks)
  • tests/fgpyo/util/test_logging.py (2 hunks)

Walkthrough

The pull request introduces a new method record_alignments to the ProgressLogger class in the fgpyo/util/logging.py file. This method facilitates the logging of multiple pysam.AlignedSegment objects by iterating through an iterable of these records and utilizing the existing record_alignment method. It accepts a single argument, recs, and returns a boolean indicating whether any messages were logged. Additionally, an import statement for Iterable from the typing module has been added.

A corresponding test function, test_record_multiple_alignments, has been added to tests/fgpyo/util/test_logging.py. This function uses SamBuilder to create records, constructs a Template object, and verifies that the logging mechanism accurately records all related entries. An import statement for the Template class has also been included in the test file.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • 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. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @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
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)
tests/fgpyo/util/test_logging.py (1)

88-101: Verifying logging functionality
The test ensures the logger captures all records. Consider adding a scenario where a template has zero records to confirm a no-op.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 017dbc2 and c5791a1.

📒 Files selected for processing (2)
  • fgpyo/util/logging.py (2 hunks)
  • tests/fgpyo/util/test_logging.py (2 hunks)
🔇 Additional comments (5)
tests/fgpyo/util/test_logging.py (3)

7-7: Import is correct
Good job importing Template to test the new functionality.


65-71: Test creation is clear
The test covers mapped, secondary, and supplementary reads. This comprehensive setup ensures thorough testing of complex templates.


77-86: Template construction approach
The manual construction of the expected Template with secondaries and supplementaries is appropriate. No issues found.

fgpyo/util/logging.py (2)

47-47: Importing Template
Needed to reference Template within record_template.


168-184: Method implementation
The method iterates all records in the provided Template and logs each. The logic is straightforward and consistent with record_alignment usage.

@tfenne
Copy link
Member

tfenne commented Dec 24, 2024

I don't object to this, but I wonder if a slightly different approach would work for you and be more general:

def record_alignments(
        self,
        alns: Iterable[AlignedSegment],
    ) -> bool:
        """Correctly record all records in an iterable of alignments.
        Args:
            alns: an iterable of zero or more alignments
        Returns:
            true if a message was logged, false otherwise
        """
        logged_message: bool = False
        for rec in alns:
            logged_message = self.record_alignment(rec) or logged_message
        return logged_message

That could then be called with progress.record(template.all_recs()).

@clintval
Copy link
Member Author

clintval commented Dec 24, 2024

@tfenne that also works. If I went that approach I might suggest making a small API break (in the naming of the parameter only) since we already have a record_alignment() and could support variadic args:

diff --git a/fgpyo/util/logging.py b/fgpyo/util/logging.py
--- a/fgpyo/util/logging.py
+++ b/fgpyo/util/logging.py
@@ -150,20 +150,23 @@ class ProgressLogger(AbstractContextManager):

     def record_alignment(
         self,
-        rec: AlignedSegment,
+        *recs: AlignedSegment,
     ) -> bool:
         """Correctly record pysam.AlignedSegments (zero-based coordinates).

Then usage would be like:

progress.record_alignment(*template.all_recs())

I often have both iterables of alignments to log and templates so I wouldn't mind having both upgrades (variadic support for record_alignment()) and a template logging method that allows me to send in a template-based collection of alignments (record_template()).

Let me know which option you like the most and I can fixup.

@tfenne
Copy link
Member

tfenne commented Dec 27, 2024

Let me know which option you like the most and I can fixup.

I'm not sure I like the variadic arg version without changing the name (e.g. to record_alignment*s*), which is a bigger breaking change. And without that, I think most people would miss it.

But I'd be fine to add a record_alignments and a record_template that just vectors to the former.

@clintval clintval changed the title Add a method for progress logging templates Add a method for progress logging multiple alignments Dec 28, 2024
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)
fgpyo/util/logging.py (1)

171-178: Add usage example to docstring.

Add example showing how to use with BAM records.

 """Correctly record multiple pysam.AlignedSegments (zero-based coordinates).

 Args:
     recs: pysam.AlignedSegment objects

 Returns:
     true if a message was logged, false otherwise
+
+Example:
+    >>> from pysam import AlignmentFile
+    >>> bam = AlignmentFile("input.bam")
+    >>> progress.record_alignments(bam.fetch())
"""
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c5791a1 and c93fc80.

📒 Files selected for processing (2)
  • fgpyo/util/logging.py (2 hunks)
  • tests/fgpyo/util/test_logging.py (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/fgpyo/util/test_logging.py
🔇 Additional comments (2)
fgpyo/util/logging.py (2)

41-41: LGTM!

Import correctly placed with other typing imports.


167-182: Implementation looks solid.

Correctly handles multiple alignments while maintaining logging behavior.

@clintval
Copy link
Member Author

@tfenne I thought about this more and chose to ditch the idea of recording "templates". Simply because I didn't want a user to think the logger was incrementing 1 for every template instead of n for all the records in the template. So I've changed the title of the PR and added a new method called record_alignments() to help out with collections of alignments including templates.

@clintval clintval assigned clintval and unassigned clintval Dec 30, 2024
@clintval clintval merged commit b0b4227 into main Jan 7, 2025
12 checks passed
@clintval clintval deleted the cv_template_logger branch January 7, 2025 01:34
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.

3 participants