lf_labels_utils
sleap.sleap_io_adaptors.lf_labels_utils
¶
Standalone utility functions for working with Labels and LabeledFrame objects.
Functions:
| Name | Description |
|---|---|
add_suggestion |
Add a suggestion to the labels dataset. |
clear_suggestion |
Delete all suggestions. |
find_first |
Find the first occurrence of a matching labeled frame. |
find_last |
Find the last occurrence of a matching labeled frame. |
find_path_using_paths |
Find a file in the given search paths. |
find_suggestion |
Return the suggestion for the given (video, frame_idx) or None. |
find_track_occupancy |
Get instances for a given video, track, and range of frames. |
fix_paths_with_saved_prefix |
Try to fix missing file paths using saved prefix conversions. |
get_instances_to_show |
Return a list of instances to show in GUI for this frame. |
get_labeled_frame_count |
Return count of frames matching video/filter. |
get_next_suggestion |
Return a (video, frame_idx) tuple seeking from given frame. |
get_predictions_on_user_frames |
Find predictions on frames that have user instances. |
get_template_instance_points |
Get template instance points for a skeleton. |
get_track_occupancy |
Get track occupancy information for a specific video. |
get_unused_predictions |
Return a list of "unused" PredictedInstance objects in frame. |
get_video_suggestions |
Get suggested frame indices for a specific video. |
iterate_labeled_frames |
Return an iterator over lfs in a video with start pos (opt) and order control. |
labeled_frame_find |
Find instances in a labeled frame that match the given track. |
labels_add_instance |
Add an instance to a labeled frame. |
labels_add_video |
Add a video to the Labels object with duplicate prevention. |
labels_all_instances |
Get all instances as a list for backward compatibility. |
labels_append_suggestions |
Append suggestions to the Labels object. |
labels_clear_suggestions |
Clear all suggestions from labels for backward compatibility. |
labels_copy |
Create a copy of the Labels object. |
labels_frames |
Get labeled frames, optionally filtered by video. |
labels_get |
Get labeled frames for backward compatibility. |
labels_get_labels_attr |
Get labeled frames for backward compatibility. |
labels_get_nodes |
Get skeleton nodes for backward compatibility. |
labels_get_suggestions |
Get all suggestions from labels for backward compatibility. |
labels_load_file |
Load a Labels object from file. |
labels_pop |
Remove and return a labeled frame at the given index. |
labels_remove_frame |
Remove a single labeled frame from the Labels object. |
load_labels_video_search |
Load labels file with video path search/resolution. |
make_video_callback |
Adapter function for callback function to finding missing video. |
merge_nodes |
Merge two nodes and update data accordingly. |
merge_nodes_data |
Copy point data from one node to another. |
remove_all_tracks |
Remove all tracks from the labels dataset and update all related instances. |
remove_frames |
Remove a list of frames from the labels dataset. |
remove_instance |
Remove an instance from a labeled frame and update all related instances. |
remove_track |
Remove a track from the labels dataset and update all related instances. |
remove_unused_tracks |
Remove all tracks from the labels dataset that are not used by any instances. |
remove_video |
Remove a video from the labels dataset and update all related instances. |
track_set_instance |
Set track on given instance, updating occupancy. |
track_swap |
Swap track assignment for instances in two tracks. |
add_suggestion(labels, video, frame_idx)
¶
clear_suggestion(labels)
¶
find_first(labels, video, frame_idx=None, use_cache=False)
¶
Find the first occurrence of a matching labeled frame.
This function recreates labels.find_first(video, frame_idx, use_cache) from the original SLEAP codebase.
Matches on frames for the given video and/or frame index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
A Labels object containing labeled frames |
required | |
video
|
A Video instance that is associated with the labeled frames |
required | |
frame_idx
|
An integer specifying the frame index within the video (optional) |
None
|
|
use_cache
|
bool
|
Boolean that determines whether to use cache. If True, use the labels data cache, else loop through all labels to search. |
False
|
Returns:
| Type | Description |
|---|---|
|
First LabeledFrame that matches the criteria or None if none were found. |
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
find_last(labels, video, frame_idx=None)
¶
Find the last occurrence of a matching labeled frame.
This function recreates the functionality of labels.find_last(video, frame_idx) from the original SLEAP codebase.
Matches on frames for the given video and/or frame index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
A Labels object containing labeled frames |
required | |
video
|
A Video instance that is associated with the labeled frames |
required | |
frame_idx
|
An integer specifying the frame index within the video (optional) |
None
|
Returns:
| Type | Description |
|---|---|
|
Last LabeledFrame that matches the criteria or None if none were found. |
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
find_path_using_paths(filename, search_paths)
¶
Find a file in the given search paths.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
Union[str, List[str]]
|
The filename to search for. Can be a string path or a list of paths (for image sequences). |
required |
search_paths
|
List[str]
|
List of directories to search in. |
required |
Returns:
| Type | Description |
|---|---|
Union[str, List[str]]
|
The found path or the original filename if not found. For image sequences (lists), returns the original list unchanged. |
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
find_suggestion(labels, video, frame_idx)
¶
Return the suggestion for the given (video, frame_idx) or None.
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
find_track_occupancy(labels, video, track, frame_range=None)
¶
Get instances for a given video, track, and range of frames.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
video
|
Video
|
the |
required |
track
|
Union[Track, int]
|
the |
required |
frame_range
|
optional
|
If specified, only return instances on frames in range. If None, return all instances for given track. |
None
|
Returns:
| Type | Description |
|---|---|
List[Instance]
|
List of :class: |
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
fix_paths_with_saved_prefix(filenames, missing=None, path_prefix_conversions=None)
¶
Try to fix missing file paths using saved prefix conversions.
For image sequences (list of frame paths), ALL frames must exist at the new location for the prefix change to be applied.
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
get_instances_to_show(labeled_frame)
¶
Return a list of instances to show in GUI for this frame.
This function recreates the functionality of labeled_frame.instances_to_show from the original SLEAP codebase.
This list will not include any predicted instances for which there's a corresponding regular instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labeled_frame
|
A LabeledFrame object containing instances |
required |
Returns:
| Type | Description |
|---|---|
List
|
List of instances to show in GUI. |
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
get_labeled_frame_count(labels, video=None, filter='')
¶
Return count of frames matching video/filter.
This function recreates labels.get_labeled_frame_count(video, filter) from the original SLEAP codebase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
A Labels object containing labeled frames |
required | |
video
|
Optional Video object to filter by. If None, counts all videos |
None
|
|
filter
|
str
|
Filter string. Must be one of: "", "user", "predicted" - "": All labeled frames - "user": Only frames with user-labeled instances - "predicted": Only frames with predicted instances |
''
|
Returns:
| Type | Description |
|---|---|
int
|
Count of frames matching the criteria |
Raises:
| Type | Description |
|---|---|
ValueError
|
If filter is not one of the valid options |
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
get_next_suggestion(labels, video, frame_idx, seek_direction=1)
¶
Return a (video, frame_idx) tuple seeking from given frame.
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
get_predictions_on_user_frames(labels, video=None, unlinked_only=True)
¶
Find predictions on frames that have user instances.
This is useful for cleaning up predictions that were merged into frames that already have user labels, which causes both to be displayed in the GUI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
Labels
|
The Labels object to search. |
required |
video
|
Optional[Video]
|
Optional video to limit search. If None, searches all videos. |
None
|
unlinked_only
|
bool
|
If True (default), only return predictions not linked
via any user instance's |
True
|
Returns:
| Type | Description |
|---|---|
List[tuple]
|
List of (LabeledFrame, PredictedInstance) tuples for predictions that match the criteria. |
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
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 1486 1487 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 | |
get_template_instance_points(labels, skeleton)
¶
Get template instance points for a skeleton.
This function recreates labels.get_template_instance_points(skeleton) from the original SLEAP codebase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
Labels
|
A Labels object containing labeled frames and instances |
required |
skeleton
|
Skeleton
|
A Skeleton object to get template points for |
required |
Returns:
| Type | Description |
|---|---|
|
numpy array of template points for the skeleton |
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
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 | |
get_track_occupancy(labels, video)
¶
Get track occupancy information for a specific video.
This function recreates the functionality of labels.get_track_occupancy(video) from the original SLEAP codebase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
A Labels object containing labeled frames and tracks |
required | |
video
|
A Video object to get track occupancy for |
required |
Returns:
| Type | Description |
|---|---|
|
Dict mapping Track objects to their occupancy information (frame ranges) |
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
get_unused_predictions(labeled_frame)
¶
Return a list of "unused" PredictedInstance objects in frame.
This function recreates the functionality of labeled_frame.unused_predictions from the original SLEAP codebase.
This is all the PredictedInstance objects which do not have a corresponding Instance in the same track in frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labeled_frame
|
A LabeledFrame object containing instances |
required |
Returns:
| Type | Description |
|---|---|
List
|
List of unused PredictedInstance objects |
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
get_video_suggestions(labels, video, user_labeled=True)
¶
Get suggested frame indices for a specific video.
This function recreates the functionality of labels.get_video_suggestions(video) from the original SLEAP codebase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
A Labels object containing labeled frames and suggestions |
required | |
video
|
A Video object to get suggestions for |
required | |
user_labeled
|
bool
|
If True (the default), return frame indices for suggestions that already have user labels. If False, only suggestions with no user labeled instances will be returned. |
True
|
Returns:
| Type | Description |
|---|---|
List[int]
|
List of frame indices that are suggested for the specified video. |
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
iterate_labeled_frames(labels, video, from_frame_idx=-1, reverse=False)
¶
Return an iterator over lfs in a video with start pos (opt) and order control.
This function recreates Labels.frames() from the original SLEAP codebase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
A Labels object containing labeled frames |
required | |
video
|
A Video object that is associated with the project |
required | |
from_frame_idx
|
int
|
The frame index from which to start (default: -1 for beginning) |
-1
|
reverse
|
bool
|
Whether to iterate over frames in reverse order (default: False) |
False
|
Yields:
| Type | Description |
|---|---|
|
LabeledFrame objects for the specified video |
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
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 | |
labeled_frame_find(labeled_frame, track=None)
¶
Find instances in a labeled frame that match the given track.
This provides backward compatibility for the missing LabeledFrame.find() method. In sleap-io, we need to manually search through instances to find matching tracks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labeled_frame
|
LabeledFrame
|
LabeledFrame to search in |
required |
track
|
Track
|
Track to search for |
None
|
Returns:
| Type | Description |
|---|---|
|
List of instances that match the track, or empty list if none found |
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
labels_add_instance(labels, frame, instance)
¶
Add an instance to a labeled frame.
This provides backward compatibility for the missing add_instance() method. In sleap-io, we manually add instances to the frame's instances list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
Labels
|
Labels object (for consistency with legacy API, but not used) |
required |
frame
|
LabeledFrame
|
LabeledFrame to add instance to |
required |
instance
|
Instance to add to the frame |
required |
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
labels_add_video(labels, video)
¶
Add a video to the Labels object with duplicate prevention.
This is a thin wrapper around Labels.add_video() for backward compatibility with code that imports this function.
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
labels_all_instances(labels)
¶
Get all instances as a list for backward compatibility.
This provides backward compatibility for the missing all_instances attribute. In sleap-io, labels.instances is a generator, but legacy SLEAP expects a list-like object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
Labels
|
Labels object to get all instances from |
required |
Returns:
| Type | Description |
|---|---|
|
List of all Instance objects from all labeled frames |
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
labels_append_suggestions(labels, suggestions)
¶
Append suggestions to the Labels object.
This provides backward compatibility for the missing append_suggestions() method. In sleap-io, suggestions are stored as a list that can be extended directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
Labels
|
Labels object to append suggestions to |
required |
suggestions
|
List of SuggestionFrame objects to append |
required |
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
labels_clear_suggestions(labels)
¶
Clear all suggestions from labels for backward compatibility.
This provides backward compatibility for the missing clear_suggestions() method. In sleap-io, suggestions are stored as a list that can be cleared directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
Labels
|
Labels object to clear suggestions from |
required |
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
labels_copy(labels)
¶
Create a copy of the Labels object.
This provides backward compatibility for the missing copy() method. Uses copy.deepcopy() which should be handled gracefully by sleap-io.
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
labels_frames(labels, video=None)
¶
Get labeled frames, optionally filtered by video.
This provides backward compatibility for the missing frames() method.
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
labels_get(labels, video_and_frame_or_video, frame_idx=None, **kwargs)
¶
Get labeled frames for backward compatibility.
This provides backward compatibility for the missing get() method. Handles both tuple format (video, frame_idx) and separate video, frame_idx args.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
Labels
|
Labels object to search |
required |
video_and_frame_or_video
|
Either a (Video, frame_idx) tuple or a Video object |
required | |
frame_idx
|
Frame index (when first arg is Video) |
None
|
|
**kwargs
|
Additional arguments like use_cache (ignored for sleap-io compatibility) |
{}
|
Returns:
| Type | Description |
|---|---|
|
Single LabeledFrame if found, None otherwise (when frame_idx specified) List of LabeledFrame objects for video (when frame_idx not specified) |
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
labels_get_labels_attr(labels)
¶
Get labeled frames for backward compatibility.
Maps labels.labels to labels.labeled_frames
labels_get_nodes(labels)
¶
Get skeleton nodes for backward compatibility.
Maps labels.nodes to labels.skeleton.nodes or labels.skeletons[0].nodes
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
labels_get_suggestions(labels)
¶
Get all suggestions from labels for backward compatibility.
This provides backward compatibility for the missing get_suggestions() method. In sleap-io, suggestions are stored directly as an attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
Labels
|
Labels object to get suggestions from |
required |
Returns:
| Type | Description |
|---|---|
|
List of SuggestionFrame objects |
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
labels_load_file(filename, **kwargs)
¶
Load a Labels object from file.
This provides backward compatibility for the missing static load_file() method. Handles video_search parameter that sleap-io doesn't support.
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
labels_pop(labels, index)
¶
Remove and return a labeled frame at the given index.
This provides backward compatibility for the missing pop() method.
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
labels_remove_frame(labels, labeled_frame)
¶
Remove a single labeled frame from the Labels object.
This provides backward compatibility for a missing remove_frame() method. Uses the existing remove_frames() function.
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
load_labels_video_search(filename, video_search)
¶
Load labels file with video path search/resolution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
Path to the .slp labels file. |
required | |
video_search
|
Either a callback function, list of search paths, or a single search path string for finding missing video files. |
required |
Returns:
| Type | Description |
|---|---|
|
Labels object with videos resolved. |
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
make_video_callback(search_paths=None, use_gui=False, context=None)
¶
Adapter function for callback function to finding missing video.
The callback can be used while loading a saved project and allows the user to find videos which have been moved (or have paths from a different system).
The callback function returns True to signal "abort". Args: search_paths: If specified, this is a list of paths where we'll automatically try to find the missing videos. context: A dictionary containing a "changed_on_load" key with a boolean value. Used externally to determine if any filenames were updated. Returns: The callback function.
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 | |
merge_nodes(base_node, merge_node, labels, skeleton)
¶
Merge two nodes and update data accordingly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_node
|
str
|
Name of skeleton node that will remain after merging. |
required |
merge_node
|
str
|
Name of skeleton node that will be merged into the base node. |
required |
Notes
This method can be used to merge two nodes that might have been named differently but that should be associated with the same node.
This is useful, for example, when merging a different set of labels where a node was named differently. an
If the base_node is visible and has data, it will not be updated.
Otherwise, it will be updated with the data from the merge_node on the
same instance.
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
merge_nodes_data(predicted_instance, points_array, base_node, merge_node)
¶
Copy point data from one node to another.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_node
|
str
|
Name of node that will be merged into. |
required |
merge_node
|
str
|
Name of node that will be removed after merge. |
required |
Notes
This is used when merging skeleton nodes and should not be called directly.
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
remove_all_tracks(labels)
¶
Remove all tracks from the labels dataset and update all related instances.
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
remove_frames(labels, frames)
¶
Remove a list of frames from the labels dataset.
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
remove_instance(labels, instance, lf)
¶
Remove an instance from a labeled frame and update all related instances.
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
remove_track(labels, track)
¶
Remove a track from the labels dataset and update all related instances.
This function removes the specified track from the labels dataset by: 1. Setting the track to None for all instances that were using this track 2. Removing the track from the labels.tracks list
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
Labels
|
The Labels object containing the dataset to modify |
required |
track
|
Track
|
The Track object to remove from the dataset |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Labels |
The modified labels object (same object, modified in-place) |
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
remove_unused_tracks(labels)
¶
Remove all tracks from the labels dataset that are not used by any instances.
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
remove_video(labels, video)
¶
Remove a video from the labels dataset and update all related instances.
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
track_set_instance(labels, frame, instance, new_track)
¶
Set track on given instance, updating occupancy.
Source code in sleap/sleap_io_adaptors/lf_labels_utils.py
track_swap(labels, video, new_track, old_track, frame_range)
¶
Swap track assignment for instances in two tracks.
If you need to change the track to or from None, you'll need
to use :meth:track_set_instance for each specific
instance you want to modify.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
video
|
Video
|
The :class: |
required |
new_track
|
Track
|
A :class: |
required |
old_track
|
Optional[Track]
|
The other :class: |
required |
frame_range
|
tuple
|
Tuple of (start, end) frame indexes. If you want to swap tracks on a single frame, use (frame index, frame index + 1). |
required |