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

Use estimated count on full dataset view #5446

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

Conversation

kaixi-wang
Copy link
Contributor

@kaixi-wang kaixi-wang commented Jan 29, 2025

What changes are proposed in this pull request?

  • use estimated count for full dataset view so dataset slices with negative starts can be optimized

How is this patch tested? If it is not, please explain why.

  • tested locally with 11M sample dataset
# With Changes
In [18]: %time v=ds[-2:]
CPU times: user 5.4 ms, sys: 886 µs, total: 6.29 ms
Wall time: 770 ms
# Without changes
In [19]: %time v=ds[-2:]
CPU times: user 3.63 s, sys: 496 ms, total: 4.12 s
Wall time: 16min 58s
In [20]: len(ds)
Out[20]: 11460588

Release Notes

Is this a user-facing change that should be mentioned in the release notes?

  • No. You can skip the rest of this section.
  • Yes. Give a description of this change to be included in the release
    notes for FiftyOne users.

(Details in 1-2 sentences. You can just refer to another PR with a description
if this PR is part of a larger change.)

What areas of FiftyOne does this PR affect?

  • App: FiftyOne application changes
  • Build: Build and test infrastructure changes
  • Core: Core fiftyone Python library changes
  • Documentation: FiftyOne documentation changes
  • Other

Summary by CodeRabbit

Summary by CodeRabbit

  • Refactor
    • Simplified logic for determining full collection status.
    • Streamlined condition checks to improve clarity and maintainability.

@kaixi-wang kaixi-wang self-assigned this Jan 29, 2025
Copy link
Contributor

coderabbitai bot commented Jan 29, 2025

Walkthrough

The pull request introduces changes to the _is_full_collection method in the SampleCollection class within the fiftyone/core/collections.py file. The modification simplifies the logic for determining whether a collection is a full collection by streamlining the condition checks. The new implementation focuses on checking if the pipeline is empty and the media type is not GROUP, reducing the previous complexity of the method.

Changes

File Change Summary
fiftyone/core/collections.py Updated _is_full_collection method with simplified logic for determining full collection status

Possibly related PRs

  • Optimize dataset first/head and last/tail  #5407: This PR modifies the SampleCollection class in the same file (fiftyone/core/collections.py) and involves changes to methods related to sample retrieval, which may be conceptually related to the adjustments made in the _is_full_collection method.

Poem

🐰 A Rabbit's Ode to Code Simplicity 🔍

In collections where logic once twirled,
A method now clearer, its purpose unfurled.
Fewer lines, more precise and bright,
Complexity trimmed with algorithmic might.
Simplicity dances, code leaps with glee!

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

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.

@kaixi-wang kaixi-wang marked this pull request as draft January 29, 2025 02:17
return True
if isinstance(self, fov.DatasetView):
if not (
self._pipeline()
Copy link
Contributor

Choose a reason for hiding this comment

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

Note that _pipeline() can be expensive for some views. EG if a view contains SortBySimilarity it can even run model inference!!!

That's why I wrote the existing if statement in this function in a seemingly funny way: I first check things that are super cheap and only call _pipeline() last once I've determined that the view is simple and thus _pipeline() will be fast. Taking advantage of short-circuiting.

I'd request that a similar treatment be applied to any additional logic added to this method.

Side note, I've been staring at this first if for a minute and can't figure out what it is saying(!)

Copy link
Contributor Author

@kaixi-wang kaixi-wang Jan 29, 2025

Choose a reason for hiding this comment

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

ohh thanks for calling this out! I was wondering why we used _pipeline() and stages here

regarding the if, its just factoring out the not, so fully expanded, it translates to not pipeline and not clips and not groups

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I don't like the refactor either, it's much harder to understand imo.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the syntax...

Side note, kinda funny how the bot summary described this syntax as simplified and streamlined 🤖

Copy link
Contributor

Choose a reason for hiding this comment

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

the rabbit sees the glass half full 😃

@swheaton
Copy link
Contributor

Why would a view have a negative start?

@swheaton
Copy link
Contributor

I guess it doesn't matter, len(ds.view()) should be fast as well

@kaixi-wang
Copy link
Contributor Author

Why would a view have a negative start?

Slices from the end to see the tail

@swheaton
Copy link
Contributor

Why would a view have a negative start?

Slices from the end to see the tail

Oh well if it's at the tail, then it's not the full collection?

@kaixi-wang
Copy link
Contributor Author

We overwrite the slice operator so that dataset[-N:] triggers a skip aggregation and to get the value of skip, there's a hidden len(dataset.view()) that is previously excluded in this condition

@swheaton
Copy link
Contributor

Ah ok so really the optimization is for dataset.view() like I mentioned above. Got it.

@kaixi-wang kaixi-wang marked this pull request as ready for review January 29, 2025 15:39
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

🧹 Nitpick comments (1)
fiftyone/core/collections.py (1)

10625-10640: LGTM! The implementation correctly determines full collections.

The logic properly handles both regular and grouped datasets by checking:

  1. For regular datasets - no stages, not clips, and not GROUP media type
  2. For grouped datasets - single SelectGroupSlices stage with no pipeline

Consider performance implications of _pipeline() call.

The method calls _pipeline() which could be expensive for some views. Consider moving this check earlier in the grouped dataset condition to take advantage of short-circuiting.

 if (
     self._dataset.media_type == fom.GROUP
     and len(self._stages) == 1
     and isinstance(self._stages[0], fos.SelectGroupSlices)
-    and not self._pipeline()
 ):
+    if not self._pipeline():
+        return True
-    return True
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0fb909c and ed9aab7.

📒 Files selected for processing (1)
  • fiftyone/core/collections.py (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: e2e / test-e2e
  • GitHub Check: build

Comment on lines 10622 to 10623
if isinstance(self, fod.Dataset) and self.media_type != fom.GROUP:
return True
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if isinstance(self, fod.Dataset) and self.media_type != fom.GROUP:
return True
# Case 1: full non-group dataset
if isinstance(self, fod.Dataset) and self.media_type != fom.GROUP:
return True

Comment on lines +10625 to +10640
if isinstance(self, fov.DatasetView):
# pylint:disable=no-member
if (
not len(self._stages)
and not self._is_clips
and self._dataset.media_type != fom.GROUP
):
return True

if (
self._dataset.media_type == fom.GROUP
and len(self._stages) == 1
and isinstance(self._stages[0], fos.SelectGroupSlices)
and not self._pipeline()
):
return True
Copy link
Contributor

Choose a reason for hiding this comment

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

lets add comments about the cases

Suggested change
if isinstance(self, fov.DatasetView):
# pylint:disable=no-member
if (
not len(self._stages)
and not self._is_clips
and self._dataset.media_type != fom.GROUP
):
return True
if (
self._dataset.media_type == fom.GROUP
and len(self._stages) == 1
and isinstance(self._stages[0], fos.SelectGroupSlices)
and not self._pipeline()
):
return True
if isinstance(self, fov.DatasetView):
# Case 2: Empty non-group dataset view
# pylint:disable=no-member
if (
not len(self._stages)
and not self._is_clips
and self._dataset.media_type != fom.GROUP
):
return True
# Group dataset with all slices selected and no additional stages
if (
self._dataset.media_type == fom.GROUP
and len(self._stages) == 1
and isinstance(self._stages[0], fos.SelectGroupSlices)
and not self._pipeline()
):
return True

# pylint:disable=no-member
if (
not len(self._stages)
and not self._is_clips
Copy link
Contributor

Choose a reason for hiding this comment

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

Is clips the only case? Or is it any generated dataset?

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.

3 participants