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

Remove kwargs and mutable defaults #48

Merged
merged 28 commits into from
Jun 3, 2024

Conversation

aaprasad
Copy link
Contributor

@aaprasad aaprasad commented May 21, 2024

As per #40 and #43, mutable defaults are very dangerous and kwargs are opaque. Thus we should never use mutable defaults and only use kwargs when absolutely necessary (i.e. for downstream function params). Thus here we remove any usage of mutable defaults and remove the use of kwargs from `GlobalTrackingTransformer

Summary by CodeRabbit

  • Refactor

    • Updated import paths for Frame and Instance classes across multiple modules for better modularity and code organization.
    • Enhanced the GlobalTrackingTransformer to handle lists of Instance objects instead of Frame objects.
  • New Features

    • Introduced AssociationMatrix class for managing association scores between detections.
    • Added new methods for data manipulation and conversion in Frame and Instance classes.
  • Documentation

    • Updated documentation references to reflect new module paths for data structures.

…`Instance` classes into separate modules within `io`
…`Instance` classes into separate modules within `io`
Copy link
Contributor

coderabbitai bot commented May 21, 2024

Warning

Review failed

The pull request is closed.

Walkthrough

The changes primarily involve updating import paths for Frame and Instance classes across multiple files in the biogtr project. These classes have been moved from biogtr.data_structures to biogtr.io.frame and biogtr.io.instance, respectively. Additionally, new functionalities and methods have been introduced in the biogtr/io module to enhance data handling and manipulation capabilities.

Changes

File(s) Change Summary
biogtr/datasets/base_dataset.py Updated import for Frame from biogtr.data_structures to biogtr.io.frame.
biogtr/datasets/cell_tracking_dataset.py Updated imports for Frame and Instance from biogtr.data_structures to biogtr.io.frame and biogtr.io.instance.
biogtr/datasets/eval_dataset.py Updated imports for Frame and Instance to reference biogtr/io.
biogtr/datasets/microscopy_dataset.py Changed imports and documentation references from biogtr.data_structures to biogtr.io.
biogtr/datasets/sleap_dataset.py Restructured imports for Frame and Instance to new module paths.
biogtr/inference/metrics.py Updated import for Frame and docstring references to biogtr.io.
biogtr/inference/track.py Adjusted imports for Config and Frame to new locations within biogtr/io.
biogtr/inference/track_queue.py Updated import for Frame from biogtr.data_structures to biogtr.io.frame.
biogtr/inference/tracker.py Updated imports and method signatures to reference Frame from biogtr/io. Modified tracking logic and method calls.
biogtr/io/__init__.py Introduced new classes and methods for handling Frame, Instance, AssociationMatrix, and Track.
biogtr/io/association_matrix.py Added AssociationMatrix class with methods for matrix manipulation and conversion.
biogtr/io/config.py Modified get_model and get_gtr_runner methods to handle ckpt_path parameter.
biogtr/io/frame.py Introduced Frame class with methods for metadata handling and conversion.
biogtr/io/instance.py Introduced Instance class with methods for data conversion and manipulation.
biogtr/io/track.py Introduced Track class for storing and managing track instances.
biogtr/models/global_tracking_transformer.py Updated GlobalTrackingTransformer class to use torch.nn.Module and modified forward method to handle Instance objects.

Poem

In the code, a shift so grand,
Imports moved across the land.
Frames and Instances now reside,
In biogtr/io, they proudly abide.
New methods, classes, all in line,
To make our tracking algorithms shine.
🎉✨ Let's toast to progress, swift and bright,
As we code through day and night! 🚀🐇


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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.

@aaprasad aaprasad marked this pull request as ready for review June 3, 2024 19:45
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 10

Outside diff range and nitpick comments (15)
biogtr/io/__init__.py (5)

3-3: Consider adding Frame to __all__ if it's intended for public use, or use it to avoid the static analysis warning.

Tools
Ruff

