|
| 1 | +--- |
| 2 | +uid: basic-ephys-processing |
| 3 | +title: Basic Ephys Data Processing in Bonsai |
| 4 | +--- |
| 5 | + |
| 6 | +This tutorial shows how to use ONIX hardware and the OpenEphys.Onix1 Bonsai package to perform basic online signal |
| 7 | +processing on electrophysiology data in Bonsai such as channel selection and reordering, frequency filtering and event |
| 8 | +detection (in this example, spike detection using a fixed threshold crossing). |
| 9 | + |
| 10 | +This type of processing is helpful for visualizing data during acquisition and can be a starting point for more advanced |
| 11 | +workflows such as closed-loop experiments. For specialized data visualizations from very dense arrays like Neuropixels |
| 12 | +probes, for example, we recommend piping that data to the Open Ephys GUI. |
| 13 | + |
| 14 | +<!-- Event detection in Bonsai will be faster and it allows actuation using ONIX or other hardware for closed-loop |
| 15 | +applications. However, more advanced event detection algorithms such as spike sorting, ripple detection, etc. need |
| 16 | +specific implementations in Bonsai. --> |
| 17 | +<!-- I'm not sure we need to discuss what this tutorial doesn't do --> |
| 18 | + |
| 19 | +This tutorial guides you through building the following workflow: |
| 20 | + |
| 21 | +::: workflow |
| 22 | + |
| 23 | +::: |
| 24 | + |
| 25 | +> [!NOTE] |
| 26 | +> Although this tutorial uses headstage64 as an example, the process is similar for other ephys headstages. This |
| 27 | +> tutorial assumes you are familiar with the [hardware guide](xref:hardware) of the ONIX headstage you intend to use. |
| 28 | +> Use this [reference](xref:reference) for which ephys <xref:dataio> and scaling you need to use for each headstage, and links to relevant |
| 29 | +> documentation. |
| 30 | +
|
| 31 | +## Set up and get started in Bonsai |
| 32 | + |
| 33 | +Follow the [Getting Started](xref:getting-started) guide to set up and get familiarized with Bonsai. In particular: |
| 34 | + |
| 35 | +- [Download the necessary Bonsai packages](xref:install-configure-bonsai#install-packages-in-bonsai) or |
| 36 | +[check for updates](xref:install-configure-bonsai#update-packages-in-bonsai). This tutorial assumes |
| 37 | +you're using the latest software. |
| 38 | +- Read about [visualizing data](xref:visualize-data) since we recommend checking each step of the tutorial by visualizing the data produced but we don't cover it here. |
| 39 | + |
| 40 | +## Configure the hardware |
| 41 | + |
| 42 | +::: workflow |
| 43 | + |
| 44 | +::: |
| 45 | + |
| 46 | +Construct a [top-level hardware configuration chain](xref:initialize-onicontext): |
| 47 | + |
| 48 | +1. Place the [configuration operators](xref:configure) that correspond to the hardware you intend to use between |
| 49 | +<xref:OpenEphys.Onix1.CreateContext> and <xref:OpenEphys.Onix1.StartAcquisition>. In this example, these are |
| 50 | +<xref:OpenEphys.Onix1.ConfigureHeadstage64> and <xref:OpenEphys.Onix1.ConfigureBreakoutBoard>. |
| 51 | +1. Confirm that the device that streams electrophysiology data is enabled. The Rhd2164 device (an Intan amplifier) on |
| 52 | +the headstage64 is the only device used in this tutorial, so you could disable other devices on the headstage and on the |
| 53 | +breakout board to improve performance if you wanted to. |
| 54 | + |
| 55 | +## Stream ephys data into Bonsai |
| 56 | + |
| 57 | +::: workflow |
| 58 | + |
| 59 | +::: |
| 60 | + |
| 61 | +Place the relevant operators to stream electrophysiology data from your headstage: |
| 62 | + |
| 63 | +1. Because the device on headstage64 that streams electrophysiology data is the Rhd2164 Intan amplifier, we placed the |
| 64 | +<xref:OpenEphys.Onix1.Rhd2164Data> node onto the workflow. Use this [reference](xref:reference) to find the ephys data operator |
| 65 | +that corresponds to each device. |
| 66 | +1. Select the relevant members from the data frames that the data operator produces. In this example, the relevant members are "AmplifierData" and "Clock". To select those members, right-click the `Rhd2164` node, hover over the output option in the context menu, and select it from |
| 67 | +the list. |
| 68 | +1. Visualize the raw data to confirm that the ephys data operator is streaming data. |
| 69 | + |
| 70 | +## Select and reorder channels |
| 71 | + |
| 72 | +::: workflow |
| 73 | + |
| 74 | +::: |
| 75 | + |
| 76 | +Connect a <xref:Bonsai.Dsp.SelectChannels> operator to the electrophysiology data stream and edit its "Channels" property. |
| 77 | + |
| 78 | +- Remember indexing in Bonsai starts at 0. |
| 79 | +- Use commas to list multiple channels and brackets for ranges. |
| 80 | +- Reorder channels by listing the channel numbers in the order in which you want to visualize the channels. |
| 81 | + |
| 82 | +## Convert ephys data to microvolts |
| 83 | + |
| 84 | +::: workflow |
| 85 | + |
| 86 | +::: |
| 87 | + |
| 88 | +### Center the signal around zero |
| 89 | +Connect a <xref:Bonsai.Dsp.ConvertScale> operator to the `SelectChannels` operator and set its properties: |
| 90 | +- Edit its "Shift" property to subtract 2^bit depth - 1^ from the signal. Use this [reference](xref:reference) to find |
| 91 | +the Shift necessary for each device. In this example, we "Shift" -32768 because the Rhd2164 device outputs unsigned |
| 92 | +16-bit data. |
| 93 | +- Set the "Depth" property to F32 because this bit depth is required to correctly represent scaled data from all |
| 94 | +devices. |
| 95 | + |
| 96 | +### Scale the signal to microvolts |
| 97 | +Connect a second `ConvertScale` operator to the first `ConvertScale` operator and set its properties: |
| 98 | +- Edit its "Scale" property to multiply the signal by a scalar in order to get microvolt values. This scalar is |
| 99 | +determined by the gain of the amplifier and resolution the ADC contained in the amplifier device. Use this |
| 100 | +[reference](xref:reference) to find the "Scale" necessary for each device. In this example, we "Scale" by 0.195 because |
| 101 | +the Rhd2164 device on headstage64 has a step size of 0.195 μV/bit |
| 102 | +- Keep the "Depth" property at F32. |
| 103 | + |
| 104 | +Visualize the transformed data to confirm the output of the shifting and scaling operations |
| 105 | +worked as expected, i.e. that the signal is centered around zero and that the values make sense in microvolts. |
| 106 | + |
| 107 | +> [!NOTE] |
| 108 | +> Although both the Shift and Scale calculation can be done in one `ConvertScale` operator, the calculations are |
| 109 | +> more straightforward using two operators connected in series because the `ConvertScale` operator applies the |
| 110 | +> "Shift" offset after applying the "Scale" scalar so if we used a single operator, we would have to scale the Shift |
| 111 | +> parameter. |
| 112 | +
|
| 113 | +## Apply a filter |
| 114 | + |
| 115 | +::: workflow |
| 116 | + |
| 117 | +::: |
| 118 | + |
| 119 | +Connect a `FrequencyFilter` operator to the second `ConvertScale` operator and set its properties. |
| 120 | +- Set its "SampleRate" property to 30000. Ephys data in all devices is 30 kHz. |
| 121 | +- Set the "FilterType" property to an adequate type. In this example, we use a high pass filter to look at spikes. |
| 122 | +- Set the "Cutoff1" and "Cutoff2" properties to an adequate value. In this example, we use 300 Hz as the |
| 123 | + lower cutoff frequency. |
| 124 | + |
| 125 | +Visualize the filtered data. |
| 126 | + |
| 127 | +> [!TIP] |
| 128 | +> If you choose to save data, we recommend you place the `MatrixWriter` operator before filtering and scaling to save raw |
| 129 | +> data instead of scaled or filtered data. Filtering with the `FrequencyFilter` operator before recording could remove signals from a bandwidth of interest and converting to microvolts with the second `ConvertScale` operator could increase the size of your data without increasing meaningful information. |
| 130 | +
|
| 131 | +## Detect events |
| 132 | + |
| 133 | +::: workflow |
| 134 | + |
| 135 | +::: |
| 136 | + |
| 137 | +Based on the amplitude of the signal on the selected channel, set a fixed threshold for detecting spikes. <!-- discuss these details? --> |
| 138 | + |
| 139 | +Visualize the spike data. |
| 140 | + |
| 141 | +> [!TIP] |
| 142 | +> You can test the spike detection using a pre-recorded data known to have spikes: recreate the |
| 143 | +> workflow from this example without the hardware configuration chain in a new workflow and replace the ephys data node (in the case of the headstage64, replace |
| 144 | +> the `Rhd2164` node) with a `MatrixReader` that reads from the file containing spiking ephys data in unsigned 16-bit format. |
0 commit comments