tmp_plot_spikesorting.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # -*- coding: utf-8 -*-
  2. """
  3. Spikesorting with SpikeInterface
  4. =================================
  5. One of the most common processing steps for neuronal data is the identification
  6. of spiking activity of individual neurons. [SpikeInterface](https://github.com/Spikeinterface/Spikeinterface)
  7. is a python packages that provides a common interface to multiple different software tools designed
  8. for this task. Here we demonstrate how to preprocess a BIDS-ephys compatible dataset using SpikeInterface
  9. and visualize the result. This script is accessible at :download:`source Python script <plot_spikesorting.py>`."""
  10. # %%
  11. # First we import SpikeInterface and locate the dataset
  12. import matplotlib.pyplot as plt
  13. import spikeinterface.full as si
  14. session_folder = '../ephys_nix/sub-i/ses-140703/ephys'
  15. # %%
  16. # SpikeInterface provides a convenience function for loading a BIDS-ephys compatible dataset.
  17. # This dataset contains multiple streams (data recorded at different sampling rates).
  18. # SpikeInterface represents each of those streams in a separate `recording` object.
  19. recordings = si.read_bids(session_folder)
  20. # %%
  21. # We select the most `raw` data stream by selecting the one with the highest temporal resolution, i.e. sampling frequency.
  22. recording_raw = recordings[0]
  23. for rec in recordings[1:]:
  24. if rec.get_sampling_frequency() > recording_raw.get_sampling_frequency():
  25. recording_raw = rec
  26. # %%
  27. # By default the software packages to be used for spike sorting needs to be installed locally to
  28. # perform the sorting. However, the installation of these specialized software tools can be
  29. # complicated and inconvenient due to specific system requirement. To simplify the usage of
  30. # different spike sorting packages SpikeInterface provides a set of containers with preinstalled
  31. # sorting packages. The list of available containers and their versions is available on
  32. # [DockerHub](<TODO: Add link here>).
  33. #
  34. # Here we run the sorting package `ironclust` using the containerized version. To run the container we are using `singularity`, alternativly SpikeInterface also supports running contains
  35. # in `docker`.
  36. sorting = si.run_sorter_container(sorter_name='ironclust', recording=recording_raw,
  37. mode='singularity',
  38. container_image='spikeinterface/ironclust-compiled-base',
  39. output_folder='ironclust_output',
  40. with_output=True, fGpu=False)
  41. print(sorting)
  42. # %%
  43. # Automatic spike sorting always requires manual inspection the results to confirm the sorting
  44. # algorithm and the applied parameters were suited for the dataset. Here we visualize the extracted
  45. # spike times for each unit (neuron) in a raster plot.
  46. si.plot_rasters(sorting)
  47. plt.show()
  48. # %%
  49. # We can extract the waveforms on which the sorting is based. To align the waveforms we remove the
  50. # low frequency components before extracting the waveform snippets from the raw signal.
  51. recording_filtered = si.highpass_filter(recording_raw, freq_min=300.) # frequencies are provided in Hz
  52. recording_filtered.annotate(is_filtered=True)
  53. waveforms = si.extract_waveforms(recording_filtered, sorting, './ironclust_waveform_output',
  54. overwrite=True, ms_before=1, ms_after=2.)
  55. si.plot_unit_waveforms(waveforms)
  56. plt.show()