3-3: biogtr.io.frame.Frame imported but unused; consider removing, adding to __all__, or using a redundant alias


4-4: Consider adding Instance to __all__ if it's intended for public use, or use it to avoid the static analysis warning.

Tools
Ruff

4-4: biogtr.io.instance.Instance imported but unused; consider removing, adding to __all__, or using a redundant alias


5-5: Consider adding AssociationMatrix to __all__ if it's intended for public use, or use it to avoid the static analysis warning.

Tools
Ruff

5-5: biogtr.io.association_matrix.AssociationMatrix imported but unused; consider removing, adding to __all__, or using a redundant alias


6-6: Consider adding Track to __all__ if it's intended for public use, or use it to avoid the static analysis warning.

Tools
Ruff

6-6: biogtr.io.track.Track imported but unused; consider removing, adding to __all__, or using a redundant alias


8-8: Address or document the circular import issue mentioned in the TODO comment to ensure robust module architecture.

biogtr/inference/track.py (1)

Line range hint 133-133: Remove unnecessary f-string or add placeholders as needed to avoid potential confusion or errors.

- print(f"Pod Index: {index}")
+ print("Pod Index:", index)
biogtr/models/global_tracking_transformer.py (1)

Line range hint 11-123: The refactoring of GlobalTrackingTransformer to use Instance objects and return AssociationMatrix objects aligns with the project's new structure. However, ensure AssociationMatrix is correctly imported or defined to avoid runtime errors.

+ from biogtr.io.association_matrix import AssociationMatrix
biogtr/datasets/microscopy_dataset.py (1)

Line range hint 87-87: Replace the lambda expression with a proper function definition to improve code clarity and maintainability.

- parser = lambda x: data_utils.parse_synthetic(x, source=source)
+ def parser(x):
+     return data_utils.parse_synthetic(x, source=source)
biogtr/datasets/cell_tracking_dataset.py (1)

126-126: Documentation update to reflect new module paths.

Consider adding more detailed examples or scenarios to illustrate the use of Frame and Instance classes in the context of this dataset.

tests/test_inference.py (1)

Line range hint 168-168: Local variable N is assigned to but never used.

- N = N_t * T

Remove the unused variable to clean up the code.

Tools
Ruff

4-4: pytest imported but unused

biogtr/inference/metrics.py (1)

108-108: Documentation update to reflect new module paths.

Consider adding more detailed examples or scenarios to illustrate the use of Frame class in the context of MOT metrics.

tests/test_models.py (1)

Line range hint 429-429: Remove unused variable: img_shape.

- img_shape = (1, 100, 100)
biogtr/inference/tracker.py (2)

131-131: Update documentation to reflect new import paths.

Consider updating the documentation to reflect the new import paths accurately. This helps maintain clarity and consistency in the codebase.


Line range hint 177-177: Fix undefined variable and remove unused variable.

- curr_track = instance.pred_track_id
+ curr_track_id = instance.pred_track_id
biogtr/io/frame.py (1)

74-93: Review string representation for completeness and accuracy.

Consider reviewing the string representation method __repr__ to ensure it includes all relevant attributes and provides a clear and accurate representation of the Frame object.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f0c6f3c and adb3715.

