{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# NeuroML model (HH) in Jupyter Notebook " ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "## *Source path and filenames for NeuroML Model*" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "import ipywidgets\n", "import ui_widget\n", "from importlib.machinery import SourceFileLoader\n", "%matplotlib widget\n", "\n", "#path for source directory to read, write and execute NeuroML--------------------------------------------#\n", "path2source = \"../../Tutorial/Source/\"\n", "\n", "#filenames for NeuroML file under above direcotry--------------------------------------------------------#\n", "fname_cellNML = \"hhcell.cell.nml\"\n", "fname_netNML = \"HHCellNetwork.net.nml\"\n", "fname_LEMS = \"LEMS_HH_Simulation.xml\"\n", "fname_NML_output = \"hh_forJupyterNotebook.dat\"\n", "\n", "# imports the module from the given path-----------------------------------------------------------------#\n", "nmlPython = SourceFileLoader(\"nml2jupyter_ver1.py\",\"nml2jupyter_ver1.py\").load_module()\n", "runner = nmlPython.nml2jupyter(path2source, fname_cellNML, fname_netNML, fname_LEMS, fname_NML_output)" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "## *Functions from python class to interact with NeuroML Model*" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "#function to write inputs for NeuroML--------------------------------------------------------------------#\n", "def writeInputs(C_m, g_Na, g_K, g_L, E_Na, E_K, E_L, t_0, t_n, delta_t, I_inj_max, I_inj_width, I_inj_trans):\n", " ui_widget.highlight_slider()\n", " runner.writeNMLinputFile(C_m, g_Na, g_K, g_L, E_Na, E_K, E_L, t_0, t_n, delta_t, I_inj_max, I_inj_width, I_inj_trans)\n", " #write nml files using slider inputs\n", " \n", "#function to run NeuroML with given inputs and plot results----------------------------------------------#\n", "def runNMLmodel(b):\n", " out_log.clear_output()\n", " out_plot.clear_output()\n", " #run neuroML (write output in dat file)\n", " with out_log:\n", " LEMSoption=\" -nogui\"\n", " LEMS=os.path.join(path2source, fname_LEMS, LEMSoption)\n", " !pynml $LEMS\n", " \n", " #load dat file and plot\n", " with out_plot:\n", " runner.plotData()" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "## *Set-up widgets and interact with NeuroML Model via Python Class*" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "b76dadc5fd8d4aac8d517b238f523d73", "version_major": 2, "version_minor": 0 }, "text/plain": [ "VBox(children=(HBox(children=(HTMLMath(value=' Membrane Capacitance, \\\\(\\\\mu{F}/cm^2\\\\)'),)), HBox(chil…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "e4fefa0743aa45058f6486b1f4e0d17e", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Output()" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "3861387971ea44169924387d1abc959c", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Button(button_style='success', description='Run NeuroML', style=ButtonStyle(), tooltip='Execute NeuroML Model …" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "6821aaf620de4a5d95907b9ef027537d", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Output(layout=Layout(border='1px solid'))" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "71d8dac020534a6d956ad1b2f944af2f", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Output()" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#define and connect run button for NeuroML-----------------------------------------------------------------#\n", "ui_widget.run_button.on_click(runNMLmodel)\n", "out_log = ipywidgets.Output(layout={'border': '1px solid'}) #for displaying output log from NeuroMl execution\n", "out_plot = ipywidgets.Output() #for displaying plots \n", "\n", "#create input widget and call writeInputs to generate/update NeuroML files---------------------------------#\n", "wid_inputs=ipywidgets.interactive_output(writeInputs,{'C_m':ui_widget.slider_capacitance,\n", " 'g_Na':ui_widget.slider_cond_Na, 'g_K':ui_widget.slider_cond_K, 'g_L':ui_widget.slider_cond_L, \n", " 'E_Na':ui_widget.slider_pot_Na, 'E_K':ui_widget.slider_pot_K, 'E_L':ui_widget.slider_pot_L,\n", " 't_0':ui_widget.time_start, 't_n':ui_widget.time_end, 'delta_t':ui_widget.time_step, \n", " 'I_inj_max':ui_widget.slider_amplitude,'I_inj_width':ui_widget.slider_width,'I_inj_trans':ui_widget.slider_translation})\n", "\n", "#display all the widgets and button outputs----------------------------------------------------------------#\n", "display(ui_widget.modelInputs,wid_inputs,ui_widget.run_button,out_log,out_plot)" ] }, { "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.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }