Pybehave: a hardware agnostic, Python-based framework for controlling behavioral neuroscience experiments

.


Statement of need
Operant animal behavior training and monitoring is fundamental to scientific inquiry across fields (Krakauer et al., 2017).In many cases, a behavior of relevance, or its neural substrate, is best studied through a controlled laboratory task.These tasks require tight integration of the hardware components with which animals interact (IR beams, levers, lights, food dispensers, etc.) and the overarching software that coordinates these components to elicit desired behaviors.There are a plethora of options for systems to facilitate behavioral tasks, from commercial solutions (Panlab, Lafayette Instruments, Med Associates) to open-source packages (Akam et al., 2022;Dastin-van Rijn et al., 2023;Hwang et al., 2019) enabling a large variety of behavioral paradigms.Many of these systems are designed for the same behavioral paradigms with only slight differences in hardware, sensory modalities, or geometry.However, while the actual mechanics of these paradigms remain relatively similar, different solutions will often rely on vastly different software interfaces (Cardinal & Aitken, 2010;Lopes et al., 2015).Especially with commercial systems, behavioral tasks are often programmed in proprietary formats.This approach significantly raises the barrier to entry, leads to outdated software, and prevents sharing of tasks across labs.
Research in human behavior does not suffer from many of the aforementioned issues.Human behavioral tasks are generally run through a graphical interface implemented in a standard programming language like Python (Peirce et al., 2019), Javascript (Leeuw, 2015), or Matlab (Brainard, 1997).These tasks are readily compatible with most machines and are frequently shared between labs and used across multiple studies (Provenza et al., 2021).Protocols, data, and task code can be easily included in a manuscript and accessed and modified by future researchers.However, unlike experiments in animal behavior, human experiments rarely require hardware beyond a monitor and standard input device (keyboard/mouse).Instead, most animal platforms, even from open source developers, restrict their software to certain types of hardware (Akam et al., 2022;Hwang et al., 2019).For example, pycontrol is only compatible with their companion microcontroller and input devices and MonkeyLogic can only communicate with DAQs manufactured by National Instruments.To address these limitations, we developed pybehave as a framework for abstracting standard hardware components to enable an implementation-independent format for developing and running behavioral tasks.

Benefits
Pybehave is a complete framework for building and running behavioral neuroscience experiments.It offers the following benefits: (1) hardware independence; (2) a flexible, programmatic system for developing tasks; (3) a highly extensible graphical interface for configuring and executing tasks; (4) options for task-specific visualization; simultaneous control of multiple experiments; (6) options for locally configuring task variables and protocols; and (5) an extensive developer API, which allows users to extend the platform with tie-ins for custom hardware, event logging, or software connections.

Software Design Principles
To ensure flexibility while maintaining low-latency, pybehave is optimized through a combination of multiprocessing and multithreading along with separation of its features (events, hardware sources, tasks, etc.) into a modular software architecture.Additionally, pybehave uses two different GUI frameworks (QT and pygame) for user interfacing and task visualization/stimulus display respectively (Figure 1).The workstation process handles the interface and task GUIs.When Tasks are added from the workstation, they are initialized in the task process.Each Source with a connection to an external hardware or software system communicates with their pybehave equivalent in the Task process.All events sent between processes are mediated via inter-process communication over Pipes.

Tutorials and ongoing usage
A variety of tutorials are included in the repository aimed at all levels of usage, from technicians running tasks or analyzing behavioral data to developers aiming to build new tasks or integrate additional hardware.Pybehave has already been applied to implement a variety of behavioral tasks which have been included in a separate repository for users to pull from directly or modify.These tasks are being run in a number of ongoing studies spanning standard operant conditioning (Dastin-van Rijn et al., 2023;Mensinger et al., 2023), evoked responses (Sachse et al., 2023), and video assays.

Figure 1 :
Figure1: Framework diagram showing the information exchange between the pybehave threads and processes.The workstation process handles the interface and task GUIs.When Tasks are added from the workstation, they are initialized in the task process.Each Source with a connection to an external hardware or software system communicates with their pybehave equivalent in the Task process.All events sent between processes are mediated via inter-process communication over Pipes.