Files selected for processing (26)
  • biogtr/datasets/base_dataset.py (1 hunks)
  • biogtr/datasets/cell_tracking_dataset.py (2 hunks)
  • biogtr/datasets/eval_dataset.py (1 hunks)
  • biogtr/datasets/microscopy_dataset.py (2 hunks)
  • biogtr/datasets/sleap_dataset.py (1 hunks)
  • biogtr/inference/metrics.py (2 hunks)
  • biogtr/inference/track.py (1 hunks)
  • biogtr/inference/track_queue.py (1 hunks)
  • biogtr/inference/tracker.py (9 hunks)
  • biogtr/io/init.py (1 hunks)
  • biogtr/io/association_matrix.py (1 hunks)
  • biogtr/io/config.py (3 hunks)
  • biogtr/io/frame.py (1 hunks)
  • biogtr/io/instance.py (1 hunks)
  • biogtr/io/track.py (1 hunks)
  • biogtr/models/global_tracking_transformer.py (4 hunks)
  • biogtr/models/gtr_runner.py (8 hunks)
  • biogtr/models/model_utils.py (1 hunks)
  • biogtr/models/transformer.py (8 hunks)
  • biogtr/training/losses.py (2 hunks)
  • biogtr/training/train.py (1 hunks)
  • tests/test_config.py (1 hunks)
  • tests/test_data_model.py (4 hunks)
  • tests/test_inference.py (1 hunks)
  • tests/test_models.py (6 hunks)
  • tests/test_training.py (1 hunks)
Files skipped from review due to trivial changes (1)
  • biogtr/inference/track_queue.py
Additional context used
Ruff
biogtr/io/__init__.py

3-3: biogtr.io.frame.Frame imported but unused; consider removing, adding to __all__, or using a redundant alias


4-4: biogtr.io.instance.Instance imported but unused; consider removing, adding to __all__, or using a redundant alias


5-5: biogtr.io.association_matrix.AssociationMatrix imported but unused; consider removing, adding to __all__, or using a redundant alias


6-6: biogtr.io.track.Track imported but unused; consider removing, adding to __all__, or using a redundant alias

biogtr/io/track.py

17-17: Undefined name Instance


46-46: Undefined name Instances


64-64: Undefined name Frame


80-80: Undefined name Instance


80-80: Undefined name Instance

tests/test_training.py

10-10: biogtr.models.global_tracking_transformer.GlobalTrackingTransformer imported but unused


11-11: omegaconf.DictConfig imported but unused


47-47: Local variable feats is assigned to but never used

biogtr/inference/track.py

133-133: f-string without any placeholders

biogtr/models/global_tracking_transformer.py

83-83: Undefined name AssociationMatrix

biogtr/datasets/microscopy_dataset.py

87-87: Do not assign a lambda expression, use a def

biogtr/models/model_utils.py

3-3: typing.Tuple imported but unused

tests/test_inference.py

4-4: pytest imported but unused


168-168: Local variable N is assigned to but never used

biogtr/datasets/sleap_dataset.py

9-9: warnings imported but unused

tests/test_models.py

429-429: Local variable img_shape is assigned to but never used

biogtr/inference/tracker.py

121-121: f-string without any placeholders


177-177: Undefined name curr_track


177-177: Local variable curr_track is assigned to but never used

biogtr/io/frame.py

59-59: Undefined name AssociationMatrix


328-328: Undefined name AssociationMatrix


347-347: Undefined name AssociationMatrix

biogtr/io/instance.py

93-93: Undefined name Frame


520-520: Undefined name Frame


529-529: Undefined name Frame

Additional comments not posted (46)
biogtr/datasets/eval_dataset.py (3)

4-4: The import of Frame is correctly updated to reflect the new module path.


5-5: The import of Instance is correctly updated to reflect the new module path.


Line range hint 10-37: The implementation of EvalDataset class is robust, handling datasets merging and index errors effectively.

tests/test_config.py (2)

4-4: The import of Config is correctly updated to reflect the new module path.


Line range hint 14-73: The test functions in test_config.py are comprehensive, covering initialization, setters, and getters of the Config class effectively.

biogtr/io/track.py (3)

3-3: The import of attrs is correctly used for defining the Track class.


4-4: The import of Union from typing is correctly used for type hinting in the Track class.


7-94: The implementation of the Track class is robust, with well-defined properties and methods for managing track instances.

Tools
Ruff

17-17: Undefined name Instance


46-46: Undefined name Instances


64-64: Undefined name Frame


80-80: Undefined name Instance


80-80: Undefined name Instance

biogtr/training/train.py (2)

