RTHybrid: A Standardized and Open-Source Real-Time Software Model Library for Experimental Neuroscience

Closed-loop technologies provide novel ways of online observation, control and bidirectional interaction with the nervous system, which help to study complex non-linear and partially observable neural dynamics. These protocols are often difficult to implement due to the temporal precision required when interacting with biological components, which in many cases can only be achieved using real-time technology. In this paper we introduce RTHybrid (www.github.com/GNB-UAM/RTHybrid), a free and open-source software that includes a neuron and synapse model library to build hybrid circuits with living neurons in a wide variety of experimental contexts. In an effort to encourage the standardization of real-time software technology in neuroscience research, we compared different open-source real-time operating system patches, RTAI, Xenomai 3 and Preempt-RT, according to their performance and usability. RTHybrid has been developed to run over Linux operating systems supporting both Xenomai 3 and Preempt-RT real-time patches, and thus allowing an easy implementation in any laboratory. We report a set of validation tests and latency benchmarks for the construction of hybrid circuits using this library. With this work we want to promote the dissemination of standardized, user-friendly and open-source software tools developed for open- and closed-loop experimental neuroscience.


INTRODUCTION
The study of neural systems dynamics is hindered by various factors. The first one is their intrinsic non-linearity, since they process information in several interacting spatial and temporal scales and are affected by multiple transient adaptation and learning mechanisms. Also, from all magnitudes involved in these dynamics, just a few can be accessed simultaneously, making the system partially observable. The third factor is related to the use of the traditional stimulus-response paradigm in most experimental neuroscience research, which only allows to record the behavior of the system under different stimuli and then to analyze the collected data offline. Thus, highly complex non-stationary neural activity, which has influence from the context and previous events' feedback, can not be completely assessed. Closed-loop techniques provide an efficient way to overcome such difficulties by interacting online with the system, producing precise stimulus according to the recorded information and presenting valuable insights on transient neural processes. This paradigm allows more flexibility in the experiment and favors its automation, as well as the control of neural dynamics (Chamorro et al., 2012;Potter et al., 2014;Roth et al., 2014;Varona et al., 2016).
While closed-loop technology allows researchers to conduct online observation, control and goal-driven interaction with neural elements, it also presents some drawbacks in its implementation. Some of these difficulties are related to the complexity of the associated experimental design, but also to the accomplishment of precise temporal restrictions, in the scale of milliseconds or lower, which are often required during data acquisition and stimulation in biological experiments (Christini et al., 1999;Muñiz et al., 2005Muñiz et al., , 2008Muñiz et al., , 2009. The capacity of a system to perform periodic tasks and respond to asynchronous external events in an strict time slot (neither sooner nor later) is known as real-time (Furht et al., 1991). Therefore, to ensure the compliance of the previously mentioned temporal margins in an experimental setup, real-time technology is needed. It is also important to mention that the term "real-time" may as well be used in neuroscience literature referred to online recording, feedback or control (Siegle et al., 2017). In this paper we always refer to strict temporal precision in the millisecond range.
Electronic components easily fulfill the speed and precision requirements needed for most real-time scenarios, so hardwarebased implementations are one possible solution (Robinson and Kawai, 1993;Le Masson et al., 1995;Broccard et al., 2017). Current development of inexpensive microcontrollers with large computational power allows efficient implementations (Desai et al., 2017). On the other hand, software-based solutions are usually more programmable and manageable, offering maximum flexibility and a wide range of user-friendly frameworks. Moreover, nowadays personal computers have enough hardware capacity to comply with standard real-time restrictions and benefit from large memory for complex closedloop protocol implementations. However, modern general purpose operating systems (GPOS), such as Windows, MacOS or GNU/Linux, are multitask environments with internal schedulers, which assign computer resources to different running tasks following specific policies (Stallings, 2012). These schedulers can not be controlled by the users, hence it can not be ensured that a given task will run without interruptions and, therefore, real-time can not be assured. In order to run protocols accomplishing a set of established temporal boundaries with software-based real-time, another system framework, known as real-time operating system (RTOS), is needed.
A highly relevant example of closed-loop interactions can be found in hybrid circuits, which are networks built by connecting model neurons and synapses to living cells. They are a powerful tool to explore and characterize neural system dynamics, as well as a means to assess the role of specific circuit components (e.g., see Yarom, 1991;Pinto et al., 2000;Szücs et al., 2000;Varona et al., 2001;Le Masson et al., 2002;Nowotny et al., 2003;Oprisan et al., 2004;Arsiero et al., 2007;Chamorro et al., 2009;Grashow et al., 2010;Brochini et al., 2011;Kispersky et al., 2011;Wang et al., 2012;Thounaojam et al., 2014;Hooper et al., 2015;Norman et al., 2016;Broccard et al., 2017). The most common paradigm to build hybrid circuits consists in using dynamic-clamp to read the membrane potential of a cell and, after computing a model using this voltage, inject the resulting current into the same or into a different cell (Robinson and Kawai, 1993;Sharp et al., 1993;Prinz et al., 2004;Destexhe and Bal, 2009;Nowotny and Varona, 2014). Neuron models range from simple mathematical approximations to more complex conductance-based implementations, which reproduce biophysical behaviors with high accuracy. The same thing occurs for synapse models, which can cover from simple gap junctions using Ohm's law to chemical connections described by non-linear differential equations (Torres and Varona, 2012).
Due to the lack of flexibility in hardware real-time solutions, as well as RTOS heterogeneity and intrinsic difficulties in their use, many neuroscience researchers overlook this technology when designing closed-loop experiments. In this paper we introduce RTHybrid, a novel, multiplatform, real-time software neuron and synapse model library to build hybrid circuits. With this tool we aim to promote the use of standardized and user-friendly real-time software technology, available in different platforms, to favor the implementation of closed-loop experimentation in neuroscience research. We provide validation examples of this tool in the context of hybrid circuit implementations using dynamic-clamp, including detailed analysis and benchmarking of temporal precision in different RTOS.

Real-Time Software
Real-time performance is often wrongly considered as a matter of speed, which is of course important, when it actually relies on temporal precision: actions must be delivered within a preestablished interval, neither sooner nor later. Many neurons follow precisely a similar behavioral pattern: slow activity (less than 1 kHz) but precise subcellular sequential dynamics or spiking coding.
When a computer performs a given task, there is always some latency between the moment when this task is expected to be accomplished and when it is actually done, as well as some jitter of these latency values, due to the performance of the operating system scheduler. Moreover, not all tasks are equally sensitive to high latency values or data loss, hence real-time software can be classified in two types: soft real-time, when some deadlines can be missed without performance degradation as long as some threshold is not exceeded (for example, an online music streaming service can lose some data packages and users will not notice it), and hard real-time, when all deadlines must be met or the system fails critically (computers controlling a nuclear plant or a satellite, for example) (Shin and Ramanathan, 1994). In this paper, the term real-time always refers to hard real-time.
Despite how differently a RTOS can be designed, the functioning of all of them rely on two elements: their scheduling algorithms, which are usually preemptive, meaning that they are able to interrupt a running process without its permission; and how they manage hardware interruptions (Abbott, 2006). Some of them are implemented from scratch, while others are based in existing GPOS' kernels (Hambarde et al., 2014). Among the later, dual-kernel (a real-time microkernel is used along the standard one) and single-kernel solutions (the standard kernel is modified to support real-time) are common (Yaghmour, 2003;Dietrich and Walker, 2005).
There exist several commercial RTOS implementations, mainly designed for embedded systems, such as QNX Neutrino, VxWorks and Windows Compact Embedded, among others. Other proprietary real-time software solutions are MATLAB's toolbox xPC and National Instruments' LabVIEW Real-Time, which can achieve hard real-time precision if external hardware is used for the computation but otherwise are soft real-time, and Simulink Desktop Real-Time, which provides a hard real-time kernel (up to 1kHz sample rate) for executing Simulink models running Windows or Mac OS X. The main drawback of these tools is their high economic cost, which may be unaffordable for many research groups and laboratories. There are also opensource and free solutions to obtain an RTOS from Linux, an open-source GPOS, which avoid the stated inconveniences of commercial options and provide similar usability, or even better, in terms of performance (Aroca and Caurin, 2009;Hambarde et al., 2014). In this paper, three of these patches were studied, and their performance and characteristics were compared in relation to the task of building hybrid circuits with living and model neurons: RTAI, Xenomai and Preempt-RT.
RTAI (Real-Time Application Interface) (Mantegazza et al., 2000) was first developed in 1996, becoming one of the first and most widely used open-source real-time environments. It is based in a dual-kernel approach, using a real-time microkernel with a preemptive scheduler, which controls the interrupt requests (IRQ) and treats the standard Linux kernel as a low priority task. Another dual-kernel solution is Xenomai (Gerum, 2004), a project which was part of the RTAI project until 2005, when it become a fully independent tool. Other RTOS follow a single kernel approach, meaning that no auxiliary microkernel is used and that the standard one is modified instead to work in realtime. All changes made to Linux standard kernel for this purpose throughout the past decade are included in Preempt-RT (Dietrich and Walker, 2005).

Benchmarking Tests
Performance on the three RTOS described in the previous section, RTAI, Xenomai and Preempt-RT, was measured and compared among them and also to a GPOS with no real-time capabilities. Specifications of the computers used for these tests can be found in Table 1. Most modern computers currently have multicore processors, i.e., one component with several independent processing units. Linux also allows to isolate specific cores, so the scheduler will not assign them any task, and to manually bound a specific task to this empty core. We have also analyzed how this core isolation affected the performance in both real-time and non real-time implementations.
The benchmarking procedure consisted in a latency test, measuring the time difference between when an action was expected and when it really happened (see Figure 1). The benchmarking program 1 consisted in a periodic loop with a frequency of 20 kHz that sent a digital 0 or 1 to a digital acquisition (DAQ) device alternately on each iteration, thus producing a 100 µs square-wave signal. After sending the corresponding value the program slept until the next interval arrived: the time interval between the real and the expected awaking time was the measured latency. An Agilent MSO7104A oscilloscope was used as an external temporal reference and stress 2 , a workload generator software, was utilized to create a worst case scenario, with all processor cores and the file Input/Output system running at full capacity. To send the signal to the oscilloscope a National Instruments PCI-6251 board and a BNC-2090A DAQ device were used.

RTHybrid Validation Tests
In order to validate the proper functioning of RTHybrid, it has been tested in a real experimental environment. Hybrid circuits were built by bidirectionally connecting a neuron model simulated by the software and a living neuron, using chemical graded model synapses. For these tests, a computer with a 4core Intel Core i7-6700 3.40 GHz processor and 16 GB RAM memory, as well as a National Instruments PCI-6251 board with a NI BNC-2090A DAQ device, were used. Measures of the time taken by each task in the interaction cycle were also conducted to evaluate their contribution to minimum and maximum computational cost for each of the real-time platforms analyzed.
The membrane potential of the biological component of the experiment was recorded using in vitro electrophysiology in neurons of the pyloric central pattern generator (CPG) of adult Carcinus maenas, bought in a local fish store and kept in artificial sea water. Before the dissection, the crab was anesthetized by introducing it in the freezer for 20/30 min. Note that the computer used for non-real-time, Preempt-RT and Xenomai 3 tests was the same (we will call it Computer 1 hereafter) and the one for RTAI was different (Computer 2).
FIGURE 1 | Representation of a typical real-time process, consisting on a succession of fixed and equal duration time intervals that are repeated on each iteration of a periodic loop. For any of these intervals, T1 is the time when it should start, but, due to the system capacity or to its resource management algorithms, the beginning can be delayed until T2. The difference between the real start time (T2) and the expected one (T1) is known as latency. After T2, the process computes its iterative task, which finishes at T3. If T3 happens before the next interval start time, the process waits until that moment. On the other hand, if T3 occurs later than the expected start time of the next interval, as a result of a high latency value, a long computational time or both, it would cause a failure on the real-time system. Real-time tasks can be classified according to their tolerance of such events: soft real-time means that some deadlines can be missed under a certain limit, while hard real-time does not tolerate any failure.
The stomatogastric ganglion, dissected following the standard procedure, was attached to a Petri with Sylgard cold saline dissolution (13-15 • C kept by a microcontroller and always perfused) using pins. The saline had the following composition (in mM): 433 NaCl, 12 KCl, 12 CaCl 2 · 2 H 2 O, 20 MgCl 2 · 6 H 2 O, 10 HEPES, adjusted to pH 7.60 with 4M NaOH. Neurons were identified after desheathing the ganglion by their membrane potential waveforms and their corresponding spike times in nerves. Intracellular recordings were performed using 3 M KCl filled microelectrodes (50 M ) and a DC amplifier (ELC-03M, NPI Electronic, Hauptstrasse, Tamm, Germany). For details on the preparation see .

Computers Internal Clock Validation
Tests were conducted to certify that the internal clocks of the systems specified in Table 1 were capable of working with the required microsecond precision. These consisted in three 10 second tests on each platform, generating a squarewave signal of period 100µs, which was recorded with an external oscilloscope. When compared to the signal period measured by the computers, the oscilloscope registered a ±10µs inaccuracy in the precision of the computers' clocks (see Table 2). This is an acceptable margin for our purposes with sampling rates for the hybrid circuit electrophysiology ranging from 10 to 20 kHz.

RTOS Usability Comparison
RTAI, Xenomai and Preempt-RT were studied and compared in terms of installation requirements, usability and userfriendliness. This analysis is summarized in Table 3.

RTAI
Installation requires to patch a vanilla Linux kernel with the RTAI patch and then compile it, which is a long and tricky operation, even for experimented users. The last version of the official installation guide dates from 2008 (Monteiro, 2008). Utilization of the real-time functions provided by the platform is done through its own API, which is very powerful and complete, but documentation and examples are scarce. The safest way to achieve real-time is implementing the programs as kernel space modules, which carries many impediments. User space realtime can be reached using the LXRT library, although its use is discouraged for non-senior RTAI programmers (Racciu and Mantegazza, 2006). Currently, this tool is still maintained, but not regularly: version 4 last maintenance was in 2013 and in May 2017 version 5 was released, followed by the 5.1 patch in February 2018.

Xenomai
Patching and compiling a Linux vanilla kernel is the only way of installing Xenomai and its developers provide an up-todate guide. Since Xenomai's main purpose is to offer an opensource alternative to proprietary RTOS, it includes different APIs, intended to emulate other environments and libraries, such as VxWorks, pSOS+ and even POSIX. All of them, as well as their own API, are accessible from user space. Complete and updated documentation is available for all APIs. Although A 100µs signal, generated by the computers, was recorded with each computer and an external oscilloscope. Minimum, maximum, mean and standard deviation values of the recorded signal period are displayed at the table. When compared to the signal period measured by the computers, the oscilloscope registered a ±10µs inaccuracy in the precision of the computers clocks.

Preempt-RT
Similarly to the dual-kernel implementations, the typical way to install Preempt-RT is by patching and compiling a vanilla kernel, following the instructions that can be found at the official website, these being significantly simpler than the ones for RTAI and Xenomai. An alternative possibility for Debian distributions is to install it from its repositories as any other package. Despite the real-time patch, the system is still a normal Linux, so the standard POSIX library can be used and all its documentation is valid. In 2015 the project was transferred to The Linux Foundation, becoming the "official" Linux real-time solution.

RTOS Benchmark Analysis
RTAI, Xenomai and Preempt-RT were tested using the method described in 2.2.1. Each trial consisted in a 5 min run of the test program under stress, with a frequency of 20kHz, and was repeated twice on every platform. We measured the maximum, minimum and mean latency values, as well as the jitter, and the results can be seen in Figure 2. Distribution of latency values during these tests is shown in Figure 3. RTAI obtained the best performance scores, getting 3.65µs as maximum latency, even running on an older machine. Xenomai was not far from this performance, with a maximum latency of 5.66µs. Preempt-RT had slightly worse results, reaching a maximum latency of 15.94µs, but still acceptable for our purposes. The system without real-time is not reliable when millisecond precision or below is required, as it goes over the millisecond barrier in these tests (indicated in red in Figure 3). Nevertheless, as mentioned in section 2.2.1, Linux operating systems allow to isolate a processing core and bind a specific task to it. In this scenario, the task running over the isolated core will never be interrupted by any other users tasks, but system processes can still use this core. When this is done in RTAI or Xenomai it has little impact on their already good latency values, but we observe a remarkable improvement of latency values with both non real-time and Preempt-RT operating systems. Without an RTOS, it can be an useful tool in soft real-time environments.
Unfortunately, regarding the analyzed RTOS, the better their performance is, the worse their usability and user-friendliness. As summarized in Table 3, RTAI is quite difficult to install and use, even for experimented users, while Preempt-RT is the most accessible, since there are not many differences with a normal GPOS. Due to this results, RTHybrid was developed to run over both Xenomai and Preempt-RT to balance performance and user-friendliness.

Software Design and Implementation
RTHybrid is designed to be an user-friendly and accessible real-time tool for any researcher, regardless of their budget or computer science and programming knowledge. It is an opensource project that can be downloaded for free from its Github Frontiers in Neuroinformatics | www.frontiersin.org FIGURE 2 | Summarized results of the real-time benchmarking tests for each OS: No real-time (NRT), No real-time with an isolated core (NRT Isol), RTAI, Xenomai 3, Preempt-RT, and Preempt-RT with an isolated core (Preempt-RT Isol). Time axis is represented in log-scale. Blue bar represents the jitter, calculated as the difference between the maximum and minimum latency. Orange bar represents the maximum latency. Red bar is the mean latency, error bars indicate the standard deviation. Numbers on top of each bar correspond to the largest jitter and latency, respectively. For 20 kHz trials the real-time constrain is 50µs, which is only exceeded in this case by the OS with neither real-time capabilities nor an isolated core. repository 4 . Any Linux distribution is supported, including those running Xenomai 3 and Preempt-RT real-time patches. The code is written using C/C++ language and compiled with GCC 6.3 and QMake 3.0. Relevant information regarding the hybrid circuit experiment, such as neuron (both living and model) and synapse types employed, parameters and latency values is registered in log files. Detailed instructions on how to download, configure and install both RTHybrid and all its dependencies are provided in its user manual 5 .
Three processing threads have been used to build the program's architecture in order to address an optimized realtime software implementation (see Figure 4). Two execution modes are available: graphical user interface (GUI) mode or script mode. In the former, an intuitive GUI (see Figure 5) is displayed when the application is launched, where the user can select the desired models and modify all their parameters as well as set the experiment configuration (duration, DAQ channels, sampling rate, etc). This GUI has been designed using the Qt 5.10 framework 6 . The second option allows to automatically load all experimental parameters from an XML text file 7 and then sequentially execute various experimental protocols using scripts. Instructions on how to use the program and examples of XML configuration files can be found in RTHybrid user manual. Whatever option is chosen, the first and main process is in charge 4 Source code for RTHybrid: www.github.com/GNB-UAM/RTHybrid 5 RTHybrid User Manual: www.github.com/GNB-UAM/RTHybrid/blob/master/ docs/RTHybrid_User_Manual.pdf 6 Qt5 website: www.qt.io/ 7 libxml2 library website: http://xmlsoft.org/ of gathering the information from either source and starting two new threads with it.
The second thread performs all tasks that need to be completed with real-time precision in a periodic loop. Within each interval of this loop, key operations are fulfilled: interaction with the DAQ device, synapse and neuron models computation, automatic calibration, drift adaptation, etc. (see Figure 6). Communication with DAQ devices is achieved through Comedi open-source drivers for National Instruments' and several other manufacturers' hardware (or Analogy drivers in the case of Xenomai) (Schleef et al., 2012). Automatic calibration and experiment automation algorithms are included to deal with the differences between models and living neurons in terms of temporal scale and amplitude (Reyes-Sanchez et al., 2018). They also cover other possible experimental complications such as the presence of signal drift.
Some tasks are computationally too expensive to be carried out within the temporal restrictions established in a real-time interval, including essential ones as writing data to a file or printing it on screen. A third thread is used to write experiment data to files without disturbing the real-time performance. An inter-process message queue is utilized to send information from one thread to the other. Since it is not a real-time process, it will wait until there are enough computer resources available, and only then reads from the queue and stores the data into the files.

Model Library
RTHybrid includes a customizable library of neuron and synapse models to build a wide variety of hybrid circuits. Table 4 shows the currently included neuron models, which have been selected due to their rich intrinsic dynamics and suitability to FIGURE 3 | Distribution of latency values for each operating system during the real-time benchmarking tests. Red bars correspond to latency values that have exceeded the 50µs limit established for 20 kHz trials, which is the case for the "No real-time" scenario. Vertical axes are in log-scale. This analysis was performed on the same data than Figure 2. build hybrid circuits. All of them have different characteristics and require also specific adaptations to work in real-time, which are handled automatically by auto-calibration algorithms (Reyes-Sanchez et al., 2018). Several numerical integration methods can be chosen by the user, including Euler, Heun, Order 4 Runge-Kutta (Press et al., 1988) and (6)5 Runge-Kutta (Hull et al., 1972). Table 5 lists the types of synapse models included in the library at the moment and the different input parameters that they require to work in a hybrid circuit implementation. Models described with differential equations require minimum integration steps and specific adaptations to match the duration of events such as spikes or bursts in a given living neuron. This is the case of models such as Izhikevich (2003), Hindmarsh and Rose (1984), Ghigliazza and Holmes (2004), and Wang (1993). Other neuron models, such as the Rulkov map (Rulkov, 2002), generate activity events such as bursts using a little number of points. In this case, interpolation of the values produced by the model is required to match the living neuron's event resolution at the chosen sampling rate. Synapse models also present differences in their implementation, both in the input parameters required and the computational cost. A simple linear electrical synapse model is included along with more complex non-graded and graded chemical synapse paradigms (Destexhe et al., 1994;Golowasch et al., 1999). New models can be easily added to the library using C language. Figure 6 illustrates the average time consumed by each task at each iteration of the loop executed by RTHybrid real-time thread. This study was performed for several models of the RTHybrid library. The models were run at a 10 kHz frequency for 5min (300s), i.e., each model test contained 3 million intervals of 100 µs duration. Neuron models were bidirectionally connected through a graded chemical synapse model to a hardwareimplemented Hindmarsh-Rose model that generated bursting activity at the same characteristic rate of a pyloric CPG cell (Pinto et al., 2000). Synaptic conductances were set to g = 0.02µS and all other parameters from neuron and synapse models were fixed to produce bursting behavior. Burst duration for the models was set at 1s per burst. The computer used for these tests was the one referred as Computer 1 in Table 1, with both Preempt-RT and Xenomai 3 and the same DAQ device and board described in section 2.2.2. Models selected for this analysis included one lowcost differential equation implementation (Hindmarsh and Rose,FIGURE 4 | Diagram of RTHybrid architecture. The computer receives the membrane voltage from the living neurons through the DAQ device, computes the neurons and synapses models and sends current back to the biological cells. The main program process creates two sub-threads, rt_thread and writer_thread, to manage the different tasks of the program. Both threads communicate through inter-process communication (IPC) message queues.
FIGURE 5 | Illustration of RTHybrid graphical user interface to build hybrid circuit interactions. Users can select neuron and synapse models, as well as their parameters, and the experiment settings, such as input/output DAQ channels, sampling frequency, duration, etc.  Different models have different computational costs and need to be adapted to real-time performance using distinct methods. a Rulkov (2002), b Izhikevich (2003), c Hindmarsh and Rose (1984), d Ghigliazza and Holmes (2004), e Wang (1993).

1984)
, a fast map model (Rulkov, 2002) and two conductancebased models with exponential non-linearities (Wang, 1993;Ghigliazza and Holmes, 2004). Variability of the time consumed by the operations performed inside each interval (see Table 6) is constrained by the use of RTOS, being Xenomai 3 more effective in this task than Preempt-RT. Xenomai 3 achieved much lower latency values, but the use of the Analogy framework, instead of Comedi, translated into a higher cost when interacting with the DAQ device. Some models have on average low computation times  which may increase occasionally due to their nonlinearities or to a non stable regime for the chosen integration method. This is the case for the Hodgkin-Huxley type model from Wang (1993), which on <10 occasions on every trial, out of the three million iterations described previously for Figure 6, had an abnormally high computation time. Other models can always be computationally expensive because of different reasons such as their high dimensionality or multi-compartmental nature.
One way to reduce their integration time and make them more efficient and suitable for real-time environments is to tabulate their nonlinearities with high resolution taking advantage or the large memory capacity of modern computers. When the operations within an interval exceed the temporal margin  Figure 6 on every iteration.

