File size: 6,772 Bytes
0558c72 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | {
"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='<b> Membrane Capacitance, \\\\(\\\\mu{F}/cm^2\\\\)</b>'),)), 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
}
|