A fast intracortical brain–machine interface with patterned optogenetic feedback

Objective. The development of brain–machine interfaces (BMIs) brings new prospects to patients with a loss of autonomy. By combining online recordings of brain activity with a decoding algorithm, patients can learn to control a robotic arm in order to perform simple actions. However, in contrast to the vast amounts of somatosensory information channeled by limbs to the brain, current BMIs are devoid of touch and force sensors. Patients must therefore rely solely on vision and audition, which are maladapted to the control of a prosthesis. In contrast, in a healthy limb, somatosensory inputs alone can efficiently guide the handling of a fragile object, or ensure a smooth trajectory. We have developed a BMI in the mouse that includes a rich artificial somatosensory-like cortical feedback. Approach. Our setup includes online recordings of the activity of multiple neurons in the whisker primary motor cortex (vM1) and delivers feedback simultaneously via a low-latency, high-refresh-rate, spatially structured photo-stimulation of the whisker primary somatosensory cortex (vS1), based on a mapping obtained by intrinsic imaging. Main results. We demonstrate the operation of the loop and show that mice can detect the neuronal spiking in vS1 triggered by the photo-stimulations. Finally, we show that the mice can learn a behavioral task relying solely on the artificial inputs and outputs of the closed-loop BMI. Significance. This is the first motor BMI that includes a short-latency, intracortical, somatosensory-like feedback. It will be a useful platform to discover efficient cortical feedback schemes towards future human BMI applications.