6-6: The import of Config is correctly updated to reflect the new module path.


Line range hint 18-86: The main function in train.py is well-implemented, handling training configuration, dataset preparation, and model training effectively.

tests/test_training.py (4)

6-6: The import of Frame is correctly updated to reflect the new module path.


7-7: The import of Instance is correctly updated to reflect the new module path.


12-12: The import of Config is correctly updated to reflect the new module path.


Line range hint 19-112: The test functions in test_training.py are comprehensive, covering loss calculation and model runner configuration effectively.

Tools
Ruff

10-10: biogtr.models.global_tracking_transformer.GlobalTrackingTransformer imported but unused


11-11: omegaconf.DictConfig imported but unused

biogtr/inference/track.py (2)

3-5: Updated import paths look correct and align with the project's restructuring.


Line range hint 10-32: The export_trajectories function is well-implemented, efficiently handling data extraction and optional saving.

biogtr/datasets/base_dataset.py (1)

4-4: Updated import path for Frame is correct and aligns with the project's restructuring.

tests/test_data_model.py (1)

3-10: The imports for testing Frame, Instance, AssociationMatrix, and Track are correctly updated to reflect the new module paths.

biogtr/datasets/microscopy_dataset.py (1)

6-7: Updated import paths for Frame and Instance are correct and align with the project's restructuring.

biogtr/datasets/cell_tracking_dataset.py (1)

6-7: Updated import paths for Frame and Instance classes.

tests/test_inference.py (1)

6-7: Updated import paths for Frame and Instance classes.

biogtr/training/losses.py (1)

3-3: Updated import path for Frame class.

biogtr/models/gtr_runner.py (1)

11-12: Updated import paths for Frame and Instance classes.

biogtr/inference/metrics.py (1)

6-6: Updated import path for Frame class.

biogtr/io/config.py (1)

81-85: The handling of ckpt_path in get_model method is implemented correctly. It ensures that the model is loaded from a checkpoint if provided, which aligns with the PR objectives to manage model loading more effectively.

biogtr/io/association_matrix.py (8)

25-42: LGTM! Proper validation for reference instances against matrix columns.


44-61: LGTM! Proper validation for query instances against matrix rows.


75-83: LGTM! Efficient handling of data type conversion for the matrix.


85-131: LGTM! Flexible and correct implementation of DataFrame conversion.


133-196: LGTM! Correct and detailed implementation of matrix reduction with comprehensive error handling.


198-221: LGTM! Robust implementation of matrix element retrieval with comprehensive error handling.


223-261: LGTM! Robust type checking and index retrieval implementation.


263-302: LGTM! Flexible and correct implementation of instance grouping by track ID.

biogtr/datasets/sleap_dataset.py (1)

10-11: LGTM! Correct import statements for Frame and Instance.

biogtr/inference/tracker.py (2)

6-6: Update import paths to reflect new module structure.


284-284: Ensure that the association matrix is correctly handled.

Verification successful

The search results indicate that asso_output is used in various parts of the codebase, including tests and multiple modules. This suggests that the association matrix is indeed being handled and integrated into the frame data structure.

  • tests/test_data_model.py: Multiple assertions and assignments involving asso_output.
  • biogtr/models/model_utils.py: Functions manipulating asso_output.
  • biogtr/io/frame.py: Getter and setter methods for asso_output.
  • biogtr/inference/post_processing.py: Functions processing asso_output.
  • biogtr/inference/tracker.py: Assignments and manipulations of asso_output.

These usages indicate that asso_output is being actively used and tested, which supports the notion that it is correctly handled.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the association matrix is correctly handled and integrated into the frame data structure.

# Test: Search for usage of `asso_output` in the codebase. Expect: Correct handling and integration.
rg --type python $'asso_output'

Length of output: 61



Script:

#!/bin/bash
# Description: Verify that the association matrix is correctly handled and integrated into the frame data structure.

