Skip to content

Instantly share code, notes, and snippets.

@roomrys
roomrys / sleap_multiview_association.py
Created October 8, 2024 15:52
SLEAP: Multiview Association via Pairs of Views and Fundamental Matrix
"""This module implements cycle consistent matching using pairs of views."""
from __future__ import annotations
from typing import Generator
import cv2
import matplotlib.patches as patches
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
@roomrys
roomrys / sleap_slp_summary_csv.py
Created October 4, 2024 21:00
SLEAP: Get a summary CSV of all slp files in a directory
"""Outline number of labeled frames, recording sessions, and video paths for all SLPs in directory.
This script recursively finds SLP files in a directory and outputs a CSV outlining how many labeled frames, recording
sessions, and videos there are in each slp project within a directory.
"""
import pathlib
import pandas as pd
@roomrys
roomrys / sio_find_timestamps_in_roi.py
Created September 25, 2024 18:59
SLEAP-IO: Find timestamps for when an instances is in a ROI.
"""Script to find when an annotation is in a ROI of a given image.
# Installation
```bash
conda create -c conda-forge sleap-io shapely -n sio
```
# Usage
Make changes to the __main__ block at the bottom of the file. Then activate the
environment and run the script.
@roomrys
roomrys / sio_rfid_proofreading_v0.py
Last active September 25, 2024 18:57
SLEAP-IO: Auto-proofread with RFID (v0)
import numpy as np
import pandas as pd
import shapely
import sleap_io as sio
from shapely import Polygon
from sleap_io import Instance, Labels, LabeledFrame, PredictedInstance, Track
def load_rfid_rois(ds_rfid: str):
"""Load a dictionary of RFID ROI `Polygons`.
@roomrys
roomrys / sleap_create_labels_from_videos_directory.py
Last active September 20, 2024 21:41
SLEAP: Create a Labels project containing videos in a given directory
"""This script creates a Labels object from a list of mp4 files in a directory."""
from __future__ import annotations # Import future annotations
from pathlib import Path
from sleap import Labels, Video
def find_videos_in_directory(directory: str, video_ext: str = None) -> list[str]:
"""Find all video files in a directory.
@roomrys
roomrys / sleap_export_3d_poses.py
Last active October 17, 2024 20:47
Export 3D poses from RecordingSessions within SLEAP.
import h5py
import logging
import os
import numpy as np
from sleap_anipose import triangulate
import sleap
from sleap.io.cameras import RecordingSession, FrameGroup
from sleap.gui.commands import TriangulateSession
@roomrys
roomrys / pygfx_plot_3d_poses.py
Last active October 17, 2024 21:48
Use pygfx to plot 3D poses
from __future__ import annotations
import logging
import os
from datetime import datetime
from pathlib import Path
# TODO: Install these dependencies (and PySide6)
import cv2
import h5py
@roomrys
roomrys / sleap_midpoint_track_assignment.py
Created August 29, 2024 00:45
SLEAP: Assign `Track` based on a left/right threshold
import sleap
from sleap import Track
from sleap.gui.app import main as sleap_label
def main(ds: str, ds_output: str, midpoint:float = 0.5):
"""Reassigns tracks based on x-threshold and saves to `ds_output`.
Note: This function assumes that the dataset has only two animals. No restrictions
are in place to ensure that two animals in the same frame are assigned different
tracks. This is a simple example to demonstrate how to reassign tracks based on
@roomrys
roomrys / sleap_skeleton_convert_dict_state_to_tuple_state.py
Created August 27, 2024 21:32
SLEAP: Convert `dict` state to `tuple` state skeleton.json
"""Converts the "py/state" entries in a JSON file to contain "py/tuple" entries.
SLEAP uses jsonpickle to encode/decode `Skeleton` objects to/from JSON. When a Python
object is encoded to JSON, the "py/state" key is used to store the object's state.
jsonpickle relies on the object's `__getstate__` method to determine the state to store.
attr creates a `__getstate__` method for the object. However, in `attr>=22.2.0`, the
formatting changes s.t. `__getstate__` returns a dictionary of the object's attributes
instead of a tuple. This causes problems when decoding the JSON back to a Python object
if the system has an older version of `attr<22.2.0`.
@roomrys
roomrys / gcloud-get-sku-pricing.py
Last active August 13, 2024 21:25
GCloud: Get Pricing Info of SKU
import google.auth
# TODO: pip install pandas
import pandas as pd
# TODO: pip install google-cloud-billing
from google.cloud import billing_v1
def list_services():