{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Complex Geometry\n", "\n", "Example notebook to load a complex geometry from a swc file, and add synapses on it.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import sinaps as sn\n", "\n", "import numpy as np\n", "import holoviews as hv\n", "import random" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating Neuron structure from swc file" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We choose a neuron from the [neuromorpho](http://neuromorpho.org) database:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#filename = \"http://neuromorpho.org/dableFiles/chang/CNG%20version/V247fs-MT-Untreated-56.CNG.swc\" \n", "filename = \"https://raw.githubusercontent.com/ngltr/sinaps/master/docs/sample_data/V247fs-MT-Untreated-56.CNG.swc\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use the function read_swc from io module to create a neuron from this file:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nrn = sn.io.read_swc(filename)\n", "nrn.plot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Accessing data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The neuron structure is stored using the [networkx](https://networkx.org) library, and you can access this underlying structure using:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nrn.graph" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For example :" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nrn.graph.edges" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nrn.graph.nodes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can access the section corresponding to an edge in the graph structure using the `section` attribute of edges" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nrn.graph[1][2]['section']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Setting custom radius" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The swc file contains the radius for each node of the dendrite. In the following, we redefine the radius of each section, assuming that this radius decreases at each bifurcation.\n", "We define for that a recursive function. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def set_radius(G,dep,a,factor):\n", " if G.degree(dep)>1:\n", " a=max(a*(G.degree(dep)-1)**(factor),0.1)\n", " for node in G[dep]:\n", " s = G[dep][node]['section']\n", " if s.a == 1000:# 1000 = marker\n", " s.a=a\n", " set_radius(G,node,a,factor)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nrn['dendrite'].a = 1000 #initialisation" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Setting radius of each node\n", "set_radius(nrn.graph,1,5,-0.5) " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nrn.plot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The neuron is plotted using the coordinates of the swc file, but we can force the recalculation of the layout to optimize the graph visualization:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nrn.plot.layout(force=True)\n", "nrn.plot().opts(node_size=0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By defaut, the layout is calculated using the Kamada-Kawai algorithm but you can use any other node positioning algorithm setting the `layout`parameter" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Modeling synapses\n", "\n", "### Selecting leaves\n", "\n", "We now randomly choose 10 leaves (excluding the soma) where we put synpases: we first remove the Hodgkin-Huxley channels, and add AMPA receptors." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We first find the node indices of the soma, to exclude it:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s = nrn['soma']\n", "[nrn.sections[s] for s in s]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then we use the `nrn.leaves` function to find leaves of the neuron structure" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "leaves = nrn.leaves() # Finding the leaves\n", "leaves.remove(2) # Removing the soma\n", "leaves.remove(3) # Removing the soma\n", "stim_leaves = random.sample(leaves,10) # getting randomly 10 leaves \n", "stim_sec = nrn[stim_leaves] # list of the stimulated sections" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "stim_sec.a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`stim_sec` is a list of sections, of type section_list, on which one can operate. For example, `stim_sec.a` will give the radius of all sections in `stim_sec`. It also allows the operations on all the series in list, such as adding channels, or changing the radius." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Setting up the channels\n", "We add Hodgkin Huxley channel everywhere in the neuron\n", "and AMPAR channel on previously chosen synapses.\n", "\n", "The `nrn[:]` command select all sections and allws to see or set parameters in one command\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nrn[:].clear_channels() #reset all channels\n", "nrn[:].add_channel(sn.channels.Hodgkin_Huxley()) # add HH channel everywhere\n", "stim_sec.clear_channels() #remove all channels on stim_sec sections \n", "stim_sec.add_channel(sn.channels.AMPAR(0.5,gampa=0.5),1) # add NMDAR channel on stim_sec sections" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plotting stimulated leaves\n", "\n", " " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nrn.add_node_data(type=0) #default node\n", "nrn.add_node_data(stim_leaves,type=2) # synapses are put in a different color, different size.\n", "plt = nrn.plot()\n", "plt.opts(\n", " node_size = hv.dim('type')*4,\n", " node_color = 'red', \n", "\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Running the simulation" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Initialisation of the simulation\n", "sim=sn.Simulation(nrn,dx=50)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Runing the simulation\n", "sim.run((0,100))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plotting" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Plotting the potential\n", "sim.plot()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sim.plot.I(sn.channels.Hodgkin_Huxley)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sim.plot.V_field()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Extracting some sections\n", "sim['soma'].plot()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sim.plot.I_field(sn.channels.AMPAR)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.4" } }, "nbformat": 4, "nbformat_minor": 4 }