# Test: Search for usage of `asso_output` in the codebase. Expect: Correct handling and integration.
rg --type py 'asso_output'

Length of output: 5904

biogtr/io/frame.py (3)

51-62: Ensure correct use of data types and default values.

Verification successful

The search results show the attribute definitions in the Frame class, confirming the data types and default values. Here are the relevant lines from biogtr/io/frame.py:

  • _video_id: int = attrs.field(alias="video_id", converter=_to_tensor)
  • _frame_id: int = attrs.field(alias="frame_id", converter=_to_tensor)
  • _video: str = attrs.field(alias="vid_file", default="")
  • _img_shape: ArrayLike = attrs.field(alias="img_shape", converter=_to_tensor, factory=list)
  • _instances: list["Instance"] = attrs.field(alias="instances", factory=list)
  • _asso_output: "AssociationMatrix" = attrs.field(alias="asso_output", default=None)
  • _matches: tuple = attrs.field(alias="matches", factory=tuple)
  • _traj_score: dict = attrs.field(alias="traj_score", factory=dict)
  • _device: str = attrs.field(alias="device", default=None)

These definitions appear to use correct data types and sensible default values.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the data types and default values are correctly used in the `Frame` class.

# Test: Search for attribute definitions in the `Frame` class. Expect: Correct data types and sensible default values.
rg --type python $'attrs.field'

Length of output: 61



Script:

#!/bin/bash
# Description: Verify that the data types and default values are correctly used in the `Frame` class.

# Test: Search for attribute definitions in the `Frame` class. Expect: Correct data types and sensible default values.
rg 'attrs.field' --glob '*.py'

Length of output: 2391

Tools
Ruff

59-59: Undefined name AssociationMatrix


95-123: Ensure device handling is robust and error-free.


125-157: Validate conversion logic from sio.LabeledFrame to Frame.

Verification successful

The from_slp method in biogtr/io/frame.py appears to handle the conversion from sio.LabeledFrame to Frame correctly, including edge cases for image shapes and data types. The method processes the image shape, frame index, video filename, and instances appropriately.

  • The method checks the shape of the image and adjusts it if necessary.
  • It converts the frame index to np.int32 if it is a number.
  • It extracts the video filename.
  • It converts instances using the Instance.from_slp method.

Based on the provided code, the conversion logic seems robust and handles various edge cases.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Validate the conversion logic from `sio.LabeledFrame` to `Frame` to ensure it handles all edge cases and data types correctly.

# Test: Review the `from_slp` class method. Expect: Correct handling of all data types and edge cases.
rg --type python $'from_slp'

Length of output: 58



Script:

#!/bin/bash
# Description: Locate and review the `from_slp` class method in the codebase to ensure it handles all edge cases and data types correctly.

# Locate the `from_slp` method in the codebase
ast-grep --lang python --pattern $'class $_ {
  $$$
  @classmethod
  def from_slp($_, $_, $_=0, $_=None, **$_) -> $_:
    $$$
  $$$
}'

Length of output: 656



Script:

#!/bin/bash
# Description: Locate and review the `from_slp` class method in the codebase to ensure it handles all edge cases and data types correctly.

# Locate the `from_slp` method in the codebase
ast-grep --lang python --pattern $'@classmethod
def from_slp($_, $_, $_=0, $_=None, **$_) -> $_:
  $$$
'

Length of output: 105



Script:

#!/bin/bash
# Description: Locate and review the `from_slp` class method in the codebase to ensure it handles all edge cases and data types correctly.

# Locate the `from_slp` method in the codebase
rg --python 'def from_slp'

Length of output: 439



Script:

#!/bin/bash
# Description: Locate and review the `from_slp` class method in the codebase to ensure it handles all edge cases and data types correctly.

# Locate the `from_slp` method in the codebase
rg 'def from_slp' --glob '*.py'

Length of output: 110



Script:

#!/bin/bash
# Description: Extract the full implementation of the `from_slp` method in `biogtr/io/frame.py` to ensure it handles all edge cases and data types correctly.

