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

Add menu item for deleting instances beyond frame limit #1797

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
6 changes: 6 additions & 0 deletions sleap/gui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,12 @@ def new_instance_menu_action():
"Delete Predictions beyond Max Instances...",
self.commands.deleteInstanceLimitPredictions,
)
add_menu_item(
labelMenu,
"delete frame limit predictions",
"Delete Predictions beyond Frame Limit...",
self.commands.deleteFrameLimitPredictions,
)

### Tracks Menu ###

Expand Down
37 changes: 37 additions & 0 deletions sleap/gui/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,10 @@ def deleteInstanceLimitPredictions(self):
"""Gui for deleting instances beyond some number in each frame."""
self.execute(DeleteInstanceLimitPredictions)

def deleteFrameLimitPredictions(self):
"""Gui for deleting instances beyond some frame number."""
self.execute(DeleteFrameLimitPredictions)

Comment on lines +497 to +500
Copy link

Choose a reason for hiding this comment

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

Add method documentation for deleteFrameLimitPredictions.

Consider adding a detailed docstring for the deleteFrameLimitPredictions method to explain its parameters, return type, and any exceptions it might raise. This will enhance code maintainability and readability.

def completeInstanceNodes(self, instance: Instance):
"""Adds missing nodes to given instance."""
self.execute(AddMissingInstanceNodes, instance=instance)
Expand Down Expand Up @@ -2470,6 +2474,39 @@ def ask(cls, context: CommandContext, params: dict) -> bool:
return super().ask(context, params)


class DeleteFrameLimitPredictions(InstanceDeleteCommand):
@staticmethod
def get_frame_instance_list(context: CommandContext, params: Dict):
shrivaths16 marked this conversation as resolved.
Show resolved Hide resolved
"""Called from the parent `InstanceDeleteCommand.ask` method.

Returns:
List of instances to be deleted.
"""
predicted_instances = []
# Select the instances to be deleted
for lf in context.labels.labeled_frames:
if lf.frame_idx >= params["frame_idx_threshold"]:
predicted_instances.extend(
[(lf, inst) for inst in lf.predicted_instances]
)
return predicted_instances

@classmethod
def ask(cls, context: CommandContext, params: Dict) -> bool:
current_video = context.state["video"]
frame_idx_thresh, okay = QtWidgets.QInputDialog.getInt(
context.app,
"Delete Instance beyond Frame Number...",
"Frame number after which instances to be deleted:",
1,
1,
len(current_video),
)
if okay:
params["frame_idx_threshold"] = frame_idx_thresh
return super().ask(context, params)


class TransposeInstances(EditCommand):
topics = [UpdateTopic.project_instances, UpdateTopic.tracks]

Expand Down
22 changes: 22 additions & 0 deletions tests/gui/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
ReplaceVideo,
OpenSkeleton,
SaveProjectAs,
DeleteFrameLimitPredictions,
get_new_version_filename,
)
from sleap.instance import Instance, LabeledFrame
Expand Down Expand Up @@ -847,6 +848,27 @@ def load_and_assert_changes(new_video_path: Path):
shutil.move(new_video_path, expected_video_path)


def test_DeleteFrameLimitPredictions(
centered_pair_predictions: Labels, centered_pair_vid: Video
):
"""Test deleting instances beyond a certain frame limit."""
labels = centered_pair_predictions

# Set-up command context
context = CommandContext.from_labels(labels)
context.state["video"] = centered_pair_vid

# Set-up params for the command
params = {"frame_idx_threshold": 900}

expected_instances = 423
predicted_instances = DeleteFrameLimitPredictions.get_frame_instance_list(
context, params
)

assert len(predicted_instances) == expected_instances


@pytest.mark.parametrize("export_extension", [".json.zip", ".slp"])
def test_exportLabelsPackage(export_extension, centered_pair_labels: Labels, tmpdir):
def assert_loaded_package_similar(path_to_pkg: Path, sugg=False, pred=False):
Expand Down