{ "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "execution": {}, "id": "view-in-github" }, "source": [ "  " ] }, { "cell_type": "markdown", "metadata": { "execution": {} }, "source": [ "# Tutorial 1: The Leaky Integrate-and-Fire (LIF) Neuron Model\n", "\n", "**Week 2, Day 3: Biological Neuron Models**\n", "\n", "**By Neuromatch Academy**\n", "\n", "**Content creators:** Qinglong Gu, Songtin Li, John Murray, Richard Naud, Arvind Kumar\n", "\n", "**Content reviewers:** Maryam Vaziri-Pashkam, Ella Batty, Lorenzo Fontolan, Richard Gao, Matthew Krause, Spiros Chavlis, Michael Waskom, Ethan Cheng\n", "\n", "**Production editors:** Gagana B, Spiros Chavlis" ] }, { "cell_type": "markdown", "metadata": { "execution": {} }, "source": [ "---\n", "# Tutorial Objectives\n", "\n", "*Estimated timing of tutorial: 1 hour, 10 min*\n", "\n", "This is Tutorial 1 of a series on implementing realistic neuron models. In this tutorial, we will build up a leaky integrate-and-fire (LIF) neuron model and study its dynamics in response to various types of inputs. In particular, we are going to write a few lines of code to:\n", "\n", "- simulate the LIF neuron model\n", "\n", "- drive the LIF neuron with external inputs, such as direct currents, Gaussian white noise, and Poisson spike trains, etc.\n", "\n", "- study how different inputs affect the LIF neuron's output (firing rate and spike time irregularity)\n", "\n", "Here, we will especially emphasize identifying conditions (input statistics) under which a neuron can spike at low firing rates and in an irregular manner. The reason for focusing on this is that in most cases, neocortical neurons spike in an irregular manner." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "execution": {}, "tags": [ "remove-input" ] }, "outputs": [], "source": [ "# @markdown\n", "from IPython.display import IFrame\n", "from ipywidgets import widgets\n", "out = widgets.Output()\n", "with out:\n", " print(f\"If you want to download the slides: https://osf.io/download/8djsm/\")\n", " display(IFrame(src=f\"https://mfr.ca-1.osf.io/render?url=https://osf.io/8djsm/?direct%26mode=render%26action=download%26mode=render\", width=730, height=410))\n", "display(out)" ] }, { "cell_type": "markdown", "metadata": { "execution": {} }, "source": [ "---\n", "# Setup" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Install and import feedback gadget\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "execution": {}, "tags": [ "hide-input" ] }, "outputs": [], "source": [ "# @title Install and import feedback gadget\n", "\n", "!pip3 install vibecheck datatops --quiet\n", "\n", "from vibecheck import DatatopsContentReviewContainer\n", "def content_review(notebook_section: str):\n", " return DatatopsContentReviewContainer(\n", " \"\", # No text prompt\n", " notebook_section,\n", " {\n", " \"url\": \"https://pmyvdlilci.execute-api.us-east-1.amazonaws.com/klab\",\n", " \"name\": \"neuromatch_cn\",\n", " \"user_key\": \"y1x3mpx5\",\n", " },\n", " ).render()\n", "\n", "\n", "feedback_prefix = \"W2D3_T1\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "both", "execution": {} }, "outputs": [], "source": [ "# Imports\n", "import numpy as np\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Figure Settings\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "execution": {}, "tags": [ "hide-input" ] }, "outputs": [], "source": [ "# @title Figure Settings\n", "import logging\n", "logging.getLogger('matplotlib.font_manager').disabled = True\n", "\n", "import ipywidgets as widgets # interactive display\n", "%config InlineBackend.figure_format = 'retina'\n", "# use NMA plot style\n", "plt.style.use(\"https://raw.githubusercontent.com/NeuromatchAcademy/course-content/main/nma.mplstyle\")\n", "my_layout = widgets.Layout()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plotting Functions\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "execution": {}, "tags": [ "hide-input" ] }, "outputs": [], "source": [ "# @title Plotting Functions\n", "\n", "def plot_volt_trace(pars, v, sp):\n", " \"\"\"\n", " Plot trajetory of membrane potential for a single neuron\n", "\n", " Expects:\n", " pars : parameter dictionary\n", " v : volt trajetory\n", " sp : spike train\n", "\n", " Returns:\n", " figure of the membrane potential trajetory for a single neuron\n", " \"\"\"\n", "\n", " V_th = pars['V_th']\n", " dt, range_t = pars['dt'], pars['range_t']\n", " if sp.size:\n", " sp_num = (sp / dt).astype(int) - 1\n", " v[sp_num] += 20 # draw nicer spikes\n", "\n", " plt.plot(pars['range_t'], v, 'b')\n", " plt.axhline(V_th, 0, 1, color='k', ls='--')\n", " plt.xlabel('Time (ms)')\n", " plt.ylabel('V (mV)')\n", " plt.legend(['Membrane\\npotential', r'Threshold V$_{\\mathrm{th}}$'],\n", " loc=[1.05, 0.75])\n", " plt.ylim([-80, -40])\n", " plt.show()\n", "\n", "\n", "def plot_GWN(pars, I_GWN):\n", " \"\"\"\n", " Args:\n", " pars : parameter dictionary\n", " I_GWN : Gaussian white noise input\n", "\n", " Returns:\n", " figure of the gaussian white noise input\n", " \"\"\"\n", "\n", " plt.figure(figsize=(12, 4))\n", " plt.subplot(121)\n", " plt.plot(pars['range_t'][::3], I_GWN[::3], 'b')\n", " plt.xlabel('Time (ms)')\n", " plt.ylabel(r'$I_{GWN}$ (pA)')\n", " plt.subplot(122)\n", " plot_volt_trace(pars, v, sp)\n", " plt.tight_layout()\n", " plt.show()\n", "\n", "\n", "def my_hists(isi1, isi2, cv1, cv2, sigma1, sigma2):\n", " \"\"\"\n", " Args:\n", " isi1 : vector with inter-spike intervals\n", " isi2 : vector with inter-spike intervals\n", " cv1 : coefficient of variation for isi1\n", " cv2 : coefficient of variation for isi2\n", "\n", " Returns:\n", " figure with two histograms, isi1, isi2\n", "\n", " \"\"\"\n", " plt.figure(figsize=(11, 4))\n", " my_bins = np.linspace(10, 30, 20)\n", " plt.subplot(121)\n", " plt.hist(isi1, bins=my_bins, color='b', alpha=0.5)\n", " plt.xlabel('ISI (ms)')\n", " plt.ylabel('count')\n", " plt.title(r'$\\sigma_{GWN}=$%.1f, CV$_{\\mathrm{isi}}$=%.3f' % (sigma1, cv1))\n", "\n", " plt.subplot(122)\n", " plt.hist(isi2, bins=my_bins, color='b', alpha=0.5)\n", " plt.xlabel('ISI (ms)')\n", " plt.ylabel('count')\n", " plt.title(r'$\\sigma_{GWN}=$%.1f, CV$_{\\mathrm{isi}}$=%.3f' % (sigma2, cv2))\n", " plt.tight_layout()\n", " plt.show()" ] }, { "cell_type": "markdown", "metadata": { "execution": {} }, "source": [ "---\n", "# Section 1: The Leaky Integrate-and-Fire (LIF) model" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Video 1: Reduced Neuron Models\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "execution": {}, "tags": [ "remove-input" ] }, "outputs": [], "source": [ "# @title Video 1: Reduced Neuron Models\n", "from ipywidgets import widgets\n", "from IPython.display import YouTubeVideo\n", "from IPython.display import IFrame\n", "from IPython.display import display\n", "\n", "\n", "class PlayVideo(IFrame):\n", " def __init__(self, id, source, page=1, width=400, height=300, **kwargs):\n", " self.id = id\n", " if source == 'Bilibili':\n", " src = f'https://player.bilibili.com/player.html?bvid={id}&page={page}'\n", " elif source == 'Osf':\n", " src = f'https://mfr.ca-1.osf.io/render?url=https://osf.io/download/{id}/?direct%26mode=render'\n", " super(PlayVideo, self).__init__(src, width, height, **kwargs)\n", "\n", "\n", "def display_videos(video_ids, W=400, H=300, fs=1):\n", " tab_contents = []\n", " for i, video_id in enumerate(video_ids):\n", " out = widgets.Output()\n", " with out:\n", " if video_ids[i] == 'Youtube':\n", " video = YouTubeVideo(id=video_ids[i], width=W,\n", " height=H, fs=fs, rel=0)\n", " print(f'Video available at https://youtube.com/watch?v={video.id}')\n", " else:\n", " video = PlayVideo(id=video_ids[i], source=video_ids[i], width=W,\n", " height=H, fs=fs, autoplay=False)\n", " if video_ids[i] == 'Bilibili':\n", " print(f'Video available at https://www.bilibili.com/video/{video.id}')\n", " elif video_ids[i] == 'Osf':\n", " print(f'Video available at https://osf.io/{video.id}')\n", " display(video)\n", " tab_contents.append(out)\n", " return tab_contents\n", "\n", "\n", "video_ids = [('Youtube', 'rSExvwCVRYg'), ('Bilibili', 'BV1m5411h7Jv')]\n", "tab_contents = display_videos(video_ids, W=730, H=410)\n", "tabs = widgets.Tab()\n", "tabs.children = tab_contents\n", "for i in range(len(tab_contents)):\n", " tabs.set_title(i, video_ids[i])\n", "display(tabs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Submit your feedback\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "execution": {}, "tags": [ "hide-input" ] }, "outputs": [], "source": [ "# @title Submit your feedback\n", "content_review(f\"{feedback_prefix}_LIF_model_Video\")" ] }, { "cell_type": "markdown", "metadata": { "execution": {} }, "source": [ "This video introduces the reduction of a biological neuron to a simple leaky-integrate-fire (LIF) neuron model.\n", "\n", "
\n", " Click here for text recap of video \n", "\n", "Now, it's your turn to implement one of the simplest mathematical model of a neuron: the leaky integrate-and-fire (LIF) model. The basic idea of LIF neuron was proposed in 1907 by Louis Édouard Lapicque, long before we understood the electrophysiology of a neuron (see a translation of [Lapicque's paper](https://pubmed.ncbi.nlm.nih.gov/17968583/) ). More details of the model can be found in the book [**Theoretical neuroscience**](http://www.gatsby.ucl.ac.uk/~dayan/book/) by Peter Dayan and Laurence F. Abbott.\n", "\n", "The subthreshold membrane potential dynamics of a LIF neuron is described by\n", "\n", "\\begin{eqnarray}\n", "C_m\\frac{dV}{dt} = -g_L(V-E_L) + I,\\quad (1)\n", "\\end{eqnarray}\n", "\n", "where $C_m$ is the membrane capacitance, $V$ is the membrane potential, $g_L$ is the leak conductance ($g_L = 1/R$, the inverse of the leak resistance $R$ mentioned in previous tutorials), $E_L$ is the resting potential, and $I$ is the external input current.\n", "\n", "Dividing both sides of the above equation by $g_L$ gives\n", "\n", "\\begin{align}\n", "\\tau_m\\frac{dV}{dt} = -(V-E_L) + \\frac{I}{g_L}\\,,\\quad (2)\n", "\\end{align}\n", "\n", "where the $\\tau_m$ is membrane time constant and is defined as $\\tau_m=C_m/g_L$.\n", "\n", "Note that dividing capacitance by conductance gives units of time!\n", "\n", "Below, we will use Eqn.(2) to simulate LIF neuron dynamics.\n", "\n", "If $I$ is sufficiently strong such that $V$ reaches a certain threshold value $V_{\\rm th}$, $V$ is reset to a reset potential $V_{\\rm reset}< V_{\\rm th}$, and voltage is clamped to $V_{\\rm reset}$ for $\\tau_{\\rm ref}$ ms, mimicking the refractoriness of the neuron during an action potential:\n", "\n", "\\begin{eqnarray}\n", "\\mathrm{if}\\quad V(t_{\\text{sp}})\\geq V_{\\rm th}&:& V(t)=V_{\\rm reset} \\text{ for } t\\in(t_{\\text{sp}}, t_{\\text{sp}} + \\tau_{\\text{ref}}]\n", "\\end{eqnarray}\n", "where $t_{\\rm sp}$ is the spike time when $V(t)$ just exceeded $V_{\\rm th}$.\n", "\n", "(**Note:** in the lecture slides, $\\theta$ corresponds to the threshold voltage $V_{th}$, and $\\Delta$ corresponds to the refractory time $\\tau_{\\rm ref}$.)\n", "\n", "
\n", "\n", "Note that you have seen the LIF model before if you looked at the pre-reqs Python or Calculus days!\n", "\n", "The LIF model captures the facts that a neuron:\n", "- performs spatial and temporal integration of synaptic inputs\n", "- generates a spike when the voltage reaches a certain threshold\n", "- goes refractory during the action potential\n", "- has a leaky membrane\n", "\n", "The LIF model assumes that the spatial and temporal integration of inputs is linear. Also, membrane potential dynamics close to the spike threshold are much slower in LIF neurons than in real neurons." ] }, { "cell_type": "markdown", "metadata": { "execution": {} }, "source": [ "## Coding Exercise 1: Python code to simulate the LIF neuron\n", "\n", "We now write Python code to calculate our equation for the LIF neuron and simulate the LIF neuron dynamics. We will use the Euler method, which you saw in the linear systems case yesterday to numerically integrate this equation:\n", "\n", "\\begin{equation}\n", "\\tau_m\\frac{dV}{dt} = -(V-E_L) + \\frac{I}{g_L}\\,\n", "\\end{equation}\n", "\n", "where $V$ is the membrane potential, $g_L$ is the leak conductance, $E_L$ is the resting potential, $I$ is the external input current, and $\\tau_m$ is membrane time constant.\n", "\n", "The cell below initializes a dictionary that stores parameters of the LIF neuron model and the simulation scheme. You can use pars=default_pars(T=simulation_time, dt=time_step) to get the parameters. Note that, simulation_time and time_step have the unit ms. In addition, you can add the value to a new parameter by pars['New_param'] = value." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Execute this code to initialize the default parameters\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "execution": {}, "tags": [ "hide-input" ] }, "outputs": [], "source": [ "# @markdown Execute this code to initialize the default parameters\n", "\n", "def default_pars(**kwargs):\n", " pars = {}\n", "\n", " # typical neuron parameters#\n", " pars['V_th'] = -55. # spike threshold [mV]\n", " pars['V_reset'] = -75. # reset potential [mV]\n", " pars['tau_m'] = 10. # membrane time constant [ms]\n", " pars['g_L'] = 10. # leak conductance [nS]\n", " pars['V_init'] = -75. # initial potential [mV]\n", " pars['E_L'] = -75. # leak reversal potential [mV]\n", " pars['tref'] = 2. # refractory time (ms)\n", "\n", " # simulation parameters #\n", " pars['T'] = 400. # Total duration of simulation [ms]\n", " pars['dt'] = .1 # Simulation time step [ms]\n", "\n", " # external parameters if any #\n", " for k in kwargs:\n", " pars[k] = kwargs[k]\n", "\n", " pars['range_t'] = np.arange(0, pars['T'], pars['dt']) # Vector of discretized time points [ms]\n", "\n", " return pars\n", "\n", "\n", "pars = default_pars()\n", "print(pars)" ] }, { "cell_type": "markdown", "metadata": { "execution": {} }, "source": [ "Complete the function below to simulate the LIF neuron when receiving external current inputs. You can use v, sp = run_LIF(pars, Iinj) to get the membrane potential (v) and spike train (sp) given the dictionary pars and input current Iinj." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "both", "execution": {} }, "outputs": [], "source": [ "def run_LIF(pars, Iinj, stop=False):\n", " \"\"\"\n", " Simulate the LIF dynamics with external input current\n", "\n", " Args:\n", " pars : parameter dictionary\n", " Iinj : input current [pA]. The injected current here can be a value\n", " or an array\n", " stop : boolean. If True, use a current pulse\n", "\n", " Returns:\n", " rec_v : membrane potential\n", " rec_sp : spike times\n", " \"\"\"\n", "\n", " # Set parameters\n", " V_th, V_reset = pars['V_th'], pars['V_reset']\n", " tau_m, g_L = pars['tau_m'], pars['g_L']\n", " V_init, E_L = pars['V_init'], pars['E_L']\n", " dt, range_t = pars['dt'], pars['range_t']\n", " Lt = range_t.size\n", " tref = pars['tref']\n", "\n", " # Initialize voltage\n", " v = np.zeros(Lt)\n", " v = V_init\n", "\n", " # Set current time course\n", " Iinj = Iinj * np.ones(Lt)\n", "\n", " # If current pulse, set beginning and end to 0\n", " if stop:\n", " Iinj[:int(len(Iinj) / 2) - 1000] = 0\n", " Iinj[int(len(Iinj) / 2) + 1000:] = 0\n", "\n", " # Loop over time\n", " rec_spikes = [] # record spike times\n", " tr = 0. # the count for refractory duration\n", "\n", " for it in range(Lt - 1):\n", "\n", " if tr > 0: # check if in refractory period\n", " v[it] = V_reset # set voltage to reset\n", " tr = tr - 1 # reduce running counter of refractory period\n", "\n", " elif v[it] >= V_th: # if voltage over threshold\n", " rec_spikes.append(it) # record spike event\n", " v[it] = V_reset # reset voltage\n", " tr = tref / dt # set refractory time\n", "\n", " ########################################################################\n", " ## TODO for students: compute the membrane potential v, spike train sp #\n", " # Fill out function and remove\n", " raise NotImplementedError('Student Exercise: calculate the dv/dt and the update step!')\n", " ########################################################################\n", "\n", " # Calculate the increment of the membrane potential\n", " dv = ...\n", "\n", " # Update the membrane potential\n", " v[it + 1] = ...\n", "\n", " # Get spike times in ms\n", " rec_spikes = np.array(rec_spikes) * dt\n", "\n", " return v, rec_spikes\n", "\n", "\n", "# Get parameters\n", "pars = default_pars(T=500)\n", "\n", "# Simulate LIF model\n", "v, sp = run_LIF(pars, Iinj=100, stop=True)\n", "\n", "# Visualize\n", "plot_volt_trace(pars, v, sp)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "execution": {} }, "source": [ "[*Click for solution*](https://github.com/NeuromatchAcademy/course-content/tree/main/tutorials/W2D3_BiologicalNeuronModels/solutions/W2D3_Tutorial1_Solution_60a1e954.py)\n", "\n", "*Example output:*\n", "\n", " \n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Submit your feedback\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "execution": {}, "tags": [ "hide-input" ] }, "outputs": [], "source": [ "# @title Submit your feedback\n", "content_review(f\"{feedback_prefix}_LIF_model_Exercise\")" ] }, { "cell_type": "markdown", "metadata": { "execution": {} }, "source": [ "---\n", "# Section 2: Response of an LIF model to different types of input currents\n", "\n", "*Estimated timing to here from start of tutorial: 20 min*\n", "\n", "In the following section, we will learn how to inject direct current and white noise to study the response of an LIF neuron." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Video 2: Response of the LIF neuron to different inputs\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "execution": {}, "tags": [ "remove-input" ] }, "outputs": [], "source": [ "# @title Video 2: Response of the LIF neuron to different inputs\n", "from ipywidgets import widgets\n", "from IPython.display import YouTubeVideo\n", "from IPython.display import IFrame\n", "from IPython.display import display\n", "\n", "\n", "class PlayVideo(IFrame):\n", " def __init__(self, id, source, page=1, width=400, height=300, **kwargs):\n", " self.id = id\n", " if source == 'Bilibili':\n", " src = f'https://player.bilibili.com/player.html?bvid={id}&page={page}'\n", " elif source == 'Osf':\n", " src = f'https://mfr.ca-1.osf.io/render?url=https://osf.io/download/{id}/?direct%26mode=render'\n", " super(PlayVideo, self).__init__(src, width, height, **kwargs)\n", "\n", "\n", "def display_videos(video_ids, W=400, H=300, fs=1):\n", " tab_contents = []\n", " for i, video_id in enumerate(video_ids):\n", " out = widgets.Output()\n", " with out:\n", " if video_ids[i] == 'Youtube':\n", " video = YouTubeVideo(id=video_ids[i], width=W,\n", " height=H, fs=fs, rel=0)\n", " print(f'Video available at https://youtube.com/watch?v={video.id}')\n", " else:\n", " video = PlayVideo(id=video_ids[i], source=video_ids[i], width=W,\n", " height=H, fs=fs, autoplay=False)\n", " if video_ids[i] == 'Bilibili':\n", " print(f'Video available at https://www.bilibili.com/video/{video.id}')\n", " elif video_ids[i] == 'Osf':\n", " print(f'Video available at https://osf.io/{video.id}')\n", " display(video)\n", " tab_contents.append(out)\n", " return tab_contents\n", "\n", "\n", "video_ids = [('Youtube', 'preNGdab7Kk'), ('Bilibili', 'BV1Li4y137wS')]\n", "tab_contents = display_videos(video_ids, W=730, H=410)\n", "tabs = widgets.Tab()\n", "tabs.children = tab_contents\n", "for i in range(len(tab_contents)):\n", " tabs.set_title(i, video_ids[i])\n", "display(tabs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Submit your feedback\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "execution": {}, "tags": [ "hide-input" ] }, "outputs": [], "source": [ "# @title Submit your feedback\n", "content_review(f\"{feedback_prefix}_Response_LIF_model_Video\")" ] }, { "cell_type": "markdown", "metadata": { "execution": {} }, "source": [ "## Section 2.1: Direct current (DC)\n", "\n", "*Estimated timing to here from start of tutorial: 30 min*\n" ] }, { "cell_type": "markdown", "metadata": { "execution": {} }, "source": [ "### Interactive Demo 2.1: Parameter exploration of DC input amplitude\n", "Here's an interactive demo that shows how the LIF neuron behavior changes for DC input (constant current) with different amplitudes. We plot the membrane potential of an LIF neuron. You may notice that the neuron generates a spike. But this is just a cosmetic spike only for illustration purposes. In an LIF neuron, we only need to keep track of times when the neuron hits the threshold so the postsynaptic neurons can be informed of the spike.\n", "\n", "How much DC is needed to reach the threshold (rheobase current)? How does the membrane time constant affect the frequency of the neuron?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Make sure you execute this cell to enable the widget!\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "execution": {}, "tags": [ "hide-input" ] }, "outputs": [], "source": [ "# @markdown Make sure you execute this cell to enable the widget!\n", "my_layout.width = '450px'\n", "@widgets.interact(\n", " I_dc=widgets.FloatSlider(50., min=0., max=300., step=10.,\n", " layout=my_layout),\n", " tau_m=widgets.FloatSlider(10., min=2., max=20., step=2.,\n", " layout=my_layout)\n", ")\n", "\n", "def diff_DC(I_dc=200., tau_m=10.):\n", " pars = default_pars(T=100.)\n", " pars['tau_m'] = tau_m\n", " v, sp = run_LIF(pars, Iinj=I_dc)\n", " plot_volt_trace(pars, v, sp)\n", " plt.show()" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "execution": {} }, "source": [ "[*Click for solution*](https://github.com/NeuromatchAcademy/course-content/tree/main/tutorials/W2D3_BiologicalNeuronModels/solutions/W2D3_Tutorial1_Solution_1058324c.py)\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Submit your feedback\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "execution": {}, "tags": [ "hide-input" ] }, "outputs": [], "source": [ "# @title Submit your feedback\n", "content_review(f\"{feedback_prefix}_Parameter_exploration_of_DC_input_amplitude_Interactive_Demo_and_Discussion\")" ] }, { "cell_type": "markdown", "metadata": { "execution": {} }, "source": [ "## Section 2.2: Gaussian white noise (GWN) current\n", "\n", "*Estimated timing to here from start of tutorial: 38 min*\n", "\n", "Given the noisy nature of neuronal activity _in vivo_, neurons usually receive complex, time-varying inputs.\n", "\n", "To mimic this, we will now investigate the neuronal response when the LIF neuron receives Gaussian white noise $\\xi(t)$ with mean 0 ($\\mu = 0$) and some standard deviation $\\sigma$.\n", "\n", "Note that the GWN has zero mean, that is, it describes only the fluctuations of the input received by a neuron. We can thus modify our definition of GWN to have a nonzero mean value $\\mu$ that equals the DC input, since this is the average input into the cell. The cell below defines the modified gaussian white noise currents with nonzero mean $\\mu$." ] }, { "cell_type": "markdown", "metadata": { "execution": {} }, "source": [ "### Interactive Demo 2.2: LIF neuron Explorer for noisy input\n", "\n", "\n", "The mean of the Gaussian white noise (GWN) is the amplitude of DC. Indeed, when $\\sigma = 0$, GWN is just a DC.\n", "\n", "So the question arises how does $\\sigma$ of the GWN affect the spiking behavior of the neuron. For instance we may want to know\n", "1. how does the minimum input (i.e., $\\mu$) needed to make a neuron spike change with increase in $\\sigma$\n", "2. how does the spike regularity change with increase in $\\sigma$\n", "\n", "To get an intuition about these questions you can use the following interactive demo that shows how the LIF neuron behavior changes for noisy input with different amplitudes (the mean $\\mu$) and fluctuation sizes ($\\sigma$). We use a helper function to generate this noisy input current: my_GWN(pars, mu, sig, myseed=False). Note that fixing the value of the random seed (e.g., myseed=2020) will allow you to obtain the same result every time you run this. We then use our run_LIF function to simulate the LIF model.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Execute to enable helper function my_GWN\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "execution": {}, "tags": [ "hide-input" ] }, "outputs": [], "source": [ "# @markdown Execute to enable helper function my_GWN\n", "\n", "def my_GWN(pars, mu, sig, myseed=False):\n", " \"\"\"\n", " Function that generates Gaussian white noise input\n", "\n", " Args:\n", " pars : parameter dictionary\n", " mu : noise baseline (mean)\n", " sig : noise amplitute (standard deviation)\n", " myseed : random seed. int or boolean\n", " the same seed will give the same\n", " random number sequence\n", "\n", " Returns:\n", " I : Gaussian white noise input\n", " \"\"\"\n", "\n", " # Retrieve simulation parameters\n", " dt, range_t = pars['dt'], pars['range_t']\n", " Lt = range_t.size\n", "\n", " # Set random seed\n", " if myseed:\n", " np.random.seed(seed=myseed)\n", " else:\n", " np.random.seed()\n", "\n", " # Generate GWN\n", " # we divide here by 1000 to convert units to sec.\n", " I_gwn = mu + sig * np.random.randn(Lt) / np.sqrt(dt / 1000.)\n", "\n", " return I_gwn\n", "\n", "help(my_GWN)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Make sure you execute this cell to enable the widget!\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "execution": {}, "tags": [ "hide-input" ] }, "outputs": [], "source": [ "# @markdown Make sure you execute this cell to enable the widget!\n", "\n", "my_layout.width = '450px'\n", "@widgets.interact(\n", " mu_gwn=widgets.FloatSlider(200., min=100., max=300., step=5.,\n", " layout=my_layout),\n", " sig_gwn=widgets.FloatSlider(2.5, min=0., max=5., step=.5,\n", " layout=my_layout)\n", ")\n", "\n", "\n", "def diff_GWN_to_LIF(mu_gwn, sig_gwn):\n", " pars = default_pars(T=100.)\n", " I_GWN = my_GWN(pars, mu=mu_gwn, sig=sig_gwn)\n", " v, sp = run_LIF(pars, Iinj=I_GWN)\n", " plt.figure(figsize=(12, 4))\n", " plt.subplot(121)\n", " plt.plot(pars['range_t'][::3], I_GWN[::3], 'b')\n", " plt.xlabel('Time (ms)')\n", " plt.ylabel(r'$I_{GWN}$ (pA)')\n", " plt.subplot(122)\n", " plot_volt_trace(pars, v, sp)\n", " plt.tight_layout()\n", " plt.show()" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "execution": {} }, "source": [ "[*Click for solution*](https://github.com/NeuromatchAcademy/course-content/tree/main/tutorials/W2D3_BiologicalNeuronModels/solutions/W2D3_Tutorial1_Solution_2de5d8a9.py)\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Submit your feedback\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "execution": {}, "tags": [ "hide-input" ] }, "outputs": [], "source": [ "# @title Submit your feedback\n", "content_review(f\"{feedback_prefix}_Gaussian_White_Noise_Interactive_Demo_and_Discussion\")" ] }, { "cell_type": "markdown", "metadata": { "execution": {} }, "source": [ "### Think! 2.2: Analyzing GWN Effects on Spiking\n", "- As we increase the input average ($\\mu$) or the input fluctuation ($\\sigma$), the spike count changes. How much can we increase the spike count, and what might be the relationship between GWN mean/std or DC value and spike count?\n", "\n", "- We have seen above that when we inject DC, the neuron spikes in a regular manner (clock-like), and this regularity is reduced when GWN is injected. The question is, how irregular can we make the neurons spiking by changing the parameters of the GWN?\n", "\n", "We will see the answers to these questions in the next section but discuss first!\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Submit your feedback\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "execution": {}, "tags": [ "hide-input" ] }, "outputs": [], "source": [ "# @title Submit your feedback\n", "content_review(f\"{feedback_prefix}_Analyzing_GWN_effects_on_spiking_Discussion\")" ] }, { "cell_type": "markdown", "metadata": { "execution": {} }, "source": [ "---\n", "# Section 3: Firing rate and spike time irregularity\n", "\n", "*Estimated timing to here from start of tutorial: 48 min*\n", "\n", "When we plot the output firing rate as a function of GWN mean or DC value, it is called the input-output transfer function of the neuron (so simply F-I curve).\n", "\n", "Spike regularity can be quantified as the **coefficient of variation (CV) of the interspike interval (ISI)**:\n", "\n", "\\begin{equation}\n", "\\text{CV}_{\\text{ISI}} = \\frac{std(\\text{ISI})}{mean(\\text{ISI})}\n", "\\end{equation}\n", "\n", "A Poisson train is an example of high irregularity, in which $\\textbf{CV}_{\\textbf{ISI}} \\textbf{= 1}$. And for a clocklike (regular) process we have $\\textbf{CV}_{\\textbf{ISI}} \\textbf{= 0}$ because of **std(ISI)=0**." ] }, { "cell_type": "markdown", "metadata": { "execution": {} }, "source": [ "## Interactive Demo 3A: F-I Explorer for different sig_gwn\n", "\n", "How does the F-I curve of the LIF neuron change as we increase the $\\sigma$ of the GWN? We can already expect that the F-I curve will be stochastic and the results will vary from one trial to another. But will there be any other change compared to the F-I curved measured using DC?\n", "\n", "Here's an interactive demo that shows how the F-I curve of a LIF neuron changes for different levels of fluctuation $\\sigma$.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Make sure you execute this cell to enable the widget!\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "execution": {}, "tags": [ "hide-input" ] }, "outputs": [], "source": [ "# @markdown Make sure you execute this cell to enable the widget!\n", "\n", "my_layout.width = '450px'\n", "@widgets.interact(\n", " sig_gwn=widgets.FloatSlider(3.0, min=0., max=6., step=0.5,\n", " layout=my_layout)\n", ")\n", "\n", "\n", "def diff_std_affect_fI(sig_gwn):\n", " pars = default_pars(T=1000.)\n", " I_mean = np.arange(100., 400., 10.)\n", " spk_count = np.zeros(len(I_mean))\n", " spk_count_dc = np.zeros(len(I_mean))\n", "\n", " for idx in range(len(I_mean)):\n", " I_GWN = my_GWN(pars, mu=I_mean[idx], sig=sig_gwn, myseed=2020)\n", " v, rec_spikes = run_LIF(pars, Iinj=I_GWN)\n", " v_dc, rec_sp_dc = run_LIF(pars, Iinj=I_mean[idx])\n", " spk_count[idx] = len(rec_spikes)\n", " spk_count_dc[idx] = len(rec_sp_dc)\n", "\n", " # Plot the F-I curve i.e. Output firing rate as a function of input mean.\n", " plt.figure()\n", " plt.plot(I_mean, spk_count, 'k',\n", " label=r'$\\sigma_{\\mathrm{GWN}}=%.2f$' % sig_gwn)\n", " plt.plot(I_mean, spk_count_dc, 'k--', alpha=0.5, lw=4, dashes=(2, 2),\n", " label='DC input')\n", " plt.ylabel('Spike count')\n", " plt.xlabel('Average injected current (pA)')\n", " plt.legend(loc='best')\n", " plt.show()" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "execution": {} }, "source": [ "[*Click for solution*](https://github.com/NeuromatchAcademy/course-content/tree/main/tutorials/W2D3_BiologicalNeuronModels/solutions/W2D3_Tutorial1_Solution_5aa4e3b0.py)\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Submit your feedback\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "execution": {}, "tags": [ "hide-input" ] }, "outputs": [], "source": [ "# @title Submit your feedback\n", "content_review(f\"{feedback_prefix}_F_I_explorer_Interactive_Demo_and_Discussion\")" ] }, { "cell_type": "markdown", "metadata": { "execution": {} }, "source": [ "## Coding Exercise 3: Compute $CV_{ISI}$ values\n", "\n", "As shown above, the F-I curve becomes smoother while increasing the amplitude of the fluctuation ($\\sigma$). In addition, the fluctuation can also change the irregularity of the spikes. Let's investigate the effect of $\\mu=250$ with $\\sigma=0.5$ vs $\\sigma=3$.\n", "\n", "Fill in the code below to compute ISI, then plot the histogram of the ISI and compute the $CV_{ISI}$. Note that, you can use np.diff to calculate ISI." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": {} }, "outputs": [], "source": [ "def isi_cv_LIF(spike_times):\n", " \"\"\"\n", " Calculates the interspike intervals (isi) and\n", " the coefficient of variation (cv) for a given spike_train\n", "\n", " Args:\n", " spike_times : (n, ) vector with the spike times (ndarray)\n", "\n", " Returns:\n", " isi : (n-1,) vector with the inter-spike intervals (ms)\n", " cv : coefficient of variation of isi (float)\n", "\n", " \"\"\"\n", " ########################################################################\n", " ## TODO for students: compute the membrane potential v, spike train sp #\n", " # Fill out function and remove\n", " raise NotImplementedError('Student Exercise: calculate the isi and the cv!')\n", " ########################################################################\n", " if len(spike_times) >= 2:\n", " # Compute isi\n", " isi = ...\n", " # Compute cv\n", " cv = ...\n", " else:\n", " isi = np.nan\n", " cv = np.nan\n", "\n", " return isi, cv\n", "\n", "\n", "# Set parameters\n", "pars = default_pars(T=1000.)\n", "mu_gwn = 250\n", "sig_gwn1 = 0.5\n", "sig_gwn2 = 3.0\n", "\n", "# Run LIF model for sigma = 0.5\n", "I_GWN1 = my_GWN(pars, mu=mu_gwn, sig=sig_gwn1, myseed=2020)\n", "_, sp1 = run_LIF(pars, Iinj=I_GWN1)\n", "\n", "# Run LIF model for sigma = 3\n", "I_GWN2 = my_GWN(pars, mu=mu_gwn, sig=sig_gwn2, myseed=2020)\n", "_, sp2 = run_LIF(pars, Iinj=I_GWN2)\n", "\n", "# Compute ISIs/CV\n", "isi1, cv1 = isi_cv_LIF(sp1)\n", "isi2, cv2 = isi_cv_LIF(sp2)\n", "\n", "# Visualize\n", "my_hists(isi1, isi2, cv1, cv2, sig_gwn1, sig_gwn2)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "execution": {} }, "source": [ "[*Click for solution*](https://github.com/NeuromatchAcademy/course-content/tree/main/tutorials/W2D3_BiologicalNeuronModels/solutions/W2D3_Tutorial1_Solution_27d69c89.py)\n", "\n", "*Example output:*\n", "\n", " \n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Submit your feedback\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "execution": {}, "tags": [ "hide-input" ] }, "outputs": [], "source": [ "# @title Submit your feedback\n", "content_review(f\"{feedback_prefix}_Compute_CV_ISI_Exercise\")" ] }, { "cell_type": "markdown", "metadata": { "execution": {} }, "source": [ "## Interactive Demo 3B: Spike irregularity explorer for different sig_gwn\n", "\n", "In the above illustration, we see that the CV of inter-spike-interval (ISI) distribution depends on $\\sigma$ of GWN. What about the mean of GWN, should that also affect the CV$_{\\rm ISI}$? If yes, how? Does the efficacy of $\\sigma$ in increasing the CV$_{\\rm ISI}$ depend on $\\mu$?\n", "\n", "In the following interactive demo, you will examine how different levels of fluctuation $\\sigma$ affect the CVs for different average injected currents ($\\mu$).\n", "\n", "1. Does the standard deviation of the injected current affect the F-I curve in any qualitative manner?\n", "2. Why does increasing the mean of GWN reduce the CV$_{\\rm ISI}$?\n", "3. If you plot spike count (or rate) vs. CV$_{\\rm ISI}$, should there be a relationship between the two? Try out yourself." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Make sure you execute this cell to enable the widget!\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "execution": {}, "tags": [ "hide-input" ] }, "outputs": [], "source": [ "# @markdown Make sure you execute this cell to enable the widget!\n", "\n", "my_layout.width = '450px'\n", "@widgets.interact(\n", " sig_gwn=widgets.FloatSlider(0.0, min=0., max=10.,\n", " step=0.5, layout=my_layout)\n", ")\n", "\n", "\n", "def diff_std_affect_fI(sig_gwn):\n", " pars = default_pars(T=1000.)\n", " I_mean = np.arange(100., 400., 20)\n", " spk_count = np.zeros(len(I_mean))\n", " cv_isi = np.empty(len(I_mean))\n", "\n", " for idx in range(len(I_mean)):\n", " I_GWN = my_GWN(pars, mu=I_mean[idx], sig=sig_gwn)\n", " v, rec_spikes = run_LIF(pars, Iinj=I_GWN)\n", " spk_count[idx] = len(rec_spikes)\n", " if len(rec_spikes) > 3:\n", " isi = np.diff(rec_spikes)\n", " cv_isi[idx] = np.std(isi) / np.mean(isi)\n", "\n", " # Plot the F-I curve i.e. Output firing rate as a function of input mean.\n", " plt.figure()\n", " plt.plot(I_mean[spk_count > 5], cv_isi[spk_count > 5], 'bo', alpha=0.5)\n", " plt.xlabel('Average injected current (pA)')\n", " plt.ylabel(r'Spike irregularity ($\\mathrm{CV}_\\mathrm{ISI}$)')\n", " plt.ylim(-0.1, 1.5)\n", " plt.grid(True)\n", " plt.show()" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "execution": {} }, "source": [ "[*Click for solution*](https://github.com/NeuromatchAcademy/course-content/tree/main/tutorials/W2D3_BiologicalNeuronModels/solutions/W2D3_Tutorial1_Solution_c6f1c4a2.py)\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Submit your feedback\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "execution": {}, "tags": [ "hide-input" ] }, "outputs": [], "source": [ "# @title Submit your feedback\n", "content_review(f\"{feedback_prefix}_Spike_Irregularity_Interactive_Demo_and_Discussion\")" ] }, { "cell_type": "markdown", "metadata": { "execution": {} }, "source": [ "---\n", "# Summary\n", "\n", "*Estimated timing of tutorial: 1 hour, 10 min*\n", "\n", "Congratulations! You've just built a leaky integrate-and-fire (LIF) neuron model from scratch, and studied its dynamics in response to various types of inputs, having:\n", "\n", "- simulated the LIF neuron model\n", "\n", "- driven the LIF neuron with external inputs, such as direct current and Gaussian white noise\n", "\n", "- studied how different inputs affect the LIF neuron's output (firing rate and spike time irregularity),\n", "\n", "with a special focus on low rate and irregular firing regime to mimic real cortical neurons. The next tutorial will look at how spiking statistics may be influenced by a neuron's input statistics.\n", "\n", "If you have extra time, look at the bonus sections below to explore a different type of noise input and learn about extensions to integrate-and-fire models.\n" ] }, { "cell_type": "markdown", "metadata": { "execution": {} }, "source": [ "---\n", "# Bonus" ] }, { "cell_type": "markdown", "metadata": { "execution": {} }, "source": [ "## Bonus Section 1: Ornstein-Uhlenbeck Process\n", "\n", "When a neuron receives spiking input, the synaptic current is Shot Noise -- which is a kind of colored noise and the spectrum of the noise determined by the synaptic kernel time constant. That is, a neuron is driven by **colored noise** and not GWN.\n", "\n", "We can model colored noise using the Ornstein-Uhlenbeck process - filtered white noise." ] }, { "cell_type": "markdown", "metadata": { "execution": {} }, "source": [ "We next study if the input current is temporally correlated and is modeled as an Ornstein-Uhlenbeck process $\\eta(t)$, i.e., low-pass filtered GWN with a time constant $\\tau_{\\eta}$:\n", "\n", "\\begin{equation}\n", "\\tau_\\eta \\frac{d}{dt}\\eta(t) = \\mu-\\eta(t) + \\sigma_\\eta\\sqrt{2\\tau_\\eta}\\xi(t)\n", "\\end{equation}\n", "\n", "**Hint:** An OU process as defined above has\n", "\n", "\\begin{equation}\n", "\\mathbb{E}[\\eta(t)]=\\mu\n", "\\end{equation}\n", "\n", "and autocovariance\n", "\n", "\\begin{equation}\n", "[\\eta(t)\\eta(t+\\tau)]=\\sigma_\\eta^2e^{-|t-\\tau|/\\tau_\\eta}\n", "\\end{equation}\n", "\n", "which can be used to check your code." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Execute this cell to get helper function my_OU\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "execution": {}, "tags": [ "hide-input" ] }, "outputs": [], "source": [ "# @markdown Execute this cell to get helper function my_OU\n", "\n", "\n", "def my_OU(pars, mu, sig, myseed=False):\n", " \"\"\"\n", " Function that produces Ornstein-Uhlenbeck input\n", "\n", " Args:\n", " pars : parameter dictionary\n", " sig : noise amplitute\n", " myseed : random seed. int or boolean\n", "\n", " Returns:\n", " I_ou : Ornstein-Uhlenbeck input current\n", " \"\"\"\n", "\n", " # Retrieve simulation parameters\n", " dt, range_t = pars['dt'], pars['range_t']\n", " Lt = range_t.size\n", " tau_ou = pars['tau_ou'] # [ms]\n", "\n", " # set random seed\n", " if myseed:\n", " np.random.seed(seed=myseed)\n", " else:\n", " np.random.seed()\n", "\n", " # Initialize\n", " noise = np.random.randn(Lt)\n", " I_ou = np.zeros(Lt)\n", " I_ou = noise * sig\n", "\n", " # generate OU\n", " for it in range(Lt-1):\n", " I_ou[it+1] = I_ou[it] + (dt / tau_ou) * (mu - I_ou[it]) + np.sqrt(2 * dt / tau_ou) * sig * noise[it + 1]\n", "\n", " return I_ou\n", "\n", "\n", "help(my_OU)" ] }, { "cell_type": "markdown", "metadata": { "execution": {} }, "source": [ "### Bonus Interactive Demo 1: LIF Explorer with OU input\n", "In the following, we will check how a neuron responds to a noisy current that follows the statistics of an OU process.\n", "\n", "- How does the OU type input change neuron responsiveness?\n", "- What do you think will happen to the spike pattern and rate if you increased or decreased the time constant of the OU process?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Remember to enable the widget by running the cell!\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "execution": {}, "tags": [ "hide-input" ] }, "outputs": [], "source": [ "# @markdown Remember to enable the widget by running the cell!\n", "\n", "my_layout.width = '450px'\n", "@widgets.interact(\n", " tau_ou=widgets.FloatSlider(10.0, min=5., max=20.,\n", " step=2.5, layout=my_layout),\n", " sig_ou=widgets.FloatSlider(10.0, min=5., max=40.,\n", " step=2.5, layout=my_layout),\n", " mu_ou=widgets.FloatSlider(190.0, min=180., max=220.,\n", " step=2.5, layout=my_layout)\n", ")\n", "\n", "\n", "def LIF_with_OU(tau_ou=10., sig_ou=40., mu_ou=200.):\n", " pars = default_pars(T=1000.)\n", " pars['tau_ou'] = tau_ou # [ms]\n", "\n", " I_ou = my_OU(pars, mu_ou, sig_ou)\n", " v, sp = run_LIF(pars, Iinj=I_ou)\n", "\n", " plt.figure(figsize=(12, 4))\n", " plt.subplot(121)\n", " plt.plot(pars['range_t'], I_ou, 'b', lw=1.0)\n", " plt.xlabel('Time (ms)')\n", " plt.ylabel(r'$I_{\\mathrm{OU}}$ (pA)')\n", " plt.subplot(122)\n", " plot_volt_trace(pars, v, sp)\n", " plt.tight_layout()\n", " plt.show()" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "execution": {} }, "source": [ "[*Click for solution*](https://github.com/NeuromatchAcademy/course-content/tree/main/tutorials/W2D3_BiologicalNeuronModels/solutions/W2D3_Tutorial1_Solution_a0f797af.py)\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Submit your feedback\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "execution": {}, "tags": [ "hide-input" ] }, "outputs": [], "source": [ "# @title Submit your feedback\n", "content_review(f\"{feedback_prefix}_LIF_explorer_with_OU_input_Bonus_Interactive_Demo_and_Discussion\")" ] }, { "cell_type": "markdown", "metadata": { "execution": {} }, "source": [ "## Bonus Section 2: Generalized Integrate-and-Fire models\n", "\n", "LIF model is not the only abstraction of real neurons. If you want to learn about more realistic types of neuronal models, watch the Bonus Video!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Video 3 (Bonus): Extensions to Integrate-and-Fire models\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "execution": {}, "tags": [ "remove-input" ] }, "outputs": [], "source": [ "# @title Video 3 (Bonus): Extensions to Integrate-and-Fire models\n", "from ipywidgets import widgets\n", "from IPython.display import YouTubeVideo\n", "from IPython.display import IFrame\n", "from IPython.display import display\n", "\n", "\n", "class PlayVideo(IFrame):\n", " def __init__(self, id, source, page=1, width=400, height=300, **kwargs):\n", " self.id = id\n", " if source == 'Bilibili':\n", " src = f'https://player.bilibili.com/player.html?bvid={id}&page={page}'\n", " elif source == 'Osf':\n", " src = f'https://mfr.ca-1.osf.io/render?url=https://osf.io/download/{id}/?direct%26mode=render'\n", " super(PlayVideo, self).__init__(src, width, height, **kwargs)\n", "\n", "\n", "def display_videos(video_ids, W=400, H=300, fs=1):\n", " tab_contents = []\n", " for i, video_id in enumerate(video_ids):\n", " out = widgets.Output()\n", " with out:\n", " if video_ids[i] == 'Youtube':\n", " video = YouTubeVideo(id=video_ids[i], width=W,\n", " height=H, fs=fs, rel=0)\n", " print(f'Video available at https://youtube.com/watch?v={video.id}')\n", " else:\n", " video = PlayVideo(id=video_ids[i], source=video_ids[i], width=W,\n", " height=H, fs=fs, autoplay=False)\n", " if video_ids[i] == 'Bilibili':\n", " print(f'Video available at https://www.bilibili.com/video/{video.id}')\n", " elif video_ids[i] == 'Osf':\n", " print(f'Video available at https://osf.io/{video.id}')\n", " display(video)\n", " tab_contents.append(out)\n", " return tab_contents\n", "\n", "\n", "video_ids = [('Youtube', 'G0b6wLhuQxE'), ('Bilibili', 'BV1c54y1B7d7')]\n", "tab_contents = display_videos(video_ids, W=730, H=410)\n", "tabs = widgets.Tab()\n", "tabs.children = tab_contents\n", "for i in range(len(tab_contents)):\n", " tabs.set_title(i, video_ids[i])\n", "display(tabs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Submit your feedback\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "execution": {}, "tags": [ "hide-input" ] }, "outputs": [], "source": [ "# @title Submit your feedback\n", "content_review(f\"{feedback_prefix}_Extension_to_Integrate_and_Fire_Bonus_Video\")" ] } ], "metadata": { "colab": { "collapsed_sections": [], "include_colab_link": true, "name": "W2D3_Tutorial1", "provenance": [], "toc_visible": true }, "kernel": { "display_name": "Python 3", "language": "python", "name": "python3" }, "kernelspec": { "display_name": "Python 3", "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.9.17" } }, "nbformat": 4, "nbformat_minor": 0 }