# Extract the `from_slp` method implementation
ast-grep --lang python --pattern $'class $_ {
  $$$
  @classmethod
  def from_slp($_, $_, $_=0, $_=None, **$_) -> $_:
    $$$
  $$$
}' biogtr/io/frame.py

Length of output: 675



Script:

#!/bin/bash
# Description: Extract the full implementation of the `from_slp` method in `biogtr/io/frame.py` to ensure it handles all edge cases and data types correctly.

# Extract the `from_slp` method implementation
ast-grep --lang python --pattern $'@classmethod
def from_slp($_, $_, $_=0, $_=None, **$_) -> $_:
  $$$
' biogtr/io/frame.py

Length of output: 124



Script:

#!/bin/bash
# Description: Locate and review the `from_slp` class method in the codebase to ensure it handles all edge cases and data types correctly.

# Locate the `from_slp` method in the codebase
rg 'def from_slp' biogtr/io/frame.py

Length of output: 56



Script:

#!/bin/bash
# Description: Display the content of `biogtr/io/frame.py` to review the `from_slp` method implementation.

# Display the content of `biogtr/io/frame.py`
cat biogtr/io/frame.py

Length of output: 18363

biogtr/models/transformer.py (7)

14-15: Updated imports align with the new module structure.

Also applies to: 18-18


144-145: The modifications in the forward method to use Instance and AssociationMatrix are consistent with the PR objectives.

Also applies to: 149-150, 158-160, 162-165, 167-169, 171-175, 178-180, 182-182, 184-184, 186-186, 188-190, 194-194, 197-197, 202-206, 208-212, 214-216, 218-218, 220-220, 222-222, 224-226, 228-228, 230-231, 233-235, 238-241, 253-254, 256-258, 261-261


261-261: The TransformerEncoderLayer class is well-implemented and documented.


384-385: Positional embeddings are correctly applied in the encoder layers.

Also applies to: 393-393, 401-402, 405-405, 531-531


384-385: The TransformerDecoderLayer class is well-implemented and documented.

Also applies to: 393-393, 401-402, 405-405, 531-531


384-385: Positional embeddings are correctly applied in the decoder layers.

Also applies to: 393-393, 401-402, 405-405, 531-531


384-385: Ensure positional embeddings are correctly handled in the decoder layers.

Also applies to: 393-393, 401-402, 405-405, 531-531

Verification successful

Ensure positional embeddings are correctly handled in the decoder layers.

The search results indicate that ref_pos_emb and query_pos_emb are used in various parts of the biogtr/models/transformer.py file, including their initialization, addition to embeddings, and usage in encoder and decoder layers. This suggests that positional embeddings are being handled appropriately.

  • Lines where ref_pos_emb and query_pos_emb are used:
    • Initialization: ref_pos_emb = self.pos_emb(ref_boxes), query_pos_emb = self.pos_emb(query_boxes)
    • Addition to embeddings: instance.add_embedding("pos", ref_pos_emb[i]), instance.add_embedding("pos", query_pos_emb[i])
    • Usage in encoder and decoder layers: encoder_features = encoder_features + ref_pos_emb, decoder_queries = decoder_queries + query_pos_emb
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that positional embeddings are correctly handled in the decoder layers.

# Test: Search for the usage of positional embeddings in the decoder layers. Expect: Correct handling of positional embeddings.
rg --type python $'ref_pos_emb|query_pos_emb' biogtr/models/transformer.py

Length of output: 106



Script:

#!/bin/bash
# Description: Verify that positional embeddings are correctly handled in the decoder layers.

# Test: Search for the usage of positional embeddings in the decoder layers. Expect: Correct handling of positional embeddings.
rg 'ref_pos_emb|query_pos_emb' biogtr/models/transformer.py

Length of output: 1469

Comment on lines 109 to 111
ckpt_path = model_params.pop("ckpt_path", None)