Preempt-RT Xenomai 3
Mean ± Std(µs) Max (µs) Mean ± Std(µs) Max (µs) established, RTHybrid tries to minimize the negative impact on the system by taking time from the next interval and reducing the sleeping period, as can be seen in Figure 7. The presence of latency values above the real-time threshold can be detected from the log file.

Validation With Hybrid Circuits
Proper performance of RTHybrid neuron and synapse models was tested building hybrid circuits as detailed in section 2.2.2. Living neurons from the pyloric CPG were bidirectionally connected through chemical graded synapse models with neuron models. Four trials per model were conducted, each of them 5 min long, with one a minute long control period before and after the hybrid circuit interaction. The first two trials were performed with a sampling frequency of 10 kHz, thus cycle interval duration was 100µs. The remaining two trials were run at 20 kHz, and the interval was 50µs long. Any latency value exceeding that limit was considered a real-time failure. All four trials per model were repeated both in Preempt-RT and Xenomai 3, without core isolation. Connections in these hybrid circuits mimicked graded chemical synapses with fast and slow dynamics (Golowasch et al., 1999). The connection from the model to the living neuron was built with a slow synapse. A fast graded synapse was used for the connection from the living neuron to the model. Our target was to achieve rhythmic antiphase behavior between the neurons so we set inhibitory synapses in both directions. The slow synapse had a conductance of g = 0.2µS, V th = 15% and s = 1% of the maximum amplitude range and kinetic parameters k 1 = 14.0 and k 2 = 4.0, except when using Hindmarsh-Rose model that the conductance was g = 0.1µS. The fast synapse had V th = 50% and s = 5% of the maximum amplitude range and different conductances depending on the neuron model at use: for Izhikevich and Ghigliazza-Holmes models the conductance was g = 0.8µS, for Hindmarsh-Rose model it was g = 1.0µS and for Rulkov was g = 0.2µS. Input voltage scaling factor was set to 100 and output current/voltage conversion factor to 10nA/V. Neuron model parameters were set to produce bursting behavior and the duration of each burst was set to automatically match the living neuron activity. Signal amplitude and temporal scaling was automatically performed by RTHybrid calibration algorithms (Reyes-Sanchez et al., 2018). Validation tests are shown in Figures 8, 9. Figure 8 shows the results of the validation tests for the Rulkov and Izhikevich models, and Figure 9 displays the results with the Hindmarsh-Rose and Ghigliazza-Holmes models. For each model, sorted by increasing complexity, there are four panels displaying different information about the hybrid interaction. The worst performance trials for each model, understood as those with higher latency values, were selected for the analysis. Recorded membrane potential when both living and model neurons are uncoupled is displayed in (Figures 8A, 9A). Voltage is scaled to the living neuron range, showing the independent behavior of each neuron's bursting activity when there is no interaction. Membrane potential during the hybrid interaction along with the synaptic current injected in both directions is shown in (Figures 8B, 9B), portraying the robust antiphase rhythm achieved due to the bidirectional inhibitory graded chemical connectivity. (Figures 8C, 9C) represents Preempt-RT test latency values, showing that in all cases the 100µs limit established for 10 kHz trials, represented by the right-most red vertical line, was fulfilled. However, this RTOS was not able to keep 20 kHz constrains, indicated by the left-most red vertical line, during these experiments. This was not the case for Xenomai 3, portrayed at (Figures 8D, 9D), whose latency values were far under the 50µs barrier set during 20 kHz trials. Due to this outcome, the results displayed correspond to Preempt-RT 10 kHz and Xenomai 20 kHz trials.

