{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"execution": {},
"id": "view-in-github"
},
"source": [
"
"
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"# Tutorial 2: \"How\" models\n",
"**Week 1, Day 1: Model Types**\n",
"\n",
"**By Neuromatch Academy**\n",
"\n",
"__Content creators:__ Matt Laporte, Byron Galbraith, Konrad Kording\n",
"\n",
"__Content reviewers:__ Dalin Guo, Aishwarya Balwani, Madineh Sarvestani, Maryam Vaziri-Pashkam, Michael Waskom, Ella Batty"
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"**Our 2021 Sponsors, including Presenting Sponsor Facebook Reality Labs**\n",
"\n",
"

\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"## Interactive Demo 1: Linear-IF neuron\n",
"Like last time, you can now explore how various parametes of the LIF model influence the ISI distribution. Specifically, you can vary `alpha`, which is input scaling factor, and `rate`, which is the mean rate of incoming spikes.\n",
"\n",
"\n",
"1. What is the spiking pattern of this model?\n",
"2. What effect does raising or lowering alpha have?\n",
"3. What effect does raising or lowering the rate have?\n",
"4. Does the distribution of ISIs ever look like what you observed in the data in Tutorial 1?\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"execution": {}
},
"outputs": [],
"source": [
"# @markdown You don't need to worry about how the code works – but you do need to **run the cell** to enable the sliders.\n",
"\n",
"def _lif_neuron(n_steps=1000, alpha=0.01, rate=10):\n",
" exc = stats.poisson(rate).rvs(n_steps)\n",
" v = np.zeros(n_steps)\n",
" spike_times = []\n",
" for i in range(1, n_steps):\n",
" dv = alpha * exc[i]\n",
" v[i] = v[i-1] + dv\n",
" if v[i] > 1:\n",
" spike_times.append(i)\n",
" v[i] = 0\n",
" return v, spike_times\n",
"\n",
"@widgets.interact(\n",
" alpha=widgets.FloatLogSlider(0.01, min=-2, max=-1),\n",
" rate=widgets.IntSlider(10, min=5, max=20)\n",
")\n",
"def plot_lif_neuron(alpha=0.01, rate=10):\n",
" v, spike_times = _lif_neuron(2000, alpha, rate)\n",
"\n",
" plot_neuron_stats(v, spike_times)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"execution": {}
},
"source": [
"[*Click for solution*](https://github.com/NeuromatchAcademy/course-content/tree/master//tutorials/W1D1_ModelTypes/solutions/W1D1_Tutorial2_Solution_2af538db.py)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"execution": {}
},
"outputs": [],
"source": [
"# @title Video 2: Linear-IF models\n",
"from ipywidgets import widgets\n",
"\n",
"out2 = widgets.Output()\n",
"with out2:\n",
" from IPython.display import IFrame\n",
" class BiliVideo(IFrame):\n",
" def __init__(self, id, page=1, width=400, height=300, **kwargs):\n",
" self.id=id\n",
" src = 'https://player.bilibili.com/player.html?bvid={0}&page={1}'.format(id, page)\n",
" super(BiliVideo, self).__init__(src, width, height, **kwargs)\n",
"\n",
" video = BiliVideo(id=\"BV1iZ4y1u7en\", width=854, height=480, fs=1)\n",
" print('Video available at https://www.bilibili.com/video/{0}'.format(video.id))\n",
" display(video)\n",
"\n",
"out1 = widgets.Output()\n",
"with out1:\n",
" from IPython.display import YouTubeVideo\n",
" video = YouTubeVideo(id=\"QBD7kulhg4U\", width=854, height=480, fs=1, rel=0)\n",
" print('Video available at https://youtube.com/watch?v=' + video.id)\n",
" display(video)\n",
"\n",
"out = widgets.Tab([out1, out2])\n",
"out.set_title(0, 'Youtube')\n",
"out.set_title(1, 'Bilibili')\n",
"\n",
"display(out)"
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"---\n",
"# Section 2: Inhibitory signals\n",
"\n",
"*Estimated timing to here from start of tutorial: 20 min*\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"\n",
"Our linear integrate-and-fire neuron from the previous section was indeed able to produce spikes. However, our ISI histogram doesn't look much like empirical ISI histograms seen in Tutorial 1, which had an exponential-like shape. What is our model neuron missing, given that it doesn't behave like a real neuron?\n",
"\n",
"In the previous model we only considered excitatory behavior -- the only way the membrane potential could decrease was upon a spike event. We know, however, that there are other factors that can drive $V_m$ down. First is the natural tendency of the neuron to return to some steady state or resting potential. We can update our previous model as follows:\n",
"\n",
"\\begin{align}\n",
" dV_m = -{\\beta}V_m + {\\alpha}I\n",
"\\end{align}\n",
"\n",
"where $V_m$ is the current membrane potential and $\\beta$ is some leakage factor. This is a basic form of the popular Leaky Integrate-and-Fire model neuron (for a more detailed discussion of the LIF Neuron, see Bonus Section 2 and the Biological Neuron Models day later in this course).\n",
"\n",
"We also know that in addition to excitatory presynaptic neurons, we can have inhibitory presynaptic neurons as well. We can model these inhibitory neurons with another Poisson random variable:\n",
"\n",
"\\begin{align}\n",
"I = I_{\\mathrm{exc}} - I_{\\mathrm{inh}} \\\\\n",
"I_{\\mathrm{exc}} \\sim \\mathrm{Poisson}(\\lambda_{\\mathrm{exc}}) \\\\\n",
"I_{\\mathrm{inh}} \\sim \\mathrm{Poisson}(\\lambda_{\\mathrm{inh}})\n",
"\\end{align}\n",
"\n",
"where $\\lambda_{\\mathrm{exc}}$ and $\\lambda_{\\mathrm{inh}}$ are the average spike rates (per timestep) of the excitatory and inhibitory presynaptic neurons, respectively."
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"### Coding Exercise 2: Compute $dV_m$ with inhibitory signals\n",
"\n",
"For your second exercise, you will again write the code to compute the change in voltage $dV_m$, though now of the LIF model neuron described above. Like last time, the rest of the code needed to handle the neuron dynamics are provided for you, so you just need to fill in a definition for `dv` below.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {}
},
"outputs": [],
"source": [
"def lif_neuron_inh(n_steps=1000, alpha=0.5, beta=0.1, exc_rate=10, inh_rate=10):\n",
" \"\"\" Simulate a simplified leaky integrate-and-fire neuron with both excitatory\n",
" and inhibitory inputs.\n",
"\n",
" Args:\n",
" n_steps (int): The number of time steps to simulate the neuron's activity.\n",
" alpha (float): The input scaling factor\n",
" beta (float): The membrane potential leakage factor\n",
" exc_rate (int): The mean rate of the incoming excitatory spikes\n",
" inh_rate (int): The mean rate of the incoming inhibitory spikes\n",
" \"\"\"\n",
"\n",
" # precompute Poisson samples for speed\n",
" exc = stats.poisson(exc_rate).rvs(n_steps)\n",
" inh = stats.poisson(inh_rate).rvs(n_steps)\n",
"\n",
" v = np.zeros(n_steps)\n",
" spike_times = []\n",
"\n",
" ###############################################################################\n",
" # Students: compute dv, then comment out or remove the next line\n",
" raise NotImplementedError(\"Excercise: compute the change in membrane potential\")\n",
" ################################################################################\n",
"\n",
" for i in range(1, n_steps):\n",
"\n",
" dv = ...\n",
"\n",
" v[i] = v[i-1] + dv\n",
" if v[i] > 1:\n",
" spike_times.append(i)\n",
" v[i] = 0\n",
"\n",
" return v, spike_times\n",
"\n",
"# Set random seed (for reproducibility)\n",
"np.random.seed(12)\n",
"\n",
"# Model LIF neuron\n",
"v, spike_times = lif_neuron_inh()\n",
"\n",
"# Visualize\n",
"plot_neuron_stats(v, spike_times)"
]
},
{
"cell_type": "markdown",
"metadata": {
"cellView": "both",
"colab_type": "text",
"execution": {}
},
"source": [
"[*Click for solution*](https://github.com/NeuromatchAcademy/course-content/tree/master//tutorials/W1D1_ModelTypes/solutions/W1D1_Tutorial2_Solution_6bd84e18.py)\n",
"\n",
"*Example output:*\n",
"\n",
"
\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"## Interactive Demo 2: LIF + inhibition neuron\n",
"\n",
"As in Interactive Demo 1, you can play with the parameters of the input to our LIF neuron and visualize what happens. Here, in addition to controlling `alpha`, which scales the input, you can also control `beta`, which is a leakage factor on the voltage, `exc_rate`, which is the mean rate of the excitatory presynaptic neurons, and `inh_rate`, which is the mean rate of the inhibitory presynaptic neurons.\n",
"\n",
"1. What effect does raising the excitatory rate have?\n",
"2. What effect does raising the inhibitory rate have?\n",
"3. What if you raise both the excitatory and inhibitory rate?\n",
"4. Does the distribution of ISIs ever look like what you observed in the data in Tutorial 1?\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"execution": {}
},
"outputs": [],
"source": [
"#@title\n",
"#@markdown **Run the cell** to enable the sliders.\n",
"def _lif_neuron_inh(n_steps=1000, alpha=0.5, beta=0.1, exc_rate=10, inh_rate=10):\n",
" \"\"\" Simulate a simplified leaky integrate-and-fire neuron with both excitatory\n",
" and inhibitory inputs.\n",
"\n",
" Args:\n",
" n_steps (int): The number of time steps to simulate the neuron's activity.\n",
" alpha (float): The input scaling factor\n",
" beta (float): The membrane potential leakage factor\n",
" exc_rate (int): The mean rate of the incoming excitatory spikes\n",
" inh_rate (int): The mean rate of the incoming inhibitory spikes\n",
" \"\"\"\n",
" # precompute Poisson samples for speed\n",
" exc = stats.poisson(exc_rate).rvs(n_steps)\n",
" inh = stats.poisson(inh_rate).rvs(n_steps)\n",
"\n",
" v = np.zeros(n_steps)\n",
" spike_times = []\n",
" for i in range(1, n_steps):\n",
" dv = -beta * v[i-1] + alpha * (exc[i] - inh[i])\n",
" v[i] = v[i-1] + dv\n",
" if v[i] > 1:\n",
" spike_times.append(i)\n",
" v[i] = 0\n",
"\n",
" return v, spike_times\n",
"\n",
"@widgets.interact(alpha=widgets.FloatLogSlider(0.5, min=-1, max=1),\n",
" beta=widgets.FloatLogSlider(0.1, min=-1, max=0),\n",
" exc_rate=widgets.IntSlider(12, min=10, max=20),\n",
" inh_rate=widgets.IntSlider(12, min=10, max=20))\n",
"def plot_lif_neuron(alpha=0.5, beta=0.1, exc_rate=10, inh_rate=10):\n",
" v, spike_times = _lif_neuron_inh(2000, alpha, beta, exc_rate, inh_rate)\n",
" plot_neuron_stats(v, spike_times)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"execution": {}
},
"source": [
"[*Click for solution*](https://github.com/NeuromatchAcademy/course-content/tree/master//tutorials/W1D1_ModelTypes/solutions/W1D1_Tutorial2_Solution_570e738e.py)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"execution": {}
},
"outputs": [],
"source": [
"# @title Video 3: LIF + inhibition\n",
"from ipywidgets import widgets\n",
"\n",
"out2 = widgets.Output()\n",
"with out2:\n",
" from IPython.display import IFrame\n",
" class BiliVideo(IFrame):\n",
" def __init__(self, id, page=1, width=400, height=300, **kwargs):\n",
" self.id=id\n",
" src = 'https://player.bilibili.com/player.html?bvid={0}&page={1}'.format(id, page)\n",
" super(BiliVideo, self).__init__(src, width, height, **kwargs)\n",
"\n",
" video = BiliVideo(id=\"BV1nV41167mS\", width=854, height=480, fs=1)\n",
" print('Video available at https://www.bilibili.com/video/{0}'.format(video.id))\n",
" display(video)\n",
"\n",
"out1 = widgets.Output()\n",
"with out1:\n",
" from IPython.display import YouTubeVideo\n",
" video = YouTubeVideo(id=\"Aq7JrxRkn2w\", width=854, height=480, fs=1, rel=0)\n",
" print('Video available at https://youtube.com/watch?v=' + video.id)\n",
" display(video)\n",
"\n",
"out = widgets.Tab([out1, out2])\n",
"out.set_title(0, 'Youtube')\n",
"out.set_title(1, 'Bilibili')\n",
"\n",
"display(out)"
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"---\n",
"# Section 3: Reflecting on how models\n",
"\n",
"*Estimated timing to here from start of tutorial: 35 min*"
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"## Think! 3: Reflecting on how models\n",
"\n",
"Please discuss the following questions for around 10 minutes with your group:\n",
"\n",
"- Have you seen how models before?\n",
"- Have you ever done one?\n",
"- Why are how models useful?\n",
"- When are they possible? Does your field have how models?\n",
"- What do we learn from constructing them?"
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"# Summary\n",
"\n",
"*Estimated timing of tutorial: 45 minutes*\n",
"\n",
"In this tutorial we gained some intuition for the mechanisms that produce the observed behavior in our real neural data. First, we built a simple neuron model with excitatory input and saw that it's behavior, measured using the ISI distribution, did not match our real neurons. We then improved our model by adding leakiness and inhibitory input. The behavior of this balanced model was much closer to the real neural data."
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"---\n",
"# Notation\n",
"\n",
"\\begin{align}\n",
"V_m &\\quad \\text{membrane potential} \\\\\n",
"dV_m &\\quad \\text{change in membrane potential}\\\\\n",
"C_m &\\quad \\text{membrane capacitance}\\\\\n",
"I &\\quad \\text{input current}\\\\\n",
"R_m &\\quad \\text{membrane resistance}:\\\\\n",
"V_\\mathrm{rest} &\\quad \\text{resting potential}\\\\\n",
"\\alpha &\\quad \\text{scaling factor for input current}\\\\\n",
"\\beta &\\quad \\text{leakage factor}\\\\\n",
"\\lambda &\\quad \\text{average spike rate}\\\\\n",
"\\lambda_\\mathrm{exc} &\\quad \\text{average spike rate for excitatory neurons}\\\\\n",
"\\lambda_\\mathrm{inh} &\\quad \\text{average spike rate for inhibitory neurons}\\\\\n",
"\\end{align}"
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"---\n",
"# Bonus"
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"---\n",
"## Bonus Section 1: Why do neurons spike?\n",
"\n",
"A neuron stores energy in an electric field across its cell membrane, by controlling the distribution of charges (ions) on either side of the membrane. This energy is rapidly discharged to generate a spike when the field potential (or membrane potential) crosses a threshold. The membrane potential may be driven toward or away from this threshold, depending on inputs from other neurons: excitatory or inhibitory, respectively. The membrane potential tends to revert to a resting potential, for example due to the leakage of ions across the membrane, so that reaching the spiking threshold depends not only on the amount of input ever received following the last spike, but also the timing of the inputs.\n",
"\n",
"The storage of energy by maintaining a field potential across an insulating membrane can be modeled by a capacitor. The leakage of charge across the membrane can be modeled by a resistor. This is the basis for the leaky integrate-and-fire neuron model."
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"---\n",
"## Bonus Section 2: The LIF Model Neuron\n",
"\n",
"The full equation for the LIF neuron is\n",
"\n",
"\\begin{align}\n",
"C_{m}\\frac{dV_m}{dt} = -(V_m - V_{\\mathrm{rest}})/R_{m} + I\n",
"\\end{align}\n",
"\n",
"where $C_m$ is the membrane capacitance, $R_m$ is the membrane resistance, $V_{\\mathrm{rest}}$ is the resting potential, and $I$ is some input current (from other neurons, an electrode, ...).\n",
"\n",
"In our above examples we set many of these parameters to convenient values ($C_m = R_m = dt = 1$, $V_{\\mathrm{rest}} = 0$) to focus more on the general behavior of the model. However, these too can be manipulated to achieve different dynamics, or to ensure the dimensions of the problem are preserved between simulation units and experimental units (e.g. with $V_m$ given in millivolts, $R_m$ in megaohms, $t$ in milliseconds)."
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"include_colab_link": true,
"name": "W1D1_Tutorial2",
"provenance": [],
"toc_visible": true
},
"kernel": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 0
}