Introduction
Invasive brain-machine interfaces (BMIs) have recently trans itioned from research proto types (Fetz 1969, Chapin et al 1999, Wessberg et al 2000 to the first trials in human patients (Hochberg et al 2006, Collinger et al 2013, Wodlinger et al 2015, Ajiboye et al 2017. These trials show that over the course of months, a fully paralyzed patient can learn to con trol a robotic prosthesis and perform simple actions such as grasping a bottle and drinking through a straw. These achieve ments rely solely on the readout of the spiking activity from electrodes chronically implanted in motor areas, which is translated into motor commands for the prosthetic device. movements (Sainburg et al 1995, Akay et al 2014 and grip (Johansson et al 1984, Monzée et al 2003 as well as in achieving embodiment of the prosthesis (Marasco et al 2011).
Prostheses that address sensory periphery deficits pre date motor BMIs, and include cochlear (Djourno et al 1957), retinal (Brandli et al 2016 and tactile implants (Clippinger et al 1974, Raspopovic et al 2014, OrtizCatalan et al 2014, Graczyk et al 2016, Oddo et al 2016. Prostheses mediated by cortical microstimulation are rarer, but they are increasingly successful. Prototypes have been developed to rescue vision (Brindley et al 1968, Dobelle et al 1974, and an active effort is underway to build a somatosensory prosthesis based on S1 microstimulations (O'Doherty et al 2009, Flesher et al 2016. Such S1targeting cortical prosthesis allows spatial discrimination between touchlike stimuli (Tabot et al 2013). It can also be used to support a redundant, population vector coding (Dadarlat et al 2015) of touchlike inputs, as well as other, nontouch related, but topically structured S1 micro stimulationbased inputs (Hartmann et al 2016). Nevertheless, there is debate regarding the benefit of multichannel feedback compared to single channel feedback (Kim et al 2015a).
One attractive feature of S1 is the topographical represen tations of touch inputs at the surface of the cortex (Penfield et al 1937). This homology between the spatial organization of the sensory periphery and of the cortical surface is a core computational principle of cortical circuits and constrains sensory perception (Estebanez et al 2018). Therefore, feed back based on spatial patterns of cortical stimulation may be both intuitive and carry a larger set of information compared to recent feedback strategies based on timedependent micro stimulations in S1 (O'Doherty et al 2009(O'Doherty et al , 2011. However, despite the efforts to design attractive multi site S1 microstimulations, they have not so far been inte grated into closedloop BMIs. More generally, few studies have integrated artificial feedback into a closedloop invasive BMI (O'Doherty et al 2011, Prsa et al 2017. This is in part because electrical microstimulations in S1 disrupt the readout of electrophysiological inputs in M1, preventing continuous closedloop operation (O'Doherty et al 2011). In addition, reproducing a somatotopy can be technically challenging, since the spatial resolution of current epicortical microstimu lation arrays is limited to a pitch of at least several hundreds of microns between adjacent electrodes (Flesher et al 2016) and the actual spatial extent of electrical microstimulation is hard to control and may depend on the specific axonal con nectivity of the stimulated area (Histed et al 2009). Finally, electrical stimulations are not selective to the neuronal cell type, activating both excitatory and inhibitory neurons at the same time.
In contrast, optogenetic BMI feedback would not be affected by these shortcomings (Prsa et al 2017). We therefore developed a BMI prototype in the headfixed mouse model that couples into a closed BMI loop both a readout of neu ronal activity in vM1 using chronic extracellular recording electrodes and feedback delivered through the spatially pat terned, low latency photostimulation of Channelrhodopsin2 expressing pyramidal neurons in vS1. We characterize this loop, and show that the vS1 artificial stimulation triggers controlled spiking activity. We show that this activity can be detected by the mouse in the context of a detection task, and finally we show that the mouse can learn to carry a task exclu sively through the inputs and outputs provided by the closed loop BMI.

General structure of the bidirectional BMI
To set up this BMI with feedback, we have worked on a number of modules that we describe and test (figure 1). First, a photostimulation system capable of dynamically updating, within milliseconds, 100 µm scale illumination patterns at the vS1 cortical surface (section 2.2). Second, an intrinsic imaging device coupled to the photostimulation system to target the photostimulations at functionally localized barrels of vS1 (sections 2.3-2.6). Third, an extracellular recording system that acquires neuronal activity from vM1 (sections 2.7 and 2.8). Fourth, computer software that connects the input and output modules (sections 2.9 and 2.10), and delivers timely waterbased rewards (section 2.11). The code, as well as a list of the parts used to build the setup, has been made available at the following link: https://github.com/BMIunic/ Sensorimotor_BMI.git.

Photostimulation optical sub-system
To project photostimulation patterns into vS1, we used a Digital Light Processing module (DLP, Vialux V7001, Germany) containing a 1024 × 768 Texas Instruments micro mirror chip, which was illuminated by a 462 nm blue LED (optimal to photoactivate Channelrhodopsin, Bamann et al 2008). This device (figure 1(B)) allowed us to display arbitrary patterns of stimulation by streaming binary (black and white) frames through a USB 3.0 link using the manufacturer's API.
We coupled the DLP optically to a tandemlens macro scope, to focus the divergent light beam coming out of the projector and project it onto the surface of the cortex of a head fixed mouse. Two lenses were arranged in an Lshape optical pathway with a dichroic beamsplitter inbetween. Light pat terns entered the macroscope though the rear end of the first lens (150 mm smc PentaxA 645 lens) and went through the dichroic beamsplitter placed at a 45° angle. We used camera lenses instead of optical lenses to minimize image distortions (figures 1(C) and (D)). The beamsplitter (640 nm cut off wave length; 60 × 60 mm; Semrock) reflected the light patterns to the front end of the second lens (50 mm Nikon NIKKOR lens), which demagnified the image by a factor of three and projected it over the cortex of the mouse. The working dis tance of the macroscope is 4.6 cm.

Intrinsic imaging optical sub-system
We combined this projection pathway with a secondary 'measurement' tandemlens optical pathway in which the light from a red or green LED, illuminating the cortex, was reflected back through the rear end of the second lens and transmitted by the beamsplitter to the front end of a third lens (50 mm Fujinon HF50HA1B). This lens was mounted on a 12bit monochrome CCD camera (Basler acA640120um). This second optical pathway was devoted to the acquisition of intrinsic signals from the cortex (figure 2).

Implantation and preparation for intrinsic imaging
All animal experiment procedures were in accordance with European and French law, and CNRS guidelines. Initial surgery was carried under Isoflurane anesthesia (1%-4% Isoflurane depending on mouse state, assessed by breathing rate and response to tail pinch), in 100% O 2 . The surgery was performed in a stereotactic frame (Stoelting) on a regulated heating pad, with the mouse held by a nose clamp. The scalp was resected and conjunctive tissues were removed. At this stage, a head fixation bar was attached (figure 1(E)) using a bonding layer of cyanoacrylate glue applied to the skull (Henkel Loctite) followed by dental cement (Lang Dental, USA) to encase the attachment bar (Guo et al 2014).
After this initial surgery, the animal was transferred under the macroscope (figure 1(B)) to identify the location of the cortical columns that receive the input projection from indi vidual whiskers in vS1 (figure 2(B)). The mouse underwent a first intrinsic imaging session (figure 2(C)) through the intact skull (figures 2(D)-(F)), in order to identify function ally the location of the cortical area that responds to whisker deflections, and more specifically the column ('barrel') that corresponds to the central vibrissa of the whiskerpad, the C2 whisker .

Intrinsic imaging procedure over vS1
To carry intrinsic imaging (Grinvald et al 1986) and identify the location of the barrel associated with a specific whisker (Knutsen et al 2016), a red LED (625 nm) was used to illu minate vS1 through the skull of the isoflurane anesthetized mouse (figure 2(C)). To increase the skull transparency as much as possible, a temporary well made from high vacuum grease (Dow Corning, USA) was built around vS1 (coordi nates P1.5mm L3.3mm from bregma, figure 1(E)), filled with Ringer solution, and topped with a coverslip (Fisher Scientific, USA). This configuration ensured good light transmission from cortex to the camera sensor, in particular through the skull. The CCD camera (figure 2(C)) was trig gered to acquire 659 × 494 px images at a rate of 50 fps. Two seconds after the onset of the imaging, the C2 whisker (figure 2(A)) was deflected by a piezoelectric bender (PI PICMA Bender) mounted on a vibrationdampening holder (Jacob et al 2010) (figure 2(C)). The whisker was deflected 100 times at 100 Hz following a train of 5 ms square wave deflections in the caudorostral axis (figure 2(D)).
This acquisition sequence was repeated 20 times to obtain a trialaveraged movie. To identify the location of the brain region activated by the stimulated whisker (the socalled barrel column), a baseline was computed by timeaveraging the images acquired in the two seconds before stimulus onset. Each frame in the trial was divided by this baseline frame and the 'response' image obtained by averaging the normalized images in the time interval 1-2 s post stimulus. We defined the position of the barrel in the image as the absolute lumi nance minimum across the whole baselinenormalized image, following a smoothing procedure to remove local noise fluctuations. The procedure resulted in a dark spot (approx imately 200 µm in diameter) at the barrel location (figures 2(D) and (E)).
During the same imaging procedure, we also acquired an image under green LED illumination (527 nm) that highlighted the blood vessel pattern at the surface of the cortex and served as a reference image for the followup surgery (figure 2(F)).

Setting up the optical window
In a second surgical procedure that followed immediately after the intrinsic imaging session, we removed a 4 mm diam eter disk of skull over the barrel cortex while preserving the dura, (figure 1(E)) centered on the positioning of barrel C2 derived from the first intrinsic imaging session. A 5 mm diam eter glass coverslip was then glued to the sides of the cra niotomy, followed by dental cement reinforcement, thereby providing a permanent optical window centered on the barrel cortex and allowing both the readout of intrinsic signals and the photostimulation of Channelrhodopsin positive neurons in the barrel cortex (Holtmaat et al 2009). After this step, the mouse was left to rest for up to 10 d to recover, and the optical window checked to ensure it stayed clear in the long run. Out of 18 mice that we implanted with an optical window, ten showed a clear window after 14 d, and these remained clear until we terminated the experiments, up to two months later. Mice without a clear window were removed from the experiments.

Implantation of silicon probes over M1
A third surgery was then performed under isoflurane anes thesia, this time to set up a chronic 32 channel multisite extracellular electrode (eight tetrodes across four shanks, Neuronexus A4x2tet5mm150200121CM32) centered over the whisker motor cortex (vM1) (Ferezou et al 2007, Zagha et al 2013. We followed an established protocol (Okun et al 2016) to insert the probe into the cortex and then attach it to the mouse skull. We made a small (approximately 1 mm 2 ) opening in the skull above the stereotactic coordinates of vM1 (A: 1.5 mm L: 0.6 mm from bregma, figure 1(E)). We then resected the dura to reveal the cortex, and positioned the elec trode above the opening using the stereotax arm. After a slow insertion of the electrode tip into the cortex (1 µm s −1 ) down to layer 5 (700 µm), the craniotomy was filled with a fast set, lowtoxicity silicon adhesive (KwikCast, WPI), and the electrode connector attached to the skull using dental cement primed with a thin coat of cyanoacrylate glue.
After one week of recovery, signals were recorded by plugging an extracellular recording system onto the electrode (Blackrock Cerebus). After highpass filtering and thresh olding to extract spikes, a qualitative analysis of the spiking activity was carried out.
Spiking activity recorded up to two months after electrode implantation showed no sign of degradation.

Identification of neurons with stable spiking activity
Recorded activity was manually clustered within the space of the amplitude of the spikes acquired from the four elec trodes, using the online spike sorting software (Blackrock Microsystems Central) provided with the extracellular recording system. After this initial spike sorting, recordings were obtained on five consecutive days. A subset of the initial units that appeared stable (in terms of spike shape and mean firing rate) was selected to be part of the rest of the experiment.
The preprogrammed spike sorting was carried out online by the acquisition system within a few milliseconds of the spike occurrence, and the resulting pattern used as a template for the ongoing neuronal activity.

Generating photostimulation patterns
To control neuronal activity using photoactivation, we based our experiments on a mouse strand that expresses the light gated ion channel Channelrhodopsin specifically in pyramidal neurons (EMXCre;Ai27). In these mice, photostimulation of brain tissue resulted in a spiking discharge of local pyramidal neurons (Madisen et al 2012). We took advantage of the large scale optical window to project complex spatial patterns that spanned the whole vS1 (Roy et al 2016). We designed pho tostimulation patterns that were carefully aligned on the bar rels of vS1 (figures 2(A) and (B)) and conformed with the discrete nature of these columnar structures, being composed of photostimulation discs (225 µm diameter, light intensity 25 mW mm −2 ) aligned on the barrels. To position this shared pattern accurately on the mouse barrel, we carried out a new intrinsic imaging session, this time through the optical window (figures 2(D)-(I)). We acquired the position of at least three barrels scattered across the barrel field. We then used these located barrels to align an average barrel pattern (figure 2(J)) acquired in another study in adult mice of the same strain background (Knutsen et al 2016) at the surface of the cortex. Photostimulation discs were centered on this pat tern (figure 2(K)). These operations were done using custom made Python software.

2.10.Online control of the hardware
Access to the spikesorted ongoing activity was provided by the extracellular recording system through a C++ API, via a UDP ethernet link. We built a Qtbased application (the 'master' application) that pooled the spikes emitted by the acquisition system and kept in memory only the spikes that were part of the userselected units.
This master application was tasked with the control of the photostimulation system, as well as the reward system, based on the spiking activity. To ensure that performance was not affected by user interactions, the graphical user interface was implemented in a second Qt thread. It provided: a readout of the ongoing spiking activity recorded in vM1; a visualiza tion of the current photostimulation feedback; and a widget to adjust the parameters affecting the feedback based on the spiking activity (Video 1 (stacks.iop.org/JNE/15/046011/ mmedia)).
The master computer also controlled all the analog and digital inputs and outputs of the experiment. These were chan neled through the same device used for the electrophysiolog ical acquisition system (Blackrock Cerebus NSP). This has the advantage of sharing the same clock with the electrophysi ological data. The same system also drove the piezo electric whisker stimulator and triggered the acquisition of frames during intrinsic imaging sessions. Finally, during behavior, it triggered the opening of the valve to deliver water rewards to the water restrained mice, and recorded each time the photo activation pattern was updated.

Reward mechanism
In our behavioral paradigm, a range of firing rates were asso ciated with rewards in order to promote a controlled modula tion of the firing rate by the mouse. To establish this reward schedule, the mouse access to water supply was restricted to a single 30 min time slot at the end of each training day and free water on weekends. This water schedule ensured that the mice were thirsty at the start of the behavioral trials. Water drop lets were delivered through a lickport system (Estebanez et al 2017) set within reach of the headfixed mouse tongue ( figure  3(A)). This device was under control of the master computer program; licks and rewards were displayed on the graphical user interface.

Acute recordings of vS1 neuronal activity during photostimulation
To ensure that the photostimulation patterns did result in an activation of neurons in vS1, we set up a control experiment to record the activity of vS1 neurons during a standard pho tostimulation pattern over vS1. To do so, we implanted mice with a headholder and optical window over vS1 following the standard procedure (sections 2.4-2.6). On the day of the planned recording, we anesthetized the mouse using isoflu rane. Using a diamondcoated dental bur, we cut the glass open over a small section. We then slid an acute 32 channel extracellular electrode (Neuronexus A1x32Poly35mm25s 177A32) through this opening, below the glass window at a 45° angle, down to approximately 300 µm depth. Finally, we applied 225 µm diameter illumination spots through the glass window, either apical to the electrode tip, or at 300 or 600 µm away from the electrode.

A GO/NOGO task to assess the detection of vS1 photostimulation
To test if vS1 photostimulations could be detected and inte grated in a behavioral sequence, we designed a GO/NOGO task in which a mouse was rewarded upon the correct detection of a vS1 photostimulation pattern. Trials started with an audi tory cue followed by a random 4-9 s period where the animal must refrain from licking. After this period, photostimulation started (500 ms, 7 ms ON/3 ms OFF flicker, 100 Hz). Rewards were delivered after five licks during the photostimulation or the following 500 ms.
To ensure that the mouse detected the photoactivation via the activation of neurons and vS1 and not through its visual system, we applied a flickering blue light source to its eyes as a distractor. Finally, during the experiments we played a white noise sound background to limit distraction from reward valve clicks.
To check that photostimulations could not in fact be visu ally detected by the mice, we included in the behavior a sham condition (20% of the trials) in which the photostimulation was applied to the dental cement next to the optical window.

Pace and lag of the feedback loop
To assess the refresh rate of our closed loop, we quantified the pace of the translation between spiking rate and photo stimulation by monitoring the photostimulation display with a photodiode (PDA10AEC, ThorLabs) while it was forced to flip between dark and bright frames at each program loop iteration. Except for the systematic flicker of the display, the setup was not altered for this test. The calibration was carried out while a 500 Hz mean spiking rate load was applied as an input to the system. We found that the program main loop ran every 1.06 ms (mean), with a low variability (SD 0.28 ms, figure 3(B)).
Beyond the refresh rate, the time needed for information to propagate from one side of the setup to its end is critical. This endtoend latency of the setup (the socalled lag) can have a major impact on the loop performance. In particular, a large lag (in the hundreds of milliseconds) may make the informa tion fed to the cortex entirely mismatched with the ongoing motor output. To estimate the lag, we timed the delays in the full loop using a modified feedback algorithm that flashed a bright frame as soon as a spike was read. To fully control the input during this test, we applied an artificial electrical pulse to the input stage of the electrophysiology recording system, which interpreted it as a spike. The timing of this artificial spike was used as a reference to measure the delay in updating the photostimulation, as measured by a photodiode. Using this setting, we found that the loop operated with a hardware related endtoend latency of 12.3 ms (mean, SD 3.03 ms, figure 3(C)) between the spiking input and frame output.

Validation of vM1 electrophysiological recordings
Next, we checked that in this setting the chronic recordings obtained in vM1 were not impacted by artifacts related to the closed loop operation. In particular, we looked at the poten tial impact of mouse licks, as well as of the photostimulation flicker applied on vS1.
To do so, we implanted one mouse with both a chronic electrode in vM1 and an optical window over vS1, and we trained this mouse to lick to obtain water rewards ad libitum. Next, in this mouse we acquired with the implanted vM1 elec trode both the local field potential (LFP) and the multiunit activity, while the mouse was licking and photostimulation was applied.
Visual inspection of electrophysiological traces revealed no sign of lick (figure 3(D)) or photostimulationrelated artifacts (figure 3(E)). Next, we quantified these potential artifacts by computing the distribution of the amplitude of LFP modula tions over a 10 ms window around licks and photostimulation (1000 trials), compared to the distribution of LFP amplitude in random 10 ms windows. For both photostimulations and licks, test and control amplitude distributions were not significantly different (Mann-Whitney p > 0.05). Therefore, we argue that in our settings, electrophysiological recordings in vM1 were devoid of the main potential sources of artifacts.

Activation of neurons in vS1 by photostimulation
We then ensured that the photostimulations applied to vS1 are able to activate neurons in the targeted area. With this aim, we recorded extracellularly the activity of vS1 neurons in two anesthetized mice while photostimulation was applied over the optical window (see methods, figure 4(A)). We projected a 225 µm diameter photostimulation spot onto vS1 at various distances from the electrode: next to the electrode recording site, 300 µm and 600 µm away. These photostimulations were flickered (5 ms ON/5 ms OFF) for 1.5 s each, and different positions were presented at random times. We found that pho tostimulation spots next to the electrode led to a fast activa tion of recorded putative pyramidal neurons, while the same pattern of photostimulation led to much diminished spiking activation at 300 and 600 µm away from the recorded elec trode (example in figures 4(B) and (C), population analysis in figures 4(D) and (E)). Overall, we found that we could trigger timely spiking in vS1 neurons with these settings, and that the spatially structured photostimulations led to spatially struc tured neuronal activation in vS1.

Behavioral demonstration of the detection of vS1 photostimulation
To assess if the photostimulations can actually be perceived by the mouse and result in a behavior, we trained two mice in a GO/NOGO photostimulation detection task (see methods, figure 5(A)). The mice were implanted with a headpost and a glass window. We used intrinsic imaging to locate the barrels through the glass window, and generated photostimulation of individual barrels that formed a barlike stimulus, including five barrels along the C2 row (photostimulation similar to the pattern shown in figure 6(B)). We trained the mouse to detect this stimulus in order to obtain a water reward ( figure 5(A), left).
Following training (approximately 10 d, twice daily), the mice learned to lick specifically after the trial photostimula tion. This was not the case in sham trials where the photostim ulation was applied on the dental cement next to the optical window (figures 5(B) and (C)). Therefore, we conclude that the vS1 photostimulationbased feedback could be sensed by the mice and that this detection occurred through the optoge netic activation of vS1 neurons.

Mice are able to perform a behavioral task using the closed-loop
To test the full closed loop of the BMI and demonstrate its capabilities, we implemented a task that relied on the artificial vS1 inputs and vM1 outputs. This task was based on the con trol of a virtual bar that deflected virtual whiskers during its rostrocaudal displacement ( figure 6(A)). Contact of the virtual bar with the virtual whiskers led to photostimulation of the corresponding barrels using a simple onetoone correspon dence between specific firing rate ranges and a given pattern of photostimulation (figures 6(B) and (C) and video 1).
The 1D position of the virtual bar was directly tied to the pooled activity of the units that were recorded in vM1, after binning with a sliding 0.1 s box kernel to improve the smooth ness of the control of the bar. Low firing rate corresponded to a caudal position of the bar, and high firing rate to a rostral position.
The photostimulation patterns were derived from the 24whisker pattern of photostimulation (figure 2(K)) by selecting subsets of the photostimulation discs that mimic a bar crossing the whiskerpad ( figure 6(B)). Overall, large swings in population firing rate resulted in an almost simul taneous back and forth motion of a barlike photostimulation in vS1 (video 1 and figure 6(C), obtained in a nontrained mouse). These stimulus patterns are known to drive barrel cortex responses strongly (Drew et al 2007, Jacob et al 2008. To minimize Channelrhodopsin desensitization resulting from permanent photoactivation (Nagel et al 2003), the patterns were pulsed at 100 Hz (50% duty cycle) by using a builtin flickering capability of the projector. The goal of this task was to train the mouse to position the bar within a limited range of firing rate defined by the experi menter. With this aim, the mouse was water restricted to trans form water droplets into valuable rewards. The mouse could obtain such water drops by licking a water port at the specific time when it managed to hold its firing rate within the target range. To limit opportunistic rewards, only the first lick after the firing rate entered the reward zone was rewarded.
To test this behavior, we implanted two additional mice with an optical window over vS1 and a chronic extracellular electrode in vM1. After recovery, the mouse was headfixed. The photostimulation was aligned over the barrels in vS1 using custommade calibration software to drive the camera and projector (figure 2).
To train the waterrestricted mouse to perform the task, we shaped it with the following steps. On the first training session, we let the mouse lick freely to obtain water. Then, over the following sessions, we conditioned the water delivery on population firing rate that was inside the target firing rate range. Initially, this range was close to the baseline popula tion firing rate and was crossed frequently at random. But, over the course of the trials, we moved the rewarded firing rate range away from the baseline firing rate. Within one to two weeks of training to the task once a day for 40 min, we found that the mouse had learned to (1) perform large swings of firing rate to attain the rewarded range, and (2) lick timely and thereby obtain water rewards (example in figure 6(D), left and figure 6(E)). To assess the impact of the photostimulation on this behavior, we then tested exactly the same behavior, but this time with the photostimulation feedback turned off. We found that in this condition, there were still fast increases of firing rate, but they were no longer simultaneous to licks and led to very few rewards in both mice (figures 6(D) and (E)).
Overall, we showed that mice can learn to solve a behav ioral task in the context of our closedloop BMI, and that the behavior performance relies on the artificial feedback pro vided in vS1.

Choice of vS1/vM1 implantation
The decision to develop a feedback system that directly tar gets the cortex and not the periphery ensures that, in the con text of medical treatment, it could operate even in patients with a number of peripheral disabilities. Similarly, invasive cortical readout interfaces are designed to bypass a defec tive spinal cord (Hochberg et al 2006, Hochberg et al 2012, Collinger et al 2013, Ajiboye et al 2017. This invasive feed back strategy has recently been selected to set up somatosen sory feedback interfaces (Tabot et al 2013, Flesher et al 2016, as well as to replace other senses such as vision (Dobelle et al 1974, Dobelle et al 2000. Within the cortex, the choice of vS1 and vM1 among the many possible input/output areas is motivated by three rea sons. First, S1 and M1 are nearby areas-particularly in humans. Therefore, from the perspective of a medical applica tion, both inputs and outputs could be grouped into a compact 'neural port' attached to the skull in a stable way. Beyond this practical reason, we selected S1 for feedback as this area is specialized in the processing of touch input (Estebanez et al 2018), but also proprioceptive inputs (Kim et al 2015b), which are the two main inputs that should be conveyed from an arti ficial prosthesis. Third, and equally important to our focus on the vM1/vS1, there is a large bundle of axonal fibers that projects from vS1 to vM1 (Welker et al 1988, Ferezou et al 2007, Zakiewicz et al 2014. This suggests that inputs to vS1, including artificial inputs, may be quickly and reliably relayed to vM1 for integration into motor commands, as confirmed by functional studies in vM1 (Ferezou et al 2007, Zagha et al 2015, Fassihi et al 2017. . This bar is located inside a virtual whiskerpad, and when it touches the virtual whiskers, this results in a photoactivation of the corresponding barrels in vS1. (B) Frames projected onto vS1, as a function of the level of vM1 firing rate from low (frame 0) to high (frame 7). White discs: photostimulated barrels. Grayed discs: nonphotostimulated barrels. (C) Example of population vM1 activity in a waking, nontrained mouse. Raster of activity and population firing rate (bottom) and the resulting vS1 photostimulation frame identity across time (top). The translation of firing rate to projection happens within upper and lower firing rate limits (dashed lines). Rewards are dispensed when firing rate is maintained within a narrow range (frames 3 and 4, green). (D) Example of the behavior of a mouse that learned the task, including licks (bottom, rewarded licks in green) and the population firing rate that controls the virtual bar (middle). Green interval: rewarded. Left: optogenetic feedback activated. Right: feedback deactivated leads to a drop in reward rate. (E) Rewarded lick rate (Hz) in the two mice that were trained to the behavior, with versus without feedback.
Finally, we should mention that this short latency connec tion between vS1 and vM1 does not preclude the projection of vS1 inputs to other brain areas, as well as the control of vM1 activity from other brain areas ( figure 3(A)). Therefore, this choice does not restrict the involvement of the whole brain in solving tasks that are interfaced through the closedloop BMI. Instead, it only ensures that a highperformance, reflex pathway is available to carry part of the behavior.

Optogenetic control of vS1 activity via DLP projector
We chose to implement this BMI feedback using photostimu lation of channelrhodopsinexpressing pyramidal neurons in vS1-while, to date, attempts at including feedback in a BMI have always relied on electrical microstimulation of S1. We were interested in taking advantage of the high level of con trol on the spatial organization that is provided by optogenetics in the mouse. In addition, this technique makes it possible to restrict the neuronal activation to a specific cell subtype (Cardin et al 2010). Finally, photoactivation of Channelrhodopsin does not disturb the electrophysiological recording in vM1, in con trast with electrical microstimulations (O'Doherty et al 2011).
Using this versatile feedback device, we will be able to explore a large array of feedback schemes, including feedback schemes that adhere to the somatotopical map like the one we demonstrate here, but also photostimulation patterns that are independent of the functional maps. Both high frequency, up to kHz modulations, and slower gray scale displays may also be explored. This approach was inspired by a number of studies that showed photoactivation of Channelrhodopsine expressing neurons to be a promising way to substitute failing physiological sensory inputs at the periphery. In particular, in the visual pathway, peripheral photostimulation of the retina can be substituted to physiological light activation, including complex coding properties of retinal neurons (Greenberg et al 2011). These studies aim to develop a spatially structured (Reutsky et al 2007), multichannel, visuallike input to the central nervous system. Although we aim to achieve a com parable sensory substitution, we are aiming at the cortex, and this led us to different choices. In particular, the photostimu lation characteristics are profoundly different when targeting vS1 versus the retina, which is a highresolution, spatially extended imaging layer with well known microcircuitryin sharp contrast with the lower spatial resolution, higher temporal resolution and poorly known microcircuits of vS1. Overall, beyond the shared use of optogenetics, we believe that cortical optogenetic substitution at the cortical level requires a whole different set of development compared to its use at the periphery-for instance, at the retinal level.

Readout of vM1 activity
To read out the activity of the subject, we have chosen to use extracellular electrodes implanted in the primary motor cortex. This ensures stable access to the activity of neurons (Okun et al 2016). The use of extracellular invasive recordings to ensure the data readout is a proven technique (Arduin et al 2013(Arduin et al , 2014 in driving prosthetic devices, including forearm/ hand surrogates with a large number of independent actuators (Hochberg et al 2012, Collinger et al 2013, Ajiboye et al 2017 which would benefit greatly from rich feedback. However, the choice of extracellular recording also has its downsides. In particular, in contrast to optical methods, it cannot capture the spatial organization of the functional activity in M1, despite the functional maps that structure this area, in particular corre sponding to the body plan (Penfield et al 1937). In addi tion, in the long run, electrodes tend to damage the structure where they have been inserted, by causing tissue inflammation (Karumbaiah et al 2013). Nevertheless, month long recordings with chronic silicon probes are possible (Vetter et al 2004), in particular with the tethered connector and thin (15 µm thick) silicon probe design that we selected (Karumbaiah et al 2013). An alternative design choice would be to extend the optical window to reveal M1 and use optical methods to measure the tissue activity, including genetically encoded calcium indica tors such as GCaMP6 (Chen et al 2013). Such an alloptical BMI has already been attempted recently, using twophoton microscopy to read out activityrelated fluorescence fluctua tions in individual M1 neurons (Prsa et al 2017), but alterna tive strategies could also be used to record the activity at the scale at which the photostimulation is applied, potentially by using the camera that captured the intrinsic imaging signal. By this means, we could acquire mesoscale fluorescence sig nals in mice that express a calcium indicator in M1 (Minderer et al 2012).

End-to-end latency of the closed loop
Due to the low latency of inputs from the periphery to vS1 (Le Cam et al 2011), as well as to the potentially disruptive impact of lags on closedloop settings (Kim et al 2005), we focused our attention on a technological solution that would result in a lowlatency transmission of information. To do so, we chose to perform all our programming in a highperformance language (C ++ ) and we relied on a lowlag electrophysiology acquisition system and photostimulation device.
However, lags occur naturally in all sensorimotor loops, including the whisker system, and such lags might be worth mimicking in an artificial connection between S1 and M1, to comply with what M1/S1 neurons may be expecting during natural behavior.
Several studies have timed the whisker system sensori motor loop in waking mice and found that early responses in vS1 occur within 7 ms of the whisker stimulus onset. The transmission of this input from vS1 to vM1 requires approxi mately 8 ms (Ferezou et al 2007). Finally, electrical and optogenetic microstimulation experiments showed that M1 activation triggered whisker movements after at least a 20 ms delay (Matyas et al 2010, Auffret et al 2017. Therefore, in total, the natural whisker system full sensorimotor loop from vM1 motor command to vS1 feedback takes up at least 27 ms, a delay larger than the 12.3 ms we measured in our artificial BMI loop. This short latency gives us the opportunity to play with the lag in our BMI loop and decide if it should precede, match or lag behind the physiological endtoend latency of the sensorimotor loop.
In addition to these hardware latencies, one should note that our artificial loop is also impacted by delays related to the time window that is required to translate instantaneous firing recorded in vM1 into a motor command. At the moment, we have chosen to rely on a very simple algorithm-specifically, a 100 ms binning kernel which translates raw spikes into a smooth firing rate that constitutes the motor command. In future, several strategies could be developed to reduce the impact of this integration window. One strategy is to use more elaborate integration algorithms. But beyond processing strat egies, probably the most efficient way to reduce the need for a long integration window is to increase the average firing rate recorded in vM1. One way to achieve this goal is by training the mouse to shift the activity of the vM1 neurons to higher firing rate ranges. Another option would be to merge a larger number of neurons into the population firing rate. These addi tional neurons could be acquired by increasing the number of independent electrode recording sites by moving from a 32 to a 64 channel electrode.

Conclusion
We have reported on the development of an invasive BMI that includes a versatile optogeneticsbased feedback system able to produce arbitrary spatiotemporal patterns of stimulation on the surface of the cortex, at a mesoscopic scale (150 µm pat terns). In the headfixed mouse, we used intrinsic imaging to position an optical window above vS1. Using the same optical window, we applied spatiotemporally structured photostimu lation patterns apically on the cortex, aligned on the barrels. In the same mice, we implanted an extracellular silicon mul tielectrode in vM1. We coordinated these components using C ++ programs. Finally, we benchmarked the full BMI loop and showed that the photoactivation of vS1 led to neuronal spiking that could be detected by the mouse and support a BMI task.
Overall, this setup has the potential to become a versatile platform to explore various somatosensory feedback strate gies in BMIs, and more generally to evaluate the attractive ness of the cortical artificial feedback strategy.