From 41ed515e4fef7f3d7d37f48dcf5e483c591a67e0 Mon Sep 17 00:00:00 2001 From: Benjamin Kane Date: Wed, 11 Sep 2024 10:55:06 -0400 Subject: [PATCH 1/2] add selection filtering with slices in handle_group_filter --- fiftyone/server/view.py | 9 +++++++- tests/unittests/server_group_tests.py | 31 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/fiftyone/server/view.py b/fiftyone/server/view.py index 98ea785bd7..16183fcb36 100644 --- a/fiftyone/server/view.py +++ b/fiftyone/server/view.py @@ -264,11 +264,18 @@ def handle_group_filter( for stage in stages: # add stages after flattening and group match + if group_by and isinstance(stage, fosg.GroupBy) and filter.slices: view = view.match( {group_field + ".name": {"$in": filter.slices}} ) - view = view._add_view_stage(stage, validate=False) + + # if selecting a group, filter out select/reorder stages + if ( + not filter.id + or type(stage) not in fosg._STAGES_THAT_SELECT_OR_REORDER + ): + view = view._add_view_stage(stage, validate=False) elif filter.id: view = fov.make_optimized_select_view(view, filter.id, groups=True) diff --git a/tests/unittests/server_group_tests.py b/tests/unittests/server_group_tests.py index b368c57159..3c981ca670 100644 --- a/tests/unittests/server_group_tests.py +++ b/tests/unittests/server_group_tests.py @@ -90,3 +90,34 @@ def test_manual_group_slice(self): GroupElementFilter(), ) self.assertEqual(view._all_stages, [fo.Select(first)]) + + @drop_datasets + def test_group_selection(self): + dataset: fo.Dataset = fo.Dataset() + group = fo.Group() + one = fo.Sample( + filepath="image.png", + group=group.element("one"), + ) + two = fo.Sample( + filepath="image.png", + group=group.element("two"), + ) + + dataset.add_samples([one, two]) + + selection = dataset.select(one.id) + + with_slices, _ = fosv.handle_group_filter( + dataset, + selection, + GroupElementFilter(id=group.id, slices=["one", "two"]), + ) + self.assertEqual(len(with_slices), 2) + + without_slices, _ = fosv.handle_group_filter( + dataset, + selection, + GroupElementFilter(id=group.id, slices=["one", "two"]), + ) + self.assertEqual(len(without_slices), 2) From 8afe2b91f7f8e1b32e93c9a8112efee4d387d31a Mon Sep 17 00:00:00 2001 From: Benjamin Kane Date: Thu, 12 Sep 2024 11:36:41 -0400 Subject: [PATCH 2/2] lint --- fiftyone/server/view.py | 1 - 1 file changed, 1 deletion(-) diff --git a/fiftyone/server/view.py b/fiftyone/server/view.py index 16183fcb36..7a8d4b9464 100644 --- a/fiftyone/server/view.py +++ b/fiftyone/server/view.py @@ -264,7 +264,6 @@ def handle_group_filter( for stage in stages: # add stages after flattening and group match - if group_by and isinstance(stage, fosg.GroupBy) and filter.slices: view = view.match( {group_field + ".name": {"$in": filter.slices}}