This notebook shows how to set up a new project, train a keypoint-MoSeq model and visualize the resulting syllables.

Note

To prevent errors during the calibration step below, make sure to launch jupyter from the keypoint_moseq environment.

Project setup

Create a new project directory with a keypoint-MoSeq config.yml file.

The first cell below imports keypoint-MoSeq and checks the installation. It prints the installed versions of keypoint-MoSeq and JAX and the devices JAX can use, and raises an error that says how to fix the problem if the installed keypoint-MoSeq is a different version than this notebook is written for, if keypoint-MoSeq was upgraded since the kernel started, or if the GPU setup has a known problem.

import keypoint_moseq as kpms
import matplotlib.pyplot as plt

project_dir = "demo_project"
config = lambda: kpms.load_config(project_dir)

kpms.check_installation(notebook_version="0.8")
Loading BokehJS ...

Setup from DeepLabCut

dlc_config = "dlc_project/config.yaml"
kpms.setup_project(project_dir, deeplabcut_config=dlc_config)

Setup from SLEAP

sleap_file = "XXX"  # any .slp or .h5 file with predictions for a single video
kpms.setup_project(project_dir, sleap_file=sleap_file)

Custom setup

bodyparts = [
    "tail",
    "spine4",
    "spine3",
    "spine2",
    "spine1",
    "head",
    "nose",
    "right ear",
    "left ear",
]

skeleton = [
    ["tail", "spine4"],
    ["spine4", "spine3"],
    ["spine3", "spine2"],
    ["spine2", "spine1"],
    ["spine1", "head"],
    ["nose", "head"],
    ["left ear", "head"],
    ["right ear", "head"],
]

video_dir = "path/to/videos/"

kpms.setup_project(project_dir, video_dir=video_dir, bodyparts=bodyparts, skeleton=skeleton)

Edit the config file

The config can be edited in a text editor or using the function kpms.update_config, as shown below. In general, the following parameters should be specified for each project:

  • bodyparts (name of each keypoint; automatically imported from SLEAP/DeepLabCut)

  • use_bodyparts (subset of bodyparts to use for modeling, set to all bodyparts by default; for mice we recommend excluding the tail)

  • anterior_bodyparts and posterior_bodyparts (used for rotational alignment)

  • video_dir (directory with videos of each experiment)

  • fps (frames per second of the input videos)

Edit the config as follows for the example DeepLabCut dataset:

kpms.update_config(
    project_dir,
    video_dir="dlc_project/videos/",
    anterior_bodyparts=["nose"],
    posterior_bodyparts=["spine4"],
    use_bodyparts=["spine4", "spine3", "spine2", "spine1", "head", "nose", "right ear", "left ear"],
    fps=30,
)

Load data

The code below shows how to load keypoint detections from DeepLabCut. To load other formats, replace 'deeplabcut' in the example with one of 'sleap', 'anipose', 'sleap-anipose', 'nwb'. For other formats, see the FAQ.

# load data (e.g. from DeepLabCut)
keypoint_data_path = "dlc_project/videos/"  # can be a file, a directory, or a list of files
coordinates, confidences, bodyparts = kpms.load_keypoints(keypoint_data_path, "deeplabcut")

Check videos

Calibration and grid movies read frames from the videos in video_dir. The cell below matches each recording to its video and prints the pairing. Look over the printed table to check that each recording is paired with the video you expect. It raises an error if a video is missing or unreadable, or if a recording’s keypoints need frames past the end of its video, and it warns if keypoints fall outside the video frame, as they do when they were tracked on a resized or cropped copy of the video. It also warns when one video is matched to several recordings. That is expected when several animals were tracked in one video, in which case set shared_videos to True in the config. Otherwise, some videos may be missing. If you trimmed or downsampled the keypoints, pass video_frame_indexes here as you would for calibration and grid movies. Skip this step if you don’t have videos for your recordings.

kpms.check_videos(coordinates, print_table=True, **config())

Remove outlier keypoints

Removing large outliers can improve the robustness of model fitting. A common type of outlier is a keypoint which briefly moves very far away from the animal as the result of a tracking error. The following cell classifies keypoints as outliers based on their distance to the animal’s medoid. The outlier keypoints are then interpolated and their confidences are set to 0 so that they are interpolated for modeling as well.

  • Use outlier_scale_factor to set the upper distance threshold (higher values -> higher threshold, fewer points removed)

  • Plots showing distance to medoid before and after outlier interpolation are saved to {project_dir}/QA/plots/ for 10 randomly chosen recordings. The commented-out lines show how to plot more recordings, every recording, or specific ones.

  • In a notebook, the plots are saved in the background while you continue, and a progress bar under the cell shows when they are done.

  • This cell replaces coordinates and confidences with the cleaned data, so to run it again (for example with a different outlier_scale_factor, or to plot other recordings), first re-run the load_keypoints cell above. Plots that already exist are not redrawn unless you set overwrite=True in outlier_removal.

kpms.update_config(project_dir, outlier_scale_factor=6.0)

coordinates, confidences = kpms.outlier_removal(
    coordinates,
    confidences,
    project_dir,
    overwrite=False,
    # plot_recordings=50,  # QA plots for 50 random recordings instead of 10
    # plot_recordings=None,  # QA plots for every recording
    # plot_recordings=["recording_name"],  # QA plots for specific recordings
    **config()
)

Format data for modeling

data, metadata = kpms.format_data(coordinates, confidences, **config())

Calibration

The purpose of calibration is to learn the relationship between keypoint errors and confidence scores. The results are stored using the slope and intercept parameters in the config.

  • Run the cell below. A widget should appear with a video frame and the name of a bodypart. A yellow marker denotes the detected location of the bodypart.

  • Annotate each frame with the correct location of the labeled bodypart

    • Click on the image at the correct location - an “X” should appear.

    • Use the prev/next buttons to annotate additional frames.

    • Click and drag the bottom-right shaded corner of the widget to adjust image size.

    • Use the toolbar to the left of the figure to pan and zoom.

  • We suggest annotating at least 50 frames.

  • Annotations will be automatically saved once you’ve completed at least 20 annotations. Each new annotation after that will trigger an auto-save of all your work. The message at the top of the widget will indicate when your annotations are being saved.

%matplotlib widget
kpms.noise_calibration(project_dir, coordinates, confidences, **config())
Loading sample frames: 100%|████████████| 90/90 [00:06<00:00, 14.25it/s]
WARNING:param.OverlayPlot01356: Tool of type 'pan' could not be found and could not be activated by default.
WARNING:param.OverlayPlot01356:Tool of type 'pan' could not be found and could not be activated by default.
WARNING:param.OverlayPlot01356: Tool of type 'wheel_zoom' could not be found and could not be activated by default.
WARNING:param.OverlayPlot01356:Tool of type 'wheel_zoom' could not be found and could not be activated by default.