.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials/forhowto/plot_1_working_with_tetrodes.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_tutorials_forhowto_plot_1_working_with_tetrodes.py: Working with tetrodes ===================== Tetrodes are a common recording method for electrophysiological data. It is also common to record from several tetrodes at the same time. In this 'how to' we'll see how to work with data from two tetrodes, each with four channels. We'll start by importing some functions we'll use in this How To guide .. GENERATED FROM PYTHON SOURCE LINES 11-18 .. code-block:: Python import spikeinterface.preprocessing as spre from spikeinterface.widgets import plot_traces, plot_probe_map from spikeinterface import generate_ground_truth_recording from probeinterface import generate_tetrode, ProbeGroup .. GENERATED FROM PYTHON SOURCE LINES 19-22 In practice, you would read in your raw data from a file. Instead, we will generate a recording with eight channels. We can also set a duration, number of units and sampling frequency. .. GENERATED FROM PYTHON SOURCE LINES 22-30 .. code-block:: Python recording, _ = generate_ground_truth_recording( durations = [60], # make the recording 60s long sampling_frequency=30_000, num_channels=8, num_units=10, ) .. GENERATED FROM PYTHON SOURCE LINES 31-38 We now need to define the probe. This will tell the recording which channels came from which tetrode. To do this, we will use the :code:`generate_tetrode` function from :code:`ProbeInterface` to generate two 4-channel probes (representing one tetrode each). In our case, since we don't know the relative distances between the tetrodes, we will move the second tetrode away from the first by 100 microns. This is just so we can visualize the results more easily. Eventually, we will sort each tetrode separately, so their relative distance won't affect the results. .. GENERATED FROM PYTHON SOURCE LINES 38-61 .. code-block:: Python # Technically, we will add each tetrode to a :code:`ProbeGroup`. Read more in the ProbeInterface # docs. # Create each individual tetrode tetrode_1 = generate_tetrode() tetrode_1.create_auto_shape() tetrode_2 = generate_tetrode() tetrode_2.move([100, 0]) tetrode_2.create_auto_shape() # Add the two tetrodes to a ProbeGroup tetrode_group = ProbeGroup() tetrode_group.add_probe(tetrode_1) tetrode_group.add_probe(tetrode_2) # Now we need to "wire" our tetrodes to ensure that each contact # can be associated with the correct channel when we attach it # to the recording. In this example we are just using `range` # but see ProbeInterface for more tutorials on wiring tetrode_group.set_global_device_channel_indices(range(8)) .. GENERATED FROM PYTHON SOURCE LINES 62-64 We can now attach the :code:`tetrode_group` to our recording. To check if this worked, we'll plot the probe map .. GENERATED FROM PYTHON SOURCE LINES 64-68 .. code-block:: Python recording_with_probe = recording.set_probegroup(tetrode_group) plot_probe_map(recording_with_probe) .. image-sg:: /tutorials/forhowto/images/sphx_glr_plot_1_working_with_tetrodes_001.png :alt: plot 1 working with tetrodes :srcset: /tutorials/forhowto/images/sphx_glr_plot_1_working_with_tetrodes_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 69-72 Looks good! Now that the recording is aware of the probe geometry, we can begin a standard spike sorting pipeline. First, we can apply preprocessing. Note that we apply this preprocessing on the entire bundle of tetrodes. .. GENERATED FROM PYTHON SOURCE LINES 72-75 .. code-block:: Python preprocessed_recording = spre.bandpass_filter(recording_with_probe) .. GENERATED FROM PYTHON SOURCE LINES 76-87 WARNING: a very common preprocessing step is to apply a common median reference. This subtracts the median signal from all channels to help remove noise. However, for a tetrode, a spike is often seen on all channels. So removing the median can remove the entire spike! This is still a danger if you have two tetrodes in a bundle, which might pick up the same spike, but becomes less dangerous as the number of tetrodes in your bundle increases. Tetrodes often have dead channels, so it is advised to try and detect and remove these. For tetrodes, we should use a detection method which doesn't depend on the channel locations such as std or mad: .. GENERATED FROM PYTHON SOURCE LINES 87-93 .. code-block:: Python recording_good_channels = spre.detect_and_remove_bad_channels( preprocessed_recording, method = "std", ) .. GENERATED FROM PYTHON SOURCE LINES 94-98 It can be a good idea to sort your tetrode data separately for each tetrode. When we use :code:`set_probegroup`, the channels are automatically labelled by which probe in the probe group they belong to. We can access this labeling using the "group" property. .. GENERATED FROM PYTHON SOURCE LINES 98-101 .. code-block:: Python print(recording_good_channels.get_property("group")) .. rst-class:: sphx-glr-script-out .. code-block:: none [0 0 0 0 1 1 1 1] .. GENERATED FROM PYTHON SOURCE LINES 102-103 We can then use this information to split the recording by the group property: .. GENERATED FROM PYTHON SOURCE LINES 103-107 .. code-block:: Python grouped_recordings = recording_good_channels.split_by('group') print(grouped_recordings) .. rst-class:: sphx-glr-script-out .. code-block:: none {0: GroundTruthRecording (ChannelSliceRecording): 4 channels - 30.0kHz - 1 segments 1,800,000 samples - 60.00s (1.00 minutes) - float32 dtype - 27.47 MiB, 1: GroundTruthRecording (ChannelSliceRecording): 4 channels - 30.0kHz - 1 segments 1,800,000 samples - 60.00s (1.00 minutes) - float32 dtype - 27.47 MiB} .. GENERATED FROM PYTHON SOURCE LINES 108-110 Now that we've got preprocess, clean data. Let's take a look at a snippet of data from the first group: .. GENERATED FROM PYTHON SOURCE LINES 110-113 .. code-block:: Python plot_traces(grouped_recordings[0]) .. image-sg:: /tutorials/forhowto/images/sphx_glr_plot_1_working_with_tetrodes_002.png :alt: plot 1 working with tetrodes :srcset: /tutorials/forhowto/images/sphx_glr_plot_1_working_with_tetrodes_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 114-118 Beautiful! We are now ready to sort. To read more about sorting by group, see :ref:`sorting-by-channel-group`. Note that many modern sorters are designed to sort data from high-density probes and will fail for tetrodes. Please read each spike sorter's documentation to find out if it is appropriate for tetrodes. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.632 seconds) .. _sphx_glr_download_tutorials_forhowto_plot_1_working_with_tetrodes.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_1_working_with_tetrodes.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_1_working_with_tetrodes.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_1_working_with_tetrodes.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_