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

Utility for adding a path to a sidebar group #5251

Merged
merged 1 commit into from
Dec 12, 2024

Conversation

brimoor
Copy link
Contributor

@brimoor brimoor commented Dec 10, 2024

Refactored the sidebar group-related code in Dataset.create_summary_field() into a DatasetAppConfig._add_path_to_sidebar_group() utility so it can be reused by other methods in the future.

Summary by CodeRabbit

  • New Features

    • Introduced a method for dynamically managing sidebar groups within the dataset configuration.
  • Bug Fixes

    • Improved logic for handling sidebar groups to prevent duplication and ensure proper path management.
  • Refactor

    • Simplified the sidebar group management logic in the create_summary_field method for better clarity and organization.

@brimoor brimoor added the enhancement Code enhancement label Dec 10, 2024
@brimoor brimoor requested a review from a team December 10, 2024 15:23
Copy link
Contributor

coderabbitai bot commented Dec 10, 2024

Walkthrough

The changes in this pull request involve modifications to the fiftyone/core/dataset.py and fiftyone/core/odm/dataset.py files. The create_summary_field method has been refactored to improve the management of sidebar groups by removing unnecessary imports and encapsulating sidebar group logic in a new method, _add_path_to_sidebar_group. This new method enhances the handling of sidebar paths, ensuring proper management without duplication. Overall, the core functionality remains intact, focusing on code organization and clarity.

Changes

File Change Summary
fiftyone/core/dataset.py - Removed import statement for SidebarGroupDocument.
- Refactored create_summary_field method to use _add_path_to_sidebar_group.
fiftyone/core/odm/dataset.py - Added new method _add_path_to_sidebar_group in DatasetAppConfig for managing sidebar paths.
- Minor adjustments to _delete_path, _rename_path, and _matches_path methods.

Possibly related PRs

  • Add support for summary fields #4765: This PR adds significant enhancements to the create_summary_field method in fiftyone/core/dataset.py, which is directly related to the changes made in the main PR that also modifies this method's logic for managing sidebar groups.
  • Summary field and index improvements #5091: This PR includes updates to the create_summary_field method in fiftyone/core/dataset.py, aligning with the main PR's changes that refactor the sidebar group management within the same method.

Suggested reviewers

  • minhtuev
  • CamronStaley

🐇 In the code, we hop and play,
Refactoring brings a brighter day.
Sidebar groups now neatly align,
With paths that twist and paths that twine.
Clarity shines, and logic flows,
In our dataset, the magic grows! 🌟


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. (Experiment)
  • @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.

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.

@brimoor brimoor force-pushed the add-path-to-sidebar-group branch from 26665b9 to fe56fb3 Compare December 10, 2024 15:27
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: 2

🧹 Outside diff range and nitpick comments (1)
fiftyone/core/odm/dataset.py (1)

640-661: Simplify group management logic

The group insertion logic can be simplified and made more maintainable.

-        index_group = None
-        for group in self.sidebar_groups:
-            if group.name == sidebar_group:
-                index_group = group
-            else:
-                if path in group.paths:
-                    group.paths.remove(path)
+        # Remove path from all other groups
+        for group in self.sidebar_groups:
+            if path in group.paths:
+                group.paths.remove(path)
+
+        # Find or create target group
+        index_group = next(
+            (group for group in self.sidebar_groups if group.name == sidebar_group),
+            None
+        )
 
         if index_group is None:
             index_group = SidebarGroupDocument(name=sidebar_group)
-
-            insert_after = None
-            if after_group is not None:
-                for i, group in enumerate(self.sidebar_groups):
-                    if group.name == after_group:
-                        insert_after = i
-
-            if insert_after is None:
-                self.sidebar_groups.append(index_group)
-            else:
-                self.sidebar_groups.insert(insert_after + 1, index_group)
+            insert_index = (
+                next(
+                    (i for i, group in enumerate(self.sidebar_groups)
+                     if group.name == after_group),
+                    None
+                )
+            )
+            insert_index = (insert_index + 1) if insert_index is not None else len(self.sidebar_groups)
+            self.sidebar_groups.insert(insert_index, index_group)
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 26665b9 and fe56fb3.

📒 Files selected for processing (2)
  • fiftyone/core/dataset.py (2 hunks)
  • fiftyone/core/odm/dataset.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • fiftyone/core/dataset.py

