Skip to content
Snippets Groups Projects
1_RateML_ModGen.ipynb 5.16 KiB
Newer Older
Sandra Diaz's avatar
Sandra Diaz committed
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# RateML model generation\n",
    "\n",
    "### Using RateML to generate the Python or CUDA models"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Building rate based models in RateML, starts by creating an XML model file. To understand which constructs can be used to build the model, one should take a closer look at the README file. The cell below will prints the latest README file from the repository. Every construct which can be used, is explained."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# set root\n",
    "ROOT = \"/opt/app-root/src/drive/My Libraries/main/tvb-root/tvb_library/tvb/rateML/\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import Markdown, display, Code\n",
    "\n",
    "mdfile = open(ROOT + \"README.md\",\"r\")\n",
    "model = mdfile.read()\n",
    "display(Markdown(model))\n",
    "mdfile.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Generate a model\n",
    "After reading the README, one should be able to build an XML model file. Lets use the relatively small Kuramoto model as an example. \n",
    "Your model should look like similar to the Kuramoto python file and define some constants, an exposure and dynamics behavior. The dynamics for the Kuramoto consist of a state variable, a derived variable and a time derivative. Except for the derived variable, there are the construct that a RateML XML model file should contain. The template: tvb-root/scientific_library/tvb/rateML/XMLmodels/model_template.xml is an empty template which can be used to create a model XML file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Open the model template\n",
    "from IPython.display import Markdown, display, Code\n",
    "\n",
    "model_location = ROOT + \"XMLmodels/model_template.xml\"\n",
    "xmlfile = open(model_location,\"r\")\n",
    "model = xmlfile.read()\n",
    "display(Markdown(model))\n",
    "xmlfile.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Generating the model code\n",
    "\n",
    "We will use an existing xml model file to generate a model called \"inifite_theta\". This model relates the biophysically relevant macroscopic quantities, namely the firing rate and the membrane potential, which govern the evolution of the neuronal network. These equations describe exaclty all possible macroscopical dynamical states of the network. (Montbrió, E., Pazó, D., and Roxin, A. (2015). Macroscopic Description for Networks of Spiking Neurons. Phys. Rev. X 5, 1–15. doi:10.1103/PhysRevX. 5.021028). \n",
    "\n",
    "#### Please open: \"tvb-root/tvb_library/tvb/rateML/XMLmodels/montbrio.xml\"\n",
    "\n",
    "If you are satified with the pre set time derivates properties, save the file. \n",
    "\n",
    "We will call the templating function in order to automatically generate the model code.\n",
    "In XML2model.py the class \n",
    "```python\n",
    "RateML('model_filename', language=('python' | 'cuda'), 'path/to/your/XMLmodels', 'path/to/your/generatedModels')\n",
    "``` \n",
    "will start the code generation.\n",
    "\n",
    "If you get an error this might be that either the first 3 steps from the '0_Install_TVB.ipynb' need to be done or that you havent restarted this kernel. Kernel > Restart Kernel ..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-09-28 13:05:49,187 - INFO - tvb.rateML.XML2model - True validation of /opt/app-root/src/drive/My Libraries/main/tvb-root/tvb_library/tvb/rateML/XMLmodels/montbrio.xml against /opt/app-root/src/.local/lib/python3.8/site-packages/tvb/rateML/rML_v0.xsd\n",
      "2022-09-28 13:05:49,199 - INFO - tvb.rateML.XML2model - model file generated montbrio\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tvb.rateML.XML2model.RateML at 0x7f221c3639a0>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from tvb.rateML.XML2model import RateML\n",
    "\n",
    "# put here the name of the xml file\n",
    "# model_filename = 'oscillator'\n",
    "model_filename = 'montbrio'\n",
    "\n",
    "language = \"python\"\n",
    "# language = \"cuda\"\n",
    "XMLin = ROOT + \"XMLmodels\"\n",
    "GenModOut = ROOT + \"generatedModels\"\n",
    "RateML(model_filename, language, XMLin, GenModOut)"
   ]
  }
 ],
 "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.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}