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

Allow tracking on Instances (by adding Instance.tracking_score) #1302

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

Conversation

roomrys
Copy link
Collaborator

@roomrys roomrys commented May 5, 2023

Description

Prior to Allow Retracking, we did not allow any tracking on user instances. Since re-tracking enables users to run tracking on user instances, we now need to either add a tracking_score attribute to user-instances, or refrain from assigning the tracking score if the matched instance is a user instance (see #1301). This PR adds an Instance.tracking_score attribute and updates spots where the tracking_score is written (including a FORMAT_ID bump in LabelsV1Adaptor).

Types of changes

  • Bugfix (Should have done this in Allow retracking #867)
  • New feature (Allow tracking on instances)
  • Refactor / Code style update (no logical changes)
  • Build / CI changes
  • Documentation Update
  • Other (explain)

Does this address any currently open issues?

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

  • New Features

    • Enhanced tracking capabilities with consistent recording, display, and persistence of tracking scores across the app.
    • Improved instance tracking logic now prioritizes user-defined instances over automated predictions for greater accuracy.
  • Tests

    • Added automated tests to verify the accurate preservation and functionality of tracking scores during data saving and loading.

@codecov
Copy link

codecov bot commented May 5, 2023

Codecov Report

Attention: Patch coverage is 78.57143% with 3 lines in your changes missing coverage. Please review.

Project coverage is 76.16%. Comparing base (7991f14) to head (5ebf04a).
Report is 97 commits behind head on develop.

Files with missing lines Patch % Lines
sleap/nn/tracking.py 62.50% 3 Missing ⚠️

❌ Your patch check has failed because the patch coverage (78.57%) is below the target coverage (100.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1302      +/-   ##
===========================================
+ Coverage    75.43%   76.16%   +0.73%     
===========================================
  Files          134      134              
  Lines        24749    24812      +63     
===========================================
+ Hits         18670    18899     +229     
+ Misses        6079     5913     -166     

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

@roomrys roomrys added the Re-tracking Spread This PR is part of the re-tracking spread of PRs label May 5, 2023
@roomrys roomrys marked this pull request as ready for review May 5, 2023 18:08
@talmo
Copy link
Collaborator

talmo commented May 5, 2023

Quick question: what happens when there is a user Instance that was created from a PredictedInstance on a frame? Will the user Instance be used preferentially and the PredictedInstance ignored? If not, this may lead to issues where extraneous tracks are generated right?

@roomrys
Copy link
Collaborator Author

roomrys commented May 25, 2023

All instances in the labeled frame were being considered individually. I've added the following to prefer user labeled instances in the frame if any:

sleap/sleap/nn/tracking.py

Lines 1192 to 1213 in 1ea047d

# Prefer user instances over predicted instances
instances = []
if lf.has_user_instances:
instances_to_track = lf.user_instances
if lf.has_predicted_instances:
instances = lf.predicted_instances
else:
instances_to_track = lf.predicted_instances
track_args = {"untracked_instances": instances_to_track}
if tracker.uses_image:
track_args["img"] = lf.video[lf.frame_idx]
else:
track_args["img"] = None
instances.extend(tracker.track(**track_args))
new_lf = LabeledFrame(
frame_idx=lf.frame_idx,
video=lf.video,
instances=instances,
)

@roomrys
Copy link
Collaborator Author

roomrys commented May 25, 2023

Holding off on this PR since it would require a file format update... best to be worn in on the develop branch for a bit before tacking onto the release. This mean that retracking can only be done on prediction-only projects until this PR is merged (i.e. users need to remove their user-labeled instances before retracking to avoid error).

@roomrys roomrys self-assigned this Jan 5, 2024
@gitttt-1234 gitttt-1234 self-requested a review December 16, 2024 22:39
Copy link

coderabbitai bot commented Feb 4, 2025

Walkthrough

This pull request introduces explicit handling of tracking scores across multiple modules. A new tracking_score attribute is added to the Instance class, and associated functions are updated to assign and propagate this value. The changes modify tracking score assignment in occupancy matrix generation, file I/O adaptors (HDF5 and Nix), and the logic used by the tracker to prioritize user-defined instances. A new test function has also been added to verify that tracking scores persist correctly when labels are saved and reloaded.

Changes

File(s) Change Summary
sleap/info/write_tracking_h5.py Updated get_occupancy_and_points_matrices function by adding a line to assign the instance’s tracking_score to the tracking scores array.
sleap/instance.py Added tracking_score: float attribute to Instance and updated __repr__; removed tracking_score from PredictedInstance and modified from_instance and __getitem__ to pass and handle tracking scores properly.
sleap/io/format/hdf5.py, sleap/io/format/nix.py Upgraded HDF5 adaptor’s FORMAT_ID to 1.3 and modified read/write methods (in HDF5) and chunked write (in Nix) to include explicit handling of tracking_score.
sleap/nn/tracking.py Modified the run_tracker function to prioritize user instances over predicted ones and updated the track_args accordingly.
tests/io/test_dataset.py Added a new test function test_instance_cattr to validate that tracking scores are properly saved and loaded within labels.

Sequence Diagram(s)

sequenceDiagram
    participant LF as LabeledFrame
    participant run as run_tracker
    participant Tracker as Tracker
    LF->>run: Provide instances (user & predicted)
    alt User instances present
       run->>run: Select user instances for tracking
       run->>Tracker: Call tracker.track(user instances)
    else No user instances
       run->>Tracker: Call tracker.track(predicted instances)
    end
    Tracker-->>run: Return tracked instances
    run->>LF: Update LabeledFrame with tracked instances
Loading

Possibly related PRs

Suggested labels

2024-hackathon

Suggested reviewers

  • talmo
  • gitttt-1234
  • eberrigan

Poem

I’m a rabbit in a code-filled glen,
Hopping through changes again and again.
Tracking scores now dance in the light,
With steps so nimble and logic so bright.
I nibble on bugs and skip past the fray,
Celebrating new code in my bunny way!
🐰💻

Tip

🌐 Web search-backed reviews and chat
  • We have enabled web search-based reviews and chat for all users. This feature allows CodeRabbit to access the latest documentation and information on the web.
  • You can disable this feature by setting web_search: false in the knowledge_base settings.
  • Please share any feedback in the Discord discussion.

📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 59e1f70 and 5ebf04a.

📒 Files selected for processing (6)
  • sleap/info/write_tracking_h5.py (1 hunks)
  • sleap/instance.py (4 hunks)
  • sleap/io/format/hdf5.py (3 hunks)
  • sleap/io/format/nix.py (1 hunks)
  • sleap/nn/tracking.py (1 hunks)
  • tests/io/test_dataset.py (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (8)
  • GitHub Check: Run tests using wheel (windows-2022)
  • GitHub Check: Run tests using wheel (ubuntu-22.04)
  • GitHub Check: Build package from push (macos-14)
  • GitHub Check: Build package from push (ubuntu-22.04)
  • GitHub Check: Tests (macos-14)
  • GitHub Check: Build package from push (windows-2022)
  • GitHub Check: Tests (windows-2022)
  • GitHub Check: Tests (ubuntu-22.04)
🔇 Additional comments (11)
sleap/info/write_tracking_h5.py (1)

180-180: LGTM! Tracking score assignment is now consistent for all instances.

The change ensures that tracking scores are correctly recorded for both user and predicted instances.

sleap/io/format/nix.py (1)

242-242: LGTM! Tracking score is now consistently written to NIX format.

The change ensures that tracking scores are correctly written to NIX format files for both user and predicted instances.

sleap/io/format/hdf5.py (2)

31-36: LGTM! Format version and documentation updated correctly.

The format version bump and documentation update properly track the addition of tracking scores for Instance objects.


226-228: LGTM! Backward compatibility is maintained for tracking scores.

The conditional assignment ensures backward compatibility by defaulting to 0.0 for older format versions while supporting tracking scores in version 1.3+.

tests/io/test_dataset.py (1)

750-778: LGTM! Comprehensive test coverage for tracking scores.

The test thoroughly verifies:

  • Default tracking score initialization
  • Tracking score modification
  • Persistence through save/load operations
  • Handling for both user and predicted instances
sleap/nn/tracking.py (2)

1566-1584: LGTM! The user instance prioritization logic is well implemented.

The changes correctly implement the prioritization of user instances over predicted instances during tracking, which aligns with the PR objectives. The logic handles both cases appropriately:

  1. When user instances exist: They are assigned to instances_to_track while predicted instances go to instances.
  2. When no user instances exist: Predicted instances are used for tracking.

775-787: LGTM! The tracking score assignment is correctly implemented.

The method properly assigns tracking scores to instances using attr.evolve, which is the recommended way to update attrs objects. The tracking score is correctly propagated from the match score.

sleap/instance.py (4)

373-374: LGTM! The tracking_score attribute is properly defined.

The tracking_score attribute is correctly added to the Instance class with appropriate type conversion and default value.


668-669: LGTM! The tracking score is properly included in the string representation.

The tracking score is correctly added to the __repr__ method with appropriate formatting (2 decimal places).


1458-1458: LGTM! The return type annotation is more accurate.

The return type annotation is correctly updated to Union[Instance, PredictedInstance], which better reflects the actual types that can be returned by this method.


1031-1032: LGTM! The tracking score is properly included in the predicted instance representation.

The tracking score is correctly added to the PredictedInstance.__repr__ method with consistent formatting, matching the base Instance class implementation.

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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

CodeRabbit Commands (Invoked using PR comments)

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

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Re-tracking Spread This PR is part of the re-tracking spread of PRs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants