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

Small improvements to tracking #1894

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
Open

Conversation

getzze
Copy link
Contributor

@getzze getzze commented Aug 2, 2024

Description

Several things were buggy when running tracking-only from GUI or sleap-track from CLI with only tracking:

  • improve the ETA in the GUI progress bar by computing the list of frames before starting the timer.
  • show a progress bar when tracking, like for inference(+tracking)
  • correctly pass default options for tracking to CLI. It seems all tracking options that were not defined were given the None value, which is boolean-False, so it could give the opposite for True-by-default options (there are none so far, fortunately).
  • small bug in GUI video that makes it crash when clicking.
  • switch the default value of save_shifted_instances to True and give back access to setting it from the GUI (removed in Limit max tracks via track-local queues #1447, I don't know why)
  • allow running tracking only on defined frames. It always run on the full video now.
  • [EDIT] fix a bug introduced by Allow csv and text file support on sleap track #1875, where the data_path variable was overwritten.

Types of changes

  • Bugfix
  • New feature
  • Refactor / Code style update (no logical changes)
  • Build / CI changes
  • Documentation Update
  • Other (explain)

Does this address any currently open issues?

None

Outside contributors checklist

  • Review the guidelines for contributing to this repository
  • Read and sign the CLA and add yourself to the authors list
  • Make sure you are making a pull request against the develop branch (not main). Also you should start your branch off develop
  • Add tests that prove your fix is effective or that your feature works
  • Add necessary documentation (if appropriate)

Thank you for contributing to SLEAP!

❤️

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Enhanced tracking capabilities with improved reporting options, allowing for more user control over output.
    • Reintroduced and refined configuration options for saving shifted instances during tracking.
    • Introduced a custom progress rate display for better user feedback during tasks.
  • Bug Fixes

    • Improved error handling in mouse event detection to prevent negative values.
    • Adjusted calculations to ensure non-negative counts during tracking.
  • Refactor

    • Integrated tracking logic directly into the Tracker class, removing standalone functions for better encapsulation.
    • Updated the data processing flow for batch predictions to enhance modularity and clarity.
    • Simplified tracking test logic by removing intermediary functions.
  • Tests

    • Updated tracking tests to incorporate image scaling and parameterized testing for improved coverage.
    • Adjusted expected outcomes in tracking integration tests for accuracy.

Copy link

coderabbitai bot commented Aug 2, 2024

Walkthrough

The recent updates to the Sleap framework enhance tracking and inference capabilities. Key modifications include refined configuration options, improved error handling, and better user experience. Changes optimize performance, introduce new reporting features, and streamline object-oriented design, making the system more maintainable and extendable.

Changes

Files Change Summary
sleap/config/pipeline_form.yaml Removed tracking.max_tracking; reintroduced tracking.save_shifted_instances with updated defaults and help description.
sleap/gui/learning/dialog.py Adjusted no_result_count calculation to prevent negative values, enhancing reliability.
sleap/gui/widgets/video.py Introduced _down_pos for mouse tracking; improved logic in mouseReleaseEvent to ensure error-free click detection.
sleap/nn/inference.py Added methods for batch processing and improved progress reporting; refactored existing logic for clarity and efficiency.
sleap/nn/tracking.py Added verbosity and report_rate attributes; expanded run_tracker method; improved tracking feedback mechanisms.
sleap/util.py Introduced RateColumn for custom progress rendering; reorganized imports for clarity.
tests/nn/test_tracker_components.py Renamed tracker_by_name to run_tracker_by_name; included img_scale parameter for enhanced testing capabilities.
tests/nn/test_tracking_integration.py Updated assertions in test_simple_tracker; refactored tests to utilize instance methods for tracking.

Possibly related PRs

  • Add Keep visualizations checkbox to training GUI #1824: The changes in the main PR regarding the configuration option tracking.save_shifted_instances and the removal of tracking.max_tracking relate to the modifications in the training GUI that introduce a checkbox for retaining prediction visualization images after training, indicating a focus on tracking functionalities.
  • Allow csv and text file support on sleap track #1875: The enhancements to the tracking functionality in the main PR align with the introduction of CSV and text file support in the sleap track tool, as both involve improvements to how tracking data is managed and processed.
  • Add normalized_instance_similarity method  #1939: The addition of the normalized_instance_similarity method in the main PR complements the changes in the tracking system, as both aim to improve the accuracy and reliability of instance matching in tracking processes.

Suggested labels

MultiView Stack

Suggested reviewers

  • roomrys

🐰 In a world of code so bright,
Each change brings a new delight!
With tracking swift and errors few,
Our framework hops to something new.
Here’s to progress, let’s all cheer,
For every byte, we hold so dear! 🌟


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

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 5ab305c and c37b715.

Files selected for processing (8)
  • sleap/config/pipeline_form.yaml (2 hunks)
  • sleap/gui/learning/dialog.py (1 hunks)
  • sleap/gui/widgets/video.py (2 hunks)
  • sleap/nn/inference.py (12 hunks)
  • sleap/nn/tracking.py (15 hunks)
  • sleap/util.py (2 hunks)
  • tests/nn/test_tracker_components.py (2 hunks)
  • tests/nn/test_tracking_integration.py (4 hunks)
Additional context used
Ruff
sleap/util.py

38-38: Undefined name Task

(F821)

sleap/nn/tracking.py

6-6: collections.defaultdict imported but unused

Remove unused import: collections.defaultdict

(F401)


614-614: Use enumerate() for index variable n_processed in for loop

(SIM113)

Additional comments not posted (21)
tests/nn/test_tracking_integration.py (3)

22-22: Verify the new expected value in the assertion.

The assertion has been changed to expect 8 tracks instead of 27. Ensure that this new expected value is correct based on the updated tracking logic or data.


44-44: Verify the tracker.run_tracker method invocation.

Ensure that the tracker object is correctly instantiated and that the run_tracker method handles the verbosity parameter appropriately.


51-51: Verify the tracker.run_tracker method invocation.

Ensure that the tracker object is correctly instantiated and that the run_tracker method handles the verbosity parameter appropriately.

tests/nn/test_tracker_components.py (2)

18-27: Verify the handling of the img_scale parameter and the test skipping logic.

Ensure that the img_scale parameter is correctly applied to the candidate_maker of the tracker and that the function appropriately skips tests when img_scale is not defined.


47-58: Verify the parameterized tests and the passing of the img_scale parameter.

Ensure that the parameterized tests are correctly implemented and that the img_scale parameter is passed appropriately to the run_tracker_by_name function.

sleap/config/pipeline_form.yaml (2)

458-463: New configuration option tracking.save_shifted_instances added.

This option is well-documented and the default value is set to true, which aligns with the PR summary indicating the enhancement of instance matching.


457-457: Verify the impact of removing tracking.max_tracking.

Ensure that the removal of this configuration option does not negatively impact any existing functionality or user workflows.

Verification successful

Verify the integration of tracking.max_tracks and check for any remaining references to tracking.max_tracking.

Ensure that tracking.max_tracks is correctly integrated into the codebase and that there are no remaining dependencies on tracking.max_tracking.


The integration of tracking.max_tracks is correct and there are no remaining dependencies on tracking.max_tracking.

The replacement of tracking.max_tracking with tracking.max_tracks has been successfully integrated into the configuration file, and there are no lingering references to the old configuration.

  • tracking.max_tracks is correctly defined and used in the pipeline_form.yaml file.
  • No remaining references to tracking.max_tracking were found in non-YAML files.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the impact of removing `tracking.max_tracking`.

# Test: Search for the usage of `tracking.max_tracking`. Expect: No occurrences.
rg --type yaml -A 5 $'tracking.max_tracking'

Length of output: 421


Script:

#!/bin/bash
# Description: Verify the integration of `tracking.max_tracks` and check for any remaining references to `tracking.max_tracking`.

# Test 1: Search for the usage of `tracking.max_tracks`. Expect: Relevant occurrences showing correct integration.
rg --type yaml -A 5 $'tracking.max_tracks'

# Test 2: Search for any remaining references to `tracking.max_tracking` in non-YAML files. Expect: No occurrences.
rg --type yaml -v $'tracking.max_tracking'

Length of output: 138158

sleap/gui/learning/dialog.py (1)

739-739: Enhanced robustness in run method.

The calculation of no_result_count now ensures it is non-negative, preventing potential errors in the inference process.

sleap/nn/tracking.py (7)

142-142: Verify the impact of changing the default value of save_shifted_instances.

Changing the default value of save_shifted_instances to True could impact memory usage and performance. Ensure that this change is intentional and tested.


532-548: LGTM!

The new method run_step encapsulates the logic for running a single step of tracking.


549-653: LGTM!

The new method run_tracker encapsulates the tracking logic and enhances reporting capabilities based on verbosity levels.

Tools
Ruff

614-614: Use enumerate() for index variable n_processed in for loop

(SIM113)


1707-1707: LGTM!

The modification aligns with the changes in the BaseTracker class.


520-521: LGTM! Verify the usage of new attributes.

The new attributes verbosity and report_rate enhance the reporting capabilities.

Ensure that these attributes are correctly used and tested throughout the codebase.

Verification successful

Let's modify the script to search for the usage of verbosity and report_rate in all Python files without specifying the file type.


The new attributes verbosity and report_rate are correctly used and tested throughout the codebase.

The attributes are integrated into the main functionality and are covered by tests, ensuring their proper usage.

  • sleap/nn/tracking.py
  • sleap/nn/inference.py
  • tests/nn/test_tracking_integration.py
  • tests/nn/test_inference.py
  • tests/nn/test_tracker_components.py
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of new attributes `verbosity` and `report_rate`.

# Test: Search for the usage of `verbosity` and `report_rate`. Expect: Occurrences of the new attributes.
rg --type python 'verbosity|report_rate'

Length of output: 73


Script:

#!/bin/bash
# Description: Verify the usage of new attributes `verbosity` and `report_rate`.

# Test: Search for the usage of `verbosity` and `report_rate`. Expect: Occurrences of the new attributes.
rg 'verbosity|report_rate' --glob '*.py'

Length of output: 3311


742-747: LGTM! Verify the usage of new attributes.

The new attributes verbosity and report_rate enhance the reporting capabilities.

Ensure that these attributes are correctly used and tested throughout the codebase.

Verification successful

The new attributes verbosity and report_rate are correctly used and tested throughout the codebase.

The search results indicate that both attributes are utilized in various parts of the code, including the sleap/nn/tracking.py, sleap/nn/inference.py, and several test files. This confirms that the new attributes are integrated and tested.

  • Files with relevant usage:
    • sleap/nn/tracking.py
    • sleap/nn/inference.py
    • tests/nn/test_tracking_integration.py
    • tests/nn/test_tracker_components.py
    • tests/nn/test_inference.py
    • sleap/gui/learning/runners.py
    • docs/notebooks/Training_and_inference_on_an_example_dataset.ipynb
    • docs/guides/cli.md
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of new attributes `verbosity` and `report_rate`.

# Test: Search for the usage of `verbosity` and `report_rate`. Expect: Occurrences of the new attributes.
rg --type python 'verbosity|report_rate'

Length of output: 73


Script:

#!/bin/bash
# Description: Verify the usage of new attributes `verbosity` and `report_rate`.

# Test: Search for the usage of `verbosity` and `report_rate` in Python files.
rg --type-add 'py:*.py' 'verbosity|report_rate'

Length of output: 5005


1484-1489: LGTM! Verify the usage of new attributes.

The new attributes verbosity and report_rate enhance the reporting capabilities.

Ensure that these attributes are correctly used and tested throughout the codebase.

Verification successful

The new attributes verbosity and report_rate are correctly used and tested throughout the codebase.

The attributes are integrated into multiple files, including sleap/nn/tracking.py, sleap/nn/inference.py, and various test files, ensuring their proper utilization and testing.

  • Files with usage:
    • sleap/nn/tracking.py
    • sleap/nn/inference.py
    • tests/nn/test_tracker_components.py
    • tests/nn/test_inference.py
    • tests/nn/test_tracking_integration.py
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of new attributes `verbosity` and `report_rate`.

# Test: Search for the usage of `verbosity` and `report_rate`. Expect: Occurrences of the new attributes.
rg --type python 'verbosity|report_rate'

Length of output: 73


Script:

#!/bin/bash
# Description: Verify the usage of new attributes `verbosity` and `report_rate`.

# Test: Search for the usage of `verbosity` and `report_rate`. Expect: Occurrences of the new attributes.
rg --type py 'verbosity|report_rate'

Length of output: 3307

sleap/gui/widgets/video.py (2)

807-808: Initialization of _down_pos is appropriate.

The _down_pos attribute is correctly initialized to None to ensure it is defined before use.


1032-1032: Robustness improved with _down_pos check.

The check for _down_pos being None before comparing it with the current mouse position enhances the robustness of the click detection logic.

sleap/nn/inference.py (4)

48-51: Conditional import for cached_property is appropriate.

The conditional import ensures compatibility with Python versions below 3.8, which is a good practice.


163-165: Use of cached_property for report_period is appropriate.

The cached_property decorator improves performance by caching the computed value of report_period.


5341-5343: Inclusion of example_indices in LabelsReader initialization is beneficial.

The modification enhances the configurability by allowing more flexible handling of input frames based on user-defined indices.


5437-5440: Inclusion of progress_reporting in tracker instantiation is beneficial.

The modification allows for more detailed tracking feedback, enhancing the monitoring of the tracking process.

sleap/nn/tracking.py Outdated Show resolved Hide resolved
sleap/util.py Outdated Show resolved Hide resolved
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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c37b715 and 138c225.

Files selected for processing (1)
  • sleap/nn/tracking.py (15 hunks)
Additional context used
Ruff
sleap/nn/tracking.py

614-614: Use enumerate() for index variable n_processed in for loop

(SIM113)

Additional comments not posted (12)
sleap/nn/tracking.py (12)

4-5: Imports approved.

The additions of json, sys, and rich.progress are appropriate for the new reporting functionality.


520-521: New attributes verbosity and report_rate approved.

These attributes enhance user control over the output of the tracking process.


532-547: New run_step method approved.

This method clears tracks and processes instances for tracking.


742-746: New attributes verbosity and report_rate approved.

These attributes enhance user control over the output of the tracking process.


1484-1488: New attributes verbosity and report_rate approved.

These attributes enhance user control over the output of the tracking process.


1014-1015: New parameters progress_reporting and report_rate approved.

These parameters enhance user control over the output of the tracking process.


Line range hint 1226-1248:
Updated add_cli_parser_args method approved.

The method updates the CLI parser to include the new progress_reporting and report_rate parameters.


1280-1283: Updated get_by_name_factory_options method approved.

The method updates the factory options to include the new progress_reporting and report_rate parameters.


1484-1489: Updated track method approved.

The method enhances the tracking process by incorporating Kalman filters for better accuracy.


1707-1707: Modified retrack function approved.

The modification aligns with the removal of the standalone run_tracker function.

However, ensure that all function calls to retrack match the new implementation.

Verification successful

Modified retrack function approved.

The modification aligns with the removal of the standalone run_tracker function. The function calls to retrack match the new implementation.

  • tests/nn/test_inference.py: Verified test case involving retrack.
  • sleap/nn/tracking.py: Verified function definition and call.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `retrack` match the new implementation.

# Test: Search for the function usage. Expect: Only occurrences of the new implementation.
rg --type python -A 5 $'retrack'

Length of output: 62


Script:

#!/bin/bash
# Description: Verify all function calls to `retrack` match the new implementation.

# Test: Search for the function usage. Expect: Only occurrences of the new implementation.
rg --type py -A 5 $'retrack'

Length of output: 1193


142-142: Verify the impact of changing the default value of save_shifted_instances to True.

This change could affect memory usage and performance.

Verification successful

Verify the impact of changing the default value of save_shifted_instances to True.

This change could affect memory usage and performance.


Verify the impact of changing the default value of save_shifted_instances to True.

The change in the default value of save_shifted_instances to True will increase memory usage due to the saving of shifted instances between frames. However, it is expected to improve instance matching. This behavior is documented and tested in the codebase.

  • sleap/nn/tracking.py: Multiple references to save_shifted_instances show its impact on saving shifted instances.
  • sleap/gui/learning/runners.py: Configuration references.
  • tests/nn/test_inference.py: Test cases covering the new default behavior.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the impact of changing the default value of `save_shifted_instances` to `True`.

# Test: Search for the usage of `save_shifted_instances` in the codebase. Expect: Analyze its impact on memory and performance.
rg --type python -A 5 $'save_shifted_instances'

Length of output: 77


Script:

#!/bin/bash
# Description: Verify the impact of changing the default value of `save_shifted_instances` to `True`.

# Test: Search for the usage of `save_shifted_instances` in the codebase. Expect: Analyze its impact on memory and performance.
rg --type py -A 5 $'save_shifted_instances'

Length of output: 5528


549-653: New run_tracker method approved.

This method includes logic for different verbosity modes, providing visual feedback via a progress bar or JSON-formatted progress updates.

However, ensure that all function calls to run_tracker match the new signature.

Tools
Ruff

614-614: Use enumerate() for index variable n_processed in for loop

(SIM113)

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 138c225 and 7d293c9.

Files selected for processing (8)
  • sleap/config/pipeline_form.yaml (2 hunks)
  • sleap/gui/learning/dialog.py (1 hunks)
  • sleap/gui/widgets/video.py (2 hunks)
  • sleap/nn/inference.py (12 hunks)
  • sleap/nn/tracking.py (24 hunks)
  • sleap/util.py (2 hunks)
  • tests/nn/test_tracker_components.py (2 hunks)
  • tests/nn/test_tracking_integration.py (4 hunks)
Files skipped from review as they are similar to previous changes (5)
  • sleap/config/pipeline_form.yaml
  • sleap/gui/learning/dialog.py
  • sleap/gui/widgets/video.py
  • sleap/nn/inference.py
  • sleap/util.py
Additional context used
Ruff
sleap/nn/tracking.py

614-614: Use enumerate() for index variable n_processed in for loop

(SIM113)

Additional comments not posted (10)
tests/nn/test_tracking_integration.py (3)

22-22: Verify the updated assertion.

The expected number of tracks has been reduced from 27 to 8. Ensure this change is correct and consistent with the updated tracking logic.


44-44: LGTM! Ensure correct initialization of tracker object.

The change to use tracker.run_tracker with verbosity improves encapsulation and maintainability.


51-51: LGTM! Ensure correct initialization of tracker object.

The change to use tracker.run_tracker with verbosity improves encapsulation and maintainability.

tests/nn/test_tracker_components.py (2)

18-28: LGTM! Improved flexibility and error handling.

The addition of the img_scale parameter and the use of pytest.skip() enhance the function's flexibility and robustness.


40-58: LGTM! Enhanced testing coverage.

The inclusion of img_scale as a parameter broadens the scope of the tests to include variations in image scaling.

sleap/nn/tracking.py (5)

520-531: LGTM! Improved user control and reporting.

The addition of verbosity and report_rate attributes, along with the enhanced run_tracker method, improve user control and reporting capabilities.


532-548: LGTM! New method run_step improves modularity.

The run_step method enhances modularity by encapsulating the logic for processing a single frame.


549-654: LGTM! Enhanced run_tracker method.

The run_tracker method now includes logic for different verbosity modes, providing better user feedback during the tracking process.

Tools
Ruff

614-614: Use enumerate() for index variable n_processed in for loop

(SIM113)


704-709: LGTM! Improved user control and reporting.

The addition of verbosity and report_rate attributes improves user control and reporting capabilities.


1009-1010: LGTM! Updated make_tracker_by_name method.

The make_tracker_by_name method now includes progress_reporting and report_rate parameters, enhancing user control over the tracking process.

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 7d293c9 and 2be8b80.

Files selected for processing (3)
  • sleap/nn/tracking.py (23 hunks)
  • tests/nn/test_tracker_components.py (2 hunks)
  • tests/nn/test_tracking_integration.py (5 hunks)
Additional context used
Ruff
sleap/nn/tracking.py

614-614: Use enumerate() for index variable n_processed in for loop

(SIM113)

Additional comments not posted (10)
tests/nn/test_tracking_integration.py (3)

22-22: Verify the change in the expected number of tracks.

The assertion change from 27 to 8 indicates a significant reduction in the number of tracks. Verify if this change aligns with the intended behavior and updated tracking logic.


45-45: LGTM! Verify the handling of the verbosity parameter.

The change to use tracker.run_tracker with an additional verbosity parameter is approved. Ensure that the verbosity parameter is handled correctly within the tracker class.


52-52: LGTM! Verify the handling of the verbosity parameter.

The change to use tracker.run_tracker with an additional verbosity parameter is approved. Ensure that the verbosity parameter is handled correctly within the tracker class.

tests/nn/test_tracker_components.py (2)

18-27: LGTM! Verify the handling of the new img_scale parameter.

The changes to rename the function and include the img_scale parameter enhance flexibility. Ensure that the new parameter and logic are correctly implemented.


40-40: LGTM! Verify the integration of the new img_scale parameter.

The changes to include the img_scale parameter in the tests enhance coverage. Ensure that the new parameter is correctly integrated into the tests.

sleap/nn/tracking.py (5)

141-141: Verify the change in the default value of save_shifted_instances.

The default value of save_shifted_instances is changed from False to True. Verify if this change aligns with the intended functionality and does not adversely impact memory usage and performance.


520-521: LGTM! Verify the implementation of new attributes and methods.

The new attributes (verbosity, report_rate) and methods (run_step, run_tracker) enhance the tracking process. Ensure that they are correctly implemented and integrated.

Also applies to: 527-531, 532-548, 549-654


704-709: LGTM! Verify the implementation of new attributes and modifications.

The new attributes (verbosity, report_rate) and modifications enhance the tracking process. Ensure that they are correctly implemented and integrated.

Also applies to: 742-747


1477-1482: LGTM! Verify the implementation of new attributes.

The new attributes (verbosity, report_rate) enhance the tracking process. Ensure that they are correctly implemented and integrated.


1699-1699: LGTM! Verify the modification to call tracker.run_tracker.

The change to call tracker.run_tracker aligns with the refactor. Ensure that the modification is correctly implemented.

@getzze
Copy link
Contributor Author

getzze commented Aug 2, 2024

Also fixes a bug introduced by #1875, where the data_path variable was overwritten.

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2be8b80 and 98c96dc.

Files selected for processing (1)
  • sleap/nn/inference.py (13 hunks)
Files skipped from review as they are similar to previous changes (1)
  • sleap/nn/inference.py

Copy link

codecov bot commented Aug 2, 2024

Codecov Report

Attention: Patch coverage is 66.47059% with 57 lines in your changes missing coverage. Please review.

Project coverage is 74.95%. Comparing base (7ed1229) to head (2e9b45a).
Report is 53 commits behind head on develop.

Files with missing lines Patch % Lines
sleap/nn/tracking.py 63.41% 30 Missing ⚠️
sleap/nn/inference.py 64.70% 24 Missing ⚠️
sleap/gui/learning/dialog.py 0.00% 1 Missing ⚠️
sleap/gui/widgets/video.py 50.00% 1 Missing ⚠️
sleap/util.py 94.11% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1894      +/-   ##
===========================================
+ Coverage    73.30%   74.95%   +1.64%     
===========================================
  Files          134      133       -1     
  Lines        24087    24344     +257     
===========================================
+ Hits         17658    18247     +589     
+ Misses        6429     6097     -332     

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

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: 3

Outside diff range, codebase verification and nitpick comments (14)
tests/nn/test_tracking_integration.py (2)

131-134: Approve use of os.path.join for file path handling.

The replacement of string concatenation with os.path.join for file path handling is a good improvement. It enhances the robustness of the code across different operating systems.

Consider using an f-string for the filename part to improve readability:

return os.path.join(
    dir,
    f"{tracker_name}_{int(scale * 100)}_{matcher_name}_{sim_name}.h5"
)

40-40: Incomplete refactoring of run_tracker function.

The run_tracker function is still being used directly in the following test files, indicating that the refactoring might not be complete:

  • tests/nn/test_tracking_integration.py
  • tests/nn/test_tracker_components.py

Please review these instances to ensure consistency and alignment with the PR objectives.

Analysis chain

Approve removal of run_tracker function.

The removal of the run_tracker function and its replacement with direct calls to tracker.run_tracker(frames, verbosity="none") in both make_ground_truth and compare_ground_truth functions simplifies the code and aligns with the PR objectives.

To ensure this change doesn't affect test coverage or introduce regressions, consider running the full test suite and comparing the results with the previous version.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for other usages of run_tracker to ensure complete refactoring
rg --type python "run_tracker\(" tests/

Length of output: 72


Script:

#!/bin/bash
# Correcting the file type to search for other usages of run_tracker to ensure complete refactoring
rg --type py "run_tracker\(" tests/

Length of output: 320

tests/nn/test_tracker_components.py (1)

18-32: Improved function with better naming and image scaling support.

The changes to this function are well-implemented:

  1. The new name run_tracker_by_name better reflects its functionality.
  2. The addition of the img_scale parameter increases flexibility.
  3. The error handling for unsupported img_scale is good.

Consider using a custom exception instead of pytest.skip for better error handling outside of test scenarios. For example:

class UnsupportedImageScaleError(Exception):
    pass

# ...

if img_scale:
    if hasattr(t, "candidate_maker") and hasattr(t.candidate_maker, "img_scale"):
        t.candidate_maker.img_scale = img_scale
    else:
        raise UnsupportedImageScaleError("img_scale is not defined for this tracker")

This would allow for more flexible error handling in non-test environments.

sleap/config/pipeline_form.yaml (2)

458-463: Consider adding a performance note to the documentation

The help text for save_shifted_instances mentions improved instance matching at the cost of more memory usage. It might be beneficial to add a note in the user documentation about the performance implications of this option.

Consider adding a brief performance note in the user documentation for this new option. This will help users make informed decisions about whether to enable this feature based on their hardware capabilities and project requirements.


458-463: Potential Impact of Removing tracking.max_tracks

The tracking.max_tracks parameter has been removed from the configuration file, but it is still referenced in several parts of the codebase. This could lead to issues if these parts of the code expect this parameter to be present. Please review the following files and references to ensure consistency and functionality:

  • sleap/nn/tracking.py: Multiple references to max_tracks as a parameter and in tracking logic.
  • sleap/nn/inference.py: Reference to max_tracks in a function call.
  • sleap/gui/learning/runners.py: References to tracking.max_tracks in inference parameter logic.
Analysis chain

Removal of tracking.max_tracks parameter

The tracking.max_tracks parameter has been removed from the configuration. This change might affect users who were relying on this option to limit the number of tracks.

Let's check if there are any references to max_tracks in the codebase that might need to be updated:

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for references to max_tracks in Python files
rg --type python '\bmax_tracks\b' -g '!tests/'

Length of output: 79


Script:

#!/bin/bash
# Corrected command to search for references to max_tracks in Python files
rg -t py '\bmax_tracks\b' -g '!tests/'

Length of output: 1659

sleap/nn/tracking.py (5)

35-38: Conditional import for cached_property added.

The addition of the conditional import for cached_property improves compatibility across different Python versions. This is a good practice for maintaining backwards compatibility while leveraging newer features when available.

Consider adding a comment explaining why cached_property is preferred over regular property when available, to provide context for future maintainers.


520-522: New attributes added for progress reporting.

The addition of verbosity and report_rate attributes to the BaseTracker class supports the enhanced progress reporting feature mentioned in the PR objectives. This will allow for more flexible and customizable tracking progress updates.

Consider adding docstring comments for these new attributes to explain their purpose and expected values, improving the class's documentation.


532-547: New run_step method added to BaseTracker class.

The run_step method encapsulates the logic for processing a single frame in the tracking process. This improves modularity and reusability of the tracking code. The method handles both image-based and non-image-based tracking scenarios, making it versatile.

Consider adding a docstring to the run_step method to explain its purpose, parameters, and return value. This would improve the method's documentation and make it easier for other developers to understand and use.


742-747: Tracker class updated with new progress reporting attributes.

The addition of verbosity and report_rate attributes to the Tracker class ensures consistency with the BaseTracker class. The use of a validator for the verbosity attribute is a good practice to ensure only valid values are used.

Consider adding a validator for the report_rate attribute to ensure it's a positive number, preventing potential issues with invalid reporting rates:

report_rate: float = attr.ib(
    default=2.0,
    validator=attr.validators.instance_of(float) and attr.validators.gt(0)
)

This addition would improve the robustness of the class by preventing invalid input for the reporting rate.


1477-1482: KalmanTracker class updated with progress reporting attributes.

The addition of verbosity and report_rate attributes to the KalmanTracker class ensures consistency with the BaseTracker and Tracker classes. This change maintains a uniform interface for progress reporting across different tracker implementations.

For consistency with the Tracker class, consider using attr.ib() for the report_rate attribute and adding a validator:

report_rate: float = attr.ib(
    default=2.0,
    validator=attr.validators.instance_of(float) and attr.validators.gt(0)
)

This change would align the attribute definition style with the verbosity attribute and add an extra layer of input validation.

sleap/nn/inference.py (4)

433-434: Consider using enumerate for better readability.

Instead of iterating directly over examples, consider using enumerate(examples) to get both the index and the example in each iteration. This can make the code more readable and provide the iteration count if needed.

for i, ex in enumerate(examples):
    ex = process_batch(ex)
    progress.update(task, advance=len(ex["frame_ind"]))

453-454: Consider using enumerate for better readability.

Similar to the previous comment, using enumerate(examples) here could improve readability and provide the iteration count if needed.

for i, ex in enumerate(examples):
    # Process batch of examples.
    ex = process_batch(ex)

490-491: Consider using enumerate for consistency.

For consistency with the previous loops, consider using enumerate(examples) here as well, even if the index is not used.

for i, ex in enumerate(examples):
    yield process_batch(ex)

5663-5663: Consider adding error handling for tracker.run_tracker.

While the change itself is good, it might be beneficial to add some error handling around the tracker.run_tracker(frames=frames) call. This could help catch and report any issues that might occur during the tracking process.

try:
    frames = tracker.run_tracker(frames=frames)
except Exception as e:
    print(f"Error occurred during tracking: {e}")
    # Handle the error appropriately
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 98c96dc and 0bb7f9c.

Files selected for processing (8)
  • sleap/config/pipeline_form.yaml (2 hunks)
  • sleap/gui/learning/dialog.py (1 hunks)
  • sleap/gui/widgets/video.py (2 hunks)
  • sleap/nn/inference.py (13 hunks)
  • sleap/nn/tracking.py (23 hunks)
  • sleap/util.py (2 hunks)
  • tests/nn/test_tracker_components.py (2 hunks)
  • tests/nn/test_tracking_integration.py (5 hunks)
Additional context used
Ruff
sleap/nn/tracking.py

614-614: Use enumerate() for index variable n_processed in for loop

(SIM113)

Additional comments not posted (30)
tests/nn/test_tracking_integration.py (2)

45-45: Approve direct call to tracker.run_tracker.

The replacement of the run_tracker function call with a direct call to tracker.run_tracker(frames, verbosity="none") is consistent with the earlier changes and simplifies the code.


22-22: Verify the change in expected number of tracks.

The assertion for the number of tracks has been significantly reduced from 27 to 8. This change might be related to the improvements in tracking functionality mentioned in the PR objectives.

Could you provide more context on why the expected number of tracks has changed so drastically? This change might have implications for other parts of the system that depend on the number of tracks.

sleap/util.py (7)

6-6: LGTM: Good use of postponed annotation evaluation.

The addition of from __future__ import annotations is a good practice. It enables postponed evaluation of annotations, which can improve performance and reduce circular import issues.


16-16: LGTM: Improved import for type checking.

The addition of TYPE_CHECKING to the import statement is a good practice. It allows for conditional imports that are only evaluated during type checking, which can optimize runtime performance.


20-24: LGTM: Improved import handling and new feature support.

The try-except block for importing files ensures compatibility with different Python versions. The addition of rich.progress import supports the new RateColumn class functionality.

Also applies to: 30-30


36-38: LGTM: Fixed undefined name issue.

The conditional import of Task from rich.progress addresses the issue mentioned in the previous comment about the undefined name Task. It's now properly imported only during type checking.


40-48: LGTM: New feature for improved progress visualization.

The new RateColumn class is a great addition that aligns with the PR objective of improving progress visualization. It provides a custom rendering of the progress rate, displaying the speed in FPS.


Line range hint 315-315: LGTM: Improved path handling.

The modification to use files("sleap").joinpath(filename) is a more modern and flexible approach for creating the package file path.


Line range hint 320-321: LGTM: Enhanced config file handling.

The additions to the get_config_file function provide more flexibility in config file handling. The new parameters ignore_file_not_found and get_defaults, along with the associated logic, allow for ignoring file not found errors and retrieving default configs. This aligns well with the PR objective of improving default options handling.

Also applies to: 332-333, 340-341, 349-350

sleap/config/pipeline_form.yaml (3)

458-463: New option to save shifted instances during tracking

The addition of the tracking.save_shifted_instances parameter aligns with the PR objectives of improving tracking functionality. This new option allows users to save flow-shifted instances between frames, which can enhance instance matching at the cost of increased memory usage.

This change improves the flexibility of the tracking process and gives users more control over the trade-off between accuracy and memory usage.


458-463: Summary of changes to tracking configuration

The modifications to the tracking configuration in this file align with the PR objectives of improving tracking functionality. The addition of the save_shifted_instances option provides users with more control over the tracking process, allowing for a trade-off between improved instance matching and memory usage.

However, the removal of the max_tracks parameter might affect existing users who relied on this option. It's important to ensure that this change doesn't break any existing functionality and that appropriate alternatives are provided if necessary.

These changes enhance the flexibility of the tracking process. However, please consider the following actions:

  1. Update the user documentation to reflect these changes, especially regarding the new save_shifted_instances option and its performance implications.
  2. Verify that the removal of max_tracks doesn't negatively impact existing users or workflows.
  3. If max_tracks functionality is still needed, consider implementing it in a different way or providing clear migration instructions for users.

458-463: Verify the impact of saving shifted instances on memory usage

While the new save_shifted_instances option can improve instance matching, it's important to ensure that the increased memory usage doesn't cause issues in resource-constrained environments.

Let's check if there are any memory-related comments or considerations in the codebase:

sleap/gui/learning/dialog.py (2)

736-736: Approve: Prevent negative count with max function

The modification to use max(0, total_count - new_counts) is a good improvement. It ensures that no_result_count is never negative, which could happen if new_counts somehow exceeded total_count. This change adds robustness to the code and prevents potential issues or confusing output related to negative frame counts.


736-736: Verify similar calculations in the codebase

While this change is good, it might be worth checking if there are similar calculations elsewhere in the codebase that could benefit from the same robustness check.

Let's search for similar patterns in the codebase:

sleap/nn/tracking.py (7)

4-9: Imports updated to support new functionality.

The new imports (json, sys, time) are added to support the enhanced tracking and reporting features. The removal of the unused defaultdict import aligns with the previous review comment and improves code cleanliness.


33-34: New import for progress reporting.

The addition of RateColumn from sleap.util supports the enhanced progress reporting feature in the tracking system.


141-141: Default value for save_shifted_instances changed to True.

This change aligns with the PR objectives, which mentioned changing the default value for the save_shifted_instances option to True. This modification will likely improve the default behavior of instance tracking.


527-531: New cached_property for report_period added.

The addition of the report_period cached_property provides a clean and efficient way to calculate the time interval between progress reports. This improves code readability and potentially performance by caching the calculated value.


1085-1086: Progress reporting parameters added to make_tracker_by_name method.

The addition of progress_reporting and report_rate parameters to the make_tracker_by_name method allows for customization of the progress reporting features when creating a tracker instance. This change provides flexibility in how tracking progress is reported, aligning with the enhanced progress reporting capabilities introduced in this update.


Line range hint 1-1699: Summary of changes and overall assessment.

The modifications in this file significantly enhance the progress reporting capabilities of the tracking system, aligning well with the PR objectives. Key improvements include:

  1. Addition of verbosity modes for flexible progress reporting.
  2. Implementation of a rich progress bar and JSON output options.
  3. Consistent updates across different tracker implementations (BaseTracker, Tracker, KalmanTracker).
  4. New methods for step-by-step tracking with progress updates.

These changes will greatly improve the user experience by providing better feedback during the tracking process. The code is generally well-structured and consistent, with only minor suggestions for improvements in documentation and input validation.

The overall changes are well-implemented and will positively impact the SLEAP application's tracking functionality.

Tools
Ruff

614-614: Use enumerate() for index variable n_processed in for loop

(SIM113)


614-614: False positive from static analysis tool.

The static analysis tool (Ruff) suggests using enumerate() for the index variable n_processed in the for loop. However, this suggestion is not applicable in this context. The variable n_processed is not used as an index for the loop, but rather as a counter of processed frames. The current implementation is correct and more efficient for its purpose.

The current implementation is appropriate for the intended functionality.

Tools
Ruff

614-614: Use enumerate() for index variable n_processed in for loop

(SIM113)

sleap/gui/widgets/video.py (3)

819-820: LGTM: Good addition for mouse event handling.

The new instance variable _down_pos is a good practice for tracking the initial position of the mouse when a button is pressed. This will allow for more precise control over mouse events, such as distinguishing between clicks and drags.


1044-1045: LGTM: Improved mouse movement detection.

The updated logic for detecting mouse movement during a click is more robust. By checking if _down_pos is not None before comparing it with the current position, we prevent potential errors that could occur if _down_pos hasn't been set (e.g., if mouseReleaseEvent is somehow triggered without a corresponding mousePressEvent).


Line range hint 1-1185: Overall: Good improvements to mouse event handling.

The changes in this file are focused on improving the mouse event handling, particularly in the mouseReleaseEvent method. The addition of the _down_pos instance variable and its use in detecting mouse movement during clicks enhance the robustness of the user interaction logic.

These modifications should lead to more reliable distinction between clicks and drags, improving the overall user experience without introducing unnecessary complexity.

sleap/nn/inference.py (6)

49-53: LGTM: Conditional import of cached_property.

This change adds compatibility for Python versions below 3.8 by conditionally importing cached_property. This is a good practice for maintaining backwards compatibility.


415-417: Improved ETA calculation.

Compiling the loop examples before starting the timer is a good optimization for improving the accuracy of the ETA calculation. This change addresses the PR objective of enhancing the ETA calculation in the GUI progress bar.


5388-5390: Updated LabelsReader initialization.

The change in the LabelsReader initialization adds the example_indices parameter, which is derived from frame_list(args.frames). This modification allows for more flexible frame selection during inference, addressing the PR objective of adding functionality to run tracking only on defined frames.


5484-5487: Added progress_reporting to tracker initialization.

The addition of the progress_reporting parameter to the tracker initialization is a good improvement. It allows for consistent progress reporting between the predictor and the tracker.


5655-5660: Improved frame filtering for tracking.

This change improves the efficiency of frame filtering when running tracking on an existing prediction file. By converting the example_indices to a set, the search operation becomes O(1) instead of O(n), which can significantly speed up the process for large datasets.


5684-5684: Use of os.fspath for consistent path handling.

The use of os.fspath(data_path) is a good practice for ensuring consistent path handling across different operating systems. This change improves the robustness of the code.

tests/nn/test_tracker_components.py Show resolved Hide resolved
sleap/nn/tracking.py Outdated Show resolved Hide resolved
sleap/nn/inference.py Show resolved Hide resolved
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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 0bb7f9c and ed8d14b.

Files selected for processing (2)
  • sleap/nn/inference.py (11 hunks)
  • sleap/nn/tracking.py (23 hunks)
Additional context used
Learnings (1)
sleap/nn/inference.py (1)
Learnt from: getzze
PR: talmolab/sleap#1894
File: sleap/nn/inference.py:164-167
Timestamp: 2024-09-03T14:11:31.284Z
Learning: When deciding between `@cached_property` and `@lru_cache`, prefer `@cached_property` when the goal is to ensure that the cached value is automatically deleted when the instance is deleted, avoiding potential memory leaks.
Ruff
sleap/nn/tracking.py

568-568: Use enumerate() for index variable n_processed in for loop

(SIM113)

Additional comments not posted (13)
sleap/nn/tracking.py (9)

141-141: Approved: Default value for save_shifted_instances changed to True.

The default value has been appropriately changed from False to True. As mentioned in the PR objectives, this restores access to the save_shifted_instances setting in the GUI, which was removed in a previous commit.

Saving the shifted instances between elapsed frames uses a bit more memory but leads to better instance matches.


520-521: Approved: New verbosity and report_rate attributes added to BaseTracker.

The addition of the verbosity and report_rate attributes to the BaseTracker class aligns with the PR objective of introducing different verbosity modes for progress reporting during tracking.

  • verbosity likely controls the mode of progress reporting (e.g., "rich", "json", "none").
  • report_rate likely controls the frequency of progress updates.

These attributes will enable more flexible and informative progress reporting during the tracking process.


527-530: Approved: New report_period property added to BaseTracker.

The addition of the report_period property to the BaseTracker class is a useful enhancement. It provides a convenient way to access the time interval between progress reports, calculated based on the report_rate attribute.

Using the @cached_property decorator ensures that the value is calculated only once and cached for subsequent accesses, which can improve performance by avoiding redundant calculations.


549-597: Approved: New _run_tracker_json method added to BaseTracker.

The addition of the _run_tracker_json method to the BaseTracker class is a significant enhancement that implements the "json" verbosity mode for progress reporting during tracking. This aligns with the PR objective of introducing different verbosity modes for progress reporting.

Key features of the _run_tracker_json method:

  • Iterates over the frames, calls run_step on each frame, and yields the new labeled frame.
  • Tracks the progress and timing information, including the number of processed frames, total frames, elapsed time, processing rate, and estimated time remaining (ETA).
  • Reports the progress in JSON format, which can be easily captured and parsed for programmatic monitoring.
  • Uses the max_length argument to limit the length of the deques used for calculating the recent processing rate.

This method will greatly enhance the ability to monitor the progress of the tracking process programmatically, especially in scenarios like headless servers or notebooks.

Tools
Ruff

568-568: Use enumerate() for index variable n_processed in for loop

(SIM113)


599-623: Approved: New _run_tracker_rich method added to BaseTracker.

The addition of the _run_tracker_rich method to the BaseTracker class is another significant enhancement that implements the "rich" verbosity mode for progress reporting during tracking. This aligns with the PR objective of introducing different verbosity modes for progress reporting.

Key features of the _run_tracker_rich method:

  • Uses the rich library to display a visually appealing and informative progress bar during tracking.
  • Iterates over the frames, calls run_step on each frame, and yields the new labeled frame.
  • Updates the progress bar with the current progress percentage, estimated time remaining (ETA), and processing rate.
  • Manually refreshes the progress bar based on the report_period to support notebooks.

This method will greatly improve the user experience by providing a clear and informative visual representation of the tracking progress, especially when running in interactive environments like notebooks.


625-661: Approved: Updated run_tracker method in BaseTracker.

The updates to the run_tracker method in the BaseTracker class are a significant improvement that incorporates the new verbosity modes for progress reporting during tracking. This aligns with the PR objective of introducing different verbosity modes for progress reporting.

Key changes in the updated run_tracker method:

  • Accepts frames, verbosity, and final_pass as arguments.
  • Checks the is_valid property to determine if tracking should be performed.
  • Uses the verbosity argument or the verbosity attribute to determine the progress reporting mode.
  • Delegates the actual tracking and progress reporting to the appropriate _run_tracker_* method based on the verbosity mode.
  • Supports running a final pass after tracking if final_pass is True.

These updates provide flexibility and modularity in the tracking process, allowing users to choose the desired verbosity mode and enabling the integration of different progress reporting mechanisms.


750-754: Approved: Updated verbosity and added report_rate attributes in Tracker.

The changes to the verbosity and report_rate attributes in the Tracker class are in line with the PR objective of introducing different verbosity modes for progress reporting during tracking.

  • The verbosity attribute now has a default value of "none", meaning that by default, no progress reporting will be displayed during tracking.
  • The validator for the verbosity attribute ensures that only valid verbosity modes ("none", "rich", "json") can be assigned.
  • The addition of the report_rate attribute with a default value of 2.0 allows controlling the frequency of progress updates during tracking.

These changes provide flexibility in configuring the progress reporting behavior of the Tracker class, enabling users to choose the desired verbosity mode and update frequency.


1228-1233: Verify the data type consistency for the save_shifted_instances option.

The default value for the save_shifted_instances option in the Tracker.get_by_name_factory_options method has been changed from False to 1. This change aligns with the modification made to the save_shifted_instances attribute in the FlowCandidateMaker class, as mentioned in the PR objectives.

However, there seems to be an inconsistency in the data type used for the default value. The option's data type is specified as int, but using 1 instead of True might cause confusion.

Please ensure that the data type for the save_shifted_instances option is consistently used throughout the codebase. If the intended data type is bool, consider updating the default value to True for clarity.


1247-1250: Approved: Updated help text for the kf_init_frame_count option.

The changes to the help text for the kf_init_frame_count option in the Tracker.get_by_name_factory_options method provide clearer guidance on the behavior of this option.

By explicitly mentioning that setting kf_init_frame_count to 0 means no Kalman filters will be used, the updated help text improves the understanding of this option's functionality and makes it easier for users to configure the tracking behavior correctly.

sleap/nn/inference.py (4)

49-53: LGTM!

The code changes are approved. Conditionally defining cached_property based on the Python version is a good way to maintain compatibility.


164-167: The existing discussion in the comments is still relevant. No need to duplicate the reasoning for using @cached_property here.


371-407: LGTM!

The new _process_batch method looks good. It nicely encapsulates the batch processing logic and correctly handles updating the input dictionary with model predictions and adjusting for potential scaling.


Line range hint 408-523: Looks good!

The new _run_batch_json and _run_batch_rich methods are a nice addition to provide different progress reporting modes. The implementation looks solid, correctly tracking progress, timing and reporting.

Updating _predict_generator to use these methods based on the verbosity setting is the right approach.

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

Outside diff range, codebase verification and nitpick comments (1)
sleap/nn/tracking.py (1)

549-597: Use enumerate() for index variable n_processed.

The static analysis tool suggests using enumerate() for the index variable n_processed in the for loop.

Consider applying this change to improve code readability:

for n_processed, lf in enumerate(frames, start=1):
    new_lf = self.run_step(lf)
    
    # Track timing and progress
    elapsed_all = time() - t0_all
    n_batch += 1
    
    # Report
    if time() > last_report + self.report_period:
        # ... (rest of the code remains the same)
Tools
Ruff

568-568: Use enumerate() for index variable n_processed in for loop

(SIM113)

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ed8d14b and 854432a.

Files selected for processing (2)
  • sleap/nn/inference.py (11 hunks)
  • sleap/nn/tracking.py (23 hunks)
Additional context used
Learnings (1)
sleap/nn/inference.py (1)
Learnt from: getzze
PR: talmolab/sleap#1894
File: sleap/nn/inference.py:164-167
Timestamp: 2024-09-03T14:11:31.284Z
Learning: When deciding between `@cached_property` and `@lru_cache`, prefer `@cached_property` when the goal is to ensure that the cached value is automatically deleted when the instance is deleted, avoiding potential memory leaks.
Ruff
sleap/nn/tracking.py

568-568: Use enumerate() for index variable n_processed in for loop

(SIM113)

Additional comments not posted (14)
sleap/nn/tracking.py (10)

532-661: New run_tracker method added with enhanced progress reporting.

The run_tracker method is a significant addition that implements comprehensive progress reporting for the tracking process. It supports different verbosity modes ("rich", "json", and default) as mentioned in the PR objectives. This enhancement will greatly improve user experience by providing better feedback during the tracking process.

Key improvements:

  1. Support for rich progress bar display
  2. JSON output option for programmatic monitoring
  3. Flexible reporting rate

Consider the following improvements:

  1. Extract the progress reporting logic for each verbosity mode into separate methods to improve readability and maintainability.
  2. Add error handling for potential exceptions during the tracking process.
  3. Consider using a context manager for the "json" verbosity mode to ensure proper resource management, similar to the "rich" mode.
Tools
Ruff

568-568: Use enumerate() for index variable n_processed in for loop

(SIM113)


599-623: LGTM!

The _run_tracker_rich method effectively utilizes the rich library to display an informative and visually appealing progress bar for the "rich" verbosity mode. The code is well-structured and follows best practices.


750-754: LGTM!

The addition of the verbosity and report_rate attributes to the Tracker class aligns with the enhanced progress reporting feature. The validation of the verbosity attribute ensures that only valid verbosity modes are accepted, and the default value of report_rate is reasonable.


1305-1319: LGTM!

The changes to the add_cli_parser_args method are necessary to support the new "tracker" option without a default value, allowing for the detection of malformed command-line arguments. The addition of the other command-line arguments with their default values is consistent with the existing code.


Line range hint 1017-1094: LGTM!

The changes to the make_tracker_by_name method are consistent with the enhanced progress reporting feature. The new progress_reporting argument allows for specifying the desired verbosity mode when creating a Tracker instance, and the verbosity and report_rate attributes are set accordingly. The changes are well-integrated with the existing code.


1228-1233: LGTM!

The change in the default value of the save_shifted_instances option from False to True aligns with the PR objective of restoring access to this setting in the GUI. The updated help text accurately reflects the change in default value and provides clarity on the behavior of the option. The change is consistent with the overall goals of the PR.


1247-1250: LGTM!

The updated help text for the kf_init_frame_count option provides clearer guidance on the behavior of the option. It explicitly states that a value of 0 indicates that no Kalman filters will be used. This clarification improves the usability and understanding of the option. The change is minor but beneficial.


1271-1274: LGTM!

The updated help text for the oks_score_weighting option provides a clearer explanation of the behavior when the value is set to 0 or 1. It specifies that a value of 0 (default) means only the distance between the reference and query keypoint is used to compute the similarity, while a value of 1 means each distance is weighted by the prediction scores of the reference and query keypoint. This clarification enhances the understanding and usability of the option. The change is minor but valuable.


1282-1285: LGTM!

The updated help text for the oks_normalization option provides a detailed explanation of the behavior for different values. It clarifies that a value of "all" means the similarity score is normalized by the number of reference points, "ref" means the score is normalized by the number of visible reference points, and "union" means the score is normalized by the number of points both visible in query and reference instance. This detailed explanation improves the understanding and usability of the option. The change is valuable for users to make informed decisions when configuring the tracker.


1485-1489: LGTM!

The addition of the verbosity and `report_

sleap/nn/inference.py (4)

49-53: LGTM!

The code adds compatibility for Python versions below 3.8 by conditionally defining cached_property. This is a good practice to ensure backward compatibility.


164-167: Good optimization based on learnings!

The change from @property to @cached_property for report_period is a good optimization. As mentioned in the learnings:

When deciding between @cached_property and @lru_cache, prefer @cached_property when the goal is to ensure that the cached value is automatically deleted when the instance is deleted, avoiding potential memory leaks.

This ensures that the cached value is automatically deleted when the instance is deleted, avoiding potential memory leaks.


371-407: Improved modularity and clarity!

The new _process_batch method encapsulates the logic for running inference on a batch and updating the input dictionary with predictions. This improves modularity and clarity by extracting the inline processing logic into a dedicated method. The method handles important postprocessing steps such as:

  • Skipping inference if the model is not loaded
  • Running inference on the current batch
  • Adding model outputs to the input data example
  • Converting video_ind and frame_ind to numpy arrays if they are TensorFlow tensors
  • Adjusting instance_peaks based on potential SizeMatcher scaling

The code is well-structured and follows good practices.


508-523: Improved ETA calculation and flexible progress reporting!

The changes introduce several improvements to the inference loop:

  1. Compiling loop examples before starting the timer improves the ETA calculation by providing a more accurate estimate of the total number of examples. This enhances the user experience by providing a more reliable progress estimate.

  2. The optional progress reporting based on the verbosity attribute allows for flexibility in how progress is displayed:

    • The "rich" option displays an informative and visually appealing progress bar using the _run_batch_rich method.
    • The "json" option outputs progress information as JSON using the _run_batch_json method, which can be useful for programmatic monitoring or logging.
    • The "none" option allows for silent execution without any progress reporting, suitable for scenarios where progress updates are not needed.

The code is well-organized and uses appropriate methods for different verbosity levels, making it easy to understand and maintain.

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 854432a and 25cd34b.

Files selected for processing (8)
  • sleap/config/pipeline_form.yaml (2 hunks)
  • sleap/gui/learning/dialog.py (1 hunks)
  • sleap/gui/widgets/video.py (2 hunks)
  • sleap/nn/inference.py (11 hunks)
  • sleap/nn/tracking.py (23 hunks)
  • sleap/util.py (2 hunks)
  • tests/nn/test_tracker_components.py (2 hunks)
  • tests/nn/test_tracking_integration.py (5 hunks)
Files skipped from review as they are similar to previous changes (4)
  • sleap/config/pipeline_form.yaml
  • sleap/gui/learning/dialog.py
  • sleap/gui/widgets/video.py
  • sleap/util.py
Additional context used
Learnings (1)
sleap/nn/inference.py (1)
Learnt from: getzze
PR: talmolab/sleap#1894
File: sleap/nn/inference.py:164-167
Timestamp: 2024-09-03T14:11:31.284Z
Learning: When deciding between `@cached_property` and `@lru_cache`, prefer `@cached_property` when the goal is to ensure that the cached value is automatically deleted when the instance is deleted, avoiding potential memory leaks.
Ruff
sleap/nn/tracking.py

568-568: Use enumerate() for index variable n_processed in for loop

(SIM113)

Additional comments not posted (22)
tests/nn/test_tracking_integration.py (4)

22-22: Verify the change in the expected number of tracks.

The assertion in the test_simple_tracker function has been updated to expect 8 tracks instead of 27. This change suggests a potential adjustment in the underlying tracking logic or the data being processed.

Please confirm that this change aligns with the intended behavior and provide a brief explanation for the adjustment in the expected number of tracks.


45-45: Verify the impact of removing the run_tracker function.

The run_tracker function has been removed, and its functionality has been replaced by a direct call to tracker.run_tracker(frames, verbosity="none") in the make_ground_truth function. While this change simplifies the code, it's important to ensure that it doesn't introduce any unintended side effects or break existing functionality.

Please confirm that the removal of the run_tracker function and the direct utilization of the tracker's method have been thoroughly tested and do not negatively impact the overall behavior of the code.


52-52: Verify the impact of removing the run_tracker function in the compare_ground_truth function.

Similar to the change in the make_ground_truth function, the run_tracker function has been removed, and its functionality has been replaced by a direct call to tracker.run_tracker(frames, verbosity="none") in the compare_ground_truth function. While this change simplifies the code, it's important to ensure that it doesn't introduce any unintended side effects or break existing functionality.

Please confirm that the removal of the run_tracker function and the direct utilization of the tracker's method in the compare_ground_truth function have been thoroughly tested and do not negatively impact the overall behavior of the code.


131-133: LGTM!

The modification in the make_filename function, where the string concatenation for the file path has been replaced with os.path.join, improves the robustness of file path handling across different operating systems. This change enhances the portability and maintainability of the code.

tests/nn/test_tracker_components.py (3)

18-32: LGTM!

The new run_tracker_by_name function is well-structured and handles the img_scale parameter appropriately. Skipping the tracking process when img_scale is not supported is a good practice to prevent unnecessary execution. The assertion at the end ensures that the tracking process produces the expected number of frames.


40-40: Also applies to: 47-47, 53-58


Line range hint 77-84:

sleap/nn/tracking.py (11)

625-661: New run_tracker method added with enhanced progress reporting.

The run_tracker method is a significant addition that implements comprehensive progress reporting for the tracking process. It supports different verbosity modes ("rich", "json", and default) as mentioned in the PR objectives. This enhancement will greatly improve user experience by providing better feedback during the tracking process.


599-623: LGTM!

The _run_tracker_rich method is well-implemented and provides a rich progress bar for tracking progress. It uses a context manager to ensure proper resource management.


532-547: LGTM!

The run_step method is correctly implemented and clears the tracks before running the tracking step.


527-530: LGTM!

The report_period cached property is correctly implemented and uses the report_rate attribute to calculate the report period.


750-754: LGTM!

The new verbosity and report_rate attributes are correctly added to the Tracker class and will be used for progress reporting during tracking.


1017-1018: LGTM!

The make_tracker_by_name class method correctly handles the new progress_reporting and report_rate arguments and passes them to the Tracker constructor.


1228-1233: LGTM!

The get_by_name_factory_options class method correctly updates the default value of save_shifted_instances to 1. This change aligns with the PR objective of restoring access to this setting in the GUI.


1305-1312: LGTM!

The add_cli_parser_args class method correctly handles the case when the default value is defined for "tracking.tracker" by not passing the default value to the argument parser. This change prevents the detection of malformed command line arguments.


1485-1489: LGTM!

The new verbosity and report_rate attributes are correctly added to the KalmanTracker class and will be used for progress reporting during tracking.


1707-1707: LGTM!

The run_tracker method of the KalmanTracker class correctly passes the frames argument to the init_tracker.run_tracker method.


568-568: Skip the Ruff suggestion.

The suggestion to use enumerate() for index variable n_processed is not applicable in this case because n_processed is not used as an index variable in the for loop. It is a counter variable that is incremented in each iteration of the loop. Using enumerate() would not provide any benefit here.

Tools
Ruff

568-568: Use enumerate() for index variable n_processed in for loop

(SIM113)

sleap/nn/inference.py (4)

164-167: LGTM!

The code changes are approved. Using @cached_property is the right choice here to avoid potential memory leaks.


371-407: Looks good!

The new _process_batch function correctly handles running inference on a batch and postprocessing the results. The logic for skipping inference when the model is not loaded, running inference, updating the input dictionary, and adjusting for SizeMatcher scaling is implemented properly.


408-455: LGTM!

The new _run_batch_json function correctly processes batches of examples and reports the progress in JSON format. The logic for tracking timing and progress, computing the recent rate and ETA, and printing the JSON progress information at the specified report period is implemented properly.


456-486: Looks good!

The new _run_batch_rich function correctly processes batches of examples and reports the progress using the rich progress bar. The setup of the rich progress bar with custom configuration, updating the progress for each batch, and manually refreshing the progress bar to support notebooks is implemented properly.

sleap/nn/tracking.py Show resolved Hide resolved
@getzze
Copy link
Contributor Author

getzze commented Sep 4, 2024

It's ready for review!

sleap/config/pipeline_form.yaml Show resolved Hide resolved
sleap/config/pipeline_form.yaml Outdated Show resolved Hide resolved
sleap/gui/widgets/video.py Show resolved Hide resolved
tests/nn/test_tracking_integration.py Outdated Show resolved Hide resolved
@talmo
Copy link
Collaborator

talmo commented Sep 12, 2024

Thanks for the updates @getzze! Just keep us posted on when it's ready for review again.

@getzze
Copy link
Contributor Author

getzze commented Sep 12, 2024

It's ready to review!

@gitttt-1234 gitttt-1234 self-requested a review September 20, 2024 19:20
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: 11

🧹 Outside diff range and nitpick comments (3)
sleap/util.py (1)

40-48: LGTM: Well-implemented RateColumn class for progress tracking.

The RateColumn class is a good addition for displaying the progress rate in FPS. It handles edge cases well and provides a clear, formatted output.

Consider adding a brief comment explaining the meaning of FPS in this context, as it might not be immediately clear to all developers that it refers to "Frames Per Second" in this tracking scenario. For example:

return rich.progress.Text(f"{speed:.1f} FPS", style="progress.data.speed")  # FPS: Frames Per Second
sleap/config/pipeline_form.yaml (1)

458-463: LGTM! Consider a minor improvement in the help text.

The addition of the tracking.save_shifted_instances option is well-placed and aligns with the PR objectives. The configuration is clear and the default value is set correctly.

Consider slightly rewording the help text for clarity:

-      help: 'Save the flow-shifted instances between elapsed frames. It improves
-      instance matching at the cost of using a bit more of memory.'
+      help: 'Save the flow-shifted instances between elapsed frames. This improves
+      instance matching at the cost of using more memory.'

This minor change improves readability and precision in describing the memory impact.

sleap/nn/inference.py (1)

51-51: Typographical error in comment

There's a typo in the comment on line 51: "define" should be "defined".

Apply this diff to correct the typo:

-else:  # cached_property is define only for python >=3.8
+else:  # cached_property is defined only for python >=3.8
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 10799b9 and 809e580.

📒 Files selected for processing (6)
  • sleap/config/pipeline_form.yaml (1 hunks)
  • sleap/nn/inference.py (10 hunks)
  • sleap/nn/tracking.py (13 hunks)
  • sleap/util.py (1 hunks)
  • tests/nn/test_tracker_components.py (3 hunks)
  • tests/nn/test_tracking_integration.py (4 hunks)
🧰 Additional context used
📓 Learnings (1)
sleap/nn/inference.py (1)

<retrieved_learning>
Learnt from: getzze
PR: #1894
File: sleap/nn/inference.py:164-167
Timestamp: 2024-09-03T14:11:31.284Z
Learning: When deciding between @cached_property and @lru_cache, prefer @cached_property when the goal is to ensure that the cached value is automatically deleted when the instance is deleted, avoiding potential memory leaks.
</retrieved_learning>

🪛 Ruff
sleap/nn/tracking.py

571-571: Use enumerate() for index variable n_processed in for loop

(SIM113)

sleap/util.py

8-8: base64 imported but unused

Remove unused import: base64

(F401)


31-31: PIL.Image imported but unused

Remove unused import: PIL.Image

(F401)

🔇 Additional comments (14)
tests/nn/test_tracking_integration.py (4)

5-5: LGTM: Import Path from pathlib

The addition of from pathlib import Path is a good change. It aligns with the project's move towards using pathlib for cross-platform path handling.


46-46: LGTM: Simplified tracker execution

The direct use of tracker.run_tracker(frames, verbosity="none") in both make_ground_truth and compare_ground_truth functions is a good simplification. It removes the unnecessary intermediate run_tracker function, making the code more straightforward.

Also applies to: 53-53


134-136: LGTM: Improved path handling

The use of Path.joinpath for constructing the file path is an excellent change. It improves cross-platform compatibility and follows the project's move towards using pathlib for path handling.


23-23: Verify the change in expected number of tracks

The assertion for the number of tracks has been significantly reduced from 27 to 8. This change might reflect improvements in the tracking algorithm or changes in its configuration.

Please confirm:

  1. Is this change intentional?
  2. What improvements or changes in the tracking algorithm led to this reduction?
  3. Are there any implications for the overall tracking performance?
sleap/util.py (2)

29-29: LGTM: New imports for progress tracking and type hinting.

The addition of rich.progress import and the conditional import of Task for type hinting are appropriate for the new RateColumn class and improve type checking without affecting runtime performance.

Also applies to: 36-37


1-49: Summary: Enhancements align well with PR objectives.

The changes in this file, particularly the addition of the RateColumn class, contribute significantly to the PR's goal of improving tracking functionality and progress display in the SLEAP application. These modifications lay the groundwork for implementing the progress bar during tracking, as outlined in the PR objectives.

The new imports and class definition are well-structured and follow best practices. With the suggested minor improvements (removing unused imports and adding a clarifying comment), this file will be in excellent shape to support the enhanced tracking features.

🧰 Tools
🪛 Ruff

8-8: base64 imported but unused

Remove unused import: base64

(F401)


31-31: PIL.Image imported but unused

Remove unused import: PIL.Image

(F401)

tests/nn/test_tracker_components.py (2)

18-32: Function run_tracker_by_name implemented correctly

The new run_tracker_by_name function correctly replaces tracker_by_name and appropriately handles the img_scale parameter.


50-61: Parameters added to test_tracker_by_name appropriately

The addition of img_scale and count parameters to test_tracker_by_name is appropriate and correctly integrated into the test function.

sleap/nn/tracking.py (5)

4-8: LGTM on added imports for progress reporting functionality

The newly added imports are appropriate and necessary for the implementation of progress reporting features.


12-13: Importing rich.progress enhances progress bar capabilities

The addition of import rich.progress is essential for the rich progress bar implementation in the tracking process.


602-627: New method _run_tracker_rich adds rich progress bar display

The _run_tracker_rich method introduces a rich progress bar during tracking, significantly improving user experience by providing visual feedback.


552-601: New method _run_tracker_json provides JSON-based progress reporting

The implementation of the _run_tracker_json method effectively adds JSON-based progress reporting, enabling programmatic monitoring of the tracking process.

🧰 Tools
🪛 Ruff

571-571: Use enumerate() for index variable n_processed in for loop

(SIM113)


628-665: Updates to run_tracker method to incorporate verbosity options

The run_tracker method now supports different verbosity levels, allowing for flexible progress reporting tailored to various use cases.

sleap/nn/inference.py (1)

508-509: Confirm that data_provider supports len()

In the line n_total = len(data_provider), ensure that data_provider implements the __len__ method. If not, this will raise a TypeError. Verify that data_provider supports the length operation.


Try not to put things in here unless they really have no other place.
"""

from __future__ import annotations

import base64
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Remove unused imports to improve code cleanliness.

The following imports are not used in this file and should be removed:

  • base64 (line 8)
  • PIL.Image (line 31)

Apply this diff to remove the unused imports:

-import base64
-from PIL import Image

This will help maintain a clean and efficient codebase by removing unnecessary dependencies.

Also applies to: 31-31

🧰 Tools
🪛 Ruff

8-8: base64 imported but unused

Remove unused import: base64

(F401)

Comment on lines +27 to +28
pytest.skip("img_scale is not defined for this tracker")
return
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Unnecessary return statement after pytest.skip

The pytest.skip function raises an exception and halts the test execution. The return statement following it is redundant and can be removed.

Apply this diff to remove the unnecessary return statement:

    pytest.skip("img_scale is not defined for this tracker")
-    return
📝 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
pytest.skip("img_scale is not defined for this tracker")
return
pytest.skip("img_scale is not defined for this tracker")

Comment on lines +530 to +534
@cached_property
def report_period(self) -> float:
"""Time between progress reports in seconds."""
return 1.0 / self.report_rate

Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Ensure report_rate is non-zero to prevent division by zero

In the report_period property, dividing by self.report_rate could result in a division by zero if report_rate is zero. Please add validation to ensure report_rate is not zero.

Apply this diff to add validation:

     @cached_property
     def report_period(self) -> float:
         """Time between progress reports in seconds."""
+        if self.report_rate == 0:
+            raise ValueError("report_rate must be non-zero")
         return 1.0 / self.report_rate
📝 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
@cached_property
def report_period(self) -> float:
"""Time between progress reports in seconds."""
return 1.0 / self.report_rate
@cached_property
def report_period(self) -> float:
"""Time between progress reports in seconds."""
if self.report_rate == 0:
raise ValueError("report_rate must be non-zero")
return 1.0 / self.report_rate

Comment on lines +1027 to +1028
progress_reporting: str = "rich",
report_rate: float = 2.0,
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Align default verbosity values across classes for consistency

The default value for progress_reporting in make_tracker_by_name is 'rich', whereas the default verbosity in Tracker and KalmanTracker classes is 'none'. This inconsistency might lead to unexpected behavior. Consider setting the default verbosity in Tracker and KalmanTracker to 'rich' for consistency.

Apply this diff to align the default values:

# In the Tracker class
 verbosity: str = attr.ib(
     validator=attr.validators.in_(["none", "rich", "json"]),
-    default="none",
+    default="rich",
 )

# In the KalmanTracker class
 verbosity: str = attr.ib(
     validator=attr.validators.in_(["none", "rich", "json"]),
-    default="none",
+    default="rich",
 )

Committable suggestion was skipped due to low confidence.

Comment on lines +566 to +573
for lf in frames:
new_lf = self.run_step(lf)

# Track timing and progress
elapsed_all = time() - t0_all
n_processed += 1
n_batch += 1

Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Simplify loop by using enumerate for n_processed

You can simplify the loop by using enumerate to automatically increment n_processed, enhancing code readability.

Apply this diff to refactor the loop:

     for n_processed, lf in enumerate(frames, start=1):
         new_lf = self.run_step(lf)

         # Track timing and progress
         elapsed_all = time() - t0_all
-        n_processed += 1
         n_batch += 1
📝 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
for lf in frames:
new_lf = self.run_step(lf)
# Track timing and progress
elapsed_all = time() - t0_all
n_processed += 1
n_batch += 1
for n_processed, lf in enumerate(frames, start=1):
new_lf = self.run_step(lf)
# Track timing and progress
elapsed_all = time() - t0_all
n_batch += 1
🧰 Tools
🪛 Ruff

571-571: Use enumerate() for index variable n_processed in for loop

(SIM113)

Comment on lines +37 to +40
if sys.version_info >= (3, 8):
from functools import cached_property
else: # cached_property is define only for python >=3.8
cached_property = property
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Use a proper fallback for cached_property in Python < 3.8

Replacing cached_property with property in versions below Python 3.8 does not replicate the caching behavior, which may lead to performance issues. Consider using functools.lru_cache as a fallback to maintain similar functionality.

Additionally, there's a typographical error in the comment on line 39: "define" should be "defined".

Apply this diff to fix the issue:

 import functools

 if sys.version_info >= (3, 8):
     from functools import cached_property
 else:  # cached_property is defined only for python >=3.8
-    cached_property = property
+    def cached_property(func):
+        return property(functools.lru_cache()(func))
📝 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
if sys.version_info >= (3, 8):
from functools import cached_property
else: # cached_property is define only for python >=3.8
cached_property = property
import functools
if sys.version_info >= (3, 8):
from functools import cached_property
else: # cached_property is defined only for python >=3.8
def cached_property(func):
return property(functools.lru_cache()(func))

@@ -167,7 +161,7 @@ class Predictor(ABC):
report_rate: float = attr.ib(default=2.0, kw_only=True)
model_paths: List[str] = attr.ib(factory=list, kw_only=True)

@property
@cached_property
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Handle potential ZeroDivisionError when computing report_period

Since report_period is calculated as 1.0 / self.report_rate, if self.report_rate is zero, it will raise a ZeroDivisionError. Ensure that self.report_rate is initialized to a non-zero value or add a check to prevent division by zero.

Comment on lines +434 to +435
rate = sum(n_recent) / sum(elapsed_recent)
eta = (n_total - n_processed) / rate
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Prevent division by zero in rate calculation

In the calculation of rate = sum(n_recent) / sum(elapsed_recent), there's a possibility of ZeroDivisionError if sum(elapsed_recent) is zero. Ensure that elapsed_recent contains non-zero values or add a condition to handle the zero case.

Apply this diff to handle the zero division case:

-rate = sum(n_recent) / sum(elapsed_recent)
+if sum(elapsed_recent) > 0:
+    rate = sum(n_recent) / sum(elapsed_recent)
+else:
+    rate = 0
📝 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
rate = sum(n_recent) / sum(elapsed_recent)
eta = (n_total - n_processed) / rate
if sum(elapsed_recent) > 0:
rate = sum(n_recent) / sum(elapsed_recent)
else:
rate = 0
eta = (n_total - n_processed) / rate

Comment on lines +393 to +397
if isinstance(ex["video_ind"], tf.Tensor):
ex["video_ind"] = ex["video_ind"].numpy().flatten()
if isinstance(ex["frame_ind"], tf.Tensor):
ex["frame_ind"] = ex["frame_ind"].numpy().flatten()

Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Verify existence of keys before accessing dictionary items

When accessing ex["video_ind"] and ex["frame_ind"], ensure that these keys exist in the ex dictionary to prevent potential KeyError. Consider using ex.get("video_ind") and ex.get("frame_ind") with default values or add checks before accessing.

Apply this diff to safely access the keys:

-if isinstance(ex["video_ind"], tf.Tensor):
+if "video_ind" in ex and isinstance(ex["video_ind"], tf.Tensor):

-if isinstance(ex["frame_ind"], tf.Tensor):
+if "frame_ind" in ex and isinstance(ex["frame_ind"], tf.Tensor):
📝 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
if isinstance(ex["video_ind"], tf.Tensor):
ex["video_ind"] = ex["video_ind"].numpy().flatten()
if isinstance(ex["frame_ind"], tf.Tensor):
ex["frame_ind"] = ex["frame_ind"].numpy().flatten()
if "video_ind" in ex and isinstance(ex["video_ind"], tf.Tensor):
ex["video_ind"] = ex["video_ind"].numpy().flatten()
if "frame_ind" in ex and isinstance(ex["frame_ind"], tf.Tensor):
ex["frame_ind"] = ex["frame_ind"].numpy().flatten()

Comment on lines +49 to +52
if sys.version_info >= (3, 8):
from functools import cached_property
else: # cached_property is define only for python >=3.8
cached_property = property
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Ensure consistent caching behavior across Python versions

Currently, the fallback for Python versions earlier than 3.8 assigns cached_property = property, which means that caching will not occur and report_period will be recalculated each time it's accessed. To maintain the intended caching behavior across all supported Python versions, consider using the cached_property backport available in the backports.cached_property package.

Apply this diff to fix the issue:

-if sys.version_info >= (3, 8):
-    from functools import cached_property
-else:  # cached_property is define only for python >=3.8
-    cached_property = property
+try:
+    from functools import cached_property
+except ImportError:
+    from backports.cached_property import cached_property
📝 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
if sys.version_info >= (3, 8):
from functools import cached_property
else: # cached_property is define only for python >=3.8
cached_property = property
try:
from functools import cached_property
except ImportError:
from backports.cached_property import cached_property

@gitttt-1234
Copy link
Contributor

Hi @getzze ,

I rebased you branch off of our updated develop branch and I noticed there are few errors in the tests because of the new features added. (One of the main addition is having a new parameter img_hw to the Tracker.track() method) Since I don't have access to your branch, could you please resolve the issues so that we could merge this PR to develop?

Thanks,

Divya

@getzze
Copy link
Contributor Author

getzze commented Sep 30, 2024

Hi @gitttt-1234,
I just pushed the changes.

Thanks

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

🧹 Outside diff range and nitpick comments (1)
sleap/nn/tracking.py (1)

37-40: Typographical error in comment on line 39.

The comment on line 39 contains a typo: "define" should be "defined".

Apply this diff to correct the typo:

-else:  # cached_property is define only for python >=3.8
+else:  # cached_property is defined only for python >=3.8
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 809e580 and 2e9b45a.

📒 Files selected for processing (3)
  • sleap/nn/tracking.py (14 hunks)
  • tests/nn/test_tracker_components.py (3 hunks)
  • tests/nn/test_tracking_integration.py (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • tests/nn/test_tracker_components.py
  • tests/nn/test_tracking_integration.py
🧰 Additional context used
🪛 Ruff
sleap/nn/tracking.py

572-572: Use enumerate() for index variable n_processed in for loop

(SIM113)

🔇 Additional comments (14)
sleap/nn/tracking.py (14)

4-9: Imports are appropriately added for new functionality.

The imports of json, sys, deque, time, and typing annotations are necessary for the added methods and enhancements.


12-13: Necessary imports numpy and rich.progress added.

These imports are required for numerical computations and enhanced progress reporting.


35-36: Import of RateColumn for progress bar display.

The RateColumn is used in the rich progress bar to display processing rates.


523-525: Duplicate comment: Missing default values and validators for verbosity and report_rate.


530-534: Duplicate comment: Potential division by zero in report_period due to report_rate being zero.


553-602: The _run_tracker_json method is correctly implemented for JSON progress reporting.

The method effectively handles progress calculations and outputs, providing useful feedback during tracking.

🧰 Tools
🪛 Ruff

572-572: Use enumerate() for index variable n_processed in for loop

(SIM113)


603-628: The _run_tracker_rich method utilizes rich library effectively for progress display.

The implementation is appropriate and follows best practices for progress bar integration.


630-666: run_tracker method incorporates verbosity options and handles execution flow correctly.

The method correctly delegates to the appropriate progress reporting method based on the verbosity setting.


755-760: Added verbosity and report_rate with validation in Tracker class.

Including validators and default values ensures robust usage of these attributes.


1029-1030: Duplicate comment: Inconsistent default verbosity values across classes.


1105-1106: Passing verbosity and report_rate to the tracker instance.

This ensures that the configured verbosity and report rate are correctly applied.


1497-1502: Added verbosity and report_rate with validation in KalmanTracker class.

Consistent implementation with the Tracker class.


1566-1566: The track method in KalmanTracker correctly implements the abstract base class signature.

Ensures compatibility with the BaseTracker.


1720-1720: Updated retrack function to use run_tracker method.

This change aligns the main function with the new tracking workflow.

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.

4 participants