if self.cfg.model.ckpt_path is not None and self.cfg.model.ckpt_path != "":
if ckpt_path is not None and ckpt_path != "":
Copy link
Contributor

Choose a reason for hiding this comment

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

There is a potential bug in the get_gtr_runner method. The use of self.cfg.model.ckpt_path after it has potentially been removed by pop could lead to an AttributeError. Consider using the local variable ckpt_path instead.

-                self.cfg.model.ckpt_path,
+                ckpt_path,
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.

Suggested change
ckpt_path = model_params.pop("ckpt_path", None)
if self.cfg.model.ckpt_path is not None and self.cfg.model.ckpt_path != "":
if ckpt_path is not None and ckpt_path != "":
ckpt_path = model_params.pop("ckpt_path", None)
if ckpt_path is not None and ckpt_path != "":
ckpt_path,

self._embeddings[emb_type] = embedding

@property
def frame(self) -> "Frame":
Copy link
Contributor

Choose a reason for hiding this comment

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

Undefined reference to Frame in type hints and setter.

The type hints and setter method for frame reference Frame, which is not defined in this file. This could lead to runtime errors if not addressed. Ensure that Frame is properly imported or defined.

Also applies to: 529-529

Tools
Ruff

520-520: Undefined name Frame

_skeleton: sio.Skeleton = attrs.field(alias="skeleton", default=None)
_pose: dict[str, ArrayLike] = attrs.field(alias="pose", factory=dict)
_device: str = attrs.field(alias="device", default=None)
_frame: "Frame" = None
Copy link
Contributor

Choose a reason for hiding this comment

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

Undefined reference to Frame.

The type hint for _frame references Frame, which is not defined in this file. Ensure that Frame is imported or defined elsewhere in the project.

Tools
Ruff

93-93: Undefined name Frame