DISCUSSION
Characterization and control of neural systems dynamics, as well as experimental protocol automation can largely benefit from the use of closed-loop techniques, and specifically from hybrid circuits built by connecting model neurons and synapses to living cells. RTHybrid provides a neuron and synapse model library aimed to build hybrid circuits in an easy and userfriendly manner with large flexibility provided by a real-time software approach that takes advantage of the computational and memory capacity of modern computers. Developed for Linux, this program is open-source and can be downloaded for free from www.github.com/GNB-UAM/RTHybrid, where installation guide, user manuals and examples can also be found. RTHybrid has a simple GUI to design and configure the hybrid experiments. This tool also incorporates a command-line mode where configuration XML files can be loaded, a useful feature for experiment automation and scripting for activity-dependent exploration of neural dynamics. Beyond a standard Comedi compatible DAQ board, no additional hardware requirements are needed. Thus, standard computers used for data acquisition in any lab can run this software, for example in a dual-boot configuration with another hard-drive with a GPOS setup, e.g., Windows, to keep existing protocol configurations.
FIGURE 7 | Illustration of how RTHybrid recovers from an unusual high time-consuming operation. In this case, during iteration N of a test on Preempt-RT with Wang model (Wang, 1993), the computation time of the neuron model was abnormally high, reaching almost 140 µs and, therefore, exceeding the real-time constrains. RTHybrid handles this kind of situation by taking time from iteration N + 1 to finish the pending operations from iteration N, and reducing or skipping the waiting time, so on iteration N + 2 the proper behavior of the system is restored.
FIGURE 8 | Hybrid circuit built with a pyloric CPG neuron and a simulated neuron using models from (Rulkov, 2002) and (Izhikevich, 2003). For both models, each of the four panels represent: (A) membrane potential of the uncoupled neurons before any interaction (adapted to the living neuron range), showing their independent dynamics (B) membrane potential and synaptic currents (adapted to the living neuron range) during the hybrid interaction, showing their coupled antiphase dynamics (C) Preempt-RT latency values (D) Xenomai 3 latency values. On the latency figures, left-most red line represents the 20 kHz limit and the right-most, the 10 kHz limit.
Temporal precision requirements for closed-loop interactions in RTHybrid are fulfilled by using hard real-time software technology. An extensive analysis of available RTOS was conducted to select the most suitable platforms to implement RTHybrid. The software has been developed to run over Preempt-RT and Xenomai 3 frameworks for Linux due to their balance between performance and accessibility. RTHybrid model library includes a wide variety of neuron models: from computationally-unexpensive paradigms, such as Rulkov (2002), Izhikevich (2003), and Hindmarsh and Rose (1984), to realistic conductance-based Hodgkin-Huxley type (Wang, 1993;Ghigliazza and Holmes, 2004). The library also contains several FIGURE 9 | Hybrid circuit built with a pyloric CPG neuron and a simulated neuron using models from (Hindmarsh and Rose, 1984) and (Ghigliazza and Holmes, 2004). For both models, each of the four panels represent: (A) membrane potential of the uncoupled neurons before any interaction (adapted to the living neuron range), showing their independent dynamics (B) membrane potential and synaptic currents (adapted to the living neuron range) during the hybrid interaction, showing their coupled antiphase dynamics (C) Preempt-RT latency values (D) Xenomai 3 latency values. On the latency figures, left-most red line represents the 20 kHz limit and the right-most, the 10 kHz limit. synapse models: from simple gap junctions implementations to configurable chemical synapses (Destexhe et al., 1994;Golowasch et al., 1999). All of them are adapted to work under hard realtime restrictions and additional neuron, network and synapse models can be easily added using C language. Moreover, calibration algorithms are integrated in the library to automate the adaptation of model amplitude and time scales to the living neuron behavioral range (Reyes-Sanchez et al., 2018). Core isolation can also be employed as validated in the tests reported in this paper.
Models currently included in the RTHybrid library are suitable for a wide variety of hybrid circuit experiments implemented using dynamic clamp. Beyond electrophysiological protocols, RTHybrid can also be easily generalized to drive open-and closed-loop interactions in optogenetic and drug microinjection paradigms. Future development in parallelization and GPU computing will be considered to implement large scale network or highly-realistic biophysical models.
Many researchers and laboratories overlook closedloop techniques despite their advantages due to the difficulties in the installation and use of the required technology. With RTHybrid, we aim to encourage the use of open-source, standardized and user-friendly real-time software tools, available in different platforms, to facilitate the implementation of closed-loop experimentation in neuroscience research.

ETHICS STATEMENT
Permission for invertebrate research was obtained from the ethics committee of Universidad Autónoma de Madrid (DPI2015-65833-P).

AUTHOR CONTRIBUTIONS
RA, MR-S, and PV conceived of and designed the project. RA and MR-S wrote, tested, and documented the software. RA, MR-S, and IE and performed the experiments. RA, MR-S, IE, FR, and PV analyzed the data and wrote the paper.