Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
345 changes: 345 additions & 0 deletions notebooks/experiment_example.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,345 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"toc_visible": true,
"authorship_tag": "ABX9TyMvW7lhxBXG2gi+iXDgU0PT",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/Olimaol/CompNeuroPy/blob/olimaol_develop/notebooks/experiment_example.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"#Install CompNeuroPy and ANNarchy"
],
"metadata": {
"id": "MQwRmyFdmUuD"
}
},
{
"cell_type": "code",
"source": [
"!pip install CompNeuroPy\n",
"!git clone https://github.com/ANNarchy/ANNarchy && cd ANNarchy && git checkout develop && pip install .\n",
"!rm -rf ANNarchy"
],
"metadata": {
"collapsed": true,
"id": "VGC0ujzTm-z3"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"#Imports and setup ANNarchy timestep"
],
"metadata": {
"id": "Mcoz2oq76flU"
}
},
{
"cell_type": "code",
"source": [
"from IPython.display import Image, display\n",
"from CompNeuroPy import (\n",
" CompNeuroExp,\n",
" CompNeuroMonitors,\n",
" CompNeuroModel,\n",
" current_step,\n",
" current_ramp,\n",
" PlotRecordings,\n",
")\n",
"from CompNeuroPy.full_models import HHmodelBischop\n",
"from ANNarchy import dt, setup, get_population\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"setup(dt=0.01)"
],
"metadata": {
"id": "bMhApHy-6iJ3"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Define the Experiment\n",
"A single run resets the model, sets the leakage potential of population 0 and runs a current ramp and current step."
],
"metadata": {
"id": "pyf2gSTyN80I"
}
},
{
"cell_type": "code",
"source": [
"class MyExp(CompNeuroExp):\n",
"\n",
" def run(self, model: CompNeuroModel, E_L: float):\n",
" # PREPARE RUN\n",
" self.reset()\n",
" self.monitors.start()\n",
" # SET E_L PARAMETER\n",
" get_population(model.populations[0]).E_L = E_L\n",
" # SIMULATION\n",
" ret_current_ramp = current_ramp(pop=model.populations[0], a0=0, a1=100, dur=1000, n=50)\n",
" self.reset(parameters=False)\n",
" ret_current_step = current_step(pop=model.populations[0], t1=500, t2=500, a1=0, a2=50)\n",
" # OPTIONAL DATA OF RUN\n",
" self.data[\"population_name\"] = model.populations[0]\n",
" self.data[\"time_step\"] = dt()\n",
" self.data[\"current_arr\"] = np.concatenate([ret_current_ramp[\"current_arr\"], ret_current_step[\"current_arr\"]])\n",
" # RETURN RESULTS\n",
" return self.results()"
],
"metadata": {
"id": "8coZOl3oOc1B"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Create/Compile Model\n",
"The model is a single population (consisting of 1 neuron) of a Hodgkin & Huxley neuron. The `HHmodelBischop` class is a child of the `CompNeuroModel` class with a predefined model creation function."
],
"metadata": {
"id": "XCbqszREO0U2"
}
},
{
"cell_type": "code",
"source": [
"model = HHmodelBischop()\n",
"model.populations"
],
"metadata": {
"id": "gHmfSnNlO2WP"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Initialize the experiment\n",
"Recording the membrane potential of the models first population."
],
"metadata": {
"id": "EQL2vn40PA_O"
}
},
{
"cell_type": "code",
"source": [
"my_exp = MyExp(monitors=CompNeuroMonitors({model.populations[0]: [\"v\"]}))"
],
"metadata": {
"id": "I6SphV4qPHTU"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Set the \"reset-state\" for the experiment\n",
"Set the membrane potential of the model to -90 mV."
],
"metadata": {
"id": "bJn_adfcPRz5"
}
},
{
"cell_type": "code",
"source": [
"print(f\"Compilation state v = {get_population(model.populations[0]).v}\")\n",
"get_population(model.populations[0]).v = -90.0\n",
"print(f\"Changed state v = {get_population(model.populations[0]).v}\")\n",
"my_exp.store_model_state(compartment_list=model.populations)"
],
"metadata": {
"id": "wyE8p0SmPZ0c"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Run the experiment twice with different leakage potentials"
],
"metadata": {
"id": "QOuRAfGhPeyT"
}
},
{
"cell_type": "code",
"source": [
"results_run1: CompNeuroExp._ResultsCl = my_exp.run(model=model, E_L=-68.0)\n",
"results_run2: CompNeuroExp._ResultsCl = my_exp.run(model=model, E_L=-90.0)"
],
"metadata": {
"id": "PKqJScoJPf7q"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# PlotRecordings\n",
"This allows to easily get overview plots of the recordings of a single recording chunk."
],
"metadata": {
"id": "Xg_52O9uPkgd"
}
},
{
"cell_type": "code",
"source": [
"for chunk in [0,1]:\n",
" PlotRecordings(\n",
" figname=f\"example_experiment_chunk_{chunk}.png\",\n",
" recordings=results_run1.recordings,\n",
" recording_times=results_run1.recording_times,\n",
" chunk=chunk,\n",
" shape=(1, 1),\n",
" plan={\n",
" \"position\": [1],\n",
" \"compartment\": [results_run1.data[\"population_name\"]],\n",
" \"variable\": [\"v\"],\n",
" \"format\": [\"line\"],\n",
" },\n",
" )"
],
"metadata": {
"id": "ftBPfNZgPmok"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"display(Image(filename='example_experiment_chunk_0.png', height=700))"
],
"metadata": {
"id": "o8Lvap-8QFIL"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"display(Image(filename='example_experiment_chunk_1.png', height=700))"
],
"metadata": {
"id": "p4-rWESeQJkm"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Get Data and Time Arrays\n",
"Each experiment run created 2 recording chunks. They all start at time 0 (because of resetting the model, see above). The function combine_chunks() can be used to combine the chunks into a single recording time and value array."
],
"metadata": {
"id": "OkEO6pyePzmT"
}
},
{
"cell_type": "code",
"source": [
"time_arr1, data_arr1 = results_run1.recording_times.combine_chunks(\n",
" recordings=results_run1.recordings,\n",
" recording_data_str=f\"{results_run1.data['population_name']};v\",\n",
" mode=\"consecutive\",\n",
")\n",
"time_arr2, data_arr2 = results_run2.recording_times.combine_chunks(\n",
" recordings=results_run2.recordings,\n",
" recording_data_str=f\"{results_run2.data['population_name']};v\",\n",
" mode=\"consecutive\",\n",
")\n",
"current_arr = results_run1.data[\"current_arr\"]"
],
"metadata": {
"id": "SoiA-7cMP2bj"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Plot Data of Both Runs"
],
"metadata": {
"id": "mUso4pycQTLf"
}
},
{
"cell_type": "code",
"source": [
"plt.figure()\n",
"plt.subplot(211)\n",
"plt.plot(time_arr1, data_arr1, label=\"E_L = -68.0\")\n",
"plt.plot(time_arr2, data_arr2, label=\"E_L = -90.0\")\n",
"plt.plot(\n",
" [time_arr1[0], time_arr1[-1]], [-90, -90], ls=\"dotted\", label=\"initial v = -90.0\"\n",
")\n",
"plt.legend()\n",
"plt.ylabel(\"Membrane potential [mV]\")\n",
"plt.subplot(212)\n",
"plt.plot(time_arr1, current_arr, \"k--\")\n",
"plt.ylabel(\"Input current\")\n",
"plt.xlabel(\"Time [ms]\")\n",
"plt.tight_layout()\n",
"plt.savefig(\"example_experiment_combined.png\")"
],
"metadata": {
"id": "61hXVuUDQYJ2"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"display(Image(filename='example_experiment_chunk_1.png', height=700))"
],
"metadata": {
"id": "aMVQLAi-QcFG"
},
"execution_count": null,
"outputs": []
}
]
}
Loading