Comment on lines 262 to 267
asso_matrix = model(all_instances, query_instances)
# if model.transformer.return_embedding:
# query_frame.embeddings = embed TODO add embedding to Instance Object
# if query_frame == 1:
# print(asso_output)
asso_output = asso_output[-1].split(
asso_output = asso_matrix[-1].matrix.split(
Copy link
Contributor

Choose a reason for hiding this comment

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

Optimize tensor operations and ensure correct usage of API.

Consider optimizing tensor operations and ensuring that the API usage is correct. For instance, verify that the split method is used appropriately and that the tensor dimensions align as expected.

@@ -207,7 +209,7 @@

Args:
model: the pretrained GlobalTrackingTransformer to be used for inference
frames: A list of Frames containing reid features. See `biogtr.data_structures` for more info.
frames: A list of Frames containing reid features. See `biogtr.io.data_structures` for more info.
Copy link
Contributor

Choose a reason for hiding this comment

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

Refactor to improve clarity and reduce redundancy.

Consider refactoring the code to improve clarity and reduce redundancy. For example, the list comprehension for all_instances can be simplified or explained better to enhance readability.

Also applies to: 237-239

Comment on lines 144 to 146
tracked_frames = self.track_queue.collate_tracks(
device=frame_to_track.frame_id.device
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Tip

Codebase Verification

Ensure device compatibility for tensor operations.

The search results indicate several instances where tensors are created without specifying a device. This could lead to potential issues if the tensors are used in operations that require device compatibility.

  • biogtr/io/frame.py
  • biogtr/io/instance.py
  • biogtr/models/model_utils.py
  • biogtr/datasets/sleap_dataset.py
  • biogtr/inference/boxes.py
  • biogtr/models/embedding.py
  • tests/test_data_model.py
  • tests/test_inference.py

Please review these instances and ensure that the device is specified where necessary.

Analysis chain

Ensure device compatibility for tensor operations.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that all tensor operations are compatible with the specified device.

# Test: Search for tensor operations that might not specify device. Expect: All operations specify the device.
rg --type python $'tensor\('

Length of output: 60



Script:

#!/bin/bash
# Description: Verify that all tensor operations are compatible with the specified device.

# Test: Search for tensor operations that might not specify device. Expect: All operations specify the device.
rg --type py $'tensor\('

Length of output: 2528

)

_instances: list["Instance"] = attrs.field(alias="instances", factory=list)
_asso_output: "AssociationMatrix" = attrs.field(alias="asso_output", default=None)
Copy link
Contributor

Choose a reason for hiding this comment

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

Define or import AssociationMatrix before usage.

It appears that AssociationMatrix is used but not defined or imported in this file. Ensure that AssociationMatrix is correctly imported from its module to avoid runtime errors.

Also applies to: 328-328, 347-347

Tools
Ruff

59-59: Undefined name AssociationMatrix

@@ -3,13 +3,93 @@
import torch
import pytest
Copy link
Contributor

Choose a reason for hiding this comment

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

pytest imported but unused.

Consider removing the unused import to clean up the code.

Tools
Ruff

4-4: pytest imported but unused

@@ -7,7 +7,8 @@
import sleap_io as sio
import random
import warnings
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove unused import: warnings.

- import warnings
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.

Suggested change
import warnings
Tools
Ruff

9-9: warnings imported but unused

@@ -2,35 +2,69 @@

from typing import List, Tuple, Iterable
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove the unused import to clean up the code.

- from typing import List, Tuple, Iterable
+ from typing import List, Iterable
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.

Suggested change
from typing import List, Tuple, Iterable
from typing import List, Iterable
Tools
Ruff

3-3: typing.Tuple imported but unused

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range and nitpick comments (3)
biogtr/models/global_tracking_transformer.py (1)

Line range hint 83-83: Undefined name AssociationMatrix in the return type of the forward method.

- from biogtr.io.instance import Instance
+ from biogtr.io.instance import Instance
+ from biogtr.io.association_matrix import AssociationMatrix
biogtr/io/association_matrix.py (1)

Line range hint 180-180: Add missing self parameter in the reduce method.

- def reduce(
+ def reduce(self,

Also applies to: 181-181, 183-183, 184-184, 186-186, 187-187, 190-190, 190-190, 195-195, 195-195, 202-202

biogtr/inference/tracker.py (1)

Line range hint 176-176: Initialize the curr_track variable before use.

+ curr_track = 0
  for i, instance in enumerate(frames[batch_idx].instances):
      instance.pred_track_id = instance.gt_track_id
      curr_track = instance.pred_track_id
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between adb3715 and 5d021d4.

Files selected for processing (5)
  • biogtr/inference/tracker.py (1 hunks)
  • biogtr/io/association_matrix.py (6 hunks)
  • biogtr/io/config.py (2 hunks)
  • biogtr/models/global_tracking_transformer.py (1 hunks)
  • biogtr/models/gtr_runner.py (3 hunks)
Files skipped from review as they are similar to previous changes (2)
  • biogtr/io/config.py
  • biogtr/models/gtr_runner.py
Additional context used
Ruff
biogtr/models/global_tracking_transformer.py

83-83: Undefined name AssociationMatrix

biogtr/io/association_matrix.py

113-113: f-string without any placeholders


137-137: f-string without any placeholders


180-180: Undefined name self


181-181: Undefined name self


183-183: Undefined name self


184-184: Undefined name self


186-186: Undefined name self


187-187: Undefined name self


190-190: Undefined name self


190-190: Undefined name self


195-195: Undefined name self


195-195: Undefined name self


202-202: Undefined name self

biogtr/inference/tracker.py

121-121: f-string without any placeholders


176-176: Undefined name curr_track


176-176: Local variable curr_track is assigned to but never used

@aaprasad aaprasad merged commit f8a33df into main Jun 3, 2024
3 checks passed
@aaprasad aaprasad deleted the aadi/remove-kwargs-mutable-defaults branch June 3, 2024 21:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant