Skip to content

commands

sleap.gui.commands

Module for gui command context and commands objects.

Each open project (i.e., MainWindow) will have its own CommandContext. The context enables commands to access and modify the GuiState and Labels, as well as potentially maintaining a command history (so we can add support for undo!). See sleap.gui.app for how the context is created and used.

Every command will have both a method in CommandContext (this is what should be used to trigger the command, e.g., connected to the menu action) and a class which inherits from AppCommand (or a more specialized class such as NavCommand, GoIteratorCommand, or EditCommand). Note that this code relies on inheritance, so some care and attention is required.

A typical command will override the ask and do_action methods. If the command updates something which affects the GUI, it should override the topic attribute (this then gets passed back to the update_callback from the context. If a command doesn't require any input from the user, then it doesn't need to override the ask method.

If it's not possible to separate the GUI "ask" and the non-GUI "do" code, then instead of ask and do_action you should add an ask_and_do method (for instance, DeleteDialogCommand and MergeProject show dialogues which handle both the GUI and the action). Ideally we'd endorse separation of "ask" and "do" for all commands (this is important if we're going to implement undo)-- for now it's at least easy to see where this separation is violated.

Classes:

Name Description
AddInstance
AddMissingInstanceNodes
AddVideo
AppCommand

Base class for specific commands.

CommandContext

Context within in which commands are executed.

DeleteFrameLimitPredictions
DeleteMultipleTracks
EditCommand

Class for commands which change data in project.

ExportLabeledClip

Export a labeled video clip with labels and edges.

ExportLabelsSubset

Export a subset of labels to a new file with either images or a trimmed video.

ExportVideoClip

Base class for exporting video clips.

FakeApp

Use if you want to execute commands independently of the GUI app.

GoIteratorCommand
InstanceDeleteCommand
LoadLabelsObject
OpenSkeleton
ReplaceVideo
SaveProjectAs
SetInstancePointLocations

Sets locations for node(s) for an instance.

SetInstancePointVisibility

Toggles visibility set for a node for an instance.

ToggleGrayscale
UpdateTopic

Topics so context can tell callback what was updated by the command.

Functions:

Name Description
copy_to_clipboard

Copy a string to the system clipboard.

export_dataset_gui

Export dataset with image data and display progress GUI dialog.

get_new_version_filename

Increment version number in filenames that end in .v###.slp.

open_file

Opens file in native system file browser or registered application.

open_website

Open website in default browser.

AddInstance

Bases: EditCommand

Methods:

Name Description
create_new_instance

Create new instance.

fill_missing_nodes

Fill in missing nodes for new instance.

find_instance_to_copy_from

Find instance to copy from.

get_previous_frame_index

Returns index of previous frame.

set_visible_nodes

Sets visible nodes for new instance.

Source code in sleap/gui/commands.py
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
class AddInstance(EditCommand):
    topics = [UpdateTopic.frame, UpdateTopic.project_instances, UpdateTopic.suggestions]

    @classmethod
    def do_action(cls, context: CommandContext, params: dict):
        copy_instance = params.get("copy_instance", None)
        init_method = params.get("init_method", "best")
        location = params.get("location", None)
        mark_complete = params.get("mark_complete", False)
        offset = params.get("offset", 0)

        if context.state["labeled_frame"] is None:
            return

        if len(context.state["skeleton"]) == 0:
            return

        (
            copy_instance,
            from_predicted,
            from_prev_frame,
        ) = AddInstance.find_instance_to_copy_from(
            context, copy_instance=copy_instance, init_method=init_method
        )

        new_instance = AddInstance.create_new_instance(
            context=context,
            from_predicted=from_predicted,
            copy_instance=copy_instance,
            mark_complete=mark_complete,
            init_method=init_method,
            location=location,
            from_prev_frame=from_prev_frame,
            offset=offset,
        )

        # add new instance
        if new_instance not in context.state["labeled_frame"].instances:
            context.state["labeled_frame"].instances.append(new_instance)

        existing_tracks = [track.name for track in context.labels.tracks]
        if (
            new_instance.track is not None
            and new_instance.track.name not in existing_tracks
        ):
            context.labels.tracks.append(new_instance.track)

        if context.state["labeled_frame"] not in context.labels:
            context.labels.append(context.state["labeled_frame"])

        context.labels.update()

    @staticmethod
    def create_new_instance(
        context: CommandContext,
        from_predicted: Optional[PredictedInstance],
        copy_instance: Optional[Union[Instance, PredictedInstance]],
        mark_complete: bool,
        init_method: str,
        location: Optional[QtCore.QPoint],
        from_prev_frame: bool,
        offset: int = 0,
    ) -> Instance:
        """Create new instance."""

        # Now create the new instance
        new_instance = Instance.empty(
            skeleton=context.state["skeleton"],
            from_predicted=from_predicted,
        )

        has_missing_nodes = AddInstance.set_visible_nodes(
            context=context,
            copy_instance=copy_instance,
            new_instance=new_instance,
            mark_complete=mark_complete,
            init_method=init_method,
            location=location,
            offset=offset,
        )

        if has_missing_nodes:
            AddInstance.fill_missing_nodes(
                context=context,
                copy_instance=copy_instance,
                init_method=init_method,
                new_instance=new_instance,
                location=location,
            )

        # If we're copying a predicted instance or from another frame, copy the track
        if hasattr(copy_instance, "score") or from_prev_frame:
            copy_instance = cast(Union[PredictedInstance, Instance], copy_instance)
            new_instance.track = copy_instance.track

        return new_instance

    @staticmethod
    def fill_missing_nodes(
        context: CommandContext,
        copy_instance: Optional[Union[Instance, PredictedInstance]],
        init_method: str,
        new_instance: Instance,
        location: Optional[QtCore.QPoint],
    ):
        """Fill in missing nodes for new instance.

        Args:
            context: The command context.
            copy_instance: The instance to copy from.
            init_method: The initialization method.
            new_instance: The new instance.
            location: The location of the instance.

        Returns:
            None
        """

        # mark the node as not "visible" if we're copying from a predicted instance
        # without this node
        is_visible = copy_instance is None or (not hasattr(copy_instance, "score"))

        if init_method == "force_directed":
            AddMissingInstanceNodes.add_force_directed_nodes(
                context=context,
                instance=new_instance,
                visible=is_visible,
                center_point=location,
            )
        elif init_method == "random":
            AddMissingInstanceNodes.add_random_nodes(
                context=context, instance=new_instance, visible=is_visible
            )
        elif init_method == "template":
            AddMissingInstanceNodes.add_nodes_from_template(
                context=context,
                instance=new_instance,
                visible=is_visible,
                center_point=location,
            )
        else:
            AddMissingInstanceNodes.add_best_nodes(
                context=context, instance=new_instance, visible=is_visible
            )

    @staticmethod
    def set_visible_nodes(
        context: CommandContext,
        copy_instance: Optional[Union[Instance, PredictedInstance]],
        new_instance: Instance,
        mark_complete: bool,
        init_method: str,
        location: Optional[QtCore.QPoint] = None,
        offset: int = 0,
    ) -> bool:
        """Sets visible nodes for new instance.

        Args:
            context: The command context.
            copy_instance: The instance to copy from.
            new_instance: The new instance.
            mark_complete: Whether to mark the instance as complete.
            init_method: The initialization method.
            location: The location of the mouse click if any.
            offset: The offset to apply to all nodes.

        Returns:
            Whether the new instance has missing nodes.
        """
        if copy_instance is None:
            return True

        has_missing_nodes = False

        # Calculate scale factor for getting new x and y values.
        # Get video from context since instances don't have frame attribute
        old_video = context.state.get("video") or context.labels.videos[0]
        new_video = context.state.get("video") or context.labels.videos[0]
        old_size_width = old_video.shape[2]
        old_size_height = old_video.shape[1]
        new_size_width = new_video.shape[2]
        new_size_height = new_video.shape[1]
        scale_width = new_size_width / old_size_width
        scale_height = new_size_height / old_size_height

        # The offset is 0, except when using Ctrl + I or Add Instance button.
        offset_x = offset
        offset_y = offset

        # Using right click and context menu with option "best"
        if (init_method == "best") and (location is not None):
            reference_node = next(
                (node for node in copy_instance if not np.any(np.isnan(node["xy"]))),
                None,
            )
            reference_x, reference_y = reference_node["xy"]
            offset_x = location.x() - (reference_x * scale_width)
            offset_y = location.y() - (reference_y * scale_height)

        # Go through each node in skeleton.
        for node in context.state["skeleton"].node_names:
            # If we're copying from a skeleton that has this node.
            node_idx = context.state["skeleton"].node_names.index(node)
            if node in copy_instance.skeleton.node_names and not np.any(
                np.isnan(copy_instance.numpy()[node_idx])
            ):
                # Ensure x, y inside current frame, then copy x, y, and visible.
                # We don't want to copy a PredictedPoint or score attribute.
                point_data = copy_instance[node_idx]
                x_old, y_old = point_data["xy"]

                # Copy the instance without scale or offset if predicted
                if isinstance(copy_instance, PredictedInstance):
                    x_new = x_old
                    y_new = y_old
                else:
                    x_new = x_old * scale_width
                    y_new = y_old * scale_height

                # Apply offset if in bounds
                x_new_offset = x_new + offset_x
                y_new_offset = y_new + offset_y

                # Default visibility is same as copied instance.
                visible = point_data["visible"]

                # If the node is offset to outside the frame, mark as not visible.
                if x_new_offset < 0:
                    x_new = 0
                    visible = False
                elif x_new_offset > new_size_width:
                    x_new = new_size_width
                    visible = False
                else:
                    x_new = x_new_offset
                if y_new_offset < 0:
                    y_new = 0
                    visible = False
                elif y_new_offset > new_size_height:
                    y_new = new_size_height
                    visible = False
                else:
                    y_new = y_new_offset

                new_instance[node]["xy"] = np.array([x_new, y_new])
                new_instance[node]["visible"] = visible
                new_instance[node]["complete"] = mark_complete
                new_instance[node]["name"] = node
            else:
                has_missing_nodes = True

        return has_missing_nodes

    @staticmethod
    def find_instance_to_copy_from(
        context: CommandContext,
        copy_instance: Optional[Union[Instance, PredictedInstance]],
        init_method: bool,
    ) -> Tuple[
        Optional[Union[Instance, PredictedInstance]], Optional[PredictedInstance], bool
    ]:
        """Find instance to copy from.

        Args:
            context: The command context.
            copy_instance: The `Instance` to copy from.
            init_method: The initialization method.

        Returns:
            The instance to copy from, the predicted instance (if it is from a predicted
            instance, else None), and whether it's from a previous frame.
        """

        from_predicted = copy_instance
        from_prev_frame = False

        if init_method == "best" and copy_instance is None:
            selected_inst = context.state["instance"]
            if selected_inst is not None:
                # If the user has selected an instance, copy that one.
                copy_instance = selected_inst
                from_predicted = copy_instance

        if (
            init_method == "best" and copy_instance is None
        ) or init_method == "prediction":
            unused_predictions = context.state["labeled_frame"].unused_predictions
            if len(unused_predictions):
                # If there are predicted instances that don't correspond to an instance
                # in this frame, use the first predicted instance without
                # matching instance.
                copy_instance = unused_predictions[0]
                from_predicted = copy_instance

        if (
            init_method == "best" and copy_instance is None
        ) or init_method == "prior_frame":
            # Otherwise, if there are instances in previous frames,
            # copy the points from one of those instances.
            prev_idx = AddInstance.get_previous_frame_index(context)

            if prev_idx is not None:
                prev_instances = context.labels.find(
                    context.state["video"], prev_idx, return_new=True
                )[0].instances
                if len(prev_instances) > len(context.state["labeled_frame"].instances):
                    # If more instances in previous frame than current, then use the
                    # first unmatched instance.
                    copy_instance = prev_instances[
                        len(context.state["labeled_frame"].instances)
                    ]
                    from_prev_frame = True
                elif init_method == "best" and (
                    context.state["labeled_frame"].instances
                ):
                    # Otherwise, if there are already instances in current frame,
                    # copy the points from the last instance added to frame.
                    copy_instance = context.state["labeled_frame"].instances[-1]
                elif len(prev_instances):
                    # Otherwise use the last instance added to previous frame.
                    copy_instance = prev_instances[-1]
                    from_prev_frame = True

        from_predicted = from_predicted if hasattr(from_predicted, "score") else None
        from_predicted = cast(Optional[PredictedInstance], from_predicted)

        return copy_instance, from_predicted, from_prev_frame

    @staticmethod
    def get_previous_frame_index(context: CommandContext) -> Optional[int]:
        """Returns index of previous frame."""
        from sleap.sleap_io_adaptors.lf_labels_utils import iterate_labeled_frames

        frames_iter = iterate_labeled_frames(
            context.labels,
            context.state["video"],
            from_frame_idx=context.state["frame_idx"],
            reverse=True,
        )

        try:
            next_idx = next(frames_iter).frame_idx
        except Exception:
            return

        return next_idx

create_new_instance(context, from_predicted, copy_instance, mark_complete, init_method, location, from_prev_frame, offset=0) staticmethod

Create new instance.

Source code in sleap/gui/commands.py
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
@staticmethod
def create_new_instance(
    context: CommandContext,
    from_predicted: Optional[PredictedInstance],
    copy_instance: Optional[Union[Instance, PredictedInstance]],
    mark_complete: bool,
    init_method: str,
    location: Optional[QtCore.QPoint],
    from_prev_frame: bool,
    offset: int = 0,
) -> Instance:
    """Create new instance."""

    # Now create the new instance
    new_instance = Instance.empty(
        skeleton=context.state["skeleton"],
        from_predicted=from_predicted,
    )

    has_missing_nodes = AddInstance.set_visible_nodes(
        context=context,
        copy_instance=copy_instance,
        new_instance=new_instance,
        mark_complete=mark_complete,
        init_method=init_method,
        location=location,
        offset=offset,
    )

    if has_missing_nodes:
        AddInstance.fill_missing_nodes(
            context=context,
            copy_instance=copy_instance,
            init_method=init_method,
            new_instance=new_instance,
            location=location,
        )

    # If we're copying a predicted instance or from another frame, copy the track
    if hasattr(copy_instance, "score") or from_prev_frame:
        copy_instance = cast(Union[PredictedInstance, Instance], copy_instance)
        new_instance.track = copy_instance.track

    return new_instance

fill_missing_nodes(context, copy_instance, init_method, new_instance, location) staticmethod

Fill in missing nodes for new instance.

Parameters:

Name Type Description Default
context CommandContext

The command context.

required
copy_instance Optional[Union[Instance, PredictedInstance]]

The instance to copy from.

required
init_method str

The initialization method.

required
new_instance Instance

The new instance.

required
location Optional[QPoint]

The location of the instance.

required

Returns:

Type Description

None

Source code in sleap/gui/commands.py
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
@staticmethod
def fill_missing_nodes(
    context: CommandContext,
    copy_instance: Optional[Union[Instance, PredictedInstance]],
    init_method: str,
    new_instance: Instance,
    location: Optional[QtCore.QPoint],
):
    """Fill in missing nodes for new instance.

    Args:
        context: The command context.
        copy_instance: The instance to copy from.
        init_method: The initialization method.
        new_instance: The new instance.
        location: The location of the instance.

    Returns:
        None
    """

    # mark the node as not "visible" if we're copying from a predicted instance
    # without this node
    is_visible = copy_instance is None or (not hasattr(copy_instance, "score"))

    if init_method == "force_directed":
        AddMissingInstanceNodes.add_force_directed_nodes(
            context=context,
            instance=new_instance,
            visible=is_visible,
            center_point=location,
        )
    elif init_method == "random":
        AddMissingInstanceNodes.add_random_nodes(
            context=context, instance=new_instance, visible=is_visible
        )
    elif init_method == "template":
        AddMissingInstanceNodes.add_nodes_from_template(
            context=context,
            instance=new_instance,
            visible=is_visible,
            center_point=location,
        )
    else:
        AddMissingInstanceNodes.add_best_nodes(
            context=context, instance=new_instance, visible=is_visible
        )

find_instance_to_copy_from(context, copy_instance, init_method) staticmethod

Find instance to copy from.

Parameters:

Name Type Description Default
context CommandContext

The command context.

required
copy_instance Optional[Union[Instance, PredictedInstance]]

The Instance to copy from.

required
init_method bool

The initialization method.

required

Returns:

Type Description
Tuple[Optional[Union[Instance, PredictedInstance]], Optional[PredictedInstance], bool]

The instance to copy from, the predicted instance (if it is from a predicted instance, else None), and whether it's from a previous frame.

Source code in sleap/gui/commands.py
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
@staticmethod
def find_instance_to_copy_from(
    context: CommandContext,
    copy_instance: Optional[Union[Instance, PredictedInstance]],
    init_method: bool,
) -> Tuple[
    Optional[Union[Instance, PredictedInstance]], Optional[PredictedInstance], bool
]:
    """Find instance to copy from.

    Args:
        context: The command context.
        copy_instance: The `Instance` to copy from.
        init_method: The initialization method.

    Returns:
        The instance to copy from, the predicted instance (if it is from a predicted
        instance, else None), and whether it's from a previous frame.
    """

    from_predicted = copy_instance
    from_prev_frame = False

    if init_method == "best" and copy_instance is None:
        selected_inst = context.state["instance"]
        if selected_inst is not None:
            # If the user has selected an instance, copy that one.
            copy_instance = selected_inst
            from_predicted = copy_instance

    if (
        init_method == "best" and copy_instance is None
    ) or init_method == "prediction":
        unused_predictions = context.state["labeled_frame"].unused_predictions
        if len(unused_predictions):
            # If there are predicted instances that don't correspond to an instance
            # in this frame, use the first predicted instance without
            # matching instance.
            copy_instance = unused_predictions[0]
            from_predicted = copy_instance

    if (
        init_method == "best" and copy_instance is None
    ) or init_method == "prior_frame":
        # Otherwise, if there are instances in previous frames,
        # copy the points from one of those instances.
        prev_idx = AddInstance.get_previous_frame_index(context)

        if prev_idx is not None:
            prev_instances = context.labels.find(
                context.state["video"], prev_idx, return_new=True
            )[0].instances
            if len(prev_instances) > len(context.state["labeled_frame"].instances):
                # If more instances in previous frame than current, then use the
                # first unmatched instance.
                copy_instance = prev_instances[
                    len(context.state["labeled_frame"].instances)
                ]
                from_prev_frame = True
            elif init_method == "best" and (
                context.state["labeled_frame"].instances
            ):
                # Otherwise, if there are already instances in current frame,
                # copy the points from the last instance added to frame.
                copy_instance = context.state["labeled_frame"].instances[-1]
            elif len(prev_instances):
                # Otherwise use the last instance added to previous frame.
                copy_instance = prev_instances[-1]
                from_prev_frame = True

    from_predicted = from_predicted if hasattr(from_predicted, "score") else None
    from_predicted = cast(Optional[PredictedInstance], from_predicted)

    return copy_instance, from_predicted, from_prev_frame

get_previous_frame_index(context) staticmethod

Returns index of previous frame.

Source code in sleap/gui/commands.py
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
@staticmethod
def get_previous_frame_index(context: CommandContext) -> Optional[int]:
    """Returns index of previous frame."""
    from sleap.sleap_io_adaptors.lf_labels_utils import iterate_labeled_frames

    frames_iter = iterate_labeled_frames(
        context.labels,
        context.state["video"],
        from_frame_idx=context.state["frame_idx"],
        reverse=True,
    )

    try:
        next_idx = next(frames_iter).frame_idx
    except Exception:
        return

    return next_idx

set_visible_nodes(context, copy_instance, new_instance, mark_complete, init_method, location=None, offset=0) staticmethod

Sets visible nodes for new instance.

Parameters:

Name Type Description Default
context CommandContext

The command context.

required
copy_instance Optional[Union[Instance, PredictedInstance]]

The instance to copy from.

required
new_instance Instance

The new instance.

required
mark_complete bool

Whether to mark the instance as complete.

required
init_method str

The initialization method.

required
location Optional[QPoint]

The location of the mouse click if any.

None
offset int

The offset to apply to all nodes.

0

Returns:

Type Description
bool

Whether the new instance has missing nodes.

Source code in sleap/gui/commands.py
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
@staticmethod
def set_visible_nodes(
    context: CommandContext,
    copy_instance: Optional[Union[Instance, PredictedInstance]],
    new_instance: Instance,
    mark_complete: bool,
    init_method: str,
    location: Optional[QtCore.QPoint] = None,
    offset: int = 0,
) -> bool:
    """Sets visible nodes for new instance.

    Args:
        context: The command context.
        copy_instance: The instance to copy from.
        new_instance: The new instance.
        mark_complete: Whether to mark the instance as complete.
        init_method: The initialization method.
        location: The location of the mouse click if any.
        offset: The offset to apply to all nodes.

    Returns:
        Whether the new instance has missing nodes.
    """
    if copy_instance is None:
        return True

    has_missing_nodes = False

    # Calculate scale factor for getting new x and y values.
    # Get video from context since instances don't have frame attribute
    old_video = context.state.get("video") or context.labels.videos[0]
    new_video = context.state.get("video") or context.labels.videos[0]
    old_size_width = old_video.shape[2]
    old_size_height = old_video.shape[1]
    new_size_width = new_video.shape[2]
    new_size_height = new_video.shape[1]
    scale_width = new_size_width / old_size_width
    scale_height = new_size_height / old_size_height

    # The offset is 0, except when using Ctrl + I or Add Instance button.
    offset_x = offset
    offset_y = offset

    # Using right click and context menu with option "best"
    if (init_method == "best") and (location is not None):
        reference_node = next(
            (node for node in copy_instance if not np.any(np.isnan(node["xy"]))),
            None,
        )
        reference_x, reference_y = reference_node["xy"]
        offset_x = location.x() - (reference_x * scale_width)
        offset_y = location.y() - (reference_y * scale_height)

    # Go through each node in skeleton.
    for node in context.state["skeleton"].node_names:
        # If we're copying from a skeleton that has this node.
        node_idx = context.state["skeleton"].node_names.index(node)
        if node in copy_instance.skeleton.node_names and not np.any(
            np.isnan(copy_instance.numpy()[node_idx])
        ):
            # Ensure x, y inside current frame, then copy x, y, and visible.
            # We don't want to copy a PredictedPoint or score attribute.
            point_data = copy_instance[node_idx]
            x_old, y_old = point_data["xy"]

            # Copy the instance without scale or offset if predicted
            if isinstance(copy_instance, PredictedInstance):
                x_new = x_old
                y_new = y_old
            else:
                x_new = x_old * scale_width
                y_new = y_old * scale_height

            # Apply offset if in bounds
            x_new_offset = x_new + offset_x
            y_new_offset = y_new + offset_y

            # Default visibility is same as copied instance.
            visible = point_data["visible"]

            # If the node is offset to outside the frame, mark as not visible.
            if x_new_offset < 0:
                x_new = 0
                visible = False
            elif x_new_offset > new_size_width:
                x_new = new_size_width
                visible = False
            else:
                x_new = x_new_offset
            if y_new_offset < 0:
                y_new = 0
                visible = False
            elif y_new_offset > new_size_height:
                y_new = new_size_height
                visible = False
            else:
                y_new = y_new_offset

            new_instance[node]["xy"] = np.array([x_new, y_new])
            new_instance[node]["visible"] = visible
            new_instance[node]["complete"] = mark_complete
            new_instance[node]["name"] = node
        else:
            has_missing_nodes = True

    return has_missing_nodes

AddMissingInstanceNodes

Bases: EditCommand

Methods:

Name Description
get_rect_center_xy

Returns x, y at center of rect.

get_xy_in_rect

Returns random x, y coordinates within given rect.

Source code in sleap/gui/commands.py
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
class AddMissingInstanceNodes(EditCommand):
    topics = [UpdateTopic.frame]

    @classmethod
    def do_action(cls, context: CommandContext, params: dict):
        instance = params["instance"]
        visible = params.get("visible", False)

        cls.add_best_nodes(context, instance, visible)

    @classmethod
    def add_best_nodes(cls, context, instance, visible):
        # Try placing missing nodes using a "template" instance
        cls.add_nodes_from_template(context, instance, visible)

        # If the "template" instance has missing nodes (i.e., a node that isn't
        # labeled on any of the instances we used to generate the template),
        # then adding nodes from the template may still result in missing nodes.
        # So we'll use random placement for anything that's still missing.
        cls.add_random_nodes(context, instance, visible)

    @classmethod
    def add_random_nodes(cls, context, instance, visible):
        # TODO: Move this to Instance so we can do this on-demand
        # the rect that's currently visible in the window view
        in_view_rect = context.app.player.getVisibleRect()

        input_arrays = instance.points

        for node_name in context.state["skeleton"].node_names:
            node_idx = context.state["skeleton"].node_names.index(node_name)
            if node_name not in instance.points["name"] or np.any(
                np.isnan(instance.numpy()[node_idx])
            ):
                # pick random points within currently zoomed view
                x, y = cls.get_xy_in_rect(in_view_rect)
                # set point for node

                input_array = np.array(
                    (np.array([x, y]), visible, False, node_name),
                    dtype=[
                        ("xy", "<f8", (2,)),
                        ("visible", "bool"),
                        ("complete", "bool"),
                        ("name", "O"),
                    ],
                )
                input_arrays[node_idx] = input_array
            else:
                x, y = instance.points[node_idx]["xy"]
                visible = instance.points[node_idx]["visible"]
                complete = instance.points[node_idx]["complete"]
                input_arrays[node_idx] = np.array(
                    (np.array([x, y]), visible, complete, node_name),
                    dtype=[
                        ("xy", "<f8", (2,)),
                        ("visible", "bool"),
                        ("complete", "bool"),
                        ("name", "O"),
                    ],
                )
        instance.points = PointsArray.from_array(input_arrays)

    @staticmethod
    def get_xy_in_rect(rect: QtCore.QRectF):
        """Returns random x, y coordinates within given rect."""
        x = rect.x() + (rect.width() * 0.1) + (np.random.rand() * rect.width() * 0.8)
        y = rect.y() + (rect.height() * 0.1) + (np.random.rand() * rect.height() * 0.8)
        return x, y

    @staticmethod
    def get_rect_center_xy(rect: QtCore.QRectF):
        """Returns x, y at center of rect."""

    @classmethod
    def add_nodes_from_template(
        cls,
        context,
        instance,  # should be zeroes
        visible: bool = False,
        center_point: QtCore.QPoint = None,
    ):
        # Get the "template" instance
        # context.labels.get_template_instance()
        template_points = get_template_instance_points(
            context.labels, skeleton=instance.skeleton
        )

        # Align the template on to the current instance with missing points
        if not np.all(np.isnan(instance.numpy())) and not np.allclose(
            instance.numpy(), 0.0
        ):
            aligned_template = align.align_instance_points(
                source_points_array=template_points,
                target_points_array=instance.points["xy"],
            )
        else:
            template_mean = np.nanmean(template_points, axis=0)

            center_point = center_point or context.app.player.getVisibleRect().center()
            center = np.array([center_point.x(), center_point.y()])

            aligned_template = (template_points - template_mean) + center

        input_arrays = PointsArray.empty(len(instance.skeleton.nodes))
        # Make missing points from the aligned template
        for i, node in enumerate(instance.skeleton.nodes):
            if np.all(np.isnan(instance.points[i]["xy"])) or np.allclose(
                instance.points[i]["xy"], 0.0, equal_nan=True
            ):
                x, y = aligned_template[i]
                input_array = np.array(
                    [([x, y], visible, False, node.name)],
                    dtype=[
                        ("xy", "<f8", (2,)),
                        ("visible", "bool"),
                        ("complete", "bool"),
                        ("name", "O"),
                    ],
                )
                input_arrays[i] = input_array
            else:
                input_arrays[i] = instance.points[i]

        instance.points = PointsArray.from_array(input_arrays)

    @classmethod
    def add_force_directed_nodes(
        cls, context, instance, visible, center_point: QtCore.QPoint = None
    ):
        import networkx as nx

        center_point = center_point or context.app.player.getVisibleRect().center()
        center_tuple = (center_point.x(), center_point.y())

        node_positions = nx.spring_layout(
            G=to_graph(context.state["skeleton"]), center=center_tuple, scale=50
        )

        for node, pos in node_positions.items():
            # Create the input array first, then use PointsArray.from_array()
            node_name = node if isinstance(node, str) else node.name
            instance[node_name]["xy"] = np.array([pos[0], pos[1]])
            instance[node_name]["visible"] = visible
            instance[node_name]["complete"] = False
            instance[node_name]["name"] = node_name

get_rect_center_xy(rect) staticmethod

Returns x, y at center of rect.

Source code in sleap/gui/commands.py
3764
3765
3766
@staticmethod
def get_rect_center_xy(rect: QtCore.QRectF):
    """Returns x, y at center of rect."""

get_xy_in_rect(rect) staticmethod

Returns random x, y coordinates within given rect.

Source code in sleap/gui/commands.py
3757
3758
3759
3760
3761
3762
@staticmethod
def get_xy_in_rect(rect: QtCore.QRectF):
    """Returns random x, y coordinates within given rect."""
    x = rect.x() + (rect.width() * 0.1) + (np.random.rand() * rect.width() * 0.8)
    y = rect.y() + (rect.height() * 0.1) + (np.random.rand() * rect.height() * 0.8)
    return x, y

AddVideo

Bases: EditCommand

Methods:

Name Description
ask

Shows gui for adding video to project.

Source code in sleap/gui/commands.py
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
class AddVideo(EditCommand):
    topics = [UpdateTopic.video]

    @staticmethod
    def do_action(context: CommandContext, params: dict):
        import_list = params["import_list"]

        new_videos = ImportVideos.create_videos(import_list)
        video = None
        for video in new_videos:
            # Add to labels
            if video not in context.labels.videos:
                context.labels.videos.append(video)
                context.labels.update()
            context.changestack_push("add video")

        # Load if no video currently loaded
        if context.state["video"] is None:
            context.state["video"] = video

    @staticmethod
    def ask(context: CommandContext, params: dict) -> bool:
        """Shows gui for adding video to project."""
        params["import_list"] = ImportVideos().ask()

        return len(params["import_list"]) > 0

ask(context, params) staticmethod

Shows gui for adding video to project.

Source code in sleap/gui/commands.py
2100
2101
2102
2103
2104
2105
@staticmethod
def ask(context: CommandContext, params: dict) -> bool:
    """Shows gui for adding video to project."""
    params["import_list"] = ImportVideos().ask()

    return len(params["import_list"]) > 0

AppCommand

Base class for specific commands.

Note that this is not an abstract base class. For specific commands, you should override ask and/or do_action methods, or add an ask_and_do method. In many cases you'll want to override the topics and does_edits attributes. That said, these are not virtual methods/attributes and have are implemented in the base class with default behaviors (i.e., doing nothing).

You should not override execute or do_with_signal.

Attributes:

Name Type Description
topics List[UpdateTopic]

List of UpdateTopic items. Override this to indicate what should be updated after command is executed.

does_edits bool

Whether command will modify data that could be saved.

Methods:

Name Description
ask

Method for information gathering.

do_action

Method for performing action.

do_with_signal

Wrapper to perform action and notify/track changes.

execute

Entry point for running command.

Source code in sleap/gui/commands.py
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
class AppCommand:
    """Base class for specific commands.

    Note that this is not an abstract base class. For specific commands, you
    should override `ask` and/or `do_action` methods, or add an `ask_and_do`
    method. In many cases you'll want to override the `topics` and `does_edits`
    attributes. That said, these are not virtual methods/attributes and have
    are implemented in the base class with default behaviors (i.e., doing
    nothing).

    You should not override `execute` or `do_with_signal`.

    Attributes:
        topics: List of `UpdateTopic` items. Override this to indicate what
            should be updated after command is executed.
        does_edits: Whether command will modify data that could be saved.
    """

    topics: List[UpdateTopic] = []
    does_edits: bool = False

    def execute(self, context: "CommandContext", params: dict = None):
        """Entry point for running command.

        This calls internal methods to gather information required for
        execution, perform the action, and notify about changes.

        Ideally, any information gathering should be performed in the `ask`
        method, and be added to the `params` dictionary which then gets
        passed to `do_action`. The `ask` method should not modify state.

        (This will make it easier to add support for undo,
        using an `undo_action` which will be given the same `params`
        dictionary.)

        If it's not possible to easily separate information gathering from
        performing the action, the child class should implement `ask_and_do`,
        which it turn should call `do_with_signal` to notify about changes.

        Args:
            context: This is the `CommandContext` in which the command will
                execute. Commands will use this to access `MainWindow`,
                `GuiState`, and `Labels`.
            params: Dictionary of any params for command.
        """
        params = params or dict()

        if hasattr(self, "ask_and_do") and callable(self.ask_and_do):
            self.ask_and_do(context, params)
        else:
            okay = self.ask(context, params)
            if okay:
                self.do_with_signal(context, params)

    @staticmethod
    def ask(context: "CommandContext", params: dict) -> bool:
        """Method for information gathering.

        Returns:
            Whether to perform action. By default returns True, but this is
            where we should return False if we prompt user for confirmation
            and they abort.
        """
        return True

    @staticmethod
    def do_action(context: "CommandContext", params: dict):
        """Method for performing action."""
        pass

    @classmethod
    def do_with_signal(cls, context: "CommandContext", params: dict):
        """Wrapper to perform action and notify/track changes.

        Don't override this method!
        """
        cls.do_action(context, params)
        if cls.topics:
            context.signal_update(cls.topics)
        if cls.does_edits:
            context.changestack_push(cls.__name__)

ask(context, params) staticmethod

Method for information gathering.

Returns:

Type Description
bool

Whether to perform action. By default returns True, but this is where we should return False if we prompt user for confirmation and they abort.

Source code in sleap/gui/commands.py
197
198
199
200
201
202
203
204
205
206
@staticmethod
def ask(context: "CommandContext", params: dict) -> bool:
    """Method for information gathering.

    Returns:
        Whether to perform action. By default returns True, but this is
        where we should return False if we prompt user for confirmation
        and they abort.
    """
    return True

do_action(context, params) staticmethod

Method for performing action.

Source code in sleap/gui/commands.py
208
209
210
211
@staticmethod
def do_action(context: "CommandContext", params: dict):
    """Method for performing action."""
    pass

do_with_signal(context, params) classmethod

Wrapper to perform action and notify/track changes.

Don't override this method!

Source code in sleap/gui/commands.py
213
214
215
216
217
218
219
220
221
222
223
@classmethod
def do_with_signal(cls, context: "CommandContext", params: dict):
    """Wrapper to perform action and notify/track changes.

    Don't override this method!
    """
    cls.do_action(context, params)
    if cls.topics:
        context.signal_update(cls.topics)
    if cls.does_edits:
        context.changestack_push(cls.__name__)

execute(context, params=None)

Entry point for running command.

This calls internal methods to gather information required for execution, perform the action, and notify about changes.

Ideally, any information gathering should be performed in the ask method, and be added to the params dictionary which then gets passed to do_action. The ask method should not modify state.

(This will make it easier to add support for undo, using an undo_action which will be given the same params dictionary.)

If it's not possible to easily separate information gathering from performing the action, the child class should implement ask_and_do, which it turn should call do_with_signal to notify about changes.

Parameters:

Name Type Description Default
context 'CommandContext'

This is the CommandContext in which the command will execute. Commands will use this to access MainWindow, GuiState, and Labels.

required
params dict

Dictionary of any params for command.

None
Source code in sleap/gui/commands.py
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
def execute(self, context: "CommandContext", params: dict = None):
    """Entry point for running command.

    This calls internal methods to gather information required for
    execution, perform the action, and notify about changes.

    Ideally, any information gathering should be performed in the `ask`
    method, and be added to the `params` dictionary which then gets
    passed to `do_action`. The `ask` method should not modify state.

    (This will make it easier to add support for undo,
    using an `undo_action` which will be given the same `params`
    dictionary.)

    If it's not possible to easily separate information gathering from
    performing the action, the child class should implement `ask_and_do`,
    which it turn should call `do_with_signal` to notify about changes.

    Args:
        context: This is the `CommandContext` in which the command will
            execute. Commands will use this to access `MainWindow`,
            `GuiState`, and `Labels`.
        params: Dictionary of any params for command.
    """
    params = params or dict()

    if hasattr(self, "ask_and_do") and callable(self.ask_and_do):
        self.ask_and_do(context, params)
    else:
        okay = self.ask(context, params)
        if okay:
            self.do_with_signal(context, params)

CommandContext

Context within in which commands are executed.

When you create a new command, you should both create a class for the command (which inherits from CommandClass) and add a distinct method for the command in the CommandContext class. This method is what should be connected/called from other code to invoke the command.

Attributes:

Name Type Description
state GuiState

The GuiState object used to store state and pass messages.

app 'MainWindow'

The MainWindow, available for commands that modify the app.

update_callback Optional[Callable]

A callback to receive update notifications. This function should accept a list of UpdateTopic items.

Methods:

Name Description
addCurrentFrameAsSuggestion

Add current frame as a suggestion.

addTrack

Creates new track and moves selected instance into this track.

addUserInstancesFromPredictions

Create user instance from a predicted instance.

addVideo

Shows gui for adding videos to project.

changestack_clear

Clears stack of changes.

changestack_push

Adds to stack of changes made by user.

changestack_savepoint

Marks that project was just saved.

checkForUpdates

Check for updates online.

clearSuggestions

Clear all suggestions.

completeInstanceNodes

Adds missing nodes to given instance.

copyInstance

Copy the selected instance to the instance clipboard.

copyInstanceTrack

Copies the selected instance's track to the track clipboard.

deleteAreaPredictions

Gui for deleting instances within some rect on frame images.

deleteClipPredictions

Deletes all predictions within selected range of video frames.

deleteDialog

Deletes using options selected in a dialog.

deleteEdge

Removes (currently selected) edge from skeleton.

deleteFrameLimitPredictions

Gui for deleting instances beyond some frame number.

deleteFramePredictions

Deletes all predictions on current frame.

deleteInstanceLimitPredictions

Gui for deleting instances beyond some number in each frame.

deleteLowScorePredictions

Gui for deleting instances below some score threshold.

deleteMultipleTracks

Delete all tracks.

deleteNode

Removes (currently selected) node from skeleton.

deletePredictions

Deletes all predicted instances in project.

deleteSelectedInstance

Deletes currently selected instance.

deleteSelectedInstanceTrack

Deletes all instances from track of currently selected instance.

deleteTrack

Delete a track and remove from all instances.

execute

Execute command in this context, passing named arguments.

exportAnalysisFile

Shows gui for exporting analysis h5 file.

exportCSVFile

Shows gui for exporting analysis csv file.

exportFullPackage

Gui for exporting the dataset with any labeled frames and suggestions.

exportLabeledClip

Shows gui for exporting clip with visual annotations.

exportLabelsSubset

Exports a selected range of video frames and their corresponding labels.

exportNWB

Show gui for exporting nwb file.

exportTrainingPackage

Gui for exporting the dataset with user-labeled images and suggestions.

exportUserLabelsPackage

Gui for exporting the dataset with user-labeled images.

from_labels

Creates a command context for use independently of GUI app.

generateSuggestions

Generates suggestions using given params dictionary.

gotoFrame

Shows gui to go to frame by number.

gotoVideoAndFrame

Activates video and goes to frame.

importAnalysisFile

Imports SLEAP analysis hdf5 files.

importCoco

Imports COCO datasets.

importDLC

Imports DeepLabCut datasets.

importDLCFolder

Imports multiple DeepLabCut datasets.

importNWB

Imports NWB datasets.

lastInteractedFrame

Goes to last frame that user interacted with.

loadLabelsObject

Loads a Labels object into the GUI, replacing any currently loaded.

loadProjectFile

Loads given labels file into GUI.

mergeProject

Starts gui for importing another dataset into currently one.

newEdge

Adds new edge to skeleton.

newInstance

Creates a new instance, copying node coordinates as appropriate.

newNode

Adds new node to skeleton.

newProject

Create a new project in a new window.

nextLabeledFrame

Goes to labeled frame after current frame.

nextSuggestedFrame

Goes to next suggested frame.

nextTrackFrame

Goes to next frame on which a track starts.

nextUserLabeledFrame

Goes to next labeled frame with user instances.

openPrereleaseVersion

Open the current prerelease version.

openProject

Allows user to select and then open a saved project.

openSkeleton

Shows gui for loading saved skeleton into project.

openSkeletonTemplate

Shows gui for loading saved skeleton into project.

openStableVersion

Open the current stable version.

openWebsite

Open a website from URL using the native system browser.

pasteInstance

Paste the instance from the clipboard as a new copy.

pasteInstanceTrack

Pastes the track in the clipboard to the selected instance.

prevSuggestedFrame

Goes to previous suggested frame.

previousLabeledFrame

Goes to labeled frame prior to current frame.

removeSuggestion

Remove the selected frame from suggestions.

removeVideo

Removes selected video from project.

replaceVideo

Shows gui for replacing videos to project.

saveProject

Show gui to save project (or save as if not yet saved).

saveProjectAs

Show gui to save project as a new file.

saveSkeleton

Shows gui for saving skeleton from project.

selectToFrame

Shows gui to go to frame by number.

setInstancePointVisibility

Toggles visibility set for a node for an instance.

setInstanceTrack

Sets track for selected instance.

setNodeName

Changes name of node in skeleton.

setNodeSymmetry

Sets node symmetry in skeleton.

setPointLocations

Sets locations for node(s) for an instance.

setTrackName

Sets name for track.

showImportVideos

Show video importer GUI without the file browser.

signal_update

Calls the update callback after data has been changed.

toggleGrayscale

Toggles grayscale setting for current video.

transposeInstance

Transposes tracks for two instances.

updateEdges

Called when edges in skeleton have been changed.

Source code in sleap/gui/commands.py
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
@attr.s(auto_attribs=True, eq=False)
class CommandContext:
    """
    Context within in which commands are executed.

    When you create a new command, you should both create a class for the
    command (which inherits from `CommandClass`) and add a distinct method
    for the command in the `CommandContext` class. This method is what should
    be connected/called from other code to invoke the command.

    Attributes:
        state: The `GuiState` object used to store state and pass messages.
        app: The `MainWindow`, available for commands that modify the app.
        update_callback: A callback to receive update notifications.
            This function should accept a list of `UpdateTopic` items.
    """

    state: GuiState
    app: "MainWindow"

    update_callback: Optional[Callable] = None
    _change_stack: List = attr.ib(default=attr.Factory(list))

    @classmethod
    def from_labels(cls, labels: Labels) -> "CommandContext":
        """Creates a command context for use independently of GUI app."""
        state = GuiState()
        state["labels"] = labels
        app = FakeApp(labels)
        return cls(state=state, app=app)

    @property
    def labels(self) -> Labels:
        """Alias to app.labels."""
        return self.app.labels

    def signal_update(self, what: List[UpdateTopic]):
        """Calls the update callback after data has been changed."""
        if callable(self.update_callback):
            self.update_callback(what)

    def changestack_push(self, change: str = ""):
        """Adds to stack of changes made by user."""
        # Currently the change doesn't store any data, and we're only using this
        # to determine if there are unsaved changes. Eventually we could use this
        # to support undo/redo.
        self._change_stack.append(change)
        # print(len(self._change_stack))
        self.state["has_changes"] = True

    def changestack_savepoint(self):
        """Marks that project was just saved."""
        self.changestack_push("SAVE")
        self.state["has_changes"] = False

    def changestack_clear(self):
        """Clears stack of changes."""
        self._change_stack = list()
        self.state["has_changes"] = False

    @property
    def has_any_changes(self):
        return len(self._change_stack) > 0

    def execute(self, command: Type[AppCommand], **kwargs):
        """Execute command in this context, passing named arguments."""
        command().execute(context=self, params=kwargs)

    # File commands

    def newProject(self):
        """Create a new project in a new window."""
        self.execute(NewProject)

    def loadLabelsObject(self, labels: Labels, filename: Optional[str] = None):
        """Loads a `Labels` object into the GUI, replacing any currently loaded.

        Args:
            labels: The `Labels` object to load.
            filename: The filename where this file is saved, if any.

        Returns:
            None.

        """
        self.execute(LoadLabelsObject, labels=labels, filename=filename)

    def loadProjectFile(self, filename: Union[str, Labels]):
        """Loads given labels file into GUI.

        Args:
            filename: The path to the saved labels dataset or the `Labels` object.
                If None, then don't do anything.

        Returns:
            None
        """
        self.execute(LoadProjectFile, filename=filename)

    def openProject(self, filename: Optional[str] = None, first_open: bool = False):
        """Allows user to select and then open a saved project.

        Args:
            filename: Filename of the project to be opened. If None, a file browser
                dialog will prompt the user for a path.
            first_open: Whether this is the first window opened. If True,
                then the new project is loaded into the current window
                rather than a new application window.

        Returns:
            None.
        """
        self.execute(OpenProject, filename=filename, first_open=first_open)

    def importNWB(self):
        """Imports NWB datasets."""
        self.execute(ImportNWB)

    def importCoco(self):
        """Imports COCO datasets."""
        self.execute(ImportCoco)

    def importDLC(self):
        """Imports DeepLabCut datasets."""
        self.execute(ImportDeepLabCut)

    def importDLCFolder(self):
        """Imports multiple DeepLabCut datasets."""
        self.execute(ImportDeepLabCutFolder)

    def importAnalysisFile(self):
        """Imports SLEAP analysis hdf5 files."""
        self.execute(ImportAnalysisFile)

    def saveProject(self):
        """Show gui to save project (or save as if not yet saved)."""
        self.execute(SaveProject)

    def saveProjectAs(self):
        """Show gui to save project as a new file."""
        self.execute(SaveProjectAs)

    def exportAnalysisFile(self, all_videos: bool = False):
        """Shows gui for exporting analysis h5 file."""
        self.execute(ExportAnalysisFile, all_videos=all_videos, csv=False)

    def exportCSVFile(self, all_videos: bool = False):
        """Shows gui for exporting analysis csv file."""
        self.execute(ExportAnalysisFile, all_videos=all_videos, csv=True)

    def exportNWB(self):
        """Show gui for exporting nwb file."""
        self.execute(SaveProjectAs, adaptor="nwb")

    def exportLabeledClip(self):
        """Shows gui for exporting clip with visual annotations."""
        self.execute(ExportLabeledClip)

    def exportUserLabelsPackage(self):
        """Gui for exporting the dataset with user-labeled images."""
        self.execute(ExportUserLabelsPackage)

    def exportTrainingPackage(self):
        """Gui for exporting the dataset with user-labeled images and suggestions."""
        self.execute(ExportTrainingPackage)

    def exportFullPackage(self):
        """Gui for exporting the dataset with any labeled frames and suggestions."""
        self.execute(ExportFullPackage)

    # Navigation Commands

    def previousLabeledFrame(self):
        """Goes to labeled frame prior to current frame."""
        self.execute(GoPreviousLabeledFrame)

    def nextLabeledFrame(self):
        """Goes to labeled frame after current frame."""
        self.execute(GoNextLabeledFrame)

    def nextUserLabeledFrame(self):
        """Goes to next labeled frame with user instances."""
        self.execute(GoNextUserLabeledFrame)

    def lastInteractedFrame(self):
        """Goes to last frame that user interacted with."""
        self.execute(GoLastInteractedFrame)

    def nextSuggestedFrame(self):
        """Goes to next suggested frame."""
        self.execute(GoNextSuggestedFrame)

    def prevSuggestedFrame(self):
        """Goes to previous suggested frame."""
        self.execute(GoPrevSuggestedFrame)

    def addCurrentFrameAsSuggestion(self):
        """Add current frame as a suggestion."""
        self.execute(AddSuggestion)

    def removeSuggestion(self):
        """Remove the selected frame from suggestions."""
        self.execute(RemoveSuggestion)

    def clearSuggestions(self):
        """Clear all suggestions."""
        self.execute(ClearSuggestions)

    def nextTrackFrame(self):
        """Goes to next frame on which a track starts."""
        self.execute(GoNextTrackFrame)

    def gotoFrame(self):
        """Shows gui to go to frame by number."""
        self.execute(GoFrameGui)

    def selectToFrame(self):
        """Shows gui to go to frame by number."""
        self.execute(SelectToFrameGui)

    def gotoVideoAndFrame(self, video: Video, frame_idx: int):
        """Activates video and goes to frame."""
        NavCommand.go_to(self, frame_idx, video)

    # Editing Commands

    def toggleGrayscale(self):
        """Toggles grayscale setting for current video."""
        self.execute(ToggleGrayscale)

    def addVideo(self):
        """Shows gui for adding videos to project."""
        self.execute(AddVideo)

    def showImportVideos(self, filenames: List[str]):
        """Show video importer GUI without the file browser."""
        self.execute(ShowImportVideos, filenames=filenames)

    def replaceVideo(self):
        """Shows gui for replacing videos to project."""
        self.execute(ReplaceVideo)

    def removeVideo(self):
        """Removes selected video from project."""
        self.execute(RemoveVideo)

    def openSkeletonTemplate(self):
        """Shows gui for loading saved skeleton into project."""
        self.execute(OpenSkeleton, template=True)

    def openSkeleton(self):
        """Shows gui for loading saved skeleton into project."""
        self.execute(OpenSkeleton)

    def saveSkeleton(self):
        """Shows gui for saving skeleton from project."""
        self.execute(SaveSkeleton)

    def newNode(self):
        """Adds new node to skeleton."""
        self.execute(NewNode)

    def deleteNode(self):
        """Removes (currently selected) node from skeleton."""
        self.execute(DeleteNode)

    def setNodeName(self, skeleton, node, name):
        """Changes name of node in skeleton."""
        self.execute(SetNodeName, skeleton=skeleton, node=node, name=name)

    def setNodeSymmetry(self, skeleton, node, symmetry: str):
        """Sets node symmetry in skeleton."""
        self.execute(SetNodeSymmetry, skeleton=skeleton, node=node, symmetry=symmetry)

    def updateEdges(self):
        """Called when edges in skeleton have been changed."""
        self.signal_update([UpdateTopic.skeleton])

    def newEdge(self, src_node, dst_node):
        """Adds new edge to skeleton."""
        self.execute(NewEdge, src_node=src_node, dst_node=dst_node)

    def deleteEdge(self):
        """Removes (currently selected) edge from skeleton."""
        self.execute(DeleteEdge)

    def deletePredictions(self):
        """Deletes all predicted instances in project."""
        self.execute(DeleteAllPredictions)

    def deleteFramePredictions(self):
        """Deletes all predictions on current frame."""
        self.execute(DeleteFramePredictions)

    def deleteClipPredictions(self):
        """Deletes all predictions within selected range of video frames."""
        self.execute(DeleteClipPredictions)

    def deleteAreaPredictions(self):
        """Gui for deleting instances within some rect on frame images."""
        self.execute(DeleteAreaPredictions)

    def deleteLowScorePredictions(self):
        """Gui for deleting instances below some score threshold."""
        self.execute(DeleteLowScorePredictions)

    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)

    def completeInstanceNodes(self, instance: Instance):
        """Adds missing nodes to given instance."""
        self.execute(AddMissingInstanceNodes, instance=instance)

    def newInstance(
        self,
        copy_instance: Optional[Instance] = None,
        init_method: str = "best",
        location: Optional[QtCore.QPoint] = None,
        mark_complete: bool = False,
        offset: int = 0,
    ):
        """Creates a new instance, copying node coordinates as appropriate.

        Args:
            copy_instance: The :class:`Instance` (or
                :class:`PredictedInstance`) which we want to copy.
            init_method: Method to use for positioning nodes.
            location: The location where instance should be added (if node init
                method supports custom location).
            mark_complete: Whether to mark the instance as complete.
            offset: Offset to apply to the location if given.
        """
        self.execute(
            AddInstance,
            copy_instance=copy_instance,
            init_method=init_method,
            location=location,
            mark_complete=mark_complete,
            offset=offset,
        )

    def setPointLocations(
        self, instance: Instance, nodes_locations: Dict[Node, Tuple[int, int]]
    ):
        """Sets locations for node(s) for an instance."""
        self.execute(
            SetInstancePointLocations,
            instance=instance,
            nodes_locations=nodes_locations,
        )

    def setInstancePointVisibility(self, instance: Instance, node: Node, visible: bool):
        """Toggles visibility set for a node for an instance."""
        self.execute(
            SetInstancePointVisibility, instance=instance, node=node, visible=visible
        )

    def addUserInstancesFromPredictions(self):
        """Create user instance from a predicted instance."""
        self.execute(AddUserInstancesFromPredictions)

    def copyInstance(self):
        """Copy the selected instance to the instance clipboard."""
        self.execute(CopyInstance)

    def pasteInstance(self):
        """Paste the instance from the clipboard as a new copy."""
        self.execute(PasteInstance)

    def deleteSelectedInstance(self):
        """Deletes currently selected instance."""
        self.execute(DeleteSelectedInstance)

    def deleteSelectedInstanceTrack(self):
        """Deletes all instances from track of currently selected instance."""
        self.execute(DeleteSelectedInstanceTrack)

    def deleteDialog(self):
        """Deletes using options selected in a dialog."""
        self.execute(DeleteDialogCommand)

    def addTrack(self):
        """Creates new track and moves selected instance into this track."""
        self.execute(AddTrack)

    def setInstanceTrack(self, new_track: "Track"):
        """Sets track for selected instance."""
        self.execute(SetSelectedInstanceTrack, new_track=new_track)

    def deleteTrack(self, track: "Track"):
        """Delete a track and remove from all instances."""
        self.execute(DeleteTrack, track=track)

    def deleteMultipleTracks(self, delete_all: bool = False):
        """Delete all tracks."""
        self.execute(DeleteMultipleTracks, delete_all=delete_all)

    def copyInstanceTrack(self):
        """Copies the selected instance's track to the track clipboard."""
        self.execute(CopyInstanceTrack)

    def pasteInstanceTrack(self):
        """Pastes the track in the clipboard to the selected instance."""
        self.execute(PasteInstanceTrack)

    def setTrackName(self, track: "Track", name: str):
        """Sets name for track."""
        self.execute(SetTrackName, track=track, name=name)

    def transposeInstance(self):
        """Transposes tracks for two instances.

        If there are only two instances, then this swaps tracks.
        Otherwise, it allows user to select the instances for which we want
        to swap tracks.
        """
        self.execute(TransposeInstances)

    def mergeProject(self, filenames: Optional[List[str]] = None):
        """Starts gui for importing another dataset into currently one."""
        self.execute(MergeProject, filenames=filenames)

    def generateSuggestions(self, params: Dict):
        """Generates suggestions using given params dictionary."""
        self.execute(GenerateSuggestions, **params)

    def openWebsite(self, url):
        """Open a website from URL using the native system browser."""
        self.execute(OpenWebsite, url=url)

    def checkForUpdates(self):
        """Check for updates online."""
        self.execute(CheckForUpdates)

    def openStableVersion(self):
        """Open the current stable version."""
        self.execute(OpenStableVersion)

    def openPrereleaseVersion(self):
        """Open the current prerelease version."""
        self.execute(OpenPrereleaseVersion)

    def exportLabelsSubset(
        self, as_package: bool = False, open_new_project: bool = True
    ):
        """Exports a selected range of video frames and their corresponding labels.

        Args:
            as_package: Whether to export as a package.
            open_new_project: Whether to open the exported labels in a new project GUI.
        """
        self.execute(
            ExportLabelsSubset, as_package=as_package, open_new_project=open_new_project
        )

labels property

Alias to app.labels.

addCurrentFrameAsSuggestion()

Add current frame as a suggestion.

Source code in sleap/gui/commands.py
429
430
431
def addCurrentFrameAsSuggestion(self):
    """Add current frame as a suggestion."""
    self.execute(AddSuggestion)

addTrack()

Creates new track and moves selected instance into this track.

Source code in sleap/gui/commands.py
619
620
621
def addTrack(self):
    """Creates new track and moves selected instance into this track."""
    self.execute(AddTrack)

addUserInstancesFromPredictions()

Create user instance from a predicted instance.

Source code in sleap/gui/commands.py
595
596
597
def addUserInstancesFromPredictions(self):
    """Create user instance from a predicted instance."""
    self.execute(AddUserInstancesFromPredictions)

addVideo()

Shows gui for adding videos to project.

Source code in sleap/gui/commands.py
463
464
465
def addVideo(self):
    """Shows gui for adding videos to project."""
    self.execute(AddVideo)

changestack_clear()

Clears stack of changes.

Source code in sleap/gui/commands.py
288
289
290
291
def changestack_clear(self):
    """Clears stack of changes."""
    self._change_stack = list()
    self.state["has_changes"] = False

changestack_push(change='')

Adds to stack of changes made by user.

Source code in sleap/gui/commands.py
274
275
276
277
278
279
280
281
def changestack_push(self, change: str = ""):
    """Adds to stack of changes made by user."""
    # Currently the change doesn't store any data, and we're only using this
    # to determine if there are unsaved changes. Eventually we could use this
    # to support undo/redo.
    self._change_stack.append(change)
    # print(len(self._change_stack))
    self.state["has_changes"] = True

changestack_savepoint()

Marks that project was just saved.

Source code in sleap/gui/commands.py
283
284
285
286
def changestack_savepoint(self):
    """Marks that project was just saved."""
    self.changestack_push("SAVE")
    self.state["has_changes"] = False

checkForUpdates()

Check for updates online.

Source code in sleap/gui/commands.py
668
669
670
def checkForUpdates(self):
    """Check for updates online."""
    self.execute(CheckForUpdates)

clearSuggestions()

Clear all suggestions.

Source code in sleap/gui/commands.py
437
438
439
def clearSuggestions(self):
    """Clear all suggestions."""
    self.execute(ClearSuggestions)

completeInstanceNodes(instance)

Adds missing nodes to given instance.

Source code in sleap/gui/commands.py
547
548
549
def completeInstanceNodes(self, instance: Instance):
    """Adds missing nodes to given instance."""
    self.execute(AddMissingInstanceNodes, instance=instance)

copyInstance()

Copy the selected instance to the instance clipboard.

Source code in sleap/gui/commands.py
599
600
601
def copyInstance(self):
    """Copy the selected instance to the instance clipboard."""
    self.execute(CopyInstance)

copyInstanceTrack()

Copies the selected instance's track to the track clipboard.

Source code in sleap/gui/commands.py
635
636
637
def copyInstanceTrack(self):
    """Copies the selected instance's track to the track clipboard."""
    self.execute(CopyInstanceTrack)

deleteAreaPredictions()

Gui for deleting instances within some rect on frame images.

Source code in sleap/gui/commands.py
531
532
533
def deleteAreaPredictions(self):
    """Gui for deleting instances within some rect on frame images."""
    self.execute(DeleteAreaPredictions)

deleteClipPredictions()

Deletes all predictions within selected range of video frames.

Source code in sleap/gui/commands.py
527
528
529
def deleteClipPredictions(self):
    """Deletes all predictions within selected range of video frames."""
    self.execute(DeleteClipPredictions)

deleteDialog()

Deletes using options selected in a dialog.

Source code in sleap/gui/commands.py
615
616
617
def deleteDialog(self):
    """Deletes using options selected in a dialog."""
    self.execute(DeleteDialogCommand)

deleteEdge()

Removes (currently selected) edge from skeleton.

Source code in sleap/gui/commands.py
515
516
517
def deleteEdge(self):
    """Removes (currently selected) edge from skeleton."""
    self.execute(DeleteEdge)

deleteFrameLimitPredictions()

Gui for deleting instances beyond some frame number.

Source code in sleap/gui/commands.py
543
544
545
def deleteFrameLimitPredictions(self):
    """Gui for deleting instances beyond some frame number."""
    self.execute(DeleteFrameLimitPredictions)

deleteFramePredictions()

Deletes all predictions on current frame.

Source code in sleap/gui/commands.py
523
524
525
def deleteFramePredictions(self):
    """Deletes all predictions on current frame."""
    self.execute(DeleteFramePredictions)

deleteInstanceLimitPredictions()

Gui for deleting instances beyond some number in each frame.

Source code in sleap/gui/commands.py
539
540
541
def deleteInstanceLimitPredictions(self):
    """Gui for deleting instances beyond some number in each frame."""
    self.execute(DeleteInstanceLimitPredictions)

deleteLowScorePredictions()

Gui for deleting instances below some score threshold.

Source code in sleap/gui/commands.py
535
536
537
def deleteLowScorePredictions(self):
    """Gui for deleting instances below some score threshold."""
    self.execute(DeleteLowScorePredictions)

deleteMultipleTracks(delete_all=False)

Delete all tracks.

Source code in sleap/gui/commands.py
631
632
633
def deleteMultipleTracks(self, delete_all: bool = False):
    """Delete all tracks."""
    self.execute(DeleteMultipleTracks, delete_all=delete_all)

deleteNode()

Removes (currently selected) node from skeleton.

Source code in sleap/gui/commands.py
495
496
497
def deleteNode(self):
    """Removes (currently selected) node from skeleton."""
    self.execute(DeleteNode)

deletePredictions()

Deletes all predicted instances in project.

Source code in sleap/gui/commands.py
519
520
521
def deletePredictions(self):
    """Deletes all predicted instances in project."""
    self.execute(DeleteAllPredictions)

deleteSelectedInstance()

Deletes currently selected instance.

Source code in sleap/gui/commands.py
607
608
609
def deleteSelectedInstance(self):
    """Deletes currently selected instance."""
    self.execute(DeleteSelectedInstance)

deleteSelectedInstanceTrack()

Deletes all instances from track of currently selected instance.

Source code in sleap/gui/commands.py
611
612
613
def deleteSelectedInstanceTrack(self):
    """Deletes all instances from track of currently selected instance."""
    self.execute(DeleteSelectedInstanceTrack)

deleteTrack(track)

Delete a track and remove from all instances.

Source code in sleap/gui/commands.py
627
628
629
def deleteTrack(self, track: "Track"):
    """Delete a track and remove from all instances."""
    self.execute(DeleteTrack, track=track)

execute(command, **kwargs)

Execute command in this context, passing named arguments.

Source code in sleap/gui/commands.py
297
298
299
def execute(self, command: Type[AppCommand], **kwargs):
    """Execute command in this context, passing named arguments."""
    command().execute(context=self, params=kwargs)

exportAnalysisFile(all_videos=False)

Shows gui for exporting analysis h5 file.

Source code in sleap/gui/commands.py
375
376
377
def exportAnalysisFile(self, all_videos: bool = False):
    """Shows gui for exporting analysis h5 file."""
    self.execute(ExportAnalysisFile, all_videos=all_videos, csv=False)

exportCSVFile(all_videos=False)

Shows gui for exporting analysis csv file.

Source code in sleap/gui/commands.py
379
380
381
def exportCSVFile(self, all_videos: bool = False):
    """Shows gui for exporting analysis csv file."""
    self.execute(ExportAnalysisFile, all_videos=all_videos, csv=True)

exportFullPackage()

Gui for exporting the dataset with any labeled frames and suggestions.

Source code in sleap/gui/commands.py
399
400
401
def exportFullPackage(self):
    """Gui for exporting the dataset with any labeled frames and suggestions."""
    self.execute(ExportFullPackage)

exportLabeledClip()

Shows gui for exporting clip with visual annotations.

Source code in sleap/gui/commands.py
387
388
389
def exportLabeledClip(self):
    """Shows gui for exporting clip with visual annotations."""
    self.execute(ExportLabeledClip)

exportLabelsSubset(as_package=False, open_new_project=True)

Exports a selected range of video frames and their corresponding labels.

Parameters:

Name Type Description Default
as_package bool

Whether to export as a package.

False
open_new_project bool

Whether to open the exported labels in a new project GUI.

True
Source code in sleap/gui/commands.py
680
681
682
683
684
685
686
687
688
689
690
691
def exportLabelsSubset(
    self, as_package: bool = False, open_new_project: bool = True
):
    """Exports a selected range of video frames and their corresponding labels.

    Args:
        as_package: Whether to export as a package.
        open_new_project: Whether to open the exported labels in a new project GUI.
    """
    self.execute(
        ExportLabelsSubset, as_package=as_package, open_new_project=open_new_project
    )

exportNWB()

Show gui for exporting nwb file.

Source code in sleap/gui/commands.py
383
384
385
def exportNWB(self):
    """Show gui for exporting nwb file."""
    self.execute(SaveProjectAs, adaptor="nwb")

exportTrainingPackage()

Gui for exporting the dataset with user-labeled images and suggestions.

Source code in sleap/gui/commands.py
395
396
397
def exportTrainingPackage(self):
    """Gui for exporting the dataset with user-labeled images and suggestions."""
    self.execute(ExportTrainingPackage)

exportUserLabelsPackage()

Gui for exporting the dataset with user-labeled images.

Source code in sleap/gui/commands.py
391
392
393
def exportUserLabelsPackage(self):
    """Gui for exporting the dataset with user-labeled images."""
    self.execute(ExportUserLabelsPackage)

from_labels(labels) classmethod

Creates a command context for use independently of GUI app.

Source code in sleap/gui/commands.py
256
257
258
259
260
261
262
@classmethod
def from_labels(cls, labels: Labels) -> "CommandContext":
    """Creates a command context for use independently of GUI app."""
    state = GuiState()
    state["labels"] = labels
    app = FakeApp(labels)
    return cls(state=state, app=app)

generateSuggestions(params)

Generates suggestions using given params dictionary.

Source code in sleap/gui/commands.py
660
661
662
def generateSuggestions(self, params: Dict):
    """Generates suggestions using given params dictionary."""
    self.execute(GenerateSuggestions, **params)

gotoFrame()

Shows gui to go to frame by number.

Source code in sleap/gui/commands.py
445
446
447
def gotoFrame(self):
    """Shows gui to go to frame by number."""
    self.execute(GoFrameGui)

gotoVideoAndFrame(video, frame_idx)

Activates video and goes to frame.

Source code in sleap/gui/commands.py
453
454
455
def gotoVideoAndFrame(self, video: Video, frame_idx: int):
    """Activates video and goes to frame."""
    NavCommand.go_to(self, frame_idx, video)

importAnalysisFile()

Imports SLEAP analysis hdf5 files.

Source code in sleap/gui/commands.py
363
364
365
def importAnalysisFile(self):
    """Imports SLEAP analysis hdf5 files."""
    self.execute(ImportAnalysisFile)

importCoco()

Imports COCO datasets.

Source code in sleap/gui/commands.py
351
352
353
def importCoco(self):
    """Imports COCO datasets."""
    self.execute(ImportCoco)

importDLC()

Imports DeepLabCut datasets.

Source code in sleap/gui/commands.py
355
356
357
def importDLC(self):
    """Imports DeepLabCut datasets."""
    self.execute(ImportDeepLabCut)

importDLCFolder()

Imports multiple DeepLabCut datasets.

Source code in sleap/gui/commands.py
359
360
361
def importDLCFolder(self):
    """Imports multiple DeepLabCut datasets."""
    self.execute(ImportDeepLabCutFolder)

importNWB()

Imports NWB datasets.

Source code in sleap/gui/commands.py
347
348
349
def importNWB(self):
    """Imports NWB datasets."""
    self.execute(ImportNWB)

lastInteractedFrame()

Goes to last frame that user interacted with.

Source code in sleap/gui/commands.py
417
418
419
def lastInteractedFrame(self):
    """Goes to last frame that user interacted with."""
    self.execute(GoLastInteractedFrame)

loadLabelsObject(labels, filename=None)

Loads a Labels object into the GUI, replacing any currently loaded.

Parameters:

Name Type Description Default
labels Labels

The Labels object to load.

required
filename Optional[str]

The filename where this file is saved, if any.

None

Returns:

Type Description

None.

Source code in sleap/gui/commands.py
307
308
309
310
311
312
313
314
315
316
317
318
def loadLabelsObject(self, labels: Labels, filename: Optional[str] = None):
    """Loads a `Labels` object into the GUI, replacing any currently loaded.

    Args:
        labels: The `Labels` object to load.
        filename: The filename where this file is saved, if any.

    Returns:
        None.

    """
    self.execute(LoadLabelsObject, labels=labels, filename=filename)

loadProjectFile(filename)

Loads given labels file into GUI.

Parameters:

Name Type Description Default
filename Union[str, Labels]

The path to the saved labels dataset or the Labels object. If None, then don't do anything.

required

Returns:

Type Description

None

Source code in sleap/gui/commands.py
320
321
322
323
324
325
326
327
328
329
330
def loadProjectFile(self, filename: Union[str, Labels]):
    """Loads given labels file into GUI.

    Args:
        filename: The path to the saved labels dataset or the `Labels` object.
            If None, then don't do anything.

    Returns:
        None
    """
    self.execute(LoadProjectFile, filename=filename)

mergeProject(filenames=None)

Starts gui for importing another dataset into currently one.

Source code in sleap/gui/commands.py
656
657
658
def mergeProject(self, filenames: Optional[List[str]] = None):
    """Starts gui for importing another dataset into currently one."""
    self.execute(MergeProject, filenames=filenames)

newEdge(src_node, dst_node)

Adds new edge to skeleton.

Source code in sleap/gui/commands.py
511
512
513
def newEdge(self, src_node, dst_node):
    """Adds new edge to skeleton."""
    self.execute(NewEdge, src_node=src_node, dst_node=dst_node)

newInstance(copy_instance=None, init_method='best', location=None, mark_complete=False, offset=0)

Creates a new instance, copying node coordinates as appropriate.

Parameters:

Name Type Description Default
copy_instance Optional[Instance]

The :class:Instance (or :class:PredictedInstance) which we want to copy.

None
init_method str

Method to use for positioning nodes.

'best'
location Optional[QPoint]

The location where instance should be added (if node init method supports custom location).

None
mark_complete bool

Whether to mark the instance as complete.

False
offset int

Offset to apply to the location if given.

0
Source code in sleap/gui/commands.py
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
def newInstance(
    self,
    copy_instance: Optional[Instance] = None,
    init_method: str = "best",
    location: Optional[QtCore.QPoint] = None,
    mark_complete: bool = False,
    offset: int = 0,
):
    """Creates a new instance, copying node coordinates as appropriate.

    Args:
        copy_instance: The :class:`Instance` (or
            :class:`PredictedInstance`) which we want to copy.
        init_method: Method to use for positioning nodes.
        location: The location where instance should be added (if node init
            method supports custom location).
        mark_complete: Whether to mark the instance as complete.
        offset: Offset to apply to the location if given.
    """
    self.execute(
        AddInstance,
        copy_instance=copy_instance,
        init_method=init_method,
        location=location,
        mark_complete=mark_complete,
        offset=offset,
    )

newNode()

Adds new node to skeleton.

Source code in sleap/gui/commands.py
491
492
493
def newNode(self):
    """Adds new node to skeleton."""
    self.execute(NewNode)

newProject()

Create a new project in a new window.

Source code in sleap/gui/commands.py
303
304
305
def newProject(self):
    """Create a new project in a new window."""
    self.execute(NewProject)

nextLabeledFrame()

Goes to labeled frame after current frame.

Source code in sleap/gui/commands.py
409
410
411
def nextLabeledFrame(self):
    """Goes to labeled frame after current frame."""
    self.execute(GoNextLabeledFrame)

nextSuggestedFrame()

Goes to next suggested frame.

Source code in sleap/gui/commands.py
421
422
423
def nextSuggestedFrame(self):
    """Goes to next suggested frame."""
    self.execute(GoNextSuggestedFrame)

nextTrackFrame()

Goes to next frame on which a track starts.

Source code in sleap/gui/commands.py
441
442
443
def nextTrackFrame(self):
    """Goes to next frame on which a track starts."""
    self.execute(GoNextTrackFrame)

nextUserLabeledFrame()

Goes to next labeled frame with user instances.

Source code in sleap/gui/commands.py
413
414
415
def nextUserLabeledFrame(self):
    """Goes to next labeled frame with user instances."""
    self.execute(GoNextUserLabeledFrame)

openPrereleaseVersion()

Open the current prerelease version.

Source code in sleap/gui/commands.py
676
677
678
def openPrereleaseVersion(self):
    """Open the current prerelease version."""
    self.execute(OpenPrereleaseVersion)

openProject(filename=None, first_open=False)

Allows user to select and then open a saved project.

Parameters:

Name Type Description Default
filename Optional[str]

Filename of the project to be opened. If None, a file browser dialog will prompt the user for a path.

None
first_open bool

Whether this is the first window opened. If True, then the new project is loaded into the current window rather than a new application window.

False

Returns:

Type Description

None.

Source code in sleap/gui/commands.py
332
333
334
335
336
337
338
339
340
341
342
343
344
345
def openProject(self, filename: Optional[str] = None, first_open: bool = False):
    """Allows user to select and then open a saved project.

    Args:
        filename: Filename of the project to be opened. If None, a file browser
            dialog will prompt the user for a path.
        first_open: Whether this is the first window opened. If True,
            then the new project is loaded into the current window
            rather than a new application window.

    Returns:
        None.
    """
    self.execute(OpenProject, filename=filename, first_open=first_open)

openSkeleton()

Shows gui for loading saved skeleton into project.

Source code in sleap/gui/commands.py
483
484
485
def openSkeleton(self):
    """Shows gui for loading saved skeleton into project."""
    self.execute(OpenSkeleton)

openSkeletonTemplate()

Shows gui for loading saved skeleton into project.

Source code in sleap/gui/commands.py
479
480
481
def openSkeletonTemplate(self):
    """Shows gui for loading saved skeleton into project."""
    self.execute(OpenSkeleton, template=True)

openStableVersion()

Open the current stable version.

Source code in sleap/gui/commands.py
672
673
674
def openStableVersion(self):
    """Open the current stable version."""
    self.execute(OpenStableVersion)

openWebsite(url)

Open a website from URL using the native system browser.

Source code in sleap/gui/commands.py
664
665
666
def openWebsite(self, url):
    """Open a website from URL using the native system browser."""
    self.execute(OpenWebsite, url=url)

pasteInstance()

Paste the instance from the clipboard as a new copy.

Source code in sleap/gui/commands.py
603
604
605
def pasteInstance(self):
    """Paste the instance from the clipboard as a new copy."""
    self.execute(PasteInstance)

pasteInstanceTrack()

Pastes the track in the clipboard to the selected instance.

Source code in sleap/gui/commands.py
639
640
641
def pasteInstanceTrack(self):
    """Pastes the track in the clipboard to the selected instance."""
    self.execute(PasteInstanceTrack)

prevSuggestedFrame()

Goes to previous suggested frame.

Source code in sleap/gui/commands.py
425
426
427
def prevSuggestedFrame(self):
    """Goes to previous suggested frame."""
    self.execute(GoPrevSuggestedFrame)

previousLabeledFrame()

Goes to labeled frame prior to current frame.

Source code in sleap/gui/commands.py
405
406
407
def previousLabeledFrame(self):
    """Goes to labeled frame prior to current frame."""
    self.execute(GoPreviousLabeledFrame)

removeSuggestion()

Remove the selected frame from suggestions.

Source code in sleap/gui/commands.py
433
434
435
def removeSuggestion(self):
    """Remove the selected frame from suggestions."""
    self.execute(RemoveSuggestion)

removeVideo()

Removes selected video from project.

Source code in sleap/gui/commands.py
475
476
477
def removeVideo(self):
    """Removes selected video from project."""
    self.execute(RemoveVideo)

replaceVideo()

Shows gui for replacing videos to project.

Source code in sleap/gui/commands.py
471
472
473
def replaceVideo(self):
    """Shows gui for replacing videos to project."""
    self.execute(ReplaceVideo)

saveProject()

Show gui to save project (or save as if not yet saved).

Source code in sleap/gui/commands.py
367
368
369
def saveProject(self):
    """Show gui to save project (or save as if not yet saved)."""
    self.execute(SaveProject)

saveProjectAs()

Show gui to save project as a new file.

Source code in sleap/gui/commands.py
371
372
373
def saveProjectAs(self):
    """Show gui to save project as a new file."""
    self.execute(SaveProjectAs)

saveSkeleton()

Shows gui for saving skeleton from project.

Source code in sleap/gui/commands.py
487
488
489
def saveSkeleton(self):
    """Shows gui for saving skeleton from project."""
    self.execute(SaveSkeleton)

selectToFrame()

Shows gui to go to frame by number.

Source code in sleap/gui/commands.py
449
450
451
def selectToFrame(self):
    """Shows gui to go to frame by number."""
    self.execute(SelectToFrameGui)

setInstancePointVisibility(instance, node, visible)

Toggles visibility set for a node for an instance.

Source code in sleap/gui/commands.py
589
590
591
592
593
def setInstancePointVisibility(self, instance: Instance, node: Node, visible: bool):
    """Toggles visibility set for a node for an instance."""
    self.execute(
        SetInstancePointVisibility, instance=instance, node=node, visible=visible
    )

setInstanceTrack(new_track)

Sets track for selected instance.

Source code in sleap/gui/commands.py
623
624
625
def setInstanceTrack(self, new_track: "Track"):
    """Sets track for selected instance."""
    self.execute(SetSelectedInstanceTrack, new_track=new_track)

setNodeName(skeleton, node, name)

Changes name of node in skeleton.

Source code in sleap/gui/commands.py
499
500
501
def setNodeName(self, skeleton, node, name):
    """Changes name of node in skeleton."""
    self.execute(SetNodeName, skeleton=skeleton, node=node, name=name)

setNodeSymmetry(skeleton, node, symmetry)

Sets node symmetry in skeleton.

Source code in sleap/gui/commands.py
503
504
505
def setNodeSymmetry(self, skeleton, node, symmetry: str):
    """Sets node symmetry in skeleton."""
    self.execute(SetNodeSymmetry, skeleton=skeleton, node=node, symmetry=symmetry)

setPointLocations(instance, nodes_locations)

Sets locations for node(s) for an instance.

Source code in sleap/gui/commands.py
579
580
581
582
583
584
585
586
587
def setPointLocations(
    self, instance: Instance, nodes_locations: Dict[Node, Tuple[int, int]]
):
    """Sets locations for node(s) for an instance."""
    self.execute(
        SetInstancePointLocations,
        instance=instance,
        nodes_locations=nodes_locations,
    )

setTrackName(track, name)

Sets name for track.

Source code in sleap/gui/commands.py
643
644
645
def setTrackName(self, track: "Track", name: str):
    """Sets name for track."""
    self.execute(SetTrackName, track=track, name=name)

showImportVideos(filenames)

Show video importer GUI without the file browser.

Source code in sleap/gui/commands.py
467
468
469
def showImportVideos(self, filenames: List[str]):
    """Show video importer GUI without the file browser."""
    self.execute(ShowImportVideos, filenames=filenames)

signal_update(what)

Calls the update callback after data has been changed.

Source code in sleap/gui/commands.py
269
270
271
272
def signal_update(self, what: List[UpdateTopic]):
    """Calls the update callback after data has been changed."""
    if callable(self.update_callback):
        self.update_callback(what)

toggleGrayscale()

Toggles grayscale setting for current video.

Source code in sleap/gui/commands.py
459
460
461
def toggleGrayscale(self):
    """Toggles grayscale setting for current video."""
    self.execute(ToggleGrayscale)

transposeInstance()

Transposes tracks for two instances.

If there are only two instances, then this swaps tracks. Otherwise, it allows user to select the instances for which we want to swap tracks.

Source code in sleap/gui/commands.py
647
648
649
650
651
652
653
654
def transposeInstance(self):
    """Transposes tracks for two instances.

    If there are only two instances, then this swaps tracks.
    Otherwise, it allows user to select the instances for which we want
    to swap tracks.
    """
    self.execute(TransposeInstances)

updateEdges()

Called when edges in skeleton have been changed.

Source code in sleap/gui/commands.py
507
508
509
def updateEdges(self):
    """Called when edges in skeleton have been changed."""
    self.signal_update([UpdateTopic.skeleton])

DeleteFrameLimitPredictions

Bases: InstanceDeleteCommand

Methods:

Name Description
get_frame_instance_list

Called from the parent InstanceDeleteCommand.ask method.

Source code in sleap/gui/commands.py
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
class DeleteFrameLimitPredictions(InstanceDeleteCommand):
    @staticmethod
    def get_frame_instance_list(context: CommandContext, params: Dict):
        """Called from the parent `InstanceDeleteCommand.ask` method.

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

    @classmethod
    def ask(cls, context: CommandContext, params: Dict) -> bool:
        current_video = context.state["video"]
        dialog = FrameRangeDialog(
            title="Delete Instances in Frame Range...", max_frame_idx=len(current_video)
        )
        results = dialog.get_results()
        if results:
            params["min_frame_idx"] = results["min_frame_idx"]
            params["max_frame_idx"] = results["max_frame_idx"]
            return super().ask(context, params)

get_frame_instance_list(context, params) staticmethod

Called from the parent InstanceDeleteCommand.ask method.

Returns:

Type Description

List of instances to be deleted.

Source code in sleap/gui/commands.py
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
@staticmethod
def get_frame_instance_list(context: CommandContext, params: Dict):
    """Called from the parent `InstanceDeleteCommand.ask` method.

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

DeleteMultipleTracks

Bases: EditCommand

Methods:

Name Description
do_action

Delete either all tracks or just unused tracks.

Source code in sleap/gui/commands.py
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
class DeleteMultipleTracks(EditCommand):
    topics = [UpdateTopic.tracks]

    @staticmethod
    def do_action(context: CommandContext, params: dict):
        """Delete either all tracks or just unused tracks.

        Args:
            context: The command context.
            params: The command parameters.
                delete_all: If True, delete all tracks. If False, delete only
                    unused tracks.
        """
        delete_all: bool = params["delete_all"]
        if delete_all:
            remove_all_tracks(context.labels)
        else:
            remove_unused_tracks(context.labels)

do_action(context, params) staticmethod

Delete either all tracks or just unused tracks.

Parameters:

Name Type Description Default
context CommandContext

The command context.

required
params dict

The command parameters. delete_all: If True, delete all tracks. If False, delete only unused tracks.

required
Source code in sleap/gui/commands.py
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
@staticmethod
def do_action(context: CommandContext, params: dict):
    """Delete either all tracks or just unused tracks.

    Args:
        context: The command context.
        params: The command parameters.
            delete_all: If True, delete all tracks. If False, delete only
                unused tracks.
    """
    delete_all: bool = params["delete_all"]
    if delete_all:
        remove_all_tracks(context.labels)
    else:
        remove_unused_tracks(context.labels)

EditCommand

Bases: AppCommand

Class for commands which change data in project.

Source code in sleap/gui/commands.py
2035
2036
2037
2038
class EditCommand(AppCommand):
    """Class for commands which change data in project."""

    does_edits = True

ExportLabeledClip

Bases: ExportVideoClip

Export a labeled video clip with labels and edges.

This command is used to export a labeled video clip with labels and edges. It inherits from the ExportVideoClip class and provides additional functionality for exporting labeled videos.

Methods:

Name Description
ask

Ask the user for parameters to export a labeled video clip.

write_new_video

Write a new annotated video using the parameters provided.

Source code in sleap/gui/commands.py
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
class ExportLabeledClip(ExportVideoClip):
    """Export a labeled video clip with labels and edges.

    This command is used to export a labeled video clip with labels and edges. It
    inherits from the `ExportVideoClip` class and provides additional functionality for
    exporting labeled videos.
    """

    @classmethod
    def ask(cls, context: CommandContext, params: dict) -> bool:
        """Ask the user for parameters to export a labeled video clip."""
        params["form_name"] = "labeled_clip_form"
        ok = super().ask(context, params)
        return ok

    @classmethod
    def write_new_video(cls, context, params):
        """Write a new annotated video using the parameters provided.

        Args:
            context: The command context.
            params: The parameters for the export.
        """
        save_labeled_video(
            filename=params["video_filename"],
            labels=context.state["labels"],
            video=context.state["video"],
            frames=list(params["frames"]),
            fps=params["fps"],
            color_manager=params["color_manager"],
            background=params["background"],
            show_edges=params["show edges"],
            edge_is_wedge=params["edge_is_wedge"],
            marker_size=params["marker size"],
            scale=params["scale"],
            crop_size_xy=params["crop"],
            gui_progress=params["gui_progress"],
        )

ask(context, params) classmethod

Ask the user for parameters to export a labeled video clip.

Source code in sleap/gui/commands.py
1496
1497
1498
1499
1500
1501
@classmethod
def ask(cls, context: CommandContext, params: dict) -> bool:
    """Ask the user for parameters to export a labeled video clip."""
    params["form_name"] = "labeled_clip_form"
    ok = super().ask(context, params)
    return ok

write_new_video(context, params) classmethod

Write a new annotated video using the parameters provided.

Parameters:

Name Type Description Default
context

The command context.

required
params

The parameters for the export.

required
Source code in sleap/gui/commands.py
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
@classmethod
def write_new_video(cls, context, params):
    """Write a new annotated video using the parameters provided.

    Args:
        context: The command context.
        params: The parameters for the export.
    """
    save_labeled_video(
        filename=params["video_filename"],
        labels=context.state["labels"],
        video=context.state["video"],
        frames=list(params["frames"]),
        fps=params["fps"],
        color_manager=params["color_manager"],
        background=params["background"],
        show_edges=params["show edges"],
        edge_is_wedge=params["edge_is_wedge"],
        marker_size=params["marker size"],
        scale=params["scale"],
        crop_size_xy=params["crop"],
        gui_progress=params["gui_progress"],
    )

ExportLabelsSubset

Bases: ExportFullPackage

Export a subset of labels to a new file with either images or a trimmed video.

This command subclasses ExportFullPackage, but uses also uses methods from ExportVideoClip to provide functionality for exporting a subset of labels to a new file with either images or a trimmed video. It allows the user to specify the labels to export and the format of the output file.

Methods:

Name Description
get_labels_subset_unshifted

Get the labels subset for the export.

get_lfs_subset

Get the labeled frames subset for the export.

get_or_create_video_subset

Get the video subset for the export.

get_suggestions_subset

Get the suggestions subset for the labels.

Source code in sleap/gui/commands.py
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
class ExportLabelsSubset(ExportFullPackage):
    """Export a subset of labels to a new file with either images or a trimmed video.

    This command subclasses `ExportFullPackage`, but uses also uses methods from
    `ExportVideoClip` to provide functionality for exporting a subset of labels to a new
    file with either images or a trimmed video. It allows the user to specify the labels
    to export and the format of the output file.
    """

    @classmethod
    def ask(cls, context: CommandContext, params: dict) -> bool:
        # Ask for the labels subset to export.
        if not super().ask(context=context, params=params):
            return False

        # If we are exporting as a pkg.slp, then we just need the frame range.
        # Not interested in opening the video.
        if params.get("as_package", False):
            ExportVideoClip.get_frame_range_params(context=context, params=params)
        # Otherwise, exporting as slp and need to get video clip parameters.
        elif not ExportVideoClip.ask(context=context, params=params):
            return False

        return True

    @classmethod
    def do_action(cls, context: CommandContext, params: dict):
        # Get the video subset for the export.
        video_subset = cls.get_or_create_video_subset(context=context, params=params)

        # Get the (unshifted) labels subset for the export.
        labels_subset_unshifted: Labels = cls.get_labels_subset_unshifted(
            context=context, params=params
        )

        # Get the shifted and updated labels frames subset for the export.
        lfs_subset = cls.get_lfs_subset(
            labels_subset_unshifted=labels_subset_unshifted,
            video_subset=video_subset,
            params=params,
        )

        # Also need to update anything that references the video or frame index.
        suggestions_subset = cls.get_suggestions_subset(
            labels_subset_unshifted=labels_subset_unshifted,
            video_subset=video_subset,
            params=params,
        )

        # Create the labels subset for the export.
        labels_subset = Labels(
            labeled_frames=lfs_subset,
            videos=[video_subset],
            skeletons=labels_subset_unshifted.skeletons,
            tracks=labels_subset_unshifted.tracks,
            suggestions=suggestions_subset,
            provenance=labels_subset_unshifted.provenance,
        )

        # Save the labels subset to a new file.
        params["labels"] = labels_subset
        super().do_action(context=context, params=params)

        # Now let's open the new file.
        if params.get("open_new_project", False):
            OpenProject.do_action(context=context, params=params)

    @classmethod
    def get_or_create_video_subset(cls, context: CommandContext, params: dict) -> Video:
        """Get the video subset for the export.

        Args:
            context: The command context.
            params: The parameters for the export.

        Returns:
            Video: The video subset for the export.
        """
        # Get variables from params and context.
        as_package = params.get("as_package", False)
        frames = params["frames"]
        end_frame_idx = frames[-1]  # 0-indexed
        video: Video = context.state["video"]
        n_frames = len(video)
        # Initialize video subset.
        video_subset = video

        # If the user selected the entire video, then do not create a new video.
        if (end_frame_idx < n_frames - 1) and not as_package:
            # Do not open the video when done.
            open_when_done = params.get("open_when_done", False)
            params["open_when_done"] = False

            # Export the video clip using the parameters provided.
            ExportVideoClip.do_action(context=context, params=params)
            video_subset_filename = params["video_filename"]
            video_subset = Video.from_filename(filename=video_subset_filename)

            # Reset the open_when_done parameter. Not currently used, but maybe we
            # should use this for opening the new project.
            params["open_when_done"] = open_when_done

        return video_subset

    @classmethod
    def get_labels_subset_unshifted(
        cls, context: CommandContext, params: dict
    ) -> Labels:
        """Get the labels subset for the export.

        Args:
            context: The command context.
            params: The parameters for the export.

        Returns:
            Labels: The labels subset for the export.
        """
        # Get variables from params.
        video: Video = context.state["video"]
        frames: range = params["frames"]

        # Get subset of labels to export
        labels: Labels = context.state["labels"]
        frames_in_labels = [(video, frame) for frame in frames]
        labels_subset_unshifted: Labels = labels.extract(
            inds=frames_in_labels, copy=True
        )
        return labels_subset_unshifted

    @classmethod
    def get_lfs_subset(
        cls, labels_subset_unshifted: Labels, video_subset: Video, params: dict
    ) -> list[LabeledFrame]:
        """Get the labeled frames subset for the export.

        Args:
            labels_subset_unshifted: The labels subset to export.
            video_subset: The video subset to export.
            params: The parameters for the export.

        Returns:
            list[LabeledFrame]: The labeled frames subset for the export.
        """
        # Get variables from params.
        as_package = params.get("as_package", False)
        frames: range = params["frames"]
        start_frame_idx = frames[0]  # 0-indexed

        # Update the video and frame indices of the labels.
        lfs_subset = labels_subset_unshifted.labeled_frames if as_package else []
        for lf in labels_subset_unshifted.labeled_frames:
            # Use the new video for the subset (need to do this even if video is same,
            # otherwise, fails in Labels __init__ when updating cache).
            lf.video = video_subset

            if not as_package:
                # Shift the frame index to match the new video
                lf.frame_idx -= start_frame_idx

                # Add the labeled frame to the subset
                lfs_subset.append(lf)

        return lfs_subset

    @classmethod
    def get_suggestions_subset(
        cls,
        labels_subset_unshifted: Labels,
        video_subset: Video,
        params: dict,
    ) -> list[SuggestionFrame]:
        """Get the suggestions subset for the labels.

        Args:
            labels_subset_unshifted: The labels subset to export.
            video_subset: The video subset to export.

        Returns:
            list[SuggestionFrame]: The suggestions subset for the labels.
        """
        # Get variables from params.
        as_package = params.get("as_package", False)
        frames = params["frames"]
        start_frame_idx = frames[0]  # 0-indexed
        end_frame_idx = frames[-1]

        # Get the suggestions subset for the labels.
        suggestions_subset = []
        for suggestion in labels_subset_unshifted.suggestions:
            suggestion.video = video_subset

            if (
                suggestion.frame_idx >= start_frame_idx
                and suggestion.frame_idx < end_frame_idx
            ):
                # Shift the frame index to match the new video if not a package.
                if not as_package:
                    suggestion.frame_idx -= start_frame_idx

                suggestions_subset.append(suggestion)

        return suggestions_subset

get_labels_subset_unshifted(context, params) classmethod

Get the labels subset for the export.

Parameters:

Name Type Description Default
context CommandContext

The command context.

required
params dict

The parameters for the export.

required

Returns:

Name Type Description
Labels Labels

The labels subset for the export.

Source code in sleap/gui/commands.py
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
@classmethod
def get_labels_subset_unshifted(
    cls, context: CommandContext, params: dict
) -> Labels:
    """Get the labels subset for the export.

    Args:
        context: The command context.
        params: The parameters for the export.

    Returns:
        Labels: The labels subset for the export.
    """
    # Get variables from params.
    video: Video = context.state["video"]
    frames: range = params["frames"]

    # Get subset of labels to export
    labels: Labels = context.state["labels"]
    frames_in_labels = [(video, frame) for frame in frames]
    labels_subset_unshifted: Labels = labels.extract(
        inds=frames_in_labels, copy=True
    )
    return labels_subset_unshifted

get_lfs_subset(labels_subset_unshifted, video_subset, params) classmethod

Get the labeled frames subset for the export.

Parameters:

Name Type Description Default
labels_subset_unshifted Labels

The labels subset to export.

required
video_subset Video

The video subset to export.

required
params dict

The parameters for the export.

required

Returns:

Type Description
list[LabeledFrame]

list[LabeledFrame]: The labeled frames subset for the export.

Source code in sleap/gui/commands.py
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
@classmethod
def get_lfs_subset(
    cls, labels_subset_unshifted: Labels, video_subset: Video, params: dict
) -> list[LabeledFrame]:
    """Get the labeled frames subset for the export.

    Args:
        labels_subset_unshifted: The labels subset to export.
        video_subset: The video subset to export.
        params: The parameters for the export.

    Returns:
        list[LabeledFrame]: The labeled frames subset for the export.
    """
    # Get variables from params.
    as_package = params.get("as_package", False)
    frames: range = params["frames"]
    start_frame_idx = frames[0]  # 0-indexed

    # Update the video and frame indices of the labels.
    lfs_subset = labels_subset_unshifted.labeled_frames if as_package else []
    for lf in labels_subset_unshifted.labeled_frames:
        # Use the new video for the subset (need to do this even if video is same,
        # otherwise, fails in Labels __init__ when updating cache).
        lf.video = video_subset

        if not as_package:
            # Shift the frame index to match the new video
            lf.frame_idx -= start_frame_idx

            # Add the labeled frame to the subset
            lfs_subset.append(lf)

    return lfs_subset

get_or_create_video_subset(context, params) classmethod

Get the video subset for the export.

Parameters:

Name Type Description Default
context CommandContext

The command context.

required
params dict

The parameters for the export.

required

Returns:

Name Type Description
Video Video

The video subset for the export.

Source code in sleap/gui/commands.py
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
@classmethod
def get_or_create_video_subset(cls, context: CommandContext, params: dict) -> Video:
    """Get the video subset for the export.

    Args:
        context: The command context.
        params: The parameters for the export.

    Returns:
        Video: The video subset for the export.
    """
    # Get variables from params and context.
    as_package = params.get("as_package", False)
    frames = params["frames"]
    end_frame_idx = frames[-1]  # 0-indexed
    video: Video = context.state["video"]
    n_frames = len(video)
    # Initialize video subset.
    video_subset = video

    # If the user selected the entire video, then do not create a new video.
    if (end_frame_idx < n_frames - 1) and not as_package:
        # Do not open the video when done.
        open_when_done = params.get("open_when_done", False)
        params["open_when_done"] = False

        # Export the video clip using the parameters provided.
        ExportVideoClip.do_action(context=context, params=params)
        video_subset_filename = params["video_filename"]
        video_subset = Video.from_filename(filename=video_subset_filename)

        # Reset the open_when_done parameter. Not currently used, but maybe we
        # should use this for opening the new project.
        params["open_when_done"] = open_when_done

    return video_subset

get_suggestions_subset(labels_subset_unshifted, video_subset, params) classmethod

Get the suggestions subset for the labels.

Parameters:

Name Type Description Default
labels_subset_unshifted Labels

The labels subset to export.

required
video_subset Video

The video subset to export.

required

Returns:

Type Description
list[SuggestionFrame]

list[SuggestionFrame]: The suggestions subset for the labels.

Source code in sleap/gui/commands.py
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
@classmethod
def get_suggestions_subset(
    cls,
    labels_subset_unshifted: Labels,
    video_subset: Video,
    params: dict,
) -> list[SuggestionFrame]:
    """Get the suggestions subset for the labels.

    Args:
        labels_subset_unshifted: The labels subset to export.
        video_subset: The video subset to export.

    Returns:
        list[SuggestionFrame]: The suggestions subset for the labels.
    """
    # Get variables from params.
    as_package = params.get("as_package", False)
    frames = params["frames"]
    start_frame_idx = frames[0]  # 0-indexed
    end_frame_idx = frames[-1]

    # Get the suggestions subset for the labels.
    suggestions_subset = []
    for suggestion in labels_subset_unshifted.suggestions:
        suggestion.video = video_subset

        if (
            suggestion.frame_idx >= start_frame_idx
            and suggestion.frame_idx < end_frame_idx
        ):
            # Shift the frame index to match the new video if not a package.
            if not as_package:
                suggestion.frame_idx -= start_frame_idx

            suggestions_subset.append(suggestion)

    return suggestions_subset

ExportVideoClip

Bases: AppCommand

Base class for exporting video clips.

The ask method provides all functionality to gather parameters from the export dialog.

Methods:

Name Description
ask

Ask the user for parameters to export a video clip.

do_action

Export video clip using the parameters provided.

get_export_options

Get export options from the user.

get_frame_range_params

Get frame range parameters.

get_video_augmentation_params

Get video augmentation parameters.

get_video_markup_params

Get video markup parameters.

get_video_save_params

Get video save parameters.

write_new_video

Write a new video using the parameters provided.

Source code in sleap/gui/commands.py
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
class ExportVideoClip(AppCommand):
    """Base class for exporting video clips.

    The ask method provides all functionality to gather parameters from the export
    dialog.
    """

    @classmethod
    def ask(cls, context: CommandContext, params: dict) -> bool:
        """Ask the user for parameters to export a video clip.

        Args:
            context: The command context.
            params: The parameters for the export.

        Returns:
            bool: True if the user provided valid parameters, False otherwise.
        """
        # Open export dialog.
        export_options = cls.get_export_options(context, params)
        if export_options is None:  # User hit cancel.
            return False

        # If we had a pop-up dialog, then we can also show GUI progress.
        params["gui_progress"] = True

        # Get video save parameters.
        params = cls.get_video_save_params(params, export_options)

        # Get frame range parameters.
        params = cls.get_frame_range_params(context, params)

        # Get video augmentation parameters.
        params = cls.get_video_augmentation_params(context, params, export_options)

        # Get video markup parameters.
        params = cls.get_video_markup_params(context, params, export_options)

        return True

    @classmethod
    def do_action(cls, context: CommandContext, params: dict):
        """Export video clip using the parameters provided.

        Args:
            context: The command context.
            params: The parameters for the export.
        """
        # Write the new video using the parameters provided.
        cls.write_new_video(context, params)

        # Open the file using default video playing app
        if params["open_when_done"]:
            open_file(params["video_filename"])

    @classmethod
    def write_new_video(
        cls,
        context: CommandContext,
        params: dict,
    ) -> None:
        """Write a new video using the parameters provided.

        Args:
            context: The command context.
            params: The parameters for the export.
        """
        # write_video(
        #     filename=params["video_filename"],
        #     video=context.state["video"],
        #     frames=list(params["frames"]),
        #     fps=params["fps"],
        #     scale=params["scale"],
        #     background=params["background"],
        #     gui_progress=params["gui_progress"],
        # )
        save_video(
            frames=[
                context.state["video"][frame_idx] for frame_idx in params["frames"]
            ],
            filename=params["video_filename"],
            fps=params["fps"],
        )

    @classmethod
    def get_export_options(cls, context: CommandContext, params: dict) -> dict | None:
        """Get export options from the user.

        Args:
            context: The command context.
            params: The parameters for the export.

        Returns:
            dict: The export options.
        """
        from sleap.gui.dialogs.export_clip import ExportClipDialog

        form_name = params.get("form_name", "video_clip_form")
        dialog = ExportClipDialog(form_name=form_name)

        # Set default fps from video (if video has fps attribute)
        dialog.form_widget.set_form_data(
            dict(fps=getattr(context.state["video"], "fps", 30))
        )

        # Show modal dialog and get form results
        export_options = dialog.get_results()

        # Check if user hit cancel
        if export_options is None:
            return False

        default_out_basename = params.get("filename", context.state["filename"])

        # For OpenCV we default to avi since the bundled ffmpeg
        # makes mp4's that most programs can't open (VLC can).
        default_out_filename = default_out_basename + ".avi"

        if can_use_ffmpeg():
            default_out_filename = default_out_basename + ".mp4"

        # Ask where user wants to save video file
        filename, _ = FileDialog.save(
            context.app,
            caption="Save Video As...",
            dir=default_out_filename,
            filter="Video (*.avi *.mp4)",
        )

        # Check if user hit cancel
        if len(filename) == 0:
            return None

        export_options["video_filename"] = filename
        return export_options

    @classmethod
    def get_video_save_params(cls, params: dict, export_options: dict) -> dict:
        """Get video save parameters.

        Args:
            params: The parameters for the export.
            export_options: The export options.

        Side Effects:
            Sets "video_filename", "fps", and "open_when_done" in params.

        Returns:
            dict: Containing the video save parameters (in addition to other params).
        """
        params["video_filename"] = export_options["video_filename"]
        params["fps"] = export_options["fps"]
        params["open_when_done"] = export_options["open_when_done"]
        return params

    @classmethod
    def get_frame_range_params(cls, context: CommandContext, params: dict) -> dict:
        """Get frame range parameters.

        Args:
            context: The command context.
            params: The parameters for the export.

        Side Effects:
            Sets "frames" in params.

        Returns:
            dict: Containing the frame range parameters (in addition to other params).
        """
        # If user selected a clip, use that; otherwise include all frames.
        if context.state["has_frame_range"]:
            params["frames"] = range(*context.state["frame_range"])
        else:
            params["frames"] = range(len(context.state["video"]))

        return params

    @classmethod
    def get_video_augmentation_params(
        cls, context: CommandContext, params: dict, export_options: dict
    ) -> dict:
        """Get video augmentation parameters.

        Args:
            context: The command context.
            params: The parameters for the export.
            export_options: The export options.

        Side Effects:
            Sets "scale", "background", and "crop" in params.

        Returns:
            dict: Containing the video augmentation parameters (in addition to other
                params).
        """
        params["scale"] = export_options.get("scale", 1.0)
        params["background"] = export_options.get("background", None)
        params["crop"] = None

        export_options_crop = export_options.get("crop", None)
        if export_options_crop is None:
            return params

        # Determine crop size relative to original size and scale
        # (crop size should be *final* output size, thus already scaled).
        video = context.state["video"]
        img_h, img_w = video.shape[1:3]
        w = int(img_w * params["scale"])
        h = int(img_h * params["scale"])
        if export_options_crop == "Half":
            params["crop"] = (w // 2, h // 2)
        elif export_options_crop == "Quarter":
            params["crop"] = (w // 4, h // 4)

        return params

    @classmethod
    def get_video_markup_params(
        cls, context: CommandContext, params: dict, export_options: dict
    ) -> dict:
        """Get video markup parameters.

        Args:
            context: The command context.
            params: The parameters for the export.
            export_options: The export options.

        Side Effects:
            Sets "color_manager", "show edges", "edge_is_wedge", and "marker size" in
            params.

        Returns:
            dict: Containing the video markup parameters (in addition to other params).
        """
        if export_options.get("use_gui_visuals", False):
            params["color_manager"] = context.app.color_manager
        else:
            params["color_manager"] = None

        params["show edges"] = context.state.get("show edges", default=True)
        params["edge_is_wedge"] = (
            context.state.get("edge style", default="").lower() == "wedge"
        )

        params["marker size"] = context.state.get("marker size", default=4)
        return params

ask(context, params) classmethod

Ask the user for parameters to export a video clip.

Parameters:

Name Type Description Default
context CommandContext

The command context.

required
params dict

The parameters for the export.

required

Returns:

Name Type Description
bool bool

True if the user provided valid parameters, False otherwise.

Source code in sleap/gui/commands.py
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
@classmethod
def ask(cls, context: CommandContext, params: dict) -> bool:
    """Ask the user for parameters to export a video clip.

    Args:
        context: The command context.
        params: The parameters for the export.

    Returns:
        bool: True if the user provided valid parameters, False otherwise.
    """
    # Open export dialog.
    export_options = cls.get_export_options(context, params)
    if export_options is None:  # User hit cancel.
        return False

    # If we had a pop-up dialog, then we can also show GUI progress.
    params["gui_progress"] = True

    # Get video save parameters.
    params = cls.get_video_save_params(params, export_options)

    # Get frame range parameters.
    params = cls.get_frame_range_params(context, params)

    # Get video augmentation parameters.
    params = cls.get_video_augmentation_params(context, params, export_options)

    # Get video markup parameters.
    params = cls.get_video_markup_params(context, params, export_options)

    return True

do_action(context, params) classmethod

Export video clip using the parameters provided.

Parameters:

Name Type Description Default
context CommandContext

The command context.

required
params dict

The parameters for the export.

required
Source code in sleap/gui/commands.py
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
@classmethod
def do_action(cls, context: CommandContext, params: dict):
    """Export video clip using the parameters provided.

    Args:
        context: The command context.
        params: The parameters for the export.
    """
    # Write the new video using the parameters provided.
    cls.write_new_video(context, params)

    # Open the file using default video playing app
    if params["open_when_done"]:
        open_file(params["video_filename"])

get_export_options(context, params) classmethod

Get export options from the user.

Parameters:

Name Type Description Default
context CommandContext

The command context.

required
params dict

The parameters for the export.

required

Returns:

Name Type Description
dict dict | None

The export options.

Source code in sleap/gui/commands.py
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
@classmethod
def get_export_options(cls, context: CommandContext, params: dict) -> dict | None:
    """Get export options from the user.

    Args:
        context: The command context.
        params: The parameters for the export.

    Returns:
        dict: The export options.
    """
    from sleap.gui.dialogs.export_clip import ExportClipDialog

    form_name = params.get("form_name", "video_clip_form")
    dialog = ExportClipDialog(form_name=form_name)

    # Set default fps from video (if video has fps attribute)
    dialog.form_widget.set_form_data(
        dict(fps=getattr(context.state["video"], "fps", 30))
    )

    # Show modal dialog and get form results
    export_options = dialog.get_results()

    # Check if user hit cancel
    if export_options is None:
        return False

    default_out_basename = params.get("filename", context.state["filename"])

    # For OpenCV we default to avi since the bundled ffmpeg
    # makes mp4's that most programs can't open (VLC can).
    default_out_filename = default_out_basename + ".avi"

    if can_use_ffmpeg():
        default_out_filename = default_out_basename + ".mp4"

    # Ask where user wants to save video file
    filename, _ = FileDialog.save(
        context.app,
        caption="Save Video As...",
        dir=default_out_filename,
        filter="Video (*.avi *.mp4)",
    )

    # Check if user hit cancel
    if len(filename) == 0:
        return None

    export_options["video_filename"] = filename
    return export_options

get_frame_range_params(context, params) classmethod

Get frame range parameters.

Parameters:

Name Type Description Default
context CommandContext

The command context.

required
params dict

The parameters for the export.

required
Side Effects

Sets "frames" in params.

Returns:

Name Type Description
dict dict

Containing the frame range parameters (in addition to other params).

Source code in sleap/gui/commands.py
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
@classmethod
def get_frame_range_params(cls, context: CommandContext, params: dict) -> dict:
    """Get frame range parameters.

    Args:
        context: The command context.
        params: The parameters for the export.

    Side Effects:
        Sets "frames" in params.

    Returns:
        dict: Containing the frame range parameters (in addition to other params).
    """
    # If user selected a clip, use that; otherwise include all frames.
    if context.state["has_frame_range"]:
        params["frames"] = range(*context.state["frame_range"])
    else:
        params["frames"] = range(len(context.state["video"]))

    return params

get_video_augmentation_params(context, params, export_options) classmethod

Get video augmentation parameters.

Parameters:

Name Type Description Default
context CommandContext

The command context.

required
params dict

The parameters for the export.

required
export_options dict

The export options.

required
Side Effects

Sets "scale", "background", and "crop" in params.

Returns:

Name Type Description
dict dict

Containing the video augmentation parameters (in addition to other params).

Source code in sleap/gui/commands.py
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
@classmethod
def get_video_augmentation_params(
    cls, context: CommandContext, params: dict, export_options: dict
) -> dict:
    """Get video augmentation parameters.

    Args:
        context: The command context.
        params: The parameters for the export.
        export_options: The export options.

    Side Effects:
        Sets "scale", "background", and "crop" in params.

    Returns:
        dict: Containing the video augmentation parameters (in addition to other
            params).
    """
    params["scale"] = export_options.get("scale", 1.0)
    params["background"] = export_options.get("background", None)
    params["crop"] = None

    export_options_crop = export_options.get("crop", None)
    if export_options_crop is None:
        return params

    # Determine crop size relative to original size and scale
    # (crop size should be *final* output size, thus already scaled).
    video = context.state["video"]
    img_h, img_w = video.shape[1:3]
    w = int(img_w * params["scale"])
    h = int(img_h * params["scale"])
    if export_options_crop == "Half":
        params["crop"] = (w // 2, h // 2)
    elif export_options_crop == "Quarter":
        params["crop"] = (w // 4, h // 4)

    return params

get_video_markup_params(context, params, export_options) classmethod

Get video markup parameters.

Parameters:

Name Type Description Default
context CommandContext

The command context.

required
params dict

The parameters for the export.

required
export_options dict

The export options.

required
Side Effects

Sets "color_manager", "show edges", "edge_is_wedge", and "marker size" in params.

Returns:

Name Type Description
dict dict

Containing the video markup parameters (in addition to other params).

Source code in sleap/gui/commands.py
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
@classmethod
def get_video_markup_params(
    cls, context: CommandContext, params: dict, export_options: dict
) -> dict:
    """Get video markup parameters.

    Args:
        context: The command context.
        params: The parameters for the export.
        export_options: The export options.

    Side Effects:
        Sets "color_manager", "show edges", "edge_is_wedge", and "marker size" in
        params.

    Returns:
        dict: Containing the video markup parameters (in addition to other params).
    """
    if export_options.get("use_gui_visuals", False):
        params["color_manager"] = context.app.color_manager
    else:
        params["color_manager"] = None

    params["show edges"] = context.state.get("show edges", default=True)
    params["edge_is_wedge"] = (
        context.state.get("edge style", default="").lower() == "wedge"
    )

    params["marker size"] = context.state.get("marker size", default=4)
    return params

get_video_save_params(params, export_options) classmethod

Get video save parameters.

Parameters:

Name Type Description Default
params dict

The parameters for the export.

required
export_options dict

The export options.

required
Side Effects

Sets "video_filename", "fps", and "open_when_done" in params.

Returns:

Name Type Description
dict dict

Containing the video save parameters (in addition to other params).

Source code in sleap/gui/commands.py
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
@classmethod
def get_video_save_params(cls, params: dict, export_options: dict) -> dict:
    """Get video save parameters.

    Args:
        params: The parameters for the export.
        export_options: The export options.

    Side Effects:
        Sets "video_filename", "fps", and "open_when_done" in params.

    Returns:
        dict: Containing the video save parameters (in addition to other params).
    """
    params["video_filename"] = export_options["video_filename"]
    params["fps"] = export_options["fps"]
    params["open_when_done"] = export_options["open_when_done"]
    return params

write_new_video(context, params) classmethod

Write a new video using the parameters provided.

Parameters:

Name Type Description Default
context CommandContext

The command context.

required
params dict

The parameters for the export.

required
Source code in sleap/gui/commands.py
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
@classmethod
def write_new_video(
    cls,
    context: CommandContext,
    params: dict,
) -> None:
    """Write a new video using the parameters provided.

    Args:
        context: The command context.
        params: The parameters for the export.
    """
    # write_video(
    #     filename=params["video_filename"],
    #     video=context.state["video"],
    #     frames=list(params["frames"]),
    #     fps=params["fps"],
    #     scale=params["scale"],
    #     background=params["background"],
    #     gui_progress=params["gui_progress"],
    # )
    save_video(
        frames=[
            context.state["video"][frame_idx] for frame_idx in params["frames"]
        ],
        filename=params["video_filename"],
        fps=params["fps"],
    )

FakeApp

Use if you want to execute commands independently of the GUI app.

Source code in sleap/gui/commands.py
226
227
228
229
230
@attr.s(auto_attribs=True)
class FakeApp:
    """Use if you want to execute commands independently of the GUI app."""

    labels: Labels

GoIteratorCommand

Bases: AppCommand

Source code in sleap/gui/commands.py
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
class GoIteratorCommand(AppCommand):
    @staticmethod
    def _plot_if_next(context, frame_iterator: Iterator) -> bool:
        """Plots next frame (if there is one) from iterator.

        Arguments:
            frame_iterator: The iterator from which we'll try to get next
            :class:`LabeledFrame`.

        Returns:
            True if we went to next frame.
        """
        try:
            next_lf = next(frame_iterator)
        except StopIteration:
            return False

        context.state["frame_idx"] = next_lf.frame_idx
        return True

    @staticmethod
    def _get_frame_iterator(context: CommandContext):
        raise NotImplementedError("Call to virtual method.")

    @classmethod
    def do_action(cls, context: CommandContext, params: dict):
        frames = cls._get_frame_iterator(context)
        cls._plot_if_next(context, frames)

InstanceDeleteCommand

Bases: EditCommand

Source code in sleap/gui/commands.py
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
class InstanceDeleteCommand(EditCommand):
    topics = [UpdateTopic.project_instances]

    @staticmethod
    def get_frame_instance_list(context: CommandContext, params: dict):
        raise NotImplementedError("Call to virtual method.")

    @staticmethod
    def _confirm_deletion(context: CommandContext, lf_inst_list: List) -> bool:
        """Helper function to confirm before deleting instances.

        Args:
            lf_inst_list: A list of (labeled frame, instance) tuples.
        """

        title = "Deleting instances"
        message = (
            f"There are {len(lf_inst_list)} instances which would be deleted. "
            f"Are you sure you want to delete these?"
        )

        # Confirm that we want to delete
        resp = QtWidgets.QMessageBox.critical(
            context.app,
            title,
            message,
            QtWidgets.QMessageBox.Yes,
            QtWidgets.QMessageBox.No,
        )

        if resp == QtWidgets.QMessageBox.No:
            return False

        return True

    @staticmethod
    def _do_deletion(context: CommandContext, lf_inst_list: List[int]):
        # Delete the instances
        lfs_to_remove = []
        for lf, inst in lf_inst_list:
            remove_instance(context.labels, instance=inst, lf=lf)
            if context.state["instance"] == inst:
                context.state["instance"] = None
            if len(lf.instances) == 0:
                lfs_to_remove.append(lf)

        remove_frames(context.labels, lfs_to_remove)

        # # Update caches since we skipped doing this after each deletion
        # context.labels.update_cache()

        # Update visuals
        context.changestack_push("delete instances")

    @classmethod
    def do_action(cls, context: CommandContext, params: dict):
        cls._do_deletion(context, params["lf_instance_list"])

    @classmethod
    def ask(cls, context: CommandContext, params: dict) -> bool:
        lf_inst_list = cls.get_frame_instance_list(context, params)
        params["lf_instance_list"] = lf_inst_list

        return cls._confirm_deletion(context, lf_inst_list)

LoadLabelsObject

Bases: AppCommand

Methods:

Name Description
do_action

Loads a Labels object into the GUI, replacing any currently loaded.

Source code in sleap/gui/commands.py
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
class LoadLabelsObject(AppCommand):
    @staticmethod
    def do_action(context: "CommandContext", params: dict):
        """Loads a `Labels` object into the GUI, replacing any currently loaded.

        Args:
            labels: The `Labels` object to load.
            filename: The filename where this file is saved, if any.

        Returns:
            None.
        """
        filename = params.get("filename", None)  # If called with just a Labels object
        labels: Labels = params["labels"]

        context.state["labels"] = labels
        context.state["filename"] = filename

        context.changestack_clear()
        context.app.color_manager.labels = context.labels
        context.app.color_manager.set_palette(context.state["palette"])

        context.app._load_overlays()

        if len(labels.skeletons):
            context.state["skeleton"] = labels.skeletons[0]

        # Load first video
        if len(labels.videos):
            context.state["video"] = labels.videos[0]

        context.state["project_loaded"] = True
        context.state["has_changes"] = params.get("changed_on_load", False) or (
            filename is None
        )

        # This is not listed as an edit command since we want a clean changestack
        context.app.on_data_update([UpdateTopic.project, UpdateTopic.all])

do_action(context, params) staticmethod

Loads a Labels object into the GUI, replacing any currently loaded.

Parameters:

Name Type Description Default
labels

The Labels object to load.

required
filename

The filename where this file is saved, if any.

required

Returns:

Type Description

None.

Source code in sleap/gui/commands.py
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
@staticmethod
def do_action(context: "CommandContext", params: dict):
    """Loads a `Labels` object into the GUI, replacing any currently loaded.

    Args:
        labels: The `Labels` object to load.
        filename: The filename where this file is saved, if any.

    Returns:
        None.
    """
    filename = params.get("filename", None)  # If called with just a Labels object
    labels: Labels = params["labels"]

    context.state["labels"] = labels
    context.state["filename"] = filename

    context.changestack_clear()
    context.app.color_manager.labels = context.labels
    context.app.color_manager.set_palette(context.state["palette"])

    context.app._load_overlays()

    if len(labels.skeletons):
        context.state["skeleton"] = labels.skeletons[0]

    # Load first video
    if len(labels.videos):
        context.state["video"] = labels.videos[0]

    context.state["project_loaded"] = True
    context.state["has_changes"] = params.get("changed_on_load", False) or (
        filename is None
    )

    # This is not listed as an edit command since we want a clean changestack
    context.app.on_data_update([UpdateTopic.project, UpdateTopic.all])

OpenSkeleton

Bases: EditCommand

Methods:

Name Description
do_action

Replace skeleton with new skeleton.

get_template_skeleton_filename

Helper function to get the template skeleton filename from dropdown.

Source code in sleap/gui/commands.py
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
class OpenSkeleton(EditCommand):
    topics = [UpdateTopic.skeleton]

    @staticmethod
    def load_skeleton(filename: str):
        from sleap_io.io.skeleton import SkeletonDecoder
        import simplejson as json

        with open(filename, "r") as f:
            skeleton_data = json.load(f)
            skeleton_data = (
                skeleton_data["nx_graph"]
                if "nx_graph" in skeleton_data
                else skeleton_data
            )
        skel = SkeletonDecoder().decode(data=skeleton_data)
        return skel

    @staticmethod
    def compare_skeletons(
        skeleton: Skeleton, new_skeleton: Skeleton
    ) -> Tuple[List[str], List[str], List[str]]:
        delete_nodes = []
        add_nodes = []
        if skeleton.node_names != new_skeleton.node_names:
            # Compare skeletons
            base_nodes = skeleton.node_names
            new_nodes = new_skeleton.node_names
            delete_nodes = [node for node in base_nodes if node not in new_nodes]
            add_nodes = [node for node in new_nodes if node not in base_nodes]

        # We want to run this even if the skeletons are the same
        rename_nodes = [
            node for node in skeleton.node_names if node not in delete_nodes
        ]

        return rename_nodes, delete_nodes, add_nodes

    @staticmethod
    def delete_extra_skeletons(labels: Labels):
        if len(labels.skeletons) > 1:
            skeletons_used = list(
                set(
                    [
                        inst.skeleton
                        for lf in labels.labeled_frames
                        for inst in lf.instances
                    ]
                )
            )
            try:
                assert len(skeletons_used) == 1
            except AssertionError:
                raise ValueError("Too many skeletons used in project.")

            labels.skeletons = skeletons_used

    @staticmethod
    def get_template_skeleton_filename(context: CommandContext) -> str:
        """Helper function to get the template skeleton filename from dropdown.

        Args:
            context: The `CommandContext`.

        Returns:
            Path to the template skeleton shipped with SLEAP.
        """

        template = context.app.skeleton_dock.skeleton_templates.currentText()
        filename = get_package_file(f"skeletons/{template}.json")
        return filename

    @staticmethod
    def ask(context: CommandContext, params: dict) -> bool:
        filters = ["JSON skeleton (*.json)", "HDF5 skeleton (*.h5 *.hdf5)"]
        # Check whether to load from file or preset
        if params.get("template", False):
            # Get selected template from dropdown
            filename = OpenSkeleton.get_template_skeleton_filename(context)
        else:
            filename, selected_filter = FileDialog.open(
                context.app,
                dir=None,
                caption="Open skeleton...",
                filter=";;".join(filters),
            )

        if len(filename) == 0:
            return False

        okay = True
        if len(context.labels.skeletons) > 0:
            # Ask user permission to merge skeletons
            okay = False
            skeleton: Skeleton = context.labels.skeleton  # Assumes single skeleton

            # Load new skeleton and compare
            new_skeleton = OpenSkeleton.load_skeleton(filename)
            (rename_nodes, delete_nodes, add_nodes) = OpenSkeleton.compare_skeletons(
                skeleton, new_skeleton
            )

            if (len(delete_nodes) > 0) or (len(add_nodes) > 0):
                # Allow user to link mismatched nodes
                query = ReplaceSkeletonTableDialog(
                    rename_nodes=rename_nodes,
                    delete_nodes=delete_nodes,
                    add_nodes=add_nodes,
                )
                query.exec_()

                # Give the okay to add/delete nodes
                linked_nodes: Optional[Dict[str, str]] = query.result()
                if linked_nodes is not None:
                    delete_nodes = list(set(delete_nodes) - set(linked_nodes.values()))
                    add_nodes = list(set(add_nodes) - set(linked_nodes.keys()))
                    params["linked_nodes"] = linked_nodes
                    okay = True

            params["delete_nodes"] = delete_nodes
            params["add_nodes"] = add_nodes

        params["filename"] = filename
        return okay

    @staticmethod
    def do_action(context: CommandContext, params: dict):
        """Replace skeleton with new skeleton.

        Note that we modify the existing skeleton in-place to essentially match the new
        skeleton. However, we cannot rename the skeleton since `Skeleton.name` is used
        for hashing (see `Skeleton.name` setter).

        Args:
            context: CommandContext
            params: dict
                filename: str
                delete_nodes: List[str]
                add_nodes: List[str]
                linked_nodes: Dict[str, str]

        Returns:
            None
        """

        # TODO (LM): This is a hack to get around the fact that we do some dangerous
        # in-place operations on the skeleton. We should fix this.
        def try_and_skip_if_error(func, *args, **kwargs):
            """This is a helper function to try and skip if there is an error."""
            try:
                func(*args, **kwargs)
            except Exception as e:
                tb_str = traceback.format_exception(
                    type(e), value=e, tb=e.__traceback__
                )
                logger.warning(
                    f"Recieved the following error while replacing skeleton:\n"
                    f"{''.join(tb_str)}"
                )

        # Load new skeleton
        filename = params["filename"]
        new_skeleton = OpenSkeleton.load_skeleton(filename)

        # Description and preview image only used for template skeletons
        # new_skeleton.description = None
        # new_skeleton.preview_image = None
        # context.state["skeleton_description"] = new_skeleton.description
        # context.state["skeleton_preview_image"] = new_skeleton.preview_image

        # Case 1: No skeleton exists in project
        if len(context.labels.skeletons) == 0:
            context.state["skeleton"] = new_skeleton
            context.labels.skeletons.append(context.state["skeleton"])
            return

        # Case 2: Skeleton(s) already exist(s) in project

        # Delete extra skeletons in project
        OpenSkeleton.delete_extra_skeletons(context.labels)
        skeleton = context.labels.skeleton  # Assume single skeleton

        if "delete_nodes" in params.keys():
            # We already compared skeletons in ask() method
            delete_nodes: List[str] = params["delete_nodes"]
            add_nodes: List[str] = params["add_nodes"]
        else:
            # Otherwise, load new skeleton and compare
            (rename_nodes, delete_nodes, add_nodes) = OpenSkeleton.compare_skeletons(
                skeleton, new_skeleton
            )

        # Delete pre-existing symmetry
        for symmetry in skeleton.symmetries:
            # In sleap-io, symmetry.nodes is a set, not a list
            nodes_list = list(symmetry.nodes)
            delete_symmetry(skeleton, nodes_list[0].name, nodes_list[1].name)

        # Link mismatched nodes
        if "linked_nodes" in params.keys():
            linked_nodes = params["linked_nodes"]
            for new_name, old_name in linked_nodes.items():
                try_and_skip_if_error(skeleton.rename_node, old_name, new_name)

        # Delete nodes from skeleton that are not in new skeleton
        for node in delete_nodes:
            try_and_skip_if_error(skeleton.remove_node, node)

        # Add nodes that only exist in the new skeleton
        for node in add_nodes:
            try_and_skip_if_error(skeleton.add_node, node)

        # Add edges
        # skeleton.clear_edges()
        if isinstance(skeleton, Skeleton):
            skeleton.edges = []
        elif isinstance(skeleton, List[Skeleton]):
            for skl in skeleton:
                skl.edges = []
        for src, dest in new_skeleton.edges:
            try_and_skip_if_error(skeleton.add_edge, src.name, dest.name)

        # Add new symmetry
        for src, dst in new_skeleton.symmetries:
            try_and_skip_if_error(skeleton.add_symmetry, src.name, dst.name)

        # Set state of context
        context.state["skeleton"] = skeleton

do_action(context, params) staticmethod

Replace skeleton with new skeleton.

Note that we modify the existing skeleton in-place to essentially match the new skeleton. However, we cannot rename the skeleton since Skeleton.name is used for hashing (see Skeleton.name setter).

Parameters:

Name Type Description Default
context CommandContext

CommandContext

required
params dict

dict filename: str delete_nodes: List[str] add_nodes: List[str] linked_nodes: Dict[str, str]

required

Returns:

Type Description

None

Source code in sleap/gui/commands.py
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
@staticmethod
def do_action(context: CommandContext, params: dict):
    """Replace skeleton with new skeleton.

    Note that we modify the existing skeleton in-place to essentially match the new
    skeleton. However, we cannot rename the skeleton since `Skeleton.name` is used
    for hashing (see `Skeleton.name` setter).

    Args:
        context: CommandContext
        params: dict
            filename: str
            delete_nodes: List[str]
            add_nodes: List[str]
            linked_nodes: Dict[str, str]

    Returns:
        None
    """

    # TODO (LM): This is a hack to get around the fact that we do some dangerous
    # in-place operations on the skeleton. We should fix this.
    def try_and_skip_if_error(func, *args, **kwargs):
        """This is a helper function to try and skip if there is an error."""
        try:
            func(*args, **kwargs)
        except Exception as e:
            tb_str = traceback.format_exception(
                type(e), value=e, tb=e.__traceback__
            )
            logger.warning(
                f"Recieved the following error while replacing skeleton:\n"
                f"{''.join(tb_str)}"
            )

    # Load new skeleton
    filename = params["filename"]
    new_skeleton = OpenSkeleton.load_skeleton(filename)

    # Description and preview image only used for template skeletons
    # new_skeleton.description = None
    # new_skeleton.preview_image = None
    # context.state["skeleton_description"] = new_skeleton.description
    # context.state["skeleton_preview_image"] = new_skeleton.preview_image

    # Case 1: No skeleton exists in project
    if len(context.labels.skeletons) == 0:
        context.state["skeleton"] = new_skeleton
        context.labels.skeletons.append(context.state["skeleton"])
        return

    # Case 2: Skeleton(s) already exist(s) in project

    # Delete extra skeletons in project
    OpenSkeleton.delete_extra_skeletons(context.labels)
    skeleton = context.labels.skeleton  # Assume single skeleton

    if "delete_nodes" in params.keys():
        # We already compared skeletons in ask() method
        delete_nodes: List[str] = params["delete_nodes"]
        add_nodes: List[str] = params["add_nodes"]
    else:
        # Otherwise, load new skeleton and compare
        (rename_nodes, delete_nodes, add_nodes) = OpenSkeleton.compare_skeletons(
            skeleton, new_skeleton
        )

    # Delete pre-existing symmetry
    for symmetry in skeleton.symmetries:
        # In sleap-io, symmetry.nodes is a set, not a list
        nodes_list = list(symmetry.nodes)
        delete_symmetry(skeleton, nodes_list[0].name, nodes_list[1].name)

    # Link mismatched nodes
    if "linked_nodes" in params.keys():
        linked_nodes = params["linked_nodes"]
        for new_name, old_name in linked_nodes.items():
            try_and_skip_if_error(skeleton.rename_node, old_name, new_name)

    # Delete nodes from skeleton that are not in new skeleton
    for node in delete_nodes:
        try_and_skip_if_error(skeleton.remove_node, node)

    # Add nodes that only exist in the new skeleton
    for node in add_nodes:
        try_and_skip_if_error(skeleton.add_node, node)

    # Add edges
    # skeleton.clear_edges()
    if isinstance(skeleton, Skeleton):
        skeleton.edges = []
    elif isinstance(skeleton, List[Skeleton]):
        for skl in skeleton:
            skl.edges = []
    for src, dest in new_skeleton.edges:
        try_and_skip_if_error(skeleton.add_edge, src.name, dest.name)

    # Add new symmetry
    for src, dst in new_skeleton.symmetries:
        try_and_skip_if_error(skeleton.add_symmetry, src.name, dst.name)

    # Set state of context
    context.state["skeleton"] = skeleton

get_template_skeleton_filename(context) staticmethod

Helper function to get the template skeleton filename from dropdown.

Parameters:

Name Type Description Default
context CommandContext

The CommandContext.

required

Returns:

Type Description
str

Path to the template skeleton shipped with SLEAP.

Source code in sleap/gui/commands.py
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
@staticmethod
def get_template_skeleton_filename(context: CommandContext) -> str:
    """Helper function to get the template skeleton filename from dropdown.

    Args:
        context: The `CommandContext`.

    Returns:
        Path to the template skeleton shipped with SLEAP.
    """

    template = context.app.skeleton_dock.skeleton_templates.currentText()
    filename = get_package_file(f"skeletons/{template}.json")
    return filename

ReplaceVideo

Bases: EditCommand

Methods:

Name Description
ask

Shows gui for replacing videos in project.

Source code in sleap/gui/commands.py
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
class ReplaceVideo(EditCommand):
    topics = [UpdateTopic.video, UpdateTopic.frame]

    @staticmethod
    def do_action(context: CommandContext, params: dict) -> bool:
        import_list = params["import_list"]

        for import_item, video in import_list:
            import_params = import_item["params"]

            # TODO: Will need to create a new backend if import has different extension.
            if Path(video.filename).suffix != Path(import_params["filename"]).suffix:
                raise TypeError(
                    "Importing videos with different extensions is not supported."
                )
            # video.backend.reset(**import_params) potential breaking change
            video_util_reset(video, **import_params)

            # Remove frames in video past last frame index
            last_vid_frame = get_last_frame_idx(video)
            lfs: List[LabeledFrame] = list(context.labels.find(video))
            if lfs is not None:
                lfs = [lf for lf in lfs if lf.frame_idx > last_vid_frame]
                remove_frames(context.labels, lfs)

            # Update seekbar and video length through callbacks
            context.state.emit("video")

    @staticmethod
    def ask(context: CommandContext, params: dict) -> bool:
        """Shows gui for replacing videos in project."""

        def _get_truncation_message(truncation_messages, path, video):
            reader = cv2.VideoCapture(path)
            last_vid_frame = int(reader.get(cv2.CAP_PROP_FRAME_COUNT))
            lfs: List[LabeledFrame] = list(context.labels.find(video))
            if lfs is not None:
                lfs.sort(key=lambda lf: lf.frame_idx)
                last_lf_frame = lfs[-1].frame_idx
                lfs = [lf for lf in lfs if lf.frame_idx > last_vid_frame]

                # Message to warn users that labels will be removed if proceed
                if last_lf_frame > last_vid_frame:
                    message = (
                        "<p><strong>Warning:</strong> Replacing this video will "
                        f"remove {len(lfs)} labeled frames.</p>"
                        f"<p><em>Current video</em>: <b>{Path(video.filename).name}</b>"
                        f" (last label at frame {last_lf_frame})<br>"
                        f"<em>Replacement video</em>: <b>{Path(path).name}"
                        f"</b> ({last_vid_frame} frames)</p>"
                    )
                    # Assumes that a project won't import the same video multiple times
                    truncation_messages[path] = message

            return truncation_messages

        # Warn user: newly added labels will be discarded if project is not saved
        if not context.state["filename"] or context.state["has_changes"]:
            QtWidgets.QMessageBox(
                text=("You have unsaved changes. Please save before replacing videos.")
            ).exec_()
            return False

        # Select the videos we want to swap
        old_paths = [video.filename for video in context.labels.videos]
        paths = list(old_paths)
        okay = MissingFilesDialog(filenames=paths, replace=True).exec_()
        if not okay:
            return False

        # Only return an import list for videos we swap
        new_paths = [
            (path, video_idx)
            for video_idx, (path, old_path) in enumerate(zip(paths, old_paths))
            if path != old_path
        ]

        new_paths = []
        old_videos = dict()
        all_videos = context.labels.videos
        truncation_messages = dict()
        for video_idx, (path, old_path) in enumerate(zip(paths, old_paths)):
            if path != old_path:
                new_paths.append(path)
                old_videos[path] = all_videos[video_idx]
                truncation_messages = _get_truncation_message(
                    truncation_messages, path, video=all_videos[video_idx]
                )

        import_list = ImportVideos().ask(
            filenames=new_paths, messages=truncation_messages
        )
        # Remove videos that no longer correlate to filenames.
        old_videos_to_replace = [
            old_videos[imp["params"]["filename"]] for imp in import_list
        ]
        params["import_list"] = zip(import_list, old_videos_to_replace)

        return len(import_list) > 0

ask(context, params) staticmethod

Shows gui for replacing videos in project.

Source code in sleap/gui/commands.py
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
@staticmethod
def ask(context: CommandContext, params: dict) -> bool:
    """Shows gui for replacing videos in project."""

    def _get_truncation_message(truncation_messages, path, video):
        reader = cv2.VideoCapture(path)
        last_vid_frame = int(reader.get(cv2.CAP_PROP_FRAME_COUNT))
        lfs: List[LabeledFrame] = list(context.labels.find(video))
        if lfs is not None:
            lfs.sort(key=lambda lf: lf.frame_idx)
            last_lf_frame = lfs[-1].frame_idx
            lfs = [lf for lf in lfs if lf.frame_idx > last_vid_frame]

            # Message to warn users that labels will be removed if proceed
            if last_lf_frame > last_vid_frame:
                message = (
                    "<p><strong>Warning:</strong> Replacing this video will "
                    f"remove {len(lfs)} labeled frames.</p>"
                    f"<p><em>Current video</em>: <b>{Path(video.filename).name}</b>"
                    f" (last label at frame {last_lf_frame})<br>"
                    f"<em>Replacement video</em>: <b>{Path(path).name}"
                    f"</b> ({last_vid_frame} frames)</p>"
                )
                # Assumes that a project won't import the same video multiple times
                truncation_messages[path] = message

        return truncation_messages

    # Warn user: newly added labels will be discarded if project is not saved
    if not context.state["filename"] or context.state["has_changes"]:
        QtWidgets.QMessageBox(
            text=("You have unsaved changes. Please save before replacing videos.")
        ).exec_()
        return False

    # Select the videos we want to swap
    old_paths = [video.filename for video in context.labels.videos]
    paths = list(old_paths)
    okay = MissingFilesDialog(filenames=paths, replace=True).exec_()
    if not okay:
        return False

    # Only return an import list for videos we swap
    new_paths = [
        (path, video_idx)
        for video_idx, (path, old_path) in enumerate(zip(paths, old_paths))
        if path != old_path
    ]

    new_paths = []
    old_videos = dict()
    all_videos = context.labels.videos
    truncation_messages = dict()
    for video_idx, (path, old_path) in enumerate(zip(paths, old_paths)):
        if path != old_path:
            new_paths.append(path)
            old_videos[path] = all_videos[video_idx]
            truncation_messages = _get_truncation_message(
                truncation_messages, path, video=all_videos[video_idx]
            )

    import_list = ImportVideos().ask(
        filenames=new_paths, messages=truncation_messages
    )
    # Remove videos that no longer correlate to filenames.
    old_videos_to_replace = [
        old_videos[imp["params"]["filename"]] for imp in import_list
    ]
    params["import_list"] = zip(import_list, old_videos_to_replace)

    return len(import_list) > 0

SaveProjectAs

Bases: AppCommand

Source code in sleap/gui/commands.py
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
class SaveProjectAs(AppCommand):
    @staticmethod
    def _try_save(context, labels: Labels, filename: str):
        """Helper function which attempts save and handles errors."""
        import sleap_io as sio

        success = False
        try:
            extension = (PurePath(filename).suffix)[1:]
            extension = None if (extension == "slp") else extension
            if extension == "nwb":
                sio.save_nwb(labels=labels, filename=filename)
            else:
                save_file(labels=labels, filename=filename, format=extension)
            success = True
            # Mark savepoint in change stack
            context.changestack_savepoint()

        except Exception as e:
            message = (
                f"An error occured when attempting to save:\n {e}\n\n"
                "Try saving your project with a different filename or in a different "
                "format."
            )
            QtWidgets.QMessageBox(text=message).exec_()

        # Redraw. Not sure why, but sometimes we need to do this.
        context.app.plotFrame()

        return success

    @classmethod
    def do_action(cls, context: CommandContext, params: dict):
        if cls._try_save(context, context.state["labels"], params["filename"]):
            # If save was successful
            context.state["filename"] = params["filename"]

    @staticmethod
    def ask(context: CommandContext, params: dict) -> bool:
        default_name = context.state["filename"] or "labels.v000.slp"
        if "adaptor" in params:
            adaptor: Adaptor = params["adaptor"]
            if adaptor == "nwb":
                default_name += ".nwb"
                filters = ["(*.nwb)"]
                filters[0] = f"NWB {filters[0]}"
        else:
            filters = ["SLEAP labels dataset (*.slp)"]
            if default_name:
                default_name = get_new_version_filename(default_name)

        filename, selected_filter = FileDialog.save(
            context.app,
            caption="Save As...",
            dir=default_name,
            filter=";;".join(filters),
        )

        if len(filename) == 0:
            return False

        params["filename"] = filename
        return True

SetInstancePointLocations

Bases: EditCommand

Sets locations for node(s) for an instance.

Note: It's important that this command does not update the visual scene, since this would redraw the frame and create new visual objects. The calling code is responsible for updating the visual scene.

Parameters:

Name Type Description Default
instance

The instance

required
nodes_locations

A dictionary of data to set

required
Source code in sleap/gui/commands.py
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
class SetInstancePointLocations(EditCommand):
    """Sets locations for node(s) for an instance.

    Note: It's important that this command does *not* update the visual
    scene, since this would redraw the frame and create new visual objects.
    The calling code is responsible for updating the visual scene.

    Params:
        instance: The instance
        nodes_locations: A dictionary of data to set
        * keys are nodes (or node names)
        * values are (x, y) coordinate tuples.
    """

    topics = []

    @classmethod
    def do_action(cls, context: "CommandContext", params: dict):
        instance = params["instance"]
        nodes_locations = params["nodes_locations"]

        for node, (x, y) in nodes_locations.items():
            if node in instance.skeleton.node_names:
                instance[node]["xy"] = np.array([x, y])

SetInstancePointVisibility

Bases: EditCommand

Toggles visibility set for a node for an instance.

Note: It's important that this command does not update the visual scene, since this would redraw the frame and create new visual objects. The calling code is responsible for updating the visual scene.

Parameters:

Name Type Description Default
instance

The instance

required
node

The Node (or name string)

required
visible

Whether to set or clear visibility for node

required
Source code in sleap/gui/commands.py
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
class SetInstancePointVisibility(EditCommand):
    """Toggles visibility set for a node for an instance.

    Note: It's important that this command does *not* update the visual
    scene, since this would redraw the frame and create new visual objects.
    The calling code is responsible for updating the visual scene.

    Params:
        instance: The instance
        node: The `Node` (or name string)
        visible: Whether to set or clear visibility for node
    """

    topics = []

    @classmethod
    def do_action(cls, context: "CommandContext", params: dict):
        instance = params["instance"]
        node = params["node"]
        visible = params["visible"]

        node_name = node if isinstance(node, str) else node.name
        instance[node_name]["visible"] = visible

ToggleGrayscale

Bases: EditCommand

Methods:

Name Description
do_action

Reset the video backend.

Source code in sleap/gui/commands.py
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
class ToggleGrayscale(EditCommand):
    topics = [UpdateTopic.video, UpdateTopic.frame]

    @staticmethod
    def do_action(context: CommandContext, params: dict):
        """Reset the video backend."""

        def try_to_read_grayscale(video: Video):
            try:
                return video.grayscale
            except Exception:
                return None

        # Check that current video is set
        if len(context.labels.videos) == 0:
            raise ValueError("No videos detected in `Labels`.")

        # Intuitively find the "first" video that supports grayscale
        grayscale = try_to_read_grayscale(context.state["video"])
        if grayscale is None:
            for video in context.labels.videos:
                grayscale = try_to_read_grayscale(video)
                if grayscale is not None:
                    break

        if grayscale is None:
            raise ValueError("No videos support grayscale.")

        for idx, video in enumerate(context.labels.videos):
            try:
                # video.backend.reset(grayscale=(not grayscale))
                video_util_reset(video, grayscale=(not grayscale))
            except Exception:
                print(
                    f"This video type {type(video.backend)} for video at index {idx} "
                    f"does not support grayscale yet."
                )

do_action(context, params) staticmethod

Reset the video backend.

Source code in sleap/gui/commands.py
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
@staticmethod
def do_action(context: CommandContext, params: dict):
    """Reset the video backend."""

    def try_to_read_grayscale(video: Video):
        try:
            return video.grayscale
        except Exception:
            return None

    # Check that current video is set
    if len(context.labels.videos) == 0:
        raise ValueError("No videos detected in `Labels`.")

    # Intuitively find the "first" video that supports grayscale
    grayscale = try_to_read_grayscale(context.state["video"])
    if grayscale is None:
        for video in context.labels.videos:
            grayscale = try_to_read_grayscale(video)
            if grayscale is not None:
                break

    if grayscale is None:
        raise ValueError("No videos support grayscale.")

    for idx, video in enumerate(context.labels.videos):
        try:
            # video.backend.reset(grayscale=(not grayscale))
            video_util_reset(video, grayscale=(not grayscale))
        except Exception:
            print(
                f"This video type {type(video.backend)} for video at index {idx} "
                f"does not support grayscale yet."
            )

UpdateTopic

Bases: Enum

Topics so context can tell callback what was updated by the command.

Source code in sleap/gui/commands.py
128
129
130
131
132
133
134
135
136
137
138
139
140
class UpdateTopic(Enum):
    """Topics so context can tell callback what was updated by the command."""

    all = 1
    video = 2
    skeleton = 3
    labels = 4
    on_frame = 5
    suggestions = 6
    tracks = 7
    frame = 8
    project = 9
    project_instances = 10

copy_to_clipboard(text)

Copy a string to the system clipboard. Args: text: String to copy to clipboard.

Source code in sleap/gui/commands.py
4004
4005
4006
4007
4008
4009
4010
4011
def copy_to_clipboard(text: str):
    """Copy a string to the system clipboard.
    Args:
        text: String to copy to clipboard.
    """
    clipboard = QtWidgets.QApplication.clipboard()
    clipboard.clear(mode=clipboard.Clipboard)
    clipboard.setText(text, mode=clipboard.Clipboard)

export_dataset_gui(labels, filename, all_labeled=False, suggested=False, verbose=True, as_package=True)

Export dataset with image data and display progress GUI dialog.

Parameters:

Name Type Description Default
labels Labels

sleap.Labels dataset to export.

required
filename str

Output filename. Should end in .pkg.slp.

required
all_labeled bool

If True, export all labeled frames, including frames with no user instances. Defaults to False.

False
suggested bool

If True, include image data for suggested frames. Defaults to False.

False
verbose bool

If True, display progress dialog. Defaults to True.

True
as_package bool

If True, save as a package (saves image data instead of referencing video). Defaults to True.

True
Source code in sleap/gui/commands.py
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
def export_dataset_gui(
    labels: Labels,
    filename: str,
    all_labeled: bool = False,
    suggested: bool = False,
    verbose: bool = True,
    as_package: bool = True,
) -> str:
    """Export dataset with image data and display progress GUI dialog.

    Args:
        labels: `sleap.Labels` dataset to export.
        filename: Output filename. Should end in `.pkg.slp`.
        all_labeled: If `True`, export all labeled frames, including frames with no user
            instances. Defaults to `False`.
        suggested: If `True`, include image data for suggested frames. Defaults to
            `False`.
        verbose: If `True`, display progress dialog. Defaults to `True`.
        as_package: If `True`, save as a package (saves image data instead of
            referencing video). Defaults to `True`.
    """
    if verbose:
        win = QtWidgets.QProgressDialog(
            "Exporting dataset with frame images...", "Cancel", 0, 1
        )

    def update_progress(n, n_total):
        if win.wasCanceled():
            return False
        win.setMaximum(n_total)
        win.setValue(n)
        win.setLabelText(
            f"Exporting dataset with frame images...<br>{n}/{n_total} "
            f"(<b>{(n / n_total) * 100:.1f}%</b>)"
        )
        QtWidgets.QApplication.instance().processEvents()
        return True

    embed_option = "all" if all_labeled else "user+suggestions" if suggested else "user"
    save_file(
        labels,
        filename,
        format="slp",
        embed=embed_option if as_package else False,
        # progress_callback=update_progress if verbose else None, #TODO
    )

    if verbose:
        if win.wasCanceled():
            # Delete output if saving was canceled.
            os.remove(filename)
            return "canceled"

        win.hide()

    return filename

get_new_version_filename(filename)

Increment version number in filenames that end in .v###.slp.

Source code in sleap/gui/commands.py
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
def get_new_version_filename(filename: str) -> str:
    """Increment version number in filenames that end in `.v###.slp`."""
    p = PurePath(filename)

    match = re.match(".*\\.v(\\d+)\\.slp", filename)
    if match is not None:
        old_ver = match.group(1)
        new_ver = str(int(old_ver) + 1).zfill(len(old_ver))
        filename = filename.replace(f".v{old_ver}.slp", f".v{new_ver}.slp")
        filename = str(PurePath(filename))
    else:
        filename = str(p.with_name(f"{p.stem} copy{p.suffix}"))

    return filename

open_file(filename)

Opens file in native system file browser or registered application.

Parameters:

Name Type Description Default
filename str

Path to file or folder.

required
Notes
Source code in sleap/gui/commands.py
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
def open_file(filename: str):
    """Opens file in native system file browser or registered application.

    Args:
        filename: Path to file or folder.

    Notes:
        Source: https://stackoverflow.com/a/16204023
    """
    if sys.platform == "win32":
        os.startfile(filename)
    else:
        opener = "open" if sys.platform == "darwin" else "xdg-open"
        subprocess.call([opener, filename])

open_website(url)

Open website in default browser.

Parameters:

Name Type Description Default
url str

URL to open.

required
Source code in sleap/gui/commands.py
3956
3957
3958
3959
3960
3961
3962
def open_website(url: str):
    """Open website in default browser.

    Args:
        url: URL to open.
    """
    QtGui.QDesktopServices.openUrl(QtCore.QUrl(url))