Comment on lines +627 to +664
def _add_path_to_sidebar_group(
self,
path,
sidebar_group,
after_group=None,
dataset=None,
):
if self.sidebar_groups is None:
if dataset is None:
return

self.sidebar_groups = self.default_sidebar_groups(dataset)

index_group = None
for group in self.sidebar_groups:
if group.name == sidebar_group:
index_group = group
else:
if path in group.paths:
group.paths.remove(path)

if index_group is None:
index_group = SidebarGroupDocument(name=sidebar_group)

insert_after = None
if after_group is not None:
for i, group in enumerate(self.sidebar_groups):
if group.name == after_group:
insert_after = i

if insert_after is None:
self.sidebar_groups.append(index_group)
else:
self.sidebar_groups.insert(insert_after + 1, index_group)

if path not in index_group.paths:
index_group.paths.append(path)

Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

Based on the search results, I can now generate the final response:

Direct manipulation of sidebar groups found in mutation.py

The new utility method _add_path_to_sidebar_group should be used instead of direct manipulation of sidebar_groups found in fiftyone/server/mutation.py. This ensures consistent handling of sidebar group management across the codebase.

  • fiftyone/server/mutation.py: Replace direct assignment of sidebar_groups with calls to _add_path_to_sidebar_group
🔗 Analysis chain

Verify usage pattern in the codebase

Let's ensure this new utility method is being used consistently across the codebase.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for direct manipulation of sidebar_groups that should use this new utility.
# Test: Search for direct manipulation of sidebar_groups. Expect: No direct manipulation.

# Look for direct manipulation of sidebar_groups
rg -A 5 'sidebar_groups\s*=' --type py

# Look for path manipulation in sidebar groups
rg -A 5 'paths\.append|paths\.remove' --type py

Length of output: 18187

Comment on lines +627 to +637
def _add_path_to_sidebar_group(
self,
path,
sidebar_group,
after_group=None,
dataset=None,
):
if self.sidebar_groups is None:
if dataset is None:
return

Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add parameter validation and improve error handling

The method should validate the path parameter and provide a more informative error message when dataset is required but not provided.

     def _add_path_to_sidebar_group(
         self,
         path,
         sidebar_group,
         after_group=None,
         dataset=None,
     ):
+        if not path or not isinstance(path, str):
+            raise ValueError("path must be a non-empty string")
+        if not sidebar_group or not isinstance(sidebar_group, str):
+            raise ValueError("sidebar_group must be a non-empty string")
         if self.sidebar_groups is None:
             if dataset is None:
-                return
+                raise ValueError("dataset parameter is required when sidebar_groups is not initialized")
📝 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
def _add_path_to_sidebar_group(
self,
path,
sidebar_group,
after_group=None,
dataset=None,
):
if self.sidebar_groups is None:
if dataset is None:
return
def _add_path_to_sidebar_group(
self,
path,
sidebar_group,
after_group=None,
dataset=None,
):
if not path or not isinstance(path, str):
raise ValueError("path must be a non-empty string")
if not sidebar_group or not isinstance(sidebar_group, str):
raise ValueError("sidebar_group must be a non-empty string")
if self.sidebar_groups is None:
if dataset is None:
raise ValueError("dataset parameter is required when sidebar_groups is not initialized")

@brimoor brimoor requested review from Br2850, manivoxel51 and lanzhenw and removed request for a team December 10, 2024 15:39
Copy link
Contributor

@manivoxel51 manivoxel51 left a comment

Choose a reason for hiding this comment

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

looking good! one issue I am seeing

  • compute brightness - Data Quality group is correctly created and has brightness nested
  • delete brightness and recompute it - Data Quality group stays empty and brightness gets added to primitives

Screen Shot 2024-12-11 at 12 03 41 PM

@brimoor
Copy link
Contributor Author

brimoor commented Dec 12, 2024

delete brightness and recompute it - Data Quality group stays empty and brightness gets added to primitives

@manivoxel51 yeah I noticed that too: it's a UI issue that exists before and after this PR. If you refresh your browser, brightness jumps up into the DATA QUALITY section

Copy link
Contributor

@manivoxel51 manivoxel51 left a comment

Choose a reason for hiding this comment

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

🍨

@brimoor brimoor merged commit c4df037 into develop Dec 12, 2024
14 checks passed
@brimoor brimoor deleted the add-path-to-sidebar-group branch December 12, 2024 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Code enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants