{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tutorial 1: Single cell simulation with current injection (with BioNet)\n",
    "\n",
    "In this example we will build a network consisting of a single biophysically detailed cell. Then we will run a short simulation with a current injection at the soma of the cell, then look at the output of spikes, membrane potential and calcium flux.\n",
    "\n",
    "\n",
    "**Note** - scripts and files for running this tutorial can be found in the directory [sources/chapter01/](https://github.com/AllenInstitute/bmtk/tree/develop/docs/tutorial/sources/chapter01)\n",
    "\n",
    "**requirements:**\n",
    "* Python 2.7, 3.6+\n",
    "* bmtk\n",
    "* NEURON 7.4+"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Building the network\n",
    "\n",
    "First step is to use the bmtk Network Builder to create and save the network. First we instantiate a network with a name or our choosing (since throughout this tutorial we will use cell models that from the mouse cortex, let's call our network 'mcortex'). \n",
    "\n",
    "Once we have a network, we can add a single node by calling the add_nodes method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bmtk.builder.networks import NetworkBuilder\n",
    "\n",
    "net = NetworkBuilder('mcortex')\n",
    "net.add_nodes(\n",
    "    cell_name='Scnn1a_473845048',\n",
    "    potental='exc',\n",
    "    model_type='biophysical',\n",
    "    model_template='ctdb:Biophys1.hoc',\n",
    "    model_processing='aibs_perisomatic',\n",
    "    dynamics_params='472363762_fit.json',\n",
    "    morphology='Scnn1a_473845048_m.swc'\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Some of the parameters used to create the node are optional and only for our benefit. Others are necessary for when we will eventually run a simulation:\n",
    "* *cell_name* (optional) - Name/type of cell we will be modeling.\n",
    "* *potential* (optional) - Use to indicate that it is an excitatory type cell.\n",
    "* *model_type* - Used by the simulator to indicate that we are using a biophysical cell.\n",
    "* *dynamics_params* - Model parameters. File will be downloaded from the Allen Cell Types Database. \n",
    "* *morphology* - Model morphology. File will be downloaded from the Allen Cell Types Database.\n",
    "* *model_processing* - A custom function used by the simulator to load the model into NEURON using Allen Cell-Types files for perisomatic models\n",
    " \n",
    "#### Building and saving\n",
    "The final thing to do is to build and save the network. If successful, we should see a combination of hdf5 and csv files in the './network' directory, these files are used describe the network, and can be saved, stored and run at a later date.\n",
    "\n",
    "First, it's a good idea to remove any old files in the \"network\" folder so they don't interfere with the current simulation.\n",
    "\n",
    "```bash\n",
    "$ rm network \n",
    "```\n",
    " \n",
    "If you get the output:\n",
    "\n",
    "```rm: cannot remove network/*: No such file or directory```\n",
    "\n",
    "It's OK. Keep going."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.build()\n",
    "net.save_nodes(output_dir='sim_ch01/network')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Use the NetworkBuilder nodes() method to show that a node of our parameters was created"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'cell_name': 'Scnn1a_473845048', 'potental': 'exc', 'model_type': 'biophysical', 'model_template': 'ctdb:Biophys1.hoc', 'model_processing': 'aibs_perisomatic', 'dynamics_params': '472363762_fit.json', 'morphology': 'Scnn1a_473845048_m.swc', 'node_type_id': 100, 'node_id': 0}\n"
     ]
    }
   ],
   "source": [
    "for node in net.nodes():\n",
    "    print(node)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Setting up the simulator enviornment\n",
    "\n",
    "Now that the network has been built, we can use the BioNet simulator to setup and run it using NEURON. The easiest ways to do this is to copy and modify [existing simulation setup](https://github.com/AllenInstitute/bmtk/tree/develop/docs/examples/bio_14cells), or use the  ```bmtk.utils.sim_setup``` script which can be called from the command-line\n",
    "\n",
    "```bash\n",
    "$ python -m bmtk.utils.sim_setup  --report-vars v,cai         \\\n",
    "                                  --network network/          \\\n",
    "                                  --iclamp 0.12,500.0,1000.0  \\\n",
    "                                  --dt 0.1 --tstop 2000.0     \\\n",
    "                                  --include-examples          \\\n",
    "                                  --compile-mechanisms        \\\n",
    "                                  bionet sim_ch01\n",
    "```\n",
    "Or call the function directly in python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "config.json\n"
     ]
    }
   ],
   "source": [
    "from bmtk.utils.sim_setup import build_env_bionet\n",
    "\n",
    "build_env_bionet(\n",
    "    base_dir='sim_ch01',       # Where to save the scripts and config files \n",
    "    config_file='config.json', # Where main config will be saved.\n",
    "    network_dir='network',     # Location of directory containing network files\n",
    "    tstop=2000.0, dt=0.1,      # Run a simulation for 2000 ms at 0.1 ms intervals\n",
    "    report_vars=['v', 'cai'],  # Tells simulator we want to record membrane potential and calcium traces\n",
    "    current_clamp={            # Creates a step current from 500.ms to 1500.0 ms  \n",
    "        'amp': 0.120,\n",
    "        'delay': 500.0,\n",
    "        'duration': 1000.0\n",
    "    },\n",
    "    include_examples=True,    # Copies components files\n",
    "    compile_mechanisms=True   # Will try to compile NEURON mechanisms\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div class=\"alert alert-warning\">\n",
    "**Warning**: You may see an error that it failed while trying to compile the NEURON mechanisms - especially if jupyter is unable to find your NEURON environment path. If this happens you will need to manually compile the mechanisms:  \n",
    "\n",
    "```\n",
    " cd sim_ch01/components/mechanisms  \n",
    " nrnivmodl modfiles\n",
    "```\n",
    "</div>\n",
    "\n",
    "\n",
    "\n",
    "This will create the directory **sim_ch01** (feel free to give it a better name or set it to a different locations) with a number of new files and folders. Some of the more important ones include:\n",
    "* **circuit_config.json** - A configuration file that contains the location of the network files we created above. Plus location of neuron and synpatic models, templates, morphologies and mechanisms required to build our instantiate individual cell models.\n",
    "\n",
    "\n",
    "* **simulation_config.json** - contains information about the simulation. Including initial conditions and run-time configuration (_run_ and _conditions_). In the _inputs_ section we define what external sources we will use to drive the network (in this case a current clamp). And in the _reports_ section we define the variables (soma membrane potential and calcium) that will be recorded during the simulation \n",
    "\n",
    "\n",
    "* **components/biophysical_neuron_models/472363762_fit.json** - The parameter file for the cell we're modeling. Originally [downloaded from the Allen Cell Types Database](http://celltypes.brain-map.org/neuronal_model/download/482934212)\n",
    "\n",
    "\n",
    "* **components/biophysical_neuron_models/Scnn1a_473845048_m.swc** - The morphology file for our cell. Originally [downloaded from the Allen Cell Types Database](http://celltypes.brain-map.org/neuronal_model/download/482934212)\n",
    "\n",
    "\n",
    "Modifying these files with a text editor, or swapping out different parameter or morphologies, allows changing the simulation without extra programming."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Running the simulation\n",
    "\n",
    "Once our config file is setup we can run a simulation either through the command line:\n",
    "```bash\n",
    "$ python run_bionet.py simulation_config.json\n",
    "```\n",
    "\n",
    "or through the script"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-02-18 07:12:43,314 [INFO] Created log file\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:NEURONIOUtils:Created log file\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-02-18 07:12:43,380 [INFO] Building cells.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:NEURONIOUtils:Building cells.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-02-18 07:12:43,537 [INFO] Building recurrent connections\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:NEURONIOUtils:Building recurrent connections\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-02-18 07:12:43,550 [INFO] Running simulation for 2000.000 ms with the time step 0.100 ms\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:NEURONIOUtils:Running simulation for 2000.000 ms with the time step 0.100 ms\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-02-18 07:12:43,553 [INFO] Starting timestep: 0 at t_sim: 0.000 ms\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:NEURONIOUtils:Starting timestep: 0 at t_sim: 0.000 ms\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-02-18 07:12:43,556 [INFO] Block save every 5000 steps\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:NEURONIOUtils:Block save every 5000 steps\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-02-18 07:12:43,854 [INFO]     step:5000 t_sim:500.00 ms\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:NEURONIOUtils:    step:5000 t_sim:500.00 ms\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-02-18 07:12:44,163 [INFO]     step:10000 t_sim:1000.00 ms\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:NEURONIOUtils:    step:10000 t_sim:1000.00 ms\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-02-18 07:12:44,444 [INFO]     step:15000 t_sim:1500.00 ms\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:NEURONIOUtils:    step:15000 t_sim:1500.00 ms\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-02-18 07:12:44,779 [INFO]     step:20000 t_sim:2000.00 ms\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:NEURONIOUtils:    step:20000 t_sim:2000.00 ms\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-02-18 07:12:44,792 [INFO] Simulation completed in 1.242 seconds \n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:NEURONIOUtils:Simulation completed in 1.242 seconds \n"
     ]
    }
   ],
   "source": [
    "from bmtk.simulator import bionet\n",
    "\n",
    "\n",
    "conf = bionet.Config.from_json('sim_ch01/config.simulation.json')\n",
    "conf.build_env()\n",
    "net = bionet.BioNetwork.from_config(conf)\n",
    "sim = bionet.BioSimulator.from_config(conf, network=net)\n",
    "sim.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div class=\"alert alert-warning\">\n",
    "**Warning**: If you get the following error\n",
    "    argument not a density mechanism name.\n",
    "You will need to compile the NEURON mechanisms  \n",
    "\n",
    "\n",
    "```\n",
    " cd sim_ch01/components/mechanisms\n",
    " nrnivmodl modfiles\n",
    " \n",
    "```\n",
    "</div>\n",
    "\n",
    "A quick breakdown of the script:\n",
    "```python\n",
    "conf = config.from_json('config.json')\n",
    "io.setup_output_dir(conf)\n",
    "nrn.load_neuron_modules(conf)\n",
    "```\n",
    "This section loads the configuration file, it setups the output directory and files for writing during the simulation, and loads NEURON mechanisms needed by the cell model(s) during the simulation.\n",
    "\n",
    "```python\n",
    "net = bionet.BioNetwork.from_config(conf)\n",
    "```\n",
    "Creates a NEURON representation of the network, including cell models that have been converted into their NEURON equivelents.\n",
    "\n",
    "```python\n",
    "sim = Simulation.from_config(conf, network=net)\n",
    "sim.run()\n",
    "```\n",
    "Sets up and runs the NEURON simulation. When finished the output - spike times, membrane potential and Calcium influx, will be saved into the output directory as specified in the config.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Analyzing the run\n",
    "\n",
    "The results of the simulation are placed into various files as specified in the \"output\" section of the config file. We can change this before run-time if required.\n",
    "\n",
    "All simulations will save the spike times of the network cells. These are saved in csv format (output/spikes.txt) or hdf5 format(output/spikes.h5). To get the table of spike times for our single-cell network we can run the following method from the analyzer (gid 0 corresponds to our single cell)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>timestamps</th>\n",
       "      <th>node_ids</th>\n",
       "      <th>population</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>554.8</td>\n",
       "      <td>0</td>\n",
       "      <td>mcortex</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1379.5</td>\n",
       "      <td>0</td>\n",
       "      <td>mcortex</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1319.5</td>\n",
       "      <td>0</td>\n",
       "      <td>mcortex</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1259.5</td>\n",
       "      <td>0</td>\n",
       "      <td>mcortex</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1199.5</td>\n",
       "      <td>0</td>\n",
       "      <td>mcortex</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1139.7</td>\n",
       "      <td>0</td>\n",
       "      <td>mcortex</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1079.9</td>\n",
       "      <td>0</td>\n",
       "      <td>mcortex</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1020.4</td>\n",
       "      <td>0</td>\n",
       "      <td>mcortex</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>961.2</td>\n",
       "      <td>0</td>\n",
       "      <td>mcortex</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>902.6</td>\n",
       "      <td>0</td>\n",
       "      <td>mcortex</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>844.9</td>\n",
       "      <td>0</td>\n",
       "      <td>mcortex</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>788.7</td>\n",
       "      <td>0</td>\n",
       "      <td>mcortex</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>734.7</td>\n",
       "      <td>0</td>\n",
       "      <td>mcortex</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>683.9</td>\n",
       "      <td>0</td>\n",
       "      <td>mcortex</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>637.1</td>\n",
       "      <td>0</td>\n",
       "      <td>mcortex</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>594.4</td>\n",
       "      <td>0</td>\n",
       "      <td>mcortex</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>1439.6</td>\n",
       "      <td>0</td>\n",
       "      <td>mcortex</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>1499.7</td>\n",
       "      <td>0</td>\n",
       "      <td>mcortex</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    timestamps  node_ids population\n",
       "0        554.8         0    mcortex\n",
       "1       1379.5         0    mcortex\n",
       "2       1319.5         0    mcortex\n",
       "3       1259.5         0    mcortex\n",
       "4       1199.5         0    mcortex\n",
       "5       1139.7         0    mcortex\n",
       "6       1079.9         0    mcortex\n",
       "7       1020.4         0    mcortex\n",
       "8        961.2         0    mcortex\n",
       "9        902.6         0    mcortex\n",
       "10       844.9         0    mcortex\n",
       "11       788.7         0    mcortex\n",
       "12       734.7         0    mcortex\n",
       "13       683.9         0    mcortex\n",
       "14       637.1         0    mcortex\n",
       "15       594.4         0    mcortex\n",
       "16      1439.6         0    mcortex\n",
       "17      1499.7         0    mcortex"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from bmtk.analyzer.spike_trains import to_dataframe\n",
    "\n",
    "to_dataframe(config_file='sim_ch01/config.json')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When setting up the enviornment and config file we specified cell_vars=['v', 'cai']. This indicates to the simulator to also record membrane potential and calcium diffusion (and we can also specify other variables as long as they are supported in NEURON)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 24552 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 20928 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from bmtk.analyzer.compartment import plot_traces\n",
    "\n",
    "_ = plot_traces(config_file='sim_ch01/config.json', node_ids=[0], report_name='v_report')\n",
    "_ = plot_traces(config_file='sim_ch01/config.json', node_ids=[0], report_name='cai_report')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. Additional Information\n",
    "\n",
    "\n",
    "#### Changing run-time parameters.\n",
    "\n",
    "\n",
    "By making changes to the config file, we can change the conditions and simulation parameters without having to rebuild the network, modify parameter files, or change our run_bionet script. In fact we can iteratively run multiple simulations without any extra coding, only a text editor to change the json file.\n",
    "\n",
    "The run section of the config.json contains most of the parameters unique to the simulation:\n",
    "```json\n",
    "{\n",
    "  \"run\": {\n",
    "    \"dL\": 20,\n",
    "    \"nsteps_block\": 5000, \n",
    "    \"spike_threshold\": -15,\n",
    "    \"tstop\": 2000.0, \n",
    "    \"dt\": 0.1\n",
    "  }\n",
    "}\n",
    "```\n",
    "* tstop - simulation runtime in milliseconds.\n",
    "* dt - the time steps of the simulation. decreasing dt should increase accuracy of firing dynamics, but also increase time it takes to complete.\n",
    "* spike_threshold - used to determine when to count a action potential\n",
    "* dL - length of segments in a section. the number of segments will be forced to an odd number so the actual length may vary.\n",
    "\n",
    "Through the conditions section we can adjust simulation temperature (C) and the initial membrane potential of the cells:\n",
    "```json\n",
    "{\n",
    "  \"conditions\": {\n",
    "    \"celsius\": 34.0, \n",
    "    \"v_init\": -80\n",
    "  }\n",
    "}\n",
    "```\n",
    "\n",
    "And lastly, the input section lets us control stimulus onto the network. There are a number of different options which will be explained in the following tutorials. But even with a simple current injection we can adjust amplitude, delay and stimulation duration and measure the effect on the cell.\n",
    "```json\n",
    "{\n",
    "  \"inputs\": {\n",
    "    \"current_clamp\": {\n",
    "      \"input_type\": \"current_clamp\",\n",
    "      \"module\": \"IClamp\",\n",
    "      \"node_set\": \"all\",\n",
    "      \"amp\": 0.120,\n",
    "      \"delay\": 500.0,\n",
    "      \"duration\": 1000.0\n",
    "    }\n",
    "  }\n",
    "}\n",
    "```\n",
    "We can even add multiple injections\n",
    "```json\n",
    "{\n",
    "  \"inputs\": {\n",
    " \t\"cclamp1\": {\n",
    "      \"input_type\": \"current_clamp\",\n",
    "      \"module\": \"IClamp\",\n",
    "      \"node_set\": \"all\",\n",
    "      \"amp\": 0.150,\n",
    "      \"delay\": 0.0,\n",
    "      \"duration\": 400.0\n",
    "    },\n",
    "    \n",
    "    \"cclamp2\": {\n",
    "      \"input_type\": \"current_clamp\",\n",
    "      \"module\": \"IClamp\",\n",
    "      \"node_set\": \"all\",\n",
    "      \"amp\": 0.300,\n",
    "      \"delay\": 500.0,\n",
    "      \"duration\": 400.0\n",
    "    },\n",
    "    \n",
    "    \"cclamp3\": {\n",
    "      \"input_type\": \"current_clamp\",\n",
    "      \"module\": \"IClamp\",\n",
    "      \"node_set\": \"all\",\n",
    "      \"amp\": 0.450,\n",
    "      \"delay\": 1000.0,\n",
    "      \"duration\": 400.0\n",
    "    }\n",
    "  }\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Changing cell models\n",
    "When building the network we defined the cell model and morphology through the 'dynamics_params' and 'morphology_file' options. After building and saving the network, these values were saved in the node-types csv file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>node_type_id</th>\n",
       "      <th>model_type</th>\n",
       "      <th>model_processing</th>\n",
       "      <th>morphology</th>\n",
       "      <th>dynamics_params</th>\n",
       "      <th>cell_name</th>\n",
       "      <th>potental</th>\n",
       "      <th>model_template</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>100</td>\n",
       "      <td>biophysical</td>\n",
       "      <td>aibs_perisomatic</td>\n",
       "      <td>Scnn1a_473845048_m.swc</td>\n",
       "      <td>472363762_fit.json</td>\n",
       "      <td>Scnn1a_473845048</td>\n",
       "      <td>exc</td>\n",
       "      <td>ctdb:Biophys1.hoc</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   node_type_id   model_type  model_processing              morphology  \\\n",
       "0           100  biophysical  aibs_perisomatic  Scnn1a_473845048_m.swc   \n",
       "\n",
       "      dynamics_params         cell_name potental     model_template  \n",
       "0  472363762_fit.json  Scnn1a_473845048      exc  ctdb:Biophys1.hoc  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "pd.read_csv('network/mcortex_node_types.csv', sep=' ')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we want to run the simulation on a different cell model, all we have to do is \n",
    "1. Download new parameters.json and morphology.swc into components/biophysical\n",
    "2. Open mcortex_node_types.csv in a text editor and update 'morphology_file' and 'params_file' accordingly.\n",
    "\n",
    "In our simple one-cell example, it is likely faster to just rebuild the network. However the advantage of the use of the node types becomes clear once we start dealing with a larger network. For example we may have a network of hundreds of thousands of individual cells with tens of thousands of Scnn1a type cells. The process of adjusting/changing the Scnn1a parameter in the csv then starting another simulation only takes seconds, whereas rebuilding the entire network may take hours."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "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.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
