Skip to main content

Mouse wiskers body kinematics and behaviour

Project description

W2T Body Kinematics Pipeline (w2t-bkin)

A modular, reproducible Python pipeline for processing multi-camera rodent behavior recordings. It integrates synchronization, pose estimation (DeepLabCut/SLEAP), facial metrics, and behavioral events into standardized NWB (Neurodata Without Borders) datasets.

Key Features

  • NWB-First Architecture: Produces NWB-native data structures directly, eliminating intermediate conversion layers.
  • Bpod Integration: Parses Bpod .mat files and converts them to ndx-structured-behavior format.
  • Pose Estimation: Imports and harmonizes data from DeepLabCut and SLEAP into ndx-pose.
  • Synchronization: Robust alignment of behavioral data and video frames to a common timebase using TTL pulses.
  • Modular Design: Distinct modules for behavior, pose, sync, and session management.

Installation

The project requires Python ~3.10.

  1. Install ndx-structured-behavior (currently required from source):

    git clone https://github.com/rly/ndx-structured-behavior.git
    pip install -U ./ndx-structured-behavior
    
  2. Install w2t-bkin:

    pip install w2t-bkin
    

Configuration

The pipeline uses TOML for configuration. The main config file (e.g., configs/standard.toml) defines:

  • Paths: raw_root, intermediate_root, output_root for data management.
  • Bpod Sync: Maps trial types to specific sync signals and TTL channels (e.g., W2T_Audio -> ttl_cue).
  • Logging: Sets logging levels and formats.

Session-specific metadata is handled via metadata.toml files located in each session directory.

Quick Start

Here is a core pipeline example:

from pathlib import Path
from w2t_bkin import behavior, bpod, config, session, sync, ttl

# 1. Load Configuration
settings = config.load_config("configs/standard.toml")
rawdata_dir = settings.paths.raw_root / "Session-000001"

# 2. Initialize NWB File
nwbfile = session.create_nwb_file(rawdata_dir / "metadata.toml")

# 3. Import TTL Signals
ttl_patterns = {
    "ttl_camera": "TTLs/*.xa_7_0*.txt",
    "ttl_cue": "TTLs/*.xia_3_0*.txt",
}
ttl_pulses = ttl.get_ttl_pulses(rawdata_dir, ttl_patterns)

# 4. Parse Bpod Data
bpod_data = bpod.parse_bpod(rawdata_dir, pattern="Bpod/*.mat", order="name_asc")

# 5. Synchronize Bpod to TTL
trial_offsets, warnings = sync.align_bpod_trials_to_ttl(
    trial_type_configs=settings.bpod.sync.trial_types,
    bpod_data=bpod_data,
    ttl_pulses=ttl_pulses,
)

# 6. Extract Behavioral Data (NWB objects)
task, recording, trials = behavior.extract_behavioral_data(bpod_data, trial_offsets)

# 7. Create TTL Events Table
ttl_table = ttl.extract_ttl_table(ttl_pulses)

# 8. Assemble NWB
nwbfile.add_acquisition(ttl_table)
nwbfile.add_lab_meta_data(task)
nwbfile.add_acquisition(recording.states)
nwbfile.add_acquisition(recording.events)
nwbfile.add_acquisition(recording.actions)
nwbfile.trials = trials

Module Overview

Module Description
w2t_bkin.behavior Converts Bpod data into ndx-structured-behavior classes (StatesTable, EventsTable, TrialsTable).
w2t_bkin.bpod Low-level parsing and validation of Bpod .mat files.
w2t_bkin.pose Imports pose estimation data (DLC/SLEAP) and builds ndx-pose objects (PoseEstimation, Skeleton).
w2t_bkin.sync Handles timebase alignment, jitter calculation, and synchronization of video/behavior to TTLs.
w2t_bkin.ttl Loads hardware TTL pulse timestamps and creates ndx-events tables.
w2t_bkin.session Loads pynwb metadata and assembles the root NWBFile.
w2t_bkin.pipeline High-level orchestration of the entire workflow.

CLI Utilities

The scripts/ directory contains useful utilities:

  • mat2json.py: Converts MATLAB .mat files to JSON, handling nested structures and arrays.
  • pose2ttl.py: Generates mock TTL signals from DeepLabCut pose data (useful for testing or when hardware sync fails).
  • trials2df.py: Converts NWB TrialsTable and TaskRecording objects into a flat pandas DataFrame for analysis.

License

See LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

w2t_bkin-0.0.6.tar.gz (93.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

w2t_bkin-0.0.6-py3-none-any.whl (107.8 kB view details)

Uploaded Python 3

File details

Details for the file w2t_bkin-0.0.6.tar.gz.

File metadata

  • Download URL: w2t_bkin-0.0.6.tar.gz
  • Upload date:
  • Size: 93.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for w2t_bkin-0.0.6.tar.gz
Algorithm Hash digest
SHA256 c688d3640e1c364e2bb07d90433133af2079e3879913e7c1255dfb81167b0b75
MD5 794f756b6dd8b65034d6ba6eb2528ae8
BLAKE2b-256 06e3e725284ec0764d1e65bf58133a377e03a732cd4f52ae5c89b54a1ae338c6

See more details on using hashes here.

File details

Details for the file w2t_bkin-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: w2t_bkin-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 107.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for w2t_bkin-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 1bb9614f883ff7eacf5a14b304a81aeacfd44a46cfd8f6ef330f2e887ef516a3
MD5 6d3a6a21c4a0e50c8ab4f8db19eaf2ad
BLAKE2b-256 865413c03a1098afd44eaa1e5fb73b18f086ab0d78bb91ce57305422db51c58c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page