{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Peaks Widgets Gallery\n\nSome widgets are useful before sorting and works with \"peaks\" given by detect_peaks()\nfunction.\n\nThey are useful to check drift before running sorters.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n\nimport spikeinterface.full as si"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "First, let's download a simulated dataset\nfrom the repo 'https://gin.g-node.org/NeuralEnsemble/ephy_testing_data'\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "local_path = si.download_dataset(remote_path='mearec/mearec_test_10s.h5')\nrec, sorting = si.read_mearec(local_path)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Lets filter and detect peak on it\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from spikeinterface.sortingcomponents.peak_detection import detect_peaks\n\nrec_filtred = si.bandpass_filter(rec, freq_min=300., freq_max=6000., margin_ms=5.0)\nprint(rec_filtred)\npeaks = detect_peaks(\n        rec_filtred, method='locally_exclusive', \n        peak_sign='neg', detect_threshold=6, exclude_sweep_ms=0.3,\n        local_radius_um=100,\n        noise_levels=None,\n        random_chunk_kwargs={},\n        chunk_memory='10M', n_jobs=1, progress_bar=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "peaks is a numpy 1D array with structured dtype that contains several fields:\n sample_ind/channel_ind/amplitude/segment_ind\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(peaks.dtype)\nprint(peaks.shape)\nprint(peaks.dtype.fields.keys())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "This \"peaks\" vector can be used in several widgets, for instance\n plot_peak_activity_map()\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "si.plot_peak_activity_map(rec_filtred, peaks=peaks)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "can be also animated with bin_duration_s=1.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "si.plot_peak_activity_map(rec_filtred, bin_duration_s=1.)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "plot_drift_over_time'()\n ~~~~~~~~~~~~~~~~~~~~~~~\n heatmap mode\n here bin_duration_s=1. because the rec is short (10s).\n a better value could 60s\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "si.plot_drift_over_time(rec_filtred, peaks=peaks, bin_duration_s=1.,\n        weight_with_amplitudes=True, mode='heatmap')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "plot_drift_over_time'()\n ~~~~~~~~~~~~~~~~~~~~~~~\n in scatter mode\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "si.plot_drift_over_time(rec_filtred, peaks=peaks, weight_with_amplitudes=False, mode='scatter')\n\n\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.8.13"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}