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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add_path_to_sidebar_group util
brimoor committed Dec 10, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit fe56fb367a275d4642c06cba0741d30e1fe6bb27
38 changes: 7 additions & 31 deletions fiftyone/core/dataset.py
Original file line number Diff line number Diff line change
@@ -45,8 +45,7 @@
import fiftyone.core.labels as fol
import fiftyone.core.media as fom
import fiftyone.core.metadata as fome
from fiftyone.core.odm.dataset import SampleFieldDocument
from fiftyone.core.odm.dataset import DatasetAppConfig, SidebarGroupDocument
from fiftyone.core.odm.dataset import DatasetAppConfig
import fiftyone.migrations as fomi
import fiftyone.core.odm as foo
import fiftyone.core.sample as fos
@@ -1866,35 +1865,12 @@ def create_summary_field(
if sidebar_group is None:
sidebar_group = "summaries"

if self.app_config.sidebar_groups is None:
sidebar_groups = DatasetAppConfig.default_sidebar_groups(self)
self.app_config.sidebar_groups = sidebar_groups
else:
sidebar_groups = self.app_config.sidebar_groups

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

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

insert_after = None
for i, group in enumerate(sidebar_groups):
if group.name == "labels":
insert_after = i

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

if field_name not in index_group.paths:
index_group.paths.append(field_name)
self.app_config._add_path_to_sidebar_group(
field_name,
sidebar_group,
after_group="labels",
dataset=self,
)

if create_index:
for _field_name in index_fields:
38 changes: 38 additions & 0 deletions fiftyone/core/odm/dataset.py
Original file line number Diff line number Diff line change
@@ -624,6 +624,44 @@ def _rename_paths(self, paths, new_paths):
for path, new_path in zip(paths, new_paths):
self._rename_path(path, new_path)

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

Comment on lines +627 to +637
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")

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)

Comment on lines +627 to +664
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


def _make_default_sidebar_groups(sample_collection):
# Possible sidebar groups
Loading