Skip to content

Commit

Permalink
Merge pull request #4844 from voxel51/bugfix/grp-slice-op
Browse files Browse the repository at this point in the history
add set_group_slice operator
  • Loading branch information
brimoor authored Sep 26, 2024
2 parents 324a0cf + 0cf14c7 commit 0b928cf
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
24 changes: 24 additions & 0 deletions app/packages/operators/src/built-in-operators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,29 @@ export class ApplyPanelStatePath extends Operator {
}
}

export class SetGroupSlice extends Operator {
get config(): OperatorConfig {
return new OperatorConfig({
name: "set_group_slice",
label: "Set group slice",
// unlisted: true,
});
}
useHooks() {
const setSlice = fos.useSetGroupSlice();
return { setSlice };
}
async resolveInput(): Promise<types.Property> {
const inputs = new types.Object();
inputs.str("slice", { label: "Group slice", required: true });
return new types.Property(inputs);
}
async execute(ctx: ExecutionContext): Promise<void> {
const { slice } = ctx.params;
ctx.hooks.setSlice(slice);
}
}

export function registerBuiltInOperators() {
try {
_registerBuiltInOperator(CopyViewAsJSON);
Expand Down Expand Up @@ -1336,6 +1359,7 @@ export function registerBuiltInOperators() {
_registerBuiltInOperator(TrackEvent);
_registerBuiltInOperator(SetPanelTitle);
_registerBuiltInOperator(ApplyPanelStatePath);
_registerBuiltInOperator(SetGroupSlice);
_registerBuiltInOperator(SetPlayheadState);
_registerBuiltInOperator(SetFrameNumber);
} catch (e) {
Expand Down
5 changes: 4 additions & 1 deletion fiftyone/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,10 @@ def group_slice(self, slice_name):
if slice_name is None:
slice_name = self._doc.default_group_slice

if slice_name not in self._doc.group_media_types:
if (
slice_name is not None
and slice_name not in self._doc.group_media_types
):
raise ValueError("Dataset has no group slice '%s'" % slice_name)

self._group_slice = slice_name
Expand Down
14 changes: 12 additions & 2 deletions fiftyone/operators/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1518,7 +1518,12 @@ def execute(self, ctx):
new_name = ctx.params["new_name"]

ctx.dataset.rename_group_slice(name, new_name)
ctx.trigger("reload_dataset")

# @todo ideally we would only set this if the group slice we renamed is
# currently loaded in the App
ctx.ops.set_group_slice(ctx.dataset.default_group_slice)

ctx.ops.reload_dataset()


class DeleteGroupSlice(foo.Operator):
Expand Down Expand Up @@ -1564,7 +1569,12 @@ def execute(self, ctx):
name = ctx.params["name"]

ctx.dataset.delete_group_slice(name)
ctx.trigger("reload_dataset")

# @todo ideally we would only set this if the group slice we deleted is
# currently loaded in the App
ctx.ops.set_group_slice(ctx.dataset.default_group_slice)

ctx.ops.reload_dataset()


class ListSavedViews(foo.Operator):
Expand Down
8 changes: 8 additions & 0 deletions fiftyone/operators/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,14 @@ def set_panel_title(self, id=None, title=None):
"set_panel_title", params={"id": id, "title": title}
)

def set_group_slice(self, slice):
"""Set the active group slice in the App.
Args:
slice: the group slice to activate
"""
return self._ctx.trigger("set_group_slice", {"slice": slice})


def _serialize_view(view):
return json.loads(json_util.dumps(view._serialize()))

0 comments on commit 0b928cf

Please sign in to comment.