Skip to content

update ipynb to more modern Python + NEURON style #3357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions docs/nmodl/transpiler/notebooks/nmodl-python-tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand All @@ -164,7 +164,7 @@
}
],
"source": [
"print(\"%.200s\" % modast) # only first 200 characters"
"print(f\"{modast:.200s}\") # only first 200 characters"
]
},
{
Expand Down Expand Up @@ -235,7 +235,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand All @@ -259,32 +259,33 @@
"units = lookup_visitor.lookup(modast, ast.AstNodeType.UNIT)\n",
"\n",
"if odes:\n",
" print(\"%d differential equation(s) exist : \" % len(odes))\n",
" print(f\"{len(odes)} differential equation(s) exist:\")\n",
" for ode in odes:\n",
" print(\"\\t %s \" % nmodl.to_nmodl(ode))\n",
" print(f\"\\t {nmodl.to_nmodl(ode)}\")\n",
"\n",
"if primes:\n",
" print(\"%d prime variables exist :\" % len(primes), end=\"\")\n",
" print(f\"{len(primes)} prime variables exist:\", end=\"\")\n",
" for prime in primes:\n",
" print(\" %s\" % nmodl.to_nmodl(prime), end=\"\")\n",
" print(f\" {nmodl.to_nmodl(prime)}\", end=\"\")\n",
" print()\n",
"\n",
"if range_vars:\n",
" print(\"%d range variables exist :\" % len(range_vars), end=\"\")\n",
" print(f\"{len(range_vars)} range variables exist:\", end=\"\")\n",
" for range_var in range_vars:\n",
" print(\" %s\" % nmodl.to_nmodl(range_var), end=\"\")\n",
" print(f\" {nmodl.to_nmodl(range_var)}\", end=\"\")\n",
" print()\n",
"\n",
"if parameters:\n",
" print(\"%d parameter variables exist :\" % len(parameters), end=\"\")\n",
" for range_var in range_vars:\n",
" print(\" %s\" % nmodl.to_nmodl(range_var), end=\"\")\n",
" print(f\"{len(parameters)} parameter variables exist:\", end=\"\")\n",
" for parameter in parameters:\n",
" print(f\" {nmodl.to_nmodl(parameter)}\", end=\"\")\n",
" print()\n",
"\n",
"if units:\n",
" print(\"%d units uses :\" % len(units), end=\"\")\n",
" print(f\"{len(units)} units uses:\", end=\"\")\n",
" for unit in units:\n",
" print(\" %s\" % nmodl.to_nmodl(unit), end=\"\")"
" print(f\" {nmodl.to_nmodl(unit)}\", end=\"\")\n",
" print()"
]
},
{
Expand Down Expand Up @@ -747,7 +748,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -782,15 +783,12 @@
"\n",
"ont_statements = lookup_visitor.lookup(modast, ast.AstNodeType.ONTOLOGY_STATEMENT)\n",
"ions = lookup_visitor.lookup(modast, ast.AstNodeType.USEION)\n",
"\n",
"for statement in ont_statements:\n",
" print(\n",
" \" %s (%s)\" % (nmodl.to_nmodl(statement), nmodl.to_nmodl(statement.ontology_id))\n",
" )\n",
" print(f\" {nmodl.to_nmodl(statement)} ({nmodl.to_nmodl(statement.ontology_id)})\")\n",
"\n",
"for ion in ions:\n",
" o_id = nmodl.to_nmodl(ion.ontology_id) if ion.ontology_id else \"\"\n",
" print(\" %s (%s)\" % (nmodl.to_nmodl(ion), o_id))"
" print(f\" {nmodl.to_nmodl(ion)} ({o_id})\")\n"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"\n",
"h.load_file(\"stdrun.hoc\")\n",
"\n",
"soma = h.Section(name=\"soma\")\n",
"soma = h.Section(\"soma\")\n",
"cyt = rxd.Region([soma], name=\"cyt\", nrn_region=\"i\")\n",
"u = rxd.Species(cyt, name=\"u\")\n",
"α = rxd.Parameter(cyt, value=0.3 * mM)\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@
"metadata": {},
"outputs": [],
"source": [
"left = h.Section(name=\"left\")\n",
"right = h.Section(name=\"right\")\n",
"left = h.Section(\"left\")\n",
"right = h.Section(\"right\")\n",
"\n",
"left.nseg = right.nseg = 101\n",
"left.L = right.L = 101\n",
Expand Down Expand Up @@ -271,7 +271,7 @@
"def plot(fig):\n",
" y = protein.nodes.concentration\n",
" x = [h.distance(node, left(0)) for node in protein.nodes]\n",
" fig.add_trace(go.Scatter(x=x, y=y, name=f\"t = {h.t:g} ms\"))"
" fig.add_trace(go.Scatter(x=x, y=y, name=f\"t = {h.t} ms\"))"
]
},
{
Expand Down
6 changes: 3 additions & 3 deletions docs/rxd-tutorials/basic-initialization.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"from neuron import h, rxd\n",
"from neuron.units import mV\n",
"\n",
"soma = h.Section(name=\"soma\")\n",
"soma = h.Section(\"soma\")\n",
"cyt = rxd.Region(soma.wholetree(), name=\"cyt\", nrn_region=\"i\")\n",
"\n",
"ca = rxd.Species(cyt, name=\"ca\", charge=2, atolscale=1e-6)\n",
Expand Down Expand Up @@ -90,7 +90,7 @@
"from neuron import h, rxd\n",
"from neuron.units import mV\n",
"\n",
"soma = h.Section(name=\"soma\")\n",
"soma = h.Section(\"soma\")\n",
"cyt = rxd.Region(soma.wholetree(), name=\"cyt\")\n",
"\n",
"ca = rxd.Species(cyt, name=\"ca\", charge=2)\n",
Expand Down Expand Up @@ -187,7 +187,7 @@
"from neuron import h, rxd\n",
"from neuron.units import mV\n",
"\n",
"soma = h.Section(name=\"soma\")\n",
"soma = h.Section(\"soma\")\n",
"\n",
"cyt = rxd.Region([soma], name=\"cyt\")\n",
"m = rxd.State(cyt, initial=0.47)\n",
Expand Down
10 changes: 5 additions & 5 deletions docs/rxd-tutorials/calcium waves.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"metadata": {},
"outputs": [],
"source": [
"sec = h.Section(name=\"sec\")\n",
"sec = h.Section(\"sec\")\n",
"sec.L = 101\n",
"sec.diam = 1\n",
"sec.nseg = 101"
Expand Down Expand Up @@ -181,7 +181,7 @@
"outputs": [],
"source": [
"cyt = rxd.Region(\n",
" h.allsec(),\n",
" [sec],\n",
" nrn_region=\"i\",\n",
" geometry=rxd.FractionalVolume(fc, surface_fraction=1),\n",
" name=\"cyt\",\n",
Expand Down Expand Up @@ -211,7 +211,7 @@
"metadata": {},
"outputs": [],
"source": [
"er = rxd.Region(h.allsec(), geometry=rxd.FractionalVolume(fe), name=\"er\")"
"er = rxd.Region([sec], geometry=rxd.FractionalVolume(fe), name=\"er\")"
]
},
{
Expand All @@ -228,7 +228,7 @@
"outputs": [],
"source": [
"cyt_er_membrane = rxd.Region(\n",
" h.allsec(), geometry=rxd.DistributedBoundary(2), name=\"cyt_er_membrane\"\n",
" [sec], geometry=rxd.DistributedBoundary(2), name=\"cyt_er_membrane\"\n",
")"
]
},
Expand Down Expand Up @@ -273,7 +273,7 @@
" <li><code>name</code> assigns a name to the Species that syncs with the rest of NEURON (NMODL, GUI tools, variables of the form e.g. <code>cai</code>, etc)</li>\n",
" <li><code>charge</code> refers to the charge of the Species; e.g. a calcium ion would have charge 2</li>\n",
" <li><code>initial</code> refers to the initial concentration of the Species. If <code>initial</code> is not specified and the Species has a name and exists on an rxd.Region with <code>nrn_region</code> set, then NEURON's default initialization rules apply; otherwise the default is 0.</li>\n",
" <li><code>atolscale</code> indicates a tolerance scale factor for variable step integration and is especially important for calcium and other substances with a low concentration</li>\n",
" <li><code>atolscale</code> indicates a tolerance scale factor for variable step integration and is especially important for calcium and other substances with a low concentration. (For calcium, consider an atolscale around 1e-6.)</li>\n",
"</ul>"
]
},
Expand Down
2 changes: 1 addition & 1 deletion docs/rxd-tutorials/circadian.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
"metadata": {},
"outputs": [],
"source": [
"cell = h.Section(name=\"cell\")\n",
"cell = h.Section(\"cell\")\n",
"cell.diam = cell.L = 5\n",
"r = rxd.Region([cell], nrn_region=\"i\")"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
"outputs": [],
"source": [
"## define morphology\n",
"soma = h.Section(name=\"soma\")\n",
"soma = h.Section(\"soma\")\n",
"soma.L = soma.diam = 10 * um\n",
"\n",
"## add ion channels (h.hh is built in, so always available)\n",
Expand Down
8 changes: 4 additions & 4 deletions docs/rxd-tutorials/ip3-demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"metadata": {},
"outputs": [],
"source": [
"dend = h.Section(name=\"dend\")\n",
"dend = h.Section(\"dend\")\n",
"dend.L = 100\n",
"dend.nseg = 101"
]
Expand Down Expand Up @@ -88,7 +88,7 @@
"while dend(0.7).ip3i < 100 * nM:\n",
" h.fadvance()\n",
"\n",
"print(\"crossed 100 nM at t = {} ms\".format(h.t))"
"print(f\"crossed 100 nM at t = {h.t} ms\")"
]
},
{
Expand All @@ -105,8 +105,8 @@
"h.finitialize(-65)\n",
"h.continuerun(1000)\n",
"max_ip3 = max(ip3_vec)\n",
"print(\"peak ip3 =\", max_ip3)\n",
"print(\"final ip3 =\", dend(0.7).ip3i)"
"print(f\"peak ip3 = {max_ip3}\")\n",
"print(f\"final ip3 = {dend(0.7).ip3i}\")"
]
}
],
Expand Down
12 changes: 6 additions & 6 deletions docs/rxd-tutorials/thresholds.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
"outputs": [],
"source": [
"from matplotlib import pyplot as plt\n",
"import numpy\n",
"import numpy as np\n",
"\n",
"x = numpy.linspace(-5, 5)\n",
"y = numpy.tanh(x)\n",
"x = np.linspace(-5, 5)\n",
"y = np.tanh(x)\n",
"plt.grid()\n",
"plt.plot(x, y)\n",
"plt.xlabel(\"x\")\n",
Expand Down Expand Up @@ -78,8 +78,8 @@
"metadata": {},
"outputs": [],
"source": [
"x = numpy.linspace(0, 4, 1000)\n",
"y = (1 + numpy.tanh(2 * 10 * (x - 2))) / 2\n",
"x = np.linspace(0, 4, 1000)\n",
"y = (1 + np.tanh(2 * 10 * (x - 2))) / 2\n",
"plt.grid()\n",
"plt.plot(x, y)\n",
"plt.xlabel(\"x\")\n",
Expand Down Expand Up @@ -113,7 +113,7 @@
"\n",
"h.load_file(\"stdrun.hoc\")\n",
"\n",
"soma = h.Section(name=\"soma\")\n",
"soma = h.Section(\"soma\")\n",
"cyt = rxd.Region([soma], name=\"cyt\", nrn_region=\"i\")\n",
"ip3 = rxd.Species(cyt, name=\"ip3\", charge=0, initial=1 * mM)\n",
"k = 2 # degradation rate\n",
Expand Down